CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PhotonAnalyzer.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.Photon import Photon
13 
14 from PhysicsTools.HeppyCore.utils.deltar import deltaR, deltaPhi, bestMatch, matchObjectCollection3
15 
17 
18 
19 class PhotonAnalyzer( Analyzer ):
20 
21 
22  def __init__(self, cfg_ana, cfg_comp, looperName ):
23  super(PhotonAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
24  self.etaCentral = self.cfg_ana.etaCentral if hasattr(self.cfg_ana, 'etaCentral') else 9999
25 
26  def declareHandles(self):
27  super(PhotonAnalyzer, self).declareHandles()
28 
29  #----------------------------------------
30  # DECLARATION OF HANDLES OF PHOTONS STUFF
31  #----------------------------------------
32 
33  #photons
34  self.handles['photons'] = AutoHandle( self.cfg_ana.photons,'std::vector<pat::Photon>')
35  self.mchandles['packedGen'] = AutoHandle( 'packedGenParticles', 'std::vector<pat::PackedGenParticle>' )
36 
37  def beginLoop(self, setup):
38  super(PhotonAnalyzer,self).beginLoop(setup)
39  self.counters.addCounter('events')
40  count = self.counters.counter('events')
41  count.register('all events')
42  count.register('has >=1 gamma at preselection')
43  count.register('has >=1 selected gamma')
44 
45  def makePhotons(self, event):
46  event.allphotons = map( Photon, self.handles['photons'].product() )
47  event.allphotons.sort(key = lambda l : l.pt(), reverse = True)
48 
49  event.selectedPhotons = []
50  event.selectedPhotonsCentral = []
51 
52  foundPhoton = False
53  for gamma in event.allphotons:
54  if gamma.pt() < self.cfg_ana.ptMin: continue
55  if abs(gamma.eta()) > self.cfg_ana.etaMax: continue
56  foundPhoton = True
57 
58  def idWP(gamma,X):
59  """Create an integer equal to 1-2-3 for (loose,medium,tight)"""
60 
61  id=0
62  if gamma.photonID(X%"Loose"):
63  id=1
64  #if gamma.photonID(X%"Medium"):
65  # id=2
66  if gamma.photonID(X%"Tight"):
67  id=3
68  return id
69 
70  gamma.idCutBased = idWP(gamma, "PhotonCutBasedID%s")
71 
72 
73  keepThisPhoton = True
74  if self.cfg_ana.gammaID=="PhotonCutBasedIDLoose_CSA14" :
75  keepThisPhoton = gamma.photonIDCSA14("PhotonCutBasedIDLoose_CSA14")
76  gamma.idCutBased = keepThisPhoton
77  # we're keeing sigmaietaieta sidebands, but the id is false for them:
78 
79  if abs(gamma.eta())< 1.479 and gamma.sigmaIetaIeta()>0.010 :
80  gamma.idCutBased = False
81  if abs(gamma.eta())>=1.479 and gamma.sigmaIetaIeta()>0.0321 :
82  gamma.idCutBased = False
83  else:
84  keepThisPhoton = gamma.photonID(self.cfg_ana.gammaID)
85 
86  if gamma.hasPixelSeed():
87  keepThisPhoton = False
88  gamma.idCutBased = 0
89 
90 
91  if keepThisPhoton:
92  event.selectedPhotons.append(gamma)
93 
94  if keepThisPhoton and abs(gamma.eta()) < self.etaCentral:
95  event.selectedPhotonsCentral.append(gamma)
96 
97  event.selectedPhotons.sort(key = lambda l : l.pt(), reverse = True)
98  event.selectedPhotonsCentral.sort(key = lambda l : l.pt(), reverse = True)
99 
100  self.counters.counter('events').inc('all events')
101  if foundPhoton: self.counters.counter('events').inc('has >=1 gamma at preselection')
102  if len(event.selectedPhotons): self.counters.counter('events').inc('has >=1 selected gamma')
103 
104  def matchPhotons(self, event):
105  event.genPhotons = [ x for x in event.genParticles if x.status() == 1 and abs(x.pdgId()) == 22 ]
106  event.genPhotonsWithMom = [ x for x in event.genPhotons if x.numberOfMothers()>0 ]
107  event.genPhotonsWithoutMom = [ x for x in event.genPhotons if x.numberOfMothers()==0 ]
108  event.genPhotonsMatched = [ x for x in event.genPhotonsWithMom if abs(x.mother(0).pdgId())<23 ]
109  match = matchObjectCollection3(event.allphotons, event.genPhotonsMatched, deltaRMax = 0.1)
110  matchNoMom = matchObjectCollection3(event.allphotons, event.genPhotonsWithoutMom, deltaRMax = 0.1)
111  packedGenParts = [ p for p in self.mchandles['packedGen'].product() if abs(p.eta()) < 3.1 ]
112  for gamma in event.allphotons:
113  gen = match[gamma]
114  if gen and gen.pt()>=0.5*gamma.pt() and gen.pt()<=2.*gamma.pt():
115  gamma.mcMatchId = 22
116  sumPt = 0.;
117  for part in packedGenParts:
118  if abs(part.pdgId())==12: continue # exclude neutrinos
119  if abs(part.pdgId())==14: continue
120  if abs(part.pdgId())==16: continue
121  if abs(part.pdgId())==18: continue
122  if deltaR(gen.eta(), gen.phi(), part.eta(), part.phi()) > 0.4: continue
123  sumPt += part.pt()
124  sumPt -= gen.pt()
125  if sumPt<0. : sumPt=0.
126  gamma.genIso = sumPt
127  else:
128  genNoMom = matchNoMom[gamma]
129  if genNoMom:
130  gamma.mcMatchId = 7
131  sumPt = 0.;
132  for part in packedGenParts:
133  if abs(part.pdgId())==12: continue # exclude neutrinos
134  if abs(part.pdgId())==14: continue
135  if abs(part.pdgId())==16: continue
136  if abs(part.pdgId())==18: continue
137  if deltaR(genNoMom.eta(), genNoMom.phi(), part.eta(), part.phi()) > 0.4: continue
138  sumPt += part.pt()
139  sumPt -= genNoMom.pt()
140  if sumPt<0. : sumPt=0.
141  gamma.genIso = sumPt
142  else:
143  gamma.mcMatchId = 0
144  gamma.genIso = -1.
145 
146  def printInfo(self, event):
147  print '----------------'
148  if len(event.selectedPhotons)>0:
149  print 'lenght: ',len(event.selectedPhotons)
150  print 'gamma candidate pt: ',event.selectedPhotons[0].pt()
151  print 'gamma candidate eta: ',event.selectedPhotons[0].eta()
152  print 'gamma candidate phi: ',event.selectedPhotons[0].phi()
153  print 'gamma candidate mass: ',event.selectedPhotons[0].mass()
154  print 'gamma candidate HoE: ',event.selectedPhotons[0].hOVERe()
155  print 'gamma candidate r9: ',event.selectedPhotons[0].r9()
156  print 'gamma candidate sigmaIetaIeta: ',event.selectedPhotons[0].sigmaIetaIeta()
157  print 'gamma candidate had iso: ',event.selectedPhotons[0].chargedHadronIso()
158  print 'gamma candidate neu iso: ',event.selectedPhotons[0].neutralHadronIso()
159  print 'gamma candidate gamma iso: ',event.selectedPhotons[0].photonIso()
160  print 'gamma idCutBased',event.selectedPhotons[0].idCutBased
161 
162 
163  def process(self, event):
164  self.readCollections( event.input )
165  self.makePhotons(event)
166 # self.printInfo(event)
167 
168  if not self.cfg_comp.isMC:
169  return True
170 
171  if self.cfg_ana.do_mc_match and hasattr(event, 'genParticles'):
172  self.matchPhotons(event)
173 
174  return True
175 
176 
177 setattr(PhotonAnalyzer,"defaultConfig",cfg.Analyzer(
178  class_object=PhotonAnalyzer,
179  photons='slimmedPhotons',
180  ptMin = 20,
181  etaMax = 2.5,
182  gammaID = "PhotonCutBasedIDLoose",
183  do_mc_match = True,
184  )
185 )
T eta() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
def matchObjectCollection3
Definition: deltar.py:38
Definition: DDAxes.h:10