1 from __future__
import print_function
16 print(
"\nCannot load PYROOT, make sure you have setup ROOT in the path")
17 print(
"and pyroot library is also defined in the variable PYTHONPATH, try:\n")
18 if (os.getenv(
"PYTHONPATH")):
19 print(
" setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n")
21 print(
" setenv PYTHONPATH $ROOTSYS/lib\n")
24 from ROOT
import TFile
25 from ROOT
import TCanvas
27 from ROOT
import TLegend
28 from ROOT
import TLatex
31 from ROOT
import TVectorD
32 from ROOT
import TGraphErrors
33 from ROOT
import Double
36 from listHistos
import *
41 fileNameVal =
"BTagRelVal_TTbar_Startup_14_612SLHC2.root" 42 fileNameRef =
"BTagRelVal_TTbar_Startup_612SLHC1_14.root" 44 ValRel =
"612SLHC2_14" 45 RefRel =
"612SLHC1_14" 47 ValSample =
"TTbar_FullSim" 48 RefSample =
"TTbar_FullSim" 54 Banner =
"CMS Preliminary" 58 pathInFile =
"/DQMData/Run 1/Btag/Run summary/" 131 unity = TF1(
"unity",
"1",-1000,1000)
132 unity.SetLineColor(8)
133 unity.SetLineWidth(1)
134 unity.SetLineStyle(1)
185 if histos
is None :
return 186 if isVal : sample =
"Val" 187 else : sample =
"Ref" 193 if plot.binning
and len(plot.binning)==3 :
194 histos[k].SetBins(plot.binning[0],plot.binning[1],plot.binning[2])
195 elif plot.binning
and len(plot.binning)==2 :
196 nbins=plot.binning[1]+1-plot.binning[0]
197 xmin=histos[k].GetBinLowEdge(plot.binning[0])
198 xmax=histos[k].GetBinLowEdge(plot.binning[1]+1)
199 valtmp=TH1F(histos[k].GetName(),histos[k].GetTitle(),nbins,xmin,xmax)
201 for bin
in range(plot.binning[0],plot.binning[1]+1) :
202 valtmp.SetBinContent(i,histos[k].GetBinContent(bin))
205 if plot.Rebin
and plot.Rebin > 0 :
206 histos[k].
Rebin(plot.Rebin)
208 histos[k].SetLineColor(mapColor[k])
209 histos[k].SetMarkerColor(mapColor[k])
210 histos[k].SetMarkerStyle(mapMarker[sample])
211 if drawOption ==
"HIST" :
212 histos[k].SetLineWidth(mapLineWidth[sample])
213 histos[k].SetLineStyle(mapLineStyle[sample])
217 if plot.doNormalization :
218 histos[k].Scale(1./histos[k].Integral())
220 histos[k].Scale(weight)
222 if histos[k].GetMinimum(0.) < minY :
223 minY = histos[k].GetMinimum(0.)
225 if histos[k].GetBinContent(histos[k].GetMaximumBin()) > maxY :
226 maxY = histos[k].GetBinContent(histos[k].GetMaximumBin())+histos[k].GetBinError(histos[k].GetMaximumBin())
229 histos[k].SetXTitle(plot.Xlabel)
231 histos[k].SetYTitle(plot.Ylabel)
232 outhistos.append(histos[k])
234 if not plot.logY : outhistos[0].GetYaxis().SetRangeUser(0,1.1*maxY)
236 else : outhistos[0].GetYaxis().SetRangeUser(
max(0.0001,0.5*minY),1.1*maxY)
240 def graphProducer(plot,histos,tagFlav="B",mistagFlav=["C","DUSG"],isVal=True):
241 if histos
is None :
return 242 if isVal : sample =
"Val" 243 else : sample =
"Ref" 247 if tagFlav
not in listFlavors :
249 if plot.tagFlavor
and plot.mistagFlavor :
250 tagFlav = plot.tagFlavor
251 mistagFlav = plot.mistagFlavor
252 for f
in listFlavors :
258 for f
in listFlavors :
262 maxnpoints = histos[tagFlav].GetNbinsX()
263 for f
in listFlavors :
264 Eff[f].
append(histos[f].GetBinContent(1))
265 EffErr[f].
append(histos[f].GetBinError(1))
266 for bin
in range(2,maxnpoints+1) :
268 if len(Eff[tagFlav])>0 :
269 delta = Eff[tagFlav][-1]-histos[tagFlav].GetBinContent(bin)
270 if delta>
max(0.005,EffErr[tagFlav][-1]) :
272 for f
in listFlavors :
273 Eff[f].
append(histos[f].GetBinContent(bin))
274 EffErr[f].
append(histos[f].GetBinError(bin))
276 len_ = len(Eff[tagFlav])
279 for f
in listFlavors :
280 TVec_Eff[f] = TVectorD(len_)
281 TVec_EffErr[f] = TVectorD(len_)
283 for j
in range(0,len_) :
284 for f
in listFlavors :
285 TVec_Eff[f][j] = Eff[f][j]
286 TVec_EffErr[f][j] = EffErr[f][j]
288 for mis
in mistagFlav :
289 g[tagFlav+mis]=TGraphErrors(TVec_Eff[tagFlav],TVec_Eff[mis],TVec_EffErr[tagFlav],TVec_EffErr[mis])
291 for f
in listFlavors :
292 if f
not in mistagFlav :
continue 293 g[tagFlav+f].SetLineColor(mapColor[f])
294 g[tagFlav+f].SetMarkerStyle(mapMarker[sample])
295 g[tagFlav+f].SetMarkerColor(mapColor[f])
296 g_out.append(g[tagFlav+f])
300 if g_i
is not None :
break 302 g_out[index].GetXaxis().SetRangeUser(0,1)
303 g_out[index].GetYaxis().SetRangeUser(0.0001,1)
305 g_out[index].GetXaxis().SetTitle(plot.Xlabel)
307 g_out[index].GetYaxis().SetTitle(plot.Ylabel)
309 for index,f
in enumerate(listFlavors) :
310 if f
not in mistagFlav : g_out.insert(index,
None)
314 def savePlots(title,saveName,listFromats,plot,Histos,keyHisto,listLegend,options,ratios=None,legendName="") :
319 c[keyHisto] = TCanvas(saveName,keyHisto+plot.title,700,700+24*len(listFlavors))
320 pads[
"hist"] = TPad(
"hist", saveName+plot.title,0,0.11*len(listFlavors),1.0,1.0)
322 c[keyHisto] = TCanvas(keyHisto,saveName+plot.title,700,700)
323 pads[
"hist"] = TPad(
"hist", saveName+plot.title,0,0.,1.0,1.0)
326 for r
in range(0,len(ratios)) :
327 pads[
"ratio_"+
str(r)] = TPad(
"ratio_"+
str(r), saveName+plot.title+
str(r),0,0.11*r,1.0,0.11*(r+1))
328 pads[
"ratio_"+
str(r)].Draw()
331 if plot.logY : pads[
"hist"].SetLogy()
332 if plot.grid : pads[
"hist"].SetGrid()
334 leg = TLegend(0.6,0.4,0.8,0.6)
336 leg.SetTextSize(0.035)
342 optionSame = drawOption+
"same" 343 if plot.doPerformance :
346 for i
in range(0,len(Histos)) :
347 if Histos[i]
is None :
continue 349 if not plot.doPerformance : Histos[i].GetPainter().PaintStat(ROOT.gStyle.GetOptStat(),0)
350 Histos[i].SetTitle(title)
351 Histos[i].Draw(option)
353 else : Histos[i].Draw(optionSame)
355 if plot.legend
and len(Histos)%len(listLegend)==0:
356 r=len(Histos)/len(listLegend)
357 index=i-r*len(listLegend)
359 index+=len(listLegend)
360 legName = legendName.replace(
"KEY",listLegend[index])
361 if i<len(listLegend) : legName = legName.replace(
"isVAL",options.ValRel)
362 else : legName = legName.replace(
"isVAL",options.RefRel)
363 if drawOption==
"HIST" :
364 leg.AddEntry(Histos[i],legName,
"L")
366 leg.AddEntry(Histos[i],legName,
"P")
368 if plot.legend
and options.drawLegend : leg.Draw(
"same")
370 if options.printBanner :
371 print(type(options.printBanner))
372 tex = TLatex(0.55,0.75,options.Banner)
374 tex.SetTextSize(0.05)
378 for r
in range(0,len(ratios)) :
379 pads[
"ratio_"+
str(r)].
cd()
380 if ratios[r]
is None :
continue 381 pads[
"ratio_"+
str(r)].SetGrid()
382 ratios[r].GetYaxis().SetTitle(listLegend[r]+
"-jets")
383 ratios[r].GetYaxis().SetTitleSize(0.15)
384 ratios[r].GetYaxis().SetTitleOffset(0.2)
385 ratios[r].GetYaxis().SetNdivisions(3,3,2)
388 for format
in listFromats :
389 save = saveName+
"."+format
390 c[keyHisto].
Print(save)
391 return [c,leg,tex,pads]
395 for h_i
in range(0,len(hVal)):
396 if hVal[h_i]
is None :
continue 397 r = TH1F(hVal[h_i].GetName()+
"ratio",
"ratio "+hVal[h_i].GetTitle(),hVal[h_i].GetNbinsX(),hVal[h_i].GetXaxis().GetXmin(),hVal[h_i].GetXaxis().GetXmax())
400 r.GetYaxis().SetRangeUser(0.25,1.75)
401 r.SetMarkerColor(hVal[h_i].GetMarkerColor())
402 r.SetLineColor(hVal[h_i].GetLineColor())
403 r.GetYaxis().SetLabelSize(0.15)
404 r.GetXaxis().SetLabelSize(0.15)
410 for g_i
in range(0,len(hVal)):
411 if hVal[g_i]
is None :
414 tmp = hVal[g_i].GetHistogram()
415 histVal = TH1F(hVal[g_i].GetName()+
"_ratio",hVal[g_i].GetTitle()+
"_ratio",tmp.GetNbinsX(),tmp.GetXaxis().GetXmin(),tmp.GetXaxis().GetXmax())
416 histRef = TH1F(hRef[g_i].GetName()+
"_ratio",hRef[g_i].GetTitle()+
"_ratio",histVal.GetNbinsX(),histVal.GetXaxis().GetXmin(),histVal.GetXaxis().GetXmax())
418 for p
in range(0,hVal[g_i].GetN()-1):
422 hVal[g_i].GetPoint(p,x,y)
423 xerr = hVal[g_i].GetErrorX(p)
424 yerr = hVal[g_i].GetErrorY(p)
425 bin_p = histVal.FindBin(x)
426 xHist = histVal.GetBinCenter(bin_p)
433 xbiserr = hVal[g_i].GetErrorX(p-1)
434 ybiserr = hVal[g_i].GetErrorY(p-1)
435 hVal[g_i].GetPoint(p-1,xbis,ybis)
437 xbiserr = hVal[g_i].GetErrorX(p+1)
438 ybiserr = hVal[g_i].GetErrorY(p+1)
439 hVal[g_i].GetPoint(p+1,xbis,ybis)
443 bin_p_valContent_errP = y+yerr
444 bin_p_valContent_errM = y-yerr
449 bin_p_valContent = a*xHist+b
451 aerrP = ( (ybis+ybiserr)-(y+yerr) ) / (xbis-x)
452 berrP = (y+yerr)-aerrP*x
453 bin_p_valContent_errP = aerrP*xHist+berrP
454 aerrM = ( (ybis-ybiserr)-(y-yerr) ) / (xbis-x)
455 berrM = (y-yerr)-aerrM*x
456 bin_p_valContent_errM = aerrM*xHist+berrM
458 histVal.SetBinContent(bin_p,bin_p_valContent)
459 histVal.SetBinError(bin_p,(bin_p_valContent_errP-bin_p_valContent_errM)/2)
461 for pRef
in range(0,hRef[g_i].GetN()):
465 hRef[g_i].GetPoint(pRef,xRef,yRef)
467 if xRef > xHist :
continue 468 xReferr = hRef[g_i].GetErrorX(pRef)
469 yReferr = hRef[g_i].GetErrorY(pRef)
473 xRefbiserr = hRef[g_i].GetErrorX(pRef+1)
474 yRefbiserr = hRef[g_i].GetErrorY(pRef+1)
475 hRef[g_i].GetPoint(pRef+1,xRefbis,yRefbis)
478 bin_p_refContent = yRef
479 bin_p_refContent_errP = yRef+yReferr
480 bin_p_refContent_errM = yRef-yReferr
483 aRef=(ybis-y)/(xbis-x)
485 bin_p_refContent = aRef*xHist+bRef
487 aReferrP = ((yRefbis+yRefbiserr)-(yRef+yReferr))/((xRefbis)-(xRef))
488 bReferrP = (yRef+yReferr)-aReferrP*(xRef-xReferr)
489 bin_p_refContent_errP = aReferrP*xHist+bReferrP
490 aReferrM = ((yRefbis-yRefbiserr)-(yRef-yReferr))/((xRefbis)-(xRef))
491 bReferrM = (yRef-yReferr)-aReferrM*(xRef+xReferr)
492 bin_p_refContent_errM = aReferrM*xHist+bReferrM
495 histRef.SetBinContent(bin_p,bin_p_refContent)
496 histRef.SetBinError(bin_p,(bin_p_refContent_errP-bin_p_refContent_errM)/2)
500 histVal.Divide(histRef)
502 histVal.GetXaxis().SetRangeUser(0.,1.)
504 histVal.GetYaxis().SetRangeUser(0.25,1.75)
505 histVal.SetMarkerColor(hVal[g_i].GetMarkerColor())
506 histVal.SetLineColor(hVal[g_i].GetLineColor())
507 histVal.GetYaxis().SetLabelSize(0.15)
508 histVal.GetXaxis().SetLabelSize(0.15)
509 ratio.append(histVal)
def createRatioFromGraph(hVal, hRef)
def histoProducer(plot, histos, keys, isVal=True)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
def createRatio(hVal, hRef)
def savePlots(title, saveName, listFromats, plot, Histos, keyHisto, listLegend, options, ratios=None, legendName="")
def graphProducer(plot, histos, tagFlav="B", mistagFlav=["C", DUSG, isVal=True)
while(__syncthreads_or(more))