CMS 3D CMS Logo

Functions | Variables
histoStyle Namespace Reference

Functions

def createRatio (hVal, hRef)
 
def createRatioFromGraph (hVal, hRef)
 
def graphProducer (plot, histos, tagFlav="B", mistagFlav=["C", DUSG, isVal=True)
 
def histoProducer (plot, histos, keys, isVal=True)
 
def savePlots (title, saveName, listFromats, plot, Histos, keyHisto, listLegend, options, ratios=None, legendName="")
 

Variables

 Banner
 
 batch
 
 doRatio
 
 drawLegend
 
 drawOption
 
 EtaPtBin
 
 fileNameRef
 
 fileNameVal
 
 listFlavors
 
 listFromats
 
 listHistos
 
 listTag
 
 mapColor
 
 mapLineStyle
 
 mapLineWidth
 
 mapMarker
 
 pathInFile
 
 printBanner
 
 RefRel
 
 RefSample
 
 unity
 
 ValRel
 
 ValSample
 
 weight
 

Function Documentation

def histoStyle.createRatio (   hVal,
  hRef 
)

Definition at line 392 of file histoStyle.py.

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
def createRatio(hVal, hRef)
Definition: histoStyle.py:392
def histoStyle.createRatioFromGraph (   hVal,
  hRef 
)

Definition at line 407 of file histoStyle.py.

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
510 
def createRatioFromGraph(hVal, hRef)
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 mps_alisetup.append, and hpstanc_transforms.max.

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

Definition at line 183 of file histoStyle.py.

References hpstanc_transforms.max, and listHistos.Rebin.

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
def histoProducer(plot, histos, keys, isVal=True)
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 harvestTrackValidationPlots.str.

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
def savePlots(title, saveName, listFromats, plot, Histos, keyHisto, listLegend, options, ratios=None, legendName="")
Definition: histoStyle.py:313

Variable Documentation

histoStyle.Banner

Definition at line 53 of file histoStyle.py.

histoStyle.batch

Definition at line 49 of file histoStyle.py.

histoStyle.doRatio

Definition at line 54 of file histoStyle.py.

Referenced by HcalLutAnalyzer.analyze().

histoStyle.drawLegend

Definition at line 51 of file histoStyle.py.

histoStyle.drawOption

Definition at line 55 of file histoStyle.py.

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

histoStyle.fileNameRef

Definition at line 41 of file histoStyle.py.

histoStyle.fileNameVal

Definition at line 40 of file histoStyle.py.

histoStyle.listFlavors

Definition at line 82 of file histoStyle.py.

histoStyle.listFromats

Definition at line 126 of file histoStyle.py.

histoStyle.listHistos

Definition at line 135 of file histoStyle.py.

histoStyle.listTag

Definition at line 67 of file histoStyle.py.

histoStyle.mapColor

Definition at line 92 of file histoStyle.py.

histoStyle.mapLineStyle

Definition at line 121 of file histoStyle.py.

histoStyle.mapLineWidth

Definition at line 117 of file histoStyle.py.

histoStyle.mapMarker

Definition at line 113 of file histoStyle.py.

histoStyle.pathInFile

Definition at line 57 of file histoStyle.py.

histoStyle.printBanner

Definition at line 52 of file histoStyle.py.

histoStyle.RefRel

Definition at line 44 of file histoStyle.py.

histoStyle.RefSample

Definition at line 47 of file histoStyle.py.

histoStyle.unity

Definition at line 130 of file histoStyle.py.

histoStyle.ValRel

Definition at line 43 of file histoStyle.py.

histoStyle.ValSample

Definition at line 46 of file histoStyle.py.

histoStyle.weight

Definition at line 50 of file histoStyle.py.