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
15 from PhysicsTools.Heppy.physicsutils.PhotonCalibrator
import Run2PhotonCalibrator
19 import PhysicsTools.HeppyCore.framework.config
as cfg
25 def __init__(self, cfg_ana, cfg_comp, looperName ):
26 super(PhotonAnalyzer,self).
__init__(cfg_ana,cfg_comp,looperName)
27 self.
etaCentral = self.cfg_ana.etaCentral
if hasattr(self.cfg_ana,
'etaCentral')
else 9999
34 if self.cfg_ana.doPhotonScaleCorrections:
35 conf = cfg_ana.doPhotonScaleCorrections
39 conf[
'isSync']
if 'isSync' in conf
else False,
44 self.handles[
'rhoPhoton'] = AutoHandle( self.cfg_ana.rhoPhoton,
'double')
50 self.handles[
'photons'] = AutoHandle( self.cfg_ana.photons,
'std::vector<pat::Photon>')
51 self.mchandles[
'packedGen'] = AutoHandle(
'packedGenParticles',
'std::vector<pat::PackedGenParticle>' )
52 self.mchandles[
'prunedGen'] = AutoHandle(
'prunedGenParticles',
'std::vector<reco::GenParticle>' )
54 self.handles[
'packedCandidates'] = AutoHandle(
'packedPFCandidates',
'std::vector<pat::PackedCandidate>')
55 self.handles[
'jets'] = AutoHandle(
"slimmedJets",
'std::vector<pat::Jet>' )
59 super(PhotonAnalyzer,self).
beginLoop(setup)
60 self.counters.addCounter(
'events')
61 count = self.counters.counter(
'events')
62 count.register(
'all events')
63 count.register(
'has >=1 gamma at preselection')
64 count.register(
'has >=1 selected gamma')
67 event.allphotons =
map( Photon, self.handles[
'photons'].product() )
68 event.allphotons.sort(key =
lambda l : l.pt(), reverse =
True)
70 event.selectedPhotons = []
71 event.selectedPhotonsCentral = []
75 self.IsolationComputer.setPackedCandidates(self.handles[
'packedCandidates'].product(), -1, 0.1, 0.2)
78 if self.cfg_ana.doPhotonScaleCorrections:
79 for gamma
in event.allphotons:
80 self.photonEnergyCalibrator.correct(gamma, event.run)
83 for gamma
in event.allphotons:
84 if gamma.pt() < self.cfg_ana.ptMin:
continue 85 if abs(gamma.eta()) > self.cfg_ana.etaMax:
continue 88 gamma.rho =
float(self.handles[
'rhoPhoton'].product()[0])
90 if abs(gamma.eta()) < 1.0: gamma.EffectiveArea03 = [ 0.0234, 0.0053, 0.078 ]
91 elif abs(gamma.eta()) < 1.479: gamma.EffectiveArea03 = [ 0.0189, 0.0103, 0.0629 ]
92 elif abs(gamma.eta()) < 2.0: gamma.EffectiveArea03 = [ 0.0171, 0.0057, 0.0264 ]
93 elif abs(gamma.eta()) < 2.2: gamma.EffectiveArea03 = [ 0.0129, 0.0070, 0.0462 ]
94 elif abs(gamma.eta()) < 2.3: gamma.EffectiveArea03 = [ 0.0110, 0.0152, 0.0740 ]
95 elif abs(gamma.eta()) < 2.4: gamma.EffectiveArea03 = [ 0.0074, 0.0232, 0.0924 ]
96 else: gamma.EffectiveArea03 = [ 0.0035, 0.1709, 0.1484 ]
101 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()
104 """Create an integer equal to 1-2-3 for (loose,medium,tight)""" 107 if gamma.photonID(X%
"Loose"):
111 if gamma.photonID(X%
"Tight"):
115 gamma.idCutBased = idWP(gamma,
"PhotonCutBasedID%s")
118 keepThisPhoton =
True 120 if self.cfg_ana.gammaID==
"PhotonCutBasedIDLoose_CSA14" or self.cfg_ana.gammaID==
"PhotonCutBasedIDLoose_PHYS14" :
121 gamma.idCutBased = gamma.photonIDCSA14(self.cfg_ana.gammaID)
123 keepThisPhoton = gamma.photonIDCSA14(self.cfg_ana.gammaID,
True)
125 if gamma.hasPixelSeed():
126 keepThisPhoton =
False 128 elif "NoIso" in self.cfg_ana.gammaID:
129 idName = re.split(
'_NoIso',self.cfg_ana.gammaID)
130 keepThisPhoton = gamma.passPhotonID(idName[0],self.cfg_ana.conversionSafe_eleVeto)
131 basenameID = re.split(
'_looseSieie',idName[0])
132 gamma.idCutBased = gamma.passPhotonID(basenameID[0],self.cfg_ana.conversionSafe_eleVeto)
138 keepThisPhoton = gamma.passPhotonID(self.cfg_ana.gammaID,self.cfg_ana.conversionSafe_eleVeto)
and gamma.passPhotonIso(self.cfg_ana.gammaID,self.cfg_ana.gamma_isoCorr)
141 event.selectedPhotons.append(gamma)
144 event.selectedPhotonsCentral.append(gamma)
146 event.selectedPhotons.sort(key =
lambda l : l.pt(), reverse =
True)
147 event.selectedPhotonsCentral.sort(key =
lambda l : l.pt(), reverse =
True)
149 self.counters.counter(
'events').inc(
'all events')
150 if foundPhoton: self.counters.counter(
'events').inc(
'has >=1 gamma at preselection')
151 if len(event.selectedPhotons): self.counters.counter(
'events').inc(
'has >=1 selected gamma')
154 event.genPhotons = [ x
for x
in event.genParticles
if x.status() == 1
and abs(x.pdgId()) == 22 ]
155 event.genPhotonsWithMom = [ x
for x
in event.genPhotons
if x.numberOfMothers()>0 ]
156 event.genPhotonsWithoutMom = [ x
for x
in event.genPhotons
if x.numberOfMothers()==0 ]
157 event.genPhotonsMatched = [ x
for x
in event.genPhotonsWithMom
if abs(x.mother(0).
pdgId())<23
or x.mother(0).
pdgId()==2212 ]
160 packedGenParts = [ p
for p
in self.mchandles[
'packedGen'].product()
if abs(p.eta()) < 3.1 ]
161 partons = [ p
for p
in self.mchandles[
'prunedGen'].product()
if (p.status()==23
or p.status()==22)
and abs(p.pdgId())<22 ]
162 for gamma
in event.allphotons:
165 if gen
and gen.pt()>=0.5*gamma.pt()
and gen.pt()<=2.*gamma.pt():
169 for part
in packedGenParts:
170 if abs(part.pdgId())==12:
continue 171 if abs(part.pdgId())==14:
continue 172 if abs(part.pdgId())==16:
continue 173 if abs(part.pdgId())==18:
continue 174 deltar =
deltaR(gen.eta(), gen.phi(), part.eta(), part.phi())
181 if sumPt03<0. : sumPt03=0.
182 if sumPt04<0. : sumPt04=0.
183 gamma.genIso03 = sumPt03
184 gamma.genIso04 = sumPt04
188 deltar =
deltaR(gen.eta(), gen.phi(), p.eta(), p.phi())
189 if deltar < deltaRmin:
191 gamma.drMinParton = deltaRmin
193 genNoMom = matchNoMom[gamma]
198 for part
in packedGenParts:
199 if abs(part.pdgId())==12:
continue 200 if abs(part.pdgId())==14:
continue 201 if abs(part.pdgId())==16:
continue 202 if abs(part.pdgId())==18:
continue 203 deltar =
deltaR(genNoMom.eta(), genNoMom.phi(), part.eta(), part.phi())
208 sumPt03 -= genNoMom.pt()
209 sumPt04 -= genNoMom.pt()
210 if sumPt03<0. : sumPt03=0.
211 if sumPt04<0. : sumPt04=0.
212 gamma.genIso03 = sumPt03
213 gamma.genIso04 = sumPt04
217 deltar =
deltaR(genNoMom.eta(), genNoMom.phi(), p.eta(), p.phi())
218 if deltar < deltaRmin:
220 gamma.drMinParton = deltaRmin
225 gamma.drMinParton = -1.
233 for part
in particles:
234 if deltaR(eta, phi, part.eta(), part.phi()) < deltar:
245 if self.
checkMatch( eta, phi, jets, 2.*deltarmax ):
248 if self.
checkMatch( eta, phi, photons, 2.*deltarmax ):
251 if self.
checkMatch( eta, phi, event.selectedLeptons, deltarmax ):
257 if deltaR(eta, phi, part.eta(), part.phi()) > deltarmax :
continue 270 patcands = self.handles[
'packedCandidates'].product()
271 jets = self.handles[
'jets'].product()
273 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() ) ]
274 photons10 = [ p
for p
in patcands
if ( p.pdgId() == 22
and p.pt()>10. ) ]
275 jets20 = [ j
for j
in jets
if j.pt() > 20
and abs(j.eta())<2.5 ]
277 for gamma
in event.allphotons:
279 etaPhot = gamma.eta()
280 phiPhot = gamma.eta()
282 phiRC = phiPhot + 0.5*pi
287 gamma.chHadIsoRC03 = self.
computeRandomCone( event, etaPhot, phiRC, 0.3, charged, jets20, photons10 )
288 gamma.chHadIsoRC04 = self.
computeRandomCone( event, etaPhot, phiRC, 0.4, charged, jets20, photons10 )
292 phiRC = phiPhot - 0.5*pi
296 if gamma.chHadIsoRC03<0. : gamma.chHadIsoRC03 = self.
computeRandomCone( event, etaPhot, phiRC, 0.3, charged, jets20, photons10 )
297 if gamma.chHadIsoRC04<0. : gamma.chHadIsoRC04 = self.
computeRandomCone( event, etaPhot, phiRC, 0.4, charged, jets20, photons10 )
302 gamma.ftprAbsIsoCharged03 = self.IsolationComputer.chargedAbsIso(gamma.physObj, 0.3, 0, 0.0);
303 gamma.ftprAbsIsoPho03 = self.IsolationComputer.photonAbsIsoRaw( gamma.physObj, 0.3, 0, 0.0);
304 gamma.ftprAbsIsoNHad03 = self.IsolationComputer.neutralHadAbsIsoRaw( gamma.physObj, 0.3, 0, 0.0);
305 gamma.ftprAbsIso03 = gamma.ftprAbsIsoCharged03 + gamma.ftprAbsIsoPho03 + gamma.ftprAbsIsoNHad03
306 if self.cfg_ana.gamma_isoCorr ==
"rhoArea":
307 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))
308 elif self.cfg_ana.gamma_isoCorr !=
'raw':
309 raise RuntimeError(
"Unsupported gamma_isoCorr name '" +
str(self.cfg_ana.gamma_isoCorr) +
"'! For now only 'rhoArea', 'raw' are supported.")
310 gamma.ftprRelIso03 = gamma.ftprAbsIso03/gamma.pt()
313 print '----------------' 314 if len(event.selectedPhotons)>0:
315 print 'lenght: ',len(event.selectedPhotons)
316 print 'gamma candidate pt: ',event.selectedPhotons[0].
pt()
317 print 'gamma candidate eta: ',event.selectedPhotons[0].
eta()
318 print 'gamma candidate phi: ',event.selectedPhotons[0].phi()
319 print 'gamma candidate mass: ',event.selectedPhotons[0].
mass()
320 print 'gamma candidate HoE: ',event.selectedPhotons[0].hOVERe()
321 print 'gamma candidate r9: ',event.selectedPhotons[0].full5x5_r9()
323 print 'gamma candidate had iso: ',event.selectedPhotons[0].chargedHadronIso()
324 print 'gamma candidate neu iso: ',event.selectedPhotons[0].neutralHadronIso()
325 print 'gamma candidate gamma iso: ',event.selectedPhotons[0].photonIso()
326 print 'gamma idCutBased',event.selectedPhotons[0].idCutBased
330 self.readCollections( event.input )
334 if self.cfg_ana.do_randomCone:
337 if not self.cfg_comp.isMC:
340 if self.cfg_ana.do_mc_match
and hasattr(event,
'genParticles'):
347 setattr(PhotonAnalyzer,
"defaultConfig",cfg.Analyzer(
348 class_object=PhotonAnalyzer,
349 photons=
'slimmedPhotons',
353 doPhotonScaleCorrections=
False,
354 gammaID =
"PhotonCutBasedIDLoose_CSA14",
355 rhoPhoton =
'fixedGridRhoFastjetAll',
356 gamma_isoCorr =
'rhoArea',
358 doFootprintRemovedIsolation =
False,
359 packedCandidates =
'packedPFCandidates',
360 footprintRemovedIsolationPUCorr =
'rhoArea',
361 conversionSafe_eleVeto =
False,
363 do_randomCone =
False,
def matchPhotons(self, event)
def attachFootprintRemovedIsolation(self, gamma)
doFootprintRemovedIsolation
def printInfo(self, event)
def randomCone(self, event)
def makePhotons(self, event)
Abs< T >::type abs(const T &t)
def __init__(self, cfg_ana, cfg_comp, looperName)
double deltaR(double eta1, double eta2, double phi1, double phi2)
def computeRandomCone(self, event, eta, phi, deltarmax, charged, jets, photons)
def checkMatch(self, eta, phi, particles, deltar)
footprintRemovedIsolationPUCorr
def beginLoop(self, setup)
def matchObjectCollection3