-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawPlots.C
2116 lines (1736 loc) · 65.1 KB
/
drawPlots.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <stdexcept>
#include <vector>
#include "TChain.h"
#include "TLegend.h"
#include "TCanvas.h"
#include "TColor.h"
#include "THStack.h"
#include "TGaxis.h"
#include "TCut.h"
#include "TH1D.h"
#include "External/CTable.cpp"
#include "External/tdrstyle_SUSY.C"
#include "ConfigParser.C"
#include "ConfigHelper.C"
#include "HistTools.C"
using namespace std;
std::array<int, 14> ROOT_COLOR_PALATE = {46,8,9,38,40,2,30,6,28,42,3,5,7,41};
/*std::pair<double,double> getLegendLocation(TH1D* bg_sum){
// Returns the best (xmin, ymin) pair for the location of the legend
}*/
double errMult(double A, double B, double errA, double errB, double C) {
return sqrt(C*C*(pow(errA/A,2) + pow(errB/B,2)));
}
float err_binomial(float A, float B, float errA, float errB) {
/*
returns the error on C = A/(A+B)
note that if A and B are integers, simplifies to sqrt((C * (1-C)) / (A+B))
or thinking of an efficiency, sqrt((eff * (1-eff)) / N)
*/
return (1/pow(A+B,2)) * sqrt(pow(B*errA,2) + pow(A*errB,2));
}
void drawLatexFromTString(TString text, double x_low, double y_low){
TLatex *lumitex = NULL;
lumitex = new TLatex(x_low, y_low , text );
lumitex->SetNDC();
lumitex->SetTextSize(30);
lumitex->SetLineWidth(2);
lumitex->SetTextFont(43);
lumitex->Draw();
return;
}
bool TH1DIntegralSort(TH1D* hist_1, TH1D* hist_2){
return (hist_1->Integral() < hist_2->Integral()) ;
}
bool LabeledHistSort(pair<TH1D*,TString> hist_1, pair<TH1D*,TString> hist_2){
return (hist_1.first->Integral() < hist_2.first->Integral()) ;
}
void drawCMSLatex(double luminosity){
TLatex *lumitex = NULL;
double height=1.01-gPad->GetTopMargin();
float left_margin = gPad->GetLeftMargin();
// lumitex = new TLatex(0.66,0.955, Form("%.1f fb^{-1} (13 TeV)", luminosity) );
if (luminosity < 1){
lumitex = new TLatex(.9-left_margin, height , Form("%.1f pb^{-1} (13 TeV)", luminosity*1000) );
}
else{
lumitex = new TLatex(.9-left_margin, height , Form("%.1f fb^{-1} (13 TeV)", luminosity) );
}
// lumitex = new TLatex(0.66,0.955, Form("few pb^{-1} (13 TeV)") );
lumitex->SetNDC();
lumitex->SetTextSize(0.03);
lumitex->SetLineWidth(2);
lumitex->SetTextFont(42);
lumitex->Draw();
TLatex *cmstex = NULL;
cmstex = new TLatex(left_margin, height, "#it{CMS #bf{Preliminary}}" );
cmstex->SetNDC();
cmstex->SetTextSize(0.03);
cmstex->SetLineWidth(2);
cmstex->SetTextFont(62);
cmstex->Draw();
return;
}
void drawSRText(TString SR, double high_y, double low_x){
cout<<"Drawing SR Text"<<endl;
TString text;
float left_margin = gPad->GetLeftMargin();
if(SR == "Strong_Bveto_2j"){
text="#splitline{2-3 jets; No b-tags}{H_{T} > 500 GeV; M_{T2} > 80 GeV}";
}
else if(SR == "Strong_Bveto_4j"){
text="#splitline{4-5 jets; No b-tags}{H_{T} > 500 GeV; M_{T2} > 80 GeV}";
}
else if(SR == "Strong_Bveto_6j"){
text="#splitline{#geq 6 jets; No b-tags}{M_{T2} > 80 GeV}";
}
else if(SR == "Strong_Btag_2j"){
text="#splitline{2-3 jets; #geq 1 b-tags}{H_{T} > 200 GeV; M_{T2} > 100 GeV}";
}
else if(SR == "Strong_Btag_4j"){
text="#splitline{4-5 jets; #geq 1 b-tags}{H_{T} > 200 GeV; M_{T2} > 100 GeV}";
}
else if(SR == "Strong_Btag_6j"){
text="#splitline{#geq 6 jets; #geq 1 b-tags}{M_{T2} > 100 GeV}";
}
else if(SR == "TChiWZ"){
text="EWK WZ/ZZ Region";
}
else if(SR == "TChiHZ"){
text="EWK HZ Region";
}
else{
cout<<"Coult not match SR: "<<SR<<endl;
}
cout<<"y_max: "<<high_y<<endl;
//TLatex *SRText = new TLatex(low_x, high_y, text.Data()); //Doesn't work because splitline was coded by an asshole
TLatex *SRText = new TLatex(left_margin+.05, .85, text.Data()); //just awful style, thanks ROOT.
SRText->SetNDC();
SRText->SetTextSize(0.03);
SRText->SetLineWidth(2);
SRText->SetTextFont(62);
SRText->Draw();
return;
}
TString drawArbitraryNumberWithResidual(ConfigParser *conf){
// This method expects conf to have a plot config loaded in already.
//In the conf, we expect there to be hist names of the form file_N_path,
//hist_n_name, starting with 0 for the primary histogram, which is normally
//going to be the data events in our signal region. The rest of the hists, starting
//from 1, are added to a THStack which is normalized to hist_0 in the bin 0-50.
//num_hists should be the number of the number of histograms in the plot.
TString errors="";
TGraphAsymmErrors *prediction_errors;
int num_hists=stoi(conf->get("num_hists"));
int BG_sum_from=(conf->get("BG_sum_from") != "") ? stoi(conf->get("BG_sum_from")) : 1;
if (num_hists < 2){
return TString("Less than Two hists can not be turned into a residual plot, please call drawSingleTH1");
}
//Add files from which to obtain histos
TString default_hist_dir = getDefaultHistDir(conf);
vector<TFile*> hist_files (num_hists);
for (int i = 0; i<num_hists; i++){
TString sample_loc = "";
if (conf->get("file_"+to_string(i)+"_path") != ""){
sample_loc = TString(conf->get("file_"+to_string(i)+"_path"));
}
else{
sample_loc = default_hist_dir+conf->get("sample_"+to_string(i))+".root";
}
hist_files[i]=new TFile(sample_loc);
}
cout << "Found files "<<endl;
TString plot_name = conf->get("plot_name");
double bin_size;
if (conf->get("bin_size") != ""){
bin_size = stod(conf->get("bin_size"));
}
else{
bin_size=1;
}
//Get name of hist to read from file
vector<TString> hist_names (num_hists);
for (int i = 0; i<num_hists; i++){
if (conf->get("hist_"+to_string(i)+"_name") != ""){
hist_names[i]=conf->get("hist_"+to_string(i)+"_name");
}
else{
hist_names[i]= conf->get("hist_0_name");
}
}
cout<<"/////////////////////////////////////////////////"<<endl;
cout << "Making Plots for: "<<plot_name<<endl;
cout<<"/////////////////////////////////////////////////"<<endl;
//Get labels for TLegend
vector<TString> hist_labels (num_hists);
for (int i = 0; i<num_hists; i++){
hist_labels[i]=parseLatex(conf->get("hist_"+to_string(i)+"_label"));
}
cout<<"Hist names set"<<endl;
TString xlabel=parseLatex(conf->get("xlabel"));
TString ylabel=parseLatex(conf->get("ylabel"));
TString save_dir=(conf->get("save_dir") != "") ? conf->get("save_dir") : getOutputDir(conf, "plot");
TString plot_title=parseLatex(conf->get("title"));
vector<TH1D*> hists (num_hists);
for (int i = 0; i<num_hists; i++){
hists[i] = (TH1D*) ((TH1D*) hist_files[i]->Get(hist_names[i]))->Clone("hist_"+to_string(i)+"_"+plot_name);
cout<<hist_names[i]<<" found in "<<hist_files[i]->GetName()<<endl;
cout<<hist_labels[i]<<" Integral bin 0 to bin 100 Content: "<<hists[i]->Integral(0,100)<<endl;
}
cout << "Histograms pulled from files, adding draw options"<<endl;
//cout<<__LINE__<<endl;
if (conf->get("relabel_x_axis") == "true"){
hists[0]->LabelsDeflate();
}
double xmax = (conf->get("xmax") != "" ) ? stod(conf->get("xmax")) : hists[0]->GetNbinsX();
double xmin = (conf->get("xmin") != "" ) ? stod(conf->get("xmin")) : 0;
//============================================
// Draw Data-MC Plots
//============================================
//cout<<__LINE__<<endl;
TCanvas * c = new TCanvas("c","",2000,2000);
c->cd();
gPad->SetRightMargin(0.05);
gPad->Modified();
gStyle->SetOptStat(kFALSE);
TPad *fullpad = new TPad("fullpad", "fullpad", 0,0,1,1);
//cout<<__LINE__<<endl;
fullpad->Draw();
fullpad->cd();
//cout<<__LINE__<<endl;
TPad *plotpad = new TPad("plotpad", "plotpad",0,0.2,1.0,0.99);
plotpad->SetRightMargin(0.05);
if (conf->get("ExtraRightMargin") == "true")
{
plotpad->SetRightMargin(0.08);
}
plotpad->SetBottomMargin(0.12);
//cout<<__LINE__<<endl;
plotpad->Draw();
plotpad->cd();
//cout<<__LINE__<<endl;
if (conf->get("logy") == "true")
{
cout<<"Plot tagged for log y-axis"<<endl;
plotpad->SetLogy();
}
//cout<<__LINE__<<endl;
if (conf->get("bin_size") != ""){
for (int i = 0; i<num_hists; i++){
hists[i]->Rebin(bin_size);
}
}
else if (conf->get("binning") != ""){
vector<double> binning = parseVector(conf->get("binning"));
for (int i = 0; i<num_hists; i++){
hists[i] = (TH1D*) hists[i]->Rebin(binning.size()-1, TString(hist_names[i]+"_rebin"), &binning[0]);
}
}
cout<<"===========================================\nRebin\n===========================================\n";
for (int i = 0; i<num_hists; i++){
cout<<hist_labels[i]<<" Integral bin 0 to bin 100 Content: "<<hists[i]->Integral(hists[i]->FindBin(0),hists[i]->FindBin(100))<<endl;
}
//===========================
// Normalize
//===========================
//cout<<__LINE__<<endl;
TH1D* clonedBG_norm = NULL;
TH1D* clonedPrimary_norm = (TH1D*) hists[0]->Clone("clonedPrimary_forNorm_"+plot_name);
double numEventsData, numEventsMC, errEventsMC;
int norm_bin;
//Add scale factors like RSFOF
cout<<"Scaling Hists"<<endl;
for (int i=0; i < num_hists; i++){
if (conf->get("hist_"+to_string(i)+"_scale") != ""){
hists[i]->Scale(stod(conf->get("hist_"+to_string(i)+"_scale")));
}
}
cout<<"Zeroing Negative Numbers"<<endl;
for (int i=0; i < num_hists; i++){
zeroNegatives(hists[i]);
}
if (conf->get("normalize") == "true"){
TString hist_nums_for_norm = conf->get("normalize_hist_nums");
cout<<"Normalizing hists: "<<hist_nums_for_norm<<endl;
//cout<<__LINE__<<endl;
//This if statement is used when there is
//special normalization being done. This part of
//the code makes a proxy for the BG sum and for the
//primary hist (hists[0]). The primary proxy is just a clone,
//the BG proxy is the sum of all the hists that are named in
//hist_nums_for_norm. If subrtact_non_normed is also true then
//the hists not marked to be normalized will be subtracted from the
//primary before the scale factors are calculated.
if (hist_nums_for_norm != ""){ //if not all hists to be normed.
//cout<<__LINE__<<endl;
for (int i=1; i<num_hists; i++){ //for each hist
if (hist_nums_for_norm.Contains(to_string(i))){ //if hist is in list to be normed
cout<<"Adding hist "<<i<<" to normalization BG."<<endl;
//cout<<__LINE__<<endl;
if (clonedBG_norm == NULL){ //Make new clonedBG
//cout<<__LINE__<<endl;
clonedBG_norm = (TH1D*) hists[i]->Clone("clonedBG_forNorm_"+plot_name);
cout<<hist_labels[i]<<": original-num "<<hists[i]->GetBinContent(1)<<endl;
}
else{
//cout<<__LINE__<<endl;
clonedBG_norm->Add(hists[i]); //Add to clonedBG
cout<<hist_labels[i]<<": original-num "<<hists[i]->GetBinContent(1)<<endl;
}
//cout<<__LINE__<<endl;
}
}
}
else{ //otherwise put all hists in clonedBG
clonedBG_norm = (TH1D*) hists[1]->Clone("clonedBG_forNorm_"+plot_name);
for (int i=2; i<num_hists; i++){
clonedBG_norm->Add(hists[i]);
}
}
//cout<<__LINE__<<endl;
//check to make sure bg hist is not empty
if (clonedBG_norm == NULL){
return TString("Check the normalize_hist_nums opt, no hists in range labeled for normalization");
}
//cout<<__LINE__<<endl;
if (conf->get("subtract_non_normed")=="true"){
//cout<<__LINE__<<endl;
for (int i=1; i<num_hists; i++){
//cout<<__LINE__<<endl;
if( ! hist_nums_for_norm.Contains(to_string(i))){
//cout<<__LINE__<<endl;
cout<<hist_labels[i]<<": subtracting "<<hists[i]->GetBinContent(1)<<endl;
clonedPrimary_norm->Add(hists[i], -1); //subtract
}
}
zeroNegatives(clonedPrimary_norm);
}
//cout<<__LINE__<<endl;
//double numEventsData; -- Now made more global, defined above
double scaleFactor;
if (conf->get("norm_0_50") == "true")
{
numEventsData = clonedPrimary_norm->Integral(clonedPrimary_norm->FindBin(0),clonedPrimary_norm->FindBin(49.9));
numEventsMC = clonedBG_norm->IntegralAndError(clonedBG_norm->FindBin(0),clonedBG_norm->FindBin(49.9), errEventsMC);
norm_bin = -1;
}
else if (conf->get("norm_50_100") == "true"){
numEventsData = clonedPrimary_norm->Integral(clonedPrimary_norm->FindBin(50),clonedPrimary_norm->FindBin(99.9));
numEventsMC = clonedBG_norm->IntegralAndError(clonedBG_norm->FindBin(50),clonedBG_norm->FindBin(99.9), errEventsMC);
norm_bin = 0;
}
else{
numEventsData = clonedPrimary_norm->Integral(0,-1);
numEventsMC = clonedBG_norm->IntegralAndError(0,-1, errEventsMC);
norm_bin = -1;
}
//cout<<__LINE__<<endl;
cout<<"Num Events Primary: "<<numEventsData<<endl;
cout<<"Num Events BG: "<<numEventsMC<<endl;
//rescale everything to scale factor
scaleFactor = ((double) numEventsData/numEventsMC);
cout<<"Applying scale factor "<<scaleFactor<<" to MET templates hist."<<endl;
for (int i = 1; i<num_hists; i++){
if (hist_nums_for_norm.Contains(to_string(i)) || hist_nums_for_norm == "" ){
cout<<i<<" "<<hist_labels[i]<<" SF: "<<scaleFactor<<endl;
hists[i]->Scale(scaleFactor); //if hist is marked for norm or no hists marked for norms.
cout<<hist_labels[i]<<" count: "<<hists[i]->GetBinContent(1)<<endl;
}
}
}
cout<<"===========================================\nNorm\n===========================================\n";
for (int i = 0; i<num_hists; i++){
cout<<hist_labels[i]<<" Integral bin 0 to bin 100 Content: "<<hists[i]->Integral(hists[i]->FindBin(0),hists[i]->FindBin(100))<<endl;
}
//Create sum of background samples
TH1D *bg_sum = (TH1D*) hists[BG_sum_from]->Clone("bg_sum_"+plot_name);
bg_sum->SetTitle("Sum of background samples");
//cout<<__LINE__<<endl;
for (int i=BG_sum_from+1; i<num_hists; i++){
bg_sum->Add(hists[i]);
}
//cout<<__LINE__<<endl;
for (int i = 0; i<num_hists; i++){
cout<<hist_labels[i]<<": after-reweight "<<hists[i]->GetBinContent(1)<<endl;
}
delete clonedPrimary_norm;
delete clonedBG_norm;
//cout<<__LINE__<<endl;
//===========================
// SET MC COLORS
//===========================
//cout<<__LINE__<<endl;
for (int i = 1; i<num_hists; i++){
//cout<<__LINE__<<endl;
hists[i]->SetFillColor(ROOT_COLOR_PALATE[(i-1) % ROOT_COLOR_PALATE.size()]);
//cout<<__LINE__<<endl;
hists[i]->SetFillStyle(1001);
}
//cout<<__LINE__<<endl;
hists[0]->SetMarkerStyle(20);
//cout<<__LINE__<<endl;
//===========================
// Find Plot Maxima
//===========================
//cout<<__LINE__<<endl;
double ymax = 0;
double ymin = 0;
TH1D* clonedBG = (TH1D*) bg_sum->Clone("clonedBG_forReweight_"+plot_name);
TH1D* clonedPrimary = (TH1D*) hists[0]->Clone("clonedPrimary_forReweight_"+plot_name);
//cout<<__LINE__<<endl;
clonedPrimary->GetXaxis()->SetRangeUser(xmin, xmax);
clonedBG->GetXaxis()->SetRangeUser(xmin,xmax);
//cout<<__LINE__<<endl;
if (conf->get("ymax") != ""){
ymax = stod(conf->get("ymax"));
}
else{
if (clonedBG->GetMaximum() < clonedPrimary->GetMaximum()){
ymax = 1.2*clonedPrimary->GetMaximum();
}
else {
ymax = 1.2*clonedBG->GetMaximum();
}
}
if (conf->get("logy") == "true"){
ymax *= 10;
ymin = 0.1;
}
if (conf->get("ymin") != ""){
ymin = stod(conf->get("ymin"));
}
//cout<<__LINE__<<endl;
cout<<"Primary Max: "<< clonedPrimary->GetMaximum() << " Secondary Max: "<< clonedBG->GetMaximum() <<endl;
cout<<"Proper plot maximum set to "<<ymax<<endl;
delete clonedBG;
delete clonedPrimary;
//cout<<__LINE__<<endl;
TH2F* h_axes = new TH2F(Form("%s_axes",plot_name.Data()),plot_title,hists[0]->GetNbinsX(),xmin,xmax,1000,ymin,ymax);
if (conf->get("relabel_x_axis") == "true"){
TString bin_label;
for (int i = (int) xmin; i <= (int) xmax; i++)
{
bin_label=hists[0]->GetXaxis()->GetBinLabel(hists[0]->FindBin(i));
h_axes->GetXaxis()->SetBinLabel(h_axes->FindBin(i), bin_label);
}
//h_axes->GetXaxis()->LabelsOption("v");
h_axes->GetXaxis()->SetLabelSize(.015);
}
//-----------------------
// AXES FIX
//-----------------------
cout<<"Setting axis names"<<endl;
h_axes->GetXaxis()->SetTitle(xlabel);
h_axes->GetYaxis()->SetTitle(ylabel);
//cout<<__LINE__<<endl;
gStyle->SetTitleW(0.6);
gStyle->SetTitleH(0.06);
gStyle->SetTitleFont(12);
//===========================
// Print Closure Stats
//===========================
cout<<"===========================================\nPrint Stats\n===========================================\n";
for (int i = 0; i<num_hists; i++){
cout<<hist_labels[i]<<" Integral bin 0 to bin 100 Content: "<<hists[i]->Integral(hists[i]->FindBin(0),hists[i]->FindBin(100))<<endl;
}
if (conf->get("print_stats") == "true"){
vector<pair<double,double>> stats_bins;
int j = 0;
//cout<<__LINE__<<endl;
while (conf->get("stats_"+to_string(j)+"_low_val") != "" ){
stats_bins.push_back(make_pair(stod(conf->get("stats_"+to_string(j)+"_low_val")),stod(conf->get("stats_"+to_string(j)+"_high_val"))));
j++;
}
//cout<<__LINE__<<endl;
if(conf->get("simple_errors") == "true"){
vector<vector<pair<double, double>>> stats; //holds a pair of count error for each sample, and the bg sum
double count, error;
vector<pair<double,double>> stat_row;
//cout<<__LINE__<<endl;
//Loop over the stats bins
// Build Table ========================================================
for(int i = 0 ; i < (int)hists.size(); i++){
for(int st_bin=0; st_bin < (int) stats_bins.size(); st_bin++){
//cout<<__LINE__<<endl;
count = hists[i]->IntegralAndError(hists[i]->FindBin(stats_bins[st_bin].first), hists[i]->FindBin(stats_bins[st_bin].second - 0.000001), error);
stat_row.push_back(make_pair(count,error));
}
stats.push_back(stat_row);
stat_row.clear();
}
if (conf->get("templates_closure") == "true"){
//Tack on Ratio row
double zjets_count, zjets_err;
for(int st_bin=0; st_bin < (int) stats_bins.size(); st_bin++){
count = bg_sum->IntegralAndError(bg_sum->FindBin(stats_bins[st_bin].first), bg_sum->FindBin(stats_bins[st_bin].second - 0.000001), error);
zjets_count = stats[0][st_bin].first;
zjets_err = stats[0][st_bin].second;
cout<<"zjets_count: "<<zjets_count<<" zjets_err: "<<zjets_err<<endl;
cout<<"count: "<<count<<"err: "<<error<<endl;
error = (1./count)*(sqrt( pow(zjets_err, 2) + pow(zjets_count * error / count, 2) ) );
count = zjets_count / count;
stat_row.push_back(make_pair(count,error));
}
stats.push_back(stat_row);
}
else {
//Tack on BG sum row
for(int st_bin=0; st_bin < (int) stats_bins.size(); st_bin++){
count = bg_sum->IntegralAndError(bg_sum->FindBin(stats_bins[st_bin].first), bg_sum->FindBin(stats_bins[st_bin].second - 0.000001 ), error);
stat_row.push_back(make_pair(count,error));
}
stats.push_back(stat_row);
}
// End Table Building ==================================================
// Print Table =========================================================
CTable table;
table.setPrecision(2);
//Set Column Labels
//cout<<__LINE__<<endl;
table.setTitle(Form("Efficiencies for %s",plot_name.Data()));
table.useTitle();
for (int st_bin=0; st_bin < (int) stats_bins.size(); st_bin++){
//cout<<__LINE__<<endl;
table.setColLabel(Form("%.3f-%.3f",stats_bins[st_bin].first, stats_bins[st_bin].second), st_bin);
}
//cout<<__LINE__<<endl;
if (conf->get("templates_closure") == "true"){
//Output Rows for samples
for(int row = 0; row <= (int) hists.size(); row++ ){
if (row == hists.size()){
table.setRowLabel("Ratio", hists.size());
}
else{
table.setRowLabel(hist_labels[row], row);
}
for(int col=0; col < (int) stats_bins.size(); col++){
//cout<<__LINE__<<endl;
table.setCell(Form("%.2f+/-%.2f", stats[row][col].first, stats[row][col].second), row, col);
}
}
}
else{
//Output Rows for samples
for(int row = 0; row <= (int) hists.size(); row++ ){
if (row == hists.size()){
table.setRowLabel("Sum of BG", hists.size());
}
else{
table.setRowLabel(hist_labels[row], row);
}
for(int col=0; col < (int) stats_bins.size(); col++){
//cout<<__LINE__<<endl;
table.setCell(Form("%.2f+/-%.2f; Eff: %.2f", stats[row][col].first, stats[row][col].second, stats[row][col].first/stats[row][0].first), row, col);
}
}
}
//cout<<__LINE__<<endl;
table.print();
TString conf_id = conf->get("conf_path");
conf_id.ReplaceAll("//","/");
conf_id.ReplaceAll("/","_");
conf_id = conf_id(8, conf_id.Last('.')-8);
table.saveTex(Form("outputs/efficiency_table_%s.tex", conf_id.Data()));
}
}
cout<<"===========================================\nUpdate Overflow\n===========================================\n";
//Done stats after so that we don't double count these since they are not deleted from final bin.
for (int i = 0; i<num_hists; i++){
cout<<hist_labels[i]<<" Integral bin 0 to bin 100 Content: "<<hists[i]->Integral(hists[i]->FindBin(0),hists[i]->FindBin(100))<<endl;
}
//----------------------
// ADD OVERFLOW BIN
//----------------------
//cout<<__LINE__<<endl;
if (conf->get("overflow")=="true"){
cout<<"Plot tagged for overflow bin, building..."<<endl;
updateOverUnderflow(bg_sum, xmax);
for (int i = 0; i<num_hists; i++){
updateOverUnderflow(hists[i], xmax);
}
/*double n_bins = hists[0]->GetNbinsX();
double overflow, max;
//cout<<__LINE__<<endl;
for (int i = 0; i<num_hists; i++){
overflow = hists[i]->GetBinContent(n_bins + 1);
max = hists[i]->Integral(hists[i]->FindBin(xmax-.001), n_bins);
hists[i]->SetBinContent(hists[i]->FindBin(xmax-.001), max+overflow);
}
overflow = bg_sum->GetBinContent(n_bins + 1);
max = bg_sum->Integral(bg_sum->FindBin(xmax-.001), n_bins);
bg_sum->SetBinContent(bg_sum->FindBin(xmax-.001), max+overflow);*/
//cout<<__LINE__<<endl;
}
plotpad->SetLeftMargin(0.15);
h_axes->GetYaxis()->SetTitleOffset(1.3);
h_axes->GetYaxis()->SetTitleSize(0.05);
h_axes->GetYaxis()->SetLabelSize(0.04);
h_axes->GetXaxis()->SetTitleSize(0.05);
h_axes->GetXaxis()->SetLabelSize(0.04);
//cout<<__LINE__<<endl;
cout<<"Drawing histograms"<<endl;
h_axes->Draw();
//===========================
// MAKE STACK
//===========================
//Add all the background hists to a stack.
THStack * stack = new THStack(("stack_"+conf->get("Name")).c_str(), conf->get("title").c_str());
//cout<<__LINE__<<endl;
vector<pair<TH1D*, TString>> hists_labeled;
for (int i=1; i<num_hists; i++)
{
hists_labeled.push_back(make_pair(hists[i], hist_labels[i]));
}
cout<<"Sorting by total count"<<endl;
sort(hists_labeled.begin(), hists_labeled.end(), LabeledHistSort);
cout<<"Sorted, Adding to Stack"<<endl;
//sort(hists.begin()+1, hists.end(), TH1DIntegralSort);
for (int i=0; i< (int)hists_labeled.size(); i++)
{
stack->Add(hists_labeled[i].first);
}
stack->Draw("HIST SAME");
cout<<"Stack Drawn"<<endl;
if (conf->get("blindAfter") != ""){
blindAfter(hists[0], stod(conf->get("blindAfter")));
}
TGraphAsymmErrors *bg_err = new TGraphAsymmErrors(bg_sum);
if ((conf->get("print_stats") == "true") && (conf->get("simple_errors") != "true")){
prediction_errors->SetFillStyle(3244);
prediction_errors->SetFillColor(kGray+3);
prediction_errors->Draw("SAME 2");
}
else if(conf->get("draw_bg_errs") != "false"){
cout<<"Drawing BG hatch bands"<<endl;
bg_err->SetFillStyle(3244);
bg_err->SetFillColor(kGray+3);
bg_err->Draw("SAME 2");
}
if (conf->get("print_stats") == "true" && conf->get("simple_errors") != "true"){
hists[0]->SetMarkerSize(2.5);
}
else{
hists[0]->SetMarkerSize(1.5);
}
hists[0]->Draw("same e0 x0 e1 p0");
plotpad->RedrawAxis();
//cout<<__LINE__<<endl;
//===========================
// BUILD LEGEND
//===========================
cout<<"Building Legend"<<endl;
TLegend *l1;
if (conf->get("small_legend") == "true"){
l1 = new TLegend(0.78, 0.78, 0.93, 0.93);
}
else{
/*cout<<"UtoPixel(0.65): "<<gPad->UtoPixel(.65)<<endl;
cout<<"PixelToX(..): "<<gPad->PixeltoX(gPad->UtoPixel(.65))<<endl;
double x_under_legend = gPad->PixeltoX(gPad->UtoPixel(.65));
double max_count_under_legend = bg_sum->GetBinContent(bg_sum->FindBin(x_under_legend));
cout<<"x under legend: "<<x_under_legend<<endl;
cout<<"max count under legend: "<<max_count_under_legend<<endl;*/
l1 = new TLegend(0.65, 0.6, 0.93, 0.93);
}
l1->SetLineColor(kWhite);
l1->SetShadowColor(kWhite);
l1->SetFillColor(kWhite);
l1->SetTextSize(.03);
//cout<<__LINE__<<endl;
l1->AddEntry(hists[0], hist_labels[0], "pe");
/* //Put objects in legend with the same order as the they go into the stack
for (int i = hists_labeled.size()-1; i>=0; i--){
l1->AddEntry(hists_labeled[i].first, hists_labeled[i].second, "f");
}*/
//Put objects in legend in the order they are written in the config
for (int i = hists.size()-1; i>=1; i--){
l1->AddEntry(hists[i], hist_labels[i], "f");
}
l1->Draw("same");
//--------------------------
// Fill in Residual Plot
//--------------------------
cout<<"Getting ready for residual plots"<<endl;
fullpad->cd();
TPad *ratiopad = new TPad("ratiopad", "ratiopad" ,0.,0.,1,0.21);
ratiopad->SetTopMargin(0.05);
ratiopad->SetLeftMargin(0.15);
ratiopad->SetBottomMargin(0.1);
ratiopad->SetRightMargin(0.05);
ratiopad->SetGridy(); // doesn't actually appear for some reason..
ratiopad->Draw();
ratiopad->cd();
//cout<<__LINE__<<endl;
TH1D* residual = (TH1D*) hists[0]->Clone("residual");
residual->Divide(bg_sum);
//cout<<__LINE__<<endl;
//cout<<"Fixing error bars"<<endl;
//for (int count=1; count<=mc_sum->GetNbinsX(); count++){
// double relative_error = (mc_sum->GetBinError(count))/ (mc_sum->GetBinContent(count));
// residual->SetBinError(count, residual->GetBinContent(count)*relative_error);
//}
cout<<"Building axes"<<endl;
TH1D* h_axis_ratio = new TH1D(Form("%s_residual_axes",plot_name.Data()),"",residual->GetNbinsX(),xmin,xmax);
//cout<<__LINE__<<endl;
h_axis_ratio->GetYaxis()->SetTitleOffset(0.33);
h_axis_ratio->GetYaxis()->SetTitleSize(0.16);
//h_axis_ratio->GetYaxis()->SetTitleFont(12);
h_axis_ratio->GetYaxis()->SetNdivisions(5);
h_axis_ratio->GetYaxis()->SetLabelSize(0.15);
//h_axis_ratio->GetYaxis()->SetRangeUser(0.5,1.5);
if(residual->GetMaximum() > 3 && residual->GetMaximum() <= 4 ){
h_axis_ratio->GetYaxis()->SetRangeUser(0.001,4.0);
}
else if(residual->GetMaximum() > 2 && residual->GetMaximum() <= 3 ){
h_axis_ratio->GetYaxis()->SetRangeUser(0.001,3.0);
}
else{
h_axis_ratio->GetYaxis()->SetRangeUser(0.001,2.0);
}
if(conf->get("ratio_yaxis") != ""){
h_axis_ratio->GetYaxis()->SetTitle(parseLatex(conf->get("ratio_yaxis")));
}
else{
h_axis_ratio->GetYaxis()->SetTitle("#frac{Signal}{Prediction}");
}
h_axis_ratio->GetXaxis()->SetTickLength(0.07);
h_axis_ratio->GetXaxis()->SetTitleSize(0.005);
h_axis_ratio->GetXaxis()->SetLabelSize(0.);
h_axis_ratio->GetYaxis()->CenterTitle();
//cout<<__LINE__<<endl;
TLine* line1 = new TLine(xmin,1,xmax,1);
line1->SetLineStyle(2);
if (conf->get("print_stats") == "true" && conf->get("simple_errors") != "true"){
residual->SetMarkerSize(2.5);
}
else{
residual->SetMarkerSize(1.5);
}
cout<<"Drawing ratio plot"<<endl;
h_axis_ratio->Draw("axis");
//cout<<__LINE__<<endl;
line1->Draw("same");
//cout<<__LINE__<<endl;
residual->Draw("same e0 x0 e1 p0");
//cout<<__LINE__<<endl;
c->Update();
//cout<<__LINE__<<endl;
c->cd();
//cout<<__LINE__<<endl;
//Draw luminosity and CMS tag
if (conf->get("luminosity_fb") != ""){
plotpad->cd();
drawCMSLatex(stod(conf->get("luminosity_fb")));
}
if (conf->get("SR") != ""){
plotpad->cd();
drawSRText(conf->get("SR"), bg_sum->GetMaximum(), xmin);
}
//cout<<__LINE__<<endl;
cout<<"Saving..."<<endl;
c->SaveAs(save_dir+plot_name+TString(".pdf"));
c->SaveAs(save_dir+plot_name+TString(".png"));
//c->SaveAs(save_dir+plot_name+TString(".root"));
//c->SaveAs(save_dir+plot_name+TString(".C"));
//cout<<__LINE__<<endl;
cout<<"Cleaning up plot variables"<<endl;
delete l1;
hists_labeled.clear();
hists.clear();
hist_names.clear();
hist_labels.clear();
delete residual;
delete ratiopad;
delete plotpad;
delete fullpad;
delete bg_err;
delete c;
//cout<<__LINE__<<endl;
for (int i = 0; i<num_hists; i++){
hist_files[i]->Close();
}
hist_files.clear();
//cout<<__LINE__<<endl;
return errors;
}
TString drawArbitraryNumber(ConfigParser *conf){
// This method expects conf to have a plot config loaded in already.
//In the conf, we expect there to be hist names of the form file_N_path,
//hist_n_name, starting with 0 for the primary histogram, which is normally
//going to be the data events in our signal region. The rest of the hists, starting
//from 1, are added to a THStack which is normalized to hist_0 in the bin 0-50.
//num_hists should be the number of the number of histograms in the plot.
TString errors="";
int num_hists=stoi(conf->get("num_hists"));
int BG_sum_from=(conf->get("BG_sum_from") != "") ? stoi(conf->get("BG_sum_from")) : 0;
if (num_hists < 2){
return TString("Less than Two hists can not be turned into a stack plot, please call drawSingleTH1 (replace config PLOT_TYPE with Single)");
}
//Add files from which to obtain histos
TString default_hist_dir = getDefaultHistDir(conf);
vector<TFile*> hist_files (num_hists);
for (int i = 0; i<num_hists; i++){
TString sample_loc = "";
if (conf->get("file_"+to_string(i)+"_path") != ""){
sample_loc = TString(conf->get("file_"+to_string(i)+"_path"));
}
else{
sample_loc = default_hist_dir+conf->get("sample_"+to_string(i))+".root";
}
hist_files[i]=new TFile(sample_loc);
}
cout << "Found files "<<endl;
TString plot_name = conf->get("plot_name");
double bin_size;
if (conf->get("bin_size") != ""){
bin_size = stod(conf->get("bin_size"));
}
else{
bin_size=1;
}
//Get name of hist to read from file
vector<TString> hist_names (num_hists);
for (int i = 0; i<num_hists; i++){
if (conf->get("hist_"+to_string(i)+"_name") != ""){
hist_names[i]=conf->get("hist_"+to_string(i)+"_name");
}
else{
hist_names[i]= conf->get("hist_0_name");
}
}
//Get labels for TLegend
vector<TString> hist_labels (num_hists);
for (int i = 0; i<num_hists; i++){
hist_labels[i]=parseLatex(conf->get("hist_"+to_string(i)+"_label"));
}
cout<<"Hist names set"<<endl;
TString xlabel=parseLatex(conf->get("xlabel"));
TString ylabel=parseLatex(conf->get("ylabel"));
TString save_dir=(conf->get("save_dir") != "") ? conf->get("save_dir") : getOutputDir(conf, "plot");
TString plot_title=parseLatex(conf->get("title"));
cout << "Making Plots for: "<<plot_name<<endl;
vector<TH1D*> hists (num_hists);
for (int i = 0; i<num_hists; i++){
hists[i] = (TH1D*) ((TH1D*) hist_files[i]->Get(hist_names[i]))->Clone("hist_"+to_string(i)+"_"+plot_name);
cout<<hist_names[i]<<" found in "<<hist_files[i]->GetName()<<endl;
cout<<hist_labels[i]<<" Integral bin 0 to bin 100 Content: "<<hists[i]->Integral(0,100)<<endl;
}
cout << "Histograms pulled from files, adding draw options"<<endl;
//cout<<__LINE__<<endl;
if (conf->get("relabel_x_axis") == "true"){
hists[0]->LabelsDeflate();
}
double xmax = (conf->get("xmax") != "" ) ? stod(conf->get("xmax")) : hists[0]->GetNbinsX();
double xmin = (conf->get("xmin") != "" ) ? stod(conf->get("xmin")) : 0;
//============================================
// Draw Data-MC Plots
//============================================
//cout<<__LINE__<<endl;
TCanvas * c = new TCanvas("c","",2000,2000);
c->cd();
gPad->SetRightMargin(0.05);
gPad->Modified();
gStyle->SetOptStat(kFALSE);
TPad *fullpad = new TPad("fullpad", "fullpad", 0,0,1,1);
//cout<<__LINE__<<endl;
fullpad->Draw();
fullpad->cd();
//cout<<__LINE__<<endl;
fullpad->SetRightMargin(0.05);
if (conf->get("ExtraRightMargin") == "true")
{
fullpad->SetRightMargin(0.08);
}
fullpad->SetBottomMargin(0.12);
//cout<<__LINE__<<endl;
fullpad->Draw();
fullpad->cd();
//cout<<__LINE__<<endl;
if (conf->get("logy") == "true")
{
cout<<"Plot tagged for log y-axis"<<endl;
fullpad->SetLogy();
}
//cout<<__LINE__<<endl;
if (conf->get("bin_size") != ""){
for (int i = 0; i<num_hists; i++){
hists[i]->Rebin(bin_size);
}
}
else if (conf->get("binning") != ""){
cout<<"Rebinning plots with variable ranges"<<endl;
vector<double> binning = parseVector(conf->get("binning"));
for (int i = 0; i<num_hists; i++){
hists[i] = (TH1D*) hists[i]->Rebin(binning.size()-1, TString(hist_names[i]+"_rebin"), &binning[0]);
}