7 from ROOT
import TLorentzVector
10 from PhysicsTools.Heppy.analyzers.core.Analyzer
import Analyzer
11 from PhysicsTools.HeppyCore.framework.event
import Event
12 from PhysicsTools.HeppyCore.statistics.counter
import Counter, Counters
13 from PhysicsTools.Heppy.analyzers.core.AutoHandle
import AutoHandle
14 from PhysicsTools.Heppy.physicsobjects.Photon
import Photon
18 import PhysicsTools.HeppyCore.framework.config
as cfg
24 def __init__(self, cfg_ana, cfg_comp, looperName ):
25 super(PhotonAnalyzer,self).
__init__(cfg_ana,cfg_comp,looperName)
26 self.
etaCentral = self.cfg_ana.etaCentral
if hasattr(self.cfg_ana,
'etaCentral')
else 9999
35 self.handles[
'rhoPhoton'] = AutoHandle( self.cfg_ana.rhoPhoton,
'double')
41 self.handles[
'photons'] = AutoHandle( self.cfg_ana.photons,
'std::vector<pat::Photon>')
42 self.mchandles[
'packedGen'] = AutoHandle(
'packedGenParticles',
'std::vector<pat::PackedGenParticle>' )
43 self.mchandles[
'prunedGen'] = AutoHandle(
'prunedGenParticles',
'std::vector<reco::GenParticle>' )
45 self.handles[
'packedCandidates'] = AutoHandle(
'packedPFCandidates',
'std::vector<pat::PackedCandidate>')
46 self.handles[
'jets'] = AutoHandle(
"slimmedJets",
'std::vector<pat::Jet>' )
50 super(PhotonAnalyzer,self).
beginLoop(setup)
51 self.counters.addCounter(
'events')
52 count = self.counters.counter(
'events')
53 count.register(
'all events')
54 count.register(
'has >=1 gamma at preselection')
55 count.register(
'has >=1 selected gamma')
58 event.allphotons =
map( Photon, self.handles[
'photons'].product() )
59 event.allphotons.sort(key =
lambda l : l.pt(), reverse =
True)
61 event.selectedPhotons = []
62 event.selectedPhotonsCentral = []
66 self.IsolationComputer.setPackedCandidates(self.handles[
'packedCandidates'].product(), -1, 0.1, 0.2)
69 for gamma
in event.allphotons:
70 if gamma.pt() < self.cfg_ana.ptMin:
continue
71 if abs(gamma.eta()) > self.cfg_ana.etaMax:
continue
74 gamma.rho = float(self.handles[
'rhoPhoton'].product()[0])
76 if abs(gamma.eta()) < 1.0: gamma.EffectiveArea03 = [ 0.0234, 0.0053, 0.078 ]
77 elif abs(gamma.eta()) < 1.479: gamma.EffectiveArea03 = [ 0.0189, 0.0103, 0.0629 ]
78 elif abs(gamma.eta()) < 2.0: gamma.EffectiveArea03 = [ 0.0171, 0.0057, 0.0264 ]
79 elif abs(gamma.eta()) < 2.2: gamma.EffectiveArea03 = [ 0.0129, 0.0070, 0.0462 ]
80 elif abs(gamma.eta()) < 2.3: gamma.EffectiveArea03 = [ 0.0110, 0.0152, 0.0740 ]
81 elif abs(gamma.eta()) < 2.4: gamma.EffectiveArea03 = [ 0.0074, 0.0232, 0.0924 ]
82 else: gamma.EffectiveArea03 = [ 0.0035, 0.1709, 0.1484 ]
87 gamma.relIso = (
max(gamma.chargedHadronIso()-gamma.rho*gamma.EffectiveArea03[0],0) +
max(gamma.neutralHadronIso()-gamma.rho*gamma.EffectiveArea03[1],0) +
max(gamma.photonIso() - gamma.rho*gamma.EffectiveArea03[2],0))/gamma.pt()
90 """Create an integer equal to 1-2-3 for (loose,medium,tight)"""
93 if gamma.photonID(X%
"Loose"):
97 if gamma.photonID(X%
"Tight"):
101 gamma.idCutBased = idWP(gamma,
"PhotonCutBasedID%s")
104 keepThisPhoton =
True
106 if self.cfg_ana.gammaID==
"PhotonCutBasedIDLoose_CSA14" or self.cfg_ana.gammaID==
"PhotonCutBasedIDLoose_PHYS14" :
107 gamma.idCutBased = gamma.photonIDCSA14(self.cfg_ana.gammaID)
109 keepThisPhoton = gamma.photonIDCSA14(self.cfg_ana.gammaID,
True)
111 if gamma.hasPixelSeed():
112 keepThisPhoton =
False
114 elif "NoIso" in self.cfg_ana.gammaID:
115 idName = re.split(
'_NoIso',self.cfg_ana.gammaID)
116 keepThisPhoton = gamma.passPhotonID(idName[0],self.cfg_ana.conversionSafe_eleVeto)
117 basenameID = re.split(
'_looseSieie',idName[0])
118 gamma.idCutBased = gamma.passPhotonID(basenameID[0],self.cfg_ana.conversionSafe_eleVeto)
124 keepThisPhoton = gamma.passPhotonID(self.cfg_ana.gammaID,self.cfg_ana.conversionSafe_eleVeto)
and gamma.passPhotonIso(self.cfg_ana.gammaID,self.cfg_ana.gamma_isoCorr)
127 event.selectedPhotons.append(gamma)
130 event.selectedPhotonsCentral.append(gamma)
132 event.selectedPhotons.sort(key =
lambda l : l.pt(), reverse =
True)
133 event.selectedPhotonsCentral.sort(key =
lambda l : l.pt(), reverse =
True)
135 self.counters.counter(
'events').inc(
'all events')
136 if foundPhoton: self.counters.counter(
'events').inc(
'has >=1 gamma at preselection')
137 if len(event.selectedPhotons): self.counters.counter(
'events').inc(
'has >=1 selected gamma')
140 event.genPhotons = [ x
for x
in event.genParticles
if x.status() == 1
and abs(x.pdgId()) == 22 ]
141 event.genPhotonsWithMom = [ x
for x
in event.genPhotons
if x.numberOfMothers()>0 ]
142 event.genPhotonsWithoutMom = [ x
for x
in event.genPhotons
if x.numberOfMothers()==0 ]
143 event.genPhotonsMatched = [ x
for x
in event.genPhotonsWithMom
if abs(x.mother(0).
pdgId())<23
or x.mother(0).
pdgId()==2212 ]
146 packedGenParts = [ p
for p
in self.mchandles[
'packedGen'].product()
if abs(p.eta()) < 3.1 ]
147 partons = [ p
for p
in self.mchandles[
'prunedGen'].product()
if (p.status()==23
or p.status()==22)
and abs(p.pdgId())<22 ]
148 for gamma
in event.allphotons:
151 if gen
and gen.pt()>=0.5*gamma.pt()
and gen.pt()<=2.*gamma.pt():
155 for part
in packedGenParts:
156 if abs(part.pdgId())==12:
continue
157 if abs(part.pdgId())==14:
continue
158 if abs(part.pdgId())==16:
continue
159 if abs(part.pdgId())==18:
continue
160 deltar =
deltaR(gen.eta(), gen.phi(), part.eta(), part.phi())
167 if sumPt03<0. : sumPt03=0.
168 if sumPt04<0. : sumPt04=0.
169 gamma.genIso03 = sumPt03
170 gamma.genIso04 = sumPt04
174 deltar =
deltaR(gen.eta(), gen.phi(), p.eta(), p.phi())
175 if deltar < deltaRmin:
177 gamma.drMinParton = deltaRmin
179 genNoMom = matchNoMom[gamma]
184 for part
in packedGenParts:
185 if abs(part.pdgId())==12:
continue
186 if abs(part.pdgId())==14:
continue
187 if abs(part.pdgId())==16:
continue
188 if abs(part.pdgId())==18:
continue
189 deltar =
deltaR(genNoMom.eta(), genNoMom.phi(), part.eta(), part.phi())
194 sumPt03 -= genNoMom.pt()
195 sumPt04 -= genNoMom.pt()
196 if sumPt03<0. : sumPt03=0.
197 if sumPt04<0. : sumPt04=0.
198 gamma.genIso03 = sumPt03
199 gamma.genIso04 = sumPt04
203 deltar =
deltaR(genNoMom.eta(), genNoMom.phi(), p.eta(), p.phi())
204 if deltar < deltaRmin:
206 gamma.drMinParton = deltaRmin
211 gamma.drMinParton = -1.
219 for part
in particles:
220 if deltaR(eta, phi, part.eta(), part.phi()) < deltar:
231 if self.
checkMatch( eta, phi, jets, 2.*deltarmax ):
234 if self.
checkMatch( eta, phi, photons, 2.*deltarmax ):
237 if self.
checkMatch( eta, phi, event.selectedLeptons, deltarmax ):
243 if deltaR(eta, phi, part.eta(), part.phi()) > deltarmax :
continue
256 patcands = self.handles[
'packedCandidates'].product()
257 jets = self.handles[
'jets'].product()
259 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() ) ]
260 photons10 = [ p
for p
in patcands
if ( p.pdgId() == 22
and p.pt()>10. ) ]
261 jets20 = [ j
for j
in jets
if j.pt() > 20
and abs(j.eta())<2.5 ]
263 for gamma
in event.allphotons:
265 etaPhot = gamma.eta()
266 phiPhot = gamma.eta()
268 phiRC = phiPhot + 0.5*pi
273 gamma.chHadIsoRC03 = self.
computeRandomCone( event, etaPhot, phiRC, 0.3, charged, jets20, photons10 )
274 gamma.chHadIsoRC04 = self.
computeRandomCone( event, etaPhot, phiRC, 0.4, charged, jets20, photons10 )
278 phiRC = phiPhot - 0.5*pi
282 if gamma.chHadIsoRC03<0. : gamma.chHadIsoRC03 = self.
computeRandomCone( event, etaPhot, phiRC, 0.3, charged, jets20, photons10 )
283 if gamma.chHadIsoRC04<0. : gamma.chHadIsoRC04 = self.
computeRandomCone( event, etaPhot, phiRC, 0.4, charged, jets20, photons10 )
288 gamma.ftprAbsIsoCharged03 = self.IsolationComputer.chargedAbsIso(gamma.physObj, 0.3, 0, 0.0);
289 gamma.ftprAbsIsoPho03 = self.IsolationComputer.photonAbsIsoRaw( gamma.physObj, 0.3, 0, 0.0);
290 gamma.ftprAbsIsoNHad03 = self.IsolationComputer.neutralHadAbsIsoRaw( gamma.physObj, 0.3, 0, 0.0);
291 gamma.ftprAbsIso03 = gamma.ftprAbsIsoCharged03 + gamma.ftprAbsIsoPho03 + gamma.ftprAbsIsoNHad03
292 if self.cfg_ana.gamma_isoCorr ==
"rhoArea":
293 gamma.ftprAbsIso03 = (
max(gamma.ftprAbsIsoCharged03-gamma.rho*gamma.EffectiveArea03[0],0) +
max(gamma.ftprAbsIsoPho03-gamma.rho*gamma.EffectiveArea03[1],0) +
max(gamma.ftprAbsIsoNHad03 - gamma.rho*gamma.EffectiveArea03[2],0))
294 elif self.cfg_ana.gamma_isoCorr !=
'raw':
295 raise RuntimeError,
"Unsupported gamma_isoCorr name '" + str(self.cfg_ana.gamma_isoCorr) +
"'! For now only 'rhoArea', 'raw' are supported."
296 gamma.ftprRelIso03 = gamma.ftprAbsIso03/gamma.pt()
299 print '----------------'
300 if len(event.selectedPhotons)>0:
301 print 'lenght: ',len(event.selectedPhotons)
302 print 'gamma candidate pt: ',event.selectedPhotons[0].
pt()
303 print 'gamma candidate eta: ',event.selectedPhotons[0].
eta()
304 print 'gamma candidate phi: ',event.selectedPhotons[0].phi()
305 print 'gamma candidate mass: ',event.selectedPhotons[0].mass()
306 print 'gamma candidate HoE: ',event.selectedPhotons[0].hOVERe()
307 print 'gamma candidate r9: ',event.selectedPhotons[0].full5x5_r9()
308 print 'gamma candidate sigmaIetaIeta: ',event.selectedPhotons[0].full5x5_sigmaIetaIeta()
309 print 'gamma candidate had iso: ',event.selectedPhotons[0].chargedHadronIso()
310 print 'gamma candidate neu iso: ',event.selectedPhotons[0].neutralHadronIso()
311 print 'gamma candidate gamma iso: ',event.selectedPhotons[0].photonIso()
312 print 'gamma idCutBased',event.selectedPhotons[0].idCutBased
316 self.readCollections( event.input )
320 if self.cfg_ana.do_randomCone:
323 if not self.cfg_comp.isMC:
326 if self.cfg_ana.do_mc_match
and hasattr(event,
'genParticles'):
333 setattr(PhotonAnalyzer,
"defaultConfig",cfg.Analyzer(
334 class_object=PhotonAnalyzer,
335 photons=
'slimmedPhotons',
338 gammaID =
"PhotonCutBasedIDLoose_CSA14",
339 rhoPhoton =
'fixedGridRhoFastjetAll',
340 gamma_isoCorr =
'rhoArea',
342 doFootprintRemovedIsolation =
False,
343 packedCandidates =
'packedPFCandidates',
344 footprintRemovedIsolationPUCorr =
'rhoArea',
345 conversionSafe_eleVeto =
False,
347 do_randomCone =
False,
doFootprintRemovedIsolation
def attachFootprintRemovedIsolation
Abs< T >::type abs(const T &t)
double deltaR(double eta1, double eta2, double phi1, double phi2)
string footprintRemovedIsolationPUCorr
def matchObjectCollection3