-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataClassify.cc
More file actions
1462 lines (1194 loc) · 67.9 KB
/
DataClassify.cc
File metadata and controls
1462 lines (1194 loc) · 67.9 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
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
#include "TFile.h"
#include "TTree.h"
#include "TString.h"
#include "TCanvas.h"
#include "TColor.h"
#include "TTree.h"
#include "TVector3.h"
#include "TMatrixDSym.h"
#include "TMatrixDSymEigen.h"
#include "TVectorD.h"
#include "TEfficiency.h"
#include <vector>
#include "Helpers.cc"
void DataClassify() {
// Set defaults
gStyle->SetOptStat(0); // get rid of stats box
TH1D::SetDefaultSumw2();
TH2D::SetDefaultSumw2();
gStyle->SetPalette(kRainBow);
TGraph* gProton = new TGraph();
TGraph* gPion = new TGraph();
TGraph* gMuonTG = new TGraph();
// Initialize points
initializeProtonPoints(gProton);
initializePionPoints(gPion);
initializeMuonNoBraggPoints(gMuonTG);
int FontStyle = 132;
double TextSize = 0.06;
TString SaveDir = "/exp/lariat/app/users/epelaez/analysis/figs/DataClassify/";
// Load files
TChain* Chain = new TChain("anatree/anatree");
Chain->Add("/exp/lariat/app/users/epelaez/files/anatree_60a_data/chunks/*.root");
std::cout << "Files: " << Chain->GetListOfFiles()->GetEntries() << std::endl;
// Load nominal MC histos
TString NominalHistsPath = "/exp/lariat/app/users/epelaez/histos/nominal/RecoClassify3Cat_AllHists.root";
std::unique_ptr<TFile> fNom(TFile::Open(NominalHistsPath, "READ"));
// Load nominal MC histos (abs + scatt)
TString NominalAbsScattHistsPath = "/exp/lariat/app/users/epelaez/analysis_abs_scatt/histos/nominal/RecoClassify3Cat_AllHists.root";
std::unique_ptr<TFile> fNomAbsScatt(TFile::Open(NominalAbsScattHistsPath, "READ"));
///////////////////////
// Counters for cuts //
///////////////////////
int TotalEvents = 0;
int EventsPassingProj = 0;
int EventsPassingAperture = 0;
int EventsPassingTOFMass = 0;
int EventsWithWCMatch = 0;
int EventsWithPickyWC = 0;
int EventsPassingTG = 0;
int EventsPassingSmall = 0;
int EventsInRedVol = 0;
int EventsPassingPrimaryPID = 0;
int EventsSelectedAsScatter = 0;
int EventsSelectedAsAbsNp = 0;
int EventsNoSecondary = 0;
int EventsSelectedAsAbs0p = 0;
//////////////////////////
// Get histos for plots //
//////////////////////////
TH1D* hMCIncidentKEPion = dynamic_cast<TH1D*>(fNom->Get("hIncidentKEPion"));
TH1D* hMCIncidentKEElectron = dynamic_cast<TH1D*>(fNom->Get("hIncidentKEElectron"));
TH1D* hMCIncidentKEMuon = dynamic_cast<TH1D*>(fNom->Get("hIncidentKEMuon"));
TH1D* hMCIncidentKEPionFine = dynamic_cast<TH1D*>(fNom->Get("hIncidentKEPionFine"));
TH1D* hMCIncidentKEElectronFine = dynamic_cast<TH1D*>(fNom->Get("hIncidentKEElectronFine"));
TH1D* hMCIncidentKEMuonFine = dynamic_cast<TH1D*>(fNom->Get("hIncidentKEMuonFine"));
TH1D* hMCSmallTrksInCylinderPions = dynamic_cast<TH1D*>(fNom->Get("hSmallTrksInCylinderPions"));
TH1D* hMCSmallTrksInCylinderElectrons = dynamic_cast<TH1D*>(fNom->Get("hSmallTrksInCylinderElectrons"));
TH1D* hMCSmallTrksInCylinderMuons = dynamic_cast<TH1D*>(fNom->Get("hSmallTrksInCylinderMuons"));
TH1D* hMCFrontFaceKEPion = dynamic_cast<TH1D*>(fNom->Get("hFrontFaceKEPion"));
TH1D* hMCFrontFaceKEElectron = dynamic_cast<TH1D*>(fNom->Get("hFrontFaceKEElectron"));
TH1D* hMCFrontFaceKEMuon = dynamic_cast<TH1D*>(fNom->Get("hFrontFaceKEMuon"));
TH1D* hMCFrontFacePionKEPion = dynamic_cast<TH1D*>(fNom->Get("hFrontFacePionKEPion"));
TH1D* hMCFrontFacePionKEElectron = dynamic_cast<TH1D*>(fNom->Get("hFrontFacePionKEElectron"));
TH1D* hMCFrontFacePionKEMuon = dynamic_cast<TH1D*>(fNom->Get("hFrontFacePionKEMuon"));
TH1D* hMCWCKEPion = dynamic_cast<TH1D*>(fNom->Get("hWCKEPion"));
TH1D* hMCWCKEMuon = dynamic_cast<TH1D*>(fNom->Get("hWCKEMuon"));
TH1D* hMCWCKEElectron = dynamic_cast<TH1D*>(fNom->Get("hWCKEElectron"));
// Total interacting
TH1D* hMCPionAbs0pKE = dynamic_cast<TH1D*>(fNom->Get("hPionAbs0pKE"));
TH1D* hMCPionAbsNpKE = dynamic_cast<TH1D*>(fNom->Get("hPionAbsNpKE"));
TH1D* hMCPionScatterKE = dynamic_cast<TH1D*>(fNom->Get("hPionScatterKE"));
// Abs 0p interacting KE
TH1D* hMCPionAbs0pKETrue = dynamic_cast<TH1D*>(fNom->Get("hPionAbs0pKETrue"));
TH1D* hMCPionAbs0pKEAbsNp = dynamic_cast<TH1D*>(fNom->Get("hPionAbs0pKEAbsNp"));
TH1D* hMCPionAbs0pKEScatter = dynamic_cast<TH1D*>(fNom->Get("hPionAbs0pKEScatter"));
TH1D* hMCPionAbs0pKEChExch = dynamic_cast<TH1D*>(fNom->Get("hPionAbs0pKEChExch"));
TH1D* hMCPionAbs0pKEMuon = dynamic_cast<TH1D*>(fNom->Get("hPionAbs0pKEMuon"));
TH1D* hMCPionAbs0pKEElectron = dynamic_cast<TH1D*>(fNom->Get("hPionAbs0pKEElectron"));
TH1D* hMCPionAbs0pKEOther = dynamic_cast<TH1D*>(fNom->Get("hPionAbs0pKEOther"));
// Abs Np interacting KE
TH1D* hMCPionAbsNpKETrue = dynamic_cast<TH1D*>(fNom->Get("hPionAbsNpKETrue"));
TH1D* hMCPionAbsNpKEAbs0p = dynamic_cast<TH1D*>(fNom->Get("hPionAbsNpKEAbs0p"));
TH1D* hMCPionAbsNpKEScatter = dynamic_cast<TH1D*>(fNom->Get("hPionAbsNpKEScatter"));
TH1D* hMCPionAbsNpKEChExch = dynamic_cast<TH1D*>(fNom->Get("hPionAbsNpKEChExch"));
TH1D* hMCPionAbsNpKEMuon = dynamic_cast<TH1D*>(fNom->Get("hPionAbsNpKEMuon"));
TH1D* hMCPionAbsNpKEElectron = dynamic_cast<TH1D*>(fNom->Get("hPionAbsNpKEElectron"));
TH1D* hMCPionAbsNpKEOther = dynamic_cast<TH1D*>(fNom->Get("hPionAbsNpKEOther"));
// Scatter interacting KE
TH1D* hMCPionScatterKETrue = dynamic_cast<TH1D*>(fNom->Get("hPionScatterKETrue"));
TH1D* hMCPionScatterKEAbs0p = dynamic_cast<TH1D*>(fNom->Get("hPionScatterKEAbs0p"));
TH1D* hMCPionScatterKEAbsNp = dynamic_cast<TH1D*>(fNom->Get("hPionScatterKEAbsNp"));
TH1D* hMCPionScatterKEChExch = dynamic_cast<TH1D*>(fNom->Get("hPionScatterKEChExch"));
TH1D* hMCPionScatterKEMuon = dynamic_cast<TH1D*>(fNom->Get("hPionScatterKEMuon"));
TH1D* hMCPionScatterKEElectron = dynamic_cast<TH1D*>(fNom->Get("hPionScatterKEElectron"));
TH1D* hMCPionScatterKEOther = dynamic_cast<TH1D*>(fNom->Get("hPionScatterKEOther"));
// Abs interacting KE (abs + scattering)
TH1D* hMCPionAbsKETrue = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionAbsKETrue"));
TH1D* hMCPionAbsKEScatter = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionAbsKEScatter"));
TH1D* hMCPionAbsKEChExch = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionAbsKEChExch"));
TH1D* hMCPionAbsKEMuon = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionAbsKEMuon"));
TH1D* hMCPionAbsKEElectron = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionAbsKEElectron"));
TH1D* hMCPionAbsKEOther = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionAbsKEOther"));
// Abs interacting KE
TH1D* hMCPionScatterKETrue2 = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionScatterKETrue"));
TH1D* hMCPionScatterKEAbs2 = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionScatterKEAbs"));
TH1D* hMCPionScatterKEChExch2 = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionScatterKEChExch"));
TH1D* hMCPionScatterKEMuon2 = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionScatterKEMuon"));
TH1D* hMCPionScatterKEElectron2 = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionScatterKEElectron"));
TH1D* hMCPionScatterKEOther2 = dynamic_cast<TH1D*>(fNomAbsScatt->Get("hPionScatterKEOther"));
// Track pitch and dE/dx for WC-matched tracks
TH1D* hMCTrackPitchPion = dynamic_cast<TH1D*>(fNom->Get("hTrackPitchPion"));
TH1D* hMCTrackPitchMuon = dynamic_cast<TH1D*>(fNom->Get("hTrackPitchMuon"));
TH1D* hMCTrackPitchElectron = dynamic_cast<TH1D*>(fNom->Get("hTrackPitchElectron"));
TH1D* hMCTrackdEdxPion = dynamic_cast<TH1D*>(fNom->Get("hTrackdEdxPion"));
TH1D* hMCTrackdEdxMuon = dynamic_cast<TH1D*>(fNom->Get("hTrackdEdxMuon"));
TH1D* hMCTrackdEdxElectron = dynamic_cast<TH1D*>(fNom->Get("hTrackdEdxElectron"));
// Secondary track mean dE/dx
TH1D* hMCMeandEdxSecondaryPions = dynamic_cast<TH1D*>(fNom->Get("hMeandEdxSecondaryPions"));
TH1D* hMCMeandEdxSecondaryProtons = dynamic_cast<TH1D*>(fNom->Get("hMeandEdxSecondaryProton"));
TH1D* hMCMeandEdxSecondaryOther = dynamic_cast<TH1D*>(fNom->Get("hMeandEdxSecondaryOther"));
///////////////////
// Load branches //
///////////////////
int run, subrun, event; bool isData = true;
Chain->SetBranchAddress("run", &run);
Chain->SetBranchAddress("subrun", &subrun);
Chain->SetBranchAddress("event", &event);
// Track information
int ntracks_reco; Chain->SetBranchAddress("ntracks_reco", &ntracks_reco);
static float trkvtxx[kMaxTrackData]; Chain->SetBranchAddress("trkvtxx", &trkvtxx);
static float trkvtxy[kMaxTrackData]; Chain->SetBranchAddress("trkvtxy", &trkvtxy);
static float trkvtxz[kMaxTrackData]; Chain->SetBranchAddress("trkvtxz", &trkvtxz);
static float trkendx[kMaxTrackData]; Chain->SetBranchAddress("trkendx", &trkendx);
static float trkendy[kMaxTrackData]; Chain->SetBranchAddress("trkendy", &trkendy);
static float trkendz[kMaxTrackData]; Chain->SetBranchAddress("trkendz", &trkendz);
static int trkWCtoTPCMatch[kMaxTrackData]; Chain->SetBranchAddress("trkWCtoTPCMatch", &trkWCtoTPCMatch);
// Wire-chamber track information
float beamline_mass; Chain->SetBranchAddress("beamline_mass", &beamline_mass);
int nwctrks; Chain->SetBranchAddress("nwctrks", &nwctrks);
static float wctrk_momentum[kMaxWCTracksData]; Chain->SetBranchAddress("wctrk_momentum", &wctrk_momentum);
static float wctrk_theta[kMaxWCTracksData]; Chain->SetBranchAddress("wctrk_theta", &wctrk_theta);
static float wctrk_phi[kMaxWCTracksData]; Chain->SetBranchAddress("wctrk_phi", &wctrk_phi);
static int wctrk_picky[kMaxWCTracksData]; Chain->SetBranchAddress("wctrk_picky", &wctrk_picky);
static float WC1xPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC1xPos", &WC1xPos);
static float WC1yPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC1yPos", &WC1yPos);
static float WC1zPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC1zPos", &WC1zPos);
static float WC2xPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC2xPos", &WC2xPos);
static float WC2yPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC2yPos", &WC2yPos);
static float WC2zPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC2zPos", &WC2zPos);
static float WC3xPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC3xPos", &WC3xPos);
static float WC3yPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC3yPos", &WC3yPos);
static float WC3zPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC3zPos", &WC3zPos);
static float WC4xPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC4xPos", &WC4xPos);
static float WC4yPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC4yPos", &WC4yPos);
static float WC4zPos[kMaxWCTracksData]; Chain->SetBranchAddress("WC4zPos", &WC4zPos);
// Calorimetry information
static int ntrkcalopts[kMaxTrackData][2]; Chain->SetBranchAddress("ntrkcalopts", &ntrkcalopts);
static float trkdedx[kMaxTrackData][2][kMaxTrackHitsData]; Chain->SetBranchAddress("trkdedx", &trkdedx);
static float trkrr[kMaxTrackData][2][kMaxTrackHitsData]; Chain->SetBranchAddress("trkrr", &trkrr);
static float trkpitch[kMaxTrackData][2][kMaxTrackHitsData]; Chain->SetBranchAddress("trkpitch", &trkpitch);
static float trkxyz[kMaxTrackData][2][kMaxTrackHitsData][3]; Chain->SetBranchAddress("trkxyz", &trkxyz);
// Trajectory information for tracks
static int nTrajPoint[kMaxTrackData]; Chain->SetBranchAddress("nTrajPoint", &nTrajPoint);
static float trjPt_X[kMaxTrackData][kMaxTrajHitsData]; Chain->SetBranchAddress("trjPt_X", &trjPt_X);
static float trjPt_Y[kMaxTrackData][kMaxTrajHitsData]; Chain->SetBranchAddress("trjPt_Y", &trjPt_Y);
static float trjPt_Z[kMaxTrackData][kMaxTrajHitsData]; Chain->SetBranchAddress("trjPt_Z", &trjPt_Z);
// Information about wire plane hits
int nhits; Chain->SetBranchAddress("nhits", &nhits);
static int hit_plane[kMaxHitsData]; Chain->SetBranchAddress("hit_plane", hit_plane);
static int hit_channel[kMaxHitsData]; Chain->SetBranchAddress("hit_channel", hit_channel);
static int hit_trkid[kMaxHitsData]; Chain->SetBranchAddress("hit_trkid", hit_trkid);
static float hit_driftT[kMaxHitsData]; Chain->SetBranchAddress("hit_driftT", hit_driftT);
static float hit_x[kMaxHitsData]; Chain->SetBranchAddress("hit_x", hit_x);
static float hit_y[kMaxHitsData]; Chain->SetBranchAddress("hit_y", hit_y);
static float hit_z[kMaxHitsData]; Chain->SetBranchAddress("hit_z", hit_z);
// TOF object
int ntof; Chain->SetBranchAddress("ntof", &ntof);
static float tof[kMaxTOFData]; Chain->SetBranchAddress("tof", tof);
static float tof_timestamp[kMaxTOFData]; Chain->SetBranchAddress("tof_timestamp", tof_timestamp);
//////////////////////
// Create histogram //
//////////////////////
// Small tracks in cylinder
TH1D* hSmallTracksInCylinder = new TH1D("hSmallTracksInCylinder ", "hSmallTracksInCylinder ;;", 10, 0, 10);
// Selected interaction type
TH1D* hPionAbs0pKE = new TH1D("hPionAbs0pKE", "hPionAbs0pKE;;", NUM_BINS_KE, ARRAY_KE_BINS.data());
TH1D* hPionAbsNpKE = new TH1D("hPionAbsNpKE", "hPionAbsNpKE;;", NUM_BINS_KE, ARRAY_KE_BINS.data());
TH1D* hPionScatterKE = new TH1D("hPionScatterKE", "hPionScatterKE;;", NUM_BINS_KE, ARRAY_KE_BINS.data());
std::vector<TH1*> RecoSignals = {
hPionAbs0pKE, hPionAbsNpKE, hPionScatterKE
};
// Also want to keep track of total abs
TH1D* hPionAbsKEAbsScatt = new TH1D("hPionAbsKEAbsScatt", "hPionAbsKEAbsScatt;;", NUM_BINS_KE_ABS_SCATT, ARRAY_KE_BINS_ABS_SCATT.data());
TH1D* hPionScatterKEAbsScatt = new TH1D("hPionScatterKEAbsScatt", "hPionScatterKEAbsScatt;;", NUM_BINS_KE_ABS_SCATT, ARRAY_KE_BINS_ABS_SCATT.data());
std::vector<TH1*> RecoSignalsAbsScatt = {
hPionAbsKEAbsScatt, hPionScatterKEAbsScatt
};
// Incident kinetic energy
TH1D* hIncidentKE = new TH1D("hIncidentKE", "hIncidentKE;;", NUM_BINS_KE, ARRAY_KE_BINS.data());
TH1D* hIncidentKEFine = new TH1D("hIncidentKEFine", "hIncidentKEFine;;", NUM_BINS_KE_FINE, ARRAY_KE_FINE_BINS.data());
// Kinetic energy at the front face of the TPC
TH1D* hFrontFaceKE = new TH1D("hFrontFaceKE", "hFrontFaceKE;;", NUM_BINS_KE_FINE, ARRAY_KE_FINE_BINS.data());
TH1D* hFrontFaceKEPionCut = new TH1D("hFrontFaceKEPionCut", "hFrontFaceKEPionCut;;", NUM_BINS_KE_FINE, ARRAY_KE_FINE_BINS.data());
// Pitch and dE/dx
TH1D* hTrackPitch = new TH1D("hTrackPitch", "hTrackPitch;;", 40, 0.2, 0.8);
TH1D* hTrackdEdx = new TH1D("hTrackdEdx", "hTrackdEdx;;", 48, 0, 6);
// Mean dE/dx for secondary tracks
TH1D* hMeanSecondarydEdx = new TH1D("hMeanSecondarydEdx", "hMeanSecondarydEdx;;", 40, 0, 10);
// Wire chamber kinetic energy (before energy loss correction)
TH1D* hWCKE = new TH1D("hWCKE", "hWCKE;;", NUM_BINS_KE_FINE, ARRAY_KE_FINE_BINS.data());
// Wire-chamber quality
TH1D* hWCHits = new TH1D("hWCHits", "hWCHits", 10, 0, 10);
TH1D* hRadDistMidPlane = new TH1D("hRadDistMidPlane", "hRadDistMidPlane", 60, 0.0, 20.);
TH1D* hRadDistWC4 = new TH1D("hRadDistWC4", "RadDistWC4", 90, 0.0, 30.);
//////////////////////
// Loop over events //
//////////////////////
bool verbose = false;
Long64_t i = 0;
while (true) {
if (SKIP_INDICES_DATA.count(i)) { i++; continue; }
if (Chain->GetEntry(i++) <= 0) break;
// Reset variables
if (verbose) std::cout << std::endl;
if (verbose) std::cout << "=================================" << std::endl;
if (verbose) std::cout << "Got tree entry: " << i << std::endl;
EventVariablesData ev;
if (verbose) std::cout << "Variables reset" << std::endl;
if (verbose) std::cout << "=================================" << std::endl;
// Make script go faster
// if (i > USE_NUM_EVENTS) break;
TotalEvents++;
////////////////////////////////
// Load variables of interest //
////////////////////////////////
// Load track information
if (verbose) std::cout << std::endl;
if (verbose) std::cout << "=================================" << std::endl;
if (verbose) std::cout << "Event: " << event << std::endl;
// First, we just want to grab WC to TPC match
int primaryTrackIdx = -1;
for (size_t trk_idx = 0; trk_idx < std::min(ntracks_reco, kMaxTrackData); ++trk_idx) {
if (trkWCtoTPCMatch[trk_idx]) {
// Grab position information
ev.WC2TPCPrimaryBeginX = trkvtxx[trk_idx];
ev.WC2TPCPrimaryBeginY = trkvtxy[trk_idx];
ev.WC2TPCPrimaryBeginZ = trkvtxz[trk_idx];
ev.WC2TPCPrimaryEndX = trkendx[trk_idx];
ev.WC2TPCPrimaryEndY = trkendy[trk_idx];
ev.WC2TPCPrimaryEndZ = trkendz[trk_idx];
// Grab calorimetry information (in collection plane)
int npts_dedx = std::min(ntrkcalopts[trk_idx][1], kMaxTrackHitsData);
ev.wcMatchResR.assign(trkrr[trk_idx][1], trkrr[trk_idx][1] + npts_dedx);
ev.wcMatchDEDX.assign(trkdedx[trk_idx][1], trkdedx[trk_idx][1] + npts_dedx);
ev.wcMatchPitch.assign(trkpitch[trk_idx][1], trkpitch[trk_idx][1] + npts_dedx);
for (size_t dep_idx = 0; dep_idx < std::min(ntrkcalopts[trk_idx][1], kMaxTrackHitsData); ++dep_idx) {
ev.wcMatchEDep.push_back(trkdedx[trk_idx][1][dep_idx] * trkpitch[trk_idx][1][dep_idx]);
ev.wcMatchXPos.push_back(trkxyz[trk_idx][1][dep_idx][0]);
ev.wcMatchYPos.push_back(trkxyz[trk_idx][1][dep_idx][1]);
ev.wcMatchZPos.push_back(trkxyz[trk_idx][1][dep_idx][2]);
}
// Get location information
int npts_wc2tpc = std::min(nTrajPoint[trk_idx], kMaxTrajHitsData);
ev.WC2TPCLocationsX.assign(trjPt_X[trk_idx], trjPt_X[trk_idx] + npts_wc2tpc);
ev.WC2TPCLocationsY.assign(trjPt_Y[trk_idx], trjPt_Y[trk_idx] + npts_wc2tpc);
ev.WC2TPCLocationsZ.assign(trjPt_Z[trk_idx], trjPt_Z[trk_idx] + npts_wc2tpc);
// Set flag and index
primaryTrackIdx = trk_idx;
ev.WC2TPCMatch = true;
ev.WC2TPCsize++;
}
}
if (verbose) std::cout << "Found WC2TPC match: " << ev.WC2TPCMatch << std::endl;
if (verbose) std::cout << "Number of matches: " << ev.WC2TPCsize << std::endl;
// Set beamline information
ev.TOFMass = std::abs(beamline_mass);
if (ntof == 1) ev.tofObject = tof[0];
if (verbose) std::cout << "Beamline mass: " << ev.TOFMass << std::endl;
if (verbose) std::cout << "TOF: " << ev.tofObject << std::endl;
// Copy vertex and end for all tracks
int npts_trk = std::min(ntracks_reco, kMaxTrackData);
ev.recoEndX.assign(trkendx, trkendx + npts_trk);
ev.recoEndY.assign(trkendy, trkendy + npts_trk);
ev.recoEndZ.assign(trkendz, trkendz + npts_trk);
ev.recoBeginX.assign(trkvtxx, trkvtxx + npts_trk);
ev.recoBeginY.assign(trkvtxy, trkvtxy + npts_trk);
ev.recoBeginZ.assign(trkvtxz, trkvtxz + npts_trk);
// Now, we want to loop through all tracks
for (size_t trk_idx = 0; trk_idx < std::min(ntracks_reco, kMaxTrackData); ++trk_idx) {
// Grab calorimetry information
ev.recoResR.push_back(std::vector<double>(trkrr[trk_idx][1], trkrr[trk_idx][1] + std::min(ntrkcalopts[trk_idx][1], kMaxTrackHitsData)));
ev.recoDEDX.push_back(std::vector<double>(trkdedx[trk_idx][1], trkdedx[trk_idx][1] + std::min(ntrkcalopts[trk_idx][1], kMaxTrackHitsData)));
// Check reversed
double startDistance = distance(trkvtxx[trk_idx], ev.WC2TPCPrimaryEndX, trkvtxy[trk_idx], ev.WC2TPCPrimaryEndY, trkvtxz[trk_idx], ev.WC2TPCPrimaryEndZ);
double endDistance = distance(trkendx[trk_idx], ev.WC2TPCPrimaryEndX, trkendy[trk_idx], ev.WC2TPCPrimaryEndY, trkendz[trk_idx], ev.WC2TPCPrimaryEndZ);
if (startDistance > endDistance && !trkWCtoTPCMatch[trk_idx]) {
ev.isTrackInverted.push_back(true);
std::swap(ev.recoEndX[trk_idx], ev.recoBeginX[trk_idx]);
std::swap(ev.recoEndY[trk_idx], ev.recoBeginY[trk_idx]);
std::swap(ev.recoEndZ[trk_idx], ev.recoBeginZ[trk_idx]);
std::reverse(ev.recoDEDX[trk_idx].begin(), ev.recoDEDX[trk_idx].end());
} else {
ev.isTrackInverted.push_back(false);
}
}
// Load wire-chamber track information
if (verbose) std::cout << "Number of WC tracks : " << nwctrks << std::endl;
if (nwctrks == 1) {
ev.wcTrackPicky = wctrk_picky[0];
ev.WCTrackMomentum = wctrk_momentum[0];
ev.WCTheta = wctrk_theta[0];
ev.WCPhi = wctrk_phi[0];
ev.WC4PrimaryX = WC4xPos[0];
ev.wcHit0 = {WC1xPos[0], WC1yPos[0], WC1zPos[0]};
ev.wcHit1 = {WC2xPos[0], WC2yPos[0], WC2zPos[0]};
ev.wcHit2 = {WC3xPos[0], WC3yPos[0], WC3zPos[0]};
ev.wcHit3 = {WC4xPos[0], WC4yPos[0], WC4zPos[0]};
}
if (verbose) std::cout << "Is WC track picky? " << ev.wcTrackPicky << std::endl;
// Get information about wire hits
std::map<int, std::vector<int>> trackHitMap;
std::map<int, std::vector<double>> trackHitXMap;
std::map<int, std::vector<double>> trackHitYMap;
std::map<int, std::vector<double>> trackHitZMap;
for (size_t i_hit = 0; i_hit < std::min(nhits, kMaxHitsData); ++i_hit) {
ev.fHitPlane.push_back(hit_plane[i_hit]);
ev.fHitT.push_back(SAMPLING_RATE * hit_driftT[i_hit]);
ev.fHitX.push_back(SAMPLING_RATE * hit_driftT[i_hit] * DRIFT_VELOCITY);
if (hit_channel[i_hit] < 240) {
ev.fHitW.push_back(hit_channel[i_hit] * 0.4);
} else {
ev.fHitW.push_back((hit_channel[i_hit] - 240) * 0.4);
}
// If it is -9, no match to a track
if (hit_trkid[i_hit] != -9) {
ev.hitRecoAsTrackKey.push_back(i_hit);
if (hit_trkid[i_hit] == primaryTrackIdx) ev.hitWC2TPCKey.push_back(i_hit);
trackHitMap[hit_trkid[i_hit]].push_back(i_hit);
trackHitXMap[hit_trkid[i_hit]].push_back(hit_x[i_hit]);
trackHitYMap[hit_trkid[i_hit]].push_back(hit_y[i_hit]);
trackHitZMap[hit_trkid[i_hit]].push_back(hit_z[i_hit]);
}
}
// Fill out vectors
for (auto& [trkid, hits] : trackHitMap) {
if (trkid < 0 || trkid >= ntracks_reco) {
std::cerr << "WARNING: unexpected trkid=" << trkid << " for entry " << i << ", skipping\n";
continue;
}
if (trkid >= (int) ev.recoTrackHitIndices.size()) {
ev.recoTrackHitIndices.resize(trkid + 1);
ev.recoTrackHitX.resize(trkid + 1);
ev.recoTrackHitY.resize(trkid + 1);
ev.recoTrackHitZ.resize(trkid + 1);
}
ev.recoTrackHitIndices[trkid] = hits;
ev.recoTrackHitX[trkid] = trackHitXMap[trkid];
ev.recoTrackHitY[trkid] = trackHitYMap[trkid];
ev.recoTrackHitZ[trkid] = trackHitZMap[trkid];
}
/////////////////////////////////////////
// Wire-chamber and other initial cuts //
/////////////////////////////////////////
// Project downstream to midplane @ -437.97 (without angular corrections)
std::vector<double> midUp = projToZ(ev.wcHit0, ev.wcHit1, -437.97);
// Use this point and WC3 to project up to WC4
std::vector<double> projDown = projToZ(midUp, ev.wcHit2, -95.0);
// Requires some corrections because magnets are not the same
projDown[0] -= tan(1.32 * TMath::Pi() / 180.0) * (-95.0 - -437.97);
// Compare x and y coordinate in projection and real hit for WC4
double radDistWC4 = TMath::Sqrt(pow(projDown[0] - ev.wcHit3.at(0), 2.) + pow(projDown[1] - ev.wcHit3.at(1), 2.));
// Project upstream to midplane @ -437.97
std::vector<double> midDown = projToZ(ev.wcHit2, ev.wcHit3, -437.97);
midDown[0] -= tan(1.32 * TMath::Pi() / 180.0) * (-339.57 - -437.97);
double midPlaneDist = TMath::Sqrt(pow(midUp[0] - midDown[0] + 0.75, 2) + pow(midUp[1] - midDown[1], 2));
// double midPlaneDist = TMath::Sqrt(pow(midUp[0] - midDown[0], 2) + pow(midUp[1] - midDown[1], 2));
hRadDistMidPlane->Fill(midPlaneDist);
hRadDistWC4->Fill(radDistWC4);
// Cuts
if (radDistWC4 > 8.0) continue;
if (midPlaneDist > 3.0) continue;
EventsPassingProj++;
// Check projected tracks go through all apertures
bool Magnet1ApertureCheck = CheckUpstreamMagnetAperture(ev.wcHit0, ev.wcHit1);
bool Magnet2ApertureCheck = CheckDownstreamMagnetAperture(ev.wcHit2, ev.wcHit3);
bool DSColApertureCheck = CheckDownstreamCollimatorAperture(ev.wcHit2, ev.wcHit3);
if (!Magnet1ApertureCheck || !Magnet2ApertureCheck || !DSColApertureCheck) continue;
EventsPassingAperture++;
// Candidate mass cut, keep pions, muons and electrons
if (std::abs(ev.TOFMass) > PI_MU_EL_MASS_CUTOFF) continue;
EventsPassingTOFMass++;
// If no or multiple tracks matched to wire-chamber, skip
if (!(ev.WC2TPCMatch && ev.WC2TPCsize == 1)) continue;
EventsWithWCMatch++;
// If not picky track, skip
if (!ev.wcTrackPicky) continue;
EventsWithPickyWC++;
// Check WC and front-face momentum
double WCKE = TMath::Sqrt(ev.WCTrackMomentum * ev.WCTrackMomentum + PionMass * PionMass) - PionMass;
double calculatedEnLoss = energyLossCalculation();
if (isData) {
double tanThetaCosPhi = TMath::Tan(ev.WCTheta) * TMath::Cos(ev.WCPhi);
double tanThetaSinPhi = TMath::Tan(ev.WCTheta) * TMath::Sin(ev.WCPhi);
double den = TMath::Sqrt(1 + tanThetaCosPhi * tanThetaCosPhi);
double onTheFlyPz = ev.WCTrackMomentum / den;
double onTheFlyPx = onTheFlyPz * tanThetaSinPhi;
calculatedEnLoss = energyLossCalculation(ev.WC4PrimaryX, onTheFlyPx, isData);
}
const double initialKE = WCKE - calculatedEnLoss;
hFrontFaceKE->Fill(initialKE);
hWCKE->Fill(WCKE);
/////////////////////////////////////////
// All this are valid events, analyze! //
/////////////////////////////////////////
// Sanity check
removeRepeatedPoints(&ev.WC2TPCLocationsX, &ev.WC2TPCLocationsY, &ev.WC2TPCLocationsZ);
// Copy WC2TPCLocations
std::vector<double> wcX(ev.WC2TPCLocationsX);
std::vector<double> wcY(ev.WC2TPCLocationsY);
std::vector<double> wcZ(ev.WC2TPCLocationsZ);
// Get direction to end cylinder
int numPoints = wcX.size();
int numTail = std::min(10, numPoints - 1);
std::vector<std::vector<double>> points;
for (int j = numPoints - numTail; j < numPoints; ++j) {
points.push_back({
wcX.at(j),
wcY.at(j),
wcZ.at(j)
});
}
if (numTail > 0) {
std::vector<double> avgDir = getAverageDir(points);
// Extrapolate track to end
double scale = (maxZ - points.back()[2]) / avgDir[2];
wcX.push_back(points.back()[0] + scale * avgDir[0]);
wcY.push_back(points.back()[1] + scale * avgDir[1]);
wcZ.push_back(points.back()[2] + scale * avgDir[2]);
}
// First, number of non-primary TG tracks and electron cut
int numTGTracks = 0; int numSmallTracksInCylinder = 0;
for (size_t trk_idx = 0; trk_idx < ev.recoBeginX.size(); ++trk_idx) {
if (trkWCtoTPCMatch[trk_idx]) continue;
// Get track length
double trackLength = sqrt(
pow(ev.recoEndX.at(trk_idx) - ev.recoBeginX.at(trk_idx), 2) +
pow(ev.recoEndY.at(trk_idx) - ev.recoBeginY.at(trk_idx), 2) +
pow(ev.recoEndZ.at(trk_idx) - ev.recoBeginZ.at(trk_idx), 2)
);
// Is track contained in 10 cm cylinder?
bool startInCylinder = IsPointInsideTrackCylinder(
&wcX, &wcY, &wcZ,
ev.recoBeginX.at(trk_idx), ev.recoBeginY.at(trk_idx), ev.recoBeginZ.at(trk_idx),
CYLINDER_RADIUS
);
bool endInCylinder = IsPointInsideTrackCylinder(
&wcX, &wcY, &wcZ,
ev.recoEndX.at(trk_idx), ev.recoEndY.at(trk_idx), ev.recoEndZ.at(trk_idx),
CYLINDER_RADIUS
);
if (startInCylinder && endInCylinder && (trackLength < CYLINDER_SMALL_TRACK)) numSmallTracksInCylinder++;
if (
!isWithinReducedVolume(ev.recoBeginX.at(trk_idx), ev.recoBeginY.at(trk_idx), ev.recoBeginZ.at(trk_idx)) &&
!isWithinReducedVolume(ev.recoEndX.at(trk_idx), ev.recoEndY.at(trk_idx), ev.recoEndZ.at(trk_idx))
) numTGTracks++;
}
// Apply cuts
if (numTGTracks > MAX_NUM_TG_TRACKS) continue;
EventsPassingTG++;
hSmallTracksInCylinder->Fill(numSmallTracksInCylinder);
if (numSmallTracksInCylinder > ALLOWED_CYLINDER_SMALL_TRACKS) continue;
EventsPassingSmall++;
hFrontFaceKEPionCut->Fill(initialKE);
///////////////////////
// Primary track PID //
///////////////////////
int totalCaloPoints = ev.wcMatchDEDX.size();
int nRemoveOutliers = 2;
int nRemoveEnds = 3;
int minPoints = 5;
// Get chi^2 fits, primary tracks are already checked for reversal in first module
double pionChi2 = computeReducedChi2(gPion, ev.wcMatchResR, ev.wcMatchDEDX, false, totalCaloPoints, nRemoveOutliers, nRemoveEnds);
double MIPChi2 = computeReducedChi2(gMuonTG, ev.wcMatchResR, ev.wcMatchDEDX, false, totalCaloPoints, nRemoveOutliers, nRemoveEnds);
double protonChi2 = computeReducedChi2(gProton, ev.wcMatchResR, ev.wcMatchDEDX, false, totalCaloPoints, nRemoveOutliers, nRemoveEnds);
double minStitchedChi2 = std::numeric_limits<double>::max();
int bestBreakPoint = -1;
if (totalCaloPoints >= 4 * nRemoveEnds + 2 * nRemoveOutliers + 2 * minPoints) {
for (int caloBreakPoint = 2 * nRemoveEnds + nRemoveOutliers + minPoints; caloBreakPoint < totalCaloPoints - (2 * nRemoveEnds + nRemoveOutliers + minPoints); ++caloBreakPoint) {
std::vector<double> leftResR(ev.wcMatchResR.begin(), ev.wcMatchResR.begin() + caloBreakPoint);
std::vector<double> leftDEDX(ev.wcMatchDEDX.begin(), ev.wcMatchDEDX.begin() + caloBreakPoint);
std::vector<double> rightResR(ev.wcMatchResR.begin() + caloBreakPoint, ev.wcMatchResR.end());
std::vector<double> rightDEDX(ev.wcMatchDEDX.begin() + caloBreakPoint, ev.wcMatchDEDX.end());
// Shift right-hand side to fix r.r.
for (int i = 0; i < rightResR.size(); ++i) {
rightResR[i] = rightResR[i] - rightResR[0];
}
double chi2LHS = computeReducedChi2(gProton, leftResR, leftDEDX, false, leftResR.size(), nRemoveOutliers, nRemoveEnds);
double chi2RHS = computeReducedChi2(gMuonTG, rightResR, rightDEDX, false, rightResR.size(), nRemoveOutliers, nRemoveEnds);
double totalChi2 = (chi2LHS * leftResR.size() + chi2RHS * rightResR.size()) / totalCaloPoints;
if (totalChi2 < minStitchedChi2) {
minStitchedChi2 = totalChi2;
bestBreakPoint = caloBreakPoint;
}
}
}
// Get smallest chi^2 value for primary track
double minChi2 = std::min({minStitchedChi2, pionChi2, MIPChi2, protonChi2});
// If primary track stitched, get break point, otherwise break point is end of track
double breakPointX = ev.WC2TPCPrimaryEndX;
double breakPointY = ev.WC2TPCPrimaryEndY;
double breakPointZ = ev.WC2TPCPrimaryEndZ;
if (minChi2 == minStitchedChi2) {
breakPointX = ev.wcMatchXPos.at(bestBreakPoint);
breakPointY = ev.wcMatchYPos.at(bestBreakPoint);
breakPointZ = ev.wcMatchZPos.at(bestBreakPoint);
}
//////////////////////
// Incident KE fill //
//////////////////////
// Check these are in order
bool reverseBack = false;
if (ev.wcMatchZPos.size() > 1 && ev.wcMatchZPos.front() > ev.wcMatchZPos.back()) {
std::reverse(ev.wcMatchDEDX.begin(), ev.wcMatchDEDX.end());
std::reverse(ev.wcMatchPitch.begin(), ev.wcMatchPitch.end());
std::reverse(ev.wcMatchEDep.begin(), ev.wcMatchEDep.end());
std::reverse(ev.wcMatchXPos.begin(), ev.wcMatchXPos.end());
std::reverse(ev.wcMatchYPos.begin(), ev.wcMatchYPos.end());
std::reverse(ev.wcMatchZPos.begin(), ev.wcMatchZPos.end());
reverseBack = true;
}
double energyDeposited = 0.0;
for (size_t iDep = 0; iDep < ev.wcMatchDEDX.size(); ++iDep) {
// If we are past detected breaking point, exit loop
if (ev.wcMatchZPos.at(iDep) > breakPointZ) break;
if (isWithinReducedVolume(ev.wcMatchXPos.at(iDep), ev.wcMatchYPos.at(iDep), ev.wcMatchZPos.at(iDep))) {
hTrackPitch->Fill(ev.wcMatchPitch.at(iDep));
hTrackdEdx->Fill(ev.wcMatchDEDX.at(iDep));
}
// If larger than threshold, continue
if (ev.wcMatchDEDX.at(iDep) > HIT_DEDX_THRESHOLD) continue;
// Else, add to energy deposited so far
energyDeposited += ev.wcMatchEDep.at(iDep);
// Add to incident KE if inside reduced volume
if (isWithinReducedVolume(ev.wcMatchXPos.at(iDep), ev.wcMatchYPos.at(iDep), ev.wcMatchZPos.at(iDep))) {
hIncidentKE->Fill(initialKE - energyDeposited);
hIncidentKEFine->Fill(initialKE - energyDeposited);
}
}
double energyAtVertex = initialKE - energyDeposited;
if (reverseBack) {
std::reverse(ev.wcMatchDEDX.begin(), ev.wcMatchDEDX.end());
std::reverse(ev.wcMatchPitch.begin(), ev.wcMatchPitch.end());
std::reverse(ev.wcMatchEDep.begin(), ev.wcMatchEDep.end());
std::reverse(ev.wcMatchXPos.begin(), ev.wcMatchXPos.end());
std::reverse(ev.wcMatchYPos.begin(), ev.wcMatchYPos.end());
std::reverse(ev.wcMatchZPos.begin(), ev.wcMatchZPos.end());
}
////////////////////////
// Reduced volume cut //
////////////////////////
if (!isWithinReducedVolume(breakPointX, breakPointY, breakPointZ)) continue;
EventsInRedVol++;
/////////////////////
// Primary PID cut //
/////////////////////
if (minChi2 == pionChi2 || minChi2 == protonChi2) continue;
EventsPassingPrimaryPID++;
/////////////////////////
// Secondary track PID //
/////////////////////////
int secondaryTaggedPion = 0;
int secondaryTaggedProton = 0;
int secondaryTaggedOther = 0;
int otherTaggedPion = 0;
int otherTaggedProton = 0;
int numberOfSecondary = 0;
int numberOfSecondaryNearVertex = 0;
// std::cout << "Break point: (" << breakPointX << ", " << breakPointY << ", " << breakPointZ << ")" << std::endl;
for (size_t trk_idx = 0; trk_idx < ev.recoBeginX.size(); ++trk_idx) {
if (trkWCtoTPCMatch[trk_idx]) continue;
numberOfSecondary++;
// Have to re-check track ordering for stitched case
double distanceFromStart = distance(
ev.recoBeginX.at(trk_idx), breakPointX,
ev.recoBeginY.at(trk_idx), breakPointY,
ev.recoBeginZ.at(trk_idx), breakPointZ
);
double distanceFromEnd = distance(
ev.recoEndX.at(trk_idx), breakPointX,
ev.recoEndY.at(trk_idx), breakPointY,
ev.recoEndZ.at(trk_idx), breakPointZ
);
double thisTrackLength = sqrt(
pow(ev.recoBeginX.at(trk_idx) - ev.recoEndX.at(trk_idx), 2) +
pow(ev.recoBeginY.at(trk_idx) - ev.recoEndY.at(trk_idx), 2) +
pow(ev.recoBeginZ.at(trk_idx) - ev.recoEndZ.at(trk_idx), 2)
);
// std::cout << " " << "Secondary track " << trk_idx << ": length = " << thisTrackLength << ", distance from start = " << distanceFromStart << ", distance from end = " << distanceFromEnd << std::endl;
if ((distanceFromStart < VERTEX_RADIUS || distanceFromEnd < VERTEX_RADIUS)) {
numberOfSecondaryNearVertex++;
std::vector<double> secondaryDEDX = ev.recoDEDX.at(trk_idx);
std::vector<double> secondaryResR = ev.recoResR.at(trk_idx);
bool secondaryReversed = false;
bool originallyReversed = ev.isTrackInverted.at(trk_idx);
if (distanceFromEnd < distanceFromStart) secondaryReversed = true;
// If it was originally reversed, we do not reverse again
if (originallyReversed && secondaryReversed) secondaryReversed = false;
double pionChi2 = computeReducedChi2(gPion, secondaryResR, secondaryDEDX, secondaryReversed, secondaryDEDX.size(), nRemoveOutliers, nRemoveEnds);
double protonChi2 = computeReducedChi2(gProton, secondaryResR, secondaryDEDX, secondaryReversed, secondaryDEDX.size(), nRemoveOutliers, nRemoveEnds);
double secondaryMeanDEDX = meanDEDX(secondaryDEDX, secondaryReversed, MEAN_DEDX_NUM_TRAJ_POINTS);
// std::cout << " Pion chi^2: " << pionChi2 << ", Proton chi^2: " << protonChi2 << ", Mean dE/dx: " << secondaryMeanDEDX << std::endl;
// First, try classifying track using chi^2 fits
if ((pionChi2 < PION_CHI2_PION_VALUE) && (protonChi2 > PROTON_CHI2_PION_VALUE)) {
// Tagged as pion
secondaryTaggedPion++;
} else if ((pionChi2 > PION_CHI2_PROTON_VALUE) && (protonChi2 < PROTON_CHI2_PROTON_VALUE)) {
// Tagged as proton
secondaryTaggedProton++;
} else {
// Not tagged with chi^2, use mean dE/dx
secondaryTaggedOther++;
if (secondaryMeanDEDX <= MEAN_DEDX_THRESHOLD) {
otherTaggedPion++;
} else {
otherTaggedProton++;
}
hMeanSecondarydEdx->Fill(secondaryMeanDEDX);
}
}
}
// std::cout << "Number of secondary tracks: " << numberOfSecondary << std::endl;
// std::cout << "Number of secondary tracks near vertex: " << numberOfSecondaryNearVertex << std::endl;
// std::cout << "Classified as pion: " << secondaryTaggedPion << ", Classified as proton: " << secondaryTaggedProton << ", Not classified with chi^2: " << secondaryTaggedOther << std::endl;
// std::cout << "Of those not classified with chi^2, classified as pion with mean dE/dx: " << otherTaggedPion << ", classified as proton with mean dE/dx: " << otherTaggedProton << std::endl;
// std::cout << std::endl;
// For particles where we stitched, we also need to analyze the second part of the primary track
bool newSecondaryPion = false;
if (minChi2 == minStitchedChi2) {
std::vector<double> newSecondaryResR(ev.wcMatchResR.begin(), ev.wcMatchResR.begin() + bestBreakPoint);
std::vector<double> newSecondaryDEDX(ev.wcMatchDEDX.begin(), ev.wcMatchDEDX.begin() + bestBreakPoint);
double newPionChi2 = computeReducedChi2(gPion, newSecondaryResR, newSecondaryDEDX, false, newSecondaryDEDX.size(), nRemoveOutliers, nRemoveEnds);
double newProtonChi2 = computeReducedChi2(gProton, newSecondaryResR, newSecondaryDEDX, false, newSecondaryDEDX.size(), nRemoveOutliers, nRemoveEnds);
double newMeanDEDX = meanDEDX(newSecondaryDEDX, false, MEAN_DEDX_NUM_TRAJ_POINTS);
if ((newPionChi2 < PION_CHI2_PION_VALUE) && (newProtonChi2 > PROTON_CHI2_PION_VALUE)) {
// Tagged as pion
newSecondaryPion = true;
secondaryTaggedPion++;
} else if ((newPionChi2 > PION_CHI2_PROTON_VALUE) && (newProtonChi2 < PROTON_CHI2_PROTON_VALUE)) {
// Tagged as proton
secondaryTaggedProton++;
} else {
// Not tagged with chi^2, use mean dE/dx
secondaryTaggedOther++;
if (newMeanDEDX <= MEAN_DEDX_THRESHOLD) {
newSecondaryPion = true;
otherTaggedPion++;
} else {
otherTaggedProton++;
}
}
}
int totalTaggedPions = secondaryTaggedPion + otherTaggedPion;
int totalTaggedProtons = secondaryTaggedProton + otherTaggedProton;
//////////////////////////////////////////
// Selection based off secondary tracks //
//////////////////////////////////////////
if (totalTaggedPions > 0) {
// Reject events with > 1 pion
if (totalTaggedPions > 1 || newSecondaryPion) continue;
// Select as scatter
EventsSelectedAsScatter++;
hPionScatterKE->Fill(energyAtVertex);
hPionScatterKEAbsScatt->Fill(energyAtVertex);
continue;
}
if (totalTaggedProtons > 0) {
if (secondaryTaggedProton == 0 && otherTaggedProton > 0) continue;
// Select as Np absorption
EventsSelectedAsAbsNp++;
hPionAbsNpKE->Fill(energyAtVertex);
hPionAbsKEAbsScatt->Fill(energyAtVertex);
continue;
}
EventsNoSecondary++;
////////////////////////////////////
// Cluster non-reconstructed hits //
////////////////////////////////////
// Get unordered set for hits in tracks
std::unordered_set<int> hitsInTracks(ev.hitRecoAsTrackKey.begin(), ev.hitRecoAsTrackKey.end());
// First, we construct the clusters
std::vector<int> candidateHits;
for (size_t iHit = 0; iHit < std::min(nhits, kMaxHitsData); ++iHit) {
double hitX = ev.fHitX.at(iHit);
double hitW = ev.fHitW.at(iHit);
int hitPlane = ev.fHitPlane.at(iHit);
// Check if hit is near vertex of the primary
if (isHitNearPrimary(
&ev.hitWC2TPCKey,
&ev.fHitX,
&ev.fHitW,
&ev.fHitPlane,
hitX,
hitW,
hitPlane,
DISTANCE_TO_PRIMARY_THRESHOLD,
true
)) {
// Skip hits already in tracks
if (hitsInTracks.count(iHit) > 0) continue;
candidateHits.push_back(iHit);
}
}
// Now cluster using those hits as starting points
std::unordered_set<int> usedHits;
std::vector<HitCluster> hitClusters;
int nCandidateHits = candidateHits.size();
// Hits in the same cluster must be separated by at most some number of wires
for (int iHit = 0; iHit < nCandidateHits; ++iHit) {
// The candidate hits are only STARTING points, as this could make up
// really long tracks in the induction plane that are no longer near
// the ending point of the primary track
// First, check if we have already used this hit
int thisHitKey = candidateHits.at(iHit);
int thisHitPlane = ev.fHitPlane.at(thisHitKey);
float thisHitW = ev.fHitW.at(thisHitKey);
float thisHitX = ev.fHitX.at(thisHitKey);
float thisHitCharge = -1;
float thisHitChargeCol = -1;
if (usedHits.count(thisHitKey)) continue;
std::vector<int> clusterKeys;
std::vector<float> clusterX;
std::vector<float> clusterW;
std::vector<float> clusterCharge;
std::vector<float> clusterChargeCol;
clusterKeys.push_back(thisHitKey);
clusterX.push_back(thisHitX);
clusterW.push_back(thisHitW);
clusterCharge.push_back(thisHitCharge);
clusterChargeCol.push_back(thisHitChargeCol);
for (int iAllHit = 0; iAllHit < std::min(nhits, kMaxHitsData); ++iAllHit) {
// Skip already used hits, and those reconstructed in tracks
if (usedHits.count(iAllHit) || hitsInTracks.count(iAllHit)) continue;
// Clusters have to be in same plane
if (ev.fHitPlane.at(iAllHit) != thisHitPlane) continue;
float internalHitW = ev.fHitW.at(iAllHit);
float internalHitX = ev.fHitX.at(iAllHit);
float dW = std::abs(internalHitW - thisHitW);
float dX = std::abs(internalHitX - thisHitX);
float distance = std::sqrt(std::pow(dW, 2) + std::pow(dX, 2));
int nClusterSoFar = clusterW.size();
for (int iCluster = 0; iCluster < nClusterSoFar; ++iCluster) {
float tempdW = std::abs(internalHitW - clusterW.at(iCluster));
float tempdX = std::abs(internalHitX - clusterX.at(iCluster));
float tempDistance = std::sqrt(std::pow(tempdW, 2) + std::pow(tempdX, 2));
if (tempDistance < distance) distance = tempDistance;
if (distance < MAX_IN_CLUSTER_SEPARATION) break;
}
if (distance < MAX_IN_CLUSTER_SEPARATION) {
usedHits.insert(iAllHit);
clusterKeys.push_back(iAllHit);
clusterX.push_back(ev.fHitX.at(iAllHit));
clusterW.push_back(ev.fHitW.at(iAllHit));
clusterCharge.push_back(-1);
clusterChargeCol.push_back(-1);
}
}
if (clusterKeys.size() > MINIMUM_HITS_FOR_CLUSTER) {
HitCluster thisCluster;
double clusterSize = 0;
for (int j = 0; j < clusterW.size() - 1; ++j) {
for (int k = j + 1; k < clusterW.size(); ++k) {
double wSeparation = clusterW[j] - clusterW[k];
double xSeparation = clusterX[j] - clusterX[k];
double thisDiameter = std::sqrt(
std::pow(wSeparation, 2) +
std::pow(xSeparation, 2)
);
if (thisDiameter > clusterSize) clusterSize = thisDiameter;
}
}
thisCluster.plane = thisHitPlane;
thisCluster.hitKeys = clusterKeys;
thisCluster.hitX = clusterX;
thisCluster.hitW = clusterW;
thisCluster.hitCharge = clusterCharge;
thisCluster.hitChargeCol = clusterChargeCol;
thisCluster.clusterSize = clusterSize;
usedHits.insert(thisHitKey);
hitClusters.push_back(thisCluster);
}
}
// Now, get data for cut
int numLargeClustersInduction = 0;
int numLargeClustersCollection = 0;
int numClustersInduction = 0;
int numClustersCollection = 0;
double largestClusterSizeInduction = 0;
double largestClusterSizeCollection = 0;
for (int i = 0; i < hitClusters.size(); ++i) {
HitCluster thisCluster = hitClusters[i];
double clusterSize = thisCluster.clusterSize;
if (thisCluster.plane == 0) {
if (clusterSize > largestClusterSizeInduction) largestClusterSizeInduction = clusterSize;
if (clusterSize > LARGE_CLUSTER_THRESHOLD) numLargeClustersInduction++;
numClustersInduction++;
}
else if (thisCluster.plane == 1) {
if (clusterSize > largestClusterSizeCollection) largestClusterSizeCollection = clusterSize;
if (clusterSize > LARGE_CLUSTER_THRESHOLD) numLargeClustersCollection++;
numClustersCollection++;
}
}
// Perform cut
// if (numClustersInduction < MAX_NUM_CLUSTERS_INDUCTION) {