-
Notifications
You must be signed in to change notification settings - Fork 42
/
Plot2D.py
345 lines (332 loc) · 16.2 KB
/
Plot2D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# -*- coding: utf-8 -*-
# Author: Izaak Neutelings (May 2021)
# Description: Class to automatically make CMS 2D plot.
from TauFW.Plotter.plot.Plot import *
import TauFW.Plotter.plot.Plot
_lcolors2D = [ kOrange+7, kMagenta-4 ] + TauFW.Plotter.plot.Plot._lcolors # kOrange-3,
class Plot2D(Plot):
"""Class to automatically make CMS 2D plot."""
def __init__(self, *args, **kwargs):
"""
Initialize with list of histograms:
Plot2D(hist)
or with a variable (string or Variable object) as well:
Plot2D(xvar,yvar,hists)
"""
hists, vars = [ ], [ ]
self.verbosity = LOG.getverbosity(kwargs)
for arg in args:
if isinstance(arg,TH2):
hists.append(arg)
else:
vars.append(arg)
if len(hists)==1 and isinstance(hists[0],TH2):
hist = hists[0]
if kwargs.get('clone',False):
hist = hist.Clone(hist.GetName()+"_clone_Plot2D")
else:
LOG.warn("Plot2D.__init__: Did not recognize input: %s"%(args,))
if len(vars)==1:
LOG.warn("Plot2D.__init__: Need one more variable!")
if len(vars)>=2:
xvariable = vars[0]
yvariable = vars[1]
else:
xvariable = None
yvariable = None
self.canvas = None
self.legend = None
self.ratio = None # not used
self.frame = None
self.garbage = [ ]
self.profiles = [ ]
self.graphs = [ ]
self.lines = [ ]
self.texts = [ ]
if isinstance(xvariable,Variable) and isinstance(yvariable,Variable):
self.xvariable = xvariable
self.yvariable = yvariable
self.xtitle = kwargs.get('xtitle', xvariable.title )
self.xmin = kwargs.get('xmin', xvariable.xmin )
self.xmax = kwargs.get('xmax', xvariable.xmax )
self.logx = kwargs.get('logx', xvariable.logx )
self.xbinlabels = kwargs.get('xbinlabels', xvariable.binlabels )
self.ytitle = kwargs.get('ytitle', yvariable.title )
self.ymin = kwargs.get('ymin', yvariable.xmin )
self.ymax = kwargs.get('ymax', yvariable.xmax )
self.logy = kwargs.get('logy', xvariable.logx )
self.ybinlabels = kwargs.get('ybinlabels', yvariable.binlabels )
self.ztitle = kwargs.get('ztitle', hist.GetZaxis().GetTitle() )
self.zunits = kwargs.get('zunits', False ) # units to be added to z title
self.logz = kwargs.get('logz', xvariable.logy )
self.position = kwargs.get('position', xvariable.position )
self.name = kwargs.get('name', "%s_vs_%s"%(yvariable.filename,xvariable.filename))
else:
self.xvariable = xvariable or hist.GetXaxis().GetTitle()
self.yvariable = yvariable or hist.GetYaxis().GetTitle()
self.xtitle = kwargs.get('xtitle', xvariable )
self.xmin = kwargs.get('xmin', hist.GetXaxis().GetXmin() )
self.xmax = kwargs.get('xmax', hist.GetXaxis().GetXmax() )
self.logx = kwargs.get('logx', False )
self.xbinlabels = kwargs.get('xbinlabels', None )
self.ytitle = kwargs.get('ytitle', yvariable )
self.ymin = kwargs.get('ymin', hist.GetYaxis().GetXmin() )
self.ymax = kwargs.get('ymax', hist.GetYaxis().GetXmax() )
self.logy = kwargs.get('logy', False )
self.ybinlabels = kwargs.get('ybinlabels', None )
self.ztitle = kwargs.get('ztitle', hist.GetZaxis().GetTitle() )
self.logz = kwargs.get('logz', False )
self.position = kwargs.get('position', "" )
self.name = kwargs.get('name', "%s_vs_%s"%(yvariable,xvariable))
self.xunits = kwargs.get('xunits', False ) # units to be added to x title
self.yunits = kwargs.get('yunits', False ) # units to be added to y title
self.zunits = kwargs.get('zunits', False ) # units to be added to z title
self.zmin = kwargs.get('zmin', hist.GetMinimum() )
self.zmax = kwargs.get('zmax', hist.GetMaximum() )
self.hist = hist
def draw(self,*args,**kwargs):
"""Central method of Plot class: make plot with canvas, axis, error, ratio..."""
hist = self.hist
verbosity = LOG.getverbosity(self,kwargs)
yoffset = kwargs.get('yoffset', 1.35 if self.hist.GetYaxis().GetXmax()>=1000 else 1.15 )
option = kwargs.get('option', args[0] if args else 'COLZ' ) # COLZTEXT44
title = kwargs.get('title', "" )
xtitle = kwargs.get('xtitle', self.xtitle )
ytitle = kwargs.get('ytitle', self.ytitle )
ztitle = kwargs.get('ztitle', self.ztitle )
xunits = kwargs.get('xunits', self.xunits ) # units to be added to x title
yunits = kwargs.get('yunits', self.yunits ) # units to be added to y title
zunits = kwargs.get('zunits', self.zunits ) # units to be added to z title
zcenter = kwargs.get('zcenter', "Events" not in ztitle )
xmin = kwargs.get('xmin', self.xmin )
xmax = kwargs.get('xmax', self.xmax )
ymin = kwargs.get('ymin', self.ymin )
ymax = kwargs.get('ymax', self.ymax )
zmin = kwargs.get('zmin', self.zmin )
zmax = kwargs.get('zmax', self.zmax )
logx = kwargs.get('logx', self.logx )
logy = kwargs.get('logy', self.logy )
logz = kwargs.get('logz', self.logz )
tsize = kwargs.get('tsize', 0.050 )
tcolor = kwargs.get('tcolor', kBlack )
format = kwargs.get('format', ".2f" )
legend = kwargs.get('legend', False )
grid = kwargs.get('grid', False )
lcolor = kwargs.get('lcolor', kBlack )
cwidth = kwargs.get('width', 850 )
cheight = kwargs.get('height', 750 )
position = kwargs.get('position', self.position )
lmargin = 0.16 if self.hist.GetYaxis().GetXmax()>=1000 else 0.14
rmargin = (0.12 if logz else 0.16 if hist.GetMaximum()>=1000 else 0.14) + (0.06 if ztitle else 0.0)
yoffset = 1.35 if (not logy and self.hist.GetYaxis().GetXmax()>=1000) else 1.15
tmargin = kwargs.get('tmargin', 0.05 )
bmargin = kwargs.get('bmargin', 0.14 )
lmargin = kwargs.get('lmargin', lmargin )
rmargin = kwargs.get('rmargin', rmargin )
xoffset = kwargs.get('xoffset', 1.02 )
yoffset = kwargs.get('yoffset', yoffset )
zoffset = kwargs.get('zoffset', 5.5 if logz else 7.3 )*rmargin
labelsize = kwargs.get('labelsize', 0.048 )
xbinlabels = kwargs.get('xbinlabels', self.xbinlabels )
ybinlabels = kwargs.get('ybinlabels', self.ybinlabels )
xlabeloption = kwargs.get('xlabeloption', None ) # 'h'=horizontal, 'v'=vertical
ylabeloption = kwargs.get('ylabeloption', None ) # 'h'=horizontal, 'v'=vertical
xlabelsize = kwargs.get('xlabelsize', labelsize )*(1.7 if xbinlabels else 1)
ylabelsize = kwargs.get('ylabelsize', labelsize )*(1.7 if ybinlabels else 1)
xlabeloffset = kwargs.get('xlabeloffset', 0.005 if xbinlabels else -0.004 if logx else 0.01 )
ylabeloffset = kwargs.get('ylabeloffset', 0.008 if ybinlabels else 0.005 )
zlabeloffset = kwargs.get('zlabeloffset', -0.003 if logz else 0.01 )
markersize = kwargs.get('markersize', 1.0 )
self.xmin, self.xmax = xmin, xmax
self.ymin, self.ymax = ymin, ymax
self.zmin, self.zmax = zmin, zmax
resetx = logx and hist.GetXaxis().GetBinLowEdge(1)<=0 and xmax>0
resety = logy and hist.GetYaxis().GetBinLowEdge(1)<=0 and ymax>0
if verbosity>=2:
print(">>> Plot2D.draw: xmin=%s, xmax=%s, ymin=%s, ymax=%s, zmin=%s, zmax=%s"%(xmin,xmax,ymin,ymax,zmin,zmax))
print(">>> Plot2D.draw: logx=%s, logy=%s, logz=%s, resetx=%r, resety=%r"%(logx,logy,logz,resetx,resety))
print(">>> Plot2D.draw: tmargin=%s, bmargin=%s, lmargin=%s, rmargin=%s"%(tmargin,bmargin,lmargin,rmargin))
print(">>> Plot2D.draw: xoffset=%s, yoffset=%s, zoffset=%s"%(xoffset,yoffset,zoffset))
print(">>> Plot2D.draw: xlabeloffset=%s, xlabeloffset=%s, xlabeloffset=%s"%(xlabeloffset,ylabeloffset,zlabeloffset))
# CANVAS
canvas = TCanvas("canvas","canvas",100,100,int(cwidth),int(cheight))
canvas.SetFillColor(0)
canvas.SetBorderMode(0)
canvas.SetFrameFillStyle(0)
canvas.SetFrameBorderMode(0)
canvas.SetMargin(lmargin,rmargin,bmargin,tmargin) # LRBT
#canvas.SetTickx(0); canvas.SetTicky(0)
canvas.SetTicks(1,1)
if grid:
canvas.SetGrid()
canvas.cd()
if logx:
canvas.Update(); canvas.SetLogx()
if xmin<=0 and xmax>0:
xmin = xmax/1e4
if logy:
canvas.Update(); canvas.SetLogy()
if ymin<=0 and ymax>0:
ymin = ymax/1e4
if logz:
canvas.Update(); canvas.SetLogz()
if zmin<=0 and zmax>0:
zmin = zmax/1e4
self.canvas = canvas
# AXES
#hist.SetTitle("") # assume gStyle.SetOptTitle(False) in TDR style
if resetx or resety: # create new TH2 frame to have better control over the axis ranges
LOG.verb("Plot2D.draw: creating new TH2 frame...",verbosity,1)
hist = resetrange(hist,xmin,xmax,ymin,ymax,verb=verbosity)
frame = hist
self.garbage.append(hist) # save for later deletion
#fname = "%s_frame"%(hist.GetName())
#frame = TH2F(fname,fname,10,xmin,xmax,10,ymin,ymax) # doest not work ?
#hist.SetMinimum(zmin)
#hist.SetMaximum(zmax)
else:
frame = hist
frame.GetXaxis().SetTitleSize(0.058)
frame.GetYaxis().SetTitleSize(0.058)
frame.GetZaxis().SetTitleSize(0.056)
frame.GetXaxis().SetLabelSize(xlabelsize)
frame.GetYaxis().SetLabelSize(ylabelsize)
frame.GetZaxis().SetLabelSize(0.044)
frame.GetXaxis().SetLabelOffset(xlabeloffset)
frame.GetYaxis().SetLabelOffset(ylabeloffset)
frame.GetZaxis().SetLabelOffset(zlabeloffset)
frame.GetXaxis().SetTitleOffset(0.97)
frame.GetXaxis().SetTitleOffset(xoffset)
frame.GetYaxis().SetTitleOffset(yoffset)
frame.GetZaxis().SetTitleOffset(zoffset)
frame.GetZaxis().CenterTitle(zcenter)
frame.GetXaxis().SetTitle(makelatex(xtitle,units=xunits))
frame.GetYaxis().SetTitle(makelatex(ytitle,units=yunits))
frame.GetZaxis().SetTitle(makelatex(ztitle,units=zunits))
frame.GetXaxis().SetRangeUser(xmin,xmax)
frame.GetYaxis().SetRangeUser(ymin,ymax)
frame.SetMinimum(zmin)
frame.SetMaximum(zmax)
#frame.Draw()
self.frame = frame
# DRAW
if 'TEXT' in option.upper():
gStyle.SetPaintTextFormat(format)
hist.SetMarkerSize(markersize)
hist.SetMarkerColor(tcolor)
#hist.SetMarkerSize(1)
hist.Draw(option+'SAME')
canvas.RedrawAxis()
# alphanumerical bin labels
if xbinlabels:
nxbins = hist.GetXaxis().GetNbins()
if len(xbinlabels)<nxbins:
LOG.warn("Plot2D.plot: len(xbinlabels)=%d < %d=nxbins"%(len(xbinlabels),nxbins))
for i, xbinlabels in zip(range(1,nxbins+1),xbinlabels):
hist.GetXaxis().SetBinLabel(i,makelatex(xbinlabels,units=False))
if ybinlabels:
nybins = hist.GetYaxis().GetNbins()
if len(ybinlabels)<nybins:
LOG.warn("Plot2D.plot: len(ybinlabels)=%d < %d=nybins"%(len(ybinlabels),nybins))
for i, ybinlabels in zip(range(1,nybins+1),ybinlabels):
hist.GetYaxis().SetBinLabel(i,makelatex(ybinlabels,units=False))
if xlabeloption: # https://root.cern.ch/doc/master/classTAxis.html#a05dd3c5b4c3a1e32213544e35a33597c
frame.GetXaxis().LabelsOption(xlabeloption) # 'h'=horizontal, 'v'=vertical
if ylabeloption: # https://root.cern.ch/doc/master/classTAxis.html#a05dd3c5b4c3a1e32213544e35a33597c
frame.GetYaxis().LabelsOption(ylabeloption) # 'h'=horizontal, 'v'=vertical
# CMS STYLE
if CMSStyle.lumiText:
CMSStyle.setCMSLumiStyle(gPad,0)
return self.canvas
def drawprofile(self,axes='x',opt="",entries=[ ],**kwargs):
"""Draw profile on canvas."""
# Options: https://root.cern.ch/doc/master/classTProfile.html#a1ff9340284c73ce8762ab6e7dc0e6725
self.profiles = [ ]
# MAKE PROFILE
for i, axis in enumerate(axes):
profile = self.hist.ProfileX(opt) if "x"==axis.lower() else self.hist.ProfileY(opt)
color = kRed
if i<len(entries):
profile.SetTitle(entries[i]) # for legend
profile.SetLineColor(color)
profile.SetMarkerColor(color)
profile.SetLineWidth(3)
profile.SetLineStyle(1)
profile.SetMarkerStyle(20)
profile.SetMarkerSize(0.9)
profile.Draw('SAME')
self.profiles.append(profile)
return self.profiles
def close(self,keep=False,**kwargs):
"""Close canvas and delete the histograms."""
verbosity = LOG.getverbosity(self,kwargs)
if self.canvas:
self.canvas.Close()
if not keep: # do not keep histograms
if self.hist:
deletehist(self.hist)
for obj in self.garbage+self.profiles+self.graphs+self.lines:
deletehist(obj)
LOG.verb("closed\n>>>",verbosity,2)
def drawgraph(self,graphs,entries=[ ],**kwargs):
"""Draw graphs on canvas."""
graphs = [ g for g in ensurelist(graphs) if g ]
entries = kwargs.get('entry', [ ] )
for i, graph in enumerate(graphs):
if not graph: continue
color = _lcolors2D[i%len(_lcolors2D)]
if i<len(entries):
graph.SetTitle(entries[i])
graph.SetTitle(gtitle)
graph.SetLineColor(color)
graph.SetMarkerColor(color)
graph.SetLineWidth(3)
graph.SetMarkerSize(3)
graph.SetLineStyle(1)
graph.SetMarkerStyle(3)
graph.Draw('LPSAME')
self.grahps = graphs[:]
return graphs
def drawlegend(self,position=None,**kwargs):
position = kwargs.get('pos', position ) # legend position
position = kwargs.get('position', position ) or self.position
entries = kwargs.get('entries', [ ] )
title = kwargs.get('header', None )
title = kwargs.get('title', title ) # legend header/title
texts = kwargs.get('text', [ ] ) # extra text below legend
texts = ensurelist(texts,nonzero=True)
entries = ensurelist(entries,nonzero=False)
tsize = 0.041
width = 0.26
height = tsize*1.10*len([o for o in self.graphs+self.profiles+entries+texts+[title] if o])
if 'left' in position.lower():
x1 = 0.17; x2 = x1+width
else:
x1 = 0.78; x2 = x1-width
if 'bottom' in position.lower():
y1 = 0.20; y2 = y1+height
else:
y1 = 0.90; y2 = y1-height
legend = TLegend(x1,y1,x2,y2)
legend.SetTextSize(tsize)
legend.SetTextColor(lcolor)
legend.SetBorderSize(0)
legend.SetFillStyle(0)
#legend.SetFillStyle(1001)
#legend.SetFillStyle(4050)
legend.SetFillColor(0)
#legend.SetFillColor(kWhiteTransparent)
#legend.SetFillColorAlpha(kBlue, 0.50)
if title:
legend.SetTextFont(62)
legend.SetHeader(title)
legend.SetTextFont(42)
for graph in self.graphs:
self.legend.AddEntry(graph,graph.GetTitle(),'lp')
for profile in self.profiles:
legend.AddEntry(profile,profile.GetTitle(),'lep')
for line in texts:
legend.AddEntry(0,line,'')
self.canvas.cd()
legend.Draw('SAME')
self.legend = legend