2 from PhysicsTools.Heppy.analyzers.core.Analyzer
import Analyzer
3 from PhysicsTools.Heppy.analyzers.core.AutoHandle
import AutoHandle
4 from PhysicsTools.Heppy.physicsobjects.PhysicsObjects
import Jet
6 from PhysicsTools.Heppy.physicsutils.JetReCalibrator
import JetReCalibrator
9 from PhysicsTools.Heppy.physicsutils.QGLikelihoodCalculator
import QGLikelihoodCalculator
13 good = [
True for j
in jets ]
16 for i,j
in enumerate(jets):
17 d2i =
deltaR2(l.eta(),l.phi(), j.eta(),j.phi())
20 if ibest != -1: good[ibest] =
False
21 return [ j
for (i,j)
in enumerate(jets)
if good[i] ==
True ]
25 goodjet = [
True for j
in jets ]
26 goodlep = [
True for l
in leptons ]
27 for il, l
in enumerate(leptons):
29 for i,j
in enumerate(jets):
30 d2i =
deltaR2(l.eta(),l.phi(), j.eta(),j.phi())
32 if arbitration(j,l) == j:
39 if not goodlep[il]:
continue
40 if ibest != -1: goodjet[ibest] =
False
41 return ( [ j
for (i ,j)
in enumerate(jets)
if goodjet[i ] ==
True ],
42 [ l
for (il,l)
in enumerate(leptons)
if goodlep[il] ==
True ] )
46 """Taken from RootTools.JetAnalyzer, simplified, modified, added corrections """
47 def __init__(self, cfg_ana, cfg_comp, looperName):
48 super(JetAnalyzer,self).
__init__(cfg_ana, cfg_comp, looperName)
49 mcGT = cfg_ana.mcGT
if hasattr(cfg_ana,
'mcGT')
else "PHYS14_25_V2"
50 dataGT = cfg_ana.dataGT
if hasattr(cfg_ana,
'dataGT')
else "GR_70_V2_AN1"
51 self.
shiftJEC = self.cfg_ana.shiftJEC
if hasattr(self.cfg_ana,
'shiftJEC')
else 0
55 elif self.
recalibrateJets not in [
True,
False]:
raise RuntimeError,
"recalibrateJets must be any of { True, False, 'MC', 'Data' }, while it is %r " % self.
recalibrateJets
58 if self.cfg_comp.isMC:
62 self.
doPuId = getattr(self.cfg_ana,
'doPuId',
True)
63 self.
jetLepDR = getattr(self.cfg_ana,
'jetLepDR', 0.4)
64 self.
jetLepArbitration = getattr(self.cfg_ana,
'jetLepArbitration',
lambda jet,lepton: lepton)
65 self.
lepPtMin = getattr(self.cfg_ana,
'minLepPt', -1)
66 self.
jetGammaDR = getattr(self.cfg_ana,
'jetGammaDR', 0.4)
67 if(self.cfg_ana.doQG):
72 self.handles[
'jets'] = AutoHandle( self.cfg_ana.jetCol,
'std::vector<pat::Jet>' )
73 self.handles[
'genJet'] = AutoHandle(
'slimmedGenJets',
'vector<reco::GenJet>' )
74 self.
shiftJER = self.cfg_ana.shiftJER
if hasattr(self.cfg_ana,
'shiftJER')
else 0
75 self.handles[
'rho'] = AutoHandle( (
'fixedGridRhoFastjetAll',
'',
''),
'double' )
81 self.readCollections( event.input )
82 rho = float(self.handles[
'rho'].product()[0])
86 allJets =
map(Jet, self.handles[
'jets'].product())
88 event.deltaMetFromJEC = [0.,0.]
91 self.jetReCalibrator.correctAll(allJets, rho, delta=self.
shiftJEC, metShift=event.deltaMetFromJEC)
92 event.allJetsUsedForMET = allJets
94 if self.cfg_comp.isMC:
95 event.genJets = [ x
for x
in self.handles[
'genJet'].product() ]
97 if getattr(self.cfg_ana,
'smearJets',
False):
101 allJets.sort(key =
lambda j : j.pt(), reverse =
True)
105 event.jetsFailId = []
106 event.jetsAllNoID = []
107 event.jetsIdOnly = []
110 event.jetsAllNoID.append(jet)
113 if(self.cfg_ana.doQG):
115 jet.qgl = self.qglcalc.computeQGLikelihood(jet, rho)
118 event.jets.append(jet)
119 event.jetsIdOnly.append(jet)
121 event.jetsFailId.append(jet)
123 event.jetsIdOnly.append(jet)
127 if hasattr(event,
'selectedLeptons'):
128 leptons = [ l
for l
in event.selectedLeptons
if l.pt() > self.
lepPtMin ]
129 if self.cfg_ana.cleanJetsFromTaus
and hasattr(event,
'selectedTaus'):
130 leptons = leptons[:] + event.selectedTaus
131 if self.cfg_ana.cleanJetsFromIsoTracks
and hasattr(event,
'selectedIsoCleanTrack'):
132 leptons = leptons[:] + event.selectedIsoCleanTrack
134 event.cleanJets = [j
for j
in event.cleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
135 event.cleanJetsFwd = [j
for j
in event.cleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
136 event.discardedJets = [j
for j
in event.jets
if j
not in event.cleanJetsAll]
137 if hasattr(event,
'selectedLeptons'):
138 event.discardedLeptons = [ l
for l
in leptons
if l
not in cleanLeptons ]
139 event.selectedLeptons = [ l
for l
in event.selectedLeptons
if l
not in event.discardedLeptons ]
143 if hasattr(event,
'selectedPhotons'):
144 if self.cfg_ana.cleanJetsFromFirstPhoton:
145 photons = event.selectedPhotons[:1]
147 photons = [ g
for g
in event.selectedPhotons ]
150 event.gamma_cleanJets = [j
for j
in event.gamma_cleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
151 event.gamma_cleanJetsFwd = [j
for j
in event.gamma_cleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
156 leptons = event.inclusiveLeptons
if hasattr(event,
'inclusiveLeptons')
else event.selectedLeptons
160 jet.leptons = [l
for l
in jlpairs
if jlpairs[l] == jet ]
169 taus = getattr(event,
'selectedTaus',[])
173 jet.taus = [l
for l
in jtaupairs
if jtaupairs[l] == jet ]
175 tau.jet = jtaupairs[tau]
178 if self.cfg_comp.isMC:
179 event.deltaMetFromJetSmearing = [0, 0]
180 for j
in event.cleanJetsAll:
181 if hasattr(j,
'deltaMetFromJetSmearing'):
182 event.deltaMetFromJetSmearing[0] += j.deltaMetFromJetSmearing[0]
183 event.deltaMetFromJetSmearing[1] += j.deltaMetFromJetSmearing[1]
202 jet.puJetIdPassed = jet.puJetId()
203 jet.pfJetIdPassed = jet.jetID(
'POG_PFID_Loose')
204 if self.cfg_ana.relaxJetId:
207 return jet.pfJetIdPassed
and (jet.puJetIdPassed
or not(self.
doPuId))
211 return jet.pt() > self.cfg_ana.jetPt
and \
212 abs( jet.eta() ) < self.cfg_ana.jetEta;
227 for ii
in range(0, jet.numberOfDaughters()) :
229 part = jet.daughter(ii)
231 if part.charge() == 0 :
233 if part.pt() < 1.:
continue
237 if part.trackHighPurity()==
False:
continue
238 if part.fromPV()<=1:
continue
243 deta = part.eta() - jet.eta()
244 dphi =
deltaPhi(part.phi(), jet.phi())
246 weight = partPt*partPt
249 sum_deta += deta*weight
250 sum_dphi += dphi*weight
251 sum_deta2 += deta*deta*weight
252 sum_detadphi += deta*dphi*weight
253 sum_dphi2 += dphi*dphi*weight
263 jet.ptd = math.sqrt(sum_weight)/sum_pt
264 ave_deta = sum_deta/sum_weight
265 ave_dphi = sum_dphi/sum_weight
266 ave_deta2 = sum_deta2/sum_weight
267 ave_dphi2 = sum_dphi2/sum_weight
268 a = ave_deta2 - ave_deta*ave_deta
269 b = ave_dphi2 - ave_dphi*ave_dphi
270 c = -(sum_detadphi/sum_weight - ave_deta*ave_dphi)
273 delta = math.sqrt(math.fabs((a-b)*(a-b)+4.*c*c))
275 if a+b-delta > 0: jet.axis2 = -math.log(math.sqrt(0.5*(a+b-delta)))
276 else: jet.axis2 = -1.
282 if id > 999:
return (id/1000)%10 == f
283 if id > 99:
return (id/100)%10 == f
288 event.bqObjects = [ p
for p
in event.genParticles
if (p.status() == 2
and isFlavour(p,5)) ]
289 event.cqObjects = [ p
for p
in event.genParticles
if (p.status() == 2
and isFlavour(p,4)) ]
291 event.partons = [ p
for p
in event.genParticles
if ((p.status() == 23
or p.status() == 3)
and abs(p.pdgId())>0
and (
abs(p.pdgId())
in [1,2,3,4,5,21]) ) ]
296 for jet
in event.cleanJetsAll:
298 jet.partonId = (parton.pdgId()
if parton !=
None else 0)
299 jet.partonMotherId = (parton.mother(0).
pdgId()
if parton !=
None and parton.numberOfMothers()>0
else 0)
301 for jet
in event.jets:
302 (bmatch, dr) =
bestMatch(jet, event.bqObjects)
306 (cmatch, dr) =
bestMatch(jet, event.cqObjects)
312 event.heaviestQCDFlavour = 5
if len(event.bqObjects)
else (4
if len(event.cqObjects)
else 1);
317 event.genbquarks + event.genwzquarks,
322 jet.mcMatchId = (gen.sourceId
if gen !=
None else 0)
323 jet.mcMatchFlav = (
abs(gen.pdgId())
if gen !=
None else 0)
329 jet.mcJet = match[jet]
338 genpt, jetpt, aeta = gen.pt(), jet.pt(),
abs(jet.eta())
340 factor = 1.052 + self.shiftJER*math.hypot(0.012,0.062);
341 if aeta > 2.3: factor = 1.288 + self.shiftJER*math.hypot(0.127,0.154)
342 elif aeta > 1.7: factor = 1.134 + self.shiftJER*math.hypot(0.035,0.066)
343 elif aeta > 1.1: factor = 1.096 + self.shiftJER*math.hypot(0.017,0.063)
344 elif aeta > 0.5: factor = 1.057 + self.shiftJER*math.hypot(0.012,0.056)
345 ptscale =
max(0.0, (jetpt + (factor-1)*(jetpt-genpt))/jetpt)
347 jet.deltaMetFromJetSmearing = [ -(ptscale-1)*jet.rawFactor()*jet.px(), -(ptscale-1)*jet.rawFactor()*jet.py() ]
349 jet.setP4(jet.p4()*ptscale)
351 jet._rawFactorMultiplier *= (1.0/ptscale)
if ptscale != 0
else 1
356 setattr(JetAnalyzer,
"defaultConfig", cfg.Analyzer(
357 class_object = JetAnalyzer,
358 jetCol =
'slimmedJets',
363 jetLepArbitration = (
lambda jet,lepton : lepton),
368 recalibrateJets =
False,
372 cleanJetsFromFirstPhoton =
False,
373 cleanJetsFromTaus =
False,
374 cleanJetsFromIsoTracks =
False,
def matchObjectCollection
def matchObjectCollection2
Abs< T >::type abs(const T &t)
double deltaR2(const T1 &t1, const T2 &t2)
if(conf.exists("allCellsPositionCalc"))