CMS 3D CMS Logo

ntuplePlotting.py
Go to the documentation of this file.
1 import collections
2 import itertools
3 
4 import ROOT
5 
6 import Validation.RecoTrack.plotting.plotting as plotting
7 import six
8 
9 def saveHistograms(tdirectory, histos):
10  for h in histos:
11  h.SetDirectory(tdirectory)
12 
13 def applyStyle(h, color, markerStyle):
14  h.SetMarkerStyle(markerStyle)
15  h.SetMarkerColor(color)
16  h.SetMarkerSize(1.2)
17  h.SetLineColor(color)
18  h.SetLineWidth(2)
19 
20 # https://stackoverflow.com/questions/6076270/python-lambda-function-in-list-comprehensions
21 _defaultStyles = [(lambda c, m: (lambda h: applyStyle(h, c, m)))(color, ms) for color, ms in itertools.izip(plotting._plotStylesColor, plotting._plotStylesMarker)]
22 
23 _ratioFactor = 1.25
24 
25 def draw(name, histos, styles=_defaultStyles, legendLabels=[], **kwargs):
26  width = 600
27  height = 600
28  ratioFactor = 1.25
29 
30  args = {}
31  args.update(kwargs)
32  if not "ratioFactor" in args:
33  args["ratioFactor"] = _ratioFactor
34  ratio = args.get("ratio", False)
35 
36  if ratio:
37  height = int(height*ratioFactor)
38  c = plotting._createCanvas(name, width, height)
39  if ratio:
40  plotting._modifyPadForRatio(c, ratioFactor)
41 
42  frame = drawSingle(c, histos, styles, **args)
43 
44  if len(legendLabels) > 0:
45  if len(legendLabels) != len(histos):
46  raise Exception("Got %d histos but %d legend labels" % (len(histos), len(legendLabels)))
47  lx1 = 0.6
48  lx2 = 0.9
49  ly1 = 0.7
50  ly2 = 0.85
51 
52  lx1 += legendDx
53  lx2 += legendDx
54  ly1 += legendDy
55  ly2 += legendDy
56 
57  lx2 += legendDw
58  ly1 -= legendDh
59 
60  legend = ROOT.TLegend(lx1, ly1, lx2, ly2)
61  legend.SetLineColor(1)
62  legend.SetLineWidth(1)
63  legend.SetLineStyle(1)
64  legend.SetFillColor(0)
65  legend.SetMargin(0.07)
66  legend.SetBorderSize(0)
67 
68  for h, l in zip(histos, legendLabels):
69  legend.AddEntry(h, l, "L")
70 
71  legend.Draw()
72 
73  frame._pad.cd()
74 
75  c.Update()
76  c.RedrawAxis()
77  c.SaveAs(name+".png")
78  c.SaveAs(name+".pdf")
79 
80 
81 def drawSingle(pad, histos, styles=_defaultStyles,
82  nrows=1,
83  xtitle=None, ytitle=None,
84  drawOpt="HIST",
85  legendDx=0, legendDy=0, legendDw=0, legendDh=0,
86  xmin=None, ymin=0, xmax=None, ymax=None, xlog=False, ylog=False,
87  xgrid=True, ygrid=True,
88  ratio=False, ratioYmin=0.5, ratioYmax=1.5, ratioYTitle=plotting._ratioYTitle, ratioFactor=1.25):
89 
90  bounds = plotting._findBounds(histos, ylog, xmin, xmax, ymin, ymax)
91  if ratio:
92  ratioBounds = (bounds[0], ratioYmin, bounds[2], ratioYmax)
93  frame = plotting.FrameRatio(pad, bounds, ratioBounds, ratioFactor, ratioYTitle=ratioYTitle, nrows=nrows)
94  #frame._frameRatio.GetYaxis().SetLabelSize(0.12)
95  else:
96  frame = plotting.Frame(pad, bounds, nrows=nrows)
97 
98  if xtitle is not None:
99  frame.setXTitle(xtitle)
100  if ytitle is not None:
101  frame.setYTitle(ytitle)
102 
103  frame.setLogx(xlog)
104  frame.setLogy(ylog)
105  frame.setGridx(xgrid)
106  frame.setGridy(ygrid)
107 
108  if ratio:
109  frame._pad.cd()
110  for i, h in enumerate(histos):
111  st = styles[i%len(styles)]
112  st(h)
113  h.Draw(drawOpt+" same")
114 
115  ratios = None
116  if ratio:
117  frame._padRatio.cd()
118  ratios = plotting._calculateRatios(histos)
119  for r in ratios[1:]:
120  r.draw()
121  frame._pad.cd()
122 
123  return frame
124 
125 
126 def drawMany(name, histoDicts, styles=_defaultStyles, opts={}, ncolumns=4):
127  if len(histoDicts) == 0:
128  return
129 
130  histoNames = histoDicts[0].keys()
131  ratio = False
132  ratioFactor = _ratioFactor
133  for opt in six.itervalues(opts):
134  if "ratio" in opt:
135  ratio = True
136  if "ratioFactor" in opt:
137  ratioFactor = max(ratioFactor, opt["ratioFactor"])
138 
139  nhistos = len(histoNames)
140  nrows = int((nhistos+ncolumns-1)/ncolumns)
141 
142  width = 500*ncolumns
143  height = 500*nrows
144  if ratio:
145  height = int(_ratioFactor*height)
146 
147  canvas = plotting._createCanvas(name, width, height)
148  canvas.Divide(ncolumns, nrows)
149 
150  histos = collections.defaultdict(list)
151 
152  for d in histoDicts:
153  for n, h in six.iteritems(d):
154  histos[n].append(h)
155 
156  for i, histoName in enumerate(histoNames):
157  pad = canvas.cd(i+1)
158 
159  args = {}
160  args.update(opts.get(histoName, {}))
161  if "ratio" in args:
162  if not "ratioFactor" in args:
163  args["ratioFactor"] = _ratioFactor # use the default, not the max
164  plotting._modifyPadForRatio(pad, args["ratioFactor"])
165 
166  frame = drawSingle(pad, histos[histoName], styles, nrows, **args)
167  frame._pad.cd()
168  frame._pad.Update()
169  frame._pad.RedrawAxis()
170 
171  canvas.SaveAs(name+".png")
172  canvas.SaveAs(name+".pdf")
def _findBounds(th1s, ylog, xmin=None, xmax=None, ymin=None, ymax=None)
Definition: plotting.py:455
def drawSingle(pad, histos, styles=_defaultStyles, nrows=1, 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, ratioFactor=1.25)
def applyStyle(h, color, markerStyle)
def _calculateRatios(histos, ratioUncertainty=False)
Definition: plotting.py:146
def draw(name, histos, styles=_defaultStyles, legendLabels=[], kwargs)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def drawMany(name, histoDicts, styles=_defaultStyles, opts={}, ncolumns=4)
def saveHistograms(tdirectory, histos)
def _modifyPadForRatio(pad, ratioFactor)
Definition: plotting.py:117
def _createCanvas(name, width, height)
Definition: plotting.py:107