forked from sciaba/nanocc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_wtemplates.py
198 lines (174 loc) · 8.63 KB
/
make_wtemplates.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
import os
import sys
import shutil
import warnings
import uproot
with warnings.catch_warnings():
warnings.simplefilter("ignore")
from uproot3_methods.classes.TH1 import Methods as TH1Methods
import uproot3
from coffea import hist
from coffea.util import load, save
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import mplhep as hep
import argparse
parser = argparse.ArgumentParser()
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
parser.add_argument('--id', '--identifier', dest='identifier', required=True, help='File identifier to carry through (default: %(default)s)')
parser.add_argument("--type", default='n2cvb', choices=['n2', 'n2cvb', 'cvl'], type=str, help="Which region to run")
parser.add_argument('--plot', action='store_true', help='Make control plots')
args = parser.parse_args()
print("Running with the following options:")
print(args)
from coffea.util import load
output = load(f'scaled_hists_{args.identifier}.coffea')
store_dir = f'wfit_{args.identifier}_{args.type}'
if os.path.exists(store_dir):
shutil.rmtree(store_dir)
os.mkdir(store_dir)
if args.type == 'n2cvb':
Pass = (output['wtag_opt'] # PP
.integrate('region', 'wtag')
.integrate('systematic', 'nominal')
.rebin('msd', hist.Bin('msd','msd', 29, 40, 141.5))
.integrate('n2ddt', slice(None, 0))
.integrate('ddcvb', slice(0.017, None))
.integrate('ddc')
[['TTToSemiLeptonic', 'TTToHadronic', 'TTTo2L2Nu', 'ST', 'WJetsToLNu', 'data_obs', 'qcd', 'tqq', 'wln', 'stqq']]
.project('msd','genflavor', 'process'))
Fail = (
(output['wtag_opt'] # PF
.integrate('region', 'wtag')
.integrate('systematic', 'nominal')
#.rebin('msd', hist.Bin('msd', 'msd', 18, 40, 136.6))
.rebin('msd', hist.Bin('msd','msd', 29, 40, 141.5))
.integrate('n2ddt', slice(None, 0))
.integrate('ddcvb', slice(None, 0.017))
.integrate('ddc')
[['TTToSemiLeptonic', 'TTToHadronic', 'TTTo2L2Nu', 'ST', 'WJetsToLNu', 'data_obs', 'qcd', 'tqq', 'wln', 'stqq']]
.project('msd', 'genflavor', 'process'))
+ (output['wtag_opt'] # FP/FF
.integrate('region', 'wtag')
.integrate('systematic', 'nominal')
#.rebin('msd', hist.Bin('msd', 'msd', 18, 40, 136.6))
.rebin('msd', hist.Bin('msd','msd', 29, 40, 141.5))
.integrate('n2ddt', slice(0, None))
.integrate('ddc')
[['TTToSemiLeptonic', 'TTToHadronic', 'TTTo2L2Nu', 'ST', 'WJetsToLNu', 'data_obs', 'qcd', 'tqq', 'wln', 'stqq']]
.project('msd', 'genflavor', 'process'))
)
elif args.type == 'n2':
Pass = (output['wtag_opt'] # P
.integrate('region', 'wtag')
.integrate('systematic', 'nominal')
.rebin('msd', hist.Bin('msd','msd', 29, 40, 141.5))
.integrate('n2ddt', slice(None, 0))
.integrate('ddc')
[['TTToSemiLeptonic', 'TTToHadronic', 'TTTo2L2Nu', 'ST', 'WJetsToLNu', 'data_obs', 'qcd', 'tqq', 'wln', 'stqq']]
.project('msd','genflavor', 'process'))
Fail = (output['wtag_opt'] # F
.integrate('region', 'wtag')
.integrate('systematic', 'nominal')
.rebin('msd', hist.Bin('msd', 'msd', 29, 40, 141.5))
.integrate('n2ddt', slice(0, None))
.integrate('ddc')
[['TTToSemiLeptonic', 'TTToHadronic', 'TTTo2L2Nu', 'ST', 'WJetsToLNu', 'data_obs', 'qcd', 'tqq', 'wln', 'stqq']]
.project('msd', 'genflavor', 'process'))
elif args.type == 'cvl':
Pass = (output['wtag_opt'] # P
.integrate('region', 'wtag')
.integrate('systematic', 'nominal')
.rebin('msd', hist.Bin('msd', 'msd', 29, 40, 141.5))
.integrate('n2ddt', slice(None, 0))
.integrate('ddcvb', slice(0.017, None))
.integrate('ddc', slice(0.44, None))
[['TTToSemiLeptonic', 'TTToHadronic', 'TTTo2L2Nu', 'ST', 'WJetsToLNu', 'data_obs', 'qcd', 'tqq', 'wln', 'stqq']]
.project('msd','genflavor', 'process'))
Fail = (output['wtag_opt'] # F
.integrate('region', 'wtag')
.integrate('systematic', 'nominal')
.rebin('msd', hist.Bin('msd','msd', 29, 40, 141.5))
.integrate('n2ddt', slice(None, 0))
.integrate('ddcvb', slice(0.017, None))
.integrate('ddc', slice(None, 0.44))
[['TTToSemiLeptonic', 'TTToHadronic', 'TTTo2L2Nu', 'ST', 'WJetsToLNu', 'data_obs', 'qcd', 'tqq', 'wln', 'stqq']]
.project('msd', 'genflavor', 'process'))
else:
raise NotImplementedError
mclist = ['TTToSemiLeptonic', 'TTToHadronic', 'TTTo2L2Nu', 'ST', 'WJetsToLNu', 'qcd', 'wln', 'tqq']
mclistnoqcd = ['TTToSemiLeptonic', 'TTToHadronic', 'TTTo2L2Nu', 'ST', 'WJetsToLNu', 'wln', 'tqq']
temp_data_pass = Pass['data_obs'].integrate('genflavor').project('msd')
temp_data_fail = Fail['data_obs'].integrate('genflavor').project('msd')
temp_unmatched_pass = Pass[mclistnoqcd].integrate('genflavor', 0).project('msd')
temp_unmatched_fail = Fail[mclistnoqcd].integrate('genflavor', 0).project('msd')
temp_matched_pass = Pass[mclistnoqcd].integrate('genflavor', slice(1, 4)).project('msd')
temp_matched_fail = Fail[mclistnoqcd].integrate('genflavor', slice(1, 4)).project('msd')
print("From samples:")
print([b.name for b in Pass.axis('process').identifiers()])
print("Fit templates")
print("Data")
print(temp_data_pass.values()[()])
print(temp_data_fail.values()[()])
print("Unmatched")
print(temp_unmatched_pass.values()[()])
print(temp_unmatched_fail.values()[()])
print("Matched")
print(temp_matched_pass.values()[()])
print(temp_matched_fail.values()[()])
if args.plot:
# Matched
for stack in True, False:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))
hist.plot.plot1d(Pass[mclistnoqcd].integrate('genflavor', slice(1, 4)), ax=ax1, stack=stack)
hist.plot.plot1d(Fail[mclistnoqcd].integrate('genflavor', slice(1, 4)), ax=ax2, stack=stack)
fig.savefig(f'{store_dir}/matched{stack}.png')
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))
hist.plot.plot1d(Pass[mclistnoqcd].integrate('genflavor', 0), ax=ax1, stack=stack)
hist.plot.plot1d(Fail[mclistnoqcd].integrate('genflavor', 0), ax=ax2, stack=stack)
fig.savefig(f'{store_dir}/unmatched{stack}.png')
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))
hist.plot.plot1d(Pass['data_obs'].integrate('genflavor', 0), ax=ax1, error_opts={'color':'black'})
hist.plot.plot1d(Fail['data_obs'].integrate('genflavor', 0), ax=ax2, error_opts={'color':'black'})
hist.plot.plot1d(Pass[mclist].integrate('genflavor'), ax=ax1, clear=False, stack=True)
hist.plot.plot1d(Fail[mclist].integrate('genflavor'), ax=ax2, clear=False, stack=True)
fig.savefig(f'{store_dir}/datamc.png')
hep.set_style('CMS')
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(18, 8))
hep.histplot(temp_data_pass.values()[()],
temp_data_pass.axis('msd').edges(),
ax=ax1, histtype='errorbar', color='black', label='Data', yerr=True)
hep.histplot([temp_unmatched_pass.values()[()], temp_matched_pass.values()[()]],
temp_matched_pass.axis('msd').edges(),
stack=True, ax=ax1, label=['Unmatched', 'Matched'], histtype='fill', color=['green', 'red'])
hep.histplot(temp_data_fail.values()[()],
temp_data_fail.axis('msd').edges(),
ax=ax2, histtype='errorbar', color='black', label='Data', yerr=True)
hep.histplot([temp_unmatched_fail.values()[()], temp_matched_fail.values()[()]],
temp_matched_fail.axis('msd').edges(),
stack=True, ax=ax2, label=['Unmatched', 'Matched'], histtype='fill', color=['green', 'red'])
for ax in ax1, ax2:
ax.legend()
ax.set_xlabel('jet $m_{SD}$')
ax1.set_title("pass")
ax2.set_title("fail")
fig.savefig(f'{store_dir}/templates.png')
fout_pass = uproot3.create(f'{store_dir}/wtag_pass.root')
fout_fail = uproot3.create(f'{store_dir}/wtag_fail.root')
fout_pass['data_obs'] = hist.export1d(temp_data_pass)
fout_fail['data_obs'] = hist.export1d(temp_data_fail)
fout_pass['catp2'] = hist.export1d(temp_matched_pass)
fout_fail['catp2'] = hist.export1d(temp_matched_fail)
fout_pass['catp1'] = hist.export1d(temp_unmatched_pass)
fout_fail['catp1'] = hist.export1d(temp_unmatched_fail)
fout_pass.close()
fout_fail.close()