CMS 3D CMS Logo

Muon.py
Go to the documentation of this file.
1 from PhysicsTools.Heppy.physicsobjects.Lepton import Lepton
3 
4 class Muon( Lepton ):
5 
6  def __init__(self, *args, **kwargs):
7  super(Muon, self).__init__(*args, **kwargs)
8  self._trackForDxyDz = "muonBestTrack"
9 
10  def setTrackForDxyDz(self,what):
11  if not hasattr(self,what):
12  raise RuntimeError("I don't have a track called "+what)
13  self._trackForDxyDz = what
14 
15  def looseId( self ):
16  '''Loose ID as recommended by mu POG.'''
17  return self.physObj.isLooseMuon()
18 
19  def tightId( self ):
20  '''Tight ID as recommended by mu POG
21  (unless redefined in the lepton analyzer).
22 
23  If not using the LeptonAnalyzer, make sure to set self.associatedVertex,
24  that is necessary for tight muon identification.
25  '''
26  return getattr(self,"tightIdResult",self.muonID("POG_ID_Tight"))
27 
28  def muonID(self, name, vertex=None):
29  if name == "" or name is None:
30  return True
31  if name.startswith("POG_"):
32  if name == "POG_ID_Loose": return self.physObj.isLooseMuon()
33  if vertex is None:
34  vertex = getattr(self, 'associatedVertex', None)
35  if name == "POG_ID_Tight": return self.physObj.isTightMuon(vertex)
36  if name == "POG_ID_HighPt": return self.physObj.isHighPtMuon(vertex)
37  if name == "POG_ID_Soft": return self.physObj.isSoftMuon(vertex)
38  if name == "POG_ID_TightNoVtx": return self.looseId() and \
39  self.isGlobalMuon() and \
40  self.globalTrack().normalizedChi2() < 10 and \
41  self.globalTrack().hitPattern().numberOfValidMuonHits() > 0 and \
42  self.numberOfMatchedStations()>1 and \
43  self.innerTrack().hitPattern().numberOfValidPixelHits()>0 and \
44  self.innerTrack().hitPattern().trackerLayersWithMeasurement() > 5
45  if name == "POG_ID_Medium":
46  if not self.looseId(): return False
47  goodGlb = self.physObj.isGlobalMuon() and self.physObj.globalTrack().normalizedChi2() < 3 and self.physObj.combinedQuality().chi2LocalPosition < 12 and self.physObj.combinedQuality().trkKink < 20;
48  return self.physObj.innerTrack().validFraction() > 0.8 and self.physObj.segmentCompatibility() >= (0.303 if goodGlb else 0.451)
49  if name == "POG_Global_OR_TMArbitrated":
50  return self.physObj.isGlobalMuon() or (self.physObj.isTrackerMuon() and self.physObj.numberOfMatchedStations() > 0)
51  return self.physObj.muonID(name)
52 
53  def mvaId(self):
54  '''For a transparent treatment of electrons and muons. Returns -99'''
55  return -99
56 
57 
58  def dxy(self, vertex=None):
59  '''either pass the vertex, or set associatedVertex before calling the function.
60  note: the function does not work with standalone muons as innerTrack
61  is not available.
62  '''
63  if vertex is None:
64  vertex = self.associatedVertex
65  return getattr(self,self._trackForDxyDz)().dxy( vertex.position() )
66 
67  def edxy(self):
68  '''returns the uncertainty on dxy (from gsf track)'''
69  return getattr(self,self._trackForDxyDz)().dxyError()
70 
71 
72  def dz(self, vertex=None):
73  '''either pass the vertex, or set associatedVertex before calling the function.
74  note: the function does not work with standalone muons as innerTrack
75  is not available.
76  '''
77  if vertex is None:
78  vertex = self.associatedVertex
79  return getattr(self,self._trackForDxyDz)().dz( vertex.position() )
80 
81  def edz(self):
82  '''returns the uncertainty on dxz (from gsf track)'''
83  return getattr(self,self._trackForDxyDz)().dzError()
84 
85  def chargedHadronIsoR(self,R=0.4):
86  if R == 0.3: return self.physObj.pfIsolationR03().sumChargedHadronPt
87  elif R == 0.4: return self.physObj.pfIsolationR04().sumChargedHadronPt
88  raise RuntimeError("Muon chargedHadronIso missing for R=%s" % R)
89 
90  def neutralHadronIsoR(self,R=0.4):
91  if R == 0.3: return self.physObj.pfIsolationR03().sumNeutralHadronEt
92  elif R == 0.4: return self.physObj.pfIsolationR04().sumNeutralHadronEt
93  raise RuntimeError("Muon neutralHadronIso missing for R=%s" % R)
94 
95  def photonIsoR(self,R=0.4):
96  if R == 0.3: return self.physObj.pfIsolationR03().sumPhotonEt
97  elif R == 0.4: return self.physObj.pfIsolationR04().sumPhotonEt
98  raise RuntimeError("Muon photonIso missing for R=%s" % R)
99 
100  def chargedAllIsoR(self,R=0.4):
101  if R == 0.3: return self.physObj.pfIsolationR03().sumChargedParticlePt
102  elif R == 0.4: return self.physObj.pfIsolationR04().sumChargedParticlePt
103  raise RuntimeError("Muon chargedAllIso missing for R=%s" % R)
104 
105  def chargedAllIso(self):
106  return self.chargedAllIsoR()
107 
108  def puChargedHadronIsoR(self,R=0.4):
109  if R == 0.3: return self.physObj.pfIsolationR03().sumPUPt
110  elif R == 0.4: return self.physObj.pfIsolationR04().sumPUPt
111  raise RuntimeError("Muon chargedHadronIso missing for R=%s" % R)
112 
113 
114  def absIsoWithFSR(self, R=0.4, puCorr="deltaBeta", dBetaFactor=0.5):
115  '''
116  Calculate Isolation, subtract FSR, apply specific PU corrections"
117  '''
118  photonIso = self.photonIsoR(R)
119  if hasattr(self,'fsrPhotons'):
120  for gamma in self.fsrPhotons:
121  dr = deltaR(gamma.eta(), gamma.phi(), self.physObj.eta(), self.physObj.phi())
122  if dr > 0.01 and dr < R:
123  photonIso = max(photonIso-gamma.pt(),0.0)
124  if puCorr == "deltaBeta":
125  offset = dBetaFactor * self.puChargedHadronIsoR(R)
126  elif puCorr == "rhoArea":
127  offset = self.rho*getattr(self,"EffectiveArea"+(str(R).replace(".","")))
128  elif puCorr in ["none","None",None]:
129  offset = 0
130  else:
131  raise RuntimeError("Unsupported PU correction scheme %s" % puCorr)
132  return self.chargedHadronIsoR(R)+max(0.,photonIso+self.neutralHadronIsoR(R)-offset)
133 
134  def ptErr(self):
135  if "_ptErr" in self.__dict__: return self.__dict__['_ptErr']
136  return self.bestTrack().ptError()
def edxy(self)
Definition: Muon.py:67
def muonID(self, name, vertex=None)
Definition: Muon.py:28
def edz(self)
Definition: Muon.py:81
def chargedAllIsoR(self, R=0.4)
Definition: Muon.py:100
def chargedHadronIsoR(self, R=0.4)
Definition: Muon.py:85
def puChargedHadronIsoR(self, R=0.4)
Definition: Muon.py:108
def neutralHadronIsoR(self, R=0.4)
Definition: Muon.py:90
def tightId(self)
Definition: Muon.py:19
def replace(string, replacements)
def ptErr(self)
Definition: Muon.py:134
def chargedAllIso(self)
Definition: Muon.py:105
def dz(self, vertex=None)
Definition: Muon.py:72
def looseId(self)
Definition: Muon.py:15
def photonIsoR(self, R=0.4)
Definition: Muon.py:95
def __init__(self, args, kwargs)
Definition: Muon.py:6
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
def mvaId(self)
Definition: Muon.py:53
Definition: Muon.py:4
def setTrackForDxyDz(self, what)
Definition: Muon.py:10
_trackForDxyDz
Definition: Muon.py:8
Definition: Lepton.py:1
def dxy(self, vertex=None)
Definition: Muon.py:58
def absIsoWithFSR(self, R=0.4, puCorr="deltaBeta", dBetaFactor=0.5)
Definition: Muon.py:114