CMS 3D CMS Logo

histoStyle.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 
4 
5 # automatized plots generator for b-tagging performances
6 # Adrien Caudron, 2013, UCL
7 
8 
9 
10 #do all import
11 import os, sys
12 
13 try:
14  import ROOT
15 except:
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")
20  else:
21  print(" setenv PYTHONPATH $ROOTSYS/lib\n")
22  sys.exit()
23 
24 from ROOT import TFile
25 from ROOT import TCanvas
26 from ROOT import TPad
27 from ROOT import TLegend
28 from ROOT import TLatex
29 from ROOT import TH1F
30 from ROOT import TF1
31 from ROOT import TVectorD
32 from ROOT import TGraphErrors
33 from ROOT import Double
34 
35 import Style
36 from listHistos import *
37 
38 #define the input root files
39 #fileVal = TFile("BTagRelVal_TTbar_Startup_14_612SLHC2.root","READ")
40 #fileRef = TFile("BTagRelVal_TTbar_Startup_612SLHC1_14.root","READ")
41 fileNameVal = "BTagRelVal_TTbar_Startup_14_612SLHC2.root"
42 fileNameRef = "BTagRelVal_TTbar_Startup_612SLHC1_14.root"
43 #define the val/ref labels
44 ValRel = "612SLHC2_14"
45 RefRel = "612SLHC1_14"
46 #define the sample labels
47 ValSample = "TTbar_FullSim"
48 RefSample = "TTbar_FullSim"
49 #define different settings
50 batch = False #run on batch mode ?
51 weight = 1. #rescale the histos according to this weight
52 drawLegend = False #draw legend ?
53 printBanner = False #draw text Banner on top of the histos
54 Banner = "CMS Preliminary"
55 doRatio = True #plot the ratios
56 drawOption = "" # "" or "HIST"
57 #path in the file
58 pathInFile = "/DQMData/Run 1/Btag/Run summary/"
59 #ETA/PT bins, GLOBAL ?
60 EtaPtBin =[
61  "GLOBAL",
62  #"ETA_0-1v4",
63  #"ETA_1v4-2v4",
64  #"PT_50-80",
65  #"PT_80-120",
66  ]
67 #list of taggers to look at
68 listTag = [
69  "CSV",
70  "CSVMVA",
71  "JP",
72  "JBP",
73  "TCHE",
74  "TCHP",
75  "SSVHE",
76  "SSVHP",
77  "SMT",
78  #"SMTIP3d",
79  #"SMTPt",
80  "SET",
81  ]
82 #list of flavors to look at
83 listFlavors = [
84  #"ALL",
85  "B",
86  "C",
87  #"G",
88  #"DUS",
89  "DUSG",
90  #"NI",
91  ]
92 #map for marker color for flav-col and tag-col
93 mapColor = {
94  "ALL" : 4 ,
95  "B" : 3 ,
96  "C" : 1 ,
97  "G" : 2 ,
98  "DUS" : 2 ,
99  "DUSG" : 2 ,
100  "NI" : 5 ,
101  "CSV" : 5 ,
102  "CSVMVA" : 6 ,
103  "JP" : 3 ,
104  "JBP" : 9 ,
105  "TCHE" : 1,
106  "TCHP" : 2,
107  "SSVHE" : 4,
108  "SSVHP" : 7,
109  "SMT" : 8 ,
110  "SMTIP3d" : 11 ,
111  "SMTPt" : 12
112  }
113 #marker style map for Val/Ref
114 mapMarker = {
115  "Val" : 22,
116  "Ref" : 8
117  }
118 mapLineWidth = {
119  "Val" : 3,
120  "Ref" : 2
121  }
122 mapLineStyle = {
123  "Val" : 2,
124  "Ref" : 1
125  }
126 #choose the formats to save the plots
127 listFromats = [
128  "gif",
129  ]
130 #unity function
131 unity = TF1("unity","1",-1000,1000)
132 unity.SetLineColor(8)
133 unity.SetLineWidth(1)
134 unity.SetLineStyle(1)
135 #list of histos to plots
136 listHistos = [
137  jetPt,
138  jetEta,
139  discr,
140  effVsDiscrCut_discr,
141  FlavEffVsBEff_discr,
142  performance,
143  #performanceC,
144 
145  #IP,
146  #IPe,
147  #IPs,
148  #NTracks,
149  #decayLength,
150  #distToJetAxis,
151  #NHits,
152  #NPixelHits,
153  #NormChi2,
154  #trackPt,
155 
156 
157  #flightDist3Dval,
158  #flightDist3Dsig,
159  #jetNSecondaryVertices,
160 
161  #vertexMass,
162  #vertexNTracks,
163  #vertexJetDeltaR,
164  #vertexEnergyRatio,
165 
166  #vertexCategory,
167  #trackSip3dVal,
168  #trackSip3dSig,
169  #trackSip3dSigAboveCharm,
170  #trackDeltaR,
171  #trackEtaRel,
172  #trackDecayLenVal,
173  #trackSumJetDeltaR,
174  #trackJetDist,
175  #trackSumJetEtRatio,
176  #trackPtRel,
177  #trackPtRatio,
178  #trackMomentum,
179  #trackPPar,
180  #trackPParRatio,
181  ]
182 
183 #methode to do a plot from histos
184 def histoProducer(plot,histos,keys,isVal=True):
185  if histos is None : return
186  if isVal : sample = "Val"
187  else : sample = "Ref"
188  outhistos = []
189  minY=9999.
190  maxY=0.
191  for k in keys :
192  #Binning
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)
200  i=1
201  for bin in range(plot.binning[0],plot.binning[1]+1) :
202  valtmp.SetBinContent(i,histos[k].GetBinContent(bin))
203  i+=1
204  histos[k]=valtmp
205  if plot.Rebin and plot.Rebin > 0 :
206  histos[k].Rebin(plot.Rebin)
207  #Style
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])
214  #compute errors
215  histos[k].Sumw2()
216  #do the norm
217  if plot.doNormalization :
218  histos[k].Scale(1./histos[k].Integral())
219  elif weight!=1 :
220  histos[k].Scale(weight)
221  #get Y min
222  if histos[k].GetMinimum(0.) < minY :
223  minY = histos[k].GetMinimum(0.)
224  #get Y max
225  if histos[k].GetBinContent(histos[k].GetMaximumBin()) > maxY :
226  maxY = histos[k].GetBinContent(histos[k].GetMaximumBin())+histos[k].GetBinError(histos[k].GetMaximumBin())
227  #Axis
228  if plot.Xlabel :
229  histos[k].SetXTitle(plot.Xlabel)
230  if plot.Ylabel :
231  histos[k].SetYTitle(plot.Ylabel)
232  outhistos.append(histos[k])
233  #Range
234  if not plot.logY : outhistos[0].GetYaxis().SetRangeUser(0,1.1*maxY)
235  #else : outhistos[0].GetYaxis().SetRangeUser(0.0001,1.05)
236  else : outhistos[0].GetYaxis().SetRangeUser(max(0.0001,0.5*minY),1.1*maxY)
237  return outhistos
238 
239 #method to do a plot from a graph
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"
244  #define graphs
245  g = {}
246  g_out = []
247  if tagFlav not in listFlavors :
248  return
249  if plot.tagFlavor and plot.mistagFlavor :
250  tagFlav = plot.tagFlavor
251  mistagFlav = plot.mistagFlavor
252  for f in listFlavors :
253  #compute errors, in case not already done
254  histos[f].Sumw2()
255  #efficiency lists
256  Eff = {}
257  EffErr = {}
258  for f in listFlavors :
259  Eff[f] = []
260  EffErr[f] = []
261  #define mapping points for the histos
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) :
267  #check if we add the point to the graph for Val sample
268  if len(Eff[tagFlav])>0 :
269  delta = Eff[tagFlav][-1]-histos[tagFlav].GetBinContent(bin)
270  if delta>max(0.005,EffErr[tagFlav][-1]) :
271  #get efficiencies
272  for f in listFlavors :
273  Eff[f].append(histos[f].GetBinContent(bin))
274  EffErr[f].append(histos[f].GetBinError(bin))
275  #create TVector
276  len_ = len(Eff[tagFlav])
277  TVec_Eff = {}
278  TVec_EffErr = {}
279  for f in listFlavors :
280  TVec_Eff[f] = TVectorD(len_)
281  TVec_EffErr[f] = TVectorD(len_)
282  #Fill the vector
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]
287  #fill TGraph
288  for mis in mistagFlav :
289  g[tagFlav+mis]=TGraphErrors(TVec_Eff[tagFlav],TVec_Eff[mis],TVec_EffErr[tagFlav],TVec_EffErr[mis])
290  #style
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])
297  index = -1
298  for g_i in g_out :
299  index+=1
300  if g_i is not None : break
301  #Axis
302  g_out[index].GetXaxis().SetRangeUser(0,1)
303  g_out[index].GetYaxis().SetRangeUser(0.0001,1)
304  if plot.Xlabel :
305  g_out[index].GetXaxis().SetTitle(plot.Xlabel)
306  if plot.Ylabel :
307  g_out[index].GetYaxis().SetTitle(plot.Ylabel)
308  #add in the list None for element in listFlavors for which no TGraph is computed
309  for index,f in enumerate(listFlavors) :
310  if f not in mistagFlav : g_out.insert(index,None)
311  return g_out
312 
313 #method to draw the plot and save it
314 def savePlots(title,saveName,listFromats,plot,Histos,keyHisto,listLegend,options,ratios=None,legendName="") :
315  #create canvas
316  c = {}
317  pads = {}
318  if options.doRatio :
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)
321  else :
322  c[keyHisto] = TCanvas(keyHisto,saveName+plot.title,700,700)
323  pads["hist"] = TPad("hist", saveName+plot.title,0,0.,1.0,1.0)
324  pads["hist"].Draw()
325  if ratios :
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()
329  pads["hist"].cd()
330  #canvas style
331  if plot.logY : pads["hist"].SetLogy()
332  if plot.grid : pads["hist"].SetGrid()
333  #legend
334  leg = TLegend(0.6,0.4,0.8,0.6)
335  leg.SetMargin(0.12)
336  leg.SetTextSize(0.035)
337  leg.SetFillColor(10)
338  leg.SetBorderSize(0)
339  #draw histos
340  first = True
341  option = drawOption
342  optionSame = drawOption+"same"
343  if plot.doPerformance :
344  option = "AP"
345  optionSame = "sameP"
346  for i in range(0,len(Histos)) :
347  if Histos[i] is None : continue
348  if first :
349  if not plot.doPerformance : Histos[i].GetPainter().PaintStat(ROOT.gStyle.GetOptStat(),0)
350  Histos[i].SetTitle(title)
351  Histos[i].Draw(option)
352  first = False
353  else : Histos[i].Draw(optionSame)
354  #Fill legend
355  if plot.legend and len(Histos)%len(listLegend)==0:
356  r=len(Histos)/len(listLegend)
357  index=i-r*len(listLegend)
358  while(index<0):
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")
365  else :
366  leg.AddEntry(Histos[i],legName,"P")
367  #Draw legend
368  if plot.legend and options.drawLegend : leg.Draw("same")
369  tex = None
370  if options.printBanner :
371  print(type(options.printBanner))
372  tex = TLatex(0.55,0.75,options.Banner)
373  tex.SetNDC()
374  tex.SetTextSize(0.05)
375  tex.Draw()
376  #save canvas
377  if ratios :
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)
386  ratios[r].Draw("")
387  unity.Draw("same")
388  for format in listFromats :
389  save = saveName+"."+format
390  c[keyHisto].Print(save)
391  return [c,leg,tex,pads]
392 #to create ratio plots from histograms
393 def createRatio(hVal,hRef):
394  ratio = []
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())
398  r.Add(hVal[h_i])
399  r.Divide(hRef[h_i])
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)
405  ratio.append(r)
406  return ratio
407 #to create ratio plots from TGraphErrors
408 def createRatioFromGraph(hVal,hRef):
409  ratio = []
410  for g_i in range(0,len(hVal)):
411  if hVal[g_i] is None :
412  ratio.append(None)
413  continue
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())
417  #loop over the N points
418  for p in range(0,hVal[g_i].GetN()-1):
419  #get point p
420  x = Double(0)
421  y = Double(0)
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)
427  #get the other point as xHist in [x,xbis]
428  xbis = Double(0)
429  ybis = Double(0)
430  #points are odered from high x to low x
431  if xHist>x :
432  if p==0 : continue
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)
436  else :
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)
440  if ybis==y :
441  #just take y at x
442  bin_p_valContent = y
443  bin_p_valContent_errP = y+yerr
444  bin_p_valContent_errM = y-yerr
445  else :
446  #do a linear extrapolation (equivalent to do Eval(xHist))
447  a=(ybis-y)/(xbis-x)
448  b=y-a*x
449  bin_p_valContent = a*xHist+b
450  #extrapolate the error
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
457  #fill val hist
458  histVal.SetBinContent(bin_p,bin_p_valContent)
459  histVal.SetBinError(bin_p,(bin_p_valContent_errP-bin_p_valContent_errM)/2)
460  #loop over the reference TGraph to get the corresponding point
461  for pRef in range(0,hRef[g_i].GetN()):
462  #get point pRef
463  xRef = Double(0)
464  yRef = Double(0)
465  hRef[g_i].GetPoint(pRef,xRef,yRef)
466  #take the first point as xRef < xHist
467  if xRef > xHist : continue
468  xReferr = hRef[g_i].GetErrorX(pRef)
469  yReferr = hRef[g_i].GetErrorY(pRef)
470  #get the other point as xHist in [xRef,xRefbis]
471  xRefbis = Double(0)
472  yRefbis = Double(0)
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)
476  if yRefbis==yRef :
477  #just take yRef at xRef
478  bin_p_refContent = yRef
479  bin_p_refContent_errP = yRef+yReferr
480  bin_p_refContent_errM = yRef-yReferr
481  else :
482  #do a linear extrapolation (equivalent to do Eval(xHist))
483  aRef=(ybis-y)/(xbis-x)
484  bRef=yRef-aRef*xRef
485  bin_p_refContent = aRef*xHist+bRef
486  #extrapolate the error
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
493  break
494  #fill ref hist
495  histRef.SetBinContent(bin_p,bin_p_refContent)
496  histRef.SetBinError(bin_p,(bin_p_refContent_errP-bin_p_refContent_errM)/2)
497  #do the ratio
498  histVal.Sumw2()
499  histRef.Sumw2()
500  histVal.Divide(histRef)
501  #ratio style
502  histVal.GetXaxis().SetRangeUser(0.,1.)
503  #histRef.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)
510  return ratio
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
histoStyle.graphProducer
def graphProducer(plot, histos, tagFlav="B", mistagFlav=["C","DUSG"], isVal=True)
Definition: histoStyle.py:240
histoStyle.createRatioFromGraph
def createRatioFromGraph(hVal, hRef)
Definition: histoStyle.py:408
str
#define str(s)
Definition: TestProcessor.cc:52
listHistos.Rebin
Rebin
Definition: listHistos.py:43
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
mps_setup.append
append
Definition: mps_setup.py:85
histoStyle.histoProducer
def histoProducer(plot, histos, keys, isVal=True)
Definition: histoStyle.py:184
hippyaddtobaddatafiles.cd
def cd(newdir)
Definition: hippyaddtobaddatafiles.py:40
histoStyle.savePlots
def savePlots(title, saveName, listFromats, plot, Histos, keyHisto, listLegend, options, ratios=None, legendName="")
Definition: histoStyle.py:314
histoStyle.createRatio
def createRatio(hVal, hRef)
Definition: histoStyle.py:393
gpuVertexFinder::while
while(__syncthreads_or(more))
Definition: gpuClusterTracksIterative.h:109