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

◆ createRatio()

def histoStyle.createRatio (   hVal,
  hRef 
)

Definition at line 393 of file histoStyle.py.

References isotrackApplyRegressor.range.

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

◆ createRatioFromGraph()

def histoStyle.createRatioFromGraph (   hVal,
  hRef 
)

Definition at line 408 of file histoStyle.py.

References isotrackApplyRegressor.range.

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

◆ graphProducer()

def histoStyle.graphProducer (   plot,
  histos,
  tagFlav = "B",
  mistagFlav = ["C",
  DUSG,
  isVal = True 
)

Definition at line 240 of file histoStyle.py.

References mps_setup.append, WZElectronSkims53X_cff.max, and isotrackApplyRegressor.range.

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

◆ histoProducer()

def histoStyle.histoProducer (   plot,
  histos,
  keys,
  isVal = True 
)

Definition at line 184 of file histoStyle.py.

References WZElectronSkims53X_cff.max, isotrackApplyRegressor.range, and listHistos.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 
239 #method to do a plot from a graph
def histoProducer(plot, histos, keys, isVal=True)
Definition: histoStyle.py:184

◆ savePlots()

def histoStyle.savePlots (   title,
  saveName,
  listFromats,
  plot,
  Histos,
  keyHisto,
  listLegend,
  options,
  ratios = None,
  legendName = "" 
)

Definition at line 314 of file histoStyle.py.

References hippyaddtobaddatafiles.cd(), print(), ecalTPGAnalyzer_cfg.Print, isotrackApplyRegressor.range, str, and gpuVertexFinder.while().

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
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def savePlots(title, saveName, listFromats, plot, Histos, keyHisto, listLegend, options, ratios=None, legendName="")
Definition: histoStyle.py:314
while(__syncthreads_or(more))
#define str(s)

Variable Documentation

◆ Banner

histoStyle.Banner

Definition at line 54 of file histoStyle.py.

◆ batch

histoStyle.batch

Definition at line 50 of file histoStyle.py.

◆ doRatio

histoStyle.doRatio

Definition at line 55 of file histoStyle.py.

Referenced by HcalLutAnalyzer.analyze().

◆ drawLegend

histoStyle.drawLegend

Definition at line 52 of file histoStyle.py.

◆ drawOption

◆ EtaPtBin

◆ fileNameRef

histoStyle.fileNameRef

Definition at line 42 of file histoStyle.py.

◆ fileNameVal

histoStyle.fileNameVal

Definition at line 41 of file histoStyle.py.

◆ listFlavors

histoStyle.listFlavors

Definition at line 83 of file histoStyle.py.

◆ listFromats

histoStyle.listFromats

Definition at line 127 of file histoStyle.py.

◆ listHistos

histoStyle.listHistos

Definition at line 136 of file histoStyle.py.

◆ listTag

histoStyle.listTag

Definition at line 68 of file histoStyle.py.

◆ mapColor

histoStyle.mapColor

Definition at line 93 of file histoStyle.py.

◆ mapLineStyle

histoStyle.mapLineStyle

Definition at line 122 of file histoStyle.py.

◆ mapLineWidth

histoStyle.mapLineWidth

Definition at line 118 of file histoStyle.py.

◆ mapMarker

histoStyle.mapMarker

Definition at line 114 of file histoStyle.py.

◆ pathInFile

histoStyle.pathInFile

Definition at line 58 of file histoStyle.py.

◆ printBanner

histoStyle.printBanner

Definition at line 53 of file histoStyle.py.

◆ RefRel

histoStyle.RefRel

Definition at line 45 of file histoStyle.py.

◆ RefSample

histoStyle.RefSample

Definition at line 48 of file histoStyle.py.

◆ unity

histoStyle.unity

Definition at line 131 of file histoStyle.py.

◆ ValRel

histoStyle.ValRel

Definition at line 44 of file histoStyle.py.

◆ ValSample

histoStyle.ValSample

Definition at line 47 of file histoStyle.py.

◆ weight

histoStyle.weight

Definition at line 51 of file histoStyle.py.