CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
IsoTrackAnalyzer.py
Go to the documentation of this file.
1 import operator
2 import itertools
3 import copy
4 import types
5 
6 from ROOT import TLorentzVector
7 
8 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
9 from PhysicsTools.HeppyCore.framework.event import Event
10 from PhysicsTools.HeppyCore.statistics.counter import Counter, Counters
11 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
12 from PhysicsTools.Heppy.physicsobjects.Lepton import Lepton
13 from PhysicsTools.Heppy.physicsobjects.Tau import Tau
14 from PhysicsTools.Heppy.physicsobjects.IsoTrack import IsoTrack
15 
16 from PhysicsTools.HeppyCore.utils.deltar import deltaR, deltaPhi, bestMatch , matchObjectCollection3
17 
18 import PhysicsTools.HeppyCore.framework.config as cfg
19 
20 from ROOT import heppy
21 
22 
23 
24 
25 def mtw(x1,x2):
26  import math
27  return math.sqrt(2*x1.pt()*x2.pt()*(1-math.cos(x1.phi()-x2.phi())))
28 
29 def makeNearestLeptons(leptons,track, event):
30 
31  minDeltaR = 99999
32 
33  nearestLepton = []
34  ibest=-1
35  for i,lepton in enumerate(leptons):
36  minDeltaRtemp=deltaR(lepton.eta(),lepton.phi(),track.eta(),track.phi())
37  if minDeltaRtemp < minDeltaR:
38  minDeltaR = minDeltaRtemp
39  ibest=i
40 
41  if len(leptons) > 0 and ibest!=-1:
42  nearestLepton.append(leptons[ibest])
43 
44  return nearestLepton
45 
46 class IsoTrackAnalyzer( Analyzer ):
47 
48  def __init__(self, cfg_ana, cfg_comp, looperName ):
49  super(IsoTrackAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
51 
52  #----------------------------------------
53  # DECLARATION OF HANDLES OF LEPTONS STUFF
54  #----------------------------------------
55  def declareHandles(self):
56  super(IsoTrackAnalyzer, self).declareHandles()
57  self.handles['met'] = AutoHandle( 'slimmedMETs', 'std::vector<pat::MET>' )
58  self.handles['packedCandidates'] = AutoHandle( 'packedPFCandidates', 'std::vector<pat::PackedCandidate>')
59 
60  def beginLoop(self, setup):
61  super(IsoTrackAnalyzer,self).beginLoop(setup)
62  self.counters.addCounter('events')
63  count = self.counters.counter('events')
64  count.register('all events')
65  count.register('has >=1 selected Track')
66  count.register('has >=1 selected Iso Track')
67 
68  #------------------
69  # MAKE LIST
70  #------------------
71  def makeIsoTrack(self, event):
72 
73 
74 
75  event.selectedIsoTrack = []
76  event.selectedIsoCleanTrack = []
77  #event.preIsoTrack = []
78 
79  patcands = self.handles['packedCandidates'].product()
80 
81  charged = [ p for p in patcands if ( p.charge() != 0 and abs(p.dz())<=self.cfg_ana.dzMax ) ]
82 
83  self.IsoTrackIsolationComputer.setPackedCandidates(patcands, -1, 0.1, True)
84 
85  alltrack = map( IsoTrack, charged )
86 
87 
88  for track in alltrack:
89 
90  if ( (abs(track.pdgId())!=11) and (abs(track.pdgId())!=13) and (track.pt() < self.cfg_ana.ptMin) ): continue
91  if ( track.pt() < self.cfg_ana.ptMinEMU ): continue
92 
93  foundNonIsoTrack = False
94 
95 ## ===> require is not the leading lepton and opposite to the leading lepton
96  if( (self.cfg_ana.doSecondVeto) and len(event.selectedLeptons)>0) :
97  if( deltaR(event.selectedLeptons[0].eta(), event.selectedLeptons[0].phi(), track.eta(), track.phi()) <0.01) : continue
98  if ( (abs(track.pdgId())!=11) and (abs(track.pdgId())!=13) and (track.charge()*event.selectedLeptons[0].charge()) ): continue
99 
100 
101 ## ===> Redundant:: require the Track Candidate with a minimum dz
102  track.associatedVertex = event.goodVertices[0] if len(event.goodVertices)>0 else event.vertices[0]
103 
104 ## ===> compute the isolation and find the most isolated track
105 
106  isoSum = self.IsoTrackIsolationComputer.chargedAbsIso(track.physObj, self.cfg_ana.isoDR, 0., self.cfg_ana.ptPartMin)
107 
108  if(isoSum > (self.cfg_ana.maxAbsIso + track.pt())): continue
109 
110 
111  #if abs(track.pdgId())==211 :
112  track.absIso = isoSum - track.pt()
113 
114  #### store a preIso track
115  #event.preIsoTrack.append(track)
116 
117 # if (isoSum < minIsoSum ) :
118  if(track.absIso < min(0.2*track.pt(), self.cfg_ana.maxAbsIso)):
119  event.selectedIsoTrack.append(track)
120 
121  if self.cfg_ana.doPrune:
122  myMet = self.handles['met'].product()[0]
123  mtwIsoTrack = mtw(track, myMet)
124  if mtwIsoTrack < 100:
125  if abs(track.pdgId()) == 11 or abs(track.pdgId()) == 13:
126  if track.pt()>5 and track.absIso/track.pt()<0.2:
127 
128  myLeptons = [ l for l in event.selectedLeptons if l.pt() > 10 ]
129  nearestSelectedLeptons = makeNearestLeptons(myLeptons,track, event)
130  if len(nearestSelectedLeptons) > 0:
131  for lep in nearestSelectedLeptons:
132  if deltaR(lep.eta(), lep.phi(), track.eta(), track.phi()) > 0.1:
133  event.selectedIsoCleanTrack.append(track)
134  else:
135  event.selectedIsoCleanTrack.append(track)
136 
137 
138 
139 ## alltrack = map( IsoTrack, charged )
140 
141 ## for track in alltrack:
142 ##
143 ## foundNonIsoTrack = False
144 ##
145 #### ===> require Track Candidate above some pt and charged
146 ## if ( (abs(track.pdgId())!=11) and (abs(track.pdgId())!=13) and (track.pt() < self.cfg_ana.ptMin) ): continue
147 ## if ( track.pt() < self.cfg_ana.ptMinEMU ): continue
148 ##
149 ##
150 #### ===> require is not the leading lepton and opposite to the leading lepton
151 ## if( (self.cfg_ana.doSecondVeto) and len(event.selectedLeptons)>0) :
152 ## if( deltaR(event.selectedLeptons[0].eta(), event.selectedLeptons[0].phi(), track.eta(), track.phi()) <0.01) : continue
153 ## if ( (abs(track.pdgId())!=11) and (abs(track.pdgId())!=13) and (track.charge()*event.selectedLeptons[0].charge()) ): continue
154 ##
155 #### ===> Redundant:: require the Track Candidate with a minimum dz
156 ## track.associatedVertex = event.goodVertices[0]
157 ##
158 #### ===> compute the isolation and find the most isolated track
159 ##
160 ## othertracks = [ p for p in charged if( deltaR(p.eta(), p.phi(), track.eta(), track.phi()) < self.cfg_ana.isoDR and p.pt()>self.cfg_ana.ptPartMin ) ]
161 ## #othertracks = alltrack
162 ##
163 ## isoSum=0
164 ## for part in othertracks:
165 ## #### ===> skip pfcands with a pt min (this should be 0)
166 ## #if part.pt()<self.cfg_ana.ptPartMin : continue
167 ## #### ===> skip pfcands outside the cone (this should be 0.3)
168 ## #if deltaR(part.eta(), part.phi(), track.eta(), track.phi()) > self.cfg_ana.isoDR : continue
169 ## isoSum += part.pt()
170 ## ### break the loop to save time
171 ## if(isoSum > (self.cfg_ana.maxAbsIso + track.pt())):
172 ## foundNonIsoTrack = True
173 ## break
174 ##
175 ## if foundNonIsoTrack: continue
176 ##
177 ## ## reset
178 ## #isoSum=0
179 ## #for part in othertracks :
180 ## #### ===> skip pfcands with a pt min (this should be 0)
181 ## # if part.pt()<self.cfg_ana.ptPartMin : continue
182 ## #### ===> skip pfcands outside the cone (this should be 0.3)
183 ## # if deltaR(part.eta(), part.phi(), track.eta(), track.phi()) > self.cfg_ana.isoDR : continue
184 ## # isoSum += part.pt()
185 ##
186 ## # ### isoSum = isoSum/track.pt() ## <--- this is for relIso
187 ##
188 ## ### ===> the sum should not contain the track candidate
189 ##
190 ## track.absIso = isoSum - track.pt()
191 ##
192 ## #### store a preIso track
193 ## #event.preIsoTrack.append(track)
194 ##
195 ### if (isoSum < minIsoSum ) :
196 ## if(track.absIso < min(0.2*track.pt(), self.cfg_ana.maxAbsIso)):
197 ## event.selectedIsoTrack.append(track)
198 ##
199 ## if self.cfg_ana.doPrune:
200 ## myMet = self.handles['met'].product()[0]
201 ## mtwIsoTrack = mtw(track, myMet)
202 ## if mtwIsoTrack < 100:
203 ## if abs(track.pdgId()) == 11 or abs(track.pdgId()) == 13:
204 ## if track.pt()>5 and track.absIso/track.pt()<0.2:
205 ##
206 ## myLeptons = [ l for l in event.selectedLeptons if l.pt() > 10 ]
207 ## nearestSelectedLeptons = makeNearestLeptons(myLeptons,track, event)
208 ## if len(nearestSelectedLeptons) > 0:
209 ## for lep in nearestSelectedLeptons:
210 ## if deltaR(lep.eta(), lep.phi(), track.eta(), track.phi()) > 0.1:
211 ## event.selectedIsoCleanTrack.append(track)
212 ## else:
213 ## event.selectedIsoCleanTrack.append(track)
214 
215  event.selectedIsoTrack.sort(key = lambda l : l.pt(), reverse = True)
216  event.selectedIsoCleanTrack.sort(key = lambda l : l.pt(), reverse = True)
217 
218  self.counters.counter('events').inc('all events')
219  #if(len(event.preIsoTrack)): self.counters.counter('events').inc('has >=1 selected Track')
220  if(len(event.selectedIsoTrack)): self.counters.counter('events').inc('has >=1 selected Iso Track')
221 
222  def matchIsoTrack(self, event):
223  matchTau = matchObjectCollection3(event.selectedIsoTrack, event.gentaus + event.gentauleps + event.genleps, deltaRMax = 0.5)
224  for lep in event.selectedIsoTrack:
225  gen = matchTau[lep]
226  lep.mcMatchId = 1 if gen else 0
227 
228  def printInfo(self, event):
229  print 'event to Veto'
230  print '----------------'
231 
232  if len(event.selectedIsoTrack)>0:
233  print 'lenght: ',len(event.selectedIsoTrack)
234  print 'track candidate pt: ',event.selectedIsoTrack[0].pt()
235  print 'track candidate eta: ',event.selectedIsoTrack[0].eta()
236  print 'track candidate phi: ',event.selectedIsoTrack[0].phi()
237  print 'track candidate mass: ',event.selectedIsoTrack[0].mass()
238  print 'pdgId candidate : ',event.selectedIsoTrack[0].pdgId()
239  print 'dz: ',event.selectedIsoTrack[0].dz()
240  print 'iso: ',event.selectedIsoTrack[0].absIso
241  print 'matchId: ',event.selectedIsoTrack[0].mcMatchId
242 
243 # for lepton in event.selectedLeptons:
244 # print 'good lepton type: ',lepton.pdgId()
245 # print 'pt: ',lepton.pt()
246 
247 # for tau in event.selectedTaus:
248 # print 'good lepton type: ',tau.pdgId()
249 # print 'pt: ',tau.pt()
250 
251  print '----------------'
252 
253 
254  def process(self, event):
255 
256  if self.cfg_ana.setOff:
257  return True
258 
259  self.readCollections( event.input )
260  self.makeIsoTrack(event)
261 
262  if len(event.selectedIsoTrack)==0 : return True
263 
264 ## event.pdgIdIsoTrack.append(event.selectedIsoTrack[0].pdgId())
265 ## event.isoIsoTrack.append(minIsoSum)
266 ## event.dzIsoTrack.append(abs(dz(event.selectedIsoTrack[0])))
267 
268 ### ===> do matching
269 
270  if not self.cfg_comp.isMC:
271  return True
272 
273  if hasattr(event, 'gentaus') and hasattr(event, 'gentauleps') and hasattr(event, 'genleps') :
274  self.matchIsoTrack(event)
275 
276 ### self.printInfo(event)
277 
278 ### ===> do veto if needed
279 
280 # if (self.cfg_ana.doSecondVeto and (event.selectedIsoTrack[0].pdgId()!=11) and (event.selectedIsoTrack[0].pdgId()!=12) and event.isoIsoTrack < self.cfg_ana.MaxIsoSum ) :
281 ### self.printInfo(event)
282 # return False
283 
284 # if ((self.cfg_ana.doSecondVeto and event.selectedIsoTrack[0].pdgId()==11 or event.selectedIsoTrack[0].pdgId()==12) and event.isoIsoTrack < self.cfg_ana.MaxIsoSumEMU ) :
285 ## self.printInfo(event)
286 # return False
287 
288 
289  return True
290 
291 
292 setattr(IsoTrackAnalyzer,"defaultConfig",cfg.Analyzer(
293  class_object=IsoTrackAnalyzer,
294  setOff=True,
295  #####
296  candidates='packedPFCandidates',
297  candidatesTypes='std::vector<pat::PackedCandidate>',
298  ptMin = 5, # for pion
299  ptMinEMU = 5, # for EMU
300  dzMax = 0.1,
301  #####
302  isoDR = 0.3,
303  ptPartMin = 0,
304  dzPartMax = 0.1,
305  maxAbsIso = 8,
306  #####
307  MaxIsoSum = 0.1, ### unused
308  MaxIsoSumEMU = 0.2, ### unused
309  doSecondVeto = False,
310  #####
311  doPrune = True,
312  )
313 )
def matchIsoTrack
alltrack = map( IsoTrack, charged )
T eta() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
T min(T a, T b)
Definition: MathUtil.h:58
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
if(conf.exists("allCellsPositionCalc"))
def matchObjectCollection3
Definition: deltar.py:41
Definition: DDAxes.h:10