forked from konush/PFClusterCalib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw_results.py
More file actions
359 lines (284 loc) · 11.9 KB
/
draw_results.py
File metadata and controls
359 lines (284 loc) · 11.9 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python
"""Visualizes achieved energy resolutions.
"""
# python-2 compatibility
from __future__ import division # 1/2 = 0.5, not 0
from __future__ import print_function # print() syntax from python-3
import os
import pickle
import fnmatch
import ROOT
# for keeping drawed ROOT objects in memory
saves = []
def main():
"""Steering function.
"""
ROOT.gROOT.SetBatch(True)
ROOT.gStyle.SetOptStat(0)
ROOT.TH1.AddDirectory(False)
# keep current working directory clean from .d and .so files;
# CPU-intensive part is written in C++ (python is too slow)
ROOT.gSystem.SetBuildDir('output', True);
ROOT.gROOT.LoadMacro('draw_results_helper.cc+')
# ntuples to process
ntuples = fnmatch.filter(os.listdir('input'), '*.root')
ntuples = sorted('input/' + f for f in ntuples)
# MVAs to process
mvas = [f[f.rfind('/') + 1:].replace('.root', '') for f in ntuples]
# text in legends
txts = [f[f.rfind('gun_') + 4:].replace('.root', '') for f in ntuples]
txts = [t + ', no correction' for t in txts] + txts[:]
# make output directories
for d in ['output', 'output/cache', 'output/plots_results', 'output/plots_results/fits']:
if not os.access(d, os.X_OK):
os.mkdir(d)
eregions = [(0, 1), (1, 2), (2, 10), (10, 20), (20, 100), (100, 1000)]
for det in ['EB', 'EE']:
for mva in mvas:
r = [make_graphs(f, det, '', eregions, blockSize=3000) for f in ntuples]
r += [make_graphs(f, det, 'mva_mean_' + mva, eregions, blockSize=3000) for f in ntuples]
# repack graphs into per-parameter tuples
r = list(zip(*r))
# text in captions
cap = 'trained on {0}, {1}'.format(mva[mva.rfind('gun_') + 4:], det)
# mean vs E
title = 'mean_vs_e_{0}_{1}'.format(mva, det)
combine(r[0], txts, title, cap, 'E^{gen}', 'Mean_{E^{rec}/E^{gen}}')
combine(r[0], txts, title + '_zoom', cap, 'E^{gen}', 'Mean_{E^{rec}/E^{gen}}', False, 20)
# sigma vs E
title = 'sigma_vs_e_{0}_{1}'.format(mva, det)
combineR(r[1], txts, title, cap, 'E^{gen}', '#sigma_{E^{rec}/E^{gen}}/mean')
combineR(r[1], txts, title + '_zoom', cap, 'E^{gen}', '#sigma_{E^{rec}/E^{gen}}/mean', False, 20)
# mean vs pT
title = 'mean_vs_pt_{0}_{1}'.format(mva, det)
combine(r[2], txts, title, cap, 'p_{T}^{gen}', 'Mean_{E^{rec}/E^{gen}}')
combine(r[2], txts, title + '_zoom', cap, 'p_{T}^{gen}', 'Mean_{E^{rec}/E^{gen}}', False, 20)
# sigma vs pT
title = 'sigma_vs_pt_{0}_{1}'.format(mva, det)
combineR(r[3], txts, title, cap, 'p_{T}^{gen}', '#sigma_{E^{rec}/E^{gen}}/mean')
combineR(r[3], txts, title + '_zoom', cap, 'p_{T}^{gen}', '#sigma_{E^{rec}/E^{gen}}/mean', False, 20)
for (i, (e1, e2)) in enumerate(eregions):
# mean vs nVtx (nopu results excluded)
try:
title = 'mean_vs_nvtx_{0}_{1}_E{2}-{3}'.format(mva, det, e1, e2)
combine([x[i] for x in r[6]], txts, title, cap, 'nVtx', 'Mean_{E^{rec}/E^{gen}}', True)
except:
print('Plot excluded: {0}'.format(title))
# sigma vs nVtx
try:
title = 'sigma_vs_nvtx_{0}_{1}_E{2}-{3}'.format(mva, det, e1, e2)
combineR([x[i] for x in r[7]], txts, title, cap, 'nVtx', '#sigma_{E^{rec}/E^{gen}}/mean', True)
except:
print('Plot excluded: {0}'.format(title))
cap = 'trained on {0}, {1} < E^{{gen}} < {2} GeV/c'.format(mva[mva.rfind('gun_') + 4:], e1, e2)
# mean and sigma vs eta
if det == 'EB': # not necessary to repeat for EE
title = 'mean_vs_eta_{0}_E{1}-{2}'.format(mva, e1, e2)
combine([x[i] for x in r[4]], txts, title, cap, '#eta^{gen}', 'Mean_{E^{rec}/E^{gen}}')
# sigma vs eta
title = 'sigma_vs_eta_{0}_E{1}-{2}'.format(mva, e1, e2)
combineR([x[i] for x in r[5]], txts, title, cap, '#eta^{gen}', '#sigma_{E^{rec}/E^{gen}}/mean')
def make_graphs(infile, det, mva_branch, eregions, blockSize):
"""Fills, fits and visualizes distributions of Etrue/Erec.
Results are cached into file.
"""
# return cached results, if any
fname = os.path.basename(infile).replace('.root', '')
fmt = 'output/cache/draw_results_{0}_{1}_{2}_{3}.pkl'
cachefile = fmt.format(fname, det, mva_branch, blockSize)
if os.access(cachefile, os.R_OK):
with open(cachefile, 'rb') as f:
return pickle.load(f)
# fill necessary arrays of points in C++
friend = 'output/friend_{0}.root'.format(fname)
ROOT.fill_arrays(infile, friend, mva_branch, True if det == 'EE' else False)
# resolution vs mcE
title = 'mcE_{0}_{1}_{2}'.format(fname, det, mva_branch)
ROOT.fit_slices(0, blockSize, title, 'E^{gen}')
grMeanE = ROOT.grMean.Clone()
grSigmaE = ROOT.grSigma.Clone()
# resolution vs mcPt
title = 'mcPt_{0}_{1}_{2}'.format(fname, det, mva_branch)
ROOT.fit_slices(1, blockSize, title, 'p_{T}^{gen}')
grMeanPt = ROOT.grMean.Clone()
grSigmaPt = ROOT.grSigma.Clone()
# resolution vs mcEta in energy ranges
grMeanEta = []
grSigmaEta = []
for (e1, e2) in eregions:
title = 'mcEta_{0}_{1}_{2}_E{3}-{4}'.format(fname, det, mva_branch, e1, e2)
ROOT.fit_slices(2, blockSize, title, '#eta^{gen}', e1, e2)
grMeanEta.append(ROOT.grMean.Clone())
grSigmaEta.append(ROOT.grSigma.Clone())
# resolution vs nVtx in energy ranges
grMeanVtx = []
grSigmaVtx = []
for (e1, e2) in eregions:
title = 'nVtx_{0}_{1}_{2}_E{3}-{4}'.format(fname, det, mva_branch, e1, e2)
ROOT.fit_slices(3, blockSize, title, 'nVtx', e1, e2)
grMeanVtx.append(ROOT.grMean.Clone())
grSigmaVtx.append(ROOT.grSigma.Clone())
result = (grMeanE, grSigmaE, grMeanPt, grSigmaPt, grMeanEta, grSigmaEta,
grMeanVtx, grSigmaVtx)
# save cache
with open(cachefile, 'wb') as f:
pickle.dump(result, f)
return result
def combine(grs, txts, cname, title, xtitle, ytitle, skipNoPU=False, xmax=-1, ymax=-1):
"""Visualization of several graphs on single canvas.
"""
c = ROOT.TCanvas(cname, cname, 700, 700)
saves.append(c)
c.SetLeftMargin(0.14)
c.SetRightMargin(0.08)
c.SetTopMargin(0.06)
c.SetBottomMargin(0.1)
c.SetGridx()
c.SetGridy()
# draw dummy histogram
xmin = min(gr.GetX()[i] for gr in grs for i in range(gr.GetN()))
ymin = min(gr.GetY()[i] for gr in grs for i in range(gr.GetN()))
if xmax < 0:
xmax = max(gr.GetX()[i] for gr in grs for i in range(gr.GetN()))
if ymax < 0:
ymax = max(gr.GetY()[i] for gr in grs for i in range(gr.GetN()))
frame = c.DrawFrame(xmin, ymin * 0.9, xmax, ymax * 1.1)
frame.SetTitle(title)
frame.SetXTitle(xtitle)
frame.SetYTitle(ytitle)
frame.SetTitleOffset(1.2, 'X')
frame.SetTitleOffset(1.95, 'Y')
frame.Draw()
# number of corrected and uncorrected graphs
ncorr = len(grs)//2
# legend
leg = ROOT.TLegend(0.58, 0.12, 0.89, 0.12 + 0.033 * len(txts))
clrs = [ROOT.kOrange] * ncorr + [8, ROOT.kBlack, ROOT.kBlue, ROOT.kRed]
for (gr, clr, txt) in zip(grs, clrs, txts):
if skipNoPU and txt.startswith('nopu'):
continue
gr.SetMarkerStyle(20)
gr.SetMarkerSize(0.4)
gr.SetMarkerColor(clr)
gr.SetLineColor(clr)
gr.Draw('PZL')
leg.AddEntry(gr, txt, 'p')
leg.SetFillColor(0)
leg.Draw('same')
saves.append((frame, grs, leg))
c.Update()
c.SaveAs('output/plots_results/{0}.png'.format(c.GetTitle()))
def combineR(grs, txts, cname, title, xtitle, ytitle, skipNoPU=False, xmax=-1, ymax=-1):
"""Visualization of graphs along with corrected/uncorrected ratios.
"""
c = ROOT.TCanvas(cname, cname, 700, 700)
saves.append(c)
pad1 = ROOT.TPad('top', 'top', 0, 0.3, 1, 1)
pad2 = ROOT.TPad('bottom', 'bottom', 0, 0, 1, 0.3)
pad1.Draw()
pad2.Draw()
saves.append(pad1)
saves.append(pad2)
# top pad
pad1.cd()
pad1.SetTopMargin(0.07)
pad1.SetBottomMargin(0)
pad1.SetLeftMargin(0.095)
pad1.SetRightMargin(0.02)
pad1.SetGridx()
pad1.SetGridy()
# draw dummy histogram
xmin = min(gr.GetX()[i] for gr in grs for i in range(gr.GetN()))
ymin = min(gr.GetY()[i] for gr in grs for i in range(gr.GetN()))
if xmax < 0:
xmax = max(gr.GetX()[i] for gr in grs for i in range(gr.GetN()))
if ymax < 0:
ymax = max(gr.GetY()[i] for gr in grs for i in range(gr.GetN()))
frame = pad1.DrawFrame(xmin, ymin * 0.9, xmax, ymax * 1.1)
frame.SetTitle(title)
frame.SetXTitle(xtitle)
frame.SetYTitle(ytitle)
frame.SetTitleOffset(1.2, 'X')
frame.SetTitleOffset(1.35, 'Y')
frame.Draw()
# number of corrected and uncorrected graphs
ncorr = len(grs)//2
# legend
leg = ROOT.TLegend(0.58, 0.58, 0.95, 0.58 + 0.04 * len(txts))
clrs = [ROOT.kOrange] * ncorr + [8, ROOT.kBlack, ROOT.kBlue, ROOT.kRed]
for (gr, clr, txt) in zip(grs, clrs, txts):
if skipNoPU and txt.startswith('nopu'):
continue
gr.SetMarkerStyle(20)
gr.SetMarkerSize(0.4)
gr.SetMarkerColor(clr)
gr.SetLineColor(clr)
gr.Draw('PZL')
leg.AddEntry(gr, txt, 'p')
leg.SetFillColor(0)
leg.Draw('same')
saves.append((frame, grs, leg))
# bottom pad
pad2.cd()
pad2.SetTopMargin(0)
pad2.SetBottomMargin(0.25)
pad2.SetLeftMargin(0.095)
pad2.SetRightMargin(0.02)
pad2.SetGridx()
pad2.SetGridy()
# make corrected/uncorrected ratios
rats = [ROOT.TGraphErrors() for _ in range(ncorr)]
for n in range(ncorr):
gru = grs[n] # uncorrected
grc = grs[ncorr + n] # corrected
for i in range(grc.GetN()):
x = grc.GetX()[i]
y1 = grc.GetY()[i]
ex = grc.GetEX()[i]
ey1 = grc.GetEY()[i]
# NOTE: gru may have different x and ex, but we ignore this
# difference. Instead, y for gru is estimated from linear
# extrapolation between corresponding nearest points.
# find nearest points
for j in range(gru.GetN() - 1):
x1 = gru.GetX()[j]
x2 = gru.GetX()[j + 1]
if x1 <= x < x2:
a = (gru.GetY()[j] - gru.GetY()[j + 1]) / (x1 - x2)
b = gru.GetY()[j] - a * x1
y2 = a * x + b
ea = (gru.GetEY()[j] - gru.GetEY()[j + 1]) / (x1 - x2)
eb = gru.GetEY()[j] - ea * x1
ey2 = ea * x + eb
break
else:
if x < gru.GetX()[0]:
y2 = gru.GetY()[0]
ey2 = gru.GetEY()[0]
else:
y2 = gru.GetY()[gru.GetN() - 1]
ey2 = gru.GetEY()[gru.GetN() - 1]
rats[n].SetPoint(i, x, y1/y2)
rats[n].SetPointError(i, ex, y1/y2 * ((ey1/y1)**2 + (ey2/y2)**2)**0.5)
frame = pad2.DrawFrame(xmin, 0.1, xmax, 1.25)
frame.SetXTitle(xtitle)
frame.SetYTitle('Corr/uncorr')
frame.SetTitleOffset(1.2, 'X')
frame.SetTitleOffset(0.58, 'Y')
frame.SetTitleSize(0.08, 'XY')
frame.SetLabelSize(0.08, 'XY')
frame.Draw()
# draw corrected/uncorrected ratios
for (gr, clr, txt) in zip(rats, clrs[ncorr:], txts[ncorr:]):
if skipNoPU and txt.startswith('nopu'):
continue
gr.SetMarkerStyle(20)
gr.SetMarkerSize(0.4)
gr.SetMarkerColor(clr)
gr.SetLineColor(clr)
gr.Draw('PZL')
saves.append((frame, rats))
c.Update()
c.SaveAs('output/plots_results/{0}.png'.format(c.GetTitle()))
if __name__ == '__main__':
main()