-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathModuleMuTau.py
282 lines (240 loc) · 11.9 KB
/
ModuleMuTau.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
# Author: Izaak Neutelings (June 2020)
# Description: Simple module to pre-select mutau events
import sys
import numpy as np
from TauFW.PicoProducer.analysis.TreeProducerMuTau import *
from TauFW.PicoProducer.analysis.ModuleTauPair import *
from TauFW.PicoProducer.analysis.utils import LeptonTauPair, loosestIso, idIso, matchgenvistau, matchtaujet
from TauFW.PicoProducer.corrections.MuonSFs import *
#from TauFW.PicoProducer.corrections.TrigObjMatcher import loadTriggerDataFromJSON, TrigObjMatcher
from TauPOG.TauIDSFs.TauIDSFTool import TauIDSFTool, TauESTool
class ModuleMuTau(ModuleTauPair):
def __init__(self, fname, **kwargs):
kwargs['channel'] = 'mutau'
super(ModuleMuTau,self).__init__(fname,**kwargs)
self.out = TreeProducerMuTau(fname,self)
# TRIGGERS
if self.year==2016:
self.trigger = lambda e: e.HLT_IsoMu22 or e.HLT_IsoMu22_eta2p1 or e.HLT_IsoTkMu22 or e.HLT_IsoTkMu22_eta2p1 #or e.HLT_IsoMu19_eta2p1_LooseIsoPFTau20_SingleL1
self.muonCutPt = lambda e: 23
self.muonCutEta = lambda e: 2.4 if e.HLT_IsoMu22 or e.HLT_IsoTkMu22 else 2.1
elif self.year==2017:
self.trigger = lambda e: e.HLT_IsoMu24 or e.HLT_IsoMu27 #or e.HLT_IsoMu20_eta2p1_LooseChargedIsoPFTau27_eta2p1_CrossL1
self.muonCutPt = lambda e: 25 if e.HLT_IsoMu24 else 28
self.muonCutEta = lambda e: 2.4
else:
self.trigger = lambda e: e.HLT_IsoMu24 or e.HLT_IsoMu27 #or e.HLT_IsoMu20_eta2p1_LooseChargedIsoPFTau27_eta2p1_CrossL1
self.muonCutPt = lambda e: 25
self.muonCutEta = lambda e: 2.4
self.tauCutPt = 20
self.tauCutEta = 2.3
# CORRECTIONS
if self.ismc:
self.muSFs = MuonSFs(era=self.era,verb=self.verbosity)
self.tesTool = TauESTool(tauSFVersion[self.year])
self.tauSFs = TauIDSFTool(tauSFVersion[self.year],'DeepTau2017v2p1VSjet','Tight')
self.etfSFs = TauIDSFTool(tauSFVersion[self.year],'DeepTau2017v2p1VSe', 'VLoose')
self.mtfSFs = TauIDSFTool(tauSFVersion[self.year],'DeepTau2017v2p1VSmu', 'Tight')
# CUTFLOW
self.out.cutflow.addcut('none', "no cut" )
self.out.cutflow.addcut('trig', "trigger" )
self.out.cutflow.addcut('muon', "muon" )
self.out.cutflow.addcut('tau', "tau" )
self.out.cutflow.addcut('pair', "pair" )
self.out.cutflow.addcut('weight', "no cut, weighted", 15 )
self.out.cutflow.addcut('weight_no0PU', "no cut, weighted, PU>0", 16 ) # use for normalization
def beginJob(self):
"""Before processing any events or files."""
super(ModuleMuTau,self).beginJob()
print ">>> %-12s = %s"%('tauwp', self.tauwp)
print ">>> %-12s = %s"%('muonCutPt', self.muonCutPt)
print ">>> %-12s = %s"%('muonCutEta', self.muonCutEta)
print ">>> %-12s = %s"%('tauCutPt', self.tauCutPt)
print ">>> %-12s = %s"%('tauCutEta', self.tauCutEta)
pass
def analyze(self, event):
"""Process and pre-select events; fill branches and return True if the events passes,
return False otherwise."""
sys.stdout.flush()
##### NO CUT #####################################
self.out.cutflow.fill('none')
if self.isdata:
self.out.cutflow.fill('weight',1.)
if event.PV_npvs>0:
self.out.cutflow.fill('weight_no0PU',1.)
else:
return False
else:
self.out.cutflow.fill('weight',event.genWeight)
self.out.pileup.Fill(event.Pileup_nTrueInt)
if event.Pileup_nTrueInt>0:
self.out.cutflow.fill('weight_no0PU',event.genWeight)
else:
return False
##### TRIGGER ####################################
if not self.trigger(event):
return False
self.out.cutflow.fill('trig')
##### MUON #######################################
muons = [ ]
for muon in Collection(event,'Muon'):
if muon.pt<self.muonCutPt(event): continue
if abs(muon.eta)>self.muonCutEta(event): continue
if abs(muon.dz)>0.2: continue
if abs(muon.dxy)>0.045: continue
if not muon.mediumId: continue
if muon.pfRelIso04_all>0.50: continue
muons.append(muon)
if len(muons)==0:
return False
self.out.cutflow.fill('muon')
##### TAU ########################################
taus = [ ]
for tau in Collection(event,'Tau'):
if abs(tau.eta)>self.tauCutEta: continue
if abs(tau.dz)>0.2: continue
if tau.decayMode not in [0,1,10,11]: continue
if abs(tau.charge)!=1: continue
if tau.idDeepTau2017v2p1VSe<1: continue # VVVLoose
if tau.idDeepTau2017v2p1VSmu<1: continue # VLoose
if tau.idDeepTau2017v2p1VSjet<self.tauwp: continue
if self.ismc:
genmatch = tau.genPartFlav
if genmatch==5: # real tau
tes = 1
if self.tes!=None:
tes *= self.tes
else:
tes *= self.tesTool.getTES(tau.pt,tau.decayMode,unc=self.tessys)
if tes!=1:
tau.pt *= tes
tau.mass *= tes
elif self.ltf!=1.0 and 0<genmatch<5: # lepton -> tau fake
tau.pt *= self.ltf
tau.mass *= self.ltf
elif self.jtf!=1.0 and genmatch==0: # jet -> tau fake
tau.pt *= self.jtf
tau.mass *= self.jtf
if tau.pt<self.tauCutPt: continue
taus.append(tau)
if len(taus)==0:
return False
self.out.cutflow.fill('tau')
##### MUTAU PAIR #################################
ltaus = [ ]
for muon in muons:
for tau in taus:
if tau.DeltaR(muon)<0.5: continue
ltau = LeptonTauPair(muon,muon.pfRelIso04_all,tau,tau.rawDeepTau2017v2p1VSjet)
ltaus.append(ltau)
if len(ltaus)==0:
return False
muon, tau = max(ltaus).pair
muon.tlv = muon.p4()
tau.tlv = tau.p4()
self.out.cutflow.fill('pair')
# VETOS
extramuon_veto, extraelec_veto, dilepton_veto = getlepvetoes(event,[ ],[muon],[tau],self.channel)
self.out.extramuon_veto[0], self.out.extraelec_veto[0], self.out.dilepton_veto[0] = getlepvetoes(event,[ ],[muon],[ ],self.channel)
self.out.lepton_vetoes[0] = self.out.extramuon_veto[0] or self.out.extraelec_veto[0] or self.out.dilepton_veto[0]
self.out.lepton_vetoes_notau[0] = extramuon_veto or extraelec_veto or dilepton_veto
# EVENT
self.fillEventBranches(event)
# MUON
self.out.pt_1[0] = muon.pt
self.out.eta_1[0] = muon.eta
self.out.phi_1[0] = muon.phi
self.out.m_1[0] = muon.mass
self.out.y_1[0] = muon.tlv.Rapidity()
self.out.dxy_1[0] = muon.dxy
self.out.dz_1[0] = muon.dz
self.out.q_1[0] = muon.charge
self.out.iso_1[0] = muon.pfRelIso04_all
# TAU
self.out.pt_2[0] = tau.pt
self.out.eta_2[0] = tau.eta
self.out.phi_2[0] = tau.phi
self.out.m_2[0] = tau.mass
self.out.y_2[0] = tau.tlv.Rapidity()
self.out.dxy_2[0] = tau.dxy
self.out.dz_2[0] = tau.dz
self.out.q_2[0] = tau.charge
self.out.dm_2[0] = tau.decayMode
self.out.iso_2[0] = tau.rawIso
self.out.idiso_2[0] = idIso(tau) # cut-based tau isolation (rawIso)
self.out.rawAntiEle_2[0] = tau.rawAntiEle
self.out.rawMVAoldDM2017v2_2[0] = tau.rawMVAoldDM2017v2
self.out.rawMVAnewDM2017v2_2[0] = tau.rawMVAnewDM2017v2
self.out.rawDeepTau2017v2p1VSe_2[0] = tau.rawDeepTau2017v2p1VSe
self.out.rawDeepTau2017v2p1VSmu_2[0] = tau.rawDeepTau2017v2p1VSmu
self.out.rawDeepTau2017v2p1VSjet_2[0] = tau.rawDeepTau2017v2p1VSjet
self.out.idAntiEle_2[0] = tau.idAntiEle
self.out.idAntiMu_2[0] = tau.idAntiMu
self.out.idDecayMode_2[0] = tau.idDecayMode
self.out.idDecayModeNewDMs_2[0] = tau.idDecayModeNewDMs
self.out.idMVAoldDM2017v2_2[0] = tau.idMVAoldDM2017v2
self.out.idMVAnewDM2017v2_2[0] = tau.idMVAnewDM2017v2
self.out.idDeepTau2017v2p1VSe_2[0] = tau.idDeepTau2017v2p1VSe
self.out.idDeepTau2017v2p1VSmu_2[0] = tau.idDeepTau2017v2p1VSmu
self.out.idDeepTau2017v2p1VSjet_2[0] = tau.idDeepTau2017v2p1VSjet
self.out.chargedIso_2[0] = tau.chargedIso
self.out.neutralIso_2[0] = tau.neutralIso
self.out.leadTkPtOverTauPt_2[0] = tau.leadTkPtOverTauPt
self.out.photonsOutsideSignalCone_2[0] = tau.photonsOutsideSignalCone
self.out.puCorr_2[0] = tau.puCorr
# GENERATOR
if self.ismc:
self.out.genmatch_1[0] = muon.genPartFlav
self.out.genmatch_2[0] = tau.genPartFlav
pt, phi, eta, status = matchgenvistau(event,tau)
self.out.genvistaupt_2[0] = pt
self.out.genvistaueta_2[0] = eta
self.out.genvistauphi_2[0] = phi
self.out.gendm_2[0] = status
# JETS
jets, met, njets_vars, met_vars = self.fillJetBranches(event,muon,tau)
if self.ismc:
self.out.jpt_match_2[0], self.out.jpt_genmatch_2[0] = matchtaujet(event,tau,self.ismc)
else:
self.out.jpt_match_2[0] = matchtaujet(event,tau,self.ismc)[0]
# WEIGHTS
if self.ismc:
self.fillCommonCorrBraches(event,jets,met,njets_vars,met_vars)
if muon.pfRelIso04_all<0.50 and tau.idDeepTau2017v2p1VSjet>=2:
self.btagTool.fillEffMaps(jets,usejec=self.dojec)
# MUON WEIGHTS
self.out.trigweight[0] = self.muSFs.getTriggerSF(muon.pt,muon.eta)
self.out.idisoweight_1[0] = self.muSFs.getIdIsoSF(muon.pt,muon.eta)
# DEFAULTS
self.out.idweight_2[0] = 1.
self.out.ltfweight_2[0] = 1.
if not self.dotight:
self.out.idweightUp_2[0] = 1.
self.out.idweightDown_2[0] = 1.
self.out.ltfweightUp_2[0] = 1.
self.out.ltfweightDown_2[0] = 1.
# TAU WEIGHTS
if tau.genPartFlav==5: # real tau
self.out.idweight_2[0] = self.tauSFs.getSFvsPT(tau.pt)
if not self.dotight:
self.out.idweightUp_2[0] = self.tauSFs.getSFvsPT(tau.pt,unc='Up')
self.out.idweightDown_2[0] = self.tauSFs.getSFvsPT(tau.pt,unc='Down')
elif tau.genPartFlav in [1,3]: # muon -> tau fake
self.out.ltfweight_2[0] = self.etfSFs.getSFvsEta(tau.eta,tau.genPartFlav)
if not self.dotight:
self.out.ltfweightUp_2[0] = self.etfSFs.getSFvsEta(tau.eta,tau.genPartFlav,unc='Up')
self.out.ltfweightDown_2[0] = self.etfSFs.getSFvsEta(tau.eta,tau.genPartFlav,unc='Down')
elif tau.genPartFlav in [2,4]: # electron -> tau fake
self.out.ltfweight_2[0] = self.mtfSFs.getSFvsEta(tau.eta,tau.genPartFlav)
if not self.dotight:
self.out.ltfweightUp_2[0] = self.mtfSFs.getSFvsEta(tau.eta,tau.genPartFlav,unc='Up')
self.out.ltfweightDown_2[0] = self.mtfSFs.getSFvsEta(tau.eta,tau.genPartFlav,unc='Down')
self.out.weight[0] = self.out.genweight[0]*self.out.puweight[0]*self.out.trigweight[0]*self.out.idisoweight_1[0] #*self.out.idisoweight_2[0]
elif self.isembed:
###self.applyCommonEmbdedCorrections(event,jets,jetIds50,met,njets_vars,met_vars)
self.out.genweight[0] = event.genWeight
self.out.trackweight[0] = 0.975 if tau.decayMode==0 else 1.0247 if tau.decayMode==1 else 0.927 if tau.decayMode==10 else 0.974 if tau.decayMode==11 else 1.0
# MET & DILEPTON VARIABLES
self.fillMETAndDiLeptonBranches(event,muon.tlv,tau.tlv,met,met_vars)
self.out.fill()
return True