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 
19 
20 def mtw(x1,x2):
21  import math
22  return math.sqrt(2*x1.pt()*x2.pt()*(1-math.cos(x1.phi()-x2.phi())))
23 
24 def makeNearestLeptons(leptons,track, event):
25 
26  minDeltaR = 99999
27 
28  nearestLepton = []
29  ibest=-1
30  for i,lepton in enumerate(leptons):
31  minDeltaRtemp=deltaR(lepton.eta(),lepton.phi(),track.eta(),track.phi())
32  if minDeltaRtemp < minDeltaR:
33  minDeltaR = minDeltaRtemp
34  ibest=i
35 
36  if len(leptons) > 0 and ibest!=-1:
37  nearestLepton.append(leptons[ibest])
38 
39  return nearestLepton
40 
41 class IsoTrackAnalyzer( Analyzer ):
42 
43  def __init__(self, cfg_ana, cfg_comp, looperName ):
44  super(IsoTrackAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
45 
46  #----------------------------------------
47  # DECLARATION OF HANDLES OF LEPTONS STUFF
48  #----------------------------------------
49  def declareHandles(self):
50  super(IsoTrackAnalyzer, self).declareHandles()
51  self.handles['cmgCand'] = AutoHandle(self.cfg_ana.candidates,self.cfg_ana.candidatesTypes)
52  self.handles['met'] = AutoHandle( 'slimmedMETs', 'std::vector<pat::MET>' )
53 
54  def beginLoop(self, setup):
55  super(IsoTrackAnalyzer,self).beginLoop(setup)
56  self.counters.addCounter('events')
57  count = self.counters.counter('events')
58  count.register('all events')
59  count.register('has >=1 selected Track')
60  count.register('has >=1 selected Iso Track')
61 
62  #------------------
63  # MAKE LIST
64  #------------------
65  def makeIsoTrack(self, event):
66 
67 
68 
69  event.selectedIsoTrack = []
70  event.selectedIsoCleanTrack = []
71  #event.preIsoTrack = []
72 
73  pfcands = self.handles['cmgCand'].product()
74 
75  charged = [ p for p in pfcands if ( p.charge() != 0 and abs(p.dz())<=self.cfg_ana.dzMax ) ]
76 
77  alltrack = map( IsoTrack, charged )
78 
79 
80  for track in alltrack:
81 
82  foundNonIsoTrack = False
83 
84 ## ===> require Track Candidate above some pt and charged
85  if ( (abs(track.pdgId())!=11) and (abs(track.pdgId())!=13) and (track.pt() < self.cfg_ana.ptMin) ): continue
86  if ( track.pt() < self.cfg_ana.ptMinEMU ): continue
87 
88 
89 ## ===> require is not the leading lepton and opposite to the leading lepton
90  if( (self.cfg_ana.doSecondVeto) and len(event.selectedLeptons)>0) :
91  if( deltaR(event.selectedLeptons[0].eta(), event.selectedLeptons[0].phi(), track.eta(), track.phi()) <0.01) : continue
92  if ( (abs(track.pdgId())!=11) and (abs(track.pdgId())!=13) and (track.charge()*event.selectedLeptons[0].charge()) ): continue
93 
94 ## ===> Redundant:: require the Track Candidate with a minimum dz
95  track.associatedVertex = event.goodVertices[0]
96 
97 ## ===> compute the isolation and find the most isolated track
98 
99  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 ) ]
100  #othertracks = alltrack
101 
102  isoSum=0
103  for part in othertracks:
104  #### ===> skip pfcands with a pt min (this should be 0)
105  #if part.pt()<self.cfg_ana.ptPartMin : continue
106  #### ===> skip pfcands outside the cone (this should be 0.3)
107  #if deltaR(part.eta(), part.phi(), track.eta(), track.phi()) > self.cfg_ana.isoDR : continue
108  isoSum += part.pt()
109  ### break the loop to save time
110  if(isoSum > (self.cfg_ana.maxAbsIso + track.pt())):
111  foundNonIsoTrack = True
112  break
113 
114  if foundNonIsoTrack: continue
115 
116  ## reset
117  #isoSum=0
118  #for part in othertracks :
119  #### ===> skip pfcands with a pt min (this should be 0)
120  # if part.pt()<self.cfg_ana.ptPartMin : continue
121  #### ===> skip pfcands outside the cone (this should be 0.3)
122  # if deltaR(part.eta(), part.phi(), track.eta(), track.phi()) > self.cfg_ana.isoDR : continue
123  # isoSum += part.pt()
124 
125  # ### isoSum = isoSum/track.pt() ## <--- this is for relIso
126 
127  ### ===> the sum should not contain the track candidate
128 
129  track.absIso = isoSum - track.pt()
130 
131  #### store a preIso track
132  #event.preIsoTrack.append(track)
133 
134 # if (isoSum < minIsoSum ) :
135  if(track.absIso < min(0.2*track.pt(), self.cfg_ana.maxAbsIso)):
136  event.selectedIsoTrack.append(track)
137 
138  if self.cfg_ana.doPrune:
139  myMet = self.handles['met'].product()[0]
140  mtwIsoTrack = mtw(track, myMet)
141  if mtwIsoTrack < 100:
142  if abs(track.pdgId()) == 11 or abs(track.pdgId()) == 13:
143  if track.pt()>5 and track.absIso/track.pt()<0.2:
144 
145  myLeptons = [ l for l in event.selectedLeptons if l.pt() > 10 ]
146  nearestSelectedLeptons = makeNearestLeptons(myLeptons,track, event)
147  if len(nearestSelectedLeptons) > 0:
148  for lep in nearestSelectedLeptons:
149  if deltaR(lep.eta(), lep.phi(), track.eta(), track.phi()) > 0.1:
150  event.selectedIsoCleanTrack.append(track)
151  else:
152  event.selectedIsoCleanTrack.append(track)
153 
154  event.selectedIsoTrack.sort(key = lambda l : l.pt(), reverse = True)
155  event.selectedIsoCleanTrack.sort(key = lambda l : l.pt(), reverse = True)
156 
157  self.counters.counter('events').inc('all events')
158  #if(len(event.preIsoTrack)): self.counters.counter('events').inc('has >=1 selected Track')
159  if(len(event.selectedIsoTrack)): self.counters.counter('events').inc('has >=1 selected Iso Track')
160 
161  def matchIsoTrack(self, event):
162  matchTau = matchObjectCollection3(event.selectedIsoTrack, event.gentaus + event.gentauleps + event.genleps, deltaRMax = 0.5)
163  for lep in event.selectedIsoTrack:
164  gen = matchTau[lep]
165  lep.mcMatchId = 1 if gen else 0
166 
167  def printInfo(self, event):
168  print 'event to Veto'
169  print '----------------'
170 
171  if len(event.selectedIsoTrack)>0:
172  print 'lenght: ',len(event.selectedIsoTrack)
173  print 'track candidate pt: ',event.selectedIsoTrack[0].pt()
174  print 'track candidate eta: ',event.selectedIsoTrack[0].eta()
175  print 'track candidate phi: ',event.selectedIsoTrack[0].phi()
176  print 'track candidate mass: ',event.selectedIsoTrack[0].mass()
177  print 'pdgId candidate : ',event.selectedIsoTrack[0].pdgId()
178  print 'dz: ',event.selectedIsoTrack[0].dz()
179  print 'iso: ',event.selectedIsoTrack[0].absIso
180  print 'matchId: ',event.selectedIsoTrack[0].mcMatchId
181 
182 # for lepton in event.selectedLeptons:
183 # print 'good lepton type: ',lepton.pdgId()
184 # print 'pt: ',lepton.pt()
185 
186 # for tau in event.selectedTaus:
187 # print 'good lepton type: ',tau.pdgId()
188 # print 'pt: ',tau.pt()
189 
190  print '----------------'
191 
192 
193  def process(self, event):
194 
195  if self.cfg_ana.setOff:
196  return True
197 
198  self.readCollections( event.input )
199  self.makeIsoTrack(event)
200 
201  if len(event.selectedIsoTrack)==0 : return True
202 
203 ## event.pdgIdIsoTrack.append(event.selectedIsoTrack[0].pdgId())
204 ## event.isoIsoTrack.append(minIsoSum)
205 ## event.dzIsoTrack.append(abs(dz(event.selectedIsoTrack[0])))
206 
207 ### ===> do matching
208 
209  if not self.cfg_comp.isMC:
210  return True
211 
212  if hasattr(event, 'gentaus') and hasattr(event, 'gentauleps') and hasattr(event, 'genleps') :
213  self.matchIsoTrack(event)
214 
215 ### self.printInfo(event)
216 
217 ### ===> do veto if needed
218 
219 # if (self.cfg_ana.doSecondVeto and (event.selectedIsoTrack[0].pdgId()!=11) and (event.selectedIsoTrack[0].pdgId()!=12) and event.isoIsoTrack < self.cfg_ana.MaxIsoSum ) :
220 ### self.printInfo(event)
221 # return False
222 
223 # if ((self.cfg_ana.doSecondVeto and event.selectedIsoTrack[0].pdgId()==11 or event.selectedIsoTrack[0].pdgId()==12) and event.isoIsoTrack < self.cfg_ana.MaxIsoSumEMU ) :
224 ## self.printInfo(event)
225 # return False
226 
227 
228  return True
229 
230 
231 setattr(IsoTrackAnalyzer,"defaultConfig",cfg.Analyzer(
232  class_object=IsoTrackAnalyzer,
233  setOff=True,
234  #####
235  candidates='packedPFCandidates',
236  candidatesTypes='std::vector<pat::PackedCandidate>',
237  ptMin = 5, # for pion
238  ptMinEMU = 5, # for EMU
239  dzMax = 0.1,
240  #####
241  isoDR = 0.3,
242  ptPartMin = 0,
243  dzPartMax = 0.1,
244  maxAbsIso = 8,
245  #####
246  MaxIsoSum = 0.1, ### unused
247  MaxIsoSumEMU = 0.2, ### unused
248  doSecondVeto = False,
249  #####
250  doPrune = True,
251  )
252 )
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:38
Definition: DDAxes.h:10