CMS 3D CMS Logo

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