CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions | Variables
histoStyle Namespace Reference

Functions

def createRatio
 
def createRatioFromGraph
 
def graphProducer
 
def histoProducer
 
def savePlots
 

Variables

string Banner = "CMS Preliminary"
 
 batch = False
 
 doRatio = True
 
 drawLegend = False
 
string drawOption = ""
 
list EtaPtBin
 
string fileNameRef = "BTagRelVal_TTbar_Startup_612SLHC1_14.root"
 
string fileNameVal = "BTagRelVal_TTbar_Startup_14_612SLHC2.root"
 
list listFlavors
 
list listFromats
 
list listHistos
 
list listTag
 
dictionary mapColor
 
dictionary mapLineStyle
 
dictionary mapLineWidth
 
dictionary mapMarker
 
string pathInFile = "/DQMData/Run 1/Btag/Run summary/"
 
 printBanner = False
 
string RefRel = "612SLHC1_14"
 
string RefSample = "TTbar_FullSim"
 
tuple unity = TF1("unity","1",-1000,1000)
 
string ValRel = "612SLHC2_14"
 
string ValSample = "TTbar_FullSim"
 
int weight = 1
 

Function Documentation

def histoStyle.createRatio (   hVal,
  hRef 
)

Definition at line 392 of file histoStyle.py.

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
#to create ratio plots from TGraphErrors
def createRatio
Definition: histoStyle.py:392
def histoStyle.createRatioFromGraph (   hVal,
  hRef 
)

Definition at line 407 of file histoStyle.py.

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
def createRatioFromGraph
Definition: histoStyle.py:407
def histoStyle.graphProducer (   plot,
  histos,
  tagFlav = "B",
  mistagFlav = ["C",
  DUSG,
  isVal = True 
)

Definition at line 239 of file histoStyle.py.

References python.multivaluedict.append(), and bookConverter.max.

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 
#method to draw the plot and save it
def graphProducer
Definition: histoStyle.py:239
def histoStyle.histoProducer (   plot,
  histos,
  keys,
  isVal = True 
)

Definition at line 183 of file histoStyle.py.

References bookConverter.max, and MultipleCompare.Rebin().

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 
#method to do a plot from a graph
def histoProducer
Definition: histoStyle.py:183
def histoStyle.savePlots (   title,
  saveName,
  listFromats,
  plot,
  Histos,
  keyHisto,
  listLegend,
  options,
  ratios = None,
  legendName = "" 
)

Definition at line 313 of file histoStyle.py.

References ecalTPGAnalyzer_cfg.Print.

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]
#to create ratio plots from histograms
def savePlots
Definition: histoStyle.py:313

Variable Documentation

string histoStyle.Banner = "CMS Preliminary"

Definition at line 53 of file histoStyle.py.

histoStyle.batch = False

Definition at line 49 of file histoStyle.py.

histoStyle.doRatio = True

Definition at line 54 of file histoStyle.py.

histoStyle.drawLegend = False

Definition at line 51 of file histoStyle.py.

string histoStyle.drawOption = ""

Definition at line 55 of file histoStyle.py.

Referenced by drawHistograms(), and TauDQMHistPlotter.endRun().

list histoStyle.EtaPtBin
Initial value:
1 = [
2  "GLOBAL",
3  #"ETA_0-1v4",
4  #"ETA_1v4-2v4",
5  #"PT_50-80",
6  #"PT_80-120",
7  ]

Definition at line 59 of file histoStyle.py.

Referenced by BTagPerformanceHarvester.getEtaPtBin(), BTagPerformanceAnalyzerOnData.getEtaPtBin(), and BTagPerformanceAnalyzerMC.getEtaPtBin().

string histoStyle.fileNameRef = "BTagRelVal_TTbar_Startup_612SLHC1_14.root"

Definition at line 41 of file histoStyle.py.

string histoStyle.fileNameVal = "BTagRelVal_TTbar_Startup_14_612SLHC2.root"

Definition at line 40 of file histoStyle.py.

list histoStyle.listFlavors
Initial value:
1 = [
2  #"ALL",
3  "B",
4  "C",
5  #"G",
6  #"DUS",
7  "DUSG",
8  #"NI",
9  ]

Definition at line 82 of file histoStyle.py.

list histoStyle.listFromats
Initial value:
1 = [
2  "gif",
3  ]

Definition at line 126 of file histoStyle.py.

list histoStyle.listHistos

Definition at line 135 of file histoStyle.py.

list histoStyle.listTag
Initial value:
1 = [
2  "CSV",
3  "CSVMVA",
4  "JP",
5  "JBP",
6  "TCHE",
7  "TCHP",
8  "SSVHE",
9  "SSVHP",
10  "SMT",
11  #"SMTIP3d",
12  #"SMTPt",
13  "SET",
14  ]

Definition at line 67 of file histoStyle.py.

dictionary histoStyle.mapColor
Initial value:
1 = {
2  "ALL" : 4 ,
3  "B" : 3 ,
4  "C" : 1 ,
5  "G" : 2 ,
6  "DUS" : 2 ,
7  "DUSG" : 2 ,
8  "NI" : 5 ,
9  "CSV" : 5 ,
10  "CSVMVA" : 6 ,
11  "JP" : 3 ,
12  "JBP" : 9 ,
13  "TCHE" : 1,
14  "TCHP" : 2,
15  "SSVHE" : 4,
16  "SSVHP" : 7,
17  "SMT" : 8 ,
18  "SMTIP3d" : 11 ,
19  "SMTPt" : 12
20  }

Definition at line 92 of file histoStyle.py.

dictionary histoStyle.mapLineStyle
Initial value:
1 = {
2  "Val" : 2,
3  "Ref" : 1
4  }

Definition at line 121 of file histoStyle.py.

dictionary histoStyle.mapLineWidth
Initial value:
1 = {
2  "Val" : 3,
3  "Ref" : 2
4  }

Definition at line 117 of file histoStyle.py.

dictionary histoStyle.mapMarker
Initial value:
1 = {
2  "Val" : 22,
3  "Ref" : 8
4  }

Definition at line 113 of file histoStyle.py.

string histoStyle.pathInFile = "/DQMData/Run 1/Btag/Run summary/"

Definition at line 57 of file histoStyle.py.

histoStyle.printBanner = False

Definition at line 52 of file histoStyle.py.

string histoStyle.RefRel = "612SLHC1_14"

Definition at line 44 of file histoStyle.py.

string histoStyle.RefSample = "TTbar_FullSim"

Definition at line 47 of file histoStyle.py.

tuple histoStyle.unity = TF1("unity","1",-1000,1000)

Definition at line 130 of file histoStyle.py.

string histoStyle.ValRel = "612SLHC2_14"

Definition at line 43 of file histoStyle.py.

string histoStyle.ValSample = "TTbar_FullSim"

Definition at line 46 of file histoStyle.py.

int histoStyle.weight = 1

Definition at line 50 of file histoStyle.py.