7 #include "TFractionFitter.h" 10 #include "Fit/Fitter.h" 14 int main(
int argc,
char *argv[])
16 bool fitCharmWithLight=
false;
17 bool excludeHighCSV=
false;
23 TString fitType=
"low_njet";
26 fitType=Form(
"%s", argv[1]);
28 if(fitType!=
"low_njet" &&
29 fitType!=
"high_njet" &&
30 fitType!=
"low_njet_low_mj" &&
31 fitType!=
"low_njet_high_mj" &&
32 fitType!=
"exclude_high_csv") {
33 std::cout <<
"Invalid fit type: " << fitType << std::endl;
36 if(fitType==
"exclude_high_csv") {
45 gStyle->SetPadTopMargin(0.08);
46 gStyle->SetPadLeftMargin(0.18);
47 gStyle->SetTitleOffset(1.43,
"y");
49 TFile *
f = TFile::Open(
"csv_newmethod.root");
51 std::cout <<
"Getting weighted histograms" << std::endl;
52 TH1F *qcd_b =
static_cast<TH1F*
>(f->Get(Form(
"QCD_b_%s", fitType.Data())));
53 TH1F *qcd_c =
static_cast<TH1F*
>(f->Get(Form(
"QCD_c_%s", fitType.Data())));
54 TH1F *qcd_l =
static_cast<TH1F*
>(f->Get(Form(
"QCD_l_%s", fitType.Data())));
55 TH1F *other =
static_cast<TH1F*
>(f->Get(Form(
"other_%s", fitType.Data())));
56 TH1F *qcd_cl =
static_cast<TH1F*
>(qcd_c->Clone());
59 std::cout <<
"Getting raw histograms" << std::endl;
60 TH1F *
data =
static_cast<TH1F*
>(f->Get(Form(
"data_%s", fitType.Data())));
61 TH1F *qcd_b_raw =
static_cast<TH1F*
>(f->Get(Form(
"QCD_b_%s_raw", fitType.Data())));
62 TH1F *qcd_c_raw =
static_cast<TH1F*
>(f->Get(Form(
"QCD_c_%s_raw", fitType.Data())));
63 TH1F *qcd_l_raw =
static_cast<TH1F*
>(f->Get(Form(
"QCD_l_%s_raw", fitType.Data())));
64 TH1F *other_raw =
static_cast<TH1F*
>(f->Get(Form(
"other_%s_raw", fitType.Data())));
65 TH1F *qcd_cl_raw =
static_cast<TH1F*
>(qcd_c_raw->Clone());
66 qcd_cl_raw->Add(qcd_l_raw);
68 std::cout <<
"Getting weights" << std::endl;
69 TH1F *qcd_b_weights =
static_cast<TH1F*
>(f->Get(Form(
"QCD_b_%s_weights", fitType.Data())));
70 TH1F *qcd_c_weights =
static_cast<TH1F*
>(f->Get(Form(
"QCD_c_%s_weights", fitType.Data())));
71 TH1F *qcd_l_weights =
static_cast<TH1F*
>(f->Get(Form(
"QCD_l_%s_weights", fitType.Data())));
72 TH1F *other_weights =
static_cast<TH1F*
>(f->Get(Form(
"other_%s_weights", fitType.Data())));
73 TH1F *qcd_cl_weights =
static_cast<TH1F*
>(qcd_c_weights->Clone());
74 qcd_cl_weights->Add(qcd_l_weights);
77 float qcd_yield = qcd_b->Integral()+qcd_c->Integral()+qcd_l->Integral();
78 float scaling = (data->Integral()-other->Integral())/qcd_yield;
79 std::cout <<
"Scaling qcd by a factor: " << scaling << std::endl;
80 qcd_b->Scale(scaling);
81 qcd_c->Scale(scaling);
82 qcd_l->Scale(scaling);
83 qcd_cl->Scale(scaling);
85 qcd_b_weights->Scale(scaling);
86 qcd_c_weights->Scale(scaling);
87 qcd_l_weights->Scale(scaling);
88 qcd_cl_weights->Scale(scaling);
90 float total_yield = qcd_b->Integral()+qcd_c->Integral()+qcd_l->Integral()+other->Integral();
91 float qcd_b_fracbefore = qcd_b->Integral()/total_yield;
92 float qcd_c_fracbefore = qcd_c->Integral()/total_yield;
93 float qcd_l_fracbefore = qcd_l->Integral()/total_yield;
94 float qcd_cl_fracbefore = (qcd_c->Integral()+qcd_l->Integral())/total_yield;
95 float other_fracbefore = other->Integral()/total_yield;
97 std::cout <<
"fractions before fit:\n" 98 <<
"b: " << qcd_b_fracbefore << std::endl
99 <<
"c: " << qcd_c_fracbefore << std::endl
100 <<
"l: " << qcd_l_fracbefore << std::endl
101 <<
"cl: " << qcd_cl_fracbefore << std::endl
102 <<
"other: " << other_fracbefore << std::endl;
104 TObjArray *mc =
new TObjArray(4);
107 if(fitCharmWithLight) {
115 TFractionFitter *fit =
new TFractionFitter(data, mc,
"V");
116 fit->Constrain(0, 1e-4, 1.0);
117 fit->Constrain(1, 1e-4, 1.0);
118 fit->Constrain(2, 1e-4, 1.0);
119 if(!fitCharmWithLight) fit->Constrain(3, 1e-4, 1.0);
121 fit->SetWeight(0, qcd_b_weights);
122 fit->SetWeight(1, other_weights);
123 if(fitCharmWithLight) {
124 fit->SetWeight(2, qcd_cl_weights);
127 fit->SetWeight(2, qcd_c_weights);
128 fit->SetWeight(3, qcd_l_weights);
131 fit->SetRangeX(1, maxbin);
133 ROOT::Fit::Fitter* fitter = fit->GetFitter();
134 fitter->Config().ParSettings(1).SetValue(other->Integral()/total_yield);
135 fitter->Config().ParSettings(1).Fix();
136 if(!fitCharmWithLight) {
137 fitter->Config().ParSettings(3).SetValue(qcd_l->Integral()/total_yield);
138 fitter->Config().ParSettings(3).Fix();
141 Int_t status = fit->Fit();
145 const std::vector<ROOT::Fit::ParameterSettings>& settings = fitter->Config().ParamsSettings();
147 for (
auto isetting : settings) {
148 if(!isetting.IsFixed()) ndof--;
150 std::cout <<
"chi^2/ndof: " << fit->GetChisquare() <<
"/" << ndof <<
", prob = " << fit->GetProb() << std::endl;
152 data->GetXaxis()->SetNdivisions(505);
153 data->SetMarkerSize(1);
154 data->SetLineColor(kBlack);
155 data->SetMarkerStyle(kFullCircle);
156 data->GetYaxis()->SetLabelSize(0.04);
157 data->SetTitle(
";CSV;Events / 0.005");
158 TCanvas *
c =
new TCanvas;
161 TH1F* result_b =
static_cast<TH1F*
>(fit->GetMCPrediction(0));
162 TH1F* result_other =
static_cast<TH1F*
>(fit->GetMCPrediction(1));
167 if(fitCharmWithLight) {
168 result_cl =
static_cast<TH1F*
>(fit->GetMCPrediction(2));
171 result_c =
static_cast<TH1F*
>(fit->GetMCPrediction(2));
172 result_l =
static_cast<TH1F*
>(fit->GetMCPrediction(3));
176 result_b->SetLineColor(kRed);
177 result_b->SetLineWidth(3);
178 result_b->SetLineStyle(kDashed);
179 result_b->SetMarkerSize(0);
180 result_b->Multiply(qcd_b_weights);
181 result_b->Draw(
"same");
182 result_b->Draw(
"same,hist");
183 if(fitCharmWithLight) {
184 result_cl->SetLineColor(kBlack);
185 result_cl->SetLineWidth(3);
186 result_cl->SetLineStyle(kDashed);
187 result_cl->SetMarkerSize(0);
188 result_cl->Draw(
"same");
189 result_cl->Draw(
"same,hist");
192 result_c->SetLineColor(kBlack);
193 result_c->SetLineWidth(3);
194 result_c->SetLineStyle(kDashed);
195 result_c->SetMarkerSize(0);
196 result_c->Multiply(qcd_c_weights);
197 result_c->Draw(
"same");
198 result_c->Draw(
"same,hist");
199 result_l->SetLineColor(kMagenta);
200 result_l->SetLineWidth(3);
201 result_l->SetLineStyle(kDashed);
202 result_l->SetMarkerSize(0);
203 result_l->Multiply(qcd_l_weights);
204 result_l->Draw(
"same");
205 result_l->Draw(
"same,hist");
207 result_other->SetLineColor(kGreen);
208 result_other->SetLineWidth(3);
209 result_other->SetLineStyle(kDashed);
210 result_other->Multiply(other_weights);
211 result_other->SetMarkerSize(0);
212 result_other->Draw(
"same");
213 result_other->Draw(
"same,hist");
214 TH1F* sum =
static_cast<TH1F*
>( fit->GetPlot());
226 sum->SetLineColor(kBlue);
227 sum->SetLineStyle(kSolid);
228 sum->SetLineWidth(3);
229 sum->SetMarkerSize(0);
231 sum->Draw(
"same,hist");
232 data->Draw(
"e,same");
234 TLegend *leg1 =
new TLegend(0.25, 0.5, 0.65, 0.87);
235 leg1->SetFillStyle(0);
236 leg1->SetBorderSize(0);
237 leg1->AddEntry(data,
"Data",
"ELP");
238 leg1->AddEntry(sum,
"Total fit",
"L");
239 leg1->AddEntry(result_b,
"b events",
"PL");
240 leg1->AddEntry(result_c,
"c events",
"PL");
241 leg1->AddEntry(result_l,
"Light-parton events",
"PL");
242 leg1->AddEntry(result_other,
"Non-QCD events",
"PL");
246 tla.SetTextSize(0.038);
249 TString cmslabel =
"#font[62]{CMS} #scale[0.8]{#font[52]{Preliminary}}";
250 TString lumilabel = TString::Format(
"%1.1f",luminosity.Atof())+
" fb^{-1} (13 TeV)";
252 tla.DrawLatexNDC(0.18,0.93,cmslabel);
254 tla.DrawLatexNDC(0.71,0.93,lumilabel);
256 double qcd_b_fracafter, qcd_b_fracafter_err;
257 double qcd_c_fracafter, qcd_c_fracafter_err;
258 fit->GetResult(0, qcd_b_fracafter, qcd_b_fracafter_err);
259 fit->GetResult(2, qcd_c_fracafter, qcd_c_fracafter_err);
261 float other_fracafter = fitter->Config().ParSettings(1).Value();
262 float qcd_l_fracafter = -1.0;
263 float qcd_cl_fracafter = -1.0;
264 if(fitCharmWithLight) {
265 qcd_cl_fracafter = fitter->Config().ParSettings(2).Value();
268 qcd_c_fracafter = fitter->Config().ParSettings(2).Value();
269 qcd_l_fracafter = fitter->Config().ParSettings(3).Value();
271 float qcd_b_ratio=qcd_b_fracafter/qcd_b_fracbefore;
272 float qcd_b_ratio_err=qcd_b_fracafter_err/qcd_b_fracbefore;
273 float other_ratio=other_fracafter/other_fracbefore;
274 float qcd_l_ratio, qcd_c_ratio, qcd_cl_ratio;
275 float qcd_l_ratio_err=0.0;
276 qcd_c_ratio = qcd_l_ratio = qcd_cl_ratio = -1;
277 if(fitCharmWithLight) {
278 qcd_cl_ratio = qcd_cl_fracafter/qcd_cl_fracbefore;
281 qcd_c_ratio = qcd_c_fracafter/qcd_c_fracbefore;
282 qcd_l_ratio = qcd_l_fracafter/qcd_l_fracbefore;
284 float qcd_c_ratio_err=qcd_c_fracafter_err/qcd_c_fracbefore;
286 std::cout <<
"fractions before fit:\n" 287 <<
"b: " << qcd_b_fracbefore << std::endl
288 <<
"c: " << qcd_c_fracbefore << std::endl
289 <<
"l: " << qcd_l_fracbefore << std::endl
290 <<
"cl: " << qcd_cl_fracbefore << std::endl
291 <<
"other: " << other_fracbefore << std::endl;
292 std::cout <<
"fractions after fit:\n" 293 <<
"b: " << qcd_b_fracafter << std::endl
294 <<
"c: " << qcd_c_fracafter << std::endl
295 <<
"l: " << qcd_l_fracafter << std::endl
296 <<
"cl: " << qcd_cl_fracafter << std::endl
297 <<
"other: " << other_fracafter << std::endl;
298 std::cout <<
"after/before fit ratio:\n" 299 <<
"b: " << qcd_b_ratio <<
" +/ " << qcd_b_ratio_err << std::endl
300 <<
"c: " << qcd_c_ratio <<
" +/ " << qcd_c_ratio_err << std::endl
301 <<
"l: " << qcd_l_ratio << std::endl
302 <<
"cl: " << qcd_cl_ratio << std::endl
303 <<
"other: " << other_ratio << std::endl;
304 c->Print(Form(
"plots/csvfit_%s.pdf", fitType.Data()));
306 std::cout <<
"chi^2/ndof from TFractionFitter: " << fit->GetChisquare() <<
"/" << ndof <<
", prob = " << fit->GetProb() << std::endl;
308 for(
int ib =1; ib<=sum->GetNbinsX();ib++){
313 chi2+=pow((data->GetBinContent(ib) - sum->GetBinContent(ib))/sqrt(pow(sum->GetBinError(ib),2)+pow(data->GetBinError(ib),2)),2);
315 std::cout<<
"Total chi2, by hand, is "<<chi2<<std::endl;
321 for(
int ib =1; ib<=sum->GetNbinsX();ib++){
326 chi2_b+=pow((result_b->GetBinContent(ib) - qcd_b->GetBinContent(ib))/sqrt(pow(qcd_b->GetBinError(ib),2)+pow(result_b->GetBinError(ib),2)),2);
328 std::cout<<
"Total chi2 for b hist is "<<chi2_b<<std::endl;
331 for(
int ib =1; ib<=sum->GetNbinsX();ib++){
336 chi2_c+=pow((result_c->GetBinContent(ib) - qcd_c->GetBinContent(ib))/sqrt(pow(qcd_c->GetBinError(ib),2)+pow(result_c->GetBinError(ib),2)),2);
338 std::cout<<
"Total chi2 for c hist is "<<chi2_c<<std::endl;
340 std::cout<<
"Total chi2 for b,c, and sum"<<chi2_c+chi2_b+chi2<<std::endl;
343 if(fitType==
"low_njet" && !excludeHighCSV) {
344 out =
new TFile(Form(
"data/%s.root", fitType.Data()),
"recreate");
345 TH1F *csv_weight =
new TH1F(
"csv_weight",
"csv_weight", 3, 0, 3);
346 csv_weight->SetBinContent(1, qcd_b_ratio);
347 csv_weight->SetBinError(1, qcd_b_ratio_err);
348 csv_weight->GetXaxis()->SetBinLabel(1,
"b");
349 csv_weight->SetBinContent(2, qcd_c_ratio);
350 csv_weight->SetBinError(2, qcd_c_ratio_err);
351 csv_weight->GetXaxis()->SetBinLabel(2,
"c");
352 csv_weight->SetBinContent(3, qcd_l_ratio);
353 csv_weight->SetBinError(3, qcd_l_ratio_err);
354 csv_weight->GetXaxis()->SetBinLabel(3,
"l");
int main(int argc, char *argv[])