-
Notifications
You must be signed in to change notification settings - Fork 5
/
TauTauModule_sync.py
446 lines (320 loc) · 19.5 KB
/
TauTauModule_sync.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import ROOT
from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel import Collection
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module
from TreeProducerTauTau import *
# for trigger efficiencies
from getTauTriggerSFs import *
class declareVariables(TreeProducerTauTau):
def __init__(self, name):
super(declareVariables, self).__init__(name)
class TauTauProducer(Module):
def __init__(self, name, DataType):
self.name = name
self.out = declareVariables(name)
if DataType=='data':
self.isData = True
else:
self.isData = False
self.tauSFs = getTauTriggerSFs('vtight')
self.Nocut = 0
self.Trigger = 1
self.GoodTaus = 2
self.GoodDiLepton = 3
self.TotalWeighted = 15
self.out.trg_doubletau = num.zeros(1, dtype=int)
self.out.tree.Branch('trg_doubletau' , self.out.trg_doubletau, 'trg_doubletau/I')
def beginJob(self):
pass
def endJob(self):
self.out.outputfile.Write()
self.out.outputfile.Close()
# pass
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
pass
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
pass
def analyze(self, event):
"""process event, return True (go to next module) or False (fail, go to next event)"""
# electrons = Collection(event, "Electron")
# muons = Collection(event, "Muon")
#####################################
self.out.h_cutflow.Fill(self.Nocut)
#####################################
#####################################
if not self.isData:
self.out.h_cutflow.Fill(self.TotalWeighted, event.genWeight)
else:
self.out.h_cutflow.Fill(self.TotalWeighted, 1.)
#####################################
# if event.HLT_DoubleMediumChargedIsoPFTau35_Trk1_eta2p1_Reg or :
# print 'trig = ', event.HLT_DoubleTightChargedIsoPFTau35_Trk1_TightID_eta2p1_Reg, event.HLT_DoubleMediumChargedIsoPFTau40_Trk1_TightID_eta2p1_Reg, event.HLT_DoubleTightChargedIsoPFTau40_Trk1_eta2p1_Reg
# if event.HLT_DoubleTightChargedIsoPFTau35_Trk1_TightID_eta2p1_Reg > 0.5 or event.HLT_DoubleMediumChargedIsoPFTau40_Trk1_TightID_eta2p1_Reg > 0.5 or event.HLT_DoubleTightChargedIsoPFTau40_Trk1_eta2p1_Reg > 0.5:
# pass
# else:
# return False
#####################################
self.out.h_cutflow.Fill(self.Trigger)
#####################################
idx_goodtaus = []
for itau in range(event.nTau):
# print 'pt=', event.Tau_pt[itau], abs(event.Tau_eta[itau])
if event.Tau_pt[itau] < 40: continue
if abs(event.Tau_eta[itau]) > 2.1: continue
if abs(event.Tau_dz[itau]) > 0.2: continue
# if event.Tau_decayMode[itau] not in [0,1,10]: continue
if event.Tau_idDecayMode[itau] < 0.5: continue
if abs(event.Tau_charge[itau])!=1: continue
# print itau, 'decay mode = ', event.Tau_decayMode[itau]
idx_goodtaus.append(itau)
if len(idx_goodtaus)<2:
return False
#####################################
self.out.h_cutflow.Fill(self.GoodTaus)
#####################################
# to check dR matching
taus = Collection(event, "Tau")
dileptons = []
for idx1 in idx_goodtaus:
for idx2 in idx_goodtaus:
if idx1 >= idx2: continue
dR = taus[idx1].p4().DeltaR(taus[idx2].p4())
if dR < 0.5: continue
_dilepton = DiLeptonBasicClass(idx1, event.Tau_pt[idx1], event.Tau_rawMVAoldDM[idx1], idx2, event.Tau_pt[idx2], event.Tau_rawMVAoldDM[idx2])
dileptons.append(_dilepton)
if len(dileptons)==0:
return False
#####################################
self.out.h_cutflow.Fill(self.GoodDiLepton)
#####################################
dilepton = bestDiLepton(dileptons)
# print 'chosen tau1 (idx, pt) = ', dilepton.tau1_idx, dilepton.tau1_pt, 'check', taus[dilepton.tau1_idx].p4().Pt()
# print 'chosen tau2 (idx, pt) = ', dilepton.tau2_idx, dilepton.tau2_pt, 'check', taus[dilepton.tau2_idx].p4().Pt()
jetIds = []
jets = Collection(event, "Jet")
# jets = filter(self.jetSel,jets):
nfjets = 0
ncjets = 0
nbtag = 0
for ijet in range(event.nJet):
# for j in filter(self.jetSel,jets):
if event.Jet_pt[ijet] < 30:
continue
if abs(event.Jet_eta[ijet]) > 4.7:
continue
dR = taus[dilepton.tau1_idx].p4().DeltaR(jets[ijet].p4())
if dR < 0.5:
continue
dR = taus[dilepton.tau2_idx].p4().DeltaR(jets[ijet].p4())
if dR < 0.5:
continue
# print '#', ijet, 'pt = ', jets[ijet].p4().Pt(), event.Jet_pt[ijet]
jetIds.append(ijet)
if abs(event.Jet_eta[ijet]) > 2.4:
nfjets += 1
else:
ncjets += 1
if event.Jet_btagCSVV2[ijet] > 0.8838:
nbtag += 1
# eventSum = ROOT.TLorentzVector()
#
# for lep in muons :
# eventSum += lep.p4()
# for lep in electrons :
# eventSum += lep.p4()
# for j in filter(self.jetSel,jets):
# eventSum += j.p4()
# tau 1
self.out.pt_1[0] = event.Tau_pt[dilepton.tau1_idx]
self.out.eta_1[0] = event.Tau_eta[dilepton.tau1_idx]
self.out.phi_1[0] = event.Tau_phi[dilepton.tau1_idx]
self.out.mass_1[0] = event.Tau_mass[dilepton.tau1_idx]
self.out.dxy_1[0] = event.Tau_dxy[dilepton.tau1_idx]
self.out.dz_1[0] = event.Tau_dz[dilepton.tau1_idx]
self.out.leadTkPtOverTauPt_1[0] = event.Tau_leadTkPtOverTauPt[dilepton.tau1_idx]
self.out.chargedIso_1[0] = event.Tau_chargedIso[dilepton.tau1_idx]
self.out.neutralIso_1[0] = event.Tau_neutralIso[dilepton.tau1_idx]
self.out.photonsOutsideSignalCone_1[0] = event.Tau_photonsOutsideSignalCone[dilepton.tau1_idx]
self.out.puCorr_1[0] = event.Tau_puCorr[dilepton.tau1_idx]
self.out.rawAntiEle_1[0] = event.Tau_rawAntiEle[dilepton.tau1_idx]
self.out.rawIso_1[0] = event.Tau_rawIso[dilepton.tau1_idx]
self.out.rawMVAnewDM2017v2_1[0] = event.Tau_rawMVAnewDM2017v2[dilepton.tau1_idx]
self.out.rawMVAoldDM_1[0] = event.Tau_rawMVAoldDM[dilepton.tau1_idx]
self.out.rawMVAoldDM2017v1_1[0] = event.Tau_rawMVAoldDM2017v1[dilepton.tau1_idx]
self.out.rawMVAoldDM2017v2_1[0] = event.Tau_rawMVAoldDM2017v2[dilepton.tau1_idx]
self.out.q_1[0] = event.Tau_charge[dilepton.tau1_idx]
self.out.decayMode_1[0] = event.Tau_decayMode[dilepton.tau1_idx]
self.out.rawAntiEleCat_1[0] = event.Tau_rawAntiEleCat[dilepton.tau1_idx]
# print type(event.Tau_idAntiEle[dilepton.tau1_idx])
# print 'this is bitmask', ord(event.Tau_idAntiEle[dilepton.tau1_idx]), type(ord(event.Tau_idAntiEle[dilepton.tau1_idx]))
self.out.idAntiEle_1[0] = ord(event.Tau_idAntiEle[dilepton.tau1_idx])
self.out.idAntiMu_1[0] = ord(event.Tau_idAntiMu[dilepton.tau1_idx])
self.out.idDecayMode_1[0] = event.Tau_idDecayMode[dilepton.tau1_idx]
self.out.idDecayModeNewDMs_1[0] = event.Tau_idDecayModeNewDMs[dilepton.tau1_idx]
self.out.idMVAnewDM2017v2_1[0] = ord(event.Tau_idMVAnewDM2017v2[dilepton.tau1_idx])
self.out.idMVAoldDM_1[0] = ord(event.Tau_idMVAoldDM[dilepton.tau1_idx])
self.out.idMVAoldDM2017v1_1[0] = ord(event.Tau_idMVAoldDM2017v1[dilepton.tau1_idx])
self.out.idMVAoldDM2017v2_1[0] = ord(event.Tau_idMVAoldDM2017v2[dilepton.tau1_idx])
if not self.isData:
self.out.genPartFlav_1[0] = ord(event.Tau_genPartFlav[dilepton.tau1_idx])
genvistau = Collection(event, "GenVisTau")
_drmax_ = 1000
gendm = -1
genpt = -1
geneta = -1
genphi = -1
for igvt in range(event.nGenVisTau):
_dr_ = genvistau[igvt].p4().DeltaR(taus[dilepton.tau1_idx].p4())
if _dr_ < 0.5 and _dr_ < _drmax_:
_drmax_ = _dr_
gendm = event.GenVisTau_status[igvt]
genpt = event.GenVisTau_pt[igvt]
geneta = event.GenVisTau_eta[igvt]
genphi = event.GenVisTau_phi[igvt]
self.out.gendecayMode_1[0] = gendm
self.out.genvistaupt_1[0] = genpt
self.out.genvistaueta_1[0] = geneta
self.out.genvistauphi_1[0] = genphi
# tau 2
self.out.pt_2[0] = event.Tau_pt[dilepton.tau2_idx]
self.out.eta_2[0] = event.Tau_eta[dilepton.tau2_idx]
self.out.phi_2[0] = event.Tau_phi[dilepton.tau2_idx]
self.out.mass_2[0] = event.Tau_mass[dilepton.tau2_idx]
self.out.dxy_2[0] = event.Tau_dxy[dilepton.tau2_idx]
self.out.dz_2[0] = event.Tau_dz[dilepton.tau2_idx]
self.out.leadTkPtOverTauPt_2[0] = event.Tau_leadTkPtOverTauPt[dilepton.tau2_idx]
self.out.chargedIso_2[0] = event.Tau_chargedIso[dilepton.tau2_idx]
self.out.neutralIso_2[0] = event.Tau_neutralIso[dilepton.tau2_idx]
self.out.photonsOutsideSignalCone_2[0] = event.Tau_photonsOutsideSignalCone[dilepton.tau2_idx]
self.out.puCorr_2[0] = event.Tau_puCorr[dilepton.tau2_idx]
self.out.rawAntiEle_2[0] = event.Tau_rawAntiEle[dilepton.tau2_idx]
self.out.rawIso_2[0] = event.Tau_rawIso[dilepton.tau2_idx]
self.out.rawMVAnewDM2017v2_2[0] = event.Tau_rawMVAnewDM2017v2[dilepton.tau2_idx]
self.out.rawMVAoldDM_2[0] = event.Tau_rawMVAoldDM[dilepton.tau2_idx]
self.out.rawMVAoldDM2017v1_2[0] = event.Tau_rawMVAoldDM2017v1[dilepton.tau2_idx]
self.out.rawMVAoldDM2017v2_2[0] = event.Tau_rawMVAoldDM2017v2[dilepton.tau2_idx]
self.out.q_2[0] = event.Tau_charge[dilepton.tau2_idx]
self.out.decayMode_2[0] = event.Tau_decayMode[dilepton.tau2_idx]
self.out.rawAntiEleCat_2[0] = event.Tau_rawAntiEleCat[dilepton.tau2_idx]
self.out.idAntiEle_2[0] = ord(event.Tau_idAntiEle[dilepton.tau2_idx])
self.out.idAntiMu_2[0] = ord(event.Tau_idAntiMu[dilepton.tau2_idx])
self.out.idDecayMode_2[0] = event.Tau_idDecayMode[dilepton.tau2_idx]
self.out.idDecayModeNewDMs_2[0] = event.Tau_idDecayModeNewDMs[dilepton.tau2_idx]
self.out.idMVAnewDM2017v2_2[0] = ord(event.Tau_idMVAnewDM2017v2[dilepton.tau2_idx])
self.out.idMVAoldDM_2[0] = ord(event.Tau_idMVAoldDM[dilepton.tau2_idx])
self.out.idMVAoldDM2017v1_2[0] = ord(event.Tau_idMVAoldDM2017v1[dilepton.tau2_idx])
self.out.idMVAoldDM2017v2_2[0] = ord(event.Tau_idMVAoldDM2017v2[dilepton.tau2_idx])
# print type(event.Tau_genPartFlav[dilepton.tau2_idx])
if not self.isData:
self.out.genPartFlav_2[0] = ord(event.Tau_genPartFlav[dilepton.tau2_idx])
genvistau = Collection(event, "GenVisTau")
_drmax_ = 1000
gendm = -1
genpt = -1
geneta = -1
genphi = -1
for igvt in range(event.nGenVisTau):
_dr_ = genvistau[igvt].p4().DeltaR(taus[dilepton.tau2_idx].p4())
if _dr_ < 0.5 and _dr_ < _drmax_:
_drmax_ = _dr_
gendm = event.GenVisTau_status[igvt]
genpt = event.GenVisTau_pt[igvt]
geneta = event.GenVisTau_eta[igvt]
genphi = event.GenVisTau_phi[igvt]
self.out.gendecayMode_2[0] = gendm
self.out.genvistaupt_2[0] = genpt
self.out.genvistaueta_2[0] = geneta
self.out.genvistauphi_2[0] = genphi
# event weights
self.out.run[0] = event.run
self.out.luminosityBlock[0] = event.luminosityBlock
# print 'event =', event.event & 0xffffffffffffffff, 'original = ', event.event
self.out.event[0] = event.event & 0xffffffffffffffff
self.out.MET_pt[0] = event.MET_pt
self.out.MET_phi[0] = event.MET_phi
self.out.PuppiMET_pt[0] = event.PuppiMET_pt
self.out.PuppiMET_phi[0] = event.PuppiMET_phi
self.out.MET_significance[0] = event.MET_significance
self.out.MET_covXX[0] = event.MET_covXX
self.out.MET_covXY[0] = event.MET_covXY
self.out.MET_covYY[0] = event.MET_covYY
self.out.fixedGridRhoFastjetAll[0] = event.fixedGridRhoFastjetAll
self.out.PV_npvs[0] = event.PV_npvs
self.out.PV_npvsGood[0] = event.PV_npvsGood
if not self.isData:
self.out.GenMET_pt[0] = event.GenMET_pt
self.out.GenMET_phi[0] = event.GenMET_phi
self.out.Pileup_nPU[0] = event.Pileup_nPU
self.out.Pileup_nTrueInt[0] = event.Pileup_nTrueInt
self.out.genWeight[0] = event.genWeight
# print 'check (LO)', event.LHE_NpLO, type(event.LHE_NpLO)
# print 'check (NLO)', event.LHE_NpNLO, type(event.LHE_NpNLO)
# self.out.LHE_NpLO[0] = event.LHE_NpLO
# self.out.LHE_NpNLO[0] = event.LHE_NpNLO
self.out.LHE_Njets[0] = event.LHE_Njets
# print self.out.LHE_Njets[0], event.LHE_Njets
# print self.out.event[0], event.event, (event.event & 0xffffffffffffffff)
# print self.out.LHE_Njets[0], event.LHE_Njets, int(event.LHE_Njets)
# print event.LHE_NpNLO
# print self.out.Pileup_nPU, event.Pileup_nPU
self.out.jpt_1[0] = -9.
self.out.jeta_1[0] = -9.
self.out.jphi_1[0] = -9.
self.out.jcsvv2_1[0] = -9.
self.out.jdeepb_1[0] = -9.
self.out.jpt_2[0] = -9.
self.out.jeta_2[0] = -9.
self.out.jphi_2[0] = -9.
self.out.jcsvv2_2[0] = -9.
self.out.jdeepb_2[0] = -9.
if len(jetIds)>0:
self.out.jpt_1[0] = event.Jet_pt[jetIds[0]]
self.out.jeta_1[0] = event.Jet_eta[jetIds[0]]
self.out.jphi_1[0] = event.Jet_phi[jetIds[0]]
self.out.jcsvv2_1[0] = event.Jet_btagCSVV2[jetIds[0]]
self.out.jdeepb_1[0] = event.Jet_btagDeepB[jetIds[0]]
if len(jetIds)>1:
self.out.jpt_2[0] = event.Jet_pt[jetIds[1]]
self.out.jeta_2[0] = event.Jet_eta[jetIds[1]]
self.out.jphi_2[0] = event.Jet_phi[jetIds[1]]
self.out.jcsvv2_2[0] = event.Jet_btagCSVV2[jetIds[1]]
self.out.jdeepb_2[0] = event.Jet_btagDeepB[jetIds[1]]
self.out.njets[0] = len(jetIds)
self.out.nfjets[0] = nfjets
self.out.ncjets[0] = ncjets
self.out.nbtag[0] = nbtag
self.out.pfmt_1[0] = math.sqrt( 2 * self.out.pt_1[0] * self.out.MET_pt[0] * ( 1 - math.cos(deltaPhi(self.out.phi_1[0], self.out.MET_phi[0])) ) );
self.out.pfmt_2[0] = math.sqrt( 2 * self.out.pt_2[0] * self.out.MET_pt[0] * ( 1 - math.cos(deltaPhi(self.out.phi_2[0], self.out.MET_phi[0])) ) );
self.out.m_vis[0] = (taus[dilepton.tau1_idx].p4() + taus[dilepton.tau2_idx].p4()).M()
self.out.pt_tt[0] = (taus[dilepton.tau1_idx].p4() + taus[dilepton.tau2_idx].p4()).Pt()
self.out.dR_ll[0] = taus[dilepton.tau1_idx].p4().DeltaR(taus[dilepton.tau2_idx].p4())
self.out.dphi_ll[0] = deltaPhi(self.out.phi_1[0], self.out.phi_2[0])
# pzeta calculation
leg1 = ROOT.TVector3(taus[dilepton.tau1_idx].p4().Px(), taus[dilepton.tau1_idx].p4().Py(), 0.)
leg2 = ROOT.TVector3(taus[dilepton.tau2_idx].p4().Px(), taus[dilepton.tau2_idx].p4().Py(), 0.)
# print 'leg1 px,py,pz = ', taus[dilepton.tau1_idx].p4().Px(), taus[dilepton.tau1_idx].p4().Py(), '0'
# print 'leg2 px,py,pz = ', taus[dilepton.tau2_idx].p4().Px(), taus[dilepton.tau2_idx].p4().Py(), '0'
met_tlv = ROOT.TLorentzVector()
met_tlv.SetPxPyPzE(self.out.MET_pt[0]*math.cos(self.out.MET_phi[0]),
self.out.MET_pt[0]*math.cos(self.out.MET_phi[0]),
0,
self.out.MET_pt[0])
# print self.out.MET_pt[0]*math.cos(self.out.MET_phi[0]), self.out.MET_pt[0]*math.cos(self.out.MET_phi[0]), '0', self.out.MET_pt[0]
metleg = met_tlv.Vect()
zetaAxis = ROOT.TVector3(leg1.Unit() + leg2.Unit()).Unit()
pZetaVis_ = leg1*zetaAxis + leg2*zetaAxis
pZetaMET_ = metleg*zetaAxis
# print 'pZetaVis = ', pZetaVis_, ' pZetaMET = ', pZetaMET_
self.out.pzetamiss[0] = pZetaMET_
self.out.pzetavis[0] = pZetaVis_
self.out.pzeta_disc[0] = pZetaMET_ - 0.5*pZetaVis_
############ extra lepton vetos
self.out.extramuon_veto[0], self.out.extraelec_veto[0], self.out.dilepton_veto[0] = extraLeptonVetos(event, [-1], [-1], self.name)
self.out.isData[0] = self.isData
diTauLeg1SF = self.tauSFs.getDiTauScaleFactor( self.out.pt_1, self.out.eta_1, self.out.phi_1 )
diTauLeg2SF = self.tauSFs.getDiTauScaleFactor( self.out.pt_2, self.out.eta_2, self.out.phi_2 )
self.out.weight[0] = diTauLeg1SF*diTauLeg2SF
self.out.trg_doubletau[0] = (event.HLT_DoubleTightChargedIsoPFTau35_Trk1_TightID_eta2p1_Reg > 0.5 or event.HLT_DoubleMediumChargedIsoPFTau40_Trk1_TightID_eta2p1_Reg > 0.5 or event.HLT_DoubleTightChargedIsoPFTau40_Trk1_eta2p1_Reg > 0.5)
self.out.tree.Fill()
return True
# define modules using the syntax 'name = lambda : constructor' to avoid having them loaded when not needed
#TauTauModule = lambda : TauTauProducer(jetSelection= lambda j : j.pt > 30)