-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPtClassification_AWB_v1.C
992 lines (829 loc) · 47.1 KB
/
PtClassification_AWB_v1.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
/////////////////////////////////////////////////////////////////////////
/// Simulataneous pT classification for multiple factories and MVAs ///
/// Andrew Brinkerhoff 23.01.17 ///
/// ///
/// Adapted from ROOT TMVAClassification.C ///
/// Run using "root -l PtClassification_AWB_v1.C ///
/////////////////////////////////////////////////////////////////////////
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#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/TMVAGui.h"
// Extra tools - AWB 07.12.16
#include "interface/MVA_helper.h"
#include "src/MVA_input_var_tools.cc"
// // Class with NTuple branch definitions
// #include "interface/PtLutInputBranchesClass.hh"
const int MAX_EVT = 5000000; // Maximum number of MuGun events to process
const int MAX_TR = 500000; // Maximum number of MuGun training events
const int REPORT_EVT = 100000;
// const int MAX_EVT = 100000;
// const int MAX_TR = 50000;
// const int REPORT_EVT = 1000;
const double PI = 3.14159265359;
const double PT_SCALE = 1.;
// const double PT_SCALE = (1. / 1.25); // EMTF pT was scaled up by ~1.25 in 2016 (for 4-hit tracks, mode 15)
const double BIT = 0.000001; // Tiny value or offset
const double PTMIN = 1.; // Minimum GEN pT
const double PTMAX = 1000.; // Maximum GEN pT
const double ETAMIN = 1.0; // Minimum GEN |eta|
const double ETAMAX = 2.5; // Maximum GEN |eta|
const std::vector<int> MODES = {15};
using namespace TMVA;
void PtClassification_AWB_v1 ( TString myMethodList = "" ) {
// This loads the library
TMVA::Tools::Instance();
// Default MVA methods to be trained + tested
std::map<std::string,int> Use;
// Mutidimensional likelihood and Nearest-Neighbour methods
Use["PDERS"] = 0;
Use["PDEFoam"] = 0;
Use["KNN"] = 0;
//
// Linear Discriminant Analysis
Use["LD"] = 0;
//
// Function Discriminant analysis
Use["FDA_GA"] = 0;
Use["FDA_MC"] = 0;
Use["FDA_MT"] = 0;
Use["FDA_GAMT"] = 0;
//
// Neural Network
Use["MLP"] = 0;
Use["DNN"] = 0;
//
// Support Vector Machine
Use["SVM"] = 0;
//
// Boosted Decision Trees
Use["BDT"] = 0;
Use["BDTG_default"] = 0;
Use["BDTG_AWB"] = 1;
Use["BDTG_AWB_lite"] = 0;
Use["BDTG_AWB_50_trees"] = 0;
Use["BDTG_AWB_100_trees"] = 0;
Use["BDTG_AWB_200_trees"] = 0;
Use["BDTG_AWB_400_trees"] = 0;
Use["BDTG_AWB_800_trees"] = 0;
Use["BDTG_AWB_3_deep"] = 0;
Use["BDTG_AWB_4_deep"] = 0;
Use["BDTG_AWB_5_deep"] = 0;
Use["BDTG_AWB_6_deep"] = 0;
Use["BDTG_Carnes"] = 0;
// ---------------------------------------------------------------
std::cout << std::endl;
std::cout << "==> Start PtClassification_AWB_v1" << 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;
}
}
// --------------------------------------------------------------------------------------------------
// Here the preparation phase begins
// Create a new root output file
TString out_dir = "/afs/cern.ch/work/a/abrinke1/public/EMTF/PtAssign2017";
// out_dir = ".";
TString out_file_name;
out_file_name.Form( "%s/PtClassification_AWB_v1_17_02_08_mode_15_hiPt_16_64.root", out_dir.Data() );
TFile* out_file = TFile::Open( out_file_name, "RECREATE" );
// Read training and test data (see TMVAClassification for reading ASCII files)
// load the signal and background event samples from ROOT trees
TFile *input(0);
TString store = "root://eoscms.cern.ch//store/user/abrinke1/EMTF/Emulator/ntuples";
std::vector<TString> in_file_names;
TString in_file_name;
TString in_dirs[4] = { "ZeroBiasIsolatedBunch0/Slim/170130_224405/0000",
"ZeroBiasIsolatedBunch1/Slim/170130_175144/0000",
"ZeroBiasIsolatedBunch4/Slim/170130_175005/0000",
"ZeroBiasIsolatedBunch5/Slim/170130_174947/0000" };
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 50; j++) {
in_file_name.Form("%s/%s/tuple_%d.root", store.Data(), in_dirs[i].Data(), j);
std::cout << "Adding file " << in_file_name.Data() << std::endl;
in_file_names.push_back(in_file_name.Data());
}
}
TString in_dir = "SingleMu_Pt1To1000_FlatRandomOneOverPt/EMTF_MuGun/170113_165434/0000";
for (int i = 1; i < 99; i++) {
in_file_name.Form("%s/%s/EMTF_MC_NTuple_SingleMu_noRPC_%d.root", store.Data(), in_dir.Data(), i);
std::cout << "Adding file " << in_file_name.Data() << std::endl;
in_file_names.push_back(in_file_name.Data());
if (i*100000 > MAX_EVT) break; // ~100k events per file
}
for (UInt_t i = 0; i < in_file_names.size(); i++) {
if ( !gSystem->AccessPathName(in_file_names.at(i)) )
input = TFile::Open( in_file_names.at(i) ); // check if file in local directory exists
if (!input) {
std::cout << "ERROR: could not open data file " << in_file_names.at(i) << std::endl;
in_file_names.erase( in_file_names.begin()+i );
i -= 1;
// exit(1);
}
}
// Add trees from the input files to the TChain
// Have to use TChain for both SetBranchAddress and GetEntry to work
std::vector<TChain*> in_chains;
// Super-hacky ... but using "GetBranch" with a single chain with multiple files causes a segfault - AWB 19.01.16
for (UInt_t i = 0; i < in_file_names.size(); i++) {
TChain *tmp_chain = new TChain("ntuple/tree");
tmp_chain->Add( in_file_names.at(i) );
in_chains.push_back(tmp_chain);
}
//////////////////////////////////////////////////////////////////
/// Factories: Use different sets of variables, weights, etc. ///
//////////////////////////////////////////////////////////////////
TString fact_set = "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification";
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>, int> > factories;
// Optimized mode 15 - AWB 26.01.17
factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPt", // dPhi12, 23, 34, theta, combs, FR1, St1 ring
var_names, var_vals, 0x001f01ff) );
factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPtSig", // dPhi12, 23, 34, theta, combs, FR1, St1 ring
var_names, var_vals, 0x001f01ff) );
factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPtSq", // dPhi12, 23, 34, theta, combs, FR1, St1 ring
var_names, var_vals, 0x001f01ff) );
factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPtSqSig", // dPhi12, 23, 34, theta, combs, FR1, St1 ring
var_names, var_vals, 0x001f01ff) );
factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPtCub", // dPhi12, 23, 34, theta, combs, FR1, St1 ring
var_names, var_vals, 0x001f01ff) );
factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPtCubSig", // dPhi12, 23, 34, theta, combs, FR1, St1 ring
var_names, var_vals, 0x001f01ff) );
factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPtQrt", // dPhi12, 23, 34, theta, combs, FR1, St1 ring
var_names, var_vals, 0x001f01ff) );
factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPtQrtSig", // dPhi12, 23, 34, theta, combs, FR1, St1 ring
var_names, var_vals, 0x001f01ff) );
// // Original set of training variables
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x0000011d",
// var_names, var_vals, 0x0000011d) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x0000011d_invPt",
// var_names, var_vals, 0x0000011d) );
// // Original set of training variables + combinations
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01fd",
// var_names, var_vals, 0x001f01fd) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01fd_invPt",
// var_names, var_vals, 0x001f01fd) );
// // Original set of training variables + combinations + St 1 ring, FR bits, bending
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001fffff",
// var_names, var_vals, 0x001fffff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001fffff_invPt",
// var_names, var_vals, 0x001fffff) );
// // Various sets of variables
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x00000004_invPt", // dPhi12
// var_names, var_vals, 0x00000004) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x00000005_invPt", // dPhi12, theta
// var_names, var_vals, 0x00000005) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x0000000d_invPt", // dPhi12, 23, theta
// var_names, var_vals, 0x0000000d) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x00000085_invPt", // dPhi12, 24, theta
// var_names, var_vals, 0x00000085) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x0000001d_invPt", // dPhi12, 23, 34, theta
// var_names, var_vals, 0x0000001d) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f00fd_invPt", // dPhi12, 23, 34, theta, combs
// var_names, var_vals, 0x001f00fd) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f0ffd_invPt", // dPhi12, 23, 34, theta, combs, FRs
// var_names, var_vals, 0x001f0ffd) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f0fff_invPt", // dPhi12, 23, 34, theta, combs, FRs, St1 ring
// var_names, var_vals, 0x001f0fff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001fffff_invPt", // dPhi12, 23, 34, theta, combs, FRs, St1 ring, bends
// var_names, var_vals, 0x001fffff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x8fff0fff_invPt", // dPhi12, 23, 34, theta, combs, FRs, St1 ring, dThetas
// var_names, var_vals, 0x8fff0fff) );
// // FRs
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f00ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring
// var_names, var_vals, 0x001f00ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1
// var_names, var_vals, 0x001f01ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f03ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1/2
// var_names, var_vals, 0x001f03ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f05ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1/3
// var_names, var_vals, 0x001f05ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f09ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1/4
// var_names, var_vals, 0x001f09ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f0fff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, all FRs
// var_names, var_vals, 0x001f0fff) );
// // Bends
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f01ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1
// var_names, var_vals, 0x001f01ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f11ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1, bend 1
// var_names, var_vals, 0x001f11ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f31ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1, bend 1/2
// var_names, var_vals, 0x001f31ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f51ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1, bend 1/3
// var_names, var_vals, 0x001f51ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001f91ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1, bend 1/3
// var_names, var_vals, 0x001f91ff) );
// factories.push_back( std::make_tuple( nullF, nullL, "f_0x001ff1ff_invPt", // dPhi12, 23, 34, theta, combs, St1 ring, FR1, all bends
// var_names, var_vals, 0x001ff1ff) );
// Initialize factories and dataloaders
for (UInt_t 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
// MVA_var(TString name, TString descr, TString unit, TString type, Double_t def_val)
std::vector<MVA_var> in_vars; // All input variables
std::vector<MVA_var> spec_vars; // All spectator variables
std::vector<MVA_var> all_vars; // All variables
/////////////////////////////////////////////////////////
/// Input variables: used in BDT to estimate the pT ///
/////////////////////////////////////////////////////////
in_vars.push_back( MVA_var( "theta", "Track #theta", "int", 'I', -88 ) ); // 0x0000 0001 * 2016 variable
in_vars.push_back( MVA_var( "St1_ring", "St 1 LCT ring", "int", 'I', -88 ) ); // 0x0000 0002
in_vars.push_back( MVA_var( "dPhi_12", "#phi(2) - #phi(1)", "int", 'I', -88 ) ); // 0x0000 0004 * 2016 variable
in_vars.push_back( MVA_var( "dPhi_23", "#phi(3) - #phi(2)", "int", 'I', -88 ) ); // 0x0000 0008 * 2016 variable
in_vars.push_back( MVA_var( "dPhi_34", "#phi(4) - #phi(3)", "int", 'I', -88 ) ); // 0x0000 0010 * 2016 variable
in_vars.push_back( MVA_var( "dPhi_13", "#phi(3) - #phi(1)", "int", 'I', -88 ) ); // 0x0000 0020 * Derivable from 2016 var
in_vars.push_back( MVA_var( "dPhi_14", "#phi(4) - #phi(1)", "int", 'I', -88 ) ); // 0x0000 0040 * Derivable from 2016 var
in_vars.push_back( MVA_var( "dPhi_24", "#phi(4) - #phi(2)", "int", 'I', -88 ) ); // 0x0000 0080 * Derivable from 2016 var
in_vars.push_back( MVA_var( "FR_1", "St 1 LCT F/R", "int", 'I', -88 ) ); // 0x0000 0100 * 2016 variable
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
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
in_vars.push_back( MVA_var( "dPhiSum4", "#Sigmad#phi (6)", "int", 'I', -88 ) ); // 0x0001 0000 * Derivable from 2016 var
in_vars.push_back( MVA_var( "dPhiSum4A", "#Sigma|d#phi| (6)", "int", 'I', -88 ) ); // 0x0002 0000 * Derivable from 2016 var
in_vars.push_back( MVA_var( "dPhiSum3", "#Sigmad#phi (3)", "int", 'I', -88 ) ); // 0x0004 0000 * Derivable from 2016 var
in_vars.push_back( MVA_var( "dPhiSum3A", "#Sigma|d#phi| (3)", "int", 'I', -88 ) ); // 0x0008 0000 * Derivable from 2016 var
in_vars.push_back( MVA_var( "outStPhi", "#phi outlier St", "int", 'I', -88 ) ); // 0x0010 0000 * Derivable from 2016 var
in_vars.push_back( MVA_var( "outStTh", "#theta outlier St", "int", 'I', -88 ) ); // 0x0020 0000
in_vars.push_back( MVA_var( "dThMax4", "Max d#theta (6)", "int", 'I', -88 ) ); // 0x0040 0000
in_vars.push_back( MVA_var( "dThMax3", "Max d#theta (3)", "int", 'I', -88 ) ); // 0x0080 0000
in_vars.push_back( MVA_var( "dThSum4", "#Sigmad#theta (6)", "int", 'I', -88 ) ); // 0x0100 0000
in_vars.push_back( MVA_var( "dThSum4A", "#Sigma|d#theta| (6)", "int", 'I', -88 ) ); // 0x0200 0000
in_vars.push_back( MVA_var( "dThSum3", "#Sigmad#theta (3)", "int", 'I', -88 ) ); // 0x0400 0000
in_vars.push_back( MVA_var( "dThSum3A", "#Sigma|d#theta| (3)", "int", 'I', -88 ) ); // 0x0800 0000
in_vars.push_back( MVA_var( "dTh_12", "#theta(2) - #theta(1)", "int", 'I', -88 ) ); // 0x1000 0000
in_vars.push_back( MVA_var( "dTh_23", "#theta(3) - #theta(2)", "int", 'I', -88 ) ); // 0x2000 0000
in_vars.push_back( MVA_var( "dTh_34", "#theta(4) - #theta(3)", "int", 'I', -88 ) ); // 0x3000 0000
/////////////////////////////////////////////////////////////////////////////
/// 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 ) ); // 0x01
spec_vars.push_back( MVA_var( "EMTF_pt", "EMTF p_{T}", "GeV", 'F', -77 ) ); // 0x02
spec_vars.push_back( MVA_var( "inv_GEN_pt", "1 / GEN muon p_{T}", "GeV^{-1}", 'F', -77 ) ); // 0x04
spec_vars.push_back( MVA_var( "inv_EMTF_pt", "1 / EMTF p_{T}", "GeV^{-1}", 'F', -77 ) ); // 0x08
spec_vars.push_back( MVA_var( "log2_GEN_pt", "log_{2}(GEN muon p_{T})", "GeV", 'F', -77 ) ); // 0x10
spec_vars.push_back( MVA_var( "log2_EMTF_pt", "log_{2}(EMTF p_{T})", "GeV", 'F', -77 ) ); // 0x20
spec_vars.push_back( MVA_var( "GEN_eta", "GEN #eta", "", 'F', -77 ) ); // 0x40
spec_vars.push_back( MVA_var( "EMTF_eta", "EMTF #eta", "", 'F', -77 ) ); // 0x80
spec_vars.push_back( MVA_var( "GEN_charge", "GEN charge", "", 'I', -77 ) ); // 0x40
spec_vars.push_back( MVA_var( "EMTF_charge", "EMTF charge", "", 'I', -77 ) ); // 0x80
spec_vars.push_back( MVA_var( "dPhi_12_sign", "#phi(2) - #phi(1) sign", "", 'I', -77 ) ); // 0x80
assert( in_vars.size() > 0 ); // You need at least one input variable
// Order is important: input variables first, then specator
all_vars.insert( all_vars.end(), in_vars.begin(), in_vars.end() );
all_vars.insert( all_vars.end(), spec_vars.begin(), spec_vars.end() );
// Fill each factory with the correct set of variables
for (UInt_t iFact = 0; iFact < factories.size(); iFact++) {
std::cout << "\n*** Factory " << std::get<2>(factories.at(iFact)) << " variables ***" << std::endl;
std::cout << "*** Input ***" << std::endl;
for (UInt_t i = 0; i < in_vars.size(); i++) {
if ( 0x1 & (std::get<5>(factories.at(iFact)) >> i) ) { // Hex bit mask for in_vars
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
}
}
std::cout << "*** Spectator ***" << std::endl;
for (UInt_t i = 0; i < spec_vars.size(); i++) {
MVA_var v = spec_vars.at(i);
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++)
std::cout << "\n******* About to loop over chains *******" << std::endl;
UInt_t iEvt = 0;
UInt_t iEvtZB = 0;
UInt_t nTrain_sig = 0;
UInt_t nTrain_bkg = 0;
UInt_t nTest_sig = 0;
UInt_t nTest_bkg = 0;
for (int iCh = 0; iCh < in_chains.size(); iCh++) {
TChain *in_chain = in_chains.at(iCh);
// Get branches from the chain
TBranch *muon_br = in_chain->GetBranch("muon");
TBranch *hit_br = in_chain->GetBranch("hit");
TBranch *track_br = in_chain->GetBranch("track");
std::cout << "\n******* About to enter the event loop for chain " << iCh+1 << " *******" << std::endl;
for (UInt_t jEvt = 0; jEvt < in_chain->GetEntries(); jEvt++) {
if (iEvt > MAX_EVT) break;
// if ( (iEvt % REPORT_EVT) == 0 )
// std::cout << "Looking at event " << iEvt << std::endl;
in_chain->GetEntry(jEvt);
UInt_t nMuons = (muon_br->GetLeaf("nMuons"))->GetValue();
UInt_t nTracks = (track_br->GetLeaf("nTracks"))->GetValue();
Bool_t isMC = (nMuons > 0);
if (not isMC) // Process ZeroBias anyway
nMuons = nTracks;
// std::cout << "There are " << nMuons << " GEN muons and " << nTracks << " EMTF tracks\n" << std::endl;
if ( ( (iEvt % REPORT_EVT) == 0 && isMC) || (iEvtZB > 0 && (iEvtZB % 100000) == 0) )
std::cout << "Looking at event " << iEvt << " (" << iEvtZB << ")" << std::endl;
for (UInt_t iMu = 0; iMu < nMuons; iMu++) {
Double_t mu_pt ;
Double_t mu_eta;
Double_t mu_phi;
Int_t mu_charge;
if (isMC) {
mu_pt = (muon_br->GetLeaf("pt"))->GetValue(iMu);
mu_eta = (muon_br->GetLeaf("eta"))->GetValue(iMu);
mu_phi = (muon_br->GetLeaf("phi"))->GetValue(iMu);
mu_charge = (muon_br->GetLeaf("charge"))->GetValue(iMu);
} else {
mu_pt = -99.;
mu_eta = -99.;
mu_phi = -99.;
mu_charge = -99;
}
if ( mu_pt < PTMIN && isMC ) continue;
if ( mu_pt > PTMAX && isMC ) continue;
if ( fabs( mu_eta ) < ETAMIN && isMC ) continue;
if ( fabs( mu_eta ) > ETAMAX && isMC ) continue;
// std::cout << "Muon " << iMu+1 << " has pt = " << mu_pt << ", eta = " << mu_eta << ", phi = " << mu_phi << std::endl;
for (UInt_t iTrk = 0; iTrk < nTracks; iTrk++) {
Double_t trk_pt = (track_br->GetLeaf("pt"))->GetValue(iTrk);
Double_t trk_eta = (track_br->GetLeaf("eta"))->GetValue(iTrk);
trk_pt *= PT_SCALE;
Int_t trk_theta = (track_br->GetLeaf("theta_int"))->GetValue(iTrk);
Double_t trk_phi = (track_br->GetLeaf("phi"))->GetValue(iTrk);
Int_t trk_charge = (track_br->GetLeaf("charge"))->GetValue(iTrk);
Int_t trk_mode = (track_br->GetLeaf("mode"))->GetValue(iTrk);
if ( ( mu_eta > 0 ) != ( trk_eta > 0 ) && isMC ) continue;
Bool_t goodMode = false;
for (UInt_t iMode = 0; iMode < MODES.size(); iMode++)
if (trk_mode == MODES.at(iMode))
goodMode = true;
if (not goodMode) continue;
// std::cout << " * Track " << iTrk+1 << " has pt = " << trk_pt << ", eta = " << trk_eta
// << ", phi = " << mu_phi << ", mode = " << trk_mode << std::endl;
Int_t st1_ring = ((int) (track_br->GetLeaf("hit_ring"))->GetValue(4*iTrk + 0)) % 3; // Ring 4 --> Ring 1 (ME1/1a)
Int_t phi1 = (track_br->GetLeaf("hit_phi_int"))->GetValue(4*iTrk + 0);
Int_t phi2 = (track_br->GetLeaf("hit_phi_int"))->GetValue(4*iTrk + 1);
Int_t phi3 = (track_br->GetLeaf("hit_phi_int"))->GetValue(4*iTrk + 2);
Int_t phi4 = (track_br->GetLeaf("hit_phi_int"))->GetValue(4*iTrk + 3);
// std::cout << " * Hits have phi = " << phi1 << ", " << phi2 << ", " << phi3 << ", " << phi4 << std::endl;
Int_t th1 = (track_br->GetLeaf("hit_theta_int"))->GetValue(4*iTrk + 0);
Int_t th2 = (track_br->GetLeaf("hit_theta_int"))->GetValue(4*iTrk + 1);
Int_t th3 = (track_br->GetLeaf("hit_theta_int"))->GetValue(4*iTrk + 2);
Int_t th4 = (track_br->GetLeaf("hit_theta_int"))->GetValue(4*iTrk + 3);
// std::cout << " * Hits have theta = " << th1 << ", " << th2 << ", " << th3 << ", " << th4 << std::endl;
Int_t dPhi12 = phi2 - phi1;
Int_t dPhi13 = phi3 - phi1;
Int_t dPhi14 = phi4 - phi1;
Int_t dPhi23 = phi3 - phi2;
Int_t dPhi24 = phi4 - phi2;
Int_t dPhi34 = phi4 - phi3;
Int_t dTh12 = th2 - th1;
Int_t dTh13 = th3 - th1;
Int_t dTh14 = th4 - th1;
Int_t dTh23 = th3 - th2;
Int_t dTh24 = th4 - th2;
Int_t dTh34 = th4 - th3;
// Define all dPhi values relative to dPhi12
Int_t dPhi12_sign = ( (dPhi12 < 0) ? -1. : 1. );
dPhi13 *= dPhi12_sign;
dPhi14 *= dPhi12_sign;
dPhi23 *= dPhi12_sign;
dPhi24 *= dPhi12_sign;
dPhi34 *= dPhi12_sign;
dPhi12 = abs(dPhi12);
Int_t dPhi_sum_4 = dPhi12 + dPhi13 + dPhi14 + dPhi23 + dPhi24 + dPhi34;
Int_t dPhi_sum_4A = abs(dPhi12) + abs(dPhi13) + abs(dPhi14) + abs(dPhi23) + abs(dPhi24) + abs(dPhi34);
Int_t pDev_st1 = abs(dPhi12) + abs(dPhi13) + abs(dPhi14);
Int_t pDev_st2 = abs(dPhi12) + abs(dPhi23) + abs(dPhi24);
Int_t pDev_st3 = abs(dPhi13) + abs(dPhi23) + abs(dPhi34);
Int_t pDev_st4 = abs(dPhi14) + abs(dPhi24) + abs(dPhi34);
Int_t dTh_sum_4 = dTh12 + dTh13 + dTh14 + dTh23 + dTh24 + dTh34;
Int_t dTh_sum_4A = abs(dTh12) + abs(dTh13) + abs(dTh14) + abs(dTh23) + abs(dTh24) + abs(dTh34);
Int_t tDev_st1 = abs(dTh12) + abs(dTh13) + abs(dTh14);
Int_t tDev_st2 = abs(dTh12) + abs(dTh23) + abs(dTh24);
Int_t tDev_st3 = abs(dTh13) + abs(dTh23) + abs(dTh34);
Int_t tDev_st4 = abs(dTh14) + abs(dTh24) + abs(dTh34);
Int_t out_st_phi = -88;
if (pDev_st4 > pDev_st3 && pDev_st4 > pDev_st2 && pDev_st4 > pDev_st1) out_st_phi = 4;
else if (pDev_st3 > pDev_st4 && pDev_st3 > pDev_st2 && pDev_st3 > pDev_st1) out_st_phi = 3;
else if (pDev_st2 > pDev_st4 && pDev_st2 > pDev_st3 && pDev_st2 > pDev_st1) out_st_phi = 2;
else if (pDev_st1 > pDev_st4 && pDev_st1 > pDev_st3 && pDev_st1 > pDev_st2) out_st_phi = 1;
else out_st_phi = 0;
Int_t out_st_th = -88;
if (tDev_st4 > tDev_st3 && tDev_st4 > tDev_st2 && tDev_st4 > tDev_st1) out_st_th = 4;
else if (tDev_st3 > tDev_st4 && tDev_st3 > tDev_st2 && tDev_st3 > tDev_st1) out_st_th = 3;
else if (tDev_st2 > tDev_st4 && tDev_st2 > tDev_st3 && tDev_st2 > tDev_st1) out_st_th = 2;
else if (tDev_st1 > tDev_st4 && tDev_st1 > tDev_st3 && tDev_st1 > tDev_st2) out_st_th = 1;
else out_st_th = 0;
Int_t dPhi_sum_3 = -88;
Int_t dPhi_sum_3A = -88;
if (out_st_phi == 4) {
dPhi_sum_3 = dPhi12 + dPhi13 + dPhi23;
dPhi_sum_3A = abs(dPhi12) + abs(dPhi13) + abs(dPhi23);
} else if (out_st_phi == 3) {
dPhi_sum_3 = dPhi12 + dPhi14 + dPhi24;
dPhi_sum_3A = abs(dPhi12) + abs(dPhi14) + abs(dPhi24);
} else if (out_st_phi == 2) {
dPhi_sum_3 = dPhi13 + dPhi14 + dPhi34;
dPhi_sum_3A = abs(dPhi13) + abs(dPhi14) + abs(dPhi34);
} else {
dPhi_sum_3 = dPhi23 + dPhi24 + dPhi34;
dPhi_sum_3A = abs(dPhi23) + abs(dPhi24) + abs(dPhi34);
}
Int_t dTh_max_4 = MaxOfSix( dTh12, dTh13, dTh14, dTh23, dTh24, dTh34 );
Int_t dTh_max_3 = -88;
Int_t dTh_sum_3 = -88;
Int_t dTh_sum_3A = -88;
if (out_st_th == 4) {
dTh_sum_3 = dTh12 + dTh13 + dTh23;
dTh_sum_3A = abs(dTh12) + abs(dTh13) + abs(dTh23);
dTh_max_3 = MaxOfThree(dTh12, dTh13, dTh23);
} else if (out_st_th == 3) {
dTh_sum_3 = dTh12 + dTh14 + dTh24;
dTh_sum_3A = abs(dTh12) + abs(dTh14) + abs(dTh24);
dTh_max_3 = MaxOfThree(dTh12, dTh14, dTh24);
} else if (out_st_th == 2) {
dTh_sum_3 = dTh13 + dTh14 + dTh34;
dTh_sum_3A = abs(dTh13) + abs(dTh14) + abs(dTh34);
dTh_max_3 = MaxOfThree(dTh13, dTh14, dTh34);
} else {
dTh_sum_3 = dTh23 + dTh24 + dTh34;
dTh_sum_3A = abs(dTh23) + abs(dTh24) + abs(dTh34);
dTh_max_3 = MaxOfThree(dTh23, dTh24, dTh34);
}
Int_t FR1 = (track_br->GetLeaf("hit_FR"))->GetValue(4*iTrk + 0);
Int_t FR2 = (track_br->GetLeaf("hit_FR"))->GetValue(4*iTrk + 1);
Int_t FR3 = (track_br->GetLeaf("hit_FR"))->GetValue(4*iTrk + 2);
Int_t FR4 = (track_br->GetLeaf("hit_FR"))->GetValue(4*iTrk + 3);
Int_t bend1 = CalcBendFromPattern( (track_br->GetLeaf("hit_pattern"))->GetValue(4*iTrk + 0), trk_eta ) * dPhi12_sign;
Int_t bend2 = CalcBendFromPattern( (track_br->GetLeaf("hit_pattern"))->GetValue(4*iTrk + 1), trk_eta ) * dPhi12_sign;
Int_t bend3 = CalcBendFromPattern( (track_br->GetLeaf("hit_pattern"))->GetValue(4*iTrk + 2), trk_eta ) * dPhi12_sign;
Int_t bend4 = CalcBendFromPattern( (track_br->GetLeaf("hit_pattern"))->GetValue(4*iTrk + 3), trk_eta ) * dPhi12_sign;
/////////////////////////////////////////////////////
/// Loop over factories and set variable values ///
/////////////////////////////////////////////////////
for (UInt_t iFact = 0; iFact < factories.size(); iFact++) {
// Set vars equal to default vector of variables for this factory
var_names = std::get<3>(factories.at(iFact));
var_vals = std::get<4>(factories.at(iFact));
// Fill all variables
for (UInt_t iVar = 0; iVar < var_names.size(); iVar++) {
TString vName = var_names.at(iVar);
/////////////////////////
/// Input variables ///
/////////////////////////
if ( vName == "theta" )
var_vals.at(iVar) = abs(trk_theta);
if ( vName == "St1_ring" )
var_vals.at(iVar) = st1_ring;
if ( vName == "dPhi_12" )
var_vals.at(iVar) = dPhi12;
if ( vName == "dPhi_12_sign" )
var_vals.at(iVar) = dPhi12_sign;
if ( vName == "dPhi_13" )
var_vals.at(iVar) = dPhi13;
if ( vName == "dPhi_14" )
var_vals.at(iVar) = dPhi14;
if ( vName == "dPhi_23" )
var_vals.at(iVar) = dPhi23;
if ( vName == "dPhi_24" )
var_vals.at(iVar) = dPhi24;
if ( vName == "dPhi_34" )
var_vals.at(iVar) = dPhi34;
if ( vName == "FR_1" )
var_vals.at(iVar) = FR1;
if ( vName == "FR_2" )
var_vals.at(iVar) = FR2;
if ( vName == "FR_3" )
var_vals.at(iVar) = FR3;
if ( vName == "FR_4" )
var_vals.at(iVar) = FR4;
if ( vName == "bend_1" )
var_vals.at(iVar) = bend1;
if ( vName == "bend_2" )
var_vals.at(iVar) = bend2;
if ( vName == "bend_3" )
var_vals.at(iVar) = bend3;
if ( vName == "bend_4" )
var_vals.at(iVar) = bend4;
if ( vName == "dPhiSum4" )
var_vals.at(iVar) = dPhi_sum_4;
if ( vName == "dPhiSum4A" )
var_vals.at(iVar) = dPhi_sum_4A;
if ( vName == "dPhiSum3" )
var_vals.at(iVar) = dPhi_sum_3;
if ( vName == "dPhiSum3A" )
var_vals.at(iVar) = dPhi_sum_3A;
if ( vName == "outStPhi" )
var_vals.at(iVar) = out_st_phi;
if ( vName == "outStTh" )
var_vals.at(iVar) = out_st_th;
if ( vName == "dThMax4" )
var_vals.at(iVar) = dTh_max_4;
if ( vName == "dThMax3" )
var_vals.at(iVar) = dTh_max_3;
if ( vName == "dThSum4" )
var_vals.at(iVar) = dTh_sum_4;
if ( vName == "dThSum4A" )
var_vals.at(iVar) = dTh_sum_4A;
if ( vName == "dThSum3" )
var_vals.at(iVar) = dTh_sum_3;
if ( vName == "dThSum3A" )
var_vals.at(iVar) = dTh_sum_3A;
if ( vName == "dTh_12" )
var_vals.at(iVar) = dTh12;
if ( vName == "dTh_23" )
var_vals.at(iVar) = dTh23;
if ( vName == "dTh_34" )
var_vals.at(iVar) = dTh34;
/////////////////////////////
/// Spectator variables ///
/////////////////////////////
if ( vName == "GEN_pt" )
var_vals.at(iVar) = mu_pt;
if ( vName == "EMTF_pt" )
var_vals.at(iVar) = trk_pt;
if ( vName == "inv_GEN_pt" )
var_vals.at(iVar) = (isMC ? 1. / mu_pt : mu_pt);
if ( vName == "inv_EMTF_pt" )
var_vals.at(iVar) = 1. / trk_pt;
if ( vName == "log2_GEN_pt" )
var_vals.at(iVar) = (isMC ? log2(mu_pt) : mu_pt);
if ( vName == "log2_EMTF_pt" )
var_vals.at(iVar) = log2(trk_pt);
if ( vName == "GEN_eta" )
var_vals.at(iVar) = mu_eta;
if ( vName == "EMTF_eta" )
var_vals.at(iVar) = trk_eta;
if ( vName == "GEN_charge" )
var_vals.at(iVar) = mu_charge;
if ( vName == "EMTF_charge" )
var_vals.at(iVar) = trk_charge;
} // End loop: for (UInt_t iVar = 0; iVar < var_names.size(); iVar++)
// Unweighted distribution: flat in eta and 1/pT
Double_t sig_evt_weight = 1.0;
Double_t bkg_evt_weight = 1.0;
// Weight by 1/pT so overall distribution is (1/pT)^2
if ( isMC && std::get<2>(factories.at(iFact)).Contains("_invPt") )
bkg_evt_weight = 1. / mu_pt;
if ( isMC && std::get<2>(factories.at(iFact)).Contains("_invPtSig") )
sig_evt_weight = 1. / mu_pt;
if ( isMC && std::get<2>(factories.at(iFact)).Contains("_invPtSq") )
bkg_evt_weight = 1. / pow(mu_pt, 2);
if ( isMC && std::get<2>(factories.at(iFact)).Contains("_invPtSqSig") )
sig_evt_weight = 1. / mu_pt;
if ( isMC && std::get<2>(factories.at(iFact)).Contains("_invPtCub") )
bkg_evt_weight = 1. / pow(mu_pt, 3);
if ( isMC && std::get<2>(factories.at(iFact)).Contains("_invPtCubSig") )
sig_evt_weight = 1. / mu_pt;
if ( isMC && std::get<2>(factories.at(iFact)).Contains("_invPtQrt") )
bkg_evt_weight = 1. / pow(mu_pt, 4);
if ( isMC && std::get<2>(factories.at(iFact)).Contains("_invPtQrtSig") )
sig_evt_weight = 1. / mu_pt;
// Load values into event
if ( (iEvt % 2) == 0 && isMC && (nTrain_sig + nTrain_bkg) < (MAX_TR - (iFact == 0)) ) {
if (mu_pt > 0 && mu_pt < 16) {
std::get<1>(factories.at(iFact))->AddBackgroundTrainingEvent( var_vals, bkg_evt_weight );
if (iFact == 0) nTrain_bkg += 1;
} else if (mu_pt > 64) {
std::get<1>(factories.at(iFact))->AddSignalTrainingEvent( var_vals, sig_evt_weight );
if (iFact == 0) nTrain_sig += 1;
}
}
else {
if (mu_pt < 32) {
std::get<1>(factories.at(iFact))->AddBackgroundTestEvent( var_vals, bkg_evt_weight );
if (iFact == 0) nTest_bkg += 1;
} else {
std::get<1>(factories.at(iFact))->AddSignalTestEvent( var_vals, sig_evt_weight );
if (iFact == 0) nTest_sig += 1;
}
}
} // End loop: for (UInt_t iFact = 0; iFact < factories.size(); iFact++)
} // End loop: for (UInt_t iTrk = 0; iTrk < nTracks; iTrk++)
} // End loop: for (UInt_t iMu = 0; iMu < nMuons; iMu++)
if (isMC) iEvt += 1;
else iEvtZB += 1;
} // End loop: for (UInt_t jEvt = 0; jEvt < in_chain->GetEntries(); jEvt++)
} // End loop: for (int iCh = 0; iCh < in_chains.size(); iCh++) {
std::cout << "******* Made it out of the event loop *******" << std::endl;
string NTrS;
string NTrB;
ostringstream convertTrS;
convertTrS << nTrain_sig;
NTrS = convertTrS.str();
ostringstream convertTrB;
convertTrB << nTrain_bkg;
NTrB = convertTrB.str();
string numTrainStr = "nTrain_Signal="+NTrS+":nTrain_Background="+NTrB+":";
std::cout << "NTrS: " << NTrS << ", NTrB: " << NTrB << std::endl;
// // global event weights per tree (see below for setting event-wise weights)
// Double_t regWeight = 1.0;
for (UInt_t iFact = 0; iFact < factories.size(); iFact++) {
TMVA::Factory* factX = std::get<0>(factories.at(iFact));
TMVA::DataLoader* loadX = std::get<1>(factories.at(iFact));
// // You can add an arbitrary number of regression trees
// loadX->AddRegressionTree( regTree, regWeight );
// // This would set individual event weights (the variables defined in the
// // expression need to exist in the original TTree)
// loadX->SetWeightExpression( "var1", "Regression" );
loadX->SetWeightExpression( 1.0 );
// // Apply additional cuts on the signal and background samples (can be different)
// TCut mycut = "( abs(muon.eta[0]) > 1.25 && abs(muon.eta[1]) < 2.4 )"; // && track.mode[0] == 15 )";
// Tell the dataloader how to use the training and testing events
loadX->PrepareTrainingAndTestTree( "", "", numTrainStr+"SplitMode=Random:NormMode=NumEvents:!V" );
// loadX->PrepareTrainingAndTestTree( mycut, "nTrain_Regression=0:nTest_Regression=0:SplitMode=Random:NormMode=NumEvents:!V" );
// If no numbers of events are given, half of the events in the tree are used
// for training, and the other half for testing:
//
// loadX->PrepareTrainingAndTestTree( mycut, "SplitMode=random:!V" );
// Book MVA methods
//
// Please lookup the various method configuration options in the corresponding cxx files, eg:
// src/MethoCuts.cxx, etc, or here: http://tmva.sourceforge.net/optionRef.html
// it is possible to preset ranges in the option string in which the cut optimisation should be done:
// "...:CutRangeMin[2]=-1:CutRangeMax[2]=1"...", where [2] is the third input variable
// Linear discriminant
if (Use["LD"])
factX->BookMethod( loadX, TMVA::Types::kLD, "LD",
"!H:!V:VarTransform=None" );
// Neural network (MLP)
if (Use["MLP"])
factX->BookMethod( loadX, TMVA::Types::kMLP, "MLP", (string)
"!H:!V:VarTransform=Norm:NeuronType=tanh:NCycles=20000:HiddenLayers=N+20:"+
"TestRate=6:TrainingMethod=BFGS:Sampling=0.3:SamplingEpoch=0.8:"+
"ConvergenceImprove=1e-6:ConvergenceTests=15:!UseRegulator" );
if (Use["DNN"])
{
// TString layoutString ("Layout=TANH|(N+100)*2,LINEAR");
// TString layoutString ("Layout=SOFTSIGN|100,SOFTSIGN|50,SOFTSIGN|20,LINEAR");
// TString layoutString ("Layout=RELU|300,RELU|100,RELU|30,RELU|10,LINEAR");
// TString layoutString ("Layout=SOFTSIGN|50,SOFTSIGN|30,SOFTSIGN|20,SOFTSIGN|10,LINEAR");
// TString layoutString ("Layout=TANH|50,TANH|30,TANH|20,TANH|10,LINEAR");
// TString layoutString ("Layout=SOFTSIGN|50,SOFTSIGN|20,LINEAR");
// TString layoutString ("Layout=TANH|100,TANH|30,LINEAR");
TString layoutString ("Layout=TANH|100,LINEAR");
TString training0 ( (string) "LearningRate=1e-5,Momentum=0.5,Repetitions=1,"+
"ConvergenceSteps=500,BatchSize=50,TestRepetitions=7,WeightDecay=0.01,"+
"Regularization=NONE,DropConfig=0.5+0.5+0.5+0.5,DropRepetitions=2");
TString training1 ( (string) "LearningRate=1e-5,Momentum=0.9,Repetitions=1,"+
"ConvergenceSteps=170,BatchSize=30,TestRepetitions=7,WeightDecay=0.01,"+
"Regularization=L2,DropConfig=0.1+0.1+0.1,DropRepetitions=1");
TString training2 ( (string) "LearningRate=1e-5,Momentum=0.3,Repetitions=1,ConvergenceSteps=150,"+
"BatchSize=40,TestRepetitions=7,WeightDecay=0.01,Regularization=NONE");
TString training3 ( (string) "LearningRate=1e-6,Momentum=0.1,Repetitions=1,ConvergenceSteps=500,"+
"BatchSize=100,TestRepetitions=7,WeightDecay=0.0001,Regularization=NONE");
TString trainingStrategyString ("TrainingStrategy=");
trainingStrategyString += training0 + "|" + training1 + "|" + training2 + "|" + training3;
// TString trainingStrategyString ( (string) "TrainingStrategy=LearningRate=1e-1,Momentum=0.3,"+
// "Repetitions=3,ConvergenceSteps=20,BatchSize=30,TestRepetitions=7,"+
// "WeightDecay=0.0,L1=false,DropFraction=0.0,DropRepetitions=5");
TString nnOptions ("!H:V:ErrorStrategy=SUMOFSQUARES:VarTransform=G:WeightInitialization=XAVIERUNIFORM");
// TString nnOptions ("!H:V:VarTransform=Normalize:ErrorStrategy=CHECKGRADIENTS");
nnOptions.Append (":"); nnOptions.Append (layoutString);
nnOptions.Append (":"); nnOptions.Append (trainingStrategyString);
factX->BookMethod(loadX, TMVA::Types::kDNN, "DNN", nnOptions ); // NN
}
// Support Vector Machine
if (Use["SVM"])
factX->BookMethod( loadX, TMVA::Types::kSVM, "SVM", "Gamma=0.25:Tol=0.001:VarTransform=Norm" );
// Boosted Decision Trees
if (Use["BDT"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDT", (string)
"!H:!V:NTrees=100:MinNodeSize=1.0%:BoostType=AdaBoostR2:"+
"nCuts=20:PruneMethod=CostComplexity:PruneStrength=30" );
// Default TMVA settings
if (Use["BDTG_default"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_default", (string)
"!H:!V:NTrees=2000::BoostType=Grad:Shrinkage=0.1:UseBaggedBoost:"+
"BaggedSampleFraction=0.5:nCuts=20:MaxDepth=3" );
// AWB settings - AbsoluteDeviation
if (Use["BDTG_AWB"]) // Optimized settings
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB", (string)
"!H:!V:NTrees=400::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=5:MinNodeSize=0.000001" );
if (Use["BDTG_AWB_lite"]) // Fast, simple BDT
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_lite", (string)
"!H:!V:NTrees=40::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=3:MinNodeSize=0.000001" );
if (Use["BDTG_AWB_50_trees"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_50_trees", (string)
"!H:!V:NTrees=50::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=3:MinNodeSize=0.001" );
if (Use["BDTG_AWB_100_trees"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_100_trees", (string)
"!H:!V:NTrees=100::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=3:MinNodeSize=0.001" );
if (Use["BDTG_AWB_200_trees"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_200_trees", (string)
"!H:!V:NTrees=200::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=3:MinNodeSize=0.001" );
if (Use["BDTG_AWB_400_trees"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_400_trees", (string)
"!H:!V:NTrees=400::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=3:MinNodeSize=0.001" );
if (Use["BDTG_AWB_800_trees"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_800_trees", (string)
"!H:!V:NTrees=800::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=3:MinNodeSize=0.001" );
if (Use["BDTG_AWB_3_deep"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_3_deep", (string)
"!H:!V:NTrees=100::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=4:MinNodeSize=0.001" );
if (Use["BDTG_AWB_4_deep"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_4_deep", (string)
"!H:!V:NTrees=100::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=4:MinNodeSize=0.001" );
if (Use["BDTG_AWB_5_deep"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_5_deep", (string)
"!H:!V:NTrees=100::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=5:MinNodeSize=0.001" );
if (Use["BDTG_AWB_6_deep"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_AWB_6_deep", (string)
"!H:!V:NTrees=100::BoostType=Grad:Shrinkage=0.1:nCuts=1000:MaxDepth=6:MinNodeSize=0.001" );
// Factory settings from Andrew Carnes ... what do they do? - AWB 04.01.17
if (Use["BDTG_Carnes"])
factX->BookMethod( loadX, TMVA::Types::kBDT, "BDTG_Carnes", (string)
"!H:!V:NTrees=64::BoostType=Grad:Shrinkage=0.3:nCuts=99999:MaxDepth=4:MinNodeSize=0.001:"+
"NegWeightTreatment=IgnoreNegWeightsInTraining:PruneMethod=NoPruning" );
// --------------------------------------------------------------------------------------------------
// Now you can tell the factory to train, test, and evaluate the MVAs
// Train MVAs using the set of training events
factX->TrainAllMethods();
// Evaluate all MVAs using the set of test events
factX->TestAllMethods();
// Evaluate and compare performance of all configured MVAs
factX->EvaluateAllMethods();
// // Instead of "EvaluateAllMethods()", just write out the training and testing trees
// // Skip unnecessary evaluatioh histograms, which take time on large datasets
// // Code gleaned from original "EvaluateAllMethods()" function in tmva/tmva/src/Factory.cxx - AWB 31.01.17
// if ( factX->fMethodsMap.empty() )
// std::cout << "factX->fMethodsMap is empty" << std::endl;
// std::map<TString, std::vector<IMethod*>*>::iterator itrMap;
// for (itrMap = factX->fMethodsMap.begin(); itrMap != factX->fMethodsMap.end(); itrMap++) {
// std::vector<IMethod*> *methods = itrMap->second;
// std::list<TString> datasets;
// Int_t nmeth_used[2] = {int(mlist.size()), 1};
// for (Int_t k = 0; k < 2; k++) {
// for (Int_t i = 0; i < nmeth_used[k]; i++) {
// MethodBase* theMethod = dynamic_cast<MethodBase*>((*methods)[i]);
// if (theMethod == 0) {
// std::cout << "For k = " << k << ", i = " << i << ", no valid method" << std::endl;
// continue;
// }
// TDirectory* RootBaseDir = (TDirectory*) out_file;
// RootBaseDir->cd( std::get<2>(factories.at(iFact)) );
// if ( std::find( datasets.begin(), datasets.end(), std::get<2>(factories.at(iFact)) ) == datasets.end() ) {
// theMethod->Data()->GetTree(Types::kTesting)->Write( "", TObject::kOverwrite );
// theMethod->Data()->GetTree(Types::kTraining)->Write( "", TObject::kOverwrite );
// datasets.push_back( std::get<2>(factories.at(iFact)) );
// }
// } // End loop: for (Int_t i = 0; i < nmeth_used[k]; i++)
// } // End loop: for (Int_t k = 0; k < 2; k++)
// } // End loop: for (itrMap = factX->fMethodsMap.begin(); itrMap != factX->fMethodsMap.end(); itrMap++)
// --------------------------------------------------------------
} // End loop: for (UInt_t iFact = 0; iFact < factories.size(); iFact++)
// Save the output
out_file->Close();
std::cout << "==> Wrote root file: " << out_file->GetName() << std::endl;
std::cout << "==> TMVAClassification is done!" << std::endl;
// delete factory;
// delete dataloader;
// Launch the GUI for the root macros
if (!gROOT->IsBatch()) TMVA::TMVAGui( out_file_name );
}
int main( int argc, char** argv )
{
// Select methods (don't look at this code - not of interest)
TString methodList;
for (int i=1; i<argc; i++) {
TString regMethod(argv[i]);
if(regMethod=="-b" || regMethod=="--batch") continue;
if (!methodList.IsNull()) methodList += TString(",");
methodList += regMethod;
}
PtClassification_AWB_v1(methodList);
return 0;
}