-
Notifications
You must be signed in to change notification settings - Fork 4
/
CloverSort.c
285 lines (240 loc) · 9.02 KB
/
CloverSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* Sort code for HAGAR, the NaI gamma detector
*
* Author: Philip Adsley
*
* This code also functions as a template for gamma sort codes for K600 experiments.
*
*
*/
#include "GammaData.h"
#include "CloverSort.h"
#include <math.h>
#include <stdlib.h>
#include <TRandom3.h>
#include <vector>
extern int ADCModules;
extern int ADCsize;
extern int NumberOfClover;
extern int **CloverADCChannelLimits;
extern int **CloverTDCChannelLimits;
extern double *ADCOffsets;
extern double *ADCGains;
extern double **ADCCalibrationParameters;
TRandom3 *randy = new TRandom3(0);
int CountGammaHits[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//GammaData *CloverSort(float *ADC_import, int ntdc, int *TDC_channel_import, float *TDC_value_import)
void CloverSort(float *ADC_import, int ntdc, int *TDC_channel_import, float *TDC_value_import, GammaData *gammy)
{
//GammaData *gammy = new GammaData();
//Loop over ADC and TDC events and do the following:
//Check whether there are front-back coincidences for a detector and test the energies
//Check to see whether there's a TDC event for the same channel as the front hit ADC - TDCs are apparently in single-hit mode. Don't need to worry about multihits
//Calculate the theta and phi for the valid events
multiTDC *mTDC = new multiTDC;
mTDC->multiTDCSort(ntdc, TDC_channel_import, TDC_value_import);
//printf("mTDC->GetSize(): %d\n",mTDC->GetSize());
double *EnergyAddback = new double[NumberOfClover]; // Energy of the Addback
int *EventAddback = new int[NumberOfClover]; // Event number in gammy in which the maximum energy deposition took place (per clover)
//int *SegmentAddback = new int[NumberOfClover]; // Segment with maximum Energy deposit in the Clover (considered the first segment hit)
double *MaxEnerAddback = new double[NumberOfClover]; // Maximum energy deposit in a segment for each clover
int *NSegmentsAddback = new int[NumberOfClover]; // Number of crystals contributing to the addback.
for (int i=0; i<NumberOfClover;i++)
{
EnergyAddback[i] = 0.;
EventAddback[i] = -1;
// SegmentAddback[i] = -1;
MaxEnerAddback[i] = 0.;
NSegmentsAddback[i] = 0;
}
for(int k=0;k<mTDC->GetSize();k++)//Loop over all of the TDC values - there should only be a small number of these relative to the ADC values
{CountGammaHits[0]++;
if(CloverTDCTest(mTDC->GetChannel(k)))
{CountGammaHits[1]++;
int DetNum = CloverTDCIdentifyDetector(mTDC->GetChannel(k));
// printf("+++++++++++++++++++DetNum %d\n",DetNum);
int Segm = CloverTDCIdentifySegment(mTDC->GetChannel(k));
// printf("-------------Segm %d\n",Segm);
if(DetNum>0)
{ //printf("Test1\n");
CountGammaHits[2]++;
for(int i=CloverADCChannelLimits[DetNum-1][0];i<=CloverADCChannelLimits[DetNum-1][1];i++)
{//printf("Test2\n");
CountGammaHits[3]++;
if(CloverADCTDCChannelCheck(i,mTDC->GetChannel(k)))
{//printf("Test3\n");
CountGammaHits[4]++;
// printf("line 55");
//printf("ADCChannel: %d \t TDCChannel: %d\n",i,mTDC->GetChannel(k));
double GammaEnergy = CloverEnergyCalc(i,ADC_import[i]);
if(GammaEnergy>0.1)
{
//printf("Test4\n");
CountGammaHits[5]++;
gammy->SetEnergy(GammaEnergy);
gammy->SetTime(mTDC->GetValue(k));
gammy->SetDetectorType("Clover");
gammy->SetDetectorLabel(DetNum);
EnergyAddback[DetNum-1] += GammaEnergy;
NSegmentsAddback[DetNum-1]++;
if (GammaEnergy > MaxEnerAddback[DetNum-1])
{
EventAddback[DetNum-1] = gammy->SizeOfEvent()-1;
MaxEnerAddback[DetNum-1] = GammaEnergy;
// printf("In. Size %d, Det %d, EvtAddBack%d,\n", gammy->SizeOfEvent(), DetNum, EventAddback[DetNum-1]);
}
gammy->SetDetectorSegm(Segm);
gammy->SetGammaRawADC(ADC_import[i]);
gammy->SetGammaADCChannel(i);
gammy->SetGammaTDCChannel(mTDC->GetChannel(k));
gammy->SetGammaTDCMultiplicity(mTDC->GetMult(k));
/*
printf("GetSize: %d;\n",gammy->SizeOfEvent());
printf("EventN: %d; \n",EventAddback[DetNum-1]);
printf("GammaEnergy: %f, gammyEnergy: %f \n",GammaEnergy,gammy->GetEnergy(EventAddback[DetNum-1]));
printf("GammaEnergy: %f, gammyEnergy: %f \n",GammaEnergy,gammy->GetEnergy(gammy->SizeOfEvent()));
printf("Segm: %d, gammySegm: %d \n", Segm, gammy->GetDetectorSegm(EventAddback[DetNum-1]));
*/
}
}
}
}
}
}
for(int i=0;i<NumberOfClover;i++)
{
if(EventAddback[i] > -1)
{
gammy->SetEnergy(EnergyAddback[i]);
gammy->SetDetectorType("Addback");
gammy->SetDetectorLabel(i+1);
// printf("gammy DetNum: %d, index DetNum: %d;\n",gammy->GetDetectorLabel(EventAddback[i]),i+1);
gammy->SetDetectorSegm(gammy->GetDetectorSegm(EventAddback[i]));
gammy->SetGammaRawADC(gammy->GetGammaRawADC(EventAddback[i]));
gammy->SetGammaADCChannel(gammy->GetGammaADCChannel(EventAddback[i]));
gammy->SetGammaTDCChannel(gammy->GetGammaTDCChannel(EventAddback[i]));
//gammy->SetGammaTDCMultiplicity(gammy->GetGammaTDCMultiplicity(EventAddback[i]));
gammy->SetGammaTDCMultiplicity(NSegmentsAddback[i]);
}
}
//for(int i=0;i<6;i++)printf("CountGammaHits[%d]: %d\n",i,CountGammaHits[i]);
gammy->SetHits(gammy->SizeOfEvent());
mTDC->ClearEvent();
delete mTDC;
delete EnergyAddback;
delete MaxEnerAddback;
//delete SegmentAddback;
delete EventAddback;
delete NSegmentsAddback;
//return gammy;
}
double CloverThetaCalc(int Channel)
{
return 0;
}
double CloverPhiCalc(int Channel)
{
return 0;
}
//function to identify the clover number
int CloverTDCIdentifyDetector(int TDCChannel)
{
int testresult = -1;
int result=0;
for(int i=0;i<NumberOfClover;i++)
{
testresult = -1;
if(TDCChannel>=CloverTDCChannelLimits[i][0] && TDCChannel<=CloverTDCChannelLimits[i][1])
{
testresult = i+1;
}
if(testresult>0) result=testresult;
// printf("CloverTDCChannelLimits: %d CloverTDCChannelLimits: %d \n TDC Channel: %d\n Clovernumber: %d\n ",CloverTDCChannelLimits[i][0], CloverTDCChannelLimits[i][1], TDCChannel,i);
// printf("testresult = %d\n",testresult);
}
// printf("result = %d\n",result);
return result;
}
//function to identify the segment number
int CloverTDCIdentifySegment(int TDCChannel)
{
int testresult = -1;
int result=0;
for(int i=0;i<NumberOfClover;i++)
{
testresult = -1;
if(TDCChannel>=CloverTDCChannelLimits[i][0] && TDCChannel<=CloverTDCChannelLimits[i][1])
{
testresult = (TDCChannel-CloverTDCChannelLimits[i][0])+1;
}
if(testresult>0) result=testresult;
// printf("CloverTDCChannelLimits: %d CloverTDCChannelLimits: %d \n TDC Channel: %d\n Clovernumber: %d\n ",CloverTDCChannelLimits[i][0], CloverTDCChannelLimits[i][1], TDCChannel,i);
// printf("testresult = %d\n",testresult);;
}
// printf("+++++++++++++result = %d\n",result);
return result;
}
double CloverEnergyCalc(int Channel, double ADCValue)
{
double randNum = randy->Rndm();
double RandyADCValue = ADCValue+randNum;
int npars = ADCCalibrationParameters[Channel][0];
double result = 0;
for(int i=1;i<npars+1;i++)
{
result += ADCCalibrationParameters[Channel][i] * pow(RandyADCValue,(double)i-1.);
}
return result;
}
bool CloverTDCTest(int TDCChannel) // to check if the TDCChannel is the one associated with the Clovers
{
//printf("TDCChannel: %d\n",TDCChannel);
bool result = false;
for(int i=0;i<NumberOfClover;i++)
{
if(TDCChannel>=CloverTDCChannelLimits[i][0] && TDCChannel<=CloverTDCChannelLimits[i][1])
{
result = true;
}
if(CloverTDCChannelLimits[i][0]==-1)result = true;
if(CloverTDCChannelLimits[i][1]==-1)result = true;
}
//if(result)printf("True\n");
// else printf("False\n");
return result;
}
bool CloverADCTDCChannelCheck(int ADCChannel, int TDCChannel) // to consider only the TDC/ADC channel associated with a Clover
{
bool result = false;
// printf("ADCChannel: %d \t TDC Channel: %d\n",ADCChannel, TDCChannel);
for(int i=0;i<NumberOfClover;i++)
{
if(ADCChannel>=CloverADCChannelLimits[i][0] && ADCChannel<=CloverADCChannelLimits[i][1] && TDCChannel>=CloverTDCChannelLimits[i][0] && TDCChannel<=CloverTDCChannelLimits[i][1])//Check to see if the ADC/TDC events are in the same detector
{
if(ADCChannel-CloverADCChannelLimits[i][0]==TDCChannel-CloverTDCChannelLimits[i][0])
{
// printf("Correlation! ADCChannel: %d \t TDC Channel: %d\n",ADCChannel, TDCChannel);
result = true;
}
}
if(CloverADCChannelLimits[i][0]==-1)result = true;//If there is no information for some of the MMMs, suppress this test
if(CloverADCChannelLimits[i][1]==-1)result = true;
if(CloverTDCChannelLimits[i][0]==-1)result = true;
if(CloverTDCChannelLimits[i][1]==-1)result = true;
}
return result;
}
/*
int CloverDetHitNumber(int ADCChannel) //number of hit in one Clover
{
int result = 0;
for(int i=0;i<NumberOfClover;i++)
{
if(ADCChannel>=CloverADCChannelLimits[i][0] && ADCChannel<=CloverADCChannelLimits[i][1])
{
result = i+1;
}
}
printf("result = %d\n",result);
return result;
}
*/