2 """This script plots bkg systematics for the RPV analysis""" 6 from array
import array
8 parser = argparse.ArgumentParser()
9 parser.add_argument(
"-i",
"--input")
11 args = parser.parse_args()
15 sys.exit(
"Please provide an input root file")
24 print" getting "+histname
25 hist = file.Get(histname)
26 nbinsX = hist.GetNbinsX()
27 content = hist.GetBinContent(nbinsX) + hist.GetBinContent(nbinsX+1)
28 error = math.sqrt(math.pow(hist.GetBinError(nbinsX),2) + math.pow(hist.GetBinError(nbinsX+1),2))
29 hist.SetBinContent(nbinsX,content)
30 hist.SetBinContent(nbinsX+1,0)
31 hist.SetBinError(nbinsX+1,0)
32 hist.SetBinError(nbinsX,error)
43 systHistUp = ROOT.TH1F(directory+
"_"+sysName+
"_u",
"",5,0,5)
44 systHistDown = ROOT.TH1F(directory+
"_"+sysName+
"_d",
"",5,0,5)
53 for ip,proc
in enumerate(procList):
54 fixed = (ip != floating_process)
60 histsDown.append(down)
61 print "Up fixed "+str(fixed)+
", yield "+str(up.Integral())
62 print "Down fixed "+str(fixed)+
", yield "+str(down.Integral())
65 totUp -= up.Integral()
66 totDown -= down.Integral()
70 floatingUp = up.Integral()
71 floatingDown = down.Integral()
73 print "floatingDown = "+str(floatingDown)
76 sfUp = totUp/floatingUp
77 sfDown = totDown/floatingDown
81 histsUp[floating_process].Scale(sfUp)
82 histsDown[floating_process].Scale(sfDown)
86 print "variation UP SF, "+directory+
", floating process "+str(floating_process)+
", "+str(sfUp)
87 print "variation DOWN SF, "+directory+
", floating process "+str(floating_process)+
", "+str(sfDown)
90 for ip,proc
in enumerate(procList):
91 print "Process "+proc+
": "+str(histsNom[ip].Integral())
92 runningtotalu+=histsUp[ip].Integral()
93 runningtotald+=histsDown[ip].Integral()
95 print "UP MC total: "+str(runningtotalu)
96 print "DOWN MC total: "+str(runningtotald)
97 print "Data: "+str(tot_data)
102 for ip,proc
in enumerate(procList):
103 systHistUp.Add(histsUp[ip])
104 systHistDown.Add(histsDown[ip])
107 systHistUp.Add(total_nominal,-1)
108 systHistDown.Add(total_nominal,-1)
110 for i
in range(1,systHistUp.GetNbinsX()+1):
112 systHistUp.SetBinContent(i,abs(systHistUp.GetBinContent(i)))
113 systHistDown.SetBinContent(i,abs(systHistDown.GetBinContent(i)))
117 systHistUp.Add(systHistDown)
118 systHistUp.Scale(0.5)
119 systHistUp.Divide(total_nominal)
125 stops = [0.00, 0.10, 0.50, 0.90, 1.00]
126 red = [1.00, 0.80, 0.65, 0.50, 0.34]
127 green = [1.00, 0.80, 0.65, 0.50, 0.34]
128 blue = [1.00, 0.80, 0.65, 0.50, 0.34]
129 s = array(
'd', stops)
131 g = array(
'd', green)
134 fi = ROOT.TColor.CreateGradientColorTable(npoints, s, r, g, b, ncontours)
135 ROOT.gStyle.SetNumberContours(ncontours)
142 ROOT.gROOT.SetBatch(ROOT.kTRUE)
143 ROOT.gStyle.SetCanvasDefW(600)
144 ROOT.gStyle.SetCanvasDefH(600)
145 ROOT.gStyle.SetTitleOffset(1.2,
"x")
147 ROOT.gStyle.SetTitleOffset(1.7,
"z")
149 ROOT.gStyle.SetPadLeftMargin(0.12)
150 ROOT.gStyle.SetPadBottomMargin(0.12)
151 ROOT.gStyle.SetPadTopMargin(0.08)
152 ROOT.gStyle.SetPaintTextFormat(
"6.1f");
154 ROOT.gStyle.SetLabelFont(42)
155 ROOT.gStyle.SetLabelSize(0.05)
156 ROOT.gStyle.SetTitleFont(42)
157 ROOT.gStyle.SetTitleSize(0.07)
162 procList=[
"qcd",
"ttbar",
"wjets",
"other"]
171 systList.append([
"btag_bc",
"b,c jet b-tag SF",2,1])
172 systList.append([
"btag_udsg",
"u,d,s,g jet b-tag SF",3,1])
173 systList.append([
"jes",
"Jet energy scale",4,1])
174 systList.append([
"jer",
"Jet energy resolution",5,1])
175 systList.append([
"lep_eff",
"Lepton efficiency",6,1])
176 systList.append([
"pileup",
"Pileup",7,1])
178 systList.append([
"qcd_flavor",
"QCD flavor",8,1])
179 systList.append([
"gs45",
"Gluon splitting (N_{jet}=4,5)",9,1])
180 systList.append([
"gs67",
"Gluon splitting (N_{jet}=6,7)",10,1])
181 systList.append([
"gs89",
"Gluon splitting (N_{jet}=8,9)",11,1])
182 systList.append([
"gs10Inf",
"Gluon splitting (N_{jet}#geq10)",12,1])
183 systList.append([
"ttbar_pt",
"Top quark p_{T}",13,1])
184 systList.append([
"mur",
"Renormalization scale",14,1])
185 systList.append([
"muf",
"Factorization scale",15,1])
186 systList.append([
"murf",
"Renorm. and fact. scale",16,1])
187 systList.append([
"pdf",
"PDF",17,1])
188 systList.append([
"mc_stat",
"MC statistics",1,2])
190 nSyst = len(systList)
194 binList.append([
"bin0",
"4 #leq n_{jets} #leq 5",
"500 #leq M_{J} < 800 GeV",
"n_{lep} = 0"])
195 binList.append([
"bin1",
"6 #leq n_{jets} #leq 7",
"500 #leq M_{J} < 800 GeV",
"n_{lep} = 0"])
196 binList.append([
"bin2",
"4 #leq n_{jets} #leq 5",
"500 #leq M_{J} < 800 GeV",
"n_{lep} = 1"])
197 binList.append([
"bin3",
"4 #leq n_{jets} #leq 5",
"M_{J} #geq 800 GeV",
"n_{lep} = 0"])
198 binList.append([
"bin4",
"6 #leq n_{jets} #leq 7",
"M_{J} #geq 800 GeV",
"n_{lep} = 0"])
199 binList.append([
"bin5",
"4 #leq n_{jets} #leq 5",
"M_{J} #geq 800 GeV",
"n_{lep} = 1"])
206 binList.append([
"bin10",
"n_{jets} #geq 10",
"500 #leq M_{J} < 800 GeV",
"n_{lep} = 0"])
207 binList.append([
"bin11",
"6 #leq n_{jets} #leq 7",
"500 #leq M_{J} < 800 GeV",
"n_{lep} = 1"])
208 binList.append([
"bin12",
"n_{jets} #geq 8",
"500 #leq M_{J} < 800 GeV",
"n_{lep} = 1"])
209 binList.append([
"bin13",
"n_{jets} #geq 10",
"M_{J} #geq 800 GeV",
"n_{lep} = 0"])
210 binList.append([
"bin14",
"6 #leq n_{jets} #leq 7",
"M_{J} #geq 800 GeV",
"n_{lep} = 1"])
211 binList.append([
"bin15",
"n_{jets} #geq 8",
"M_{J} #geq 800 GeV",
"n_{lep} = 1"])
212 binList.append([
"bin16",
"8 #leq n_{jets} #leq 9",
"500 #leq M_{J} < 800 GeV",
"n_{lep} = 0"])
213 binList.append([
"bin17",
"8 #leq n_{jets} #leq 9",
"M_{J} #geq 800 GeV",
"n_{lep} = 0"])
216 sysFile = ROOT.TFile(infile,
"read")
220 print "directory is "+directory
224 tot_data = data.Integral()
225 nbinsX = data.GetNbinsX()
233 if "n_{lep} = 1" in ibin[3] :
238 for ip,proc
in enumerate(procList):
239 fixed = (ip != floating_process)
243 histsNom.append(nominal)
245 tot -= nominal.Integral()
247 floating = nominal.Integral()
254 histsNom[floating_process].Scale(sf)
257 total_nominal = ROOT.TH1F(directory+
"_nominal",
"",5,0,5)
258 total_nominal.Sumw2()
259 for ip,proc
in enumerate(procList):
260 total_nominal.Add(histsNom[ip])
263 print "Nominal SF, "+directory+
", floating process "+str(floating_process)+
", "+str(sf)
265 for ip,proc
in enumerate(procList):
266 print "Process "+proc+
": "+str(histsNom[ip].Integral())
267 runningtotal+=histsNom[ip].Integral()
269 print "MC total: "+str(runningtotal)
270 print "Nominal hist total: " + str(total_nominal.Integral())
271 print "Data: "+str(tot_data)
275 ROOT.gStyle.SetPadRightMargin()
276 ROOT.gStyle.SetPadLeftMargin(0.12)
278 leg = ROOT.TLegend(0.12,0.5,0.64,0.92)
279 table = ROOT.TH2F(
"table_"+directory,
"",nbinsX,-0.5,nbinsX-0.5,nSyst,0,nSyst)
283 for isys, syst
in enumerate(systList,start=1):
286 print "starting "+sysName
289 systHist = ROOT.TH1F(directory+
"_"+sysName+
"_sym",
"",5,0,5)
292 if "mc_stat" not in sysName:
294 if "pdf" not in sysName
and "mu" not in sysName:
297 elif "mu" in sysName:
305 for i
in range(1,thisproc.GetNbinsX()+1):
306 thisproc.SetBinContent(i,math.pow(thisproc.GetBinContent(i),2))
308 systHist.Add(thisproc)
311 for i
in range(1,systHist.GetNbinsX()+1):
312 systHist.SetBinContent(i,math.pow(systHist.GetBinContent(i),0.5))
314 elif "pdf" in sysName:
318 for i
in range(0,100):
323 for i
in range(1,systHist.GetNbinsX()+1):
324 thisvar.SetBinContent(i,math.pow(thisvar.GetBinContent(i),2))
325 systHist.Add(thisvar)
328 for i
in range(1,systHist.GetNbinsX()+1):
329 systHist.SetBinContent(i,math.pow(systHist.GetBinContent(i),0.5)/10)
332 elif "mc_stat" in sysName:
333 systHist.Add(total_nominal)
334 for i
in range(1,systHist.GetNbinsX()+1):
336 if systHist.GetBinContent(i)>0:
337 systHist.SetBinContent(i,(systHist.GetBinError(i)/systHist.GetBinContent(i)))
341 for i
in range(1,systHist.GetNbinsX()+1):
342 if systHist.GetBinContent(i) < 0.001:
343 table.SetBinContent(i,isys,0.04)
345 table.SetBinContent(i,isys,round(100*systHist.GetBinContent(i),1))
347 print "symmetrized rel error bin "+str(i)+
" "+str(systHist.GetBinContent(i))
350 systHists_sym.append(systHist)
352 table.GetYaxis().SetBinLabel(isys,syst[1])
353 systHists_sym[isys-1].SetTitle(
";N_{b};Relative Error")
354 systHists_sym[isys-1].SetLineColor(syst[2])
355 systHists_sym[isys-1].SetLineStyle(syst[3])
356 systHists_sym[isys-1].SetLineWidth(2)
357 systHists_sym[isys-1].SetMaximum(1.)
358 systHists_sym[isys-1].SetMinimum(0.)
359 systHists_sym[isys-1].SetAxisRange(1,4,
"X")
360 systHists_sym[isys-1].SetStats(0)
361 systHists_sym[isys-1].GetYaxis().SetTitleOffset(1.4)
362 systHists_sym[isys-1].GetYaxis().SetTitleSize(0.04)
363 systHists_sym[isys-1].GetXaxis().SetTitleSize(0.04)
364 systHists_sym[isys-1].GetXaxis().SetNdivisions(505)
365 leg.AddEntry(systHists_sym[isys-1],syst[1],
"l")
366 systHists_sym[isys-1].Draw(
"hist same")
370 tla.SetTextSize(0.038)
371 tla.DrawLatexNDC(0.12,0.93,
"#font[62]{CMS} #scale[0.8]{#font[52]{Simulation}}")
373 tla.DrawLatexNDC(0.71,0.93,
"#sqrt{s} = 13 TeV")
375 tla.DrawLatexNDC(0.17, 0.45, ibin[3])
376 tla.DrawLatexNDC(0.17, 0.4, ibin[1])
377 tla.DrawLatexNDC(0.17, 0.35, ibin[2])
379 if directory == binList[0][0]:
380 outname =
"plots/bkg_systs_all.pdf(" 381 elif directory == binList[len(binList)-1][0]:
382 outname =
"plots/bkg_systs_all.pdf)" 384 outname =
"plots/bkg_systs_all.pdf" 387 outname =
"plots/bkg_systs_" + directory +
".pdf" 388 print "outname is " +outname
392 ROOT.gStyle.SetPadLeftMargin(0.35)
393 ROOT.gStyle.SetPadRightMargin(0.2)
394 ROOT.gStyle.SetPadBottomMargin(0.1)
396 table.GetXaxis().SetBinLabel(1,
"0");
397 table.GetXaxis().SetBinLabel(2,
"1");
398 table.GetXaxis().SetBinLabel(3,
"2");
399 table.GetXaxis().SetBinLabel(4,
"3");
400 table.GetXaxis().SetBinLabel(5,
"#geq 4");
401 table.GetXaxis().SetNdivisions(400,0)
405 table.SetMarkerSize(1.5)
406 table.SetAxisRange(0.5,4.499,
"X")
407 table.SetXTitle(
"N_{b}")
408 table.SetZTitle(
"Uncertainty [%]")
409 table.GetYaxis().SetTitleOffset(1.4)
410 table.GetYaxis().SetTitleSize(0.05)
411 table.GetYaxis().SetLabelSize(0.045)
412 table.GetXaxis().SetTitleSize(0.04)
413 table.Draw(
"colz text")
414 ROOT.gPad.SetTicks(1,0)
415 table.Draw(
"axis y+ same")
417 tla.SetTextSize(0.038)
418 tla.DrawLatexNDC(0.35,0.93,
"#font[62]{CMS} #scale[0.8]{#font[52]{Simulation}}")
420 tla.DrawLatexNDC(0.66,0.93,
"#sqrt{s} = 13 TeV")
422 if directory == binList[0][0]:
423 outname =
"plots/table_bkg_systs_all.pdf(" 424 elif directory == binList[len(binList)-1][0]:
425 outname =
"plots/table_bkg_systs_all.pdf)" 427 outname =
"plots/table_bkg_systs_all.pdf" 430 outname =
"plots/table_bkg_systs_" + directory +
".pdf" def set_palette_gray(ncontours=20)
def get_symmetrized_relative_errors(sysName, tot_data, total_nominal, procList, floating_process, sysFile, directory)
def get_hist_with_overflow(file, histname)