CMS 3D CMS Logo

ntuplePlotting.py
Go to the documentation of this file.
1 import ROOT
2 
3 import Validation.RecoTrack.plotting.plotting as plotting
4 
5 def saveHistograms(tdirectory, histos):
6  for h in histos:
7  h.SetDirectory(tdirectory)
8 
9 def applyStyle(h, color, markerStyle):
10  h.SetMarkerStyle(markerStyle)
11  h.SetMarkerColor(color)
12  h.SetMarkerSize(1.2)
13  h.SetLineColor(color)
14  h.SetLineWidth(2)
15 
16 def draw(name, histos, styles, legendLabels=[],
17  xtitle=None, ytitle=None,
18  drawOpt="HIST",
19  legendDx=0, legendDy=0, legendDw=0, legendDh=0,
20  xmin=None, ymin=0, xmax=None, ymax=None, xlog=False, ylog=False,
21  xgrid=True, ygrid=True,
22  ratio=False, ratioYmin=0.5, ratioYmax=1.5, ratioYTitle=plotting._ratioYTitle
23  ):
24 
25  width = 600
26  height = 600
27  ratioFactor = 1.25
28 
29  if ratio:
30  height = int(height*ratioFactor)
31  c = plotting._createCanvas(name, width, height)
32  if ratio:
33  plotting._modifyPadForRatio(c, ratioFactor)
34 
35  bounds = plotting._findBounds(histos, ylog, xmin, xmax, ymin, ymax)
36  args = {"nrows": 1}
37  if ratio:
38  ratioBounds = (bounds[0], ratioYmin, bounds[2], ratioYmax)
39  frame = plotting.FrameRatio(c, bounds, ratioBounds, ratioFactor, ratioYTitle=ratioYTitle, **args)
40  #frame._frameRatio.GetYaxis().SetLabelSize(0.12)
41  else:
42  frame = plotting.Frame(c, bounds, **args)
43 
44  if xtitle is not None:
45  frame.setXTitle(xtitle)
46  if ytitle is not None:
47  frame.setYTitle(ytitle)
48 
49  frame.setLogx(xlog)
50  frame.setLogy(ylog)
51  frame.setGridx(xgrid)
52  frame.setGridy(ygrid)
53 
54  if ratio:
55  frame._pad.cd()
56  for h, st in zip(histos, styles):
57  st(h)
58  h.Draw(drawOpt+" same")
59 
60  ratios = None
61  if ratio:
62  frame._padRatio.cd()
63  ratios = plotting._calculateRatios(histos)
64  for r in ratios[1:]:
65  r.draw()
66  frame._pad.cd()
67 
68  if len(legendLabels) > 0:
69  if len(legendLabels) != len(histos):
70  raise Exception("Got %d histos but %d legend labels" % (len(histos), len(legendLabels)))
71  lx1 = 0.6
72  lx2 = 0.9
73  ly1 = 0.7
74  ly2 = 0.85
75 
76  lx1 += legendDx
77  lx2 += legendDx
78  ly1 += legendDy
79  ly2 += legendDy
80 
81  lx2 += legendDw
82  ly1 -= legendDh
83 
84  legend = ROOT.TLegend(lx1, ly1, lx2, ly2)
85  legend.SetLineColor(1)
86  legend.SetLineWidth(1)
87  legend.SetLineStyle(1)
88  legend.SetFillColor(0)
89  legend.SetMargin(0.07)
90  legend.SetBorderSize(0)
91 
92  for h, l in zip(histos, legendLabels):
93  legend.AddEntry(h, l, "L")
94 
95  legend.Draw()
96 
97  frame._pad.cd()
98 
99  c.Update()
100  c.RedrawAxis()
101  c.SaveAs(name+".png")
102  c.SaveAs(name+".pdf")
def _findBounds(th1s, ylog, xmin=None, xmax=None, ymin=None, ymax=None)
Definition: plotting.py:454
def applyStyle(h, color, markerStyle)
def _calculateRatios(histos, ratioUncertainty=False)
Definition: plotting.py:144
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def saveHistograms(tdirectory, histos)
def draw(name, histos, styles, legendLabels=[], xtitle=None, ytitle=None, drawOpt="HIST", legendDx=0, legendDy=0, legendDw=0, legendDh=0, xmin=None, ymin=0, xmax=None, ymax=None, xlog=False, ylog=False, xgrid=True, ygrid=True, ratio=False, ratioYmin=0.5, ratioYmax=1.5, ratioYTitle=plotting._ratioYTitle)
def _modifyPadForRatio(pad, ratioFactor)
Definition: plotting.py:115
def _createCanvas(name, width, height)
Definition: plotting.py:105