-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCutOptimizer.C
894 lines (761 loc) · 44.2 KB
/
CutOptimizer.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
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <utility>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <TCanvas.h>
#include <TChain.h>
#include <TFile.h>
#include <TH1.h>
#include <TLine.h>
#include <TTree.h>
// using namespace std;
//This defines our current settings for the fiducial volume
float FVx = 256.35;
float FVy = 233;
float FVz = 1036.8;
float borderx = 10.;
float bordery = 20.;
float borderz = 10.;
float cryoradius = 191.61;
float cryoz = 1086.49 + 2*67.63;
//This function returns if a 3D point is within the fiducial volume
bool inFV(double x, double y, double z);
double FlashTrackDist(double flash, double start, double end);
bool inCryostat(double x, double y, double z);
// Main function
int CutOptimizer(std::string GeneratorName, unsigned int ThreadNumber, unsigned int NumberOfThreads)
{
std::string Version = "v05_08_00";
// std::string GeneratorName = "prodgenie_bnb_nu_cosmic";
// std::string GeneratorName = "prodgenie_bnb_nu";
// std::string GeneratorName = "prodcosmics_corsika_inTime";
// std::string GeneratorName = "prodgenie_bnb_nu_cosmic_sc_uboone";
// std::string GeneratorName = "data_onbeam_bnb";
// std::string GeneratorName = "data_offbeam_bnbext";
// std::string GeneratorName = "prodgenie_bnb_nu_cosmic_uboone";
// Initialize and fill track reco product names
std::vector<std::string> TrackProdNameVec;
// TrackProdNameVec.push_back("pandoraNuKHit");
// TrackProdNameVec.push_back("pandoraCosmic");
TrackProdNameVec.push_back("pandoraNu");
// TrackProdNameVec.push_back("pmtrack");
// TrackProdNameVec.push_back("pandoraNuPMA");
// TrackProdNameVec.push_back("trackkalmanhit");
// Initialize and fill vertex reco product names
std::vector<std::string> VertexProdNameVec;
// VertexProdNameVec.push_back("nuvtx");
// VertexProdNameVec.push_back("pandoraCosmic");
VertexProdNameVec.push_back("pandoraNu");
// VertexProdNameVec.push_back("pmtrack");
std::string FileNumberStr;
if(NumberOfThreads > 1)
{
FileNumberStr = "_" + std::to_string(ThreadNumber);
}
else
{
FileNumberStr = "";
}
std::cout << "Data Sample : " << GeneratorName << std::endl;
TChain *treenc = new TChain("analysistree/anatree");
if(GeneratorName == "data_onbeam_bnb")
{
// treenc -> Add( ("/pnfs/uboone/persistent/users/aschu/onbeam_data_bnbSWtrigger/"+GeneratorName+"_"+Version+"_anatree.root").c_str() );
std::ifstream FileNames("/pnfs/uboone/persistent/users/aschu/devel/v05_11_01/hadd/GOODBNBBEAM/filesana.list");
std::string FileName;
while(std::getline(FileNames,FileName))
{
std::cout << FileName << std::endl;
treenc -> Add((FileName).c_str());
}
}
else if(GeneratorName == "data_offbeam_bnbext")
{
// treenc -> Add( ("/pnfs/uboone/persistent/users/aschu/offbeam_data_bnbSWtrigger/"+GeneratorName+"_"+Version+"_anatree.root").c_str() );
std::ifstream FileNames("/pnfs/uboone/persistent/users/aschu/devel/v05_11_01/hadd/GOODEXTBNB/filesana.list");
std::string FileName;
while(std::getline(FileNames,FileName))
{
std::cout << FileName << std::endl;
treenc -> Add((FileName).c_str());
}
}
else if(GeneratorName == "prodgenie_bnb_nu_cosmic_uboone_field")
{
// treenc -> Add( ("/pnfs/uboone/persistent/users/aschu/offbeam_data_bnbSWtrigger/"+GeneratorName+"_"+Version+"_anatree.root").c_str() );
std::ifstream FileNames("/pnfs/uboone/persistent/users/sowjanya/v05_08_00/bnbpluscosmics_nominal_scOn_100k/ana/filesana.list");
std::string FileName;
while(std::getline(FileNames,FileName))
{
std::cout << FileName << std::endl;
treenc -> Add((FileName).c_str());
}
}
else if(GeneratorName == "prodgenie_bnb_nu_cosmic_uboone")
{
treenc -> Add( ("/pnfs/uboone/persistent/users/aschu/MC_BNB_Cosmic/"+GeneratorName+"_"+Version+"_anatree.root").c_str() );
}
else if(GeneratorName == "TEM" || GeneratorName == "MEC" || GeneratorName == "MA" || GeneratorName == "BITE")
{
treenc -> Add( ("/pnfs/uboone/persistent/users/aschu/"+GeneratorName+"/"+GeneratorName+"merge.root").c_str() );
}
else if(GeneratorName == "test")
{
for(unsigned int index = 1; index < 8; index++)
{
treenc -> Add( ("/uboone/data/users/aschu/FilterTestFiles/ana_hist_"+std::to_string(index)+".root").c_str() );
}
}
else
{
treenc -> Add( ("/lheppc46/data/uBData/anatrees/"+GeneratorName+"_"+Version+"_anatree.root").c_str() );
}
// treenc -> Add( ("/media/christoph/200EFBDA63AA160B/anatrees/"+GeneratorName+"_"+Version+"_anatree.root").c_str() );
//maximum array sizes
const int maxentries = 35000;
const int maxtracks = 10000;
const int maxvtx = 500;
const int maxnu = 10;
const int maxmc = 10;
const int kMaxFlashes = 5000;
//Define variables to read from Tree
Int_t event;
Int_t run;
Int_t subrun;
Int_t triggerbits; //this is only important for data. 2048 = BNB stream
Double_t potbnb; //this is only important for data
Short_t ntracks_reco; //number of reco tracks
Short_t trkbestplane[maxtracks]; //plane that has most hits for a given track
Float_t trklen[maxtracks]; //track length
Float_t trkstartx[maxtracks];
Float_t trkstarty[maxtracks];
Float_t trkstartz[maxtracks];
Float_t trkendx[maxtracks];
Float_t trkendy[maxtracks];
Float_t trkendz[maxtracks];
Float_t trktheta[maxtracks];
Float_t trkphi[maxtracks];
Float_t trkmomrange[maxtracks]; //track momentum calculated from track range
Short_t trkId[maxtracks];
Short_t trkorigin[maxtracks][3]; //for MC only: which true particle contributes most hits to the reco track: 2 = cosmic. 1 = neutrino
Int_t TrackIDTruth[maxtracks][3]; // MC id matched with reco track
bool vertexatstart[maxtracks]; //for analysis: is the vertex at start of the track?
bool vertexatend[maxtracks]; //for analysis: ist the vertex at end of track?
Int_t no_flashes; //number of flashes in the event
Float_t flash_time[kMaxFlashes]; //flash time (in microseconds)
Float_t flash_pe[kMaxFlashes]; //total number of photoelectrons corresponding to the flash
Float_t flash_zcenter[kMaxFlashes]; //z center of flash (in cm)
Float_t flash_ycenter[kMaxFlashes]; //y center of flash (in cm)
Short_t nvtx;
Float_t vtxx[maxvtx];
Float_t vtxy[maxvtx];
Float_t vtxz[maxvtx];
//finding candidate nu interaction vertex in event
bool candvertex[maxvtx];
Short_t candtrack[maxvtx];
Float_t candlength[maxvtx];
Short_t numuvertex = -1;
Short_t mutrack = -1;
Float_t mutracklength = 0;
//MC truth
Int_t mcevts_truth; //neutrino interactions per event
Float_t nuvtxx_truth[maxmc]; //true vertex x (in cm)
Float_t nuvtxy_truth[maxmc];
Float_t nuvtxz_truth[maxmc];
Int_t ccnc_truth[maxmc]; //CC = 0, NC = 1
Int_t nuPDG_truth[maxmc]; //true neutrino pdg code. numu = 14
Int_t mode_truth[maxmc]; //QE = 0, RES = 1, DIS = 2
Int_t PDG_truth[maxtracks];
Int_t NumberOfMCTracks;
Float_t XMCTrackStart[maxtracks];
Float_t YMCTrackStart[maxtracks];
Float_t ZMCTrackStart[maxtracks];
Float_t XMCTrackEnd[maxtracks];
Float_t YMCTrackEnd[maxtracks];
Float_t ZMCTrackEnd[maxtracks];
Int_t MCTrackID[maxtracks];
Int_t MCTrueIndex[maxtracks];
//define cut variables
double flashwidth = 22;//14 80; //cm. Distance flash-track
double distcut = 38;//5; //cm. Distance track start/end to vertex
double lengthcut = 78;//102 75; //cm. Length of longest track
double beammin = 3.55/*-0.36*/; //us. Beam window start
double beammax = 5.15/*-0.36*/; //us. Beam window end
double PEthresh = 187;//50; //PE
double MCTrackToMCVtxDist = 0.5; //cm. distance between mc track start and mc vertex
double TrackToMCDist = 5; //cm. Distance track start/end to mcvertex
if(GeneratorName == "data_bnb" || GeneratorName == "data_onbeam_bnb")
{
std::cout << "Changed beam gate window for on-beam data" << std::endl;
beammin = 3.3;
beammax = 4.9;
}
if(GeneratorName == "data_offbeam_bnbext")
{
std::cout << "Changed beam gate window for off-beam data" << std::endl;
beammin = 3.65;
beammax = 5.25;
}
if(GeneratorName == "prodcosmics_corsika_inTime")
{
std::cout << "Changed beam gate window for Corsika sample" << std::endl;
beammin = 3.2;
beammax = 4.8;
}
// Loop over all product names
for(const auto& TrackingName : TrackProdNameVec)
{
for(const auto& VertexingName : VertexProdNameVec)
{
// Open output file
TFile* OutputFile = new TFile(("rootfiles/Cut_Optimizer_"+GeneratorName+"_"+Version+FileNumberStr+"_Mod.root").c_str(),"RECREATE");
// Make a clone tree which gets filled
treenc -> SetBranchAddress("event", &event);
treenc -> SetBranchAddress("potbnb", &potbnb);
treenc -> SetBranchAddress("no_flashes", &no_flashes);
treenc -> SetBranchAddress("flash_time", flash_time);
treenc -> SetBranchAddress("flash_pe", flash_pe);
treenc -> SetBranchAddress("flash_zcenter", flash_zcenter);
treenc -> SetBranchAddress("flash_ycenter", flash_ycenter);
treenc -> SetBranchAddress("mcevts_truth", &mcevts_truth);
treenc -> SetBranchAddress("nuvtxx_truth", nuvtxx_truth);
treenc -> SetBranchAddress("nuvtxy_truth", nuvtxy_truth);
treenc -> SetBranchAddress("nuvtxz_truth", nuvtxz_truth);
treenc -> SetBranchAddress("ccnc_truth", ccnc_truth);
treenc -> SetBranchAddress("nuPDG_truth", nuPDG_truth);
treenc -> SetBranchAddress("pdg", PDG_truth);
treenc -> SetBranchAddress("mode_truth", mode_truth);
treenc -> SetBranchAddress("geant_list_size", &NumberOfMCTracks);
treenc -> SetBranchAddress("StartPointx", XMCTrackStart);
treenc -> SetBranchAddress("StartPointy", YMCTrackStart);
treenc -> SetBranchAddress("StartPointz", ZMCTrackStart);
treenc -> SetBranchAddress("EndPointx", XMCTrackEnd);
treenc -> SetBranchAddress("EndPointy", YMCTrackEnd);
treenc -> SetBranchAddress("EndPointz", ZMCTrackEnd);
treenc -> SetBranchAddress("TrackId", MCTrackID);
treenc -> SetBranchAddress("MCTruthIndex", MCTrueIndex);
// Product specific stuff
treenc -> SetBranchAddress(("ntracks_"+TrackingName).c_str(),&ntracks_reco);
treenc -> SetBranchAddress(("trklen_"+TrackingName).c_str(), trklen);
treenc -> SetBranchAddress(("trkstartx_"+TrackingName).c_str(),trkstartx);
treenc -> SetBranchAddress(("trkstarty_"+TrackingName).c_str(),trkstarty);
treenc -> SetBranchAddress(("trkstartz_"+TrackingName).c_str(),trkstartz);
treenc -> SetBranchAddress(("trkendx_"+TrackingName).c_str(),trkendx);
treenc -> SetBranchAddress(("trkendy_"+TrackingName).c_str(),trkendy);
treenc -> SetBranchAddress(("trkendz_"+TrackingName).c_str(),trkendz);
treenc -> SetBranchAddress(("trktheta_"+TrackingName).c_str(),trktheta);
treenc -> SetBranchAddress(("trkphi_"+TrackingName).c_str(),trkphi);
treenc -> SetBranchAddress(("trkorigin_"+TrackingName).c_str(),trkorigin);
treenc -> SetBranchAddress(("trkidtruth_"+TrackingName).c_str(),TrackIDTruth);
treenc -> SetBranchAddress(("trkpidbestplane_"+TrackingName).c_str(), trkbestplane);
// Program hack to apply for non uniform naming of nuvtx
if(VertexingName != "nuvtx")
{
treenc -> SetBranchAddress(("nvtx_"+VertexingName).c_str(), &nvtx);
treenc -> SetBranchAddress(("vtxx_"+VertexingName).c_str(), vtxx);
treenc -> SetBranchAddress(("vtxy_"+VertexingName).c_str(), vtxy);
treenc -> SetBranchAddress(("vtxz_"+VertexingName).c_str(), vtxz);
}
else
{
treenc -> SetBranchAddress("nnuvtx", &nvtx);
treenc -> SetBranchAddress("nuvtxx", vtxx);
treenc -> SetBranchAddress("nuvtxy", vtxy);
treenc -> SetBranchAddress("nuvtxz", vtxz);
}
int theflash = -1;
int ntrue = 0;
int TrackCandidate;
int VertexCandidate;
int MCTrackCandidate;
int MCVertexCandidate;
int NuMuCCTrackCandidate;
unsigned int EventsWithFlash = 0;
unsigned int EventsVtxInFV = 0;
unsigned int EventsTrackNearVertex = 0;
unsigned int EventsFlashMatched = 0;
unsigned int EventsTracksInFV = 0;
unsigned int EventsNearVtx = 0;
unsigned int EventsTrackLong = 0;
unsigned int EventsTruelyReco = 0;
unsigned int MCEventsWithFlash = 0;
unsigned int MCEventsVtxInFV = 0;
unsigned int MCEventsTrackNearVertex = 0;
unsigned int MCEventsFlashMatched = 0;
unsigned int MCEventsTracksInFV = 0;
unsigned int MCEventsNearVtx = 0;
unsigned int MCEventsTrackLong = 0;
unsigned int NumberOfSignalTruth = 0;
unsigned int NumberOfSignalTruthSel = 0;
unsigned int NumberOfBgrNCTruthSel = 0;
unsigned int NumberOfBgrNumuBarTruthSel = 0;
unsigned int NumberOfBgrNueTruthSel = 0;
unsigned int NumberOfBgrCosmicSel = 0;
unsigned int NumberOfBgrNuOutFVSel = 0;
unsigned int NumberOfUnknownCCBgr = 0;
unsigned int NumberOfUnknownBgr = 0;
double TotalPOT = 0.0;
TH1F* FlashSignal = new TH1F("FlashSignal","FlashSignal",1500,0,1500);
TH1F* FlashBGR = new TH1F("FlashBGR","FlashBGR",1500,0,1500);
TH1F* VtxDistanceSignal = new TH1F("VtxDistanceSignal","VtxDistanceSignal",500,0,10);
TH1F* VtxDistanceBGR = new TH1F("VtxDistanceBGR","VtxDistanceBGR",500,0,10);
TH1F* TrueVtxDistanceSignal = new TH1F("TrueVtxDistanceSignal","TrueVtxDistanceSignal",500,0,500);
TH1F* TrueVtxDistanceBGR = new TH1F("TrueVtxDistanceBGR","TrueVtxDistanceBGR",500,0,500);
TH1F* XVtxPosSignal = new TH1F("XVtxPosSignal","XVtxPosSignal",128,0,128);
TH1F* XVtxPosBGR = new TH1F("XVtxPosBGR","XVtxPosBGR",128,0,128);
TH1F* YVtxPosSignal = new TH1F("YVtxPosSignal","YVtxPosSignal",117,0,117);
TH1F* YVtxPosBGR = new TH1F("YVtxPosBGR","YVtxPosBGR",117,0,117);
TH1F* ZVtxPosSignal = new TH1F("ZVtxPosSignal","ZVtxPosSignal",519,0,519);
TH1F* ZVtxPosBGR = new TH1F("ZVtxPosBGR","ZVtxPosBGR",519,0,519);
TH1F* FlashDistSignal = new TH1F("FlashDistSignal","FlashDistSignal",500,0,500);
TH1F* FlashDistBGR = new TH1F("FlashDistBGR","FlashDistBGR",500,0,500);
TH1F* TrackRangeSignal = new TH1F("TrackRangeSignal","TrackRangeSignal",1000,0,1000);
TH1F* TrackRangeBGR = new TH1F("TrackRangeBGR","TrackRangeBGR",1000,0,1000);
unsigned long int Size = treenc -> GetEntries();
// Set start and end event number for multiple threads
unsigned long int StartEvent = Size*(ThreadNumber - 1)/NumberOfThreads;
unsigned long int EndEvent = Size*ThreadNumber/NumberOfThreads;
std::cout << "number of events used is: " << EndEvent-StartEvent << " of " << Size << std::endl;
//Event Loop
for(int i = StartEvent; i < EndEvent; i++)
{
if( (i == 121558 && GeneratorName == "MEC") || ((i == 2633 || i == 22955) && GeneratorName == "prodgenie_bnb_nu_cosmic_uboone_field") ) continue;
if(i%1000 == 0) std::cout << "\t... " << i << std::endl;
// Get tree entries
treenc -> GetEntry(i);
MCTrackCandidate = -1;
MCVertexCandidate = -1;
NuMuCCTrackCandidate = -1;
std::vector<int> MCTrackCandVec;
std::vector<int> MCVertexCandVec;
// Loop over all MC neutrino vertices
for(unsigned vertex_no = 0; vertex_no < mcevts_truth; vertex_no++)
{
// Check if there is a numuCC vertex in the FV
if( nuPDG_truth[vertex_no] == 14 && ccnc_truth[vertex_no] == 0 && inFV(nuvtxx_truth[vertex_no],nuvtxy_truth[vertex_no],nuvtxz_truth[vertex_no]) )
{
// Increase truth count
NumberOfSignalTruth++;
// Loop over all MC particles
for(unsigned track_no = 0; track_no < NumberOfMCTracks; track_no++)
{
// If the a muon is not contained in a singel neutrino event, set mc-track contained flag to false
if( PDG_truth[track_no] == 13 && MCTrueIndex[track_no] == vertex_no
&& sqrt(pow(XMCTrackStart[track_no] - nuvtxx_truth[vertex_no],2) + pow(YMCTrackStart[track_no] - nuvtxy_truth[vertex_no],2) + pow(ZMCTrackStart[track_no] - nuvtxz_truth[vertex_no],2)) < MCTrackToMCVtxDist )
{
MCTrackCandVec.push_back(track_no);
MCVertexCandVec.push_back(vertex_no);
// Fill track candidate index
NuMuCCTrackCandidate = track_no;
}
} // MC particle loop
} // If numuCC in FV
} // MC vertex loop
bool flashtag = false;
float flashmax = 0.;
bool flashtagcut = false;
float flashmaxcut = 0.;
// Loop over all flashes
for(int f = 0; f < no_flashes; f++)
{
// If the flash is in the beam window and above threshold set flashtag to true
if(flash_time[f] > beammin && flash_time[f] < beammax)
{
if(flashmaxcut < flash_pe[f])
{
flashmaxcut = flash_pe[f];
flashtagcut = true;
}
// If the new flash has more PE than the current maximum, replace the maximum
if(flash_pe[f] > PEthresh && flash_pe[f] > flashmax)
{
flashtag = true; //the event does have a flash inside the beam window
theflash = f;
flashmax = flash_pe[f];
}
}
} // flash loop
if(flashtagcut)
{
if(NuMuCCTrackCandidate > -1) FlashSignal->Fill(flashmaxcut);
else FlashBGR->Fill(flashmaxcut);
}
// If the flash tag is ture and we have POT
if(flashtag)
{
// Prepare flags
bool VertexInFVFlag = true;
bool TrackDistanceFlag = true;
bool FlashMatchFlag = true;
bool TrackContainedFlag = false;
float diststart = 0;
float distend = 0;
double TrackRange = 0;
// Increase events with flash > 50 PE and within beam window
EventsWithFlash++;
if(NuMuCCTrackCandidate > -1)
MCEventsWithFlash++;
std::vector<int> CandVertex(MCVertexCandVec.size());
std::vector<float> MinDistToCand(MCVertexCandVec.size(),9999.f);
// Loop over MC truth candidate verteces
for(unsigned int mcv = 0; mcv < MCVertexCandVec.size(); ++mcv)
{
int true_v = MCVertexCandVec.at(mcv);
// Loop over all vertices
for(int v = 0; v < nvtx; v++)
{
// Find reco vertex with minimum distance to true vertex and store these as vertex candidates
float TempDist = std::sqrt( std::pow(nuvtxx_truth[true_v]-vtxx[v],2)+std::pow(nuvtxy_truth[true_v]-vtxy[v],2)+std::pow(nuvtxz_truth[true_v]-vtxz[v],2) );
if(TempDist < MinDistToCand.at(mcv))
{
CandVertex.at(mcv) = v;
MinDistToCand.at(mcv) = TempDist;
}
}
}
// Initialize a vertex and associated track collection
std::map< int,std::vector<int> > VertexTrackCollection;
// Stores the distance to every minimum track to vtx distance for every track and a boolian which shows if background
std::vector<std::pair<bool,float>> VtxDistMinVec(ntracks_reco,std::make_pair(false,9999.f));
// Loop over all vertices
for(int v = 0; v < nvtx; v++)
{
// Reset track at vertex count
unsigned int TrackCountAtVertex = 0;
// Loop over all reconstructed tracks
for(int j = 0; j < ntracks_reco; j++)
{
// Calculate distances from track start/end to vertex and calculate track lenth
diststart = std::sqrt( std::pow(vtxx[v] - trkstartx[j],2) + std::pow(vtxy[v] - trkstarty[j],2) + std::pow(vtxz[v] - trkstartz[j],2) );
distend = std::sqrt( std::pow(vtxx[v] - trkendx[j],2) + std::pow(vtxy[v] - trkendy[j],2) + std::pow(vtxz[v] - trkendz[j],2) );
TrackRange = std::sqrt( std::pow(trkstartx[j] - trkendx[j],2) + std::pow(trkstarty[j] - trkendy[j],2) + std::pow(trkstartz[j] - trkendz[j],2) );
// Loop through all candidate indices
for(const auto& v_cand : CandVertex)
{
// check if signal or background track and if the candidate vertex is chosen
if(v_cand == v && trkorigin[j][trkbestplane[j]] == 1) TrueVtxDistanceSignal->Fill(std::min(diststart,distend));
else if (v_cand == v) TrueVtxDistanceBGR->Fill(std::min(diststart,distend));
}
// If neutrino related track, change flags to signal
if(v == 0 && NuMuCCTrackCandidate > -1 && trkorigin[j][trkbestplane[j]] == 1)
{
VtxDistMinVec.at(j).first = true;
}
// Fill minimum of track to vertex distance
VtxDistMinVec.at(j).second = std::min(VtxDistMinVec.at(j).second, std::min(diststart,distend));
// If the track vertex distance is within cut, increase track count
if(diststart < distcut || distend < distcut)
{
// Increase count of events with a distance to vertex < 5 cm
if(TrackDistanceFlag)
{
EventsTrackNearVertex++;
if(NuMuCCTrackCandidate > -1)
MCEventsTrackNearVertex++;
TrackDistanceFlag = false;
}
// If we are looking at the first track which fulfills the distance to vertex cut
if(!TrackCountAtVertex)
{
// Fill vertex ID into the collection map
VertexTrackCollection.insert(std::pair< int,std::vector<int> >(v,std::vector<int>()));
}
// Push back track ID for vertex v
VertexTrackCollection.at(v).push_back(j);
// Increase track at vertex count
TrackCountAtVertex++;
}
} // track loop
} // vertex loop
// Loop over all minimum distance pairs
for(const auto& MinDistance : VtxDistMinVec)
{
// If signal fill signal histogram if not fill bgr histogram
if(MinDistance.first)
{
VtxDistanceSignal->Fill(MinDistance.second);
}
else
{
VtxDistanceBGR->Fill(MinDistance.second);
}
}
// Vertex candidate properties
VertexCandidate = -1;
float VertexCosTheta = 0.0;
// Loop over the collection of vertices
for(auto const& VtxTrack : VertexTrackCollection)
{
// Get vertexID
int VertexID = VtxTrack.first;
// Weighted cos theta average
float WeightedCosTheta = 0.0;
// Normalization factor
float NormFactor = 0.0;
// Loop over all associated track IDs of this vertex
for(auto const& TrackID : VtxTrack.second)
{
TrackRange = sqrt(pow(trkstartx[TrackID] - trkendx[TrackID],2) + pow(trkstarty[TrackID] - trkendy[TrackID],2) + pow(trkstartz[TrackID] - trkendz[TrackID],2));
// Add up numbers
NormFactor += TrackRange;
WeightedCosTheta += TrackRange*cos(trktheta[TrackID]);
}// track loop
// Make average
WeightedCosTheta /= NormFactor;
// Check for flatest angle (also backwards pointing)
if(fabs(WeightedCosTheta) > VertexCosTheta)
{
VertexCandidate = VertexID;
VertexCosTheta = fabs(WeightedCosTheta);
}
}// vertex collection loop
// Check if vertex has neutrino track
bool IsNeutrinoInteraction = false;
if(VertexCandidate > -1)
{
// Loop over all tracks of the vertex candidate
for(auto const& track_no : VertexTrackCollection.at(VertexCandidate))
{
if(trkorigin[track_no][trkbestplane[track_no]] == 1) IsNeutrinoInteraction = true;
}
}
if(IsNeutrinoInteraction && NuMuCCTrackCandidate > -1)
{
XVtxPosSignal->Fill(std::min(vtxx[VertexCandidate],FVx-vtxx[VertexCandidate]));
YVtxPosSignal->Fill(FVy/2.-std::fabs(vtxy[VertexCandidate]));
ZVtxPosSignal->Fill(std::min(vtxz[VertexCandidate],FVz-vtxz[VertexCandidate]));
}
else
{
XVtxPosBGR->Fill(std::min(vtxx[VertexCandidate],FVx-vtxx[VertexCandidate]));
YVtxPosBGR->Fill(FVy/2.-std::fabs(vtxy[VertexCandidate]));
ZVtxPosBGR->Fill(std::min(vtxz[VertexCandidate],FVz-vtxz[VertexCandidate]));
}
// Initialize Track Candidate properties
TrackCandidate = -1;
float TrackCandLength = 0.0;
// Check if vertex candidate is contained in FV
if(inFV(vtxx[VertexCandidate], vtxy[VertexCandidate], vtxz[VertexCandidate]))
{
// Increase count of events with a vertex in FV
if(VertexInFVFlag)
{
EventsVtxInFV++;
if(NuMuCCTrackCandidate > -1)
MCEventsVtxInFV++;
VertexInFVFlag = false;
}
// Looping over track IDs of tracks associated with the vertex candidate
for(auto const& TrackID : VertexTrackCollection.find(VertexCandidate)->second)
{
TrackRange = sqrt(pow(trkstartx[TrackID] - trkendx[TrackID],2) + pow(trkstarty[TrackID] - trkendy[TrackID],2) + pow(trkstartz[TrackID] - trkendz[TrackID],2));
// Check for if track is longer
if(TrackRange > TrackCandLength)
{
TrackCandidate = TrackID;
TrackCandLength = TrackRange;
}
}
} // if vertex is contained in FV
// If there is a track candidate
if(TrackCandidate > -1)
{
if(trkorigin[TrackCandidate][trkbestplane[TrackCandidate]] == 1 && NuMuCCTrackCandidate > -1 /*&& MCTrackID[NuMuCCTrackCandidate] == TrackIDTruth[TrackCandidate][trkbestplane[TrackCandidate]]*/)
{
FlashDistSignal->Fill(FlashTrackDist(flash_zcenter[theflash], trkstartz[TrackCandidate], trkendz[TrackCandidate]));
}
else FlashDistBGR->Fill(FlashTrackDist(flash_zcenter[theflash], trkstartz[TrackCandidate], trkendz[TrackCandidate]));
// If the longest track is flash matched
if( FlashTrackDist(flash_zcenter[theflash], trkstartz[TrackCandidate], trkendz[TrackCandidate]) < flashwidth )
{
if(FlashMatchFlag)
{
EventsFlashMatched++;
if(NuMuCCTrackCandidate > -1)
MCEventsFlashMatched++;
FlashMatchFlag = false;
// Set track contained flag true, so the other cuts can be applied on this vertex
TrackContainedFlag = true;
}
// If the longest track is fully contained
if( inFV(trkstartx[TrackCandidate], trkstarty[TrackCandidate], trkstartz[TrackCandidate])
&& inFV(trkendx[TrackCandidate], trkendy[TrackCandidate], trkendz[TrackCandidate])
&& TrackContainedFlag )
{
EventsTracksInFV++;
if(NuMuCCTrackCandidate > -1)
MCEventsTracksInFV++;
if(trkorigin[TrackCandidate][trkbestplane[TrackCandidate]] == 1 && NuMuCCTrackCandidate > -1 && MCTrackID[NuMuCCTrackCandidate] == TrackIDTruth[TrackCandidate][trkbestplane[TrackCandidate]])
{
TrackRangeSignal->Fill(TrackCandLength);
}
else TrackRangeBGR->Fill(TrackCandLength);
// If longest track is longer than 75 cm
if(TrackCandLength > lengthcut)
{
// If track origin is neutrino
if(trkorigin[TrackCandidate][trkbestplane[TrackCandidate]] == 1)
{
// Loop over all MCTracks
for(unsigned track_no = 0; track_no < NumberOfMCTracks; track_no++)
{
// If MCTrackID is the same as the back tracked TruthID
if(MCTrackID[track_no] == TrackIDTruth[TrackCandidate][trkbestplane[TrackCandidate]])
{
// Store new MCTrackCandidate and MCVertexCandidate
MCTrackCandidate = track_no;
MCVertexCandidate = MCTrueIndex[track_no];
}
} // end MCtrack loop
} // if neutrino origin
EventsTrackLong++;
if(NuMuCCTrackCandidate > -1)
MCEventsTrackLong++;
if(MCVertexCandidate > -1 && ccnc_truth[MCVertexCandidate] == 0 && trkorigin[TrackCandidate][trkbestplane[TrackCandidate]] == 1)
{
if(nuPDG_truth[MCVertexCandidate] == 14 && inFV(nuvtxx_truth[MCVertexCandidate],nuvtxy_truth[MCVertexCandidate],nuvtxz_truth[MCVertexCandidate]))
{
NumberOfSignalTruthSel++;
}
else if(nuPDG_truth[MCVertexCandidate] == -14) // if anti-neutrino
{
NumberOfBgrNumuBarTruthSel++;
}
else if(abs(nuPDG_truth[MCVertexCandidate]) == 12) // if electron like neutrino
{
NumberOfBgrNueTruthSel++;
}
else if(!inFV(nuvtxx_truth[MCVertexCandidate],nuvtxy_truth[MCVertexCandidate],nuvtxz_truth[MCVertexCandidate])) // if not in fiducial volume
{
NumberOfBgrNuOutFVSel++;
}
else
{
NumberOfUnknownCCBgr++;
}
}
else if(MCVertexCandidate > -1 && ccnc_truth[MCVertexCandidate] == 1 && trkorigin[TrackCandidate][trkbestplane[TrackCandidate]] == 1) // else if NC interaction
{
NumberOfBgrNCTruthSel++;
}
else if(trkorigin[TrackCandidate][trkbestplane[TrackCandidate]] != 1) // If selected track is not associated to a neutrino
{
NumberOfBgrCosmicSel++;
}
else
{
NumberOfUnknownBgr++;
}
double TrkStartMCStartDist = sqrt(pow(XMCTrackStart[MCTrackCandidate] - trkstartx[TrackCandidate],2) + pow(YMCTrackStart[MCTrackCandidate] - trkstarty[TrackCandidate],2) + pow(ZMCTrackStart[MCTrackCandidate] - trkstartz[TrackCandidate],2));
double TrkEndMCEndDist = sqrt(pow(XMCTrackEnd[MCTrackCandidate] - trkendx[TrackCandidate],2) + pow(YMCTrackEnd[MCTrackCandidate] - trkendy[TrackCandidate],2) + pow(ZMCTrackEnd[MCTrackCandidate] - trkendz[TrackCandidate],2));
double TrkStartMCEndDist = sqrt(pow(XMCTrackEnd[MCTrackCandidate] - trkstartx[TrackCandidate],2) + pow(YMCTrackEnd[MCTrackCandidate] - trkstarty[TrackCandidate],2) + pow(ZMCTrackEnd[MCTrackCandidate] - trkstartz[TrackCandidate],2));
double TrkEndMCStartDist = sqrt(pow(XMCTrackStart[MCTrackCandidate] - trkendx[TrackCandidate],2) + pow(YMCTrackStart[MCTrackCandidate] - trkendy[TrackCandidate],2) + pow(ZMCTrackStart[MCTrackCandidate] - trkendz[TrackCandidate],2));
// If the MC vertex candidate is in the FV and the muon of the numu CC interaction was picked
if( MCVertexCandidate > -1 && inFV(nuvtxx_truth[MCVertexCandidate],nuvtxy_truth[MCVertexCandidate],nuvtxz_truth[MCVertexCandidate])
&& PDG_truth[MCTrackCandidate] == 13 && MCTrackID[MCTrackCandidate] == TrackIDTruth[TrackCandidate][trkbestplane[TrackCandidate]] )
{
EventsTruelyReco++;
}
}
}
// Set track contained flag false
TrackContainedFlag = false;
} // if track candidate is flash matched
} // if there is a track candidate
} // if flashtag
// Increase the neutrino count
ntrue++;
// Add POT count
TotalPOT += potbnb;
}//loop over all events
OutputFile->cd();
FlashSignal->Write();
FlashBGR->Write();
VtxDistanceSignal->Write();
VtxDistanceBGR->Write();
TrueVtxDistanceSignal->Write();
TrueVtxDistanceBGR->Write();
XVtxPosSignal->Write();
XVtxPosBGR->Write();
YVtxPosSignal->Write();
YVtxPosBGR->Write();
ZVtxPosSignal->Write();
ZVtxPosBGR->Write();
FlashDistSignal->Write();
FlashDistBGR->Write();
TrackRangeSignal->Write();
TrackRangeBGR->Write();
std::cout << "--------------------------------------------------------------------------------------------" << std::endl;
std::cout << std::endl;
std::cout << "Track Reco Product Name : " << TrackingName << " Vertex Reco Product Name : " << VertexingName << std::endl;
std::cout << "Total POT : " << TotalPOT*1e12 << std::endl;
std::cout << "number of CC events with vertex in FV : " << ntrue << std::endl;
std::cout << "number of events with flash > 50 PE : " << EventsWithFlash << " " << MCEventsWithFlash << std::endl;
std::cout << "number of events with track start/end within 5cm to vtx : " << EventsTrackNearVertex << " " << MCEventsTrackNearVertex << std::endl;
std::cout << "number of events with vtx in FV : " << EventsVtxInFV << " " << MCEventsVtxInFV << std::endl;
std::cout << "number of events with tracks matched within 80cm to flash : " << EventsFlashMatched << " " << MCEventsFlashMatched << std::endl;
std::cout << "number of events with contained tracks : " << EventsTracksInFV << " " << MCEventsTracksInFV << std::endl;
std::cout << "number of events with longest track > 75cm : " << EventsTrackLong << " " << MCEventsTrackLong << std::endl;
std::cout << "number of events with track start end within 5cm to mc-vtx : " << EventsTruelyReco << std::endl;
std::cout << "number of events with contained MC tracks : " << NumberOfSignalTruth << std::endl;
std::cout << "number of well selected events : " << NumberOfSignalTruthSel << std::endl;
std::cout << "number of NC events selected : " << NumberOfBgrNCTruthSel << std::endl;
std::cout << "number of anti-Neutrino events selected : " << NumberOfBgrNumuBarTruthSel << std::endl;
std::cout << "number of Nu_e events selected : " << NumberOfBgrNueTruthSel << std::endl;
std::cout << "number of events selected cosmic : " << NumberOfBgrCosmicSel << std::endl;
std::cout << "number of nu_mu events out of FV : " << NumberOfBgrNuOutFVSel <<std::endl;
std::cout << "event selection efficiency : " << (float)NumberOfSignalTruthSel/(float)NumberOfSignalTruth << std::endl;
// std::cout << "event selection purity : " << (float)NumberOfSignalTruthSel/(float)(NumberOfBgrNCTruthSel+NumberOfBgrNumuBarTruthSel+NumberOfBgrNueTruthSel)
std::cout << "event selection correctness : " << (float)EventsTruelyReco/(float)EventsTrackLong << std::endl;
// std::cout << "event selection missid rate : " << fabs((float)EventsTruelyReco-(float)NumberOfSignalTruth)/(float)NumberOfSignalTruth << std::endl;
std::cout << std::endl;
std::cout << "number of unknown CC bgr : " << NumberOfUnknownCCBgr << std::endl;
std::cout << "number of unknown bgr : " << NumberOfUnknownBgr << std::endl;
std::cout << std::endl;
std::cout << "--------------------------------------------------------------------------------------------" << std::endl;
OutputFile->Close();
// Erase all branch addresses for the next iteration
treenc -> ResetBranchAddresses();
} // Loop over all vertexing data products
} // Loop over all tracking data products
return 0;
} // end main function
//This function returns if a 3D point is within the fiducial volume
bool inFV(double x, double y, double z)
{
if(x < (FVx - borderx) && (x > borderx) && (y < (FVy/2. - bordery)) && (y > (-FVy/2. + bordery)) && (z < (FVz - borderz)) && (z > borderz)) return true;
else return false;
}
//This function returns the distance between a flash and a track (in one dimension, here used only for z direction)
double FlashTrackDist(double flash, double start, double end)
{
if(end >= start) {
if(flash < end && flash > start) return 0;
else return TMath::Min(fabs(flash-start), fabs(flash-end));
}
else {
if(flash > end && flash < start) return 0;
else return TMath::Min(fabs(flash-start), fabs(flash-end));
}
}
bool inCryostat(double x, double y, double z)
{
// If out of cylinder axis set false
if(z < FVz/2-cryoz/2 || z > FVz/2+cryoz/2)
{
return false;
}
else if(sqrt( pow(x-FVx/2,2) + pow(y,2)) > cryoradius)
{
return false;
}
else
{
return true;
}
}