6 from ROOT
import TLorentzVector
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
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
33 self.handles[
'photons'] = AutoHandle( self.cfg_ana.photons,
'std::vector<pat::Photon>')
34 self.mchandles[
'packedGen'] = AutoHandle(
'packedGenParticles',
'std::vector<pat::PackedGenParticle>' )
35 self.handles[
'packedCandidates'] = AutoHandle(
'packedPFCandidates',
'std::vector<pat::PackedCandidate>')
36 self.handles[
'jets'] = AutoHandle(
"slimmedJets",
'std::vector<pat::Jet>' )
40 super(PhotonAnalyzer,self).
beginLoop(setup)
41 self.counters.addCounter(
'events')
42 count = self.counters.counter(
'events')
43 count.register(
'all events')
44 count.register(
'has >=1 gamma at preselection')
45 count.register(
'has >=1 selected gamma')
48 event.allphotons =
map( Photon, self.handles[
'photons'].product() )
49 event.allphotons.sort(key =
lambda l : l.pt(), reverse =
True)
51 event.selectedPhotons = []
52 event.selectedPhotonsCentral = []
55 for gamma
in event.allphotons:
56 if gamma.pt() < self.cfg_ana.ptMin:
continue
57 if abs(gamma.eta()) > self.cfg_ana.etaMax:
continue
61 """Create an integer equal to 1-2-3 for (loose,medium,tight)"""
64 if gamma.photonID(X%
"Loose"):
68 if gamma.photonID(X%
"Tight"):
72 gamma.idCutBased = idWP(gamma,
"PhotonCutBasedID%s")
76 if self.cfg_ana.gammaID==
"PhotonCutBasedIDLoose_CSA14" :
77 keepThisPhoton = gamma.photonIDCSA14(
"PhotonCutBasedIDLoose_CSA14")
78 gamma.idCutBased = keepThisPhoton
81 if abs(gamma.eta())< 1.479
and gamma.full5x5_sigmaIetaIeta()>0.010 :
82 gamma.idCutBased =
False
83 if abs(gamma.eta())>=1.479
and gamma.full5x5_sigmaIetaIeta()>0.0321 :
84 gamma.idCutBased =
False
85 if gamma.hasPixelSeed():
86 keepThisPhoton =
False
93 keepThisPhoton = gamma.passPhotonID(self.cfg_ana.gammaID)
96 event.selectedPhotons.append(gamma)
99 event.selectedPhotonsCentral.append(gamma)
101 event.selectedPhotons.sort(key =
lambda l : l.pt(), reverse =
True)
102 event.selectedPhotonsCentral.sort(key =
lambda l : l.pt(), reverse =
True)
104 self.counters.counter(
'events').inc(
'all events')
105 if foundPhoton: self.counters.counter(
'events').inc(
'has >=1 gamma at preselection')
106 if len(event.selectedPhotons): self.counters.counter(
'events').inc(
'has >=1 selected gamma')
109 event.genPhotons = [ x
for x
in event.genParticles
if x.status() == 1
and abs(x.pdgId()) == 22 ]
110 event.genPhotonsWithMom = [ x
for x
in event.genPhotons
if x.numberOfMothers()>0 ]
111 event.genPhotonsWithoutMom = [ x
for x
in event.genPhotons
if x.numberOfMothers()==0 ]
112 event.genPhotonsMatched = [ x
for x
in event.genPhotonsWithMom
if abs(x.mother(0).
pdgId())<23
or x.mother(0).
pdgId()==2212 ]
115 packedGenParts = [ p
for p
in self.mchandles[
'packedGen'].product()
if abs(p.eta()) < 3.1 ]
116 for gamma
in event.allphotons:
119 if gen
and gen.pt()>=0.5*gamma.pt()
and gen.pt()<=2.*gamma.pt():
123 for part
in packedGenParts:
124 if abs(part.pdgId())==12:
continue
125 if abs(part.pdgId())==14:
continue
126 if abs(part.pdgId())==16:
continue
127 if abs(part.pdgId())==18:
continue
128 deltar =
deltaR(gen.eta(), gen.phi(), part.eta(), part.phi())
135 if sumPt03<0. : sumPt03=0.
136 if sumPt04<0. : sumPt04=0.
137 gamma.genIso03 = sumPt03
138 gamma.genIso04 = sumPt04
140 genNoMom = matchNoMom[gamma]
145 for part
in packedGenParts:
146 if abs(part.pdgId())==12:
continue
147 if abs(part.pdgId())==14:
continue
148 if abs(part.pdgId())==16:
continue
149 if abs(part.pdgId())==18:
continue
150 deltar =
deltaR(genNoMom.eta(), genNoMom.phi(), part.eta(), part.phi());
155 sumPt03 -= genNoMom.pt()
156 sumPt04 -= genNoMom.pt()
157 if sumPt03<0. : sumPt03=0.
158 if sumPt04<0. : sumPt04=0.
159 gamma.genIso03 = sumPt03
160 gamma.genIso04 = sumPt04
172 for part
in particles:
173 if deltaR(eta, phi, part.eta(), part.phi()) < deltar:
184 if self.
checkMatch( eta, phi, jets, 2.*deltarmax ):
187 if self.
checkMatch( eta, phi, photons, 2.*deltarmax ):
190 if self.
checkMatch( eta, phi, event.selectedLeptons, deltarmax ):
196 if deltaR(eta, phi, part.eta(), part.phi()) > deltarmax :
continue
209 patcands = self.handles[
'packedCandidates'].product()
210 jets = self.handles[
'jets'].product()
212 charged = [ p
for p
in patcands
if ( p.charge() != 0
and abs(p.pdgId())>20
and abs(p.dz())<=0.1
and p.fromPV()>1
and p.trackHighPurity() ) ]
213 photons10 = [ p
for p
in patcands
if ( p.pdgId() == 22
and p.pt()>10. ) ]
214 jets20 = [ j
for j
in jets
if j.pt() > 20
and abs(j.eta())<2.5 ]
216 for gamma
in event.allphotons:
218 etaPhot = gamma.eta()
219 phiPhot = gamma.eta()
221 phiRC = phiPhot + 0.5*pi
226 gamma.chHadIsoRC03 = self.
computeRandomCone( event, etaPhot, phiRC, 0.3, charged, jets20, photons10 )
227 gamma.chHadIsoRC04 = self.
computeRandomCone( event, etaPhot, phiRC, 0.4, charged, jets20, photons10 )
231 phiRC = phiPhot - 0.5*pi
235 if gamma.chHadIsoRC03<0. : gamma.chHadIsoRC03 = self.
computeRandomCone( event, etaPhot, phiRC, 0.3, charged, jets20, photons10 )
236 if gamma.chHadIsoRC04<0. : gamma.chHadIsoRC04 = self.
computeRandomCone( event, etaPhot, phiRC, 0.4, charged, jets20, photons10 )
242 print '----------------'
243 if len(event.selectedPhotons)>0:
244 print 'lenght: ',len(event.selectedPhotons)
245 print 'gamma candidate pt: ',event.selectedPhotons[0].
pt()
246 print 'gamma candidate eta: ',event.selectedPhotons[0].
eta()
247 print 'gamma candidate phi: ',event.selectedPhotons[0].phi()
248 print 'gamma candidate mass: ',event.selectedPhotons[0].mass()
249 print 'gamma candidate HoE: ',event.selectedPhotons[0].hOVERe()
250 print 'gamma candidate r9: ',event.selectedPhotons[0].full5x5_r9()
251 print 'gamma candidate sigmaIetaIeta: ',event.selectedPhotons[0].full5x5_sigmaIetaIeta()
252 print 'gamma candidate had iso: ',event.selectedPhotons[0].chargedHadronIso()
253 print 'gamma candidate neu iso: ',event.selectedPhotons[0].neutralHadronIso()
254 print 'gamma candidate gamma iso: ',event.selectedPhotons[0].photonIso()
255 print 'gamma idCutBased',event.selectedPhotons[0].idCutBased
259 self.readCollections( event.input )
263 if self.cfg_ana.do_randomCone:
266 if not self.cfg_comp.isMC:
269 if self.cfg_ana.do_mc_match
and hasattr(event,
'genParticles'):
276 setattr(PhotonAnalyzer,
"defaultConfig",cfg.Analyzer(
277 class_object=PhotonAnalyzer,
278 photons=
'slimmedPhotons',
281 gammaID =
"PhotonCutBasedIDLoose_CSA14",
283 do_randomCone =
False,
Abs< T >::type abs(const T &t)
double deltaR(double eta1, double eta2, double phi1, double phi2)
def matchObjectCollection3