CMS 3D CMS Logo

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