forked from cp3-llbb/HHbbWWAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BtagEffAndMistag.py
323 lines (291 loc) · 22.8 KB
/
BtagEffAndMistag.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
import os
import sys
import json
from bamboo.analysismodules import HistogramsModule
from bamboo import treefunctions as op
from bamboo.plots import Plot, EquidistantBinning, VariableBinning, SummedPlot
from bamboo.analysisutils import forceDefine
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)))) # Add scripts in this directory
from BaseHHtobbWW import BaseNanoHHtobbWW
from scalefactorsbbWW import ScaleFactorsbbWW
#===============================================================================================#
# PlotterHHtobbWW #
#===============================================================================================#
class BtagEffAndMistagNano(BaseNanoHHtobbWW,HistogramsModule):
""" Plotter module: HH->bbW(->e/µ nu)W(->e/µ nu) histograms from NanoAOD """
def __init__(self, args):
super(BtagEffAndMistagNano, self).__init__(args)
def initialize(self):
super(BtagEffAndMistagNano, self).initialize(forSkimmer=True)
def definePlots(self, t, noSel, sample=None, sampleCfg=None):
noSel = super(BtagEffAndMistagNano,self).prepareObjects(t, noSel, sample, sampleCfg, channel='DL', forSkimmer=True)
era = sampleCfg['era']
plots = []
forceDefine(self.tree._Jet.calcProd, noSel) # Jets for configureJets
forceDefine(self.tree._FatJet.calcProd, noSel) # FatJets for configureJets
forceDefine(getattr(self.tree, "_{0}".format("MET" if self.era != "2017" else "METFixEE2017")).calcProd,noSel) # MET for configureMET
# protection against data #
if not self.is_MC:
return []
#############################################################################
# AK4 #
#############################################################################
ak4_truth_lightjets = op.select(self.ak4Jets, lambda j : j.hadronFlavour == 0)
ak4_truth_cjets = op.select(self.ak4Jets, lambda j : j.hadronFlavour == 4)
ak4_truth_bjets = op.select(self.ak4Jets, lambda j : j.hadronFlavour == 5)
N_ak4_truth_lightjets = Plot.make3D('N_ak4_truth_lightjets',
[op.map(ak4_truth_lightjets, lambda j : op.abs(j.eta)),
op.map(ak4_truth_lightjets, lambda j : j.pt),
op.map(ak4_truth_lightjets, lambda j : j.btagDeepFlavB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'lightjet #eta',
yTitle = 'lightjet P_{T}',
zTitle = 'lightjet Btagging score')
N_ak4_truth_cjets = Plot.make3D('N_ak4_truth_cjets',
[op.map(ak4_truth_cjets, lambda j : op.abs(j.eta)),
op.map(ak4_truth_cjets, lambda j : j.pt),
op.map(ak4_truth_cjets, lambda j : j.btagDeepFlavB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'cjet #eta',
yTitle = 'cjet P_{T}',
zTitle = 'cjet Btagging score')
N_ak4_truth_bjets = Plot.make3D('N_ak4_truth_bjets',
[op.map(ak4_truth_bjets, lambda j : op.abs(j.eta)),
op.map(ak4_truth_bjets, lambda j : j.pt),
op.map(ak4_truth_bjets, lambda j : j.btagDeepFlavB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'bjet #eta',
yTitle = 'bjet P_{T}',
zTitle = 'bjet Btagging score')
plots.extend([N_ak4_truth_lightjets,N_ak4_truth_cjets,N_ak4_truth_bjets])
ak4_btagged_lightjets = op.select(ak4_truth_lightjets, self.lambda_ak4Btag)
ak4_btagged_cjets = op.select(ak4_truth_cjets, self.lambda_ak4Btag)
ak4_btagged_bjets = op.select(ak4_truth_bjets, self.lambda_ak4Btag)
N_ak4_btagged_lightjets = Plot.make3D('N_ak4_btagged_lightjets',
[op.map(ak4_btagged_lightjets, lambda j : op.abs(j.eta)),
op.map(ak4_btagged_lightjets, lambda j : j.pt),
op.map(ak4_btagged_lightjets, lambda j : j.btagDeepFlavB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'lightjet #eta',
yTitle = 'lightjet P_{T}',
zTitle = 'lightjet Btagging score')
N_ak4_btagged_cjets = Plot.make3D('N_ak4_btagged_cjets',
[op.map(ak4_btagged_cjets, lambda j : op.abs(j.eta)),
op.map(ak4_btagged_cjets, lambda j : j.pt),
op.map(ak4_btagged_cjets, lambda j : j.btagDeepFlavB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'cjet #eta',
yTitle = 'cjet P_{T}',
zTitle = 'cjet Btagging score')
N_ak4_btagged_bjets = Plot.make3D('N_ak4_btagged_bjets',
[op.map(ak4_btagged_bjets, lambda j : op.abs(j.eta)),
op.map(ak4_btagged_bjets, lambda j : j.pt),
op.map(ak4_btagged_bjets, lambda j : j.btagDeepFlavB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'bjet #eta',
yTitle = 'bjet P_{T}',
zTitle = 'bjet Btagging score')
plots.extend([N_ak4_btagged_lightjets,N_ak4_btagged_cjets,N_ak4_btagged_bjets])
#############################################################################
# AK8 #
#############################################################################
# Truth MC object hadron flavour #
# subjet.nBHadrons>0 : bjet
# subjet.nCHadrons>0 : cjet
# else : lightjet
ak8subJet1_truth_lightjets = op.select(self.ak8Jets, lambda j : op.AND(j.subJet1.nBHadrons==0,j.subJet1.nCHadrons==0))
ak8subJet2_truth_lightjets = op.select(self.ak8Jets, lambda j : op.AND(j.subJet2.nBHadrons==0,j.subJet2.nCHadrons==0))
ak8subJet1_truth_cjets = op.select(self.ak8Jets, lambda j : j.subJet1.nCHadrons>0)
ak8subJet2_truth_cjets = op.select(self.ak8Jets, lambda j : j.subJet2.nCHadrons>0)
ak8subJet1_truth_bjets = op.select(self.ak8Jets, lambda j : j.subJet1.nBHadrons>0)
ak8subJet2_truth_bjets = op.select(self.ak8Jets, lambda j : j.subJet2.nBHadrons>0)
N_ak8_truth_subJet1_lightjets = Plot.make3D('N_ak8_truth_subJet1_lightjets',
[op.map(ak8subJet1_truth_lightjets, lambda j : op.abs(j.subJet1.eta)),
op.map(ak8subJet1_truth_lightjets, lambda j : j.subJet1.pt),
op.map(ak8subJet1_truth_lightjets, lambda j : j.subJet1.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'lightjet subJet1 #eta',
yTitle = 'lightjet subJet1 P_{T}',
zTitle = 'lightjet subJet1 Btagging score')
N_ak8_truth_subJet2_lightjets = Plot.make3D('N_ak8_truth_subJet2_lightjets',
[op.map(ak8subJet2_truth_lightjets, lambda j : op.abs(j.subJet2.eta)),
op.map(ak8subJet2_truth_lightjets, lambda j : j.subJet2.pt),
op.map(ak8subJet2_truth_lightjets, lambda j : j.subJet2.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'lightjet subJet2 #eta',
yTitle = 'lightjet subJet2 P_{T}',
zTitle = 'lightjet subJet2 Btagging score')
N_ak8_truth_subJet1_cjets = Plot.make3D('N_ak8_truth_subJet1_cjets',
[op.map(ak8subJet1_truth_cjets, lambda j : op.abs(j.subJet1.eta)),
op.map(ak8subJet1_truth_cjets, lambda j : j.subJet1.pt),
op.map(ak8subJet1_truth_cjets, lambda j : j.subJet1.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'cjet subJet1 #eta',
yTitle = 'cjet subJet1 P_{T}',
zTitle = 'cjet subJet1 Btagging score')
N_ak8_truth_subJet2_cjets = Plot.make3D('N_ak8_truth_subJet2_cjets',
[op.map(ak8subJet2_truth_cjets, lambda j : op.abs(j.subJet2.eta)),
op.map(ak8subJet2_truth_cjets, lambda j : j.subJet2.pt),
op.map(ak8subJet2_truth_cjets, lambda j : j.subJet2.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'cjet subJet2 #eta',
yTitle = 'cjet subJet2 P_{T}',
zTitle = 'cjet subJet2 Btagging score')
N_ak8_truth_subJet1_bjets = Plot.make3D('N_ak8_truth_subJet1_bjets',
[op.map(ak8subJet1_truth_bjets, lambda j : op.abs(j.subJet1.eta)),
op.map(ak8subJet1_truth_bjets, lambda j : j.subJet1.pt),
op.map(ak8subJet1_truth_bjets, lambda j : j.subJet1.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'bjet subJet1 #eta',
yTitle = 'bjet subJet1 P_{T}',
zTitle = 'bjet subJet1 Btagging score')
N_ak8_truth_subJet2_bjets = Plot.make3D('N_ak8_truth_subJet2_bjets',
[op.map(ak8subJet2_truth_bjets, lambda j : op.abs(j.subJet2.eta)),
op.map(ak8subJet2_truth_bjets, lambda j : j.subJet2.pt),
op.map(ak8subJet2_truth_bjets, lambda j : j.subJet2.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'bjet subJet2 #eta',
yTitle = 'bjet subJet2 P_{T}',
zTitle = 'bjet subJet2 Btagging score')
plots.extend([N_ak8_truth_subJet1_lightjets,N_ak8_truth_subJet2_lightjets,N_ak8_truth_subJet1_cjets,N_ak8_truth_subJet2_cjets,N_ak8_truth_subJet1_bjets,N_ak8_truth_subJet2_bjets])
plots.append(SummedPlot('N_ak8_truth_lightjets',
[N_ak8_truth_subJet1_lightjets,N_ak8_truth_subJet2_lightjets],
xTitle = 'lightjet #eta',
yTitle = 'lightjet P_{T}',
zTitle = 'lightjet Btagging score'))
plots.append(SummedPlot('N_ak8_truth_cjets',
[N_ak8_truth_subJet1_cjets,N_ak8_truth_subJet2_cjets],
xTitle = 'cjet #eta',
yTitle = 'cjet P_{T}',
zTitle = 'cjet Btagging score'))
plots.append(SummedPlot('N_ak8_truth_bjets',
[N_ak8_truth_subJet1_bjets,N_ak8_truth_subJet2_bjets],
xTitle = 'bjet #eta',
yTitle = 'bjet P_{T}',
zTitle = 'bjet Btagging score'))
# Btagged objects per flavour #
ak8subJet1_btagged_lightjets = op.select(ak8subJet1_truth_lightjets, lambda j: self.lambda_subjetBtag(j.subJet1))
ak8subJet2_btagged_lightjets = op.select(ak8subJet2_truth_lightjets, lambda j: self.lambda_subjetBtag(j.subJet2))
ak8subJet1_btagged_cjets = op.select(ak8subJet1_truth_cjets, lambda j: self.lambda_subjetBtag(j.subJet1))
ak8subJet2_btagged_cjets = op.select(ak8subJet2_truth_cjets, lambda j: self.lambda_subjetBtag(j.subJet2))
ak8subJet1_btagged_bjets = op.select(ak8subJet1_truth_bjets, lambda j: self.lambda_subjetBtag(j.subJet1))
ak8subJet2_btagged_bjets = op.select(ak8subJet2_truth_bjets, lambda j: self.lambda_subjetBtag(j.subJet2))
N_ak8_btagged_subJet1_lightjets = Plot.make3D('N_ak8_btagged_subJet1_lightjets',
[op.map(ak8subJet1_btagged_lightjets, lambda j : op.abs(j.subJet1.eta)),
op.map(ak8subJet1_btagged_lightjets, lambda j : j.subJet1.pt),
op.map(ak8subJet1_btagged_lightjets, lambda j : j.subJet1.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'lightjet subJet1 #eta',
yTitle = 'lightjet subJet1 P_{T}',
zTitle = 'lightjet subJet1 Btagging score')
N_ak8_btagged_subJet2_lightjets = Plot.make3D('N_ak8_btagged_subJet2_lightjets',
[op.map(ak8subJet2_btagged_lightjets, lambda j : op.abs(j.subJet2.eta)),
op.map(ak8subJet2_btagged_lightjets, lambda j : j.subJet2.pt),
op.map(ak8subJet2_btagged_lightjets, lambda j : j.subJet2.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'lightjet subJet2 #eta',
yTitle = 'lightjet subJet2 P_{T}',
zTitle = 'lightjet subJet2 Btagging score')
N_ak8_btagged_subJet1_cjets = Plot.make3D('N_ak8_btagged_subJet1_cjets',
[op.map(ak8subJet1_btagged_cjets, lambda j : op.abs(j.subJet1.eta)),
op.map(ak8subJet1_btagged_cjets, lambda j : j.subJet1.pt),
op.map(ak8subJet1_btagged_cjets, lambda j : j.subJet1.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'cjet subJet1 #eta',
yTitle = 'cjet subJet1 P_{T}',
zTitle = 'cjet subJet1 Btagging score')
N_ak8_btagged_subJet2_cjets = Plot.make3D('N_ak8_btagged_subJet2_cjets',
[op.map(ak8subJet2_btagged_cjets, lambda j : op.abs(j.subJet2.eta)),
op.map(ak8subJet2_btagged_cjets, lambda j : j.subJet2.pt),
op.map(ak8subJet2_btagged_cjets, lambda j : j.subJet2.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'cjet subJet2 #eta',
yTitle = 'cjet subJet2 P_{T}',
zTitle = 'cjet subJet2 Btagging score')
N_ak8_btagged_subJet1_bjets = Plot.make3D('N_ak8_btagged_subJet1_bjets',
[op.map(ak8subJet1_btagged_bjets, lambda j : op.abs(j.subJet1.eta)),
op.map(ak8subJet1_btagged_bjets, lambda j : j.subJet1.pt),
op.map(ak8subJet1_btagged_bjets, lambda j : j.subJet1.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'bjet subJet1 #eta',
yTitle = 'bjet subJet1 P_{T}',
zTitle = 'bjet subJet1 Btagging score')
N_ak8_btagged_subJet2_bjets = Plot.make3D('N_ak8_btagged_subJet2_bjets',
[op.map(ak8subJet2_btagged_bjets, lambda j : op.abs(j.subJet2.eta)),
op.map(ak8subJet2_btagged_bjets, lambda j : j.subJet2.pt),
op.map(ak8subJet2_btagged_bjets, lambda j : j.subJet2.btagDeepB)],
noSel,
[EquidistantBinning(100,0.,2.5),
EquidistantBinning(100,0.,1000),
EquidistantBinning(100,0.,1.)],
xTitle = 'bjet subJet2 #eta',
yTitle = 'bjet subJet2 P_{T}',
zTitle = 'bjet subJet2 Btagging score')
plots.extend([N_ak8_btagged_subJet1_lightjets,N_ak8_btagged_subJet2_lightjets,N_ak8_btagged_subJet1_cjets,N_ak8_btagged_subJet2_cjets,N_ak8_btagged_subJet1_bjets,N_ak8_btagged_subJet2_bjets])
plots.append(SummedPlot('N_ak8_btagged_lightjets',
[N_ak8_btagged_subJet1_lightjets,N_ak8_btagged_subJet2_lightjets],
xTitle = 'lightjet #eta',
yTitle = 'lightjet P_{T}',
zTitle = 'lightjet Btagging score'))
plots.append(SummedPlot('N_ak8_btagged_cjets',
[N_ak8_btagged_subJet1_cjets,N_ak8_btagged_subJet2_cjets],
xTitle = 'cjet #eta',
yTitle = 'cjet P_{T}',
zTitle = 'cjet Btagging score'))
plots.append(SummedPlot('N_ak8_btagged_bjets',
[N_ak8_btagged_subJet1_bjets,N_ak8_btagged_subJet2_bjets],
xTitle = 'bjet #eta',
yTitle = 'bjet P_{T}',
zTitle = 'bjet Btagging score'))
return plots