Repository navigation
Expand file tree
/
Copy pathplot-np-shape
More file actions
executable file
·217 lines (199 loc) · 7.82 KB
/
Copy pathplot-np-shape
File metadata and controls
executable file
·217 lines (199 loc) · 7.82 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
#!/usr/bin/env python
# python imports
import os
# ROOT/rootpy imports
from rootpy import ROOT
from rootpy.extern.argparse import ArgumentParser
from rootpy.stats.histfactory import measurements_from_xml, HistoSys, OverallSys, Sample
from rootpy.plotting import Hist, Legend, Graph, Canvas
from rootpy.plotting.utils import get_band
from rootpy.utils.path import mkdir_p
# local imports
from statstools.fixups import find_measurements
from statstools import log; log = log[__name__]
from mva.templates import RatioPlot
from mva import save_canvas
parser = ArgumentParser()
parser.add_argument('paths', nargs='+')
args = parser.parse_args()
class MultipleCanvas(Canvas):
"""
"""
def __init__(self, canvas_list):
if not isinstance(canvas_list, (tuple, list)):
raise RuntimeError('Wrong argument')
self.Divide(len(canvas_list))
for ic, canv in enumerate(canvas_list):
self.cd(ic+1)
canv.Draw()
def draw_ratio(nom, sys_var,
textsize=18,
ratio_range=(0,2),
ratio_line_values=[0.5,1,1.5],
sys_name='syst',
sample_name=None,
category_name=None,
optional_label_text=None):
"""
Draw a canvas with an Hists and its up/down systematic variations
and a ratio plot between the variations and the nominal
Parameters:
- nom: Nominal Hist -- denominator in the ratio
- sys_var: Shifted Hists (up/down) -- numerator in the ratio
"""
up = sys_var[0]
do = sys_var[1]
nom.title = 'Nominal'
up.title = '+1 #sigma'
do.title = '-1 #sigma'
nom.color = 'black'
up.color = 'red'
do.color = 'blue'
nom.legendstyle = 'L'
up.legendstyle = 'L'
do.legendstyle = 'L'
nom.markersize = 0
up.markersize = 0
do.markersize = 0
nom.linewidth = 2
up.linewidth = 2
do.linewidth = 2
nom.fillstyle = 'hollow'
up.fillstyle = 'hollow'
do.fillstyle = 'hollow'
nom.linestyle = 'solid'
up.linestyle = 'dashed'
do.linestyle = 'dashed'
nom.drawstyle='hist E0'
up.drawstyle='hist E0'
do.drawstyle='hist E0'
if nom.integral() !=0:
ratio_up = Hist.divide(up, nom, fill_value=-1)
ratio_do = Hist.divide(do, nom, fill_value=-1)
ratio_unity = Hist.divide(nom, nom, fill_value=-1)
else:
ratio_up = nom.Clone()
ratio_up[:] = (1, 0)
ratio_do = nom.Clone()
ratio_do[:] = (1, 0)
ratio_unity = nom.Clone()
ratio_unity[:] = (1, 0)
ratio_up.drawstyle = 'hist'
ratio_up.color = 'red'
ratio_do.drawstyle = 'hist'
ratio_do.color = 'blue'
ratio_band_do = get_band(ratio_unity, ratio_do)
ratio_band_up = get_band(ratio_unity, ratio_up)
ratio_band_do.fillstyle = '/'
ratio_band_do.fillcolor = 'blue'
ratio_band_do.linewidth = 0
ratio_band_do.drawstyle = '20'
ratio_band_up.fillstyle = '\\'
ratio_band_up.fillcolor = 'red'
ratio_band_up.linewidth = 0
ratio_band_up.drawstyle = '20'
ratio_range = (min(list(ratio_up.y())+list(ratio_do.y())),
max(list(ratio_up.y())+list(ratio_do.y())))
ratio_range = ( ratio_range[0]-0.1*ratio_range[0],
ratio_range[1]+0.1*ratio_range[1])
plot = RatioPlot(xtitle='Observable',
ytitle='Events',
ratio_title='Ratio',
ratio_height=0.4,
ratio_range=ratio_range)
# ratio_line_values=ratio_line_values)
plot.draw('main', [nom, up, do], ypadding=(0.3, 0.))
plot.draw('ratio', [ratio_up, ratio_do, ratio_band_up, ratio_band_do])
with plot.pad('main') as pad:
# legend
leg = Legend([nom, up, do],
leftmargin=0.65, topmargin=0.13,
margin=0.18, textsize=textsize)
leg.Draw()
# draw the category label
label = ROOT.TLatex(
pad.GetLeftMargin() + 0.04, 0.87,
sys_name)
label.SetNDC()
label.SetTextFont(43)
label.SetTextSize(textsize)
label.Draw()
# show p-value and chi^2
if nom.integral() !=0:
chi2_up = nom.Chi2Test(up, 'WW CHI2/NDF')
chi2_do = nom.Chi2Test(do, 'WW CHI2/NDF')
else:
chi2_up = 0
chi2_do = 0
chi2_up_label = ROOT.TLatex(
pad.GetLeftMargin() + 0.04, 0.64,
"+1#sigma: #chi^{{2}}/ndf={0:.2f}".format(chi2_up))
chi2_up_label.SetNDC(True)
chi2_up_label.SetTextFont(43)
chi2_up_label.SetTextSize(textsize)
chi2_up_label.Draw()
chi2_do_label = ROOT.TLatex(
pad.GetLeftMargin() + 0.04, 0.72,
"-1#sigma: #chi^{{2}}/ndf={0:.2f}".format(chi2_do))
chi2_do_label.SetNDC(True)
chi2_do_label.SetTextFont(43)
chi2_do_label.SetTextSize(textsize)
chi2_do_label.Draw()
if sample_name is not None:
if category_name is not None:
sample_cat_text = '{0}; {1}'.format(category_name, sample_name)
sample_cat_label = ROOT.TLatex(pad.GetLeftMargin()+0.04, 0.8, sample_cat_text)
sample_cat_label.SetNDC(True)
sample_cat_label.SetTextFont(43)
sample_cat_label.SetTextSize(textsize)
sample_cat_label.Draw()
if optional_label_text is not None:
optional_label = ROOT.TLatex(pad.GetLeftMargin()+0.55,0.87,
optional_label_text )
optional_label.SetNDC(True)
optional_label.SetTextFont(43)
optional_label.SetTextSize(textsize)
optional_label.Draw()
return plot
def sum_samples(sample_list):
return 0
#------------
## MAIN DRIVER
for input_path in args.paths:
"""
Traverse all measurements and create the syst plots
"""
if not os.path.isdir(input_path):
raise ValueError("input must be an existing directory")
input_path = os.path.normpath(input_path)
for dirpath, measurement_file in find_measurements(input_path):
path = os.path.join(dirpath, measurement_file)
log.info("Reading {0} ...".format(path))
measurements = measurements_from_xml(path, cd_parent=True,
collect_histograms=True,
silence=False)
plots_dir = os.path.join(dirpath, 'np_shapes')
if not os.path.exists(plots_dir):
mkdir_p(plots_dir)
for meas in measurements:
log.info(meas.name)
for channel in meas.channels:
log.info(channel.name)
for sample in channel.samples:
log.info(sample.name)
hnom = sample.hist
for name, osys, hsys in sample.iter_sys():
if isinstance(osys, OverallSys):
log.info('{0}: [{1}, {2}]'.format(name, osys.low, osys.high))
if isinstance(hsys, HistoSys):
hsys_low, hsys_high = sample.sys_hist(name)
plot = draw_ratio(hnom, (hsys_high, hsys_low),
sys_name=name,
sample_name = sample.name,
category_name = channel.name)
plot.SaveAs(os.path.join(plots_dir,
'shape_{0}_{1}_{2}.png'.format(channel.name,
sample.name,
name)))
hsys_high.Reset()
hsys_low.Reset()