-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigi_evtbuilder6.cpp
1608 lines (1497 loc) · 51 KB
/
digi_evtbuilder6.cpp
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
*
* Version: 4.0
*
* Package: DANSS SiPm Signal Processing and Calibration
*
* Description: Calculate different event parameters and put to root file
*
***/
#include <libgen.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 "TRandom2.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 "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 "readDigiData.h"
#include "danssGlobals.h"
#include "evtbuilder.h"
/*********************** Definitions ****************************/
#define MYVERSION "4.60"
// Initial clean parameters
#define MINSIPMPIXELS 3 // Minimum number of pixels to consider SiPM hit for FineTime calculation
// #define MINSIPMPIXELS2 2 // Minimum number of pixels to consider SiPM hit without confirmation (method 2)
// #define MINPMTENERGY 0.1 // Minimum PMT energy for a hit
// #define MINVETOENERGY 0.1 // Minimum VETO energy for a hit
#define SIPMEARLYTIME 45 // ns - shift from fine time
#define SOMEEARLYTIME 130 // ns - absolute if fineTime is not defined
#define MAXPOSITRONENERGY 20 // Maximum Total clean energy to calculate positron parameters
#define MAXCLUSTITER 10 // Maximum number of iterations in cluster search
#define MCNEUTRONSIGGMA 20.0 // Sigma for neutron based longitudinal correction for MC
#define NBOTTOMLAYERS 2 // Use two bottom SiPM layers as additional VETO
//#define ENERGY_CORRECTION 0.95 // Energy correction to be applied for experimental data
// fine time
#define MINENERGY4TIME 0.25 // Minimum energy to use for fine time averaging
#define MINAVRTIME 130 // Minimum time for hit to be used in fine time calculations
#define TCUT 10 // fine time cut, ns for SiPM
#define TCUTPMT 30 // fine time cut, ns for PMT and VETO
#define NOFINETIME 10000 // something out of range
// Flags
#define FLG_PRINTALL 1 // do large debuggging printout
#define FLG_DTHIST 2 // create time delta histogramms
//#define FLG_EAMPLITUDE 4 // put amplitude instead of energy to XXCleanEnergy cells - abandoned
//#define FLG_POSECORRECTIONA 0x100 // do positron energy correction based on MC and NHITS
//#define FLG_POSECORRECTIONB 0x200 // do positron energy correction based on average MC
#define FLG_SIMLONGCORR 0x1000 // simulate "neutron" correction for MC events
//#define FLG_NOCLEANNOISE 0x10000 // do not clean low energy signals
#define FLG_NOTIMECUT 0x20000 // do not clean signals by time
#define FLG_NOCONFIRM 0x40000 // do not search PMT confirmation for SiPM and vice versa
//#define FLG_NOCONFIRM2 0x80000 // do not search PMT confirmation for 1 pixel SiPM signals
#define FLG_NOPMTCORR 0x100000 // do not correct PMT energy of cluster for out of cluster SiPM hits
#define FLG_PMTTIMECUT 0x200000 // Cut PMT and Veto by time
#define FLG_CONFIRMSIPM 0x400000 // do not confirm all SiPM hits
#define FLG_MCENERGYSMEAR 0x800000 // Do additional energy smear for MC events (IBD simulation assumed)
#define MAXADCBOARD 60
#define TAGMASK ((1L<<45) - 1)
#define TIMEHISTMINENERGY 10 // Minimum sum energy in SiPm + Pmt + Veto to fill time hists (not divided by 2)
#define TIMEHISTMINHITS 4 // Minimum number of hits in SiPm + Pmt + Veto to fill time hists
using namespace std;
// Globals:
ReadDigiDataUser *user;
long long iNevtTotal;
long long upTime;
long long fileFirstTime;
long long fileLastTime;
long long dumpgTime;
int globalTimeWrapped;
int progStartTime;
char * chTimeCalibration;
char * chOutputFile;
int iFlags;
int MaxEvents;
int IsMc; // MC run flag
double EnergyCorrection; // energy correction based on 12B
double SiPMEnergyCorrection; // correct SiPM energy based on MC single pixel response
double MCEnergyCorrection; // MC energy correction
TRandom2 * Random;
struct { // additional energy smear for MC events
double st; // stokhastic term
double ct; // impurity term
} MCsmear;
TFile * OutputFile;
TTree * OutputTree;
TTree * InfoTree;
TTree * RawHitsTree;
TFile * McFile;
TTree * McEventTree;
int McEntries;
struct DanssEventStruct7 DanssEvent;
struct DanssInfoStruct4 DanssInfo;
struct DanssMcStruct DanssMc;
struct MCEventStruct McEvent;
struct DanssExtraStruct {
float SiPmEnergy;
int SiPmHits;
float PmtEnergy;
int PmtHits;
float VetoEnergy;
int VetoHits;
} DanssExtra;
int HitFlag[iMaxDataElements]; // array to flag out SiPM hits
// Hitflag = -100 - early hit
// HitFlag = -1 - bad hit
// HitFlag = 0 - ordinary hit
// Hitflag = 5 - Pmt Hit in the cluster
// HitFlag = 10 - the most energetic hit in the cluster (SiPm)
// HitFlag = 20 - other hit in the cluster (SiPm)
TH1D * hTimeDelta[MAXADCBOARD][iNChannels_AdcBoard];
TH1D * hPMTTimeDelta[MAXADCBOARD][iNChannels_AdcBoard];
double PmtFineTime;
int DeadList[MAXADCBOARD][iNChannels_AdcBoard];
struct HitStruct {
float E[iMaxDataElements];
float T[iMaxDataElements];
struct HitTypeStruct type[iMaxDataElements];
} HitArray;
struct RawStruct {
unsigned short PmtCnt;
unsigned short VetoCnt;
unsigned short SiPmCnt;
} RawHits;
struct RawArrayStruct {
long long tag;
struct RawStruct data;
};
struct RawArrayStruct *RawHitsArray;
int RawHitsPtr;
int RawHitsCnt;
TH1D *hCrossTalk;
TH1D *hPMTAmpl[iNChannels_AdcBoard];
TH1D *hSiPMtime[3];
TH1D *hEtoEMC;
TH1D *hNPEtoEMC;
/********************************************************************************************************************/
/************************ Raw hits - fight with the pickup *****************************/
/********************************************************************************************************************/
/* Open data file either directly or via zcat etc, depending on the extension */
FILE* OpenTextDataFile(const char *fname)
{
char cmd[1024];
FILE *f;
if (strstr(fname, ".bz2")) {
sprintf(cmd, "bzcat %s", fname);
f = popen(cmd, "r");
} else if (strstr(fname, ".gz")) {
sprintf(cmd, "zcat %s", fname);
f = popen(cmd, "r");
} else if (strstr(fname, ".xz")) {
sprintf(cmd, "xzcat %s", fname);
f = popen(cmd, "r");
} else {
f = fopen(fname, "rb");
}
return f;
}
// Initialize Raw hits Array from file return number of triggers read
int RawHitsArrayInit(const char *RawHitsFileName)
{
FILE *f;
int size;
char *ptr;
char str[1024];
char str_copy[1024];
void *mptr;
RawHitsCnt = 0;
RawHitsPtr = 0;
f = OpenTextDataFile(RawHitsFileName);
if (!f) {
printf("Can not open file %s: %m\n", RawHitsFileName);
goto fin;
}
size = 2000000;
RawHitsArray = (struct RawArrayStruct *) malloc(size * sizeof(struct RawArrayStruct));
if (!RawHitsArray) {
printf("Can not allocate memory: %m\n");
goto fin;
}
for (;;) {
ptr = fgets(str, sizeof(str), f);
if (!ptr) break; // EOF
strcpy(str_copy, str);
ptr = strtok(str, " \t");
if (!ptr) continue;
if (!isdigit(ptr[0])) continue;
if (RawHitsCnt >= size) { // we need larger array
mptr = realloc(RawHitsArray, (size + 1000000) * sizeof(struct RawArrayStruct));
if (!mptr) {
printf("Can not allocate memory: %m\n");
goto fin;
}
size += 1000000;
RawHitsArray = (struct RawArrayStruct *)mptr;
}
RawHitsArray[RawHitsCnt].tag = TAGMASK & strtoll(ptr, NULL, 10);
ptr = strtok(NULL, " \t");
if (!ptr) {
printf("Bad string: %s [file %s]\n", str_copy, RawHitsFileName);
continue;
}
RawHitsArray[RawHitsCnt].data.PmtCnt = strtol(ptr, NULL, 10);
ptr = strtok(NULL, " \t");
if (!ptr) {
printf("Bad string: %s [file %s]\n", str_copy, RawHitsFileName);
continue;
}
RawHitsArray[RawHitsCnt].data.VetoCnt = strtol(ptr, NULL, 10);
ptr = strtok(NULL, " \t");
if (!ptr) {
printf("Bad string: %s [file %s]\n", str_copy, RawHitsFileName);
continue;
}
RawHitsArray[RawHitsCnt].data.SiPmCnt = strtol(ptr, NULL, 10);
RawHitsCnt++;
}
fin:
if (f) pclose(f);
if (!RawHitsCnt && RawHitsArray) {
free(RawHitsArray);
RawHitsArray = NULL;
}
return RawHitsCnt;
}
// Find raw hits numbers for the trigger being processed
void FindRawHits(void)
{
long long gtA;
long long gtB;
memset(&RawHits, 0, sizeof(RawHits));
gtA = TAGMASK & DanssEvent.globalTime;
for (; RawHitsPtr < RawHitsCnt; RawHitsPtr++) {
gtB = RawHitsArray[RawHitsPtr].tag;
if (gtB == gtA) break;
}
if (gtB != gtA) { // try to start from the beginning
for (RawHitsPtr = 0; RawHitsPtr < RawHitsCnt; RawHitsPtr++) {
gtB = RawHitsArray[RawHitsPtr].tag;
if (gtB == gtA) break;
}
}
if (gtB == gtA) { // trigger found
memcpy(&RawHits, &RawHitsArray[RawHitsPtr].data, sizeof(struct RawStruct));
} else { // trigger is somehow missing
printf("Trigger %Ld not found.\n", gtA);
}
}
// Check if the current trigger is associated with pickup noise.
// We never consider VETO triggers as PickUp
// We consider PickUp either less than 30% of SiPM or PMT hits pass Ira's analysis
int IsPickUp(void)
{
if (!RawHitsArray) return 0; // no RawHits information
// "(PmtCnt > 0 && PmtCleanHits/PmtCnt < 0.3) || SiPmHits/SiPmCnt < 0.3"
if (DanssEvent.VetoCleanHits > 0) return 0; // never kill VETO trigger
if ((RawHits.PmtCnt > 0 && 1.0 * DanssEvent.PmtCleanHits / RawHits.PmtCnt < 0.3) ||
1.0 * DanssEvent.SiPmHits / RawHits.SiPmCnt < 0.3) return 1;
return 0;
}
/********************************************************************************************************************/
/************************ Analysis functions *****************************/
/********************************************************************************************************************/
// Get energy from hit number. Apply global corrections
// i - hit number
double Energy(int i)
{
double E;
E = user->e(i);
if (user->type(i) == bSiPm) E *= SiPMEnergyCorrection;
if (IsMc) {
E *= MCEnergyCorrection;
} else {
E *= EnergyCorrection;
}
return E;
}
// int SiPm - hit number in SiPM
// int Pmt - hit number in PMT
// return true if SiPM is read by this PMT
int IsInModule(int SiPm, int Pmt)
{
int SiPmXY, PmtXY;
int SiPmZ, PmtZ;
if (user->side(SiPm) != user->side(Pmt)) return false;
if (user->type(SiPm) != bSiPm || user->type(Pmt) != bPmt) return false; // wrong request
SiPmXY = user->firstCoord(SiPm);
PmtXY = user->firstCoord(Pmt);
SiPmZ = user->zCoord(SiPm);
PmtZ = user->zCoord(Pmt);
if (SiPmXY / 5 != PmtXY || SiPmZ / 20 != PmtZ) return false;
return true;
}
// int hitA, hitB - hits in SiPM
// return true if the SiPMs are neighbors or coinside
int IsNeighbor(int hitA, int hitB)
{
if (user->zCoord(hitA) == user->zCoord(hitB) && abs(user->firstCoord(hitA) - user->firstCoord(hitB)) <= 1) return 1;
if (abs(user->zCoord(hitA) - user->zCoord(hitB)) == 1) return 1;
return 0;
}
// Do MC extra energy smearing
double MCEnergySmear(double E)
{
return Random->Gaus(E, sqrt(MCsmear.st * MCsmear.st * E + MCsmear.ct * MCsmear.ct * E * E));
}
// Longitudinal correction for PMT
double PMTYAverageLightColl(double x)
{
//<func(x)=1>
const double FuncAverage = 1.00147;
double rez;
rez = (0.987387*exp(-0.0016*(x-48)) + 0.023973*exp(-0.0877*(x-48)) - 0.0113581*exp(-0.1042*(x-48))
-2.30972E-6*exp(0.2214*(x-48))) / FuncAverage;
return rez;
}
// Longitudinal correction for SiPM
double SiPMYAverageLightColl(double x)
{
//<func(x)=1>
const double FuncAverage = 1.02208;
double rez;
rez = (0.00577381*exp(-0.1823*(x-48)) + 0.999583*exp(-0.0024*(x-48)) - 8.095E-13*exp(0.5205*(x-48))
-0.00535714*exp(-0.1838*(x-48))) / FuncAverage;
return rez;
}
// Longitudinal light correctrion
double YAverageLightColl(double x, int type)
{
double rez;
switch(type) {
case bSiPm:
rez = SiPMYAverageLightColl(x);
break;
case bPmt:
rez = PMTYAverageLightColl(x);
break;
default:
rez = 1.0;
}
return rez;
}
// float energy - measured energy
// float dist - distance from zero coordinate
// return corrected energy
// simulate "neutron" correction for MC events
float acorr(float energy, float dist, char side = 'Y', int type = bSiPm)
{
float C, XY;
if (dist >= 0) {
C = 1.0 / YAverageLightColl(dist, type);
} else if (IsMc && (iFlags & FLG_SIMLONGCORR)) {
XY = (side == 'X') ? DanssMc.X[1] : DanssMc.X[0];
XY -= 2;
dist = Random->Gaus(XY, MCNEUTRONSIGGMA);
if (dist < 0) dist = 0;
if (dist > 96) dist = 96;
C = 1.0 / YAverageLightColl(dist, type);
} else {
C = 1;
}
return C * energy;
}
/********************************************************************************************************************/
/************************ Main analysis *****************************/
/********************************************************************************************************************/
// Calculate parameters assuming positron-like event
void CalculatePositron(void)
{
int i, j, k, N;
float A;
float x, y, z;
float nx, ny;
int maxHit;
int repeat;
int clusterHits[10]; // Maximum possible cluster 5x2
int xmin, xmax, ymin, ymax, zmin, zmax;
int xy;
int invalid;
float E;
DanssEvent.MinPositron2GammaZ = 1000;
DanssEvent.PositronX[0] = -1;
DanssEvent.PositronX[1] = -1;
DanssEvent.PositronX[2] = -1;
N = user->nhits();
// Find the maximum hit
A = 0;
maxHit = -1;
for (i=0; i<N; i++) if (HitFlag[i] >= 0 && user->type(i) == bSiPm && Energy(i) > A) {
A = Energy(i);
maxHit = i;
}
if (maxHit < 0) { // nothing to do - no usable SiPM hits
// DanssEvent.PositronFlags |= PFLAG_NOCLUSTER;
return;
}
HitFlag[maxHit] = 10;
// Find cluster
for (k=0; k<MAXCLUSTITER; k++) {
repeat = 0;
for (i=0; i<N; i++) if (HitFlag[i] >= 10) for (j=0; j<N; j++)
if (HitFlag[j] >= 0 && HitFlag[j] < 10 && user->type(j) == bSiPm && IsNeighbor(i, j)) {
HitFlag[j] = 20;
repeat = 1;
}
if (!repeat) break;
}
// Find cluster position
x = y = z = 0;
nx = ny = 0;
for (i=0; i<N; i++) if (HitFlag[i] >= 10) {
DanssEvent.PositronHits++;
if (user->side(i) == 'X') {
x += user->firstCoord(i) * fStripWidth * Energy(i);
z += user->zCoord(i) * fStripHeight * Energy(i);
nx += Energy(i);
} else {
y += user->firstCoord(i) * fStripWidth * Energy(i);
z += user->zCoord(i) * fStripHeight * Energy(i);
ny += Energy(i);
}
}
DanssEvent.PositronX[0] = (nx > 0) ? x / nx : -1; // Coordinate is unknown
DanssEvent.PositronX[1] = (ny > 0) ? y / ny : -1; // Coordinate is unknown
DanssEvent.PositronX[2] = (nx + ny > 0) ? z / (nx + ny) : -1; // Coordinate is unknown
// Find corrected energy
// Step 1: Count SiPM
for (i=0; i<N; i++) if (HitFlag[i] >= 10) {
if (user->side(i) == 'X') {
DanssEvent.PositronSiPmEnergy += acorr(Energy(i), DanssEvent.PositronX[1], 'X', bSiPm);
} else {
DanssEvent.PositronSiPmEnergy += acorr(Energy(i), DanssEvent.PositronX[0], 'Y', bSiPm);
}
}
// Step 2: Count PMT
for (i=0; i<N; i++) if (HitFlag[i] >= 0 && user->type(i) == bPmt) {
for (j=0; j<N; j++) if (IsInModule(j, i) && HitFlag[j] >= 10) break;
if (j >= N) continue;
HitFlag[i] = 5;
if (user->side(i) == 'X') {
DanssEvent.PositronPmtEnergy += acorr(Energy(i), DanssEvent.PositronX[1], 'X', bPmt);
} else {
DanssEvent.PositronPmtEnergy += acorr(Energy(i), DanssEvent.PositronX[0], 'Y', bPmt);
}
}
// Step 3: Subtract gammas in PMT
if (!(iFlags & FLG_NOPMTCORR)) for (i=0; i<N; i++) if (HitFlag[i] >= 0 && HitFlag[i] < 10 && user->type(i) == bSiPm) {
for (j=0; j<N; j++) if (IsInModule(i, j) && HitFlag[j] == 5) break;
if (j >= N) continue;
if (user->side(i) == 'X') {
DanssEvent.PositronPmtEnergy -= acorr(Energy(i), DanssEvent.PositronX[1], 'X', bSiPm);
} else {
DanssEvent.PositronPmtEnergy -= acorr(Energy(i), DanssEvent.PositronX[0], 'Y', bSiPm);
}
}
DanssEvent.PositronEnergy = DanssEvent.PositronSiPmEnergy + DanssEvent.PositronPmtEnergy;
// Step 4: Divide by 2, because we count SiPM + PMT
DanssEvent.PositronEnergy /= 2;
// Calculate Total energy with longitudinal correction
for (i=0; i<N; i++) if (HitFlag[i] >= 0 && (user->type(i) == bPmt || user->type(i) == bSiPm)) {
if (user->side(i) == 'X') {
DanssEvent.TotalEnergy += acorr(Energy(i), DanssEvent.PositronX[1], 'X', user->type(i));
} else {
DanssEvent.TotalEnergy += acorr(Energy(i), DanssEvent.PositronX[0], 'Y', user->type(i));
}
}
DanssEvent.TotalEnergy /= 2; // PMT + SiPM
// if (FLG_POSECORRECTIONB & iFlags) DanssEvent.TotalEnergy = MCTotalCorrection(DanssEvent.TotalEnergy);
//
// Count possible gammas
A = 0;
for (i=0; i<N; i++) if (HitFlag[i] >= 0 && HitFlag[i] < 5) switch(user->type(i)) {
case bSiPm:
DanssEvent.AnnihilationGammas++; // Add SiPm hits
if (user->side(i) == 'X') {
E = acorr(Energy(i), DanssEvent.PositronX[1], 'X', bSiPm);
} else {
E = acorr(Energy(i), DanssEvent.PositronX[0], 'Y', bSiPm);
}
DanssEvent.AnnihilationEnergy += E;
if (A < E) A = E;
break;
case bPmt:
// We will add Pmt hit only if there is no SiPm hit
for (j=0; j < N; j++) if (HitFlag[j] >= 0 && user->type(i) == bSiPm && IsInModule(j, i)) break;
if (j >= N) DanssEvent.AnnihilationGammas++;
// Add PMT energy
for (j=0; j < N; j++) if (HitFlag[j] >= 0 && HitFlag[j] < 5 && user->type(i) == bSiPm && IsInModule(j, i)) break;
if (user->side(i) == 'X') {
E = acorr(Energy(i), DanssEvent.PositronX[1], 'X', bPmt);
} else {
E = acorr(Energy(i), DanssEvent.PositronX[0], 'Y', bPmt);
}
DanssEvent.AnnihilationEnergy += E;
// Subtruct SiPMs in cluster if any
for (j=0; j < N; j++) if (HitFlag[j] >= 10 && user->type(i) == bSiPm && IsInModule(j, i)) {
if (user->side(j) == 'X') {
E = acorr(Energy(j), DanssEvent.PositronX[1], 'X', bSiPm);
} else {
E = acorr(Energy(j), DanssEvent.PositronX[0], 'Y', bSiPm);
}
DanssEvent.AnnihilationEnergy -= E;
}
}
DanssEvent.AnnihilationMax = A;
DanssEvent.AnnihilationEnergy /= 2; // (SiPm + Pmt) / 2
// Do energy correction based on MC taking into account number of hits in the cluster - Deprecated
// if (FLG_POSECORRECTIONA & iFlags) DanssEvent.PositronEnergy = HitNumberCorrection(DanssEvent.PositronEnergy, DanssEvent.PositronHits);
// if (FLG_POSECORRECTIONB & iFlags) DanssEvent.PositronEnergy = MCAverageCorrection(DanssEvent.PositronEnergy);
// Find Z-distance to the closest gamma
A = 1000;
for (i=0; i<N; i++) if (HitFlag[i] >= 0 && HitFlag[i] < 10 && user->type(i) == bSiPm && fabs(user->zCoord(i) * fStripHeight - DanssEvent.PositronX[2]) < A)
A = fabs(user->zCoord(i) * fStripHeight - DanssEvent.PositronX[2]);
DanssEvent.MinPositron2GammaZ = A;
}
void CalculateNeutron(void)
{
float x, y, z, r;
int nx, ny;
float exSiPm, eySiPm, exPmt, eyPmt;
int i, j, N;
N = user->nhits();
// Find the center (1st approximation)
x = y = z = 0;
nx = ny = 0;
exSiPm = eySiPm = exPmt = eyPmt = 0;
for (i=0; i<N; i++) if (HitFlag[i] >= 0 && user->type(i) == bSiPm) {
if (user->side(i) == 'X') {
x += user->firstCoord(i) * fStripWidth;
z += user->zCoord(i) * fStripHeight;
exSiPm += Energy(i);
nx++;
} else {
y += user->firstCoord(i) * fStripWidth;
z += user->zCoord(i) * fStripHeight;
eySiPm += Energy(i);
ny++;
}
}
DanssEvent.NeutronX[0] = (nx) ? x / nx : -1; // 50 cm is DANSS center
DanssEvent.NeutronX[1] = (ny) ? y / ny : -1; // 50 cm is DANSS center
DanssEvent.NeutronX[2] = (nx + ny) ? z / (nx + ny) : -1; // 50 cm is DANSS center
// Calculate lognitudinal correction
if (nx && ny) {
for (i=0; i<N; i++) if (HitFlag[i] >= 0 && user->type(i) == bPmt) {
if (user->side(i) == 'X') {
exPmt += Energy(i);
} else {
eyPmt += Energy(i);
}
}
DanssEvent.NeutronEnergy = (
acorr(exSiPm, DanssEvent.NeutronX[1], 'X', bSiPm) +
acorr(eySiPm, DanssEvent.NeutronX[0], 'Y', bSiPm) +
acorr(exPmt, DanssEvent.NeutronX[1], 'X', bPmt) +
acorr(eyPmt, DanssEvent.NeutronX[0], 'Y', bPmt)
) / 2;
} else {
DanssEvent.NeutronEnergy = (DanssEvent.SiPmCleanEnergy + DanssEvent.PmtCleanEnergy) / 2;
}
DanssEvent.NeutronHits = DanssEvent.SiPmCleanHits;
for (i=0; i<N; i++) if (HitFlag[i] >= 0 && user->type(i) == bPmt) {
for (j=0; j<N; j++) if (HitFlag[j] >= 0 && user->type(j) == bSiPm && IsInModule(j, i)) break;
if (j >= N) DanssEvent.NeutronHits++; // count Pmt hit if it was not counted by SiPm
}
}
// Clean hits:
// - SiPM with zero or less number of pixels
// - bad (not a number) or not positive energy
// - bad time
// - from marked bad channels
// - from dead channel list
void CleanZeroes(void)
{
int i, N;
N = user->nhits();
for (i=0; i<N; i++) if ((user->type(i) == bSiPm && user->npix(i) <= 0) || (!isfinite(user->e(i))) ||
Energy(i) <= 0 || user->t_raw(i) < -1000 || user->isBadChannel(user->chanIndex(i)) || DeadList[user->adc(i)-1][user->adcChan(i)]) {
HitFlag[i] = -1;
DanssInfo.Cuts[0]++;
}
}
/* Clean only SiPM by PMT confirmation *
* Require PMT confirmation for ALL SiPM hits */
void CleanByConfirmation(void)
{
int i, j, N;
N = user->nhits();
for (i=0; i<N; i++) if (HitFlag[i] >= 0 && user->type(i) == bSiPm) {
// commented out - search for confirmation for all SiPm hits
// if (user->npix(i) >= MINSIPMPIXELS2 && (iFlags & FLG_CONFIRMSIPM)) continue; // that's enough
for (j=0; j<N; j++) if (HitFlag[j] >= 0 && user->type(j) == bPmt && IsInModule(i, j)) break;
if (j < N) continue;
HitFlag[i] = -1;
DanssInfo.Cuts[3]++;
// if (user->npix(i) < MINSIPMPIXELS2) DanssInfo.Cuts[4]++;
}
// "early" hits
for (i=0; i<N; i++) if (HitFlag[i] == -100)
{
// if (user->npix(i) >= MINSIPMPIXELS2 && (iFlags & FLG_CONFIRMSIPM)) continue; // that's enough
for (j=0; j<N; j++) if (HitFlag[j] >= 0 && user->type(j) == bPmt && IsInModule(i, j)) break;
if (j < N) continue;
HitFlag[i] = -1;
}
}
void Clean1Pixel(void)
{
int i, N;
N = user->nhits();
for (i=0; i<N; i++) if (user->type(i) == bSiPm) if(user->npix(i) < 1.5) HitFlag[i] = -1;
}
void CleanByTime(void)
{
int i, N;
float tearly;
N = user->nhits();
if (DanssEvent.fineTime != NOFINETIME) {
for (i=0; i<N; i++) if (HitFlag[i] >= 0) switch (user->type(i)) {
case bSiPm:
hSiPMtime[0]->Fill(user->t_raw(i) - DanssEvent.fineTime);
if (user->npix(i) < 1.5) {
hSiPMtime[1]->Fill(user->t_raw(i) - DanssEvent.fineTime);
} else if (user->npix(i) < 2.5) {
hSiPMtime[2]->Fill(user->t_raw(i) - DanssEvent.fineTime);
}
if (fabs(user->t_raw(i) - DanssEvent.fineTime) > TCUT) {
HitFlag[i] = -1;
DanssInfo.Cuts[5]++;
}
break;
case bPmt:
case bVeto:
if (fabs(user->t_raw(i) - DanssEvent.fineTime) > TCUTPMT && (iFlags & FLG_PMTTIMECUT)) {
HitFlag[i] = -1;
DanssInfo.Cuts[6]++;
}
break;
}
tearly = DanssEvent.fineTime - SIPMEARLYTIME;
} else {
tearly = SOMEEARLYTIME;
}
for (i=0; i<N; i++) if (user->type(i) == bSiPm && fabs(user->t_raw(i) - tearly) <= TCUT) HitFlag[i] = -100; // mark early hit candidates
}
void CorrectEnergy(double scale)
{
int i;
DanssEvent.VetoCleanEnergy *= scale;
DanssEvent.BottomLayersEnergy *= scale;
DanssEvent.PmtCleanEnergy *= scale;
DanssEvent.SiPmEnergy *= scale;
DanssEvent.SiPmCleanEnergy *= scale;
DanssEvent.SiPmEarlyEnergy *= scale;
DanssEvent.PositronEnergy *= scale;
DanssEvent.TotalEnergy *= scale;
DanssEvent.PositronSiPmEnergy *= scale;
DanssEvent.PositronPmtEnergy *= scale;
DanssEvent.AnnihilationEnergy *= scale;
DanssEvent.AnnihilationMax *= scale;
DanssEvent.NeutronEnergy *= scale;
for (i=0; i<DanssEvent.NHits; i++) HitArray.E[i] *= scale;
}
void CreateDeadList(char *fname, int run)
{
int i, j;
char str[16*1024];
char *ptr;
FILE *f;
int from, to;
int chan;
int cnt = 0;
memset(DeadList, 0, sizeof(DeadList));
if (!fname) return;
f = fopen(fname, "rt");
if (!f) {
printf("Dead list file %s not found.\n", fname);
return;
}
for (;;) {
if (!fgets(str, sizeof(str), f)) break;
ptr = strtok(str, " \t\n");
if (!ptr) continue;
from = strtol(ptr, NULL, 10);
ptr = strtok(NULL, " \t\n");
if (!ptr) continue;
to = strtol(ptr, NULL, 10);
if (run < from || run > to) continue; // this is not the list for our run
for(;;) {
ptr = strtok(NULL, " \t\n");
if (!ptr) break; // end of dead channels list
chan = 100 * (strtod(ptr, NULL) + 0.001);
i = chan /100;
j = chan % 100;
if (i > 0 && i <= MAXADCBOARD && j >= 0 && j < iNChannels_AdcBoard) {
DeadList[i-1][j] = 1;
cnt++;
}
}
break;
}
fclose(f);
printf("Run %d: %d channels masked out.\n", run, cnt);
}
void DebugFullPrint(void)
{
int i, N;
time_t tm;
N = user->nhits();
tm = DanssEvent.unixTime;
printf("******************************************************************************************************************\n");
printf("Event: %Ld globalTime: %Ld fineTime: %6.1f ns linux time: %s",
DanssEvent.number, DanssEvent.globalTime, DanssEvent.fineTime, ctime(&tm));
printf("Total %d hits: %d SiPM %d PMT %d Veto; Clean: %d SiPM %d PMT %d Veto\n",
N, DanssExtra.SiPmHits, DanssExtra.PmtHits, DanssExtra.VetoHits,
DanssEvent.SiPmCleanHits, DanssEvent.PmtCleanHits, DanssEvent.VetoCleanHits);
printf("Energy: %6.1f SiPM %6.1f PMT %6.1f Veto; Clean: %6.1f SiPM %6.1f PMT %6.1f Veto\n",
DanssExtra.SiPmEnergy, DanssExtra.PmtEnergy, DanssExtra.VetoEnergy,
DanssEvent.SiPmCleanEnergy, DanssEvent.PmtCleanEnergy, DanssEvent.VetoCleanEnergy);
if (N) {
printf("N Type N S E time ADC.Ch side XY Z Flag\n");
// 1234512345123412345678123451234561231123123451231231234
for(i=0; i<N; i++) switch(user->type(i)) {
case bSiPm:
printf("%4d SiPM %3.0f %7.1f %4.1f %5.1f %2d.%2.2d %c %2d %2d %c\n", i+1, user->npix(i), user->signal(i),
Energy(i), user->adc(i), user->t_raw(i), user->adcChan(i), user->side(i), user->firstCoord(i), user->zCoord(i),
(HitFlag[i]<0) ? 'X' : ' ');
break;
case bPmt:
printf("%4d PMT %7.1f %4.1f %5.1f %2d.%2.2d %c %2d %2d %c\n", i+1, user->signal(i),
Energy(i), user->t_raw(i), user->adc(i), user->adcChan(i), user->side(i), user->firstCoord(i), user->zCoord(i),
(HitFlag[i]<0) ? 'X' : ' ');
break;
case bVeto:
printf("%4d VETO %7.1f %4.1f %5.1f %2d.%2.2d - xx xx %c\n", i+1, user->signal(i),
Energy(i), user->t_raw(i), user->adc(i), user->adcChan(i),
(HitFlag[i]<0) ? 'X' : ' ');
break;
}
}
}
void DumpEvent(void)
{
int i, N;
char str[1024];
DebugFullPrint();
sprintf(str, "evt_%Ld.root", DanssEvent.globalTime);
TFile *f = new TFile(str, "RECREATE");
TH2D *SiPmX = new TH2D("hSiPmX", "SiPm X-side", 25, 0, 100, 50, 0, 100);
TH2D *SiPmY = new TH2D("hSiPmY", "SiPm Y-side", 25, 0, 100, 50, 0, 100);
TH2D *SiPmCleanX = new TH2D("hSiPmCleanX", "SiPm clean X-side", 25, 0, 100, 50, 0, 100);
TH2D *SiPmCleanY = new TH2D("hSiPmCleanY", "SiPm clean Y-side", 25, 0, 100, 50, 0, 100);
TH2D *PmtX = new TH2D("hPmtX" , "Pmt X-side", 5, 0, 100, 5, 0, 100);
TH2D *PmtY = new TH2D("hPmtY" , "Pmt Y-side", 5, 0, 100, 5, 0, 100);
TH1D *Veto = new TH1D("hVeto", "Veto channels", 64, 0, 64);
TH1D *Time = new TH1D("hTime", "Raw time", 200, 100, 300);
TH1D *TimeClean = new TH1D("hTimeClean", "Raw time clean", 200, 100, 300);
TH1D *Par = new TH1D("hPar", "Parameters", 20, 0, 20);
Par->GetXaxis()->SetBinLabel(1, "fineTime");
Par->GetXaxis()->SetBinLabel(2, "SiPmEnergy");
Par->GetXaxis()->SetBinLabel(3, "PmtEnergy");
Par->GetXaxis()->SetBinLabel(4, "VetoEnergy");
Par->GetXaxis()->SetBinLabel(5, "PositronEnergy");
Par->GetXaxis()->SetBinLabel(6, "SiPmHits");
Par->GetXaxis()->SetBinLabel(7, "PmtHits");
Par->GetXaxis()->SetBinLabel(8, "VetoHits");
Par->GetXaxis()->SetBinLabel(9, "PositronHits");
Par->GetXaxis()->SetBinLabel(10, "PositronX");
Par->GetXaxis()->SetBinLabel(11, "PositronY");
Par->GetXaxis()->SetBinLabel(12, "PositronZ");
Par->GetXaxis()->SetBinLabel(13, "McX");
Par->GetXaxis()->SetBinLabel(14, "McY");
Par->GetXaxis()->SetBinLabel(15, "McZ");
Par->Fill("fineTime", DanssEvent.fineTime);
Par->Fill("SiPmEnergy", DanssEvent.SiPmCleanEnergy);
Par->Fill("PmtEnergy", DanssEvent.PmtCleanEnergy);
Par->Fill("VetoEnergy", DanssEvent.VetoCleanEnergy);
Par->Fill("PositronEnergy", DanssEvent.PositronEnergy);
Par->Fill("SiPmHits", DanssEvent.SiPmCleanHits);
Par->Fill("PmtHits", DanssEvent.PmtCleanHits);
Par->Fill("VetoHits", DanssEvent.VetoCleanHits);
Par->Fill("PositronHits", DanssEvent.PositronHits);
Par->Fill("PositronX", DanssEvent.PositronX[0]);
Par->Fill("PositronY", DanssEvent.PositronX[1]);
Par->Fill("PositronZ", DanssEvent.PositronX[2]);
if (IsMc) {
Par->Fill("McX", DanssMc.X[0]);
Par->Fill("McY", DanssMc.X[1]);
Par->Fill("McZ", DanssMc.X[2]);
} else {
Par->Fill("McX", -1.0);
Par->Fill("McY", -1.0);
Par->Fill("McZ", -1.0);
}
N = user->nhits();
for(i=0; i<N; i++) {
switch(user->type(i)) {
case bSiPm:
if (user->side(i) == 'X') {
SiPmX->Fill(user->firstCoord(i)*4.0 + 2.0, user->zCoord(i)+0.5, Energy(i));
if (HitFlag[i] >= 0) SiPmCleanX->Fill(user->firstCoord(i)*4.0 + 2.0, user->zCoord(i)+0.5, Energy(i));
} else {
SiPmY->Fill(user->firstCoord(i)*4.0 + 2.0, user->zCoord(i)+0.5, Energy(i));
if (HitFlag[i] >= 0) SiPmCleanY->Fill(user->firstCoord(i)*4.0 + 2.0, user->zCoord(i)+0.5, Energy(i));
}
break;
case bPmt:
if (user->side(i) == 'X') {
PmtX->Fill(user->firstCoord(i)*20.0 + 10.0, user->zCoord(i)*20.0+10.0, Energy(i));
} else {
PmtY->Fill(user->firstCoord(i)*20.0 + 10.0, user->zCoord(i)*20.0+10.0, Energy(i));
}
break;
case bVeto:
Veto->Fill(user->adcChan(i), Energy(i));
break;
}
Time->Fill(user->t_raw(i), Energy(i));
if (HitFlag[i] >= 0) TimeClean->Fill(user->t_raw(i), Energy(i));
}
SiPmX->Write();
SiPmY->Write();
SiPmCleanX->Write();
SiPmCleanY->Write();
PmtX->Write();
PmtY->Write();
Veto->Write();
Time->Write();
TimeClean->Write();
Par->Write();
delete SiPmX;
delete SiPmY;
delete SiPmCleanX;
delete SiPmCleanY;
delete PmtX;
delete PmtY;
delete Veto;
delete Time;
delete TimeClean;
delete Par;
f->Close();
}
void FillTimeHists(void)
{
int i, N;
N = user->nhits();
for (i=0; i<N; i++) if (Energy(i) > MINENERGY4TIME && user->t_raw(i) > 0) {
hTimeDelta[user->adc(i)-1][user->adcChan(i)]->Fill(user->t_raw(i) - DanssEvent.fineTime);
// time relative to PMT - we need some common calibration of delays
if (PmtFineTime > 0) hPMTTimeDelta[user->adc(i)-1][user->adcChan(i)]->Fill(user->t_raw(i) - PmtFineTime);
}
}
void FindFineTime(void)
{
double tsum;
double asum;
double e;
int i, k, n, N;
double PMTsumT;
double PMTsumA;
tsum = asum = 0;
PMTsumT = PMTsumA = 0;
k = 0;
n = 0;
N = user->nhits();
for (i=0; i<N; i++) if (HitFlag[i] >= 0) {
switch(user->type(i)) {
case bSiPm:
e = Energy(i);
if (user->npix(i) < MINSIPMPIXELS) e = 0;
break;
case bPmt:
e = Energy(i);
PMTsumT += user->t_raw(i) * e;
PMTsumA += e;
n++;
break;
case bVeto:
e = 0; // exclude veto from time averaging
break;
}
if (e > MINENERGY4TIME && user->t_raw(i) > MINAVRTIME) {
tsum += user->t_raw(i) * e;
asum += e;
k++;
}
}
DanssEvent.fineTime = (asum > 0) ? tsum / asum : NOFINETIME; // some large number if not usable hits found
if (DanssEvent.trigType == masterTrgRandom) DanssEvent.fineTime = 200; // some fixed good time
PmtFineTime = -1;
if (n >= 2 && PMTsumA > 2) PmtFineTime = PMTsumT / PMTsumA;
}
// Do additional smear of energy for MC events
void MCSmear(void)
{
DanssEvent.PositronEnergy = MCEnergySmear(DanssEvent.PositronEnergy);
DanssEvent.TotalEnergy = MCEnergySmear(DanssEvent.TotalEnergy);
DanssEvent.AnnihilationEnergy = MCEnergySmear(DanssEvent.AnnihilationEnergy);
DanssEvent.NeutronEnergy = MCEnergySmear(DanssEvent.NeutronEnergy);
}
void StoreHits(void)
{
int i, j, N;
j = 0;
N = user->nhits();
for (i=0; i<N; i++) if (HitFlag[i] >= 0) {
HitArray.E[j] = Energy(i);
HitArray.T[j] = user->t_raw(i);
HitArray.type[j].type = user->type(i);
HitArray.type[j].flag = HitFlag[i];
switch (user->type(i)) {
case bSiPm:
HitArray.type[j].z = user->zCoord(i);
HitArray.type[j].xy = user->firstCoord(i);
break;
case bPmt: