forked from weishi10141993/EMTFPtAssign2017
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPtRegressionRun3Prep.C
1314 lines (1118 loc) · 61.4 KB
/
PtRegressionRun3Prep.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
//////////////////////////////////////////////////////////////////
/// pT Regression with Run-3 MC for 2022 EMTF pT assignment ///
/// Sven Dildick ///
/// Adapted from PtRegression_Apr_2017.C ///
//////////////////////////////////////////////////////////////////
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <list>
#include "TChain.h"
#include "TFile.h"
#include "TTree.h"
#include "TString.h"
#include "TObjString.h"
#include "TSystem.h"
#include "TROOT.h"
#include "TMVA/Tools.h"
#include "TMVA/Factory.h"
#include "TMVA/DataLoader.h"
#include "TMVA/TMVARegGui.h"
#include "TMVA/MethodBase.h"
// Extra tools
#include "interface/MVA_helper.h"
#include "src/TrackBuilder.cc"
#include "src/PtLutVarCalc.cc"
// Configuration settings
#include "configs/PtRegression2018/Standard.h" // Constants
#include "configs/PtRegression2018/General.h" // Settings for all modes
#include "configs/PtRegression2018/User.h" // User specific setting
#include "configs/PtRegression2018/Modes.h" // Mode specific settign
//=====================================================
//Make sure it's up-to-date
//Also Ntuple format/index etc changed b/t 2017 and 2018
//Possibly cause error
//=====================================================
#include "configs/PtRegression2018/Read_FlatNtuple.h"
//////////////////////////////////
/// Main executable function ///
//////////////////////////////////
using namespace TMVA;
void PtRegressionRun3Prep(TString user = "",
TString myMethodList = "",
unsigned emtfMode = 15,
float minPt = 1.,
float maxPt = 1000.,
float minPtTrain = 1.,
float maxPtTrain = 256.,
float minEta = 1.25,
float maxEta = 2.4,
unsigned long trainVarsSelection = 0,
unsigned long trainVarsSize = 0,
bool isRun2 = true,
bool useOneQuartPrecision = false,
bool useOneEighthPrecision = false,
bool useBitCompression = false,
int nEvents = -1,
bool verbose = false) {
// Expert options
// Run-2 overrides all options
//if (isRun2) {
// useOneQuartPrecision = false;
// useOneEighthPrecision = false;
//}
// check if 1/4 is on
//if (useOneEighthPrecision)
// useOneQuartPrecision = true;
// FIXME, check if the useGEM bit is set
bool useGEM = false;
std::cout << "Running PtRegressionRun3Prep with options:\n"
<< " - emtfMode: " << emtfMode << "\n"
<< " - minPt: " << minPt << "\n"
<< " - maxPt: " << maxPt << "\n"
<< " - minPtTrain: " << minPtTrain << "\n"
<< " - maxPtTrain: " << maxPtTrain << "\n"
<< " - minEta: " << minEta << "\n"
<< " - maxEta: " << maxEta << "\n"
<< " - trainVarsSelection: " << trainVarsSelection << "\n"
<< " - isRun2: " << isRun2 << "\n"
<< " - useOneQuartPrecision: " << useOneQuartPrecision << "\n"
<< " - useOneEighthPrecision: " << useOneEighthPrecision << "\n"
<< " - useGEM: " << useGEM << "\n"
<< " - useBitCompression: " << useBitCompression << "\n"
<< std::endl;
// This loads the library
TMVA::Tools::Instance();
// Default MVA methods to be trained + tested
std::map<std::string,int> Use;
/////////////////////////
/// USER choose MVA ///
/////////////////////////
//=================================
// Neural Network
Use["MLP"] = 0;
// Support Vector Machine
Use["SVM"] = 0;
// Boosted Decision Trees
Use["BDTG_AWB"] = 0;
Use["BDTG_AWB_Hub"] = 0;
Use["BDTG_AWB_Sq"] = 1;
//==================================
std::cout << std::endl;
std::cout << "==> Start PtRegressionRun3Prep" << std::endl;
// Select methods (don't look at this code - not of interest)
std::vector<TString> mlist;
if (myMethodList != "") {
for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
mlist = gTools().SplitString( myMethodList, ',' );
for (UInt_t i=0; i<mlist.size(); i++) {
std::string regMethod(mlist[i]);
if (Use.find(regMethod) == Use.end()) {
std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
std::cout << std::endl;
return;
}
Use[regMethod] = 1;
}
}
//===================
//Preparation phase
//===================
// Configure settings for this mode and user
PtRegression2018_cfg::ConfigureMode( emtfMode );
PtRegression2018_cfg::ConfigureUser( user );
// Create a new root output file
TString out_file_str;
TString bit_str = (useBitCompression ? "bitCompr" : "noBitCompr");
out_file_str.Form( "%s/%s.root",
OUT_DIR_NAME.Data(), OUT_FILE_NAME.Data());
TFile* out_file = TFile::Open( out_file_str, "RECREATE" );
// Initialize empty file to access each file in the list
TFile *file_tmp(0);
// List of input files
std::vector<TString> SM_in_file_names;//SingleMu
std::vector<TString> ZB_in_file_names;//Zerobias
TString SM_file_name;
TString ZB_file_name;
for (int i = 0; i < SingleMu_files.size(); i++) {
SM_file_name.Form( "%s%s", EOS_DIR_NAME.Data(), SingleMu_files[i].Data() );
std::cout << "Adding file " << SM_file_name.Data() << std::endl;
SM_in_file_names.push_back(SM_file_name.Data());
}
for (int i = 0; i < USEZerobias; i++) {
ZB_file_name.Form( "%s/%s/%s", EOS_DIR_NAME.Data(), in_dir.Data(), ZeroBias_files[i].Data() );
std::cout << "Adding file " << ZB_file_name.Data() << std::endl;
ZB_in_file_names.push_back(ZB_file_name.Data());
}
// Open all input files
for (unsigned i = 0; i < SM_in_file_names.size(); i++) {
if ( !gSystem->AccessPathName(SM_in_file_names.at(i)) )
file_tmp = TFile::Open( SM_in_file_names.at(i) ); // Check if file exists
if (!file_tmp) {
std::cout << "ERROR: could not open data file " << SM_in_file_names.at(i) << std::endl;
return;
}
}
for (unsigned i = 0; i < ZB_in_file_names.size(); i++) {
if ( !gSystem->AccessPathName(ZB_in_file_names.at(i)) )
file_tmp = TFile::Open( ZB_in_file_names.at(i) ); // Check if file exists
if (!file_tmp) {
std::cout << "ERROR: could not open data file " << ZB_in_file_names.at(i) << std::endl;
return;
}
}
// Add tree from the input files to the TChain
std::vector<TChain*> in_chains;
TString treeString = "FlatNtupleMC/tree";
if (isRun2) {
// TEMP CHANGE MARCH 1, 2022
//treeString = "FlatNtupleMCRun2/tree";
//treeString = "tree";
//UPDATED TO RUN WITH RUN 2 VARS BUT USING RUN3 TPs
treeString = "FlatNtupleMCRun3/tree";
}
if (!isRun2) {
treeString = "FlatNtupleMCRun3/tree";
}
std::cout << treeString << std::endl;
TChain *SM_in_chain = new TChain(treeString);
TChain *ZB_in_chain = new TChain(treeString);
for (int i = 0; i < SM_in_file_names.size(); i++) {
SM_in_chain->Add( SM_in_file_names.at(i) );
}
for (int i = 0; i < ZB_in_file_names.size(); i++) {
ZB_in_chain->Add( ZB_in_file_names.at(i) );
}
std::cout << "SM_in_chain entries " << SM_in_chain->GetEntries() << std::endl;
std::cout << "ZB_in_chain entries " << ZB_in_chain->GetEntries() << std::endl;
InitializeMaps();
SetBranchAddresses(SM_in_chain);
SetBranchAddresses(ZB_in_chain);
in_chains.push_back(SM_in_chain);
in_chains.push_back(ZB_in_chain);
//////////////////////////////////////////////////////////////////////////
/// Factories: Use different sets of variables, target, weights, etc. ///
//////////////////////////////////////////////////////////////////////////
TString fact_set = "!V:!Silent:Color:DrawProgressBar:AnalysisType=Regression";
std::vector<TString> var_names; // Holds names of variables for a given factory and permutation
std::vector<Double_t> var_vals; // Holds values of variables for a given factory and permutation
TMVA::Factory* nullF = new TMVA::Factory("NULL", out_file, fact_set); // Placeholder factory
TMVA::DataLoader* nullL = new TMVA::DataLoader("NULL");// Placeholder loader
// Tuple is defined by the factory and dataloader, followed by a name,
// var name and value vectors, and hex bit masks for input variables.
// Each hex bit represents four variables, e.g. 0x1 would select only the 1st variable,
// 0xf the 1st 4, 0xff the 1st 8, 0xa the 2nd and 4th, 0xf1 the 1st and 5th-8th, etc.
std::vector< std::tuple<TMVA::Factory*, TMVA::DataLoader*, TString, std::vector<TString>, std::vector<Double_t>, unsigned long> > factories;
for (unsigned iTarg = 0; iTarg < TARG_VARS.size(); iTarg++) {
for (unsigned iWgt = 0; iWgt < EVT_WGTS.size(); iWgt++) {
TString factName; // "Targ" and "Wgt" components not arbitrary - correspond to specific options later on
factName.Form( "f_%sTarg_%sWgt",
TARG_VARS.at(iTarg).Data(), EVT_WGTS.at(iWgt).Data());
// the selection is now done in the Python configuration, not here!
factories.push_back( std::make_tuple( nullF, nullL, factName, var_names, var_vals, trainVarsSelection) );
}
}
// Initialize factories and dataloaders
for (unsigned iFact = 0; iFact < factories.size(); iFact++) {
std::get<0>(factories.at(iFact)) = new TMVA::Factory( std::get<2>(factories.at(iFact)), out_file, fact_set );
std::get<1>(factories.at(iFact)) = new TMVA::DataLoader( std::get<2>(factories.at(iFact)) );
}
// Defined in interface/MVA_helper.h
std::vector<MVA_var> in_vars; // All input variables
std::vector<MVA_var> targ_vars; // All target variables (should only define 1, unless using MLP)
std::vector<MVA_var> spec_vars; // All spectator variables
std::vector<MVA_var> all_vars; // All variables
/////////////////////////
/// Input variables ///
/////////////////////////
/// this block needs to match exactly the "allowedTrainingVars" block!!!
// block 1
in_vars.push_back( MVA_var( "theta", "Track #theta", "int", 'I', -88 ) ); // 0x0000 0001
in_vars.push_back( MVA_var( "St1_ring2", "St 1 hit in ring 2", "int", 'I', -88 ) ); // 0x0000 0002
in_vars.push_back( MVA_var( "dPhi_12", "#phi(2) - #phi(1)", "int", 'I', -88 ) ); // 0x0000 0004
in_vars.push_back( MVA_var( "dPhi_23", "#phi(3) - #phi(2)", "int", 'I', -88 ) ); // 0x0000 0008
// block 2
in_vars.push_back( MVA_var( "dPhi_34", "#phi(4) - #phi(3)", "int", 'I', -88 ) ); // 0x0000 0010
in_vars.push_back( MVA_var( "dPhi_13", "#phi(3) - #phi(1)", "int", 'I', -88 ) ); // 0x0000 0020
in_vars.push_back( MVA_var( "dPhi_14", "#phi(4) - #phi(1)", "int", 'I', -88 ) ); // 0x0000 0040
in_vars.push_back( MVA_var( "dPhi_24", "#phi(4) - #phi(2)", "int", 'I', -88 ) ); // 0x0000 0080
// block 3
in_vars.push_back( MVA_var( "FR_1", "St 1 LCT F/R", "int", 'I', -88 ) ); // 0x0000 0100
in_vars.push_back( MVA_var( "FR_2", "St 2 LCT F/R", "int", 'I', -88 ) ); // 0x0000 0200
in_vars.push_back( MVA_var( "FR_3", "St 3 LCT F/R", "int", 'I', -88 ) ); // 0x0000 0400
in_vars.push_back( MVA_var( "FR_4", "St 4 LCT F/R", "int", 'I', -88 ) ); // 0x0000 0800
// block 4
in_vars.push_back( MVA_var( "bend_1", "St 1 LCT bending", "int", 'I', -88 ) ); // 0x0000 1000
in_vars.push_back( MVA_var( "bend_2", "St 2 LCT bending", "int", 'I', -88 ) ); // 0x0000 2000
in_vars.push_back( MVA_var( "bend_3", "St 3 LCT bending", "int", 'I', -88 ) ); // 0x0000 4000
in_vars.push_back( MVA_var( "bend_4", "St 4 LCT bending", "int", 'I', -88 ) ); // 0x0000 8000
// block 5
in_vars.push_back( MVA_var( "dPhiSum4", "#Sigmad#phi (6)", "int", 'I', -88 ) ); // 0x0001 0000
in_vars.push_back( MVA_var( "dPhiSum4A", "#Sigma|d#phi| (6)", "int", 'I', -88 ) ); // 0x0002 0000
in_vars.push_back( MVA_var( "dPhiSum3", "#Sigmad#phi (3)", "int", 'I', -88 ) ); // 0x0004 0000
in_vars.push_back( MVA_var( "dPhiSum3A", "#Sigma|d#phi| (3)", "int", 'I', -88 ) ); // 0x0008 0000
// block 6
in_vars.push_back( MVA_var( "outStPhi", "#phi outlier St", "int", 'I', -88 ) ); // 0x0010 0000
in_vars.push_back( MVA_var( "filler", "Filler", "int", 'I', -88 ) ); // 0x0020 0000
in_vars.push_back( MVA_var( "dTh_12", "#theta(2) - #theta(1)", "int", 'I', -88 ) ); // 0x0040 0000
in_vars.push_back( MVA_var( "dTh_23", "#theta(3) - #theta(2)", "int", 'I', -88 ) ); // 0x0080 0000
// block 7
in_vars.push_back( MVA_var( "dTh_34", "#theta(4) - #theta(3)", "int", 'I', -88 ) ); // 0x0100 0000
in_vars.push_back( MVA_var( "dTh_13", "#theta(3) - #theta(1)", "int", 'I', -88 ) ); // 0x0200 0000
in_vars.push_back( MVA_var( "dTh_14", "#theta(4) - #theta(1)", "int", 'I', -88 ) ); // 0x0400 0000
in_vars.push_back( MVA_var( "dTh_24", "#theta(4) - #theta(2)", "int", 'I', -88 ) ); // 0x0800 0000
// block 8
in_vars.push_back( MVA_var( "RPC_1", "St 1 hit is RPC", "int", 'I', -88 ) ); // 0x1000 0000
in_vars.push_back( MVA_var( "RPC_2", "St 2 hit is RPC", "int", 'I', -88 ) ); // 0x2000 0000
in_vars.push_back( MVA_var( "RPC_3", "St 3 hit is RPC", "int", 'I', -88 ) ); // 0x4000 0000
in_vars.push_back( MVA_var( "RPC_4", "St 4 hit is RPC", "int", 'I', -88 ) ); // 0x8000 0000
// block 9
in_vars.push_back( MVA_var( "slope_1", "St 1 LCT slope", "int", 'I', -88 ) ); // 0x0000 1000
in_vars.push_back( MVA_var( "slope_2", "St 2 LCT slope", "int", 'I', -88 ) ); // 0x0000 2000
in_vars.push_back( MVA_var( "slope_3", "St 3 LCT slope", "int", 'I', -88 ) ); // 0x0000 4000
in_vars.push_back( MVA_var( "slope_4", "St 4 LCT slope", "int", 'I', -88 ) ); // 0x0000 8000
// block 10
in_vars.push_back( MVA_var( "dSlope_12", "slope(2) - slope(1)", "int", 'I', -88 ) ); // 0x0040 0000
in_vars.push_back( MVA_var( "dSlope_23", "slope(3) - slope(2)", "int", 'I', -88 ) ); // 0x0080 0000
in_vars.push_back( MVA_var( "dSlope_34", "slope(4) - slope(3)", "int", 'I', -88 ) ); // 0x0100 0000
in_vars.push_back( MVA_var( "dSlope_13", "slope(3) - slope(1)", "int", 'I', -88 ) ); // 0x0200 0000
// block 11
in_vars.push_back( MVA_var( "dSlope_14", "slope(4) - slope(1)", "int", 'I', -88 ) ); // 0x0400 0000
in_vars.push_back( MVA_var( "dSlope_24", "slope(4) - slope(2)", "int", 'I', -88 ) ); // 0x0800 0000
in_vars.push_back( MVA_var( "dPhi_GE11_ME11", "#phi(GE11) - #phi(ME11)", "", 'I', -88 ) ); // 0x1 0000 0000
in_vars.push_back( MVA_var( "GEM_1", "St 1 hit is GEM", "int", 'I', -88 ) ); // 0x1 0000 0000
// block 12
in_vars.push_back( MVA_var( "dSlopeSum4", "#SigmadSlope (6)", "int", 'I', -88 ) ); // 0x0001 0000
in_vars.push_back( MVA_var( "dSlopeSum4A", "#Sigma|dSlope| (6)", "int", 'I', -88 ) ); // 0x0002 0000
in_vars.push_back( MVA_var( "dSlopeSum3", "#SigmadSlope (3)", "int", 'I', -88 ) ); // 0x0001 0000
in_vars.push_back( MVA_var( "dSlopeSum3A", "#Sigma|dSlope| (3)", "int", 'I', -88 ) ); // 0x0002 0000
// block 13
in_vars.push_back( MVA_var( "outStSlope", "slope outlier St", "int", 'I', -88 ) ); // 0x0010 0000
in_vars.push_back( MVA_var( "Ph1Slope1MinusPh2", "Phi1 + Slope12 - Ph2", "int", 'I', -88 ) ); // 0x0010 0000
in_vars.push_back( MVA_var( "Ph2Slope2MinusPh3", "Phi2 + Slope23 - Ph3", "int", 'I', -88 ) ); // 0x0010 0000
in_vars.push_back( MVA_var( "Ph3Slope3MinusPh4", "Phi3 + Slope34 - Ph4", "int", 'I', -88 ) ); // 0x0010 0000
////////////////////////////////////////////////////////////
// Target variable: true muon pT, or 1/pT, or log2(pT) ///
////////////////////////////////////////////////////////////
targ_vars.push_back( MVA_var( "GEN_pt_trg", "GEN p_{T} for trigger", "GeV", 'F', -99 ) );
targ_vars.push_back( MVA_var( "inv_GEN_pt_trg", "1 / GEN muon p_{T} for trigger", "GeV^{-1}", 'F', -99 ) );
targ_vars.push_back( MVA_var( "log2_GEN_pt_trg", "log_{2}(GEN muon p_{T} for trigger)", "GeV", 'F', -99 ) );
targ_vars.push_back( MVA_var( "sqrt_GEN_pt_trg", "sqrt GEN muon p_{T} for trigger", "GeV^{0.5}",'F', -99 ) );
targ_vars.push_back( MVA_var( "GEN_charge_trg", "Muon charge x dPhi sign for trigger", "", 'I', -99 ) );
/////////////////////////////////////////////////////////////////////////////
/// Spectator variables: not used in training, but saved in output tree ///
/////////////////////////////////////////////////////////////////////////////
spec_vars.push_back( MVA_var( "GEN_pt", "GEN p_{T}", "GeV", 'F', -77 ) );
spec_vars.push_back( MVA_var( "EMTF_pt", "EMTF p_{T}", "GeV", 'F', -77 ) );
spec_vars.push_back( MVA_var( "GEN_eta", "GEN #eta", "", 'F', -77 ) );
spec_vars.push_back( MVA_var( "GEN_phi", "GEN #phi", "", 'F', -77 ) );
spec_vars.push_back( MVA_var( "EMTF_eta", "EMTF #eta", "", 'F', -77 ) );
spec_vars.push_back( MVA_var( "TRK_eta", "Track #eta", "", 'F', -77 ) );
spec_vars.push_back( MVA_var( "GEN_charge", "GEN charge", "", 'I', -77 ) );
spec_vars.push_back( MVA_var( "EMTF_charge", "EMTF charge", "", 'I', -77 ) );
spec_vars.push_back( MVA_var( "EMTF_mode", "EMTF mode", "", 'I', -77 ) );
spec_vars.push_back( MVA_var( "EMTF_mode_CSC", "EMTF CSC-only mode", "", 'I', -77 ) );
spec_vars.push_back( MVA_var( "EMTF_mode_RPC", "EMTF RPC-only", "", 'I', -77 ) );
spec_vars.push_back( MVA_var( "TRK_mode", "Track mode", "", 'I', -77 ) );
spec_vars.push_back( MVA_var( "TRK_mode_CSC", "Track CSC-only mode", "", 'I', -77 ) );
spec_vars.push_back( MVA_var( "TRK_mode_RPC", "Track RPC-only mode", "", 'I', -77 ) );
spec_vars.push_back( MVA_var( "dPhi_sign", "#phi(B) - #phi(A) sign", "", 'I', -77 ) );
spec_vars.push_back( MVA_var( "evt_weight", "Event weight for training", "", 'F', -77 ) );
// extra spectator variables to inspect correlations
//spec_vars.push_back( MVA_var( "slope_1", "St 1 LCT slope", "int", 'I', -77 ) ); // 0x0000 1000
//spec_vars.push_back( MVA_var( "slope_2", "St 2 LCT slope", "int", 'I', -77 ) ); // 0x0000 2000
//spec_vars.push_back( MVA_var( "slope_3", "St 3 LCT slope", "int", 'I', -77 ) ); // 0x0000 4000
//spec_vars.push_back( MVA_var( "slope_4", "St 4 LCT slope", "int", 'I', -77 ) ); // 0x0000 8000
spec_vars.push_back( MVA_var( "ph1", "St 1 LCT phi", "int", 'I', -77 ) ); // 0x0000 1000
spec_vars.push_back( MVA_var( "ph2", "St 2 LCT phi", "int", 'I', -77 ) ); // 0x0000 2000
spec_vars.push_back( MVA_var( "ph3", "St 3 LCT phi", "int", 'I', -77 ) ); // 0x0000 4000
spec_vars.push_back( MVA_var( "ph4", "St 4 LCT phi", "int", 'I', -77 ) ); // 0x0000 8000
spec_vars.push_back( MVA_var( "TRK_hit_ids", "Track Hits Id", "int", 'I', -77 ) );
assert( in_vars.size() > 0 ); // Need at least one input variable
assert( targ_vars.size() > 0 ); // Need at least one target variable
// Order is important: input variables first, then target, then specator
all_vars.insert( all_vars.end(), in_vars.begin(), in_vars.end() );
all_vars.insert( all_vars.end(), targ_vars.begin(), targ_vars.end() );
if (SPEC_VARS) all_vars.insert( all_vars.end(), spec_vars.begin(), spec_vars.end() );
// Fill each factory with the correct set of variables
for (unsigned iFact = 0; iFact < factories.size(); iFact++) {
std::cout << "\n*** Factory " << std::get<2>(factories.at(iFact)) << " variables ***" << std::endl;
std::cout << std::endl << "*** Input ***" << std::endl;
for (unsigned i = 0; i < in_vars.size(); i++) {
// Hex bit mask for in_vars
if ( 0x1 & (std::get<5>(factories.at(iFact)) >> i) ) {
MVA_var v = in_vars.at(i);
std::cout << v.name << std::endl;
std::get<1>(factories.at(iFact))->AddVariable( v.name, v.descr, v.unit, v.type ); // Add var to dataloader
std::get<3>(factories.at(iFact)).push_back( v.name ); // Add to vector of var names
std::get<4>(factories.at(iFact)).push_back( v.def_val ); // Add to vector of var values
}
}
TString targ_str = ""; // Save name of target variable
std::cout << std::endl << "*** Target ***" << std::endl;
for (unsigned i = 0; i < targ_vars.size(); i++) {
MVA_var v = targ_vars.at(i);
if ( (v.name == "GEN_pt_trg" && std::get<2>(factories.at(iFact)).Contains("_ptTarg")) ||
(v.name == "inv_GEN_pt_trg" && std::get<2>(factories.at(iFact)).Contains("_invPtTarg")) ||
(v.name == "log2_GEN_pt_trg" && std::get<2>(factories.at(iFact)).Contains("_logPtTarg")) ||
(v.name == "sqrt_GEN_pt_trg" && std::get<2>(factories.at(iFact)).Contains("_sqrtPtTarg")) ||
(v.name == "GEN_charge_trg" && std::get<2>(factories.at(iFact)).Contains("_chargeTarg")) ) {
std::cout << v.name << std::endl;
targ_str = v.name;
std::get<1>(factories.at(iFact))->AddTarget( v.name, v.descr, v.unit, v.type );
std::get<3>(factories.at(iFact)).push_back( v.name );
std::get<4>(factories.at(iFact)).push_back( v.def_val );
}
}
std::cout << std::endl << "*** Spectator ***" << std::endl;
for (UInt_t i = 0; i < spec_vars.size(); i++) {
MVA_var v = spec_vars.at(i);
if (v.name == targ_str) continue; // Don't add target variable
std::cout << v.name << std::endl;
std::get<1>(factories.at(iFact))->AddSpectator( v.name, v.descr, v.unit, v.type );
std::get<3>(factories.at(iFact)).push_back( v.name );
std::get<4>(factories.at(iFact)).push_back( v.def_val );
}
} // End loop: for (UInt_t iFact = 0; iFact < factories.size(); iFact++)
int nSMEvents = SM_in_chain->GetEntries();
int nZBEvents = ZB_in_chain->GetEntries();
std::cout << "\n******* About to loop over chains *******" << std::endl;
std::cout << "\n in_chains size: "<< in_chains.size() << " N(SingleMu) = " << nSMEvents << " N(ZeroBias) = " << nZBEvents << std::endl;
UInt_t NonZBEvt = 0;
UInt_t ZBEvt = 0;
UInt_t nTrain = 0;
UInt_t nTest = 0;
UInt_t nPosEndCapTrain = 0;
UInt_t nNegEndCapTrain = 0;
UInt_t nPosEndCapTest = 0;
UInt_t nNegEndCapTest = 0;
UInt_t nForceMode = 0;
Bool_t isZB = false;//tag per event
Bool_t isTEST = false;//tag per event
unsigned iEvent = 0;
//=================================
//Register events: loop over chains
//=================================
for (unsigned iCh = 0; iCh < in_chains.size(); iCh++) {
TChain *in_chain = in_chains.at(iCh);
std::cout << "******* About to enter the event loop for chain " << iCh+1 << " " << in_chain->GetEntries() << " *******" << std::endl;
for (UInt_t jEvt = 0; jEvt < in_chain->GetEntries(); jEvt++) {
if (iEvent > nEvents) break;
iEvent++;
if (jEvt%1000==0) std::cout << "******* About to loop on event " << jEvt << ", nTrain: " << nTrain << ", nTest: " << nTest << ", nPosEndCap: " << nPosEndCapTrain << ", nNegEndCap: " << nNegEndCapTrain << " Number Forced Mode: " << nForceMode << " *******" << std::endl;
//!!! jEvt restarts from 0 in new chain
//!!! iCh<1 important here: Protect against small MAX_TR setting
//When iCh = 1, it start to load ZB events, the first break from MAX_TR shouldn't affect the following ZB loading process
//Otherwise no ZB events will be loaded, cause trouble when calculating rate
if (nTrain > MAX_TR && iCh<1)
{
std::cout << "******* Ending loop on event " << jEvt << ", nTrain: " << nTrain << ", nTest: " << nTest << ", nPosEndCap: " << nPosEndCapTrain << ", nNegEndCap: " << nNegEndCapTrain << " *******" << std::endl;
break;
}
//if (nTest > MAX_TE) break;
//iCh=0 means SingleMu dataset,
//need to modify in the future if have more types of samples added for training, such as Muonia, etc
if (iCh<1) {
isZB = false;
isTEST = false;
NonZBEvt += 1;
}
in_chain->GetEntry(jEvt);
UInt_t nMuons = I("nGenMuons");//reco_* branches are true info reference
UInt_t nHits = I("nHits");//hit_* branches are unpacked hits
UInt_t nTrks = I("nTracks");//trk_* branches are EMTF tracks
UInt_t nSegs = I("nSegs");//csc segments number
//===================
//Loop over EMTF trks
//===================
for (UInt_t iTrk = 0; iTrk < nTrks; iTrk++) {
double emtf_pt = F("trk_pt",iTrk);
double emtf_eta = F("trk_eta",iTrk);
double emtf_phi = F("trk_phi",iTrk);
int emtf_eta_int = I("trk_eta_int", iTrk);
int emtf_charge = I("trk_charge", iTrk);
int emtf_mode = I("trk_mode", iTrk);
int emtf_mode_CSC = I("trk_mode_CSC", iTrk);
int emtf_mode_RPC = I("trk_mode_RPC", iTrk);
int emtf_unique_match = I("trk_dR_match_unique", iTrk);
int emtf_unique_iMu = 0;//I("trk_dR_match_iReco", iTrk);
int emtf_dR_match_nReco = I("trk_dR_match_nReco", iTrk);
int emtf_dR_match_nRecoSoft = I("trk_dR_match_nRecoSoft", iTrk);
double mu_pt = 999.;//Default for muons in ZB
double mu_eta = -99.;
double mu_phi = -99.;
int mu_charge = -99;
int gmt_pt = 999;
Bool_t mu_train = false; // tag muon for training
Bool_t equalEndCapsTrain = true; // ensure equal number of events in both endcaps
Bool_t equalEndCapsTest = true; // ensure equal number of events in both endcaps
if(verbose)
{
std::cout << "--------------------------------------------------------------- " << std::endl;
std::cout << "Event: " << jEvt << ", iTrk: " << iTrk << std::endl;
}
// index of emtf_unique_iMu is 0 or 1
mu_train = true;
mu_pt = F("mu_pt", emtf_unique_iMu);
mu_eta = F("mu_eta", emtf_unique_iMu);
mu_phi = F("mu_phi", emtf_unique_iMu);
mu_charge = I("mu_charge", emtf_unique_iMu);
//Equal EndCaps
equalEndCapsTrain = ((mu_eta > 0 && (nPosEndCapTrain < MAX_TR/2)) || (mu_eta < 0 && (nNegEndCapTrain < MAX_TR/2)));
equalEndCapsTest = ((mu_eta > 0 && (nPosEndCapTest < MAX_TE/2)) || (mu_eta < 0 && (nNegEndCapTest < MAX_TE/2)));
if(!equalEndCapsTrain && !equalEndCapsTest)
{
if(verbose)
{
std::cout << "Already filled max events for this endcap!" << std::endl;
}
continue;
}
if(verbose) {
std::cout << "True muon pt " << mu_pt << std::endl;
std::cout << "True muon eta " << mu_eta << std::endl;
std::cout << "True muon phi " << mu_phi << std::endl;
std::cout << "True muon charge " << mu_charge << std::endl;
}
if(verbose) std::cout << "RECO kinematics ... "<< std::endl;
//===============================
//RECO mu kinematics requirements
//===============================
if ( !isZB && (mu_pt < minPt || mu_pt > maxPt) ) continue;
if ( !isZB && (fabs( mu_eta ) < minEta || fabs( mu_eta ) > maxEta) ) continue;
if ( mu_pt < minPtTrain || mu_pt > maxPtTrain || isTEST) mu_train = false;
//==================
//Require valid mode
//==================
if(verbose) std::cout << "Valid modes ... "<< std::endl;
bool good_emtf_mode = false;
for (UInt_t jMode = 0; jMode < EMTF_MODES.size(); jMode++) {
if ( emtf_mode == EMTF_MODES.at(jMode) ) good_emtf_mode = true;
}
if (!good_emtf_mode) {
emtf_mode = -99;
continue;
}
if (emtf_mode < 0) {
std::cout << "Rare case: EMTF mode < 0 "<< std::endl;
continue;
}
//======================
//Trk hits from stations
//======================
int i1GEM=-99; // separate index for GEM
int i1CSC=-99; // separate index for CSC%
int i2=-99;
int i3=-99;
int i4=-99;
// added on 2019-11-05 per Andrew's suggestions
if ( I("trk_nHits", iTrk) != VI("trk_iHit", iTrk).size() and false) {
std::cout << "Checking number of Track hits " << std::endl;
std::cout << ">>>trk_nHits " << I("trk_nHits", iTrk) << std::endl;
std::cout << ">>>trk_iHit.size " << VI("trk_iHit", iTrk).size() << std::endl;
std::cout << ">>>mode_RPC " << I("trk_mode_RPC", iTrk) << std::endl;
std::cout << ">>>mode_CSC " << I("trk_mode_CSC", iTrk) << std::endl;
continue;
}
//std::cout << "i1GEM before: " << i1GEM << std::endl;
for (int jhit = 0; jhit < I("trk_nHits", iTrk); jhit++) {
int iHit = I("trk_iHit", iTrk, jhit); // Access the index of each hit in the emtf track
// trk_nHits, VI("trk_iHit", iTrk).size(), trk_nRPC, and trk_mode_CSC
if( iHit < nHits){//Avoid the case when iHit index larger than the total number of hits in the event, this happens sometimes
if( I("hit_station", iHit) == 1 && (I("hit_isCSC",iHit)==1 || I("hit_isRPC",iHit)==1) ){ i1CSC = iHit; }
else if( I("hit_station", iHit) == 1 && I("hit_isGEM",iHit)==1 ){ i1GEM = iHit; }
else if ( I("hit_station", iHit) == 2 && (I("hit_isCSC",iHit)==1 || I("hit_isRPC",iHit)==1)){ i2 = iHit; }
else if ( I("hit_station", iHit) == 3 && (I("hit_isCSC",iHit)==1 || I("hit_isRPC",iHit)==1) ){ i3 = iHit; }
else if ( I("hit_station", iHit) == 4 && (I("hit_isCSC",iHit)==1 || I("hit_isRPC",iHit)==1) ){ i4 = iHit; }
}
if( iHit < nHits && verbose){//Avoid the case when iHit index larger than the total number of hits in the event, this happens sometimes
if( I("hit_station", iHit) == 1 && I("hit_isRPC",iHit)==1 ){ std::cout << "##HIT in RPC1" << std::endl; }
else if ( I("hit_station", iHit) == 2 && I("hit_isRPC",iHit)==1 ){ std::cout << "##HIT in RPC2" << std::endl; }
else if ( I("hit_station", iHit) == 3 && I("hit_isRPC",iHit)==1 ){ std::cout << "##HIT in RPC3" << std::endl; }
else if ( I("hit_station", iHit) == 4 && I("hit_isRPC",iHit)==1 ){ std::cout << "##HIT in RPC4" << std::endl; }
}
}//end loop over hits in selected emtf track
//std::cout << "i1GEM after: " << i1GEM << std::endl;
if(verbose) {
std::cout << "index GE1/1: "<<i1GEM<< std::endl;
std::cout << "index ME1: "<<i1CSC<< std::endl;
std::cout << "index ME2: "<<i2<< std::endl;
std::cout << "index ME3: "<<i3<< std::endl;
std::cout << "index ME4: "<<i4<< std::endl;
}
//Assign built trk properties the same as emtf track
int mode = emtf_mode;
int mode_CSC = emtf_mode_CSC;
int mode_RPC = emtf_mode_RPC;
if(verbose) std::cout << "mode: "<<mode<<" MODE: "<<emtfMode<< std::endl;
if (mode != emtfMode) {
if(nForceMode > (MAX_TR + MAX_TE)*FORCE_MODE_FRAC)continue;
if(verbose) std::cout << "Attempting to artificially create MODE: " << emtfMode << std::endl;
if(emtfMode == 15) {
continue;
}else if(emtfMode == 14) {
if(i1CSC > 0 && i2 > 0 && i3 > 0) {
mode = 14;
i4 = -99;
}else{
continue;
}
}else if(emtfMode == 13) {
if(i1CSC > 0 && i2 > 0 && i4 > 0) {
mode = 13;
i3 = -99;
}else{
continue;
}
}else if(emtfMode == 11) {
if(i1CSC > 0 && i3 > 0 && i4 > 0) {
mode = 11;
i2 = -99;
}else{
continue;
}
}else if(emtfMode == 7) {
if(i2 > 0 && i3 > 0 && i4 > 0) {
mode = 7;
i1CSC = -99;
}else{
continue;
}
}else if(emtfMode == 12) {
if(i1CSC > 0 && i2 > 0) {
mode = 12;
i3 = -99;
i4 = -99;
}else{
continue;
}
}else if(emtfMode == 10) {
if(i1CSC > 0 && i3 > 0) {
mode = 10;
i2 = -99;
i4 = -99;
}else{
continue;
}
}else if(emtfMode == 9) {
if(i1CSC > 0 && i4 > 0) {
mode = 9;
i2 = -99;
i3 = -99;
}else{
continue;
}
}else if(emtfMode == 6) {
if(i2 > 0 && i3 > 0) {
mode = 6;
i1CSC = -99;
i4 = -99;
}else{
continue;
}
}else if(emtfMode == 5) {
if(i2 > 0 && i4 > 0) {
mode = 5;
i1CSC = -99;
i3 = -99;
}else{
continue;
}
}else if(emtfMode == 3) {
if(i3 > 0 && i4 > 0) {
mode = 3;
i1CSC = -99;
i2 = -99;
}else{
continue;
}
}
nForceMode++;
}
if (mode != mode_CSC) {
if(verbose) std::cout << "Not CSC-only track"<< std::endl;
//continue;
}
int ph1 = (i1CSC >= 0 ? I("hit_phi_int",i1CSC ) : -99);
int ph1GEM = (i1GEM >= 0 ? I("hit_phi_int",i1GEM ) : -99);
int ph2 = (i2 >= 0 ? I("hit_phi_int", i2 ) : -99);
int ph3 = (i3 >= 0 ? I("hit_phi_int", i3 ) : -99);
int ph4 = (i4 >= 0 ? I("hit_phi_int", i4 ) : -99);
//if ( i1CSC>0 && i1GEM>0 ) { std::cout << "ph1 CSC: " << ph1 << ", ph1GEM: " << ph1GEM << std::endl; }
//if ( i1CSC>0 && i1GEM>0 ) { std::cout << "ph1 CSC: " << ph1 << ", ph1GEM / 4.: " << ph1GEM/4. << std::endl; }
//if ( i1CSC>0 && i1GEM>0 && (abs(ph1 - ph1GEM)>1000) ) { ph1GEM = ph1GEM - 3600; }
//if ( i1CSC>0 && i1GEM>0 ) { std::cout << "dPh : " << ph1 - ph1GEM << std::endl; }
//std::cout << "Before function: " << ph1GEM << std::endl;
//ph1GEM = ph1GEMFix(ph1, ph1GEM);
//std::cout << "After function: " << ph1GEM << std::endl;
int th1 = (i1CSC >= 0 ? I("hit_theta_int",i1CSC ) : -99);
int th1GEM = (i1GEM >= 0 ? I("hit_theta_int",i1GEM ) : -99);
int th2 = (i2 >= 0 ? I("hit_theta_int", i2 ) : -99);
int th3 = (i3 >= 0 ? I("hit_theta_int", i3 ) : -99);
int th4 = (i4 >= 0 ? I("hit_theta_int", i4 ) : -99);
int endcap1 = (i1CSC >= 0 ? I("hit_endcap",i1CSC ) : -99);
int endcap2 = (i2 >= 0 ? I("hit_endcap", i2 ) : -99);
int endcap3 = (i3 >= 0 ? I("hit_endcap", i3 ) : -99);
int endcap4 = (i4 >= 0 ? I("hit_endcap", i4 ) : -99);
int station1 = (i1CSC >= 0 ? I("hit_station",i1CSC ) : -99);
int station2 = (i2 >= 0 ? I("hit_station", i2 ) : -99);
int station3 = (i3 >= 0 ? I("hit_station", i3 ) : -99);
int station4 = (i4 >= 0 ? I("hit_station", i4 ) : -99);
int ring1 = (i1CSC >= 0 ? I("hit_ring",i1CSC ) : -99);
int ring2 = (i2 >= 0 ? I("hit_ring",i2 ) : -99);
int ring3 = (i3 >= 0 ? I("hit_ring",i3 ) : -99);
int ring4 = (i4 >= 0 ? I("hit_ring",i4 ) : -99);
int chamber1 = (i1CSC >= 0 ? I("hit_chamber",i1CSC ) : -99);
int chamber2 = (i2 >= 0 ? I("hit_chamber",i2 ) : -99);
int chamber3 = (i3 >= 0 ? I("hit_chamber",i3 ) : -99);
int chamber4 = (i4 >= 0 ? I("hit_chamber",i4 ) : -99);
// 4-bit value
int strip1 = (i1CSC >= 0 ? I("hit_strip",i1CSC ) : -99);
int strip2 = (i2 >= 0 ? I("hit_strip", i2 ) : -99);
int strip3 = (i3 >= 0 ? I("hit_strip", i3 ) : -99);
int strip4 = (i4 >= 0 ? I("hit_strip", i4 ) : -99);
// if (endcap1 == 1 and station1 == 1 and ring1 == 1 and chamber1==1)
// std::cout << station1 << ring1 << chamber1 << " hit_strip1 " << strip1 << " hit_phi_int1 " << ph1 << std::endl;
// std::cout << "hit_strip2 " << strip2 << " hit_phi_int2 " << ph2 << std::endl;
// std::cout << "hit_strip3 " << strip3 << " hit_phi_int3 " << ph3 << std::endl;
// std::cout << "hit_strip4 " << strip4 << " hit_phi_int4 " << ph4 << std::endl;
// 4-bit value
int pat1 = (i1CSC >= 0 ? I("hit_pattern",i1CSC ) : -99);
int pat2 = (i2 >= 0 ? I("hit_pattern", i2 ) : -99);
int pat3 = (i3 >= 0 ? I("hit_pattern", i3 ) : -99);
int pat4 = (i4 >= 0 ? I("hit_pattern", i4 ) : -99);
// 4-bit value
int pat1_run3 = (i1CSC >= 0 ? I("hit_pattern_run3",i1CSC ) : -99);
int pat2_run3 = (i2 >= 0 ? I("hit_pattern_run3", i2 ) : -99);
int pat3_run3 = (i3 >= 0 ? I("hit_pattern_run3", i3 ) : -99);
int pat4_run3 = (i4 >= 0 ? I("hit_pattern_run3", i4 ) : -99);
// 4-bit value
int slope1 = (i1CSC >= 0 ? I("hit_slope",i1CSC ) : -99);
int slope2 = (i2 >= 0 ? I("hit_slope", i2 ) : -99);
int slope3 = (i3 >= 0 ? I("hit_slope", i3 ) : -99);
int slope4 = (i4 >= 0 ? I("hit_slope", i4 ) : -99);
// 1-bit sign
int bend1 = (i1CSC >= 0 ? I("hit_bend",i1CSC ) : -99);
int bend2 = (i2 >= 0 ? I("hit_bend", i2 ) : -99);
int bend3 = (i3 >= 0 ? I("hit_bend", i3 ) : -99);
int bend4 = (i4 >= 0 ? I("hit_bend", i4 ) : -99);
if(verbose) {
std::cout << "hit_bend1 " << bend1 << std::endl;
std::cout << "hit_bend2 " << bend2 << std::endl;
std::cout << "hit_bend3 " << bend3 << std::endl;
std::cout << "hit_bend4 " << bend4 << std::endl;
}
// CCLUT bit corrections
int strip_quart_bit1 = (i1CSC >= 0 ? I("hit_strip_quart_bit",i1CSC ) : -99);
int strip_quart_bit2 = (i2 >= 0 ? I("hit_strip_quart_bit", i2 ) : -99);
int strip_quart_bit3 = (i3 >= 0 ? I("hit_strip_quart_bit", i3 ) : -99);
int strip_quart_bit4 = (i4 >= 0 ? I("hit_strip_quart_bit", i4 ) : -99);
int strip_eight_bit1 = (i1CSC >= 0 ? I("hit_strip_eighth_bit",i1CSC ) : -99);
int strip_eight_bit2 = (i2 >= 0 ? I("hit_strip_eighth_bit", i2 ) : -99);
int strip_eight_bit3 = (i3 >= 0 ? I("hit_strip_eighth_bit", i3 ) : -99);
int strip_eight_bit4 = (i4 >= 0 ? I("hit_strip_eighth_bit", i4 ) : -99);
int st1_ring2 = (i1CSC >= 0 ? ( I("hit_ring",i1CSC ) == 2 || I("hit_ring",i1CSC ) == 3 ) : 0);
//===========
//track info: need to use offline CSC segments as well?
//===========
double eta;
double phi;
int endcap;
if (i2 >= 0) { eta = F("hit_eta", i2 ); phi = F("hit_phi", i2 ); }
else if (i3 >= 0) { eta = F("hit_eta", i3 ); phi = F("hit_phi", i3 ); }
else if (i4 >= 0) { eta = F("hit_eta", i4 ); phi = F("hit_phi", i4 ); }
else if (i1CSC >= 0) { eta = F("hit_eta",i1CSC ); phi = F("hit_phi",i1CSC ); }
endcap = (eta > 0 ? +1 : -1);
//if ( abs(F("hit_eta", i1CSC))>1.6 && abs(F("hit_eta", i1CSC))<2.1 ) { std::cout << "i1GEM: " << i1GEM << std::endl; }
//This block of code adds a correction to the integer phi value based on the quarter and eight-strip position offset.
if (ph1 != -99) CalcPhiRun3(ph1, ring1, strip_quart_bit1, strip_eight_bit1, 1, endcap,
useOneQuartPrecision, useOneEighthPrecision);
if (ph2 != -99) CalcPhiRun3(ph2, ring2, strip_quart_bit2, strip_eight_bit2, 2, endcap,
useOneQuartPrecision, useOneEighthPrecision);
if (ph3 != -99) CalcPhiRun3(ph3, ring3, strip_quart_bit3, strip_eight_bit3, 3, endcap,
useOneQuartPrecision, useOneEighthPrecision);
if (ph4 != -99) CalcPhiRun3(ph4, ring4, strip_quart_bit4, strip_eight_bit4, 4, endcap,
useOneQuartPrecision, useOneEighthPrecision);
//========================
//Variables to go into BDT
//========================
int theta;
int dPh12, dPh13, dPh14, dPh23, dPh24, dPh34, dPhSign;
int dPhSum4, dPhSum4A, dPhSum3, dPhSum3A, outStPh;
int dTh12, dTh13, dTh14, dTh23, dTh24, dTh34;
int dSlope12, dSlope13, dSlope14, dSlope23, dSlope24, dSlope34;
int dSlopeSum4, dSlopeSum4A, dSlopeSum3, dSlopeSum3A, outStSlope;
int Ph1Slope12MinusPh2, Ph2Slope23MinusPh3, Ph3Slope34MinusPh4;
int FR1, FR2, FR3, FR4;
//uncommented on 19/1/2021 int bend1, bend2, bend3, bend4;
int RPC1, RPC2, RPC3, RPC4;
int CSC1, CSC2, CSC3, CSC4;
int dPhGE11ME11;
int GE11;
// Extra variables for FR computation
int cham1, cham2, cham3, cham4;
if (emtfMode == 0) {
theta = emtf_eta_int;
goto EMTF_ONLY;
}
// GEM does not enter the theta calculation
theta = CalcTrackTheta( th1, th2, th3, th4, st1_ring2, mode, useBitCompression );
CalcDeltaPhis(dPh12,dPh13,dPh14,dPh23,dPh24,dPh34,dPhSign,
dPhSum4,dPhSum4A,dPhSum3,dPhSum3A,outStPh,
ph1, ph2, ph3, ph4, mode, useBitCompression );
// special case with GEMs
if (useGEM) {
CalcDeltaPhisGEM( dPh12, dPh13, dPh14, dPh23, dPh24, dPh34, dPhSign,
dPhSum4, dPhSum4A, dPhSum3, dPhSum3A, outStPh, dPhGE11ME11,
ph1, ph2, ph3, ph4, ph1GEM, mode, useBitCompression );
}
//Avoid too large dPhis due to neighbouring chamber effects.
if ( abs(dPh12) > 1000 || abs(dPh13) > 1000 || abs(dPh14) > 1000 ||
abs(dPh23) > 1000 || abs(dPh24) > 1000 || abs(dPh34) > 1000 )
{
std::cout << "Not CSC-only track"<< std::endl;
continue;
}
CalcDeltaThetas( dTh12, dTh13, dTh14, dTh23, dTh24, dTh34,
th1, th2, th3, th4, mode, useBitCompression );
// In firmware, RPC 'FR' bit set according to FR of corresponding CSC chamber
cham1 = (i1CSC >= 0 ? I("hit_chamber",i1CSC ) : -99);
cham2 = (i2 >= 0 ? I("hit_chamber", i2 ) : -99);
cham3 = (i3 >= 0 ? I("hit_chamber", i3 ) : -99);
cham4 = (i4 >= 0 ? I("hit_chamber", i4 ) : -99);
FR1 = (i1CSC >= 0 ? (cham1 % 2 == 0) : -99); // Odd chambers are bolted to the iron,
FR2 = (i2 >= 0 ? (cham2 % 2 == 0) : -99); // which faces forwared in stations 1 & 2,
FR3 = (i3 >= 0 ? (cham3 % 2 == 1) : -99); // backwards in 3 & 4
FR4 = (i4 >= 0 ? (cham4 % 2 == 1) : -99);
if (ring1 == 3) FR1 = 0; // In ME1/3 chambers are non-overlapping
// calculate bendings from CCLUT slope (Run-3)
// this needs to be evaluated before the CalcBends
// this function does not modify bendX
if(verbose) {
std::cout << "Before" << std::endl;
std::cout << "hit_slope1 " << slope1 << std::endl;
std::cout << "hit_slope2 " << slope2 << std::endl;
std::cout << "hit_slope3 " << slope3 << std::endl;
std::cout << "hit_slope4 " << slope4 << std::endl;
}
CalcSlopes(bend1, slope1, endcap, mode, useBitCompression, isRun2 );
CalcSlopes(bend2, slope2, endcap, mode, useBitCompression, isRun2 );
CalcSlopes(bend3, slope3, endcap, mode, useBitCompression, isRun2 );
CalcSlopes(bend4, slope4, endcap, mode, useBitCompression, isRun2 );
if(verbose) {
std::cout << "After" << std::endl;
std::cout << "hit_slope1 " << slope1 << std::endl;
std::cout << "hit_slope2 " << slope2 << std::endl;
std::cout << "hit_slope3 " << slope3 << std::endl;
std::cout << "hit_slope4 " << slope4 << std::endl;
}
CalcDeltaSlopes(slope1, slope2, slope3, slope4,
dSlope12, dSlope13, dSlope14,
dSlope23, dSlope24, dSlope34,
dSlopeSum4, dSlopeSum4A,
dSlopeSum3, dSlopeSum3A,
outStSlope);
if(verbose) {
std::cout << "DSlope" << std::endl;
std::cout << "dSlope12 " << dSlope12 << std::endl;
std::cout << "dSlope13 " << dSlope13 << std::endl;
std::cout << "dSlope14 " << dSlope14 << std::endl;
std::cout << "dSlope23 " << dSlope23 << std::endl;
std::cout << "dSlope24 " << dSlope24 << std::endl;
std::cout << "dSlope34 " << dSlope34 << std::endl;
}
// convert the 5-bit slope to run-2 pattern
if (!isRun2) {
ConvertSlopeToRun2Pattern(slope1, slope2, slope3, slope4,
pat1, pat2, pat3, pat4);
}
// calculate bendings from pattern numbers (Run-2, Run-3)
// this function modifies bendX
CalcBends(bend1, bend2, bend3, bend4,
slope1, slope2, slope3, slope4,
pat1, pat2, pat3, pat4,
pat1_run3, pat2_run3, pat3_run3, pat4_run3,
dPhSign, endcap, mode, BIT_COMP, isRun2 );
if(verbose)
{
std::cout << "(Before assignment) RPC1: " << RPC1 << ", RPC2: " << RPC2 << ", RPC3: " << RPC3 << ", RPC4: " << RPC4 << std::endl;
}
// Check for additional hits
RPC1 = (i1CSC >= 0 ? ( I("hit_isRPC",i1CSC ) == 1 ? 1 : 0) : -99);
CSC1 = (i1CSC >= 0 ? ( I("hit_isCSC",i1CSC ) == 1 ? 1 : 0) : -99);
RPC2 = (i2 >= 0 ? ( I("hit_isRPC", i2 ) == 1 ? 1 : 0) : -99);
CSC2 = (i2 >= 0 ? ( I("hit_isCSC", i2 ) == 1 ? 1 : 0) : -99);
RPC3 = (i3 >= 0 ? ( I("hit_isRPC", i3 ) == 1 ? 1 : 0) : -99);
CSC3 = (i3 >= 0 ? ( I("hit_isCSC", i3 ) == 1 ? 1 : 0) : -99);
RPC4 = (i4 >= 0 ? ( I("hit_isRPC", i4 ) == 1 ? 1 : 0) : -99);
CSC4 = (i4 >= 0 ? ( I("hit_isCSC", i4 ) == 1 ? 1 : 0) : -99);
if(verbose)
{
std::cout << "(After assignment) RPC1: " << RPC1 << ", RPC2: " << RPC2 << ", RPC3: " << RPC3 << ", RPC4: " << RPC4 << std::endl;
}
GE11 = (i1GEM >= 0 ? ( I("hit_isGEM",i1GEM ) == 1 ? 1 : 0) : -99);
CalcRPCs( RPC1, RPC2, RPC3, RPC4, mode, st1_ring2, theta, useBitCompression );
EMTF_ONLY: // Skip track building, just store EMTF info
/////////////////////////////////////////////////////