-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairbuilder6.cpp
More file actions
455 lines (412 loc) · 16.7 KB
/
pairbuilder6.cpp
File metadata and controls
455 lines (412 loc) · 16.7 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/****************************************************************************************
* DANSS data analysis - build time correlated pairs and random pairs *
****************************************************************************************/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "Riostream.h"
#include "TROOT.h"
#include "TMath.h"
#include "TFile.h"
#include "TChain.h"
#include "TNetFile.h"
#include "TRandom.h"
#include "TTree.h"
#include "TBranch.h"
#include "TCanvas.h"
#include "TPostScript.h"
#include "TStyle.h"
#include "TClonesArray.h"
#include "TStopwatch.h"
#include "TTreeCacheUnzip.h"
#include "TRandom.h"
#include "TDirectory.h"
#include "TProcessID.h"
#include "TObject.h"
#include "TClonesArray.h"
#include "TRefArray.h"
#include "TRef.h"
#include "TKey.h"
#include "TGraph.h"
#include "TF1.h"
#include "TH1.h"
#include "TH2.h"
#include "evtbuilder.h"
#define GFREQ2US (GLOBALFREQ / 1000000.0)
#define MAXTDIFF 50.0 // us
#define MINPOSE 1.0 // MeV
#define MAXPOSE 20.0 // MeV
#define AGAMMAN 0 // number of annihilation gamma hits (0 no requirement)
#define MINNEUTE 3.0 // MeV
#define MAXNEUTE 15.0 // MeV
#define NEUTN 3 // number of hits
#define MINVETOE 4.0 // MeV
#define VETON 2 // number of hits
#define DANSSVETOE 20.0 // Make veto if VETO counters are silent from Pmt or SiPM
#define RSHIFT 5000.0 // us
#define NRANDOM 16 // increase random statistics
#define ATTENUATION 0.00342 // Signal attenuation for positron energy correction
#define CORR_P0 0.179 // Positron energy correction from MC
#define CORR_P1 0.929 // Positron energy correction from MC
#define CORR_PMT_P0 0.165 // Positron energy correction from MC
#define CORR_PMT_P1 0.929 // Positron energy correction from MC
#define CORR_SIPM_P0 0.153 // Positron energy correction from MC
#define CORR_SIPM_P1 0.920 // Positron energy correction from MC
#define SHOWERMIN 800 // 800 MeV shower event threshold
#define iMaxDataElements 3000
struct HitStruct {
float E[iMaxDataElements];
float T[iMaxDataElements];
struct HitTypeStruct type[iMaxDataElements];
};
// Correction based on the neutron position if this was not done before based on the positron position
void acorr(struct DanssPairStruct6 *DanssPair) {
double C;
if (DanssPair->PositronX[0] < 0 && DanssPair->NeutronX[0] >= 0) {
C = exp(ATTENUATION * (DanssPair->NeutronX[0] - 50.0));
} else if (DanssPair->PositronX[1] < 0 && DanssPair->NeutronX[1] >= 0) {
C = exp(ATTENUATION * (DanssPair->NeutronX[1] - 50.0));
} else {
C = 1.0;
}
DanssPair->PositronEnergy *= C;
DanssPair->PositronSiPmEnergy *= C;
DanssPair->PositronPmtEnergy *= C;
}
void CopyHits(struct HitStruct *to, struct HitStruct *from, int N)
{
memcpy(to->E, from->E, N * sizeof(float));
memcpy(to->T, from->T, N * sizeof(float));
memcpy(to->type, from->type, N * sizeof(struct HitTypeStruct));
}
int IsNeutron(struct DanssEventStruct5 *DanssEvent)
{
float E;
int rc;
E = (DanssEvent->SiPmCleanEnergy + DanssEvent->PmtCleanEnergy) / 2;
rc = (E >= MINNEUTE && E < MAXNEUTE && DanssEvent->SiPmCleanHits >= NEUTN);
return rc;
}
int IsPositron(struct DanssEventStruct5 *DanssEvent)
{
float E;
int rc;
E = DanssEvent->PositronEnergy;
rc = (E >= MINPOSE && E < MAXPOSE && DanssEvent->AnnihilationGammas >= AGAMMAN);
return rc;
}
int IsVeto(struct DanssEventStruct5 *Event)
{
if (Event->VetoCleanEnergy > MINVETOE || Event->VetoCleanHits >= VETON || Event->PmtCleanEnergy + Event->SiPmCleanEnergy > 2*DANSSVETOE) return 1;
return 0;
}
int IsShower(struct DanssEventStruct5 *Event)
{
if (Event->PmtCleanEnergy + Event->SiPmCleanEnergy > 2*SHOWERMIN) return 1;
return 0;
}
void MakePair(
struct DanssEventStruct5 *DanssEvent, // Neutron
struct DanssEventStruct5 *SavedEvent, // Positron
struct DanssEventStruct5 *VetoEvent, // Veto
struct DanssEventStruct5 *ShowerEvent, // Shower
struct DanssPairStruct6 *DanssPair)
{
double tmp;
int i;
memset(DanssPair, 0, sizeof(struct DanssPairStruct6));
DanssPair->number[0] = SavedEvent->number;
DanssPair->number[1] = DanssEvent->number;
DanssPair->unixTime = DanssEvent->unixTime;
DanssPair->SiPmCleanEnergy[0] = SavedEvent->SiPmCleanEnergy;
DanssPair->PmtCleanEnergy[0] = SavedEvent->PmtCleanEnergy;
DanssPair->SiPmCleanEnergy[1] = DanssEvent->SiPmCleanEnergy;
DanssPair->PmtCleanEnergy[1] = DanssEvent->PmtCleanEnergy;
DanssPair->PositronFlags[0] = SavedEvent->PositronFlags;
DanssPair->PositronFlags[1] = DanssEvent->PositronFlags;
DanssPair->PositronHits = SavedEvent->PositronHits;
DanssPair->PositronEnergy = SavedEvent->PositronEnergy;
DanssPair->PositronMinLen = SavedEvent->PositronMinLen;
memcpy(DanssPair->PositronX, SavedEvent->PositronX, sizeof(SavedEvent->PositronX));
DanssPair->TotalEnergy = SavedEvent->TotalEnergy;
DanssPair->PositronSiPmEnergy = SavedEvent->PositronSiPmEnergy;
DanssPair->PositronPmtEnergy = SavedEvent->PositronPmtEnergy;
DanssPair->AnnihilationGammas = SavedEvent->AnnihilationGammas;
DanssPair->AnnihilationEnergy = SavedEvent->AnnihilationEnergy;
DanssPair->AnnihilationMax = SavedEvent->AnnihilationMax;
DanssPair->NeutronHits = DanssEvent->SiPmCleanHits;
DanssPair->NeutronEnergy = (DanssEvent->SiPmCleanEnergy + DanssEvent->PmtCleanEnergy) / 2;
memcpy(DanssPair->NeutronX, DanssEvent->NeutronX, sizeof(DanssEvent->NeutronX));
DanssPair->gtDiff = (DanssEvent->globalTime - SavedEvent->globalTime) / GFREQ2US;
tmp = (DanssEvent->NeutronX[2] - SavedEvent->PositronX[2]) * (DanssEvent->NeutronX[2] - SavedEvent->PositronX[2]);
for (i=0; i<2; i++) if (DanssEvent->NeutronX[i] >= 0 && SavedEvent->PositronX[i] >= 0)
tmp += (DanssEvent->NeutronX[i] - SavedEvent->PositronX[i]) * (DanssEvent->NeutronX[i] - SavedEvent->PositronX[i]);
DanssPair->Distance = sqrt(tmp);
DanssPair->DistanceZ = DanssEvent->NeutronX[2] - SavedEvent->PositronX[2];
DanssPair->gtFromVeto = (SavedEvent->globalTime - VetoEvent->globalTime) / GFREQ2US;
DanssPair->VetoHits = VetoEvent->VetoCleanHits;
DanssPair->VetoEnergy = VetoEvent->VetoCleanEnergy;
DanssPair->DanssEnergy = (VetoEvent->SiPmCleanEnergy + VetoEvent->PmtCleanEnergy) / 2;
DanssPair->gtFromShower = (SavedEvent->globalTime - ShowerEvent->globalTime) / GFREQ2US;
DanssPair->ShowerEnergy = (ShowerEvent->SiPmCleanEnergy + ShowerEvent->PmtCleanEnergy) / 2;
DanssPair->NNHits = DanssEvent->NHits;
DanssPair->NPHits = SavedEvent->NHits;
acorr(DanssPair); // correct positron energy based on neutron position if only one coordinate of positron cluster is available
DanssPair->PositronEnergy = (DanssPair->PositronEnergy - CORR_P0) / CORR_P1;
DanssPair->PositronPmtEnergy = (DanssPair->PositronPmtEnergy - CORR_PMT_P0) / CORR_PMT_P1;
DanssPair->PositronSiPmEnergy = (DanssPair->PositronSiPmEnergy - CORR_SIPM_P0) / CORR_SIPM_P1;
}
int main(int argc, char **argv)
{
struct DanssPairStruct6 DanssPair;
struct DanssEventStruct5 DanssEvent;
struct DanssEventStruct5 Neutron;
struct DanssEventStruct5 Positron;
struct DanssEventStruct5 Veto;
struct DanssEventStruct5 Shower;
struct DanssInfoStruct4 DanssInfo;
struct DanssInfoStruct SumInfo;
struct HitStruct HitArray[3]; // 0 - positron, 1 - neutron, 2 - place for input
TChain *EventChain;
TChain *InfoChain;
TTree *tOut;
TTree *tRandom;
TTree *InfoOut;
TFile *fOut;
FILE *fList;
char str[1024];
long long iEvt, nEvt;
int PairCnt[2];
int i;
int iLoop;
float tShift;
char *ptr;
if (argc < 3) {
printf("Usage: %s list_file.txt|input_file.root output_file.root\n", argv[0]);
printf("Will process files in the list_file and create root-file\n");
return 10;
}
fOut = new TFile(argv[2], "RECREATE");
if (!fOut->IsOpen()) {
printf("Can not open the output file %s: %m\n", argv[2]);
return -10;
}
tOut = new TTree("DanssPair", "Time Correlated events");
tOut->Branch("Pair", &DanssPair,
"number[2]/L:" // event numbers in the file
"unixTime/I:" // linux time, seconds
"SiPmCleanEnergy[2]/F:" // Full Clean energy SiPm
"PmtCleanEnergy[2]/F:" // Full Clean energy Pmt
"PositronFlags[2]/I:" // positron flags for both triggers
// "positron cluster" parameters
"PositronHits/I:" // hits in the cluster
"PositronMinLen/F:" // Minimum track length to create the cluster
"PositronEnergy/F:" // Energy sum of the cluster (SiPM)
"TotalEnergy/F:" // Total energy long. corrected
"PositronSiPmEnergy/F:" // SiPM energy in the cluster, corrected
"PositronPmtEnergy/F:" // PMT energy in the cluster, corrected
"PositronX[3]/F:" // cluster position
"AnnihilationGammas/I:" // number of possible annihilation gammas
"AnnihilationEnergy/F:" // Energy in annihilation gammas
"AnnihilationMax/F:" // Max hit energy beyond the cluster
// "neutron" parameters
"NeutronHits/I:" // number of hits considered as neutron capture gammas
"NeutronEnergy/F:" // Energy sum of above (SiPM)
"NeutronX[3]/F:" // center of gammas position
// Pair parameters
"gtDiff/F:" // time difference in us (from 125 MHz clock)
"Distance/F:" // distance between neutron and positron, cm
"DistanceZ/F:" // in Z, cm
// Environment
"gtFromPrevious/F:" // time from the previous hit before positron, us
"PreviousEnergy/F:" // energy of the previous event
"gtToNext/F:" // time to the next hit after neutron, counted from positron, us
"NextEnergy/F:" // energy of the next event
"EventsBetween/I:" // Events between positron and neutron
// Veto
"gtFromVeto/F:" // time from the last Veto event
"VetoHits/I:" // hits in Veto counters
"VetoEnergy/F:" // Energy in Veto counters
"DanssEnergy/F:" // Veto Energy in Danss (Pmt + SiPm)/2
"gtFromShower/F:" // time from large energy shower in DANSS
"ShowerEnergy/F:" // shower event energy in DANSS (Pmt + SiPm)/2
// Hits
"NPHits/I:" // Number of hits in "positron event"
"NNHits/I" // Number of hits in "neutron event"
);
tOut->Branch("PHitE", HitArray[0].E, "PHitE[NPHits]/F");
tOut->Branch("PHitT", HitArray[0].T, "PHitT[NPHits]/F");
tOut->Branch("PHitType", HitArray[0].type, "PHitType[NPHits]/I");
tOut->Branch("NHitE", HitArray[1].E, "NHitE[NNHits]/F");
tOut->Branch("NHitT", HitArray[1].T, "NHitT[NNHits]/F");
tOut->Branch("NHitType", HitArray[1].type, "NHitType[NNHits]/I");
tRandom = new TTree("DanssRandom", "Random coincidence events");
tRandom->Branch("Pair", &DanssPair,
"number[2]/L:" // event numbers in the file
"unixTime/I:" // linux time, seconds
"SiPmCleanEnergy[2]/F:" // Full Clean energy SiPm
"PmtCleanEnergy[2]/F:" // Full Clean energy Pmt
"PositronFlags[2]/I:" // positron flags for both triggers
// "positron cluster" parameters
"PositronHits/I:" // hits in the cluster
"PositronMinLen/F:" // Minimum track length to create the cluster
"PositronEnergy/F:" // Energy sum of the cluster (SiPM)
"TotalEnergy/F:" // Total energy long. corrected
"PositronSiPmEnergy/F:" // SiPM energy in the cluster, corrected
"PositronPmtEnergy/F:" // PMT energy in the cluster, corrected
"PositronX[3]/F:" // cluster position
"AnnihilationGammas/I:" // number of possible annihilation gammas
"AnnihilationEnergy/F:" // Energy in annihilation gammas
"AnnihilationMax/F:" // Max hit energy beyond the cluster
// "neutron" parameters
"NeutronHits/I:" // number of hits considered as neutron capture gammas
"NeutronEnergy/F:" // Energy sum of above (SiPM)
"NeutronX[3]/F:" // center of gammas position
// Pair parameters
"gtDiff/F:" // time difference in us (from 125 MHz clock)
"Distance/F:" // distance between neutron and positron, cm
"DistanceZ/F:" // in Z, cm
// Environment
"gtFromPrevious/F:" // time from the previous hit before positron, us
"PreviousEnergy/F:" // energy of the previous event
"gtToNext/F:" // time to the next hit after neutron, counted from positron, us
"NextEnergy/F:" // energy of the next event
"EventsBetween/I:" // Events between positron and neutron
// Veto
"gtFromVeto/F:" // time from the last Veto event
"VetoHits/I:" // hits in Veto counters
"VetoEnergy/F:" // Energy in Veto counters
"DanssEnergy/F:" // Veto Energy in Danss (Pmt + SiPm)/2
"gtFromShower/F:" // time from large energy shower in DANSS
"ShowerEnergy/F:" // shower event energy in DANSS (Pmt + SiPm)/2
// Hits
"NPHits/I:" // Number of hits in "positron event"
"NNHits/I" // Number of hits in "neutron event"
);
tRandom->Branch("PHitE", HitArray[0].E, "PHitE[NPHits]/F");
tRandom->Branch("PHitT", HitArray[0].T, "PHitT[NPHits]/F");
tRandom->Branch("PHitType", HitArray[0].type, "PHitType[NPHits]/I");
tRandom->Branch("NHitE", HitArray[1].E, "NHitE[NNHits]/F");
tRandom->Branch("NHitT", HitArray[1].T, "NHitT[NNHits]/F");
tRandom->Branch("NHitType", HitArray[1].type, "NHitType[NNHits]/I");
InfoOut = new TTree("SumInfo", "Summary information");
InfoOut->Branch("Info", &SumInfo,
"gTime/L:" // running time in terms of 125 MHz
"startTime/I:" // linux start time, seconds
"stopTime/I:" // linux stop time, seconds
"events/L" // number of events
);
memset(&SumInfo, 0, sizeof(struct DanssInfoStruct));
EventChain = new TChain("DanssEvent");
EventChain->SetBranchAddress("Data", &DanssEvent);
EventChain->SetBranchAddress("HitE", &HitArray[2].E);
EventChain->SetBranchAddress("HitT", &HitArray[2].T);
EventChain->SetBranchAddress("HitType", &HitArray[2].type);
InfoChain = new TChain("DanssInfo");
InfoChain->SetBranchAddress("Info", &DanssInfo);
ptr = strrchr(argv[1], '.');
if (!ptr) {
printf("Strange file extention: .txt or .root expected\n");
goto fin;
}
if (!strcmp(ptr, ".txt")) {
fList = fopen(argv[1], "rt");
if (!fList) {
printf("Can not open list of files %s: %m\n", argv[1]);
goto fin;
}
for(;;) {
if (!fgets(str, sizeof(str), fList)) break;
ptr = strchr(str, '\n');
if (ptr) *ptr = '\0';
EventChain->Add(str);
InfoChain->Add(str);
}
fclose(fList);
} else if (!strcmp(ptr, ".root")) {
EventChain->Add(argv[1]);
InfoChain->Add(argv[1]);
} else {
printf("Strange file extention: .txt or .root expected\n");
goto fin;
}
nEvt = EventChain->GetEntries();
memset(PairCnt, 0, sizeof(PairCnt));
memset(&Veto, 0, sizeof(Veto));
memset(&Shower, 0, sizeof(Shower));
for (iEvt =0; iEvt < nEvt; iEvt++) {
EventChain->GetEntry(iEvt);
// Shower
if (IsShower(&DanssEvent)) memcpy(&Shower, &DanssEvent, sizeof(struct DanssEventStruct5));
// Veto
if (IsVeto(&DanssEvent)) {
memcpy(&Veto, &DanssEvent, sizeof(struct DanssEventStruct5));
continue;
}
// Get Neutron
if (IsNeutron(&DanssEvent)) {
memcpy(&Neutron, &DanssEvent, sizeof(struct DanssEventStruct5));
CopyHits(&HitArray[1], &HitArray[2], DanssEvent.NHits);
for (iLoop = 0; iLoop <= NRANDOM; iLoop++) {
tShift = iLoop * RSHIFT;
// Now look backward for positron in the region ([-50, 0] - iLoop*RSHIFT) us
for (i=iEvt-1; i>=0; i--) {
EventChain->GetEntry(i);
if (Neutron.globalTime - DanssEvent.globalTime >= (MAXTDIFF + tShift) * GFREQ2US) break; // not found
if (Neutron.globalTime - DanssEvent.globalTime >= tShift * GFREQ2US && IsPositron(&DanssEvent)) break; // found
if (Neutron.globalTime - DanssEvent.globalTime < 0) break;
}
if (Neutron.globalTime - DanssEvent.globalTime < 0) break;
// less than 50 us from neutron
if (Neutron.globalTime - DanssEvent.globalTime < (MAXTDIFF + tShift) * GFREQ2US && i >= 0) {
memcpy(&Positron, &DanssEvent, sizeof(struct DanssEventStruct5));
CopyHits(&HitArray[0], &HitArray[2], DanssEvent.NHits);
Positron.globalTime += tShift * GFREQ2US; // assume it here !!!
MakePair(&Neutron, &Positron, &Veto, &Shower, &DanssPair);
// look backward
for (i=iEvt-1;i>=0;i--) {
EventChain->GetEntry(i);
if (DanssEvent.globalTime > Positron.globalTime) {
DanssPair.EventsBetween++;
} else if (DanssEvent.globalTime < Positron.globalTime) {
DanssPair.gtFromPrevious = (Positron.globalTime - DanssEvent.globalTime) / GFREQ2US;
DanssPair.PreviousEnergy = (DanssEvent.SiPmCleanEnergy + DanssEvent.PmtCleanEnergy) / 2;
break;
}
}
// look forward
if (iEvt + 1 < nEvt) {
EventChain->GetEntry(iEvt+1);
DanssPair.gtToNext = (DanssEvent.globalTime - Positron.globalTime) / GFREQ2US;
DanssPair.NextEnergy = (DanssEvent.SiPmCleanEnergy + DanssEvent.PmtCleanEnergy) / 2;
}
if (iLoop) {
tRandom->Fill();
PairCnt[1]++;
} else {
tOut->Fill();
PairCnt[0]++;
}
}
}
}
}
for(i=0; i<InfoChain->GetEntries(); i++) {
InfoChain->GetEntry(i);
SumInfo.upTime += DanssInfo.upTime;
SumInfo.stopTime = DanssInfo.stopTime;
SumInfo.events += DanssInfo.events;
if (!i) SumInfo.startTime = DanssInfo.startTime;
}
InfoOut->Fill();
printf("%Ld events processed with %d randomizing loops - %d/%d pairs found. Aquired time %f7.0 s\n",
iEvt, NRANDOM, PairCnt[0], PairCnt[1], SumInfo.upTime / GLOBALFREQ);
fin:
delete EventChain;
delete InfoChain;
InfoOut->Write();
tOut->Write();
tRandom->Write();
fOut->Close();
return 0;
}