-
Notifications
You must be signed in to change notification settings - Fork 1
/
MakeFit.cpp
278 lines (257 loc) · 10.8 KB
/
MakeFit.cpp
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
#include "TCanvas.h"
#include "TAxis.h"
#include "TH1.h"
int MakeFit(bool save=false, TString sel="prova", bool sideband=false){
if(sel=="prova"){
cout<<endl;
cout<<"You have to provide 3 arguments:"<<endl;
cout<<" - boolean to save (true) or not save (false)"<<endl;
cout<<" - string for the category: 'VHlep1' or 'VHlep2' or 'ttHhad' or 'VHhadbtag' or 'VHhad0tag'"<<endl;
cout<<" - a boolean: 'true' if you want to optimize with sideband, 'false' if you want to optimize with the bkg MC"<<endl;
cout<<endl;
return;
}
gROOT->Reset();
gStyle->SetCanvasColor(0);
gStyle->SetFrameBorderMode(0);
gStyle->SetOptStat(0);
gStyle->SetPalette(1,0);
gStyle->SetTitleX(0.5); //title X location
gStyle->SetTitleY(0.96); //title Y location
gStyle->SetErrorX(0);
gSystem->Load("libPhysics");
gSystem->Load("/Users/aniellospiezia/Downloads/root/lib/libRooFit.so");
using namespace RooFit;
using namespace std;
TCanvas * c = new TCanvas("c", "c", 800, 600);
//OPEN THE ROOT FILE AND HISTOGRAMS/TREES
if(sel=="VHlep1"){
TFile *mass_CS = new TFile("mass_CSVHlep1.root");
TFile *mass_SS = new TFile("mass_SSVHlep1.root");
TFile *dipho_SS = new TFile("diphobdt_output_SSVHlep1.root");
}
if(sel=="VHlep2"){
TFile *mass_CS = new TFile("mass_CSVHlep2.root");
TFile *mass_SS = new TFile("mass_SSVHlep2.root");
TFile *dipho_SS = new TFile("diphobdt_output_SSVHlep2.root");
}
if(sel=="ttHhad"){
TFile *mass_CS = new TFile("mass_CSttHhad.root");
TFile *mass_SS = new TFile("mass_SSttHhad.root");
TFile *dipho_SS = new TFile("diphobdt_output_SSttHhad.root");
}
if(sel=="VHhadB"){
TFile *mass_CS = new TFile("mass_CSVHhadB.root");
TFile *mass_SS = new TFile("mass_SSVHhadB.root");
TFile *dipho_SS = new TFile("diphobdt_output_SSVHhadB.root");
}
if(sel=="VHhad0"){
TFile *mass_CS = new TFile("mass_CSVHhad0.root");
TFile *mass_SS = new TFile("mass_SSVHhad0.root");
TFile *dipho_SS = new TFile("diphobdt_output_SSVHhad0.root");
}
if(sel=="VHmetT"){
TFile *mass_CS = new TFile("mass_CSVHmetT.root");
TFile *mass_SS = new TFile("mass_SSVHmetT.root");
TFile *dipho_SS = new TFile("diphobdt_output_SSVHmetT.root");
}
TTree *tree_data_mass_CS = (TTree*)mass_CS->Get("treeDat");
TH1F *histo_data_mass_CS = (TH1F*) mass_CS->Get("DAT");
TH1F *histo_data_mass_SS = (TH1F*) mass_SS->Get("DAT");
TH1F *histo_bkg_mass_CS = (TH1F*) mass_CS->Get("BKG");
TH1F *histo_bkg_mass_SS = (TH1F*) mass_SS->Get("BKG");
TH1F *histo_signal_dipho_SS = (TH1F*) dipho_SS->Get("SIG");
//CREATE THE DATASET AND MAKE THE FIT ON THE DIPHOTON MVA
RooRealVar diphoMVA("diphoMVA","diphoMVA",-1,1);
RooRealVar mass("mass","mass",100,180);
RooRealVar wgt("wgt","wgt",0,100);
RooArgList VarSet(wgt,diphoMVA,mass);
RooDataSet wdata("data","data",tree_data_mass_CS,VarSet,0,"wgt");
RooKeysPdf bkg("bkg","bkg",diphoMVA,wdata,RooKeysPdf::MirrorBoth,2);
RooRealVar nbkg("nbkg","background fraction",50,0.,100000.);
RooAddPdf model("model","model",RooArgList(bkg),RooArgList(nbkg));
model.fitTo(wdata, Range(-1,1), Extended(kTRUE));
RooPlot* frame = diphoMVA.frame();
wdata->plotOn(frame,Binning(10));
model.plotOn(frame,LineColor(kBlack));
model.plotOn(frame, Components(model),LineColor(kBlack),LineStyle(kDashed));
frame->SetTitle(0);
frame->SetXTitle("diphotonMVA");
frame->Draw();
RooArgSet* params = model->getVariables();
params->Print("V");
if(save) c->SaveAs("fit_diphoMVA"+sel+".png");
//MAKE THE FIT ON THE Mgg
RooRealVar c1("c1","coefficient #1", 0.5,-1000,1000);
RooExponential bkg_mass("bkg_mass","bkg_mass",mass,c1) ;
RooRealVar nbkg_mass("nbkg_mass","background fraction",50,0.,100000.);
RooAddPdf model_mass("model_mass","model_mass",RooArgList(bkg_mass),RooArgList(nbkg_mass));
mass.setRange("R1",100,120);
mass.setRange("R2",130,180);
model_mass.fitTo(wdata, Range("R1,R2"), Extended(kTRUE));
RooPlot* frame_mass = mass.frame();
wdata->plotOn(frame_mass,Binning(80));
model_mass.plotOn(frame_mass,LineColor(kBlack));
model_mass.plotOn(frame_mass, Components(model_mass),LineColor(kBlack),LineStyle(kDashed));
frame_mass->SetTitle(0);
frame_mass->SetXTitle("mass");
frame_mass->Draw();
RooArgSet* params_mass = model_mass->getVariables();
params_mass->Print("V");
if(save) c->SaveAs("fit_Mgg"+sel+".png");
mass.setRange("cut", 125-1.5, 125+1.5);
RooAbsReal* sig = model_mass.createIntegral (mass, NormSet (mass), Range("cut"));
sig->getVal();
//DEFINE NORMALIZATON FACTOR
ofstream norma;
norma.open("normalization_"+sel+".txt");
float N_data_CS = histo_data_mass_CS->Integral();
float N_data_SS = histo_data_mass_SS->Integral();
float N_bkg_SS = histo_bkg_mass_SS->Integral();
float N_bkg_SF = histo_bkg_mass_CS->Integral(1,20) + histo_bkg_mass_CS->Integral(31,80);
float eps_FWHM = sig->getVal();
float norm_data = N_data_SS/N_data_CS;
float norm = 0;
if(sideband) {
norm = eps_FWHM * norm_data;
cout<<"eps_FWHM = "<<eps_FWHM<<endl;
cout<<"norm_data = "<<norm_data<<endl;
cout<<"NORMALIZATION = "<<norm<<endl;
norma<<"eps_FWHM = "<<eps_FWHM<<endl;
norma<<"norm_data = "<<norm_data<<endl;
norma<<"NORMALIZATION = "<<norm<<endl;
norma.close();
}
else {
norm = eps_FWHM * N_bkg_SS * N_data_CS/N_bkg_SF;
cout<<"eps_FWHM = "<<eps_FWHM<<endl;
cout<<"N_bkg_SS = "<<N_bkg_SS<<endl;
cout<<"SF = "<<N_data_CS/N_bkg_SF<<endl;
cout<<"NORMALIZATION = "<<norm<<endl;
norma<<"eps_FWHM = "<<eps_FWHM<<endl;
norma<<"N_bkg_SS = "<<N_bkg_SS<<endl;
norma<<"SF = "<<N_data_CS/N_bkg_SF<<endl;
norma<<"NORMALIZATION = "<<norm<<endl;
norma.close();
}
TH1D *DEN1 = new TH1D("yield_sig","yield_sig",20,-1-0.05,1-0.05);
TH1D *DEN2 = new TH1D("yield_bkg","yield_bkg",20,-1-0.05,1-0.05);
TH1D *DEN3 = new TH1D("Efficiency","Efficiency",20,-1-0.05,1-0.05);
TH2D *DEN4 = new TH2D("ROC","ROC",100,0,1,100,0,1);
float bdt_low = -1.0; float mass_low = 1;
for(int i=0; i<20; i++){
sig01->getVal()
diphoMVA.setRange("cut01", bdt_low, 1.0);
RooAbsReal* sig01 = model.createIntegral (diphoMVA, NormSet (diphoMVA), Range("cut01"));
char txt[100];
sprintf(txt, "diphoton_%i_",i);
DEN1->SetBinContent(i+1,0.75*histo_signal_dipho_SS->Integral(mass_low,200));
DEN1->SetBinError(i+1,0.00001);
if(sideband) DEN2->SetBinContent(i+1,sig01->getVal()*norm*nbkg.getVal());
else DEN2->SetBinContent(i+1,sig01->getVal()*norm);
DEN2->SetBinError(i+1,0.00001);
DEN3->SetBinContent(i+1,sig01->getVal());
DEN3->SetBinError(i+1,0.00001);
DEN4->Fill(histo_signal_dipho_SS->Integral(mass_low,200)/histo_signal_dipho_SS->Integral(1,200),1-sig01->getVal());
ofstream myfile;
myfile.open(txt+sel+".dat");
myfile<<"# Simple counting experiment, with one signal and a few background processes"<<endl;
myfile<<"imax 1 number of channels"<<endl;
myfile<<"jmax 1 number of backgrounds"<<endl;
myfile<<"kmax 2 number of nuisance parameters (sources of systematical uncertainties)"<<endl;
myfile<<"------------"<<endl;
myfile<<"# we have just one channel, in which we observe 0 events"<<endl;
myfile<<"bin cat1"<<endl;
myfile<<"observation 0 "<<endl;
myfile<<"------------"<<endl;
myfile<<"# now we list the expected events for signal and all backgrounds in that bin"<<endl;
myfile<<"# the second 'process' line must have a positive number for backgrounds, and 0 for signal"<<endl;
myfile<<"# then we list the independent sources of uncertainties, and give their effect (syst. error)"<<endl;
myfile<<"# on each process and bin"<<endl;
myfile<<"bin cat1 cat1 "<<endl;
myfile<<"process VH bkg "<<endl;
myfile<<"process 0 1 "<<endl;
if(sideband) myfile<<"rate "<<0.75*histo_signal_dipho_SS->Integral(mass_low,200)<<" "<<sig01->getVal()*norm*nbkg.getVal()<<endl;
else myfile<<"rate "<<0.75*histo_signal_dipho_SS->Integral(mass_low,200)<<" "<<sig01->getVal()*norm<<endl;
myfile<<"------------"<<endl;
myfile<<"VH lnN 1.30 - "<<endl;
myfile<<"bkg lnN - 1.30 "<<endl;
myfile.close();
bdt_low = bdt_low + 0.1;
mass_low = mass_low + 10.;
}
TCanvas *c5 = new TCanvas("c5","c5",30,30,800,600);
DEN1->SetMarkerColor(2);
DEN1->SetMarkerStyle(20);
DEN1->SetMarkerSize(1.0);
DEN1->SetFillColor(2);
DEN1->SetLineColor(2);
DEN1->SetLineWidth(2);
DEN1->GetYaxis()->SetRangeUser(0.0,6.0);
DEN1->GetXaxis()->SetRangeUser(-1,1);
DEN1->SetXTitle("diphotonMVA cut");
DEN1->SetYTitle("Number of events");
DEN1->SetTitle(0);
DEN1->SetTitle("Number of events");
DEN1->GetYaxis()->SetTitleOffset(1.1);
DEN1->GetYaxis()->SetTitleSize(0.045);
DEN1->GetXaxis()->SetTitleSize(0.045);
DEN1->GetYaxis()->SetLabelSize(0.045);
DEN1->GetXaxis()->SetLabelSize(0.040);
DEN1->GetXaxis()->SetNdivisions(515);
DEN1->GetYaxis()->SetNdivisions(515);
DEN1->Draw("E");
DEN2->SetMarkerColor(4);
DEN2->SetMarkerStyle(20);
DEN2->SetMarkerSize(1.0);
DEN2->SetFillColor(4);
DEN2->SetLineColor(4);
DEN2->SetLineWidth(2);
DEN2->Draw("Esame");
TLegend *pl = new TLegend(0.61,0.77,0.89,0.89);
pl->SetTextSize(0.045);
pl->SetFillColor(0);
TLegendEntry *ple = pl->AddEntry(DEN1, "Signal", "L");
ple = pl->AddEntry(DEN2, "Background", "L");
pl->Draw();
if(save) c5->SaveAs("Yield_"+sel+".png");
TCanvas *c6 = new TCanvas("c6","c6",30,30,800,600);
DEN3->SetMarkerColor(2);
DEN3->SetMarkerStyle(20);
DEN3->SetMarkerSize(1.0);
DEN3->SetFillColor(2);
DEN3->SetLineColor(2);
DEN3->SetLineWidth(2);
DEN3->GetYaxis()->SetRangeUser(0.0,1.0);
DEN3->GetXaxis()->SetRangeUser(-1,1);
DEN3->SetXTitle("diphotonMVA cut");
DEN3->SetYTitle("efficiency diphotonMVA cut");
DEN3->GetYaxis()->SetTitleOffset(1.1);
DEN3->GetYaxis()->SetTitleSize(0.045);
DEN3->GetXaxis()->SetTitleSize(0.045);
DEN3->GetYaxis()->SetLabelSize(0.045);
DEN3->GetXaxis()->SetLabelSize(0.040);
DEN3->GetXaxis()->SetNdivisions(515);
DEN3->GetYaxis()->SetNdivisions(515);
DEN3->Draw("E");
if(save) c6->SaveAs("effDiphoMVA"+sel+".png");
DEN4->SetMarkerColor(2);
DEN4->SetMarkerStyle(20);
DEN4->SetMarkerSize(1.0);
DEN4->SetFillColor(2);
DEN4->SetLineColor(2);
DEN4->SetLineWidth(2);
DEN4->GetYaxis()->SetRangeUser(0.0,1.0);
DEN4->GetXaxis()->SetRangeUser(0.0,1.0);
DEN4->SetXTitle("Signal Efficiency");
DEN4->SetYTitle("(1 - Background Efficiency)");
DEN4->GetYaxis()->SetTitleOffset(0.95);
DEN4->GetYaxis()->SetTitleSize(0.045);
DEN4->GetXaxis()->SetTitleSize(0.045);
DEN4->GetYaxis()->SetLabelSize(0.045);
DEN4->GetXaxis()->SetLabelSize(0.040);
DEN4->GetXaxis()->SetNdivisions(515);
DEN4->GetYaxis()->SetNdivisions(515);
DEN4->Draw();
if(save) c6->SaveAs("ROC_"+sel+".png");
}