CMS 3D CMS Logo

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