-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhnlAnalysisMuons.cc
More file actions
1020 lines (963 loc) · 50.9 KB
/
hnlAnalysisMuons.cc
File metadata and controls
1020 lines (963 loc) · 50.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//include ROOT classes
#include "TLorentzVector.h"
#include "TH1D.h"
#include "TFile.h"
#include "TString.h"
#include "TLegend.h"
#include "TCanvas.h"
#include "THistPainter.h"
#include "TApplication.h"
#include "TStyle.h"
#include "TTree.h"
#include "THStack.h"
#include "TColor.h"
#include "TROOT.h"
#include "TGraph.h"
//include C++ library classes
#include <sstream>
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <limits>
using std::cout;
using std::endl;
using std::flush;
using std::ofstream;
//include code to calculate btag SF
#include "../bTag/BTagCalibrationStandalone.h"
//include other parts of the code
#include "MultilepSUSYfunc.h"
#include "tdrstyle.h"
#include "plotCode.h"
#include "trilTree.h"
#include "hnlTools.h"
void trilTree::Loop(){
//Set plotting style
setTDRStyle();
gROOT->SetBatch(kTRUE);
/*
const unsigned nSamples = 54; //50
const unsigned nSamples_eff = 28; //24
const unsigned nSig = 22; //18
const double lowMCoupling = 0.00001;
const double highMCoupling = 0.01;
const double couplingCorrection = 10000;
//~~~~~~~~ background normalizations~~~~~~~~~~~~~~~~~~~~
const double glugluToZZkFactor = 2.1;
const double WZSF = 0.652; //0.655
const double ZZSF = 1.029; //1.032
const double XgammaSF = 0.950; //0.948
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const TString fileList[nSamples] = {"data_combined_trilepton.root",
"HeavyNeutrino_M1_mu.root", "HeavyNeutrino_M2_mu.root", "HeavyNeutrino_M5_mu.root",
"../unskimmedSignal/HeavyNeutrino_trilepton_M-6_V-0.00316_mu_prompt.root",
"../unskimmedSignal/HeavyNeutrino_trilepton_M-7_V-0.00316_mu_prompt.root",
//new samples with correct displacement
"../unskimmedSignal/HeavyNeutrino_trilepton_M-5_V-0.0111_mu.root",
"../unskimmedSignal/HeavyNeutrino_M6_V0p00431_mu.root",
"HeavyNeutrino_M10_mu.root", "HeavyNeutrino_M20_mu.root", "HeavyNeutrino_M30_mu.root", "HeavyNeutrino_M40_mu.root", "HeavyNeutrino_M50_mu.root", "HeavyNeutrino_M60_mu.root", "HeavyNeutrino_M80_mu.root", "HeavyNeutrino_M100_mu.root", "HeavyNeutrino_M130_mu.root", "HeavyNeutrino_M150_mu.root", "HeavyNeutrino_M200_mu.root", "HeavyNeutrino_M400_mu.root", "HeavyNeutrino_M600_mu.root", "HeavyNeutrino_M800_mu.root", "HeavyNeutrino_M1000_mu.root",
"ZZTo4L.root", "GluGluToZZTo4mu.root", "GluGluToZZTo4e.root", "GluGluToZZTo4tau.root", "GluGluToZZTo2e2mu.root", "GluGluToZZTo2e2tau.root", "GluGluToZZTo2mu2tau.root", "VHToNonbb.root", "GluGluHToZZTo4L_M125.root", "VBF_HToZZTo4L_M125.root", "WWG.root","WWW.root", "WWZ.root", "WWTo2L2Nu_DoubleScattering.root", "WWTo2L2Nu.root", "ZZZ.root", "WZTo3LNu_mllmin01.root", "TTGJets.root","ZGTo2LG.root", "WGToLNuG.root", "TGJets.root", "TTJets_DiLept.root", "TTJets_SingleLeptFromTbar.root", "TTJets_SingleLeptFromT.root", "DYJetsToLL_M10to50.root", "DYJetsToLL_M50.root", "ttHToNonbb.root", "TTWJetsToLNu.root", "TTZToLLNuNu.root", "TTZToLL_M1to10.root", "TTTT.root"};
const double xSections[nSamples - 1] = { 2.614e-01*couplingCorrection*lowMCoupling, 2.614e-01*couplingCorrection*lowMCoupling, 2.600e-01*couplingCorrection*lowMCoupling,
2.609e-02*couplingCorrection*lowMCoupling*10, //m = 6 GeV
2.604e-02*couplingCorrection*lowMCoupling*10, //m = 7 GeV
//new corrected samples
6.675e-02*couplingCorrection*lowMCoupling*10*(0.00316*0.00316)/(0.00507*0.00507),
//4.810e-02*couplingCorrection*lowMCoupling*10*(0.00316*0.00316)/(0.00431*0.00431),
3.220e-01*couplingCorrection*lowMCoupling*10*(0.00316*0.00316)/(0.0111*0.0111),
2.550e-01*couplingCorrection*lowMCoupling, 2.366e-01*couplingCorrection*lowMCoupling, 2.076e-01*couplingCorrection*lowMCoupling, 1.664e-01*couplingCorrection*lowMCoupling, 1.177e-01*couplingCorrection*lowMCoupling, 6.675e-02*couplingCorrection*lowMCoupling, 1.237e-02*couplingCorrection*lowMCoupling*10, 5.160e-04*couplingCorrection*highMCoupling, 1.434e-04*couplingCorrection*highMCoupling, 7.809e-05*couplingCorrection*highMCoupling, 2.463e-05*couplingCorrection*highMCoupling, 1.730e-06*couplingCorrection*highMCoupling, 3.470e-07*couplingCorrection*highMCoupling, 1.020e-07*couplingCorrection*highMCoupling, 3.659e-08*couplingCorrection*highMCoupling,
1.256*ZZSF, 0.00159*glugluToZZkFactor*ZZSF, 0.00159*glugluToZZkFactor*ZZSF, 0.00159*glugluToZZkFactor*ZZSF, 0.00319*glugluToZZkFactor*ZZSF, 0.00319*glugluToZZkFactor*ZZSF, 0.00319*glugluToZZkFactor*ZZSF, 0.9561, 0.01212, 0.001034, 0.2147, 0.2086, 0.1651, 0.1729, 12.178, 0.01398, 58.59*WZSF, 3.697, 123.9*XgammaSF, 489*XgammaSF, 2.967, 87.315, 182.175, 182.175, 18610, 1921.8*3, 0.215, 0.2043, 0.2529, 0.0493, 0.009103};
const TString names[nSamples] = {"data", "m_{N} = 1 GeV", "m_{N} = 2 GeV", "m_{N} = 5 GeV",
"m_{N} = 6 GeV",
"m_{N} = 7 GeV",
//new corrected samples
"m_{N} = 5 GeV corrected",
"m_{N} = 6 GeV corrected",
"m_{N} = 10 GeV", "m_{N} = 20 GeV", "m_{N} = 30 GeV", "m_{N} = 40 GeV", "m_{N} = 50 GeV", "m_{N} = 60 GeV", "m_{N} = 80 GeV", "m_{N} = 100 GeV", "m_{N} = 130 GeV", "m_{N} = 150 GeV", "m_{N} = 200 GeV", "m_{N} = 400 GeV", "m_{N} = 600 GeV", "m_{N} = 800 GeV", "m_{N} = 1000 GeV",
"ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "triboson", "triboson", "triboson", "triboson", "triboson", "triboson", "WZ", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "TT/T + X", "TT/T + X", "TT/T + X", "TT/T + X", "TT/T + X"};
*/
const unsigned nSamples = 67;
const unsigned nSamples_eff = 33;
const unsigned nSig = 27;
const double lowMCoupling = 0.00001;
const double highMCoupling = 0.01;
const double couplingCorrection = 10000;
//~~~~~~~~ background normalizations~~~~~~~~~~~~~~~~~~~~
const double glugluToZZkFactor = 2.1;
const double WZSF = 0.652; //0.655
const double ZZSF = 1.029; //1.032
const double XgammaSF = 0.950; //0.948
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const TString fileList[nSamples] = {"data_combined_trilepton.root",
"HeavyNeutrino_M1_mu.root", //PUT THIS BACK IN LATER!!!!!!!
//temporary include of displaced sample remove later
////////////////////////////////////////////////////
//"../unskimmedSignal/HeavyNeutrino_trilepton_M-1_V-0.5_mu_displaced.root",
//////////////////////////////////////////////////////
"HeavyNeutrino_M2_mu.root",
"../unskimmedSignal/HeavyNeutrino_trilepton_M-3_V-0.0245_mu_prompt.root",
"../unskimmedSignal/HeavyNeutrino_trilepton_M-4_V-0.01_mu_prompt.root",
"HeavyNeutrino_M5_mu.root",
//Sample at correct displacement
"../unskimmedSignal/HeavyNeutrino_trilepton_M-5_V-0.0111_mu.root",
/////////////////////////////
"../unskimmedSignal/HeavyNeutrino_trilepton_M-6_V-0.00316_mu_prompt.root",
"../unskimmedSignal/HeavyNeutrino_trilepton_M-7_V-0.00316_mu_prompt.root",
"../unskimmedSignal/HeavyNeutrino_trilepton_M-8_V-0.00328876176_mu_prompt.root",
"../unskimmedSignal/HeavyNeutrino_trilepton_M-9_V-0.00244991552_mu_prompt.root",
"HeavyNeutrino_M10_mu.root",
"../unskimmedSignal/HeavyNeutrino_trilepton_M-11_V-0.00148345941_mu_prompt.root",
"../unskimmedSignal/HeavyNeutrino_trilepton_M-12_V-0.0011934501_mu_prompt.root",
"HeavyNeutrino_M20_mu.root", "HeavyNeutrino_M30_mu.root", "HeavyNeutrino_M40_mu.root", "HeavyNeutrino_M50_mu.root", "HeavyNeutrino_M60_mu.root",
"HeavyNeutrino_M80_mu.root", "HeavyNeutrino_VBF_M-80_mu.root",
"HeavyNeutrino_M100_mu.root", "HeavyNeutrino_VBF_M-100_mu.root",
"HeavyNeutrino_M130_mu.root", "HeavyNeutrino_VBF_M-130_mu.root",
"HeavyNeutrino_M150_mu.root", "HeavyNeutrino_VBF_M-150_mu.root",
"HeavyNeutrino_M200_mu.root",
"HeavyNeutrino_M400_mu.root", "HeavyNeutrino_VBF_M-400_mu.root",
"HeavyNeutrino_M600_mu.root", "HeavyNeutrino_VBF_M-600_mu.root",
"HeavyNeutrino_M800_mu.root", "HeavyNeutrino_VBF_M-800_mu.root",
"HeavyNeutrino_M1000_mu.root", "HeavyNeutrino_VBF_M-1000_mu.root",
"ZZTo4L.root", "GluGluToZZTo4mu.root", "GluGluToZZTo4e.root", "GluGluToZZTo4tau.root", "GluGluToZZTo2e2mu.root", "GluGluToZZTo2e2tau.root", "GluGluToZZTo2mu2tau.root", "VHToNonbb.root", "GluGluHToZZTo4L_M125.root", "VBF_HToZZTo4L_M125.root", "WWG.root","WWW.root", "WWZ.root", "WWTo2L2Nu_DoubleScattering.root", "WWTo2L2Nu.root", "ZZZ.root", "WZTo3LNu_mllmin01.root", "TTGJets.root","ZGTo2LG.root", "WGToLNuG.root", "TGJets.root", "TTJets_DiLept.root", "TTJets_SingleLeptFromTbar.root", "TTJets_SingleLeptFromT.root", "DYJetsToLL_M10to50.root", "DYJetsToLL_M50.root", "ttHToNonbb.root", "TTWJetsToLNu.root", "TTZToLLNuNu.root", "TTZToLL_M1to10.root", "TTTT.root"};
const double xSections[nSamples - 1] = {
2.614e-01*couplingCorrection*lowMCoupling, //put this back in later!!!
//Temporary inclusion of displaced sample, remove later
//2.614e-01*couplingCorrection*lowMCoupling,
///////////////////////////////////////////////////////
2.614e-01*couplingCorrection*lowMCoupling,
1.578*couplingCorrection*lowMCoupling*(0.01*0.01)/(0.0245*0.0245), //m = 3 GeV
2.625e-01*couplingCorrection*lowMCoupling, //4 GeV
2.600e-01*couplingCorrection*lowMCoupling, //5 GeV
//Sample at correct displacement
3.220e-01*lowMCoupling/(0.0111*0.0111),
/////////////////////////////
2.609e-02*couplingCorrection*lowMCoupling*10, //m = 6 GeV
2.604e-02*couplingCorrection*lowMCoupling*10, //m = 7 GeV
2.814e-02/(0.00328876176*0.00328876176)*lowMCoupling, //8 GeV
1.555e-02/(0.00244991552*0.00244991552)*lowMCoupling, //9 GeV
2.550e-01*couplingCorrection*lowMCoupling, //10 GeV
5.640e-03/(0.00148345941*0.00148345941)*lowMCoupling,//11 GeV
3.570e-03/(0.0011934501*0.0011934501)*lowMCoupling,//12 GeV
2.366e-01*couplingCorrection*lowMCoupling, 2.076e-01*couplingCorrection*lowMCoupling, 1.664e-01*couplingCorrection*lowMCoupling, 1.177e-01*couplingCorrection*lowMCoupling, 6.675e-02*couplingCorrection*lowMCoupling,
1.237e-02*couplingCorrection*lowMCoupling*10, 1.822e-05*couplingCorrection*lowMCoupling*10,//80 GeV
5.160e-04*couplingCorrection*highMCoupling, 2.637e-06*couplingCorrection*highMCoupling,//100 GeV
1.434e-04*couplingCorrection*highMCoupling, 1.809e-06*couplingCorrection*highMCoupling,//130 GeV
7.809e-05*couplingCorrection*highMCoupling, 1.500e-06*couplingCorrection*highMCoupling,//150 GeV
2.463e-05*couplingCorrection*highMCoupling, //200 GeV
1.730e-06*couplingCorrection*highMCoupling, 3.626e-07*couplingCorrection*highMCoupling,//400 GeV
3.470e-07*couplingCorrection*highMCoupling, 1.696e-07*couplingCorrection*highMCoupling,//600 GeV
1.020e-07*couplingCorrection*highMCoupling, 8.969e-08*couplingCorrection*highMCoupling,//800 GeV
3.659e-08*couplingCorrection*highMCoupling, 5.092e-08*couplingCorrection*highMCoupling,//1000 GeV
1.256*ZZSF, 0.00159*glugluToZZkFactor*ZZSF, 0.00159*glugluToZZkFactor*ZZSF, 0.00159*glugluToZZkFactor*ZZSF, 0.00319*glugluToZZkFactor*ZZSF, 0.00319*glugluToZZkFactor*ZZSF, 0.00319*glugluToZZkFactor*ZZSF, 0.9561, 0.01212, 0.001034, 0.2147, 0.2086, 0.1651, 0.1729, 12.178, 0.01398, 58.59*WZSF, 3.697, 123.9*XgammaSF, 489*XgammaSF, 2.967, 87.315, 182.175, 182.175, 18610, 1921.8*3, 0.215, 0.2043, 0.2529, 0.0493, 0.009103};
const TString names[nSamples] = {"data", "m_{N} = 1 GeV", "m_{N} = 2 GeV",
"m_{N} = 3 GeV",
"m_{N} = 4 GeV",
"m_{N} = 5 GeV",
//Sample at correct displacement
"m_{N} = 5 GeV corrected",
///////////////
"m_{N} = 6 GeV",
"m_{N} = 7 GeV",
"m_{N} = 8 GeV",
"m_{N} = 9 GeV",
"m_{N} = 10 GeV",
"m_{N} = 11 GeV",
"m_{N} = 12 GeV",
"m_{N} = 20 GeV", "m_{N} = 30 GeV", "m_{N} = 40 GeV", "m_{N} = 50 GeV", "m_{N} = 60 GeV", "m_{N} = 80 GeV", "m_{N} = 80 GeV", "m_{N} = 100 GeV", "m_{N} = 100 GeV", "m_{N} = 130 GeV", "m_{N} = 130 GeV", "m_{N} = 150 GeV", "m_{N} = 150 GeV", "m_{N} = 200 GeV", "m_{N} = 400 GeV", "m_{N} = 400 GeV", "m_{N} = 600 GeV", "m_{N} = 600 GeV", "m_{N} = 800 GeV", "m_{N} = 800 GeV", "m_{N} = 1000 GeV", "m_{N} = 1000 GeV",
"ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "ZZ/H", "triboson", "triboson", "triboson", "triboson", "triboson", "triboson", "WZ", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "X + #gamma", "TT/T + X", "TT/T + X", "TT/T + X", "TT/T + X", "TT/T + X"};
//Read Trees from ROOT files
TFile* hfile[nSamples];
TTree* inputTree[nSamples];
double hcounter[nSamples];
double pdfCounter[nSig][110];
for(unsigned sam = 0; sam < nSamples; ++sam){
cout << "name " << names[sam] << endl;
hfile[sam] = new TFile("../data_april17/"+fileList[sam],"read");
hfile[sam]->cd("FakeElectrons");
//Determine hcounter for cross section scaling
TH1D* _hCounter = new TH1D("hCounter", "Events counter", 5,0,5);
_hCounter->Read("hCounter");
hcounter[sam] = _hCounter->GetBinContent(1);
if(sam > 0 && sam <= nSig){
TH1D* _pdfCounter = new TH1D("pdfCounter", "Events counter", 110, 0, 110);
_pdfCounter->Read("pdfCounter");
for(unsigned pdf = 0; pdf < 110; ++pdf){
pdfCounter[sam -1][pdf] = _pdfCounter->GetBinContent(pdf + 1);
}
}
inputTree[sam] = (TTree*) (hfile[sam]->Get("FakeElectrons/fakeTree"));
Init(inputTree[sam], false, sam > 0);
}
readSF(true);
//Tweakable options////////////////////////////////////////////////////
const bool TestRun = false; //Break after a few events
const double DataLuminosity = 35.867; //units of fb^{-1}
const TString extra = ""; //for plot file names
//////////////////////////
const TString eff_names[nSamples_eff + 1] = {"data", "m_{N} = 1 GeV", "m_{N} = 2 GeV",
"m_{N} = 3 GeV",
"m_{N} = 4 GeV",
"m_{N} = 5 GeV",
"m_{N} = 5 GeV corrected",
"m_{N} = 6 GeV",
"m_{N} = 7 GeV",
"m_{N} = 8 GeV",
"m_{N} = 9 GeV",
"m_{N} = 10 GeV",
"m_{N} = 11 GeV",
"m_{N} = 12 GeV",
"m_{N} = 20 GeV", "m_{N} = 30 GeV", "m_{N} = 40 GeV", "m_{N} = 50 GeV", "m_{N} = 60 GeV", "m_{N} = 80 GeV", "m_{N} = 100 GeV", "m_{N} = 130 GeV", "m_{N} = 150 GeV", "m_{N} = 200 GeV", "m_{N} = 400 GeV", "m_{N} = 600 GeV", "m_{N} = 800 GeV", "m_{N} = 1000 GeV",
"ZZ/H", "triboson", "WZ", "X + #gamma", "TT/T + X", "non-prompt"};
const unsigned nCat = 6; //Number of categories
const TString catNames[nCat] = {"lowM_3lOSSF_lowPt", "lowM_3lnoOSSF_lowPt", "lowM_3lOSSF_highPt", "lowM_3lnoOSSF_highPt", "highM_3lOSSF", "highM_3lnoOSSF"};
const unsigned nSR[nCat] = {12, 4, 12, 4, 16, 9}; //numbers of search regions
const unsigned nUnc = 11;//number of shape uncertainties
const TString uncNames[nUnc] = {"jec", "metUnclustered", "scale", "pdf", "pu", "btagSF", "trigeff", "id_eff", "fakeEWK", "ZZmt", "scaleAcc"};
TH1D* yields[nCat][nSamples_eff + 1]; //nominal yields in every SR
TH1D* yieldsDown[nUnc][nCat][nSamples_eff + 1]; //yields varied down by shape unc
TH1D* yieldsUp[nUnc][nCat][nSamples_eff + 1]; //yields varied up by shape unc
TH1D* yieldsPdfVar[100][nCat][nSamples_eff + 1]; //yields for all 100 possible pdf variations
for(unsigned cat = 0; cat < nCat; ++cat){
for(unsigned effsam = 0; effsam < nSamples_eff + 1; ++effsam){
yields[cat][effsam] = new TH1D(catNames[cat] + eff_names[effsam], catNames[cat] + eff_names[effsam] + "; search region ; Events/search region", nSR[cat], 0.5, nSR[cat] + 0.5);
yields[cat][effsam]->Sumw2();
for(unsigned unc = 0; unc < nUnc; ++unc){
yieldsDown[unc][cat][effsam] = new TH1D(catNames[cat] + eff_names[effsam] + uncNames[unc] + "Up", catNames[cat] + eff_names[effsam] + uncNames[unc] + "Up; search region ; Events/search region", nSR[cat], 0.5, nSR[cat] + 0.5);
yieldsDown[unc][cat][effsam]->Sumw2();
yieldsUp[unc][cat][effsam] = new TH1D(catNames[cat] + eff_names[effsam] + uncNames[unc] + "Down", catNames[cat] + eff_names[effsam] + uncNames[unc] + "Down; search region ; Events/search region", nSR[cat], 0.5, nSR[cat] + 0.5);
yieldsUp[unc][cat][effsam]->Sumw2();
}
for(unsigned pdf = 0; pdf < 100; ++pdf){
yieldsPdfVar[pdf][cat][effsam] = new TH1D(catNames[cat] + eff_names[effsam] + "_pdf" + std::to_string(pdf), catNames[cat] + eff_names[effsam] + "_pdf" + std::to_string(pdf) + "; search region ; Events/search region", nSR[cat], 0.5, nSR[cat] + 0.5);
yieldsPdfVar[pdf][cat][effsam]->Sumw2();
}
}
}
Double_t scale[nSamples -1];
//set to check which data events have already been processed
std::set<std::tuple<unsigned long, unsigned long, unsigned long> > usedEvents; //runNb, lumiBlock, eventNb
//Loop over all samples
for(unsigned sam = 0, effsam = 0; sam < nSamples; ++sam, ++effsam){
if(sam != 0){
if(names[sam] == names[sam -1]) --effsam;
}
Long64_t nEntries = inputTree[sam]->GetEntries();
if(sam > 0){
scale[sam -1] = xSections[sam -1]*DataLuminosity*1000/(hcounter[sam]);
}
cout << eff_names[effsam] << endl;
std::cout<<"Entries in "<< fileList[sam] <<" "<<nEntries<<std::endl;
cout << effsam << endl;
for(Long64_t it = 0; it < nEntries; ++it){
inputTree[sam]->GetEntry(it);
if (it%10000 == 0) cout<<'.'<<flush;
if(TestRun && it > 10000) break;
double scal;
if(effsam == 0) scal = 1;
else{
scal = scale[sam-1]*_weight;
}
cutBased();
//TEMPORARY REWEIGHING OF DISPLACED SAMPLE, REMOVE LATER!!!
if(sam == 1){
scal*= (1.41978088057)/(26.815644318875442)*exp(- _ctau/26.815644318875442 + _ctau/1.41978088057);
}
//Baseline event selection
if(!baseline(true, true, false, false)) continue;
if(effsam > 0 && nBJets(true, false, 1) != 0) continue;
else if(effsam == 0 && nBJets(true, false, 0) != 0) continue;
//Check if data events were used before
if(effsam == 0){
auto event = usedEvents.find(std::make_tuple(_eventNb, _lumiBlock, _runNb));
if(event != usedEvents.end()) continue;
usedEvents.insert(std::make_tuple(_eventNb, _lumiBlock, _runNb));
}
//Categorize according to the number of leptons and flavors
unsigned* ind = new unsigned[_nL];
unsigned lCount = lepOrder(ind, 3, true, true);
if(lCount != 3) continue; //Veto 4th FO lepton considering signal model!
//Apply analysis Pt thresholds
if(!ptCuts_hnl(ind,lCount)) continue;
if(tril_flavorComb(ind, _flavors, lCount) < 2) continue; //Select only 2 or more muons!!
//MC prompt matching
if(effsam > nSig){
bool promptfail = false;
for(unsigned l = 0; l < lCount; ++l){
if(_origin[ind[l]] != 0){
promptfail = true;
break;
}
}
if(promptfail) continue;
}
//Require 3 leptons to be tight in data and MC, and determine nonPrompt bkg in data
unsigned nTight = tightCount(ind, lCount);
bool tightFail = nTight < 3;
//index used to fill events, needed to separate fakes from data
unsigned fill = effsam;
//Apply FR maps to data control region
double* conePt = new double[lCount];
for(unsigned l = 0; l < lCount; ++l){
conePt[l] = _lPt[ind[l]]*(1 + std::max(_isolation[ind[l]] - 0.1, 0.));
}
if(tightFail && (effsam == 0 || effsam > nSig)){ //&& effsam == 0){
//fakes go in different histogram
fill = nSamples_eff;
//Apply FR maps
if(effsam != 0) scal *= -1;
scal*= fakeWeight(ind, _flavors, conePt, _lEta, _istight, frMap[0], lCount);
} else if(tightFail) continue;
//Sample overlap removal
if(fileList[sam] == "WGToLNuG.root"){
bool promptfail = true;
for(unsigned l = 0; l < lCount; ++l){
if(_pdgmc[ind[l]] == 22 && _originPhot[ind[l]] == 0){
promptfail = false;
break;
}
}
if(promptfail) continue;
}
if(conePt[2] >= 10){
if(fileList[sam] == "TTJets_DiLept.root" || fileList[sam] == "DYJetsToLL_M10to50.root" || fileList[sam] == "DYJetsToLL_M50.root" || fileList[sam] == "TTJets_SingleLeptFromTbar.root" || fileList[sam] == "TTJets_SingleLeptFromT.root" ) continue;
} else if(conePt[2] < 10){
if(fileList[sam] == "TTJets_DiLept.root" || fileList[sam] == "DYJetsToLL_M10to50.root" || fileList[sam] == "DYJetsToLL_M50.root" || fileList[sam] == "TTJets_SingleLeptFromTbar.root" || fileList[sam] == "TTJets_SingleLeptFromT.root" ){
double maxMll = 0.;
for(unsigned l = 0; l < _gen_nL -1; ++l){
TLorentzVector lep1;
lep1.SetPtEtaPhiE(_gen_lPt[l], _gen_lEta[l], _gen_lPhi[l], _gen_lE[l]);
for(unsigned k = l + 1; k < _gen_nL; ++k){
TLorentzVector lep2;
lep2.SetPtEtaPhiE(_gen_lPt[k], _gen_lEta[k], _gen_lPhi[k], _gen_lE[k]);
if( (lep1 + lep2).M() > maxMll) maxMll = (lep1 + lep2).M();
}
}
if(maxMll > 30){
bool promptfail = false;
for(unsigned l = 0; l < lCount; ++l){
if(_pdgmc[ind[l]] == 22 && _originPhot[ind[l]] == 0){
promptfail = true;
break;
}
}
if(promptfail) continue;
}
}
if(fileList[sam] == "TTGJets.root" || fileList[sam] == "ZGTo2LG.root"){
bool promptfail =true;
for(unsigned l = 0; l < lCount; ++l){
if(_pdgmc[ind[l]] == 22 && _originPhot[ind[l]] == 0){
promptfail = false;
break;
}
}
if(promptfail) continue;
}
}
//Apply triggers to data events;
if(sam == 0 || effsam > nSig){
bool trigPass[4];
trigPass[0] = _lowM_trigger_eee || _lowM_trigger_all;
trigPass[1] = _lowM_trigger_mee || _lowM_trigger_all;
trigPass[2] = _lowM_trigger_mme || _lowM_trigger_all;
trigPass[3] = _lowM_trigger_mmm || _lowM_trigger_all;
if(!trigPass[tril_flavorComb(ind, _flavors, lCount)]) continue;
}
//determine search category
unsigned cat = hnl::cat(ind, _flavors, _charges, lCount, conePt[0]);
if(cat == 999){
continue;
}
//Set TLorentzVector for each lepton
TLorentzVector* lepV = new TLorentzVector[lCount];
for(unsigned l = 0; l < lCount; ++l){
lepV[l].SetPtEtaPhiE(conePt[l], _lEta[ind[l]], _lPhi[ind[l]], _lE[ind[l]]*(conePt[l]/_lPt[ind[l]]) );
}
//Calculate lepton system vector
TLorentzVector lepSyst;
for(int l = 0; l < 3; ++l) lepSyst += lepV[l];
//Apply ID and reco SF to simulation
if(effsam != 0){
scal*=getEventSF(ind, lCount, true);
}
//determine which leptons will be used for the calculation of mll
unsigned mllI[2] = {99, 99};
mllIndices(mllI, ind, lepV, _charges, _flavors, lCount);
//determine mll
double mll;
if(mllI[0] == 99){
if(mllI[1] != 99) std::cerr << "error one mll index is not -1 while the other is" << endl;
mll = -1;
} else{
TLorentzVector lzV[2];
for(unsigned l = 0; l < 2; ++l) lzV[l].SetPtEtaPhiE(PtCone(_lPt[mllI[l]]*(1 + std::max(_isolation[mllI[l]] - 0.1, 0.)), _flavors[mllI[l]], _lepMVA[mllI[l]], _ptratio[mllI[l]]), _lEta[mllI[l]], _lPhi[mllI[l]], _lE[mllI[l]]*(1 + std::max(_isolation[mllI[l]] - 0.1, 0.)) );
mll = (lzV[0] + lzV[1]).M();
}
//Category spefific selection:
//low mass general
if(cat < 4){
if(cat == 0 || cat == 2){
//Calculate minDeltaR and maxDeltaR
double minDeltaR = 9999.;
double maxDeltaR = 0.;
for(unsigned l = 0; l < lCount - 1; ++l){
for(unsigned k = l + 1; k < lCount; ++k){
if(lepV[l].DeltaR(lepV[k]) > maxDeltaR) maxDeltaR = lepV[l].DeltaR(lepV[k]);
if(lepV[l].DeltaR(lepV[k]) < minDeltaR) minDeltaR = lepV[l].DeltaR(lepV[k]);
}
}
if(minDeltaR < 0.05) continue;
if(maxDeltaR < 2) continue;
if(!vetoLowMll(5)) continue;
}
if(lepSyst.M() > 80) continue;
}
else if(cat > 3){
if(conePt[1] < 15) continue;
if(conePt[2] < 10) continue;
if(cat == 4){
if(fabs(mll - 91) < 15) continue; //Veto onZ events (WZ and DY)
if(fabs(lepSyst.M() - 91) < 15) continue; //veto conversions
}
if(!vetoLowMll(5)) continue;
}
//Calculate MET vector
TLorentzVector METvec;
METvec.SetPtEtaPhiE(_met, 0, _met_phi, _met);
//Calculate min(Mos)
double minMos = 0;
unsigned minI[2] = {99, 99};
for(unsigned l = 0; l < lCount -1 ; ++l){
for(unsigned k = l + 1; k < lCount; ++k){
if(_charges[ind[l]] != _charges[ind[k]]){
if( (lepV[l] + lepV[k]).M() < minMos || minMos == 0){
minMos = (lepV[l] + lepV[k]).M();
minI[0] = l;
minI[1] = k;
}
}
}
}
//find lepton for MT calculation
unsigned lw_min;
for(unsigned l = 0; l < lCount; ++l){
if(l != minI[0] && l != minI[1]){
lw_min = l;
}
}
//fill Nominal yields
double mt_min = transmass(lepV[lw_min], METvec);
unsigned searchR = hnl::sr(mt_min, minMos, lepSyst.M(), cat);
if(nBJets(true, false, 0) == 0 && (cat > 3 || _met < 75)){
yields[cat][fill]->Fill(searchR + 1, scal);
}
//EWK fake subtraction
if(fill == nSamples_eff && nBJets(true, false, 0) == 0 && (cat > 3 || _met < 75)){
yieldsDown[8][cat][fill]->Fill(searchR + 1, scal*(fakeWeight(ind, _flavors, conePt, _lEta, _istight, frMap[1], lCount)/fakeWeight(ind, _flavors, conePt, _lEta, _istight, frMap[0], lCount) ) );
yieldsUp[8][cat][fill]->Fill(searchR + 1, scal*(fakeWeight(ind, _flavors, conePt, _lEta, _istight, frMap[2], lCount)/fakeWeight(ind, _flavors, conePt, _lEta, _istight, frMap[0], lCount) ) );
}
//Following uncertainties do not apply to data or data-driven backgrounds
if(effsam == 0) continue;
//yields with JEC varied down
METvec.SetPtEtaPhiE(_metJECDown, 0, _met_phiJECDown, _metJECDown);
mt_min = transmass(lepV[lw_min], METvec);
searchR = hnl::sr(mt_min, minMos, lepSyst.M(), cat);
if(nBJets(true, false, 1) == 0 && (cat > 3 || _metJECDown < 75)){
yieldsDown[0][cat][fill]->Fill(searchR + 1, scal);
}
//yields with JEC varied up
METvec.SetPtEtaPhiE(_metJECUp, 0, _met_phiJECUp, _metJECUp);
mt_min = transmass(lepV[lw_min], METvec);
searchR = hnl::sr(mt_min, minMos, lepSyst.M(), cat);
if(nBJets(true, false, 2) == 0 && (cat > 3 || _metJECUp < 75)){
yieldsUp[0][cat][fill]->Fill(searchR + 1, scal);
}
//nominal b-veto
if(nBJets(true, false, 0) != 0) continue;
//yields with unclustered met varied down
METvec.SetPtEtaPhiE(_metOtherDown, 0, _met_phiOtherDown, _metOtherDown);
mt_min = transmass(lepV[lw_min], METvec);
searchR = hnl::sr(mt_min, minMos, lepSyst.M(), cat);
if(cat > 3 || _metOtherDown < 75){
yieldsDown[1][cat][fill]->Fill(searchR + 1, scal);
}
//yields with unclustered met varied up
METvec.SetPtEtaPhiE(_metOtherUp, 0, _met_phiOtherUp, _metOtherUp);
mt_min = transmass(lepV[lw_min], METvec);
searchR = hnl::sr(mt_min, minMos, lepSyst.M(), cat);
if(cat > 3 || _metOtherUp < 75){
yieldsUp[1][cat][fill]->Fill(searchR + 1, scal);
}
//nominal met cut for low-mass categories
if(cat < 4 && _met > 75) continue;
//nominal search region
METvec.SetPtEtaPhiE(_met, 0, _met_phi, _met);
mt_min = transmass(lepV[lw_min], METvec);
searchR = hnl::sr(mt_min, minMos, lepSyst.M(), cat);
//Scale weights for glugluToZZ
if(fileList[sam].Contains("GluGluToZZTo")){
for(unsigned pdf = 0; pdf < 110; ++pdf){
_scaleWeight[pdf] = 1;
}
}
//TEMPORARY FOR SIGNAL
if(fileList[sam].Contains("M-6_V-0.00316") || fileList[sam].Contains("M-7_V-0.00316")){
for(unsigned pdf = 0; pdf < 110; ++pdf){
_scaleWeight[pdf] = 1;
}
}
//yields with scale varied down
yieldsDown[2][cat][fill]->Fill(searchR + 1, scal*_scaleWeight[8]);
//yields with scale varied up
yieldsUp[2][cat][fill]->Fill(searchR + 1, scal*_scaleWeight[4]);
//yields with all pdf variations
for(unsigned pdf = 0; pdf < 100; ++pdf){
yieldsPdfVar[pdf][cat][fill]->Fill(searchR + 1, scal*_scaleWeight[pdf + 9]);
}
//yields with pileup varied down
yieldsDown[4][cat][fill]->Fill(searchR + 1, (scal/PUweights[0]->GetBinContent(PUweights[0]->FindBin( std::min(_n_trueInteractions, 49) )))*PUweights[1]->GetBinContent(PUweights[1]->FindBin(std::min(_n_trueInteractions, 49) )) );
//yields with pileup varied up
yieldsUp[4][cat][fill]->Fill(searchR + 1, (scal/PUweights[0]->GetBinContent(PUweights[0]->FindBin( std::min(_n_trueInteractions, 49) )))*PUweights[2]->GetBinContent(PUweights[2]->FindBin(std::min(_n_trueInteractions, 49) )) );
//yields with b-tag SF varied down
yieldsDown[5][cat][fill]->Fill(searchR + 1, (scal/bTagSF(true, 0))*bTagSF(true, 1));
//yields with b-tag SF varied up
yieldsUp[5][cat][fill]->Fill(searchR + 1, (scal/bTagSF(true, 0))*bTagSF(true, 2));
//Trigger uncertainties
if(conePt[0] > 30){
yieldsDown[6][cat][fill]->Fill(searchR + 1, scal*0.98);
yieldsUp[6][cat][fill]->Fill(searchR + 1, scal*1.02);
} else{
yieldsDown[6][cat][fill]->Fill(searchR + 1, scal*0.95);
yieldsUp[6][cat][fill]->Fill(searchR + 1, scal*1.05);
}
//Id efficiency uncertainties
unsigned flavorC = tril_flavorComb(ind, _flavors, lCount);
if(flavorC == 0 || flavorC == 3){
yieldsDown[7][cat][fill]->Fill(searchR + 1, scal*0.94);
yieldsUp[7][cat][fill]->Fill(searchR + 1, scal*1.06);
} else{
yieldsDown[7][cat][fill]->Fill(searchR + 1, scal*0.9553);
yieldsUp[7][cat][fill]->Fill(searchR + 1, scal*1.0447);
}
if(effsam - nSig -1 == 0){// only ZZ needs last nuisance!
if(mt_min < 75){
yieldsDown[9][cat][fill]->Fill(searchR + 1, scal);
yieldsUp[9][cat][fill]->Fill(searchR + 1, scal);
} else{
yieldsDown[9][cat][fill]->Fill(searchR + 1, scal*0.75);
yieldsUp[9][cat][fill]->Fill(searchR + 1, scal*1.25);
}
}
delete[] ind;
delete[] conePt;
delete[] lepV;
}
//Set negative bins to 0 before adding other processes
for(unsigned cat = 0; cat < nCat; ++cat){
for(unsigned bin = 1; bin < yields[cat][effsam]->GetNbinsX() + 1; ++bin){
if(yields[cat][effsam]->GetBinContent(bin) < 0.) yields[cat][effsam]->SetBinContent(bin, 0.);
for(unsigned unc = 0; unc < nUnc; ++unc){
if(yieldsDown[unc][cat][effsam]->GetBinContent(bin) < 0.) yieldsDown[unc][cat][effsam]->SetBinContent(bin, 0.);
if(yieldsUp[unc][cat][effsam]->GetBinContent(bin) < 0.) yieldsUp[unc][cat][effsam]->SetBinContent(bin, 0.);
}
for(unsigned pdf = 0; pdf < 100; ++pdf){
if(yieldsPdfVar[pdf][cat][effsam]->GetBinContent(bin) < 0.) yieldsPdfVar[pdf][cat][effsam]->SetBinContent(bin, 0.);
}
}
}
}
//Set fakes to 0 if they are negative
for(unsigned cat = 0; cat < nCat; ++cat){
for(unsigned bin = 1; bin < yields[cat][nSamples_eff]->GetNbinsX() + 1; ++bin){
if(yields[cat][nSamples_eff]->GetBinContent(bin) < 0) yields[cat][nSamples_eff]->SetBinContent(bin, 0.);
if(yieldsDown[8][cat][nSamples_eff]->GetBinContent(bin) < 0) yieldsDown[8][cat][nSamples_eff]->SetBinContent(bin, 0.);
if(yieldsUp[8][cat][nSamples_eff]->GetBinContent(bin) < 0) yieldsUp[8][cat][nSamples_eff]->SetBinContent(bin, 0.);
}
}
//Calculate rms of pdf shape variations
for(unsigned cat = 0; cat < nCat; ++cat){
for(unsigned effsam = 1; effsam < nSamples_eff; ++effsam){
for(unsigned b = 1; b < yields[cat][effsam]->GetNbinsX() + 1; ++b){
double pdfVarRms = 0;
for(unsigned pdf = 0; pdf < 100; ++pdf){
pdfVarRms += (yields[cat][effsam]->GetBinContent(b) - yieldsPdfVar[pdf][cat][effsam]->GetBinContent(b))*(yields[cat][effsam]->GetBinContent(b) - yieldsPdfVar[pdf][cat][effsam]->GetBinContent(b));
}
pdfVarRms = 0.01*sqrt(pdfVarRms);
yieldsDown[3][cat][effsam]->SetBinContent(b, yields[cat][effsam]->GetBinContent(b) - pdfVarRms);
yieldsUp[3][cat][effsam]->SetBinContent(b, yields[cat][effsam]->GetBinContent(b) + pdfVarRms);
}
}
}
//Cross section variation on signal because of scale
for(unsigned cat = 0; cat < nCat; ++cat){
for(unsigned sig = 0; sig < nSig; ++sig){
if(sig != 3 && sig !=4){
yieldsDown[10][cat][sig + 1] = (TH1D*) yields[cat][sig+1]->Clone();
yieldsDown[10][cat][sig + 1]->Scale(pdfCounter[sig][8]/hcounter[sig+1]);
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "pdfCounter[sig][8]/hcounter[sig+1] = " << pdfCounter[sig][8]/hcounter[sig+1] << endl;
yieldsUp[10][cat][sig + 1] = (TH1D*) yields[cat][sig+1]->Clone();
yieldsUp[10][cat][sig + 1]->Scale(pdfCounter[sig][4]/hcounter[sig+1]);
cout << "pdfCounter[sig][4]/hcounter[sig+1] = " << pdfCounter[sig][4]/hcounter[sig+1] << endl;
}
else{
yieldsDown[10][cat][sig + 1] = (TH1D*) yields[cat][sig+1]->Clone();
yieldsUp[10][cat][sig + 1] = (TH1D*) yields[cat][sig+1]->Clone();
}
}
}
//Signal cross section variation due to pdf
double pdfUnc[nSig];
for(unsigned sig = 0; sig < nSig; ++sig){
for(unsigned pdf = 0; pdf < 100; ++pdf){
double diff = fabs(pdfCounter[sig][pdf + 9] - hcounter[sig + 1])/hcounter[sig+1];
pdfUnc[sig] += diff*diff;
}
pdfUnc[sig] = 0.01*sqrt(pdfUnc[sig]);
cout << names[sig + 1] << " pdf unc = " << pdfUnc[sig] << endl;
}
//Split data and MC histograms for plotting and propagating uncertainties
TH1D* dataYields[nCat];
for(unsigned cat = 0; cat < nCat; ++cat){
//dataYields[cat] = (TH1D*) yields[cat][nSig + 1]->Clone();
dataYields[cat] = (TH1D*) yields[cat][0]->Clone();
//TEMPORARY, REMOVE LATER, ADD 10 and 200 GeV masses to pseudodata
/*
TH1D* temp30 = (TH1D*) yields[cat][6]->Clone();
TH1D* temp200 = (TH1D*) yields[cat][14]->Clone();
temp30->Scale(5.);
temp200->Scale(5.);
dataYields[cat]->Add(temp30);
dataYields[cat]->Add(temp200);
*/
}
TH1D* bkgYields[nCat][nSamples_eff -nSig];
TH1D* bkgErros[nCat][nSamples_eff - nSig];
for(unsigned cat = 0; cat < nCat; ++cat){
for(unsigned effsam = nSig + 1; effsam < nSamples_eff + 1; ++effsam){
bkgYields[cat][effsam -nSig - 1] = (TH1D*) yields[cat][effsam]->Clone();
/*
if(effsam > nSig + 1){
dataYields[cat]->Add(bkgYields[cat][effsam - nSig - 1]);
}
*/
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//PRINT DATA CARDS FOR LIMIT CALCULATION
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const unsigned nBkg = nSamples_eff - nSig;
const unsigned nSyst = 13 + 1 + 2*nBkg; //9 general uncertainties + stat signal + stat bkg + extra unc per bkg
TString systNames[nSyst] = {"lumi", "pdfAcc", "JEC", "metUncl", "scale", "pdf", "pu", "btagSF", "id_eff", "trigeff", "fakeEWK", "ZZmt", "scaleAcc"};
const TString sigN[nSig] = {"m1", "m2",
"m3",
"m4",
"m5",
"m5corrected",
"m6",
"m7",
"m8",
"m9",
"m10",
"m11",
"m12",
"m20", "m30", "m40", "m50", "m60", "m80", "m100", "m130", "m150", "m200", "m400", "m600", "m800", "m1000"};
const TString bkgNames[nBkg] = {"ZZH", "triboson", "WZ", "Xgamma", "TTX", "nonPrompt"}; //slightly rewrite bkg names not to confuse the combine tool
std::vector<std::vector<double>> systUnc(nSyst, std::vector<double>(nBkg + 1, 0)); //2D array containing all systematics
//initialize flat and shape systematics
for(unsigned proc = 0; proc < nBkg; ++proc){ //last background is non-prompt and isn't susceptible to these uncertainty sources (note that entry 0 is signal so that the loop goes up to the second to last bkg)
if(proc != 0 && (bkgNames[proc - 1] == "ZZH" || bkgNames[proc - 1] == "WZ" || bkgNames[proc - 1] == "Xgamma")) systUnc[0][proc] = 0; //lumi unc is 0 here since these bkg are normalized in data!
else systUnc[0][proc] = 1.025; //lumi
systUnc[1][proc] = 0; //pdfAcc
systUnc[2][proc] = 1.00; //JEC shape
systUnc[3][proc] = 1.00; //PU shape
systUnc[4][proc] = 1.00; //MET unclustered shape
systUnc[5][proc] = 1.00; //b-tag SF shape
systUnc[6][proc] = 1.00; //scale shape
systUnc[7][proc] = 1.00; //pdf shape
systUnc[8][proc] = 1.00; //id eff
systUnc[9][proc] = 1.00; //trig eff
systUnc[10][proc] = 0; //EWK fake subtraction
systUnc[11][proc] = 0; //ZZmt
systUnc[12][proc] = 0; //scale Acc
}
//EWK subtraction unc affects non-prompt Bkg
systUnc[10][nBkg] = 1.00;
//ZZmt uncertainty only affects ZZ Bkg
systUnc[11][1] = 1.00;
//Scale acceptance uncertainty affects signal
systUnc[12][0] = 1.00;
TString systDist[nSyst]; //probability distribution of nuisances
for(unsigned syst = 0; syst < nSyst; ++syst){
if(syst > 1 && syst < 13) systDist[syst] = "shape";
else systDist[syst] = "lnN";
}
const double extraUnc[nBkg] = {1.1, 1.5, 1.094, 1.15, 1.5, 1.3}; //extra flat uncertainties assigned to each background
for(unsigned syst = 14 + nBkg; syst < nSyst; ++syst){//loop over last nBkg uncertainties, being the exta uncertainties for each bkg
unsigned bkg = syst - 14 - nBkg;//index of the background corresponding to the uncertainty index
systNames[syst] = "extra" + bkgNames[bkg];
systUnc[syst][bkg + 1] = extraUnc[bkg];
}
unsigned binCount = 0;
for(unsigned cat = 0; cat < nCat; ++cat){
if(cat == 0 || cat == 2) continue;
binCount += nSR[cat];
}
const unsigned binC = binCount;
TH1D* histCent[nSamples_eff + 1][binC];// = new TH1D("yieldCent", "yieldCent; ; yield", 1, 0, 1);
TH1D* histUp[nUnc][nSamples_eff + 1][binC];// = new TH1D("yieldUp", "yieldUp; ; yield", 1, 0, 1);
TH1D* histDown[nUnc][nSamples_eff + 1][binC];// = new TH1D("yieldDown", "yieldDown; ; yield", 1, 0, 1);
for(unsigned effsam = 0; effsam < nSamples_eff + 1; ++effsam){
for(unsigned bin = 0; bin < binC; ++bin){
histCent[effsam][bin] = new TH1D("yieldCent" + eff_names[effsam] + std::to_string(bin), "yieldCent" + eff_names[effsam] + std::to_string(bin) + "; ; yield", 1, 0, 1);
for(unsigned unc = 0; unc < nUnc; ++unc){
histDown[unc][effsam][bin] = new TH1D("yieldsDown" + eff_names[effsam] + uncNames[unc] + std::to_string(bin), "yieldsDown" + eff_names[effsam] + uncNames[unc] + std::to_string(bin) + "; ; yield", 1, 0, 1);
histUp[unc][effsam][bin] = new TH1D("yieldsUp" + eff_names[effsam] + uncNames[unc] + std::to_string(bin), "yieldsUp" + eff_names[effsam] + uncNames[unc] + std::to_string(bin) + "; ; yield", 1, 0, 1);
}
}
}
//loop over all signals, categories and bins to fill final uncertainties and print datacards
for(unsigned sig = 0; sig < nSig; ++sig){
//Set correct pdf and scale Acceptance uncertainty for this signal
if(sig != 3 && sig != 4) systUnc[1][0] = 1 + pdfUnc[sig];
else systUnc[1][0] = 1;
//Counter to determine the "combined bin index"
unsigned binCounter = 1;
//loop over all categories and bins
for(unsigned cat = 0; cat < nCat; ++cat){
if(cat == 0 || cat == 2) continue;
for(unsigned bin = 1; bin < dataYields[cat]->GetNbinsX() + 1; ++bin){
//Define correlations between uncertainty sources
//systNames[4] += catNames[cat];
if(cat > 3) systNames[3] += catNames[cat]; //unclustered met is fully correlated in low mass because of upper met cut
if(cat == 4){
if(bin < 4) systNames[3] += "part1";
else if(bin < 9) systNames[3] += "part2";
else if(bin < 13) systNames[3] += "part3";
else systNames[3] += "part4";
} else if(cat == 5){
if(bin < 3) systNames[3] += "part1";
else if(bin < 7) systNames[3] += "part2";
else if(bin < 9) systNames[3] += "part3";
else systNames[3] += "part4";
}
systNames[4] += "bin" + std::to_string(binCounter); //Assume scale uncorrelated between bins so the bulk does not constrain the tails
//systNames[6] += "bin" + std::to_string(binCounter);
//make root file
TFile* shapeFile = new TFile("./datacards/shapes/shapeFile_MuonCoupling_" + sigN[sig] + "_bin" + (TString) std::to_string(binCounter) + extra + ".root", "recreate");
histCent[0][binCounter - 1]->SetBinContent(1, dataYields[cat]->GetBinContent(bin));
histCent[0][binCounter - 1]->Write("data_obs");//_ch" + (TString) std::to_string(binCounter));
histCent[sig + 1][binCounter - 1]->SetBinContent(1, std::max(yields[cat][1+sig]->GetBinContent(bin), 0.) );
histCent[sig + 1][binCounter - 1]->Write("signal");//_ch" + (TString) std::to_string(binCounter));
for(unsigned unc = 0; unc < nUnc; ++unc){
if(unc == 8 || unc == 9) continue; //Fake ewk subtraction and ZZ mt does not affect signal
double min = (yields[cat][1+sig]->GetBinContent(bin) == 0) ? 0. : (double) std::numeric_limits< float >::min();
histUp[unc][sig + 1][binCounter - 1]->SetBinContent(1, std::max(yieldsUp[unc][cat][sig + 1]->GetBinContent(bin), min ));
histUp[unc][sig + 1][binCounter - 1]->Write("signal_" + systNames[2 + unc] + "Up");//"signal_ch" + (TString) std::to_string(binCounter) + "_" + systNames[3 + unc] + "Up"
histDown[unc][sig + 1][binCounter - 1]->SetBinContent(1, std::max(yieldsDown[unc][cat][sig + 1]->GetBinContent(bin), min ));
histDown[unc][sig + 1][binCounter - 1]->Write("signal_" + systNames[2 + unc] + "Down"); //"signal_ch" + (TString) std::to_string(binCounter) + "_" + systNames[3 + unc] + "Down");
}
for(unsigned bkg = 0; bkg < nBkg; ++bkg){ //BUG IN BIN INDEX!
histCent[bkg + nSig + 1][binCounter - 1]->SetBinContent(1, std::max(yields[cat][bkg + nSig + 1]->GetBinContent(bin), 0.) );
histCent[bkg + nSig + 1][binCounter - 1]->Write(bkgNames[bkg]); //+ "_Ch" + std::to_string(binCounter));
//if(bkg != nBkg -1){
for(unsigned unc = 0; unc < nUnc - 1; ++unc){
if(bkg == nBkg -1 && unc != 8) continue; //only write EWK contamination unc for non-prompt
else if(bkg != nBkg -1 && unc == 8) continue;
else if(bkg != 0 && unc == 9) continue; //only write ZZmt unc for ZZ
double min = (yields[cat][bkg + nSig + 1]->GetBinContent(bin) == 0) ? 0. : (double) std::numeric_limits< float >::min();
histUp[unc][bkg + nSig + 1][binCounter - 1]->SetBinContent(1, std::max((double) yieldsUp[unc][cat][bkg + nSig + 1]->GetBinContent(bin), min ) );
histDown[unc][bkg + nSig + 1][binCounter - 1]->SetBinContent(1, std::max((double) yieldsDown[unc][cat][bkg + nSig + 1]->GetBinContent(bin), min ) );
histUp[unc][bkg + nSig + 1][binCounter - 1]->Write(bkgNames[bkg] + "_" + systNames[2 + unc] + "Up");//bkgNames[bkg] + "_ch" + (TString) std::to_string(binCounter) + "_" + systNames[3 + unc] + "Up"
histDown[unc][bkg + nSig + 1][binCounter - 1]->Write(bkgNames[bkg] + "_" + systNames[2 + unc] + "Down");//bkgNames[bkg] + "_ch" + (TString) std::to_string(binCounter)+ "_" + systNames[3 + unc] + "Down"
}
//} else{
}
shapeFile->Close();
//signal stat unc
systUnc[13][0] = 1 + std::max(0., yields[cat][sig + 1]->GetBinError(bin)/yields[cat][sig + 1]->GetBinContent(bin));
systNames[13] = "statSig" + std::to_string(binCounter);
double bkgYieldVal[nBkg];//bkg rates
//bkg stat unc
for(unsigned bkg = 0; bkg < nBkg; ++bkg){
bkgYieldVal[bkg] = bkgYields[cat][bkg]->GetBinContent(bin);
systUnc[14 + bkg][bkg + 1] = 1 + ((bkgYields[cat][bkg]->GetBinContent(bin) == 0) ? 0. : std::max(0., bkgYields[cat][bkg]->GetBinError(bin)/bkgYields[cat][bkg]->GetBinContent(bin)) );
systNames[14 + bkg] = "stat" + bkgNames[bkg] + std::to_string(binCounter);
}
//change shape uncertainty names for uncorrelated uncertainty sources
//print datacard
if(sig == 3 || sig == 4) cout << sigN[sig] << " " << yields[cat][sig + 1]->GetBinContent(bin) << std::endl;
hnl::printDataCard(dataYields[cat]->GetBinContent(bin), yields[cat][sig + 1]->GetBinContent(bin), "signal", bkgYieldVal, nBkg, bkgNames, systUnc, nSyst, systNames, systDist, "datacards/datacard_MuonCoupling" + sigN[sig] + "_bin" + (TString) std::to_string(binCounter) + extra, true, "shapes/shapeFile_MuonCoupling_" + sigN[sig] + "_bin" + (TString) std::to_string(binCounter) + extra);
//Reset uncertainty names that were changed for correlations
systNames[3] = "metUncl";
systNames[4] = "scale";
//systNames[6] = "pdf";
//increment bin Counter
++binCounter;
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Calculate histogram with systematic uncertainty for backgrounds and print the ranges of their size
double flatSyst[2] = {0.025};
TH1D* bkgSyst[nCat][nSamples_eff -nSig];
for(unsigned bkg = 0; bkg < nBkg; ++bkg){
//flatSyst[4] = fabs(pdfAccUnc[bkg + nSig + 1] - 1);
flatSyst[1] = fabs(extraUnc[bkg] -1);
for(unsigned cat = 0; cat < nCat; ++cat){
bkgSyst[cat][bkg] = (TH1D*) bkgYields[cat][bkg]->Clone();
for(unsigned b = 1; b < dataYields[cat]->GetNbinsX() + 1; ++b){
bkgSyst[cat][bkg]->SetBinContent(b,0);
/*
if(yields[cat][bkg + nSig + 1]->GetBinContent(b) == 0){
bkgSyst[cat][bkg]->SetBinContent(b,0);
} else{
*/
if(bkg != nBkg -1){
double systbin = 0;
//loop over shape uncertainties
for(unsigned unc = 0; unc < nUnc - 1; ++unc){
if(unc == 8) continue; //EWK subtraction unc is not defined for MC backgrounds
if(bkg != 0 && unc == 9) continue;
double syst = std::max(fabs(yieldsUp[unc][cat][bkg + nSig + 1]->GetBinContent(b) - yields[cat][bkg + nSig + 1]->GetBinContent(b)), fabs(yieldsDown[unc][cat][bkg + nSig + 1]->GetBinContent(b) - yields[cat][bkg + nSig + 1]->GetBinContent(b)));
systbin += syst*syst;
}
//loop over flat uncertainties
for(unsigned unc = 0; unc < 2; ++unc){
systbin += bkgYields[cat][bkg]->GetBinContent(b)*bkgYields[cat][bkg]->GetBinContent(b)*flatSyst[unc]*flatSyst[unc];
}
bkgSyst[cat][bkg]->SetBinContent(b, sqrt(systbin));
} else{
double syst = std::max(fabs(yieldsUp[8][cat][bkg + nSig + 1]->GetBinContent(b) - yields[cat][bkg + nSig + 1]->GetBinContent(b)), fabs(yieldsDown[8][cat][bkg + nSig + 1]->GetBinContent(b) - yields[cat][bkg + nSig + 1]->GetBinContent(b)));
bkgSyst[cat][bkg]->SetBinContent(b, sqrt(yields[cat][bkg + nSig + 1]->GetBinContent(b)*0.3*yields[cat][bkg + nSig + 1]->GetBinContent(b)*0.3 + syst*syst));
}
//}
bkgSyst[cat][bkg]->SetBinError(b, 0);
}
}
}
//loop over all bins
//Make total error histogram
unsigned binCountio = 0;
for(unsigned cat = 0; cat < nCat; ++cat){
if(cat == 0 || cat == 2) continue;
TH1D* totalBkg = (TH1D*) yields[cat][nSig + 1]->Clone();
TH1D* totalBkgUnc = (TH1D*) bkgSyst[cat][0]->Clone();
for(unsigned bkg = 1; bkg < nBkg; ++bkg){
totalBkg->Add(yields[cat][nSig + 1 + bkg]);
for(unsigned b = 1; b < bkgSyst[cat][bkg]->GetNbinsX() + 1; ++b){
totalBkgUnc->SetBinContent(b, sqrt(bkgSyst[cat][bkg]->GetBinContent(b)*bkgSyst[cat][bkg]->GetBinContent(b) + totalBkgUnc->GetBinContent(b)*totalBkgUnc->GetBinContent(b)) );
}
}
for(unsigned b = 1; b < yields[cat][nSig + 1]->GetNbinsX() + 1; ++b){
++binCountio;
if( yields[cat][nSig + 1]->GetBinContent(b) == 0) continue;
std::cout << "muon search region = " << binCountio << std::endl;
double totalUnc = sqrt(totalBkgUnc->GetBinContent(b)*totalBkgUnc->GetBinContent(b) + totalBkg->GetBinError(b)*totalBkg->GetBinError(b) );
std::cout << "ratio of ZZ to total bkg = " << yields[cat][nSig + 1]->GetBinContent(b)/totalBkg->GetBinContent(b) << std::endl;
std::cout << "ratio of ZZ yield to total uncertainty = " << yields[cat][nSig + 1]->GetBinContent(b)/totalUnc << std::endl;
double zzMtSyst = std::max(fabs(yieldsUp[9][cat][nSig + 1]->GetBinContent(b) - yields[cat][nSig + 1]->GetBinContent(b)), fabs(yieldsDown[9][cat][nSig + 1]->GetBinContent(b) - yields[cat][nSig + 1]->GetBinContent(b)));
std::cout << "squared ratio of ZZ MT uncertainty to total uncertainty = " << (zzMtSyst*zzMtSyst)/(totalUnc*totalUnc) << std::endl;
}
}
//Compare ZZ yield to total uncertainty
const TString backGroundNames[nSamples_eff] = {"ZZ/H", "triboson", "WZ", "X#gamma^{(*)}", "t#bar{t}/t + X", "nonprompt"};
for(unsigned cat = 0; cat < nCat; ++cat){
hnl::makeTable(cat, bkgYields[cat], bkgSyst[cat], dataYields[cat], backGroundNames, nSamples_eff -nSig, catNames[cat], "_MuonCoupling");
}
//const TString distNames[nSamples_eff + 1 - nSig] = {"total pred.", "ZZ/H", "triboson", "WZ", "X + #gamma", "TT/T + X", "non-prompt"};
const TString distNames[nSamples_eff + 1 - nSig] = {"obs.", "ZZ/H", "triboson", "WZ", "X + #gamma", "TT/T + X", "non-prompt"};
//Plot the yields as a function of the search region
for(unsigned cat = 0; cat < nCat; ++cat){
const unsigned nSigToPlot = 6;
TH1D* signals[nSigToPlot];
TString sigPlotNames[nSigToPlot];
if(cat < 4){
sigPlotNames[0] = "1 GeV, |V|^{2} = 10^{-5} ";
sigPlotNames[1] = "5 GeV, |V|^{2} = 10^{-5} ";
sigPlotNames[2] = "20 GeV, |V|^{2} = 10^{-5} ";
sigPlotNames[3] = "30 GeV, |V|^{2} = 10^{-5} ";
sigPlotNames[4] = "50 GeV, |V|^{2} = 10^{-5} ";
sigPlotNames[5] = "60 GeV, |V|^{2} = 10^{-5} ";
} else{
sigPlotNames[0] = "100 GeV, |V|^{2} = 10^{-2} ";
sigPlotNames[1] = "130 GeV, |V|^{2} = 10^{-2} ";
sigPlotNames[2] = "150 GeV, |V|^{2} = 10^{-2} ";
sigPlotNames[3] = "200 GeV, |V|^{2} = 10^{-2} ";
sigPlotNames[4] = "400 GeV, |V|^{2} = 10^{-2} ";
sigPlotNames[5] = "600 GeV, |V|^{2} = 10^{-2} ";
}
//loop over all kinematic distributions
if(cat < 4){
signals[0] = yields[cat][1];
signals[1] = yields[cat][3];
signals[2] = yields[cat][11];
signals[3] = yields[cat][12];
signals[4] = yields[cat][14];
signals[5] = yields[cat][15];
} else{
signals[0] = yields[cat][17];
signals[1] = yields[cat][18];
signals[2] = yields[cat][19];
signals[3] = yields[cat][20];
signals[4] = yields[cat][21];
signals[5] = yields[cat][22];
}
plotDataVSMC(dataYields[cat], bkgYields[cat], distNames, nSamples_eff - nSig, catNames[cat] + "_MuonCoupling_withSignal" + extra, true, 0, "HNL", bkgSyst[cat], true, signals, sigPlotNames ,nSigToPlot);
plotDataVSMC(dataYields[cat], bkgYields[cat], distNames, nSamples_eff - nSig, catNames[cat] + "_MuonCoupling" + extra, true, 0, "HNL", bkgSyst[cat]);
}
//Make one histogram containing all search regions;
unsigned totalSR = 0;
for(unsigned cat = 0; cat < nCat; ++cat){
if(cat == 0 || cat == 2) continue;
totalSR += nSR[cat];
}
TH1D* totalDataYield = new TH1D("totaldata", "totaldata + ; search region ; Events/search region", totalSR, 0.5, totalSR + 0.5);
unsigned binCounter = 1;
for(unsigned cat = 0; cat < nCat; ++cat){
if(cat == 0 || cat == 2 || cat == 4) continue;
for(unsigned bin = 1; bin < dataYields[cat]->GetNbinsX() + 1; ++bin){
totalDataYield->SetBinContent(binCounter, dataYields[cat]->GetBinContent(bin) );
totalDataYield->SetBinError(binCounter, dataYields[cat]->GetBinError(bin) );
++binCounter;
}
}
for(unsigned bin = 1; bin < dataYields[4]->GetNbinsX() + 1; ++bin){
totalDataYield->SetBinContent(binCounter, dataYields[4]->GetBinContent(bin) );
totalDataYield->SetBinError(binCounter, dataYields[4]->GetBinError(bin) );
++binCounter;
}
TH1D* totalBkgYields[nBkg];
TH1D* totalBkgError[nBkg];
for(unsigned bkg = 0; bkg < nBkg; ++bkg){
binCounter = 1;
totalBkgYields[bkg] = new TH1D(backGroundNames[bkg], backGroundNames[bkg] + "; search region ; Events/search region", totalSR, 0.5, totalSR + 0.5);
totalBkgError[bkg] = new TH1D(backGroundNames[bkg] + "error",backGroundNames[bkg] + "error; search region ; Events/search region", totalSR, 0.5, totalSR + 0.5);
for(unsigned cat = 0; cat < nCat; ++cat){
if(cat == 0 || cat == 2 || cat == 4) continue;
for(unsigned bin = 1; bin < bkgYields[cat][bkg]->GetNbinsX() + 1; ++bin){
totalBkgYields[bkg]->SetBinContent(binCounter, bkgYields[cat][bkg]->GetBinContent(bin) );
totalBkgYields[bkg]->SetBinError(binCounter, bkgYields[cat][bkg]->GetBinError(bin) );
totalBkgError[bkg]->SetBinContent(binCounter, bkgSyst[cat][bkg]->GetBinContent(bin) );
totalBkgError[bkg]->SetBinError(binCounter, 0);
++binCounter;
}
}
for(unsigned bin = 1; bin < bkgYields[4][bkg]->GetNbinsX() + 1; ++bin){
totalBkgYields[bkg]->SetBinContent(binCounter, bkgYields[4][bkg]->GetBinContent(bin) );
totalBkgYields[bkg]->SetBinError(binCounter, bkgYields[4][bkg]->GetBinError(bin) );