-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead_PiAcceptance_GEMCimulations.C
1402 lines (1197 loc) · 57.8 KB
/
Read_PiAcceptance_GEMCimulations.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
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
// clas12root Read_PiAcceptance_GEMCimulations.C\(\10, 1\)
#ifdef __CINT__
#pragma link C++ class std::vector<TVector3>+;
#pragma link C++ class std::vector<TLorentzVector>+;
#endif
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <TFile.h>
#include <TTree.h>
#include <TApplication.h>
#include <TROOT.h>
#include <TDatabasePDG.h>
#include <TLorentzVector.h>
#include <TVector3.h>
#include <TH1.h>
#include <TChain.h>
#include <TCanvas.h>
#include <TBenchmark.h>
#include "clas12reader.h"
#include "Auxiliary/DCfid_SIDIS.cpp"
#include "Auxiliary/csv_reader.h"
#include "Auxiliary/SIDISatBAND_auxiliary.cpp"
#define NMAXPIONS 20 // maximal allowed number of pions
#define r2d 180./3.1415 // radians to degrees
using namespace clas12;
SIDISatBAND_auxiliary aux;
// globals
auto db = TDatabasePDG::Instance();
TString DataPath, FileLabel, PiCharge;
TString csvheader = ( (TString)"e_P,e_Theta,e_Phi,e_Vz,"
+(TString)"pi_P,pi_Theta,pi_Phi,pi_Vz,"
+(TString)"Npips,Npims,Nelectrons,Ngammas,Nprotons,Nneutrons,Ndeuterons,"
+(TString)"e_P_g,e_Theta_g,e_Phi_g,e_Vz_g,"
+(TString)"pi_P_g,pi_Theta_g,pi_Phi_g,pi_Vz_g,"
+(TString)"pi_reconstructed,pi_passed_cuts,pi_passed_fiducial_cuts,pi_passed_PID_cuts,"
+(TString)"e_reconstructed,e_passed_cuts,"
+(TString)"e_DC_sector,pi_DC_sector,"
+(TString)"M_X,eepiPastKinematicalCuts,");
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
// start clock
auto start = std::chrono::high_resolution_clock::now();
// declare methods
TVector3 GetParticleVertex (clas12::region_part_ptr rp);
void SetLorentzVector (TLorentzVector &p4, clas12::region_part_ptr rp);
void OpenOutputFiles (TString filelabel, TString header);
void CloseOutputFiles ();
void StreamToCSVfile (std::vector<Double_t> observables, int fdebug);
void InitializeFileReading (int NeventsMax,int c12Nentries, int fdebug);
void InitializeVariables ();
void OpenResultFiles ();
void printCutValues ();
void loadCutValues (TString cutValuesFilename = "cutValues.csv", int fdebug=0);
double FindCutValue ( std::string cutName );
void ExtractElectronInformation (int fdebug);
void ExtractPionsInformation (int fdebug);
void ExtractPipsInformation (int pipsIdx, int fdebug );
void ExtractPimsInformation (int pimsIdx, int fdebug );
void ComputeKinematics (int fdebug);
void WriteEventToOutput (int fdebug);
void FinishProgram ();
void GetParticlesByType (int evnum, int fdebug );
void Stream_e_pi_line_to_CSV (int piIdx, int fdebug );
void SetDataPath ( TString fDataPath ) {DataPath = fDataPath;}
void SetFileLabel ( TString fFileLabel ) {FileLabel = fFileLabel;}
void SetPiCharge ( TString fPiCharge ) {PiCharge = fPiCharge;}
bool CheckIfElectronPassedSelectionCuts (Double_t e_PCAL_x, Double_t e_PCAL_y,
Double_t e_PCAL_W,Double_t e_PCAL_V,
Double_t e_E_PCAL,
Double_t e_E_ECIN, Double_t e_E_ECOUT,
TLorentzVector e,
TVector3 Ve,
Double_t e_DC_sector,
Double_t e_DC_x[3],
Double_t e_DC_y[3],
Double_t e_DC_z[3],
int torusBending);
bool CheckIfPionPassedSelectionCuts (TString pionCharge, // "pi+" or "pi-"
Double_t DC_sector,
Double_t DC_x[3], Double_t DC_y[3], Double_t DC_z[3],
Double_t chi2PID, Double_t p,
TVector3 Ve, TVector3 Vpi,
int pipsIdx,
int fdebug);
bool eepiPassedKinematicalCriteria (TLorentzVector pi,
int fdebug);
Double_t Chi2PID_pion_lowerBound (Double_t p, Double_t C=0.88); // C(pi+)=0.88, C(pi-)=0.93
Double_t Chi2PID_pion_upperBound (Double_t p, Double_t C=0.88); // C(pi+)=0.88, C(pi-)=0.93
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
// meta-data
int torusBending = -1; // -1 for In-bending, +1 for Out-bending
int DC_layers[3] = {6,18,36};// Region 1 is denoted at DC detector 6, Region 2 is denoted 18, Region 3 - as 36
int DC_layer;
int runnum;
int evnum;
int beam_helicity; // helicity of the electron +1 along the beam and -1 opposite to it
int status;
int NeventsMaxToProcess;
int Nevents_processed;
// number of particles per event
int Ne, Nn, Np, Npips, Npims, Ngammas;
int Nd; // number of detected deuterons
// variables
double Mp = 0.938;
double Mp2 = Mp * Mp;
double Md = 1.875612; // NIST
// leading electron
double e_E_PCAL; // electron energy deposit in PCAL [GeV]
double e_E_ECIN; // electron energy deposit in ECAL_in [GeV]
double e_E_ECOUT; // electron energy deposit in ECAL_out [GeV]
double e_PCAL_W;
double e_PCAL_V;
double e_PCAL_x;
double e_PCAL_y;
double e_PCAL_z;
double e_PCAL_sector;
double e_DC_sector;
double e_DC_Chi2N;
double e_DC_x[3];
double e_DC_y[3];
double e_DC_z[3];
bool e_reconstructed;
bool pi_reconstructed;
bool e_passed_cuts;
bool pi_passed_cuts;
bool pi_passed_fiducial_cuts;
bool pi_passed_PID_cuts;
// cut values
std::vector<std::pair<std::string, double>> cutValues;
double cutValue_Vz_min;
double cutValue_Vz_max;
double cutValue_e_PCAL_W;
double cutValue_e_PCAL_V;
double cutValue_e_E_PCAL;
double cutValue_SamplingFraction_min;
double cutValue_PCAL_ECIN_SF_min;
double cutValue_Ve_Vpi_dz_max;
double cutValue_Q2_min;
double cutValue_W_min;
double cutValue_y_max;
double cutValue_e_theta_min;
double cutValue_e_theta_max;
double cutValue_pi_theta_min;
double cutValue_pi_theta_max;
double cutValue_Ppi_min;
double cutValue_Ppi_max;
double cutValue_Zpi_min;
double cutValue_Zpi_max;
bool ePastCutsInEvent = false;
bool pipsPastCutsInEvent = false;
bool eepipsPastCutsInEvent = false;
bool pimsPastCutsInEvent = false;
bool EventPassedCuts = false;
bool eepimsPastCutsInEvent = false;
// positive pions
bool pipsReconstructed[NMAXPIONS];
bool pipsPastSelectionCuts[NMAXPIONS];
bool pipsPastFiducialCuts[NMAXPIONS];
bool pipsPastPIDCuts[NMAXPIONS];
bool eepipsPastKinematicalCuts[NMAXPIONS];
double pips_chi2PID[NMAXPIONS];
double pips_PCAL_W[NMAXPIONS];
double pips_PCAL_V[NMAXPIONS];
double pips_PCAL_x[NMAXPIONS];
double pips_PCAL_y[NMAXPIONS];
double pips_PCAL_z[NMAXPIONS];
double pips_PCAL_sector[NMAXPIONS];
double pips_DC_sector[NMAXPIONS];
double pips_Chi2N[NMAXPIONS];
double pips_DC_x[NMAXPIONS][3];
double pips_DC_y[NMAXPIONS][3];
double pips_DC_z[NMAXPIONS][3];
double pips_E_PCAL[NMAXPIONS];
double pips_E_ECIN[NMAXPIONS];
double pips_E_ECOUT[NMAXPIONS];
double Zpips[NMAXPIONS]; // hadron rest-frame energy
double piplus_Px[NMAXPIONS];
double piplus_Py[NMAXPIONS];
double piplus_Pz[NMAXPIONS];
double piplus_E[NMAXPIONS];
double Vpiplus_X[NMAXPIONS];
double Vpiplus_Y[NMAXPIONS];
double Vpiplus_Z[NMAXPIONS];
// negative pions
bool pimsReconstructed[NMAXPIONS];
bool pimsPastSelectionCuts[NMAXPIONS];
bool pimsPastFiducialCuts[NMAXPIONS];
bool pimsPastPIDCuts[NMAXPIONS];
bool eepimsPastKinematicalCuts[NMAXPIONS];
double pims_chi2PID[NMAXPIONS];
double pims_PCAL_W[NMAXPIONS];
double pims_PCAL_V[NMAXPIONS];
double pims_PCAL_x[NMAXPIONS];
double pims_PCAL_y[NMAXPIONS];
double pims_PCAL_z[NMAXPIONS];
double pims_PCAL_sector[NMAXPIONS];
double pims_DC_sector[NMAXPIONS];
double pims_Chi2N[NMAXPIONS];
double pims_DC_x[NMAXPIONS][3];
double pims_DC_y[NMAXPIONS][3];
double pims_DC_z[NMAXPIONS][3];
double pims_E_PCAL[NMAXPIONS];
double pims_E_ECIN[NMAXPIONS];
double pims_E_ECOUT[NMAXPIONS];
double Zpims[NMAXPIONS]; // hadron rest-frame energy
double piminus_Px[NMAXPIONS];
double piminus_Py[NMAXPIONS];
double piminus_Pz[NMAXPIONS];
double piminus_E[NMAXPIONS];
double Vpiminus_X[NMAXPIONS];
double Vpiminus_Y[NMAXPIONS];
double Vpiminus_Z[NMAXPIONS];
// Output CSV file
std::ofstream CSVfile;
// vectors in lab-frame
TLorentzVector Beam, target, e, q;
std::vector<TLorentzVector> piplus; // positive pions
std::vector<TLorentzVector> piminus; // negative pions
// reconstructed vertex position
TVector3 Ve;
std::vector<TVector3> Vpiplus;
std::vector<TVector3> Vpiminus;
// kinematics
Double_t Ebeam, xB, Q2, omega, W, W2, xF, y, M_X;
// MC information
TLorentzVector P_mc_particle;
TLorentzVector e_g, pi_g;
TVector3 V_mc_particle;
TVector3 Ve_g, Vpi_g;
// auxiliary
DCfid_SIDIS dcfid;
std::vector<region_part_ptr> electrons, neutrons, protons, pipluses, piminuses, gammas, deuterons;
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
// Main functionality
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
void Read_PiAcceptance_GEMCimulations(TString fPiCharge = "pips",
int NeventsMax=-1,
int fdebug=1,
int PrintProgress=50000,
TString fFileLabel = "p_uniform_distribution",
TString fDataPath = "/volatile/clas12/users/ecohen/GEMC/hipo/10.2/AcceptanceCorrection/"
){
if (fdebug>2) std::cout << "SetDataPath ( "<<fDataPath<<" ); " << std::endl;
SetDataPath ( fDataPath );
if (fdebug>2) std::cout << "SetFileLabel ( "<<fFileLabel<<" ); " << std::endl;
SetFileLabel( fFileLabel );
if (fdebug>2) std::cout << "SetPiCharge ( "<<fPiCharge<<" ); " << std::endl;
SetPiCharge ( fPiCharge );
// Open result files
if (fdebug>2) std::cout << "OpenResultFiles (); " << std::endl;
OpenResultFiles();
// Load cut values
if (fdebug>2) std::cout << "Load cut values " << std::endl;
// loadCutValues("cutValues.csv",fdebug);
// read cut values
aux.loadCutValues("macros/cuts/BANDcutValues.csv",torusBending);
// determine how many files to process
int NfilesToProcess = 1;
if (NeventsMax<0) NeventsMax = 1000000;
if (NeventsMax > 100000) {
NfilesToProcess = int( NeventsMax/100000 );
}
TChain fake("hipo");
for (int fileIdx=0; fileIdx<NfilesToProcess; fileIdx++){
TString inputFile = (DataPath + "/" + PiCharge
+ "/ee" + PiCharge + "_" + FileLabel
+ "_" + (TString)(std::to_string(fileIdx))
+ "_reco.hipo");
// TString inputFile = "/volatile/clas12/users/hauenst/erez_files/out_eepips_p_uniform_distribution_0.hipo";
if (fdebug>2) std::cout << "inputFile: " << inputFile << std::endl;
fake.Add(inputFile.Data());
}
//get the hipo data
auto files = fake.GetListOfFiles();
// step over events and extract information....
for(Int_t i=0;i<files->GetEntries();i++){
//create the event reader
if (fdebug) std::cout << "reading file " << i << std::endl;
clas12reader c12(files->At(i)->GetTitle(),{0});
InitializeFileReading( NeventsMax, c12.getReader().getEntries(), fdebug );
int event = 0;
// process the events...
while((c12.next()==true) && (event < NeventsMaxToProcess)){
runnum = c12.runconfig()->getRun();
evnum = c12.runconfig()->getEvent();
if (fdebug>2) std::cout << "begin analysis of event " << evnum << " (run " << runnum << ")" << std::endl;
InitializeVariables();
// Get Particles By Type
electrons = c12.getByID( 11 );
neutrons = c12.getByID( 2112 );
protons = c12.getByID( 2212 );
pipluses = c12.getByID( 211 );
piminuses = c12.getByID(-211 );
gammas = c12.getByID( 22 );
deuterons = c12.getByID( 1000010020 );
GetParticlesByType ( evnum, fdebug );
// add truth-information,
// i.e. generated electron and generated pion information
auto mcpbank = c12.mcparts();
const Int_t Ngen=mcpbank->getRows();
if (fdebug>1) std::cout << "Grabbing truth-information of " << Ngen << " particles" << std::endl;
for( Int_t i_mc =0; i_mc< Ngen ; i_mc++){
mcpbank -> setEntry(i_mc);
P_mc_particle.SetXYZM( mcpbank->getPx() , mcpbank->getPy() , mcpbank->getPz() , mcpbank->getMass() );
V_mc_particle.SetXYZ( mcpbank->getVx() , mcpbank->getVy() , mcpbank->getVz() );
auto pid = mcpbank->getPid();
if (fdebug>2){
std::cout << "MC particle PDG code " << pid
<< std::setprecision(4)
<< ", p: " << P_mc_particle.P() << " GeV/c, "
<< ", theta: "<< P_mc_particle.Theta() << ", " << P_mc_particle.Theta()*r2d << " deg, "
<< ", phi: " << P_mc_particle.Phi() << ", "<< P_mc_particle.Phi()*r2d << " deg, "
<< ", V(z): " << V_mc_particle.Z() << " cm"
<< std::endl;
}
if ( pid==11 ) {
e_g = P_mc_particle;
Ve_g = V_mc_particle;
}
else if ( (pid==211) && (PiCharge=="pips") ) {
pi_g = P_mc_particle;
Vpi_g = V_mc_particle;
}
else if ( (pid==-211) && (PiCharge=="pims") ) {
pi_g = P_mc_particle;
Vpi_g = V_mc_particle;
}
else {
if (fdebug>2){
std::cout << "MC particle PDG code " << pid << " do not match generated particles: e (" << 11 << ") + ";
if ( PiCharge=="pips")
std::cout << " pi+ (" << 211;
else if ( PiCharge=="pims")
std::cout << " pi- (" << -211;
std::cout << ")" << std::endl;
}
}
}
if (Ne>0){
// We do not filter, so we write event non-reconstructed events.
// But here we extract information from electrons and pions
e_reconstructed = true;
ExtractElectronInformation (fdebug);
ComputeKinematics (fdebug);
ExtractPionsInformation (fdebug);
}
else {
e_reconstructed = false;
if (fdebug>1) std::cout << "no electrons in event " << event << std::endl;
}
WriteEventToOutput (fdebug);
if (fdebug>1) {
std::cout << "done processing event " << evnum
<< " (" << event << "/" << NeventsMaxToProcess<< ") "
<< std::endl << "------------------------------------------------------------" << std::endl ;
}
event++; Nevents_processed++;
if (fdebug && event%PrintProgress==0) std::cout << std::setprecision(1) << " event " << event << std::endl;
} // end event loop
} // end file loop
FinishProgram();
}
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
TVector3 GetParticleVertex(clas12::region_part_ptr rp){
TVector3 V(rp->par()->getVx(),
rp->par()->getVy(),
rp->par()->getVz());
return V;
}
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
void SetLorentzVector (TLorentzVector &p4,clas12::region_part_ptr rp){
p4.SetXYZM(rp->par()->getPx(),
rp->par()->getPy(),
rp->par()->getPz(),
p4.M());
}
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
void OpenOutputFiles (TString header){
// Create output csv files
CSVfile.open( DataPath + "/" + PiCharge + "/ee" + PiCharge + "_" + FileLabel + ".csv" );
CSVfile << header << std::endl;
}
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
void CloseOutputFiles (){
// close output CSV
CSVfile.close();
std::cout
<< "Done processesing " << Nevents_processed << " events,"
<< std::endl;
std::cout << "output files ready in csv formats in " << std::endl
<< std::endl
<< DataPath + "/" + PiCharge + "/" + "ee" + PiCharge + "_" + FileLabel + ".csv"
<< std::endl;
}
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
void StreamToCSVfile (std::vector<Double_t> observables,
int fdebug){
if (fdebug>1) {
std::cout << "streaming to CSVfile" << std::endl;
}
for (auto v:observables) CSVfile << std::fixed << v << ",";
CSVfile << std::endl;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
int GetBeamHelicity( event_ptr p_event, int runnum, int fdebug ){
// deprecated as of Aug-11, 2021,
// since
// however we keep it here for more data
// get beam helicity (+1 along the beam and -1 opposite to it)
// [Christopher Dilks <[email protected]>, email from Aug-5, 2021]
// for more items
// [https://github.com/JeffersonLab/clas12root/blob/master/AccesssingBankDataInCpp.txt]
//// helFlip: if true, REC::Event.helicity has opposite sign from reality
//def helFlip
//if(RG=="RGA") helFlip = true
//else if(RG=="RGB") {
// helFlip = true
// if(runnum>=11093 && runnum<=11283) helFlip = false // fall, 10.4 GeV period only
// else if(runnum>=11323 && runnum<=11571) helFlip = false // winter
//};
//else if(RG=="RGK") helFlip = false
//else if(RG=="RGF") helFlip = true
if (fdebug>3) std::cout << "beam_helicity = c12.event()->getHelicity()" << std::endl;
beam_helicity = p_event->getHelicity();
if (fdebug>3) std::cout << "check spin flip" << std::endl;
// we are working here on RGB data
bool helFlip = true;
if (runnum>=11093 && runnum<=11283) helFlip = false; // falls, 10.4 GeV period only
else if (runnum>=11323 && runnum<=11571) helFlip = false; // winter
if (helFlip) {
beam_helicity = -1 * beam_helicity;
}
if (fdebug>3) std::cout << "done GetBeamHelicity() " << std::endl;
return beam_helicity;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
double GetBeamEnergy (int fdebug){
if (fdebug>3) std::cout << "set beam energy" << std::endl;
double Ebeam = 10.2; // [GeV] ( for Fall-2019 the enrgy was 10.4096)
return Ebeam;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void InitializeFileReading(int NeventsMax, int c12Nentries, int fdebug){
if (fdebug>1) {
std::cout << "InitializeFileReading( "
<< NeventsMax << " , " << c12Nentries
<< " , " << fdebug << ")" << std::endl;
}
Ebeam = GetBeamEnergy( fdebug );
Beam .SetPxPyPzE (0, 0, Ebeam, Ebeam );
target .SetXYZM (0, 0, 0, Md );
NeventsMaxToProcess = NeventsMax;
if (fdebug>1) {
std::cout << "NeventsMaxToProcess = " << NeventsMaxToProcess << "" << std::endl;
}
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void InitializeVariables(){
e = TLorentzVector(0,0,0,db->GetParticle( 11 )->Mass());
xB = Q2 = omega = -9999;
xF = y = M_X = -9999;
e_E_ECIN = e_E_ECOUT = e_E_PCAL = -9999;
e_PCAL_W = e_PCAL_V = -9999;
e_PCAL_x = e_PCAL_y = e_PCAL_z = -9999;
e_PCAL_sector = -9999;
e_DC_sector = e_DC_Chi2N = -9999;
for (int regionIdx=0; regionIdx<3; regionIdx++) {
e_DC_x[regionIdx] = -9999;
e_DC_y[regionIdx] = -9999;
e_DC_z[regionIdx] = -9999;
}
Ve = TVector3();
piplus .clear();
piminus .clear();
Vpiplus .clear();
Vpiminus .clear();
pipluses .clear();
piminuses .clear();
electrons .clear();
neutrons .clear();
protons .clear();
gammas .clear();
e_reconstructed = false;
e_passed_cuts = false;
for (int piIdx=0; piIdx<NMAXPIONS; piIdx++) {
pips_chi2PID[piIdx] = -9999;
pips_DC_sector[piIdx] = -9999;
pips_PCAL_sector[piIdx] = -9999;
pips_PCAL_W[piIdx] = pips_PCAL_V[piIdx] = -9999;
pips_PCAL_x[piIdx] = pips_PCAL_y[piIdx] = -9999;
pips_PCAL_z[piIdx] = -9999;
pips_E_PCAL[piIdx] = -9999;
pips_E_ECIN[piIdx] = pips_E_ECOUT[piIdx] = -9999;
pims_chi2PID[piIdx] = -9999;
pims_DC_sector[piIdx] = -9999;
pims_PCAL_sector[piIdx] = -9999;
pims_PCAL_W[piIdx] = pims_PCAL_V[piIdx] = -9999;
pims_PCAL_x[piIdx] = pims_PCAL_y[piIdx] = -9999;
pims_PCAL_z[piIdx] = -9999;
pims_E_PCAL[piIdx] = -9999;
pims_E_ECIN[piIdx] = pims_E_ECOUT[piIdx] = -9999;
for (int regionIdx=0; regionIdx<3; regionIdx++) {
pips_DC_x[piIdx][regionIdx]= pips_DC_y[piIdx][regionIdx] = -9999;
pips_DC_z[piIdx][regionIdx] = -9999;
pims_DC_x[piIdx][regionIdx]= pims_DC_y[piIdx][regionIdx] = -9999;
pims_DC_z[piIdx][regionIdx] = -9999;
}
piplus .push_back( TLorentzVector(0,0,0,db->GetParticle( 211 )->Mass()) );
Vpiplus .push_back( TVector3() );
piminus .push_back( TLorentzVector(0,0,0,db->GetParticle( -211 )->Mass()) );
Vpiminus.push_back( TVector3() );
piplus_Px[piIdx] = piplus_Py[piIdx] = piplus_Pz[piIdx] = piplus_E[piIdx] = -9999;
piminus_Px[piIdx] = piminus_Py[piIdx] = piminus_Pz[piIdx] = piminus_E[piIdx] = -9999;
Vpiplus_X[piIdx] = Vpiplus_Y[piIdx] = Vpiplus_Z[piIdx] = -9999;
Vpiminus_X[piIdx] = Vpiminus_Y[piIdx] = Vpiminus_Z[piIdx] = -9999;
pipsPastSelectionCuts[piIdx] = false;
pipsPastFiducialCuts[piIdx] = false;
pipsPastPIDCuts[piIdx] = false;
eepipsPastKinematicalCuts[piIdx] = false;
pimsPastSelectionCuts[piIdx] = false;
pimsPastFiducialCuts[piIdx] = false;
pimsPastPIDCuts[piIdx] = false;
eepimsPastKinematicalCuts[piIdx] = false;
}
DC_layer = -9999;
status = 1; // 0 is good...
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TString GetRunNumberSTR(int RunNumber, int fdebug){
char RunNumberStr[20];
sprintf( RunNumberStr, "00%d", RunNumber );
if (fdebug>1) std::cout << "(SIDIS) skimming run " << RunNumberStr << std::endl;
return (TString)RunNumberStr;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void OpenResultFiles(){
OpenOutputFiles(csvheader);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void ExtractElectronInformation(int fdebug){
// ------------------------------------------------------------------------------------------------
// extract electron information
// ------------------------------------------------------------------------------------------------
// find leading electron as the one with highest energy
double leading_e_E;
int leading_e_index = 0;
SetLorentzVector(e,electrons[0]);
TLorentzVector e_tmp(0,0,0,db->GetParticle(11)->Mass());
for (int eIdx=0; eIdx < Ne; eIdx++) {
SetLorentzVector(e_tmp ,electrons[eIdx]);
double Ee = e_tmp.E();
if (Ee > leading_e_E) {
leading_e_index = eIdx;
leading_e_E = Ee;
}
}
// set leading electron 4-momentum
SetLorentzVector(e , electrons[leading_e_index]);
// set leading electron vertex
Ve = GetParticleVertex( electrons[leading_e_index] );
// detector information on electron
auto e_PCAL_info= electrons[leading_e_index]->cal(PCAL);
e_E_PCAL = e_PCAL_info->getEnergy();
e_PCAL_sector = e_PCAL_info->getSector();
e_PCAL_V = e_PCAL_info->getLv();
e_PCAL_W = e_PCAL_info->getLw();
e_E_ECIN = electrons[leading_e_index]->cal(ECIN)->getEnergy();
e_E_ECOUT = electrons[leading_e_index]->cal(ECOUT)->getEnergy();
// hit position in PCAL
e_PCAL_x = e_PCAL_info->getX();
e_PCAL_y = e_PCAL_info->getY();
e_PCAL_z = e_PCAL_info->getZ();
// Drift Chamber tracking system
auto e_DC_info = electrons[leading_e_index]->trk(DC);
e_DC_sector = e_DC_info->getSector(); // tracking sector
e_DC_Chi2N = e_DC_info->getChi2N(); // tracking chi^2/NDF
for (int regionIdx=0; regionIdx<3; regionIdx++) {
int DC_layer = DC_layers[regionIdx];
e_DC_x[regionIdx] = electrons[leading_e_index]->traj(DC,DC_layer)->getX();
e_DC_y[regionIdx] = electrons[leading_e_index]->traj(DC,DC_layer)->getY();
e_DC_z[regionIdx] = electrons[leading_e_index]->traj(DC,DC_layer)->getZ();
}
e_passed_cuts = CheckIfElectronPassedSelectionCuts(e_PCAL_x, e_PCAL_y,
e_PCAL_W, e_PCAL_V,
e_E_PCAL, e_E_ECIN,
e_E_ECOUT,
e, Ve,
e_PCAL_sector, // e_PCAL_sector should be consistent with e_DC_sector
e_DC_x, e_DC_y, e_DC_z,
torusBending );
if (fdebug > 2) std::cout << "extracted electron information and computed kinematics" << std::endl;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void ExtractPionsInformation(int fdebug){
// positive pions
// if (evnum>67 && evnum<74)
// std::cout << "event " << evnum << " in ExtractPionsInformation, Npips= " << Npips << std::endl;
for (int pipsIdx=0; pipsIdx < Npips; pipsIdx++) {
// std::cout << "event " << evnum << " pipsIdx= " << pipsIdx << std::endl;
ExtractPipsInformation( pipsIdx, fdebug );
}
// negative pions
for (int pimsIdx=0; pimsIdx < Npims; pimsIdx++) {
ExtractPimsInformation( pimsIdx, fdebug );
}
// done
if (fdebug > 2) std::cout << "done extracting pion information" << std::endl;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void WriteEventToOutput(int fdebug){
if (fdebug>3) std::cout << "Writing (e,e'pi) event" << std::endl;
// if we simulated a positive pion
// stream to csv all positive pions as seperate rows
if (PiCharge=="pips") {
if (Npips==0) {
// this means that no pions were reconstructed,
// and we just want to record the event that we generated
Stream_e_pi_line_to_CSV( 0, fdebug );
}
else {
for (int pipsIdx=0; pipsIdx<Npips; pipsIdx++) {
Stream_e_pi_line_to_CSV( pipsIdx, fdebug );
}
}
}
// if we simulated a negative pion
// stream to csv all negative pions as seperate rows
else if (PiCharge=="pims") {
if (Npims==0) {
// this means that no pions were reconstructed,
// and we just want to record the event that we generated
Stream_e_pi_line_to_CSV( 0, fdebug );
}
else {
for (int pimsIdx=0; pimsIdx<Npims; pimsIdx++) {
Stream_e_pi_line_to_CSV( pimsIdx, fdebug );
}
}
}
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void FinishProgram(){
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
std::cout << "Done. Elapsed time: " << elapsed.count() << std::endl;
CloseOutputFiles();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void ExtractPipsInformation( int pipsIdx, int fdebug ){
if (fdebug>2)
std::cout << "ExtractPipsInformation( pipsIdx=" << pipsIdx << ", fdebug=" << fdebug << " )" << std::endl;
// extract positive pion information
SetLorentzVector(piplus[pipsIdx] ,pipluses[pipsIdx]);
Zpips[pipsIdx] = piplus[pipsIdx].E() / omega;
Vpiplus[pipsIdx] = GetParticleVertex( pipluses[pipsIdx] );
pips_chi2PID[pipsIdx] = pipluses[pipsIdx]->par()->getChi2Pid();
// EC in and out
pips_E_ECIN[pipsIdx] = pipluses[pipsIdx]->cal(ECIN)->getEnergy();
pips_E_ECOUT[pipsIdx] = pipluses[pipsIdx]->cal(ECOUT)->getEnergy();
// PCAL
auto pips_PCAL_info = pipluses[pipsIdx]->cal(PCAL);
pips_E_PCAL[pipsIdx] = pips_PCAL_info->getEnergy();
pips_PCAL_sector[pipsIdx] = pips_PCAL_info->getSector();
pips_PCAL_V[pipsIdx] = pips_PCAL_info->getLv();
pips_PCAL_W[pipsIdx] = pips_PCAL_info->getLw();
pips_PCAL_x[pipsIdx] = pips_PCAL_info->getX();
pips_PCAL_y[pipsIdx] = pips_PCAL_info->getY();
pips_PCAL_z[pipsIdx] = pips_PCAL_info->getZ();
// DC
auto pips_DC_info = pipluses[pipsIdx]->trk(DC);
pips_DC_sector[pipsIdx] = pips_DC_info->getSector(); // tracking sector
pips_Chi2N[pipsIdx] = pips_DC_info->getChi2N(); // tracking chi^2/NDF
for (int regionIdx=0; regionIdx<3; regionIdx++) {
DC_layer = DC_layers[regionIdx];
pips_DC_x[pipsIdx][regionIdx] = pipluses[pipsIdx]->traj(DC,DC_layer)->getX();
pips_DC_y[pipsIdx][regionIdx] = pipluses[pipsIdx]->traj(DC,DC_layer)->getY();
pips_DC_z[pipsIdx][regionIdx] = pipluses[pipsIdx]->traj(DC,DC_layer)->getZ();
if (fdebug>3) {
std::cout
<< "pips_DC_sector[pipsIdx="<<pipsIdx<<"]="
<< pips_DC_sector[pipsIdx]
<< ", DC_layer = " << DC_layer
<< std::endl
<< "pips_DC_x[pipsIdx="<<pipsIdx<<"][regionIdx="<<regionIdx<<"]="
<< pips_DC_x[pipsIdx][regionIdx]
<< std::endl
<< "pips_DC_y[pipsIdx="<<pipsIdx<<"][regionIdx="<<regionIdx<<"]="
<< pips_DC_y[pipsIdx][regionIdx]
<< std::endl
<< "pips_DC_z[pipsIdx="<<pipsIdx<<"][regionIdx="<<regionIdx<<"]="
<< pips_DC_z[pipsIdx][regionIdx]
<< std::endl;
}
}
piplus_Px[pipsIdx] = piplus[pipsIdx].Px();
piplus_Py[pipsIdx] = piplus[pipsIdx].Py();
piplus_Pz[pipsIdx] = piplus[pipsIdx].Pz();
piplus_E[pipsIdx] = piplus[pipsIdx].E();
Vpiplus_X[pipsIdx] = Vpiplus[pipsIdx].X();
Vpiplus_Y[pipsIdx] = Vpiplus[pipsIdx].Y();
Vpiplus_Z[pipsIdx] = Vpiplus[pipsIdx].Z();
// if (evnum>67 && evnum<74)
// std::cout << "event " << evnum << " before CheckIfPionPassedSelectionCuts( pipsIdx=" << pipsIdx << ")" << std::endl;
pipsPastSelectionCuts[pipsIdx] = CheckIfPionPassedSelectionCuts("pi+",
pips_DC_sector[pipsIdx],
pips_DC_x[pipsIdx],
pips_DC_y[pipsIdx],
pips_DC_z[pipsIdx],
pips_chi2PID[pipsIdx], piplus[pipsIdx].P(),
Ve,
Vpiplus[pipsIdx],
pipsIdx,
fdebug);
// if (evnum>67 && evnum<74)
// std::cout << "event " << evnum << " after CheckIfPionPassedSelectionCuts( pipsIdx=" << pipsIdx << ")" << std::endl;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void ExtractPimsInformation( int pimsIdx, int fdebug ){
// extract negative pion information
SetLorentzVector(piminus[pimsIdx] ,piminuses[pimsIdx]);
Zpims[pimsIdx] = piminus[pimsIdx].E() / omega;
Vpiminus[pimsIdx] = GetParticleVertex( piminuses[pimsIdx] );
pims_chi2PID[pimsIdx] = piminuses[pimsIdx]->par()->getChi2Pid();
// EC in and out
pims_E_ECIN[pimsIdx] = piminuses[pimsIdx]->cal(ECIN)->getEnergy();
pims_E_ECOUT[pimsIdx] = piminuses[pimsIdx]->cal(ECOUT)->getEnergy();
// PCAL
auto pims_PCAL_info = piminuses[pimsIdx]->cal(PCAL);
pims_E_PCAL[pimsIdx] = pims_PCAL_info->getEnergy();
pims_PCAL_sector[pimsIdx] = pims_PCAL_info->getSector();
pims_PCAL_V[pimsIdx] = pims_PCAL_info->getLv();
pims_PCAL_W[pimsIdx] = pims_PCAL_info->getLw();
pims_PCAL_x[pimsIdx] = pims_PCAL_info->getX();
pims_PCAL_y[pimsIdx] = pims_PCAL_info->getY();
pims_PCAL_z[pimsIdx] = pims_PCAL_info->getZ();
// DC
auto pims_DC_info = piminuses[pimsIdx]->trk(DC);
pims_DC_sector[pimsIdx] = pims_DC_info->getSector(); // tracking sector
pims_Chi2N[pimsIdx] = pims_DC_info->getChi2N(); // tracking chi^2/NDF
for (int regionIdx=0; regionIdx<3; regionIdx++) {
DC_layer = DC_layers[regionIdx];
pims_DC_x[pimsIdx][regionIdx] = piminuses[pimsIdx]->traj(DC,DC_layer)->getX();
pims_DC_y[pimsIdx][regionIdx] = piminuses[pimsIdx]->traj(DC,DC_layer)->getY();
pims_DC_z[pimsIdx][regionIdx] = piminuses[pimsIdx]->traj(DC,DC_layer)->getZ();
}
piminus_Px[pimsIdx] = piminus[pimsIdx].Px();
piminus_Py[pimsIdx] = piminus[pimsIdx].Py();
piminus_Pz[pimsIdx] = piminus[pimsIdx].Pz();
piminus_E[pimsIdx] = piminus[pimsIdx].E();
Vpiminus_X[pimsIdx] = Vpiminus[pimsIdx].X();
Vpiminus_Y[pimsIdx] = Vpiminus[pimsIdx].Y();
Vpiminus_Z[pimsIdx] = Vpiminus[pimsIdx].Z();
pimsPastSelectionCuts[pimsIdx] = CheckIfPionPassedSelectionCuts("pi-",
pims_DC_sector[pimsIdx],
pims_DC_x[pimsIdx],
pims_DC_y[pimsIdx],
pims_DC_z[pimsIdx],
pims_chi2PID[pimsIdx], piminus[pimsIdx].P(),
Ve,
Vpiminus[pimsIdx],
pimsIdx,
fdebug);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void GetParticlesByType (int evnum, int fdebug){
// get particles by type
Ne = electrons .size();
Nn = neutrons .size();
Np = protons .size();
Npips = pipluses .size();
Npims = piminuses .size();
Ngammas = gammas .size();
Nd = deuterons.size();
if (fdebug>2){
std::cout
<< "particles in event " << evnum << " : "
<< "N(electrons): " << Ne << ","
<< "N(protons): " << Np << ","
<< "N(neutrons): " << Nn << ","
<< "N(pi+): " << Npips << ","
<< "N(pi-): " << Npims << ","
<< "N(gammas): " << Ngammas << ","
<< "N(deuterons): " << Nd << ","
<< std::endl;
}
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void Stream_e_pi_line_to_CSV( int piIdx, int fdebug ){ // write a row of pion number piIdx to CSV file
TLorentzVector pi;
TVector3 Vpi;
double Zpi;
int pi_DC_sector;
bool eepiPastKinematicalCuts;
// std::cout << "Stream_e_pi_line_to_CSV(evnum="<<evnum<<"): "<< "Npips: " << Npips << ", piIdx: " << piIdx << std::endl;
if (PiCharge=="pips") {
pi = piplus [piIdx];
Vpi = Vpiplus[piIdx];
if ( Npips>piIdx ) pi_reconstructed = true;
else pi_reconstructed = false;
pi_passed_cuts = pipsPastSelectionCuts[piIdx];
pi_passed_fiducial_cuts = pipsPastFiducialCuts[piIdx];
// std::cout << "if (PiCharge==pips) { "<< "pi_passed_fiducial_cuts: " << pi_passed_fiducial_cuts << std::endl;
pi_passed_PID_cuts = pipsPastPIDCuts[piIdx];
pi_DC_sector = pips_DC_sector[piIdx];
eepiPastKinematicalCuts = eepipsPastKinematicalCuts[piIdx];
}
else if (PiCharge=="pims") {
pi = piminus [piIdx];
Vpi = Vpiminus[piIdx];
if ( Npims>piIdx ) pi_reconstructed = true;
else pi_reconstructed = false;
pi_passed_cuts = pimsPastSelectionCuts[piIdx];
pi_passed_fiducial_cuts = pimsPastFiducialCuts[piIdx];
pi_passed_PID_cuts = pimsPastPIDCuts[piIdx];
pi_DC_sector = pims_DC_sector[piIdx];
eepiPastKinematicalCuts = eepimsPastKinematicalCuts[piIdx];
}
else {
std::cout << "pion charge undefined at Stream_e_pi_line_to_CSV(), returning " << std::endl;
return;
}
// write a (e,e'pi) event-line to CSV file
// std::cout << "variables: "<< "pi_passed_fiducial_cuts: " << pi_passed_fiducial_cuts << std::endl;
// ------------------------------------------------------------------------------------------------
// compute kinematics that also relies on pion information
// ------------------------------------------------------------------------------------------------
M_X = ( Beam + target - e - pi ).Mag(); // missing mass of the (e,e'\pi) reaction
// now stream data to CSV file
std::vector<double> variables =
{ e.P(), e.Theta(), e.Phi(), Ve.Z(),
pi.P(), pi.Theta(), pi.Phi(), Vpi.Z(),
(double)Npips, (double)Npims, (double)Ne, (double)Ngammas,
(double)Np, (double)Nn, (double)Nd,
e_g.P(), e_g.Theta(), e_g.Phi(), Ve_g.Z(),
pi_g.P(), pi_g.Theta(), pi_g.Phi(), Vpi_g.Z(),
(double)pi_reconstructed, (double)pi_passed_cuts,
(double)pi_passed_fiducial_cuts, (double)pi_passed_PID_cuts,
(double)e_reconstructed, (double)e_passed_cuts,
(double)e_DC_sector, (double)pi_DC_sector,
M_X, (double)eepiPastKinematicalCuts,
};
StreamToCSVfile( variables, fdebug );
if (fdebug>2){
std::cout
<< "PiCharge: " << PiCharge << ", "
<< "pi_DC_sector: " << pi_DC_sector << ", "
<< "M_X: " << M_X << ", "
<< "eepiPastKinematicalCuts: " << eepiPastKinematicalCuts
<< std::endl;
}
}
// Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
bool CheckIfPionPassedSelectionCuts(TString pionCharge, // "pi+" or "pi-"
Double_t DC_sector,
Double_t DC_x[3], Double_t DC_y[3], Double_t DC_z[3],