-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plot.C
623 lines (509 loc) · 21.1 KB
/
Plot.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
//////////////////////////////////////////////////INSTRUCTIONS///////////////////////////////////////////////////////
//
//This macro loop over all the (nested)(sub)TDirectories inside a root file which name is passed as argument,
//when a directory with a TH1 inside is found the function plot_hist() plots the histograms found in the directory,
//then the macro will continue to loop over the other directories
//
// Arguments: filename, (mandatory, name of the root file with the histogram)
// channel name, (optional, passed as a Latex string e.g. "#mu#tau#", default: "#mu#tau#"")
// output directory name (optional, if passed all the output directories will be put in a new directory with whis name)
//
//WARNING: the directories with the TH1s have to be the most nested ones, i.e. with no other subdirectories.
//
//Auxiliary functions definitions are at the bottom
//
//OUTPUT: the macro will create directories named "output_"+ /name of the directories with inside the variables
//subdirectories/ and then put the plots and the latex tables inside these directories.
//There is an overwrite control with the following options:
//1) Overwrite file
//2) Overwrite ALL
//3) Rename old file
//4) Rename ALL
//5) Abort
//This question is asked to the user only when the macro already find an output file.
//
//Author: Alberto Bragagnolo [email protected]
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "HttStylesNew.cc"
#include "CMS_lumi.C"
#include <iostream>
#include <iomanip>
int g_over_flag;
bool g_over_all_flag = kFALSE;
TString g_ch = "#mu#tau";
TString g_dir_name = "";
TTimeStamp *time_stamp = new TTimeStamp();
void SetAxis (TString var, TString *xtitle, TString *UdM);
void SetBinErrorZero(TH1 *h);
float CountError (TH1 *h, float errLumi, float errSist);
void plot_hist();
void directory_loop(TDirectory *target);
void Plot(
TString DataFile = "", //file name
TString ch = "#mu#tau",
TString dir_name_input = ""
) {
TFile *file = new TFile(DataFile); //opening the TFile
if (file->IsZombie()) {
std::cout << "File "<<DataFile<<" is Zombie" << endl ;
exit(1);
}
g_ch = ch;
g_dir_name = dir_name_input;
if(g_dir_name != "") gSystem->mkdir(g_dir_name, kTRUE);
directory_loop(file);
}
void directory_loop(TDirectory *target) {
target->cd();
TDirectory *current_sourcedir = gDirectory; //inizializing the key and the iter
TIter nextkey( current_sourcedir->GetListOfKeys() );
TKey *key, *oldkey=0;
while ( (key = (TKey*)nextkey())) {
if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue;
TObject *obj = key->ReadObj();
if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
//if a TH1 is found the actual plotting function is called
plot_hist();
break;
} else if ( obj->IsA()->InheritsFrom( TDirectory::Class() ) ) {
//reiteration over the subdirectory
directory_loop( (TDirectory*)obj );
}
}
}
////////////MAIN PLOTTING FUNCTION/////////////
void plot_hist() {
std::cout << " enter plottting function" << std::endl;
TString Variable = gDirectory->GetName();
TString path( (char*)strstr( gDirectory->GetPath(), ":" ) );
path.Remove( 0, 2 );
TString dir_name = path( 0 , path.Last('/'));
SetStyle(); gStyle->SetOptTitle(0);
TH1::SetDefaultSumw2(kTRUE);
// TString Variable = DataFile(DataFile.Last('-')+1, DataFile.Last('.')-DataFile.Last('-')-1);
TH1 * histData = (TH1*)gDirectory->Get("data_obs");
histData->SetStats(0);
//binning and axis titles
int nBins = histData ->GetNbinsX(); //-underflowbin -overflowbin
int xmin = histData->GetXaxis()->GetXmin();
int xmax = histData->GetXaxis()->GetXmax();
float binWidth = (xmax-xmin)/nBins;
TString xtitle = "x title"; TString ytitle = "y title"; TString UdM = "";
// SetAxis(Variable, &xtitle, &UdM);
ytitle = Form("Events/ %.2f ", binWidth); ytitle += UdM;
TH1 * ZTT = (TH1*)gDirectory->Get("ZTT");
TH1 * ZL = (TH1*)gDirectory->Get("ZL");
TH1 * ZJ = (TH1*)gDirectory->Get("ZJ");
//TH1 * ZLL = (TH1*)gDirectory->Get("ZLL");
TH1 * TTT = (TH1*)gDirectory->Get("TTT");
TH1 * TTJ = (TH1*)gDirectory->Get("TTJ");
//TH1* TT = (TH1*)gDirectory->Get("TT");
TH1 * W = (TH1*)gDirectory->Get("W");
TH1 * VV = (TH1*)gDirectory->Get("VV");
TH1 * EWKZ = (TH1*)gDirectory->Get("EWKZ");
TH1 * QCD = (TH1*)gDirectory->Get("QCD");
TH1 * ggH125 = (TH1*)gDirectory->Get("ggH125");
TH1 * qqH125 = (TH1*)gDirectory->Get("qqH125");
TH1 * WH125 = (TH1*)gDirectory->Get("WH125");
TH1 * ZH125 = (TH1*)gDirectory->Get("ZH125");
ZL->Add(ZJ);
//ZLL->Add(ZJ);
TH1 * TT = (TH1*)TTT->Clone("TT");
TT->Add(TTJ);
// merging all electroweak process to the W histogram
W->Add(VV);
W->Add(EWKZ);
//number of events for samples
double nQCD = QCD->Integral();
// double nVV = VV->Integral();
double nW = W->Integral();
double nTT = TT->Integral();
double nZL = ZL->Integral();
double nZTT = ZTT->Integral();
//errors values
/*float errLumi = 0.062;
float errQCD = 0.3;
//float errVV = 0.2;
float errW = 0.15;
float errTT = 0.1;
float errZL = 0.5;
float errZTT = 0.08; // 8% on tauID */
float errLumi = 0.0;
float errQCD = 0.0;
//float errVV = 0.2;
float errW = 0.0;
float errTT = 0.0;
float errZL = 0.0;
float errZTT = 0.0;
TH1 * dummy = (TH1*)ZTT->Clone("dummy");
for (int iB=1; iB<=nBins; ++iB) {
float eQCD = errQCD*QCD->GetBinContent(iB);
//float eVV = errVV*VV->GetBinContent(iB);
float eW = errW*W->GetBinContent(iB);
float eTT = errTT*TT->GetBinContent(iB);
float eZL = errZL*ZL->GetBinContent(iB);
float eZTT = errZTT*ZTT->GetBinContent(iB);
//float err2 = eQCD*eQCD + eVV*eVV + eW*eW + eTT*eTT + eZL*eZL + eZTT*eZTT;
float err2 = eQCD*eQCD + eW*eW + eTT*eTT + eZL*eZL + eZTT*eZTT;
float errTot = TMath::Sqrt(err2);
dummy->SetBinError(iB,errTot);
}
float etotQCD = CountError(QCD, errLumi, errQCD);
//float etotVV = CountError(VV, errLumi, errVV);
float etotW = CountError(W, errLumi, errW);
float etotTT = CountError(TT, errLumi, errTT);
float etotZL = CountError(ZL, errLumi, errZL);
float etotZTT = CountError(ZTT, errLumi, errZTT);
//hist sum
//VV->Add(VV,QCD);
//W->Add(W,VV);
W->Add(W,QCD);
TT->Add(TT,W);
ZL->Add(ZL,TT);
ZTT->Add(ZTT,ZL);
TH1 * bkgdErr = (TH1*)ZTT->Clone("bkgdErr");
bkgdErr->SetFillStyle(3013);
bkgdErr->SetFillColor(1);
bkgdErr->SetMarkerStyle(21);
bkgdErr->SetMarkerSize(0);
for (int iB=1; iB<=nBins; ++iB) {
float eStat = bkgdErr->GetBinError(iB);
float X = bkgdErr->GetBinContent(iB);
float eLumi = errLumi * X;
float eBkg = dummy->GetBinError(iB);
float Err = TMath::Sqrt(eStat*eStat+eLumi*eLumi+eBkg*eBkg);
bkgdErr->SetBinError(iB, Err);
}
SetBinErrorZero(QCD);
//SetBinErrorZero(VV);
SetBinErrorZero(W);
SetBinErrorZero(TT);
SetBinErrorZero(ZL);
SetBinErrorZero(ZTT);
SetBinErrorZero(ggH125);
SetBinErrorZero(qqH125);
SetBinErrorZero(WH125);
SetBinErrorZero(ZH125);
//graphics
InitData(histData);
InitHist(QCD,"","",TColor::GetColor("#FFCCFF"),1001);
InitHist(W,"","",TColor::GetColor("#DE5A6A"),1001);
InitHist(TT,"","",TColor::GetColor("#9999CC"),1001);
//InitHist(VV,"","",TColor::GetColor("#6F2D35"),1001);
InitHist(ZL,"","",TColor::GetColor("#4496C8"),1001);
InitHist(ZTT,"","",TColor::GetColor("#FFCC66"),1001);
// signal histogram, scaled by a factor
// adding all signal contributions
TH1 * HTT = (TH1*)ggH125->Clone("HTT");
HTT->Add(qqH125);
HTT->Add(WH125);
HTT->Add(ZH125);
HTT->SetLineColor(2);
HTT->SetLineWidth(3);
float signalScaleFactor = 10.;
HTT->Scale(signalScaleFactor);
std::cout << " after signal histograms" << std::endl;
// histData->GetXaxis()->SetTitle(xtitle);
// histData->GetYaxis()->SetTitle(ytitle);
histData->GetYaxis()->SetTitleOffset(1.3);
histData->GetYaxis()->SetTitleSize(0.06);
float yUpper = histData->GetMaximum();
if(histData->GetMaximum() < ZTT->GetMaximum()) yUpper = ZTT->GetMaximum();
histData->GetYaxis()->SetRangeUser(0,1.2*yUpper);
histData->SetMarkerSize(1.2);
histData->GetXaxis()->SetLabelSize(0);
histData->GetYaxis()->SetLabelSize(0.06);
histData->GetYaxis()->SetTitleOffset(1.6);
TCanvas * c1 = new TCanvas("c1", "", 700, 800);
TPad *upper = new TPad("upper", "pad",0,0.31,1,1);
upper->Draw();
upper->cd();
upper->SetFillColor(0);
upper->SetBorderMode(0);
upper->SetBorderSize(10);
upper->SetTickx(1);
upper->SetTicky(1); upper->SetLeftMargin(0.20);
upper->SetRightMargin(0.05);
upper->SetBottomMargin(0.02);
upper->SetFrameFillStyle(0);
upper->SetFrameLineStyle(0);
upper->SetFrameLineWidth(2);
upper->SetFrameBorderMode(0);
upper->SetFrameBorderSize(10);
upper->SetFrameFillStyle(0);
upper->SetFrameLineStyle(0);
upper->SetFrameLineWidth(2);
upper->SetFrameBorderMode(0);
upper->SetFrameBorderSize(10);
//DRAWING
histData->Draw("e1");
ZTT->Draw("sameh");
ZL->Draw("sameh");
TT->Draw("sameh");
W->Draw("sameh");
//VV->Draw("sameh");
QCD->Draw("sameh");
histData->Draw("e1same");
bkgdErr->Draw("e2same");
HTT->Draw("sameh");
//CHI2
int dof=0;
float chi2 = 0;
for (int iB=1; iB<=nBins; ++iB) {
float xData = histData->GetBinContent(iB);
float xMC = ZTT->GetBinContent(iB);
float xErr = bkgdErr->GetBinError(iB);
if (xMC>1e-1) {
dof++;
float diff2 = pow(xData-xMC,2);
chi2 += diff2/(xErr*xErr);
}
}
//LEGEND
TLegend * leg = new TLegend(0.6,0.4,0.82,0.78);
SetLegendStyle(leg);
leg->SetTextSize(0.05);
leg->AddEntry(histData,"Observed","lp");
leg->AddEntry(ZTT,"Z#rightarrow #tau#tau","f");
leg->AddEntry(ZL,"DY others","f");
leg->AddEntry(TT,"t#bar{t}","f");
leg->AddEntry(W,"Electroweak","f");
//leg->AddEntry(VV,"VV+single top","f");
leg->AddEntry(QCD,"QCD","f");
leg->AddEntry(HTT,Form("SM H(125) x %.0f",signalScaleFactor),"f");
writeExtraText = false;
//extraText = "Work in Progress ";
CMS_lumi(upper,4,33);
plotchannel(g_ch);
TLatex t;
t.SetNDC();
// Drawing legend and show chi2
bool plotLegend = false;
bool plotChi2 = false;
bool LogYscale = true;
/*if ((Variable != "eta1")&&(Variable != "eta2")) {
leg->Draw();
t.DrawLatex(.6,.3, Form("#bf{#chi^{2}/d.o.f. = %.2f}", chi2/dof) );
}*/
if (plotLegend) leg->Draw();
if (plotChi2) t.DrawLatex(.6,.3, Form("#bf{#chi^{2}/d.o.f. = %.2f}", chi2/dof) );
upper->Draw("SAME");
if(LogYscale) {histData->SetMinimum(10e-3); upper->SetLogy();}
upper->RedrawAxis();
upper->Modified();
upper->Update();
c1->cd();
//RATIO
TH1 * ratioH = (TH1*)histData->Clone("ratioH");
TH1 * ratioErrH = (TH1*)bkgdErr->Clone("ratioErrH");
ratioH->SetMarkerColor(1);
ratioH->SetMarkerStyle(20);
ratioH->SetMarkerSize(1.2);
ratioH->SetLineColor(1);
ratioH->GetYaxis()->SetRangeUser(0.4,1.6);
ratioH->GetYaxis()->SetNdivisions(505);
ratioH->GetXaxis()->SetLabelFont(42);
ratioH->GetXaxis()->SetLabelOffset(0.04);
ratioH->GetXaxis()->SetLabelSize(0.14);
ratioH->GetXaxis()->SetTitleSize(0.13);
ratioH->GetXaxis()->SetTitleOffset(1.2);
ratioH->GetYaxis()->SetTitle("obs/exp");
ratioH->GetYaxis()->SetLabelFont(42);
ratioH->GetYaxis()->SetLabelOffset(0.015);
ratioH->GetYaxis()->SetLabelSize(0.13);
ratioH->GetYaxis()->SetTitleSize(0.14);
ratioH->GetYaxis()->SetTitleOffset(0.5);
ratioH->GetXaxis()->SetTickLength(0.07);
ratioH->GetYaxis()->SetTickLength(0.04);
ratioH->GetYaxis()->SetLabelOffset(0.01);
for (int iB=1; iB<=nBins; ++iB) {
float x1 = histData->GetBinContent(iB); float x2 = ZTT->GetBinContent(iB);
ratioErrH->SetBinContent(iB,1.0);
ratioErrH->SetBinError(iB,0.0);
float xBkg = bkgdErr->GetBinContent(iB);
float errBkg = bkgdErr->GetBinError(iB);
if (xBkg>0) {
float relErr = errBkg/xBkg;
ratioErrH->SetBinError(iB,relErr);
}
if (x1>0&&x2>0) {
float e1 = histData->GetBinError(iB);
float ratio = x1/x2;
float eratio = e1/x2;
ratioH->SetBinContent(iB,ratio);
ratioH->SetBinError(iB,eratio);
} else {ratioH->SetBinContent(iB,1000);}
}
lower = new TPad("lower", "pad",0,0,1,0.30);
lower->Draw();
lower->cd();
lower->SetFillColor(0);
lower->SetBorderMode(0);
lower->SetBorderSize(10);
lower->SetGridy();
lower->SetTickx(1);
lower->SetTicky(1);
lower->SetLeftMargin(0.20);
lower->SetRightMargin(0.05);
lower->SetTopMargin(0.026);
lower->SetBottomMargin(0.35);
lower->SetFrameFillStyle(0);
lower->SetFrameLineStyle(0);
lower->SetFrameLineWidth(2);
lower->SetFrameBorderMode(0);
lower->SetFrameBorderSize(10);
lower->SetFrameFillStyle(0);
lower->SetFrameLineStyle(0);
lower->SetFrameLineWidth(2);
lower->SetFrameBorderMode(0);
lower->SetFrameBorderSize(10);
ratioH->Draw("e1");
ratioErrH->Draw("e2same");
lower->Modified();
lower->RedrawAxis();
c1->cd();
c1->Modified();
c1->cd();
c1->SetSelected(c1);
TString plot_name = "plot_" + Variable + ".png";
TString plot_name_copy = plot_name;
TString output_path;
if(g_dir_name != "") {
gSystem->mkdir(g_dir_name + "/" + "output_" + dir_name, kTRUE);
output_path = g_dir_name + "/" + "output_" + dir_name + "/";
} else {
gSystem->mkdir("output_" + dir_name, kTRUE);
output_path = "output_" + dir_name + "/";
}
//controls if outputfile already exists
const char* fileExists = gSystem->FindFile((const char*)output_path, plot_name_copy);
if ( !(fileExists == 0)){
if (g_over_all_flag) {
if(g_over_flag == 2) c1->SaveAs(output_path + plot_name); //if ALL flag = true then always overwrite
if(g_over_flag == 4) {
gSystem->Rename(output_path + plot_name, output_path + "plot_" + Variable + "_"+ Form("%i", time_stamp->GetTime()) +".png" );
c1->SaveAs(output_path + plot_name);
}
} else {
cout<<endl<<"Output file "<<plot_name<<" in path"<<endl<<output_path<<endl<<"already exists."<<endl<<" What do you want to do?"<<endl;
cout<<"1 Overwrite"<<endl<<"2 Overwrite ALL"<<endl<<"3 Rename old file (a timestamp will be added) (latex table will be renamed)"<<endl<<"4 Rename old files ALL"<<endl<<"5 Abort process"<<endl<<endl;
cout<<"Enter your choice: ";
cin>>g_over_flag;
cout<<endl;
switch(g_over_flag){
case(1):
cout<<"You choose to overwrite "<<plot_name<<" in "<<output_path<<endl;
c1->SaveAs(output_path + plot_name);
break;
case(2):
cout<<"You chose to overwrite all the existent plot."<<endl;
c1->SaveAs(output_path + plot_name);
g_over_all_flag = kTRUE;
break;
case(3):
cout<<"You chose to rename the existent plot and save the new one."<<endl;
gSystem->Rename(output_path + plot_name, output_path + "plot_" + Variable + "_"+ Form("%i", time_stamp->GetTime(kFALSE)) +".png" );
c1->SaveAs(output_path + plot_name);
break;
case(4):
cout<<"You chose to rename all the existent plots."<<endl;
gSystem->Rename(output_path + plot_name, output_path + "plot_" + Variable + "_"+ Form("%i", time_stamp->GetTime(kFALSE)) +".png" );
c1->SaveAs(output_path + plot_name);
::g_over_all_flag = kTRUE;
break;
case(5):
cout<<"Process aborted."<<endl;
exit(0);
break;
default:
cout<<"Choice not valid. Process aborted."<<endl;
exit(1);
}
}
} else {c1->SaveAs(output_path + plot_name); }
//OUTPUT LATEX
double nData=histData->Integral();
double nMC=ZTT->Integral();
double errTotBkg = TMath::Sqrt(etotQCD*etotQCD+/*etotVV*etotVV*/+etotW*etotW+etotTT*etotTT+etotZL*etotZL);
double errTotMC = TMath::Sqrt(etotQCD*etotQCD+/*etotVV*etotVV*/+etotW*etotW+etotTT*etotTT+etotZL*etotZL+etotZTT*etotZTT);
TString latex_name = "tab.txt";
if( (g_over_flag == 3) || (g_over_flag == 4) ){
gSystem->Rename(output_path + latex_name, output_path + "tab_" + Form("%i", time_stamp->GetTime(kFALSE)) +".txt" );
}
std::ofstream ofs (output_path + latex_name, std::ofstream::out);
ofs << std::setprecision(0) << std::fixed;
ofs << "\\begin{table} \n\\begin{tabular}{|c|c|}"<<endl;
ofs << "\\hline\n";
ofs << "Data & "<<nData<<"$\\pm$"<<TMath::Sqrt(nData)<<" \\\\ \n";
ofs << "Z$\\rightarrow \\tau \\tau$ expected signal & "<<nZTT<<"$\\pm$"<<etotZTT<<" \\\\ \n";
ofs << "Z$\\rightarrow \\tau \\tau$ measured signal & "<<nData-(nQCD+/*nVV*/+nW+nTT+nZL)<<"$\\pm$"<<TMath::Sqrt(nData+errTotBkg*errTotBkg)<<" \\\\ \n";
ofs << "QCD background& "<<nQCD<<"$\\pm$"<<etotQCD<<" \\\\ \n";
//ofs << "diboson background& "<<nVV<<"$\\pm$"<<etotVV<<" \\\\ \n";
ofs << "W background& "<<nW<<"$\\pm$"<<etotW<<" \\\\ \n";
ofs << "$t \\bar{t}$ background& "<<nTT<<"$\\pm$"<<etotTT<<" \\\\ \n";
ofs << "$Z \\rightarrow ll$ background& "<<nZL<<"$\\pm$"<<etotZL<<" \\\\ \n";
ofs << "Expected total background & "<<nQCD+/*nVV*/+nW+nTT+nZL<<"$\\pm$"<<errTotBkg<<" \\\\ \n";
ofs << "Total MC & "<<nMC<<"$\\pm$"<<errTotMC<<" \\\\ \n";
ofs << "\\hline\n";
ofs << std::setprecision(3) << std::fixed;
ofs << "Data/MC & "<<nData/nMC<<"$\\pm$"<<TMath::Sqrt(nMC*nMC*nData+nData*nData*errTotMC*errTotMC)/(nMC*nMC)<<" \\\\ \n";
ofs << std::setprecision(1) << std::fixed;
ofs << "Sign(MC)/Bkg & "<<nZTT/(nQCD+/*nVV*/+nW+nTT+nZL)<<"$\\pm$"<<TMath::Sqrt((nQCD+/*nVV*/+nW+nTT+nZL)*(nQCD+/*nVV*/+nW+nTT+nZL)*etotZTT*etotZTT+nZTT*nZTT*errTotBkg*errTotBkg)/((nQCD+/*nVV*/+nW+nTT+nZL)*(nQCD+/*nVV*/+nW+nTT+nZL))<<" \\\\ \n";
ofs << "Significance & "<<nZTT/TMath::Sqrt(nMC)<<"$\\pm$"<<TMath::Sqrt(nZTT*nZTT*errTotMC*errTotMC+nMC*etotZTT*etotZTT)/nMC<<" \\\\ \n";
ofs << "\\hline\n";
ofs << std::setprecision(2) << std::fixed;
ofs << "$\\chi^2 / d.o.f.$ &" << chi2/dof << " \\\\ \n";
ofs << "\\hline\n";
ofs << "\\end{tabular} \n\\end{table}";
ofs.close();
// LEGEND ON SEPARATE CANVAS
TString legend_name = "legend";
if( (g_over_flag == 3) || (g_over_flag == 4) ){
gSystem->Rename(output_path + legend_name, output_path + "legend" + Form("%i", time_stamp->GetTime(kFALSE)) +".png" );
}
TCanvas * legendCanvas = new TCanvas("legendCanvas", "", 700, 800);
legendCanvas->cd();
leg->Draw();
legendCanvas->SaveAs(output_path+legend_name+".png");
}
///////////FUNCTIONS//////////
void SetAxis (TString var, TString *xtitle, TString *UdM) {
if (var == "mvis") {*xtitle= "m_{vis}"; *UdM = "GeV";}
if (var == "pt_1") {*xtitle = "muon p_{T}"; *UdM = "GeV";}
if (var == "pt_2") {*xtitle = "tau p_{T}"; *UdM = "GeV";}
if (var == "eta_1") {*xtitle = "muon #eta"; *UdM = "";}
if (var == "eta_2") {*xtitle = "tau #eta"; *UdM = "";}
if (var == "met") {*xtitle = "MET"; *UdM = "GeV";}
if (var == "puppimet") {*xtitle = "Puppi MET"; *UdM = "GeV";}
if (var == "mvamet") {*xtitle = "MVA MET"; *UdM = "GeV";}
if (var == "mt_1"){*xtitle= "m_{T,1}"; *UdM = "GeV";}
if (var == "pfmt_1"){*xtitle= "PF m_{T,1}"; *UdM = "GeV";}
if (var == "puppimt_1"){*xtitle= "Puppi m_{T,1}"; *UdM = "GeV";}
if (var == "npv"){*xtitle= "Number of primary vertices"; *UdM = "";}
}
void SetBinErrorZero(TH1 *h) { //accepts a TH1 and set all the Bin Errors to 0
int nBins = h->GetNbinsX();
for(int iB=1; iB<=nBins; ++iB){
h->SetBinError(iB, 0);
}
}
float CountError (TH1 *h, float errLumi, float errSist) { //calculate the integral error for a histogram
float errTot = 0;
float errSistTot=0;
float errLumiTot=0;
float errStatTot=0;
int nBins = h->GetNbinsX();
for(int iB=1; iB<=nBins; ++iB){
float eStat = h->GetBinError(iB);
float X = h->GetBinContent(iB);
float eLumi = errLumi * X;
float eh = errSist * X;
errSistTot += eh;
errLumiTot += eLumi;
errStatTot += eStat*eStat;
}
errStatTot=TMath::Sqrt(errStatTot);
errTot=TMath::Sqrt(errSistTot*errSistTot+errLumiTot*errLumiTot+errStatTot*errStatTot);
return errTot;
}