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 choice = arbitration(j,l)
37 elif choice == (j,l)
or choice == (l,j):
43 if not goodlep[il]:
continue
44 if ibest != -1: goodjet[ibest] =
False
45 return ( [ j
for (i ,j)
in enumerate(jets)
if goodjet[i ] ==
True ],
46 [ l
for (il,l)
in enumerate(leptons)
if goodlep[il] ==
True ] )
50 """Taken from RootTools.JetAnalyzer, simplified, modified, added corrections """
51 def __init__(self, cfg_ana, cfg_comp, looperName):
52 super(JetAnalyzer,self).
__init__(cfg_ana, cfg_comp, looperName)
53 mcGT = cfg_ana.mcGT
if hasattr(cfg_ana,
'mcGT')
else "PHYS14_25_V2"
54 dataGT = cfg_ana.dataGT
if hasattr(cfg_ana,
'dataGT')
else "GR_70_V2_AN1"
55 self.
shiftJEC = self.cfg_ana.shiftJEC
if hasattr(self.cfg_ana,
'shiftJEC')
else 0
59 elif self.
recalibrateJets not in [
True,
False]:
raise RuntimeError,
"recalibrateJets must be any of { True, False, 'MC', 'Data' }, while it is %r " % self.
recalibrateJets
62 if self.cfg_comp.isMC:
66 self.
doPuId = getattr(self.cfg_ana,
'doPuId',
True)
67 self.
jetLepDR = getattr(self.cfg_ana,
'jetLepDR', 0.4)
68 self.
jetLepArbitration = getattr(self.cfg_ana,
'jetLepArbitration',
lambda jet,lepton: lepton)
69 self.
lepPtMin = getattr(self.cfg_ana,
'minLepPt', -1)
70 self.
lepSelCut = getattr(self.cfg_ana,
'lepSelCut',
lambda lep :
True)
71 self.
jetGammaDR = getattr(self.cfg_ana,
'jetGammaDR', 0.4)
72 if(self.cfg_ana.doQG):
74 if not hasattr(self.cfg_ana ,
"collectionPostFix"):self.cfg_ana.collectionPostFix=
""
78 self.handles[
'jets'] = AutoHandle( self.cfg_ana.jetCol,
'std::vector<pat::Jet>' )
79 self.handles[
'genJet'] = AutoHandle( self.cfg_ana.genJetCol,
'vector<reco::GenJet>' )
80 self.
shiftJER = self.cfg_ana.shiftJER
if hasattr(self.cfg_ana,
'shiftJER')
else 0
81 self.handles[
'rho'] = AutoHandle( self.cfg_ana.rho,
'double' )
87 self.readCollections( event.input )
88 rho = float(self.handles[
'rho'].product()[0])
92 if self.cfg_ana.copyJetsByValue:
94 allJets =
map(
lambda j:
Jet(ROOT.pat.Jet(j)), self.handles[
'jets'].product())
96 allJets =
map(Jet, self.handles[
'jets'].product())
106 if self.cfg_comp.isMC:
107 self.
genJets = [ x
for x
in self.handles[
'genJet'].product() ]
109 if getattr(self.cfg_ana,
'smearJets',
False):
113 allJets.sort(key =
lambda j : j.pt(), reverse =
True)
117 self.jetsAllNoID = []
121 self.jetsAllNoID.append(jet)
124 if(self.cfg_ana.doQG):
126 jet.qgl = self.qglcalc.computeQGLikelihood(jet, rho)
129 self.jets.append(jet)
130 self.jetsIdOnly.append(jet)
132 self.jetsFailId.append(jet)
134 self.jetsIdOnly.append(jet)
138 if hasattr(event,
'selectedLeptons'):
139 leptons = [ l
for l
in event.selectedLeptons
if l.pt() > self.
lepPtMin and self.
lepSelCut(l) ]
140 if self.cfg_ana.cleanJetsFromTaus
and hasattr(event,
'selectedTaus'):
141 leptons = leptons[:] + event.selectedTaus
142 if self.cfg_ana.cleanJetsFromIsoTracks
and hasattr(event,
'selectedIsoCleanTrack'):
143 leptons = leptons[:] + event.selectedIsoCleanTrack
145 self.cleanJets = [j
for j
in self.cleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
146 self.cleanJetsFwd = [j
for j
in self.cleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
147 self.discardedJets = [j
for j
in self.jets
if j
not in self.cleanJetsAll]
148 if hasattr(event,
'selectedLeptons')
and self.cfg_ana.cleanSelectedLeptons:
149 event.discardedLeptons = [ l
for l
in leptons
if l
not in cleanLeptons ]
150 event.selectedLeptons = [ l
for l
in event.selectedLeptons
if l
not in event.discardedLeptons ]
154 if hasattr(event,
'selectedPhotons'):
155 if self.cfg_ana.cleanJetsFromFirstPhoton:
156 photons = event.selectedPhotons[:1]
158 photons = [ g
for g
in event.selectedPhotons ]
161 self.gamma_cleanJets = [j
for j
in self.gamma_cleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
162 self.gamma_cleanJetsFwd = [j
for j
in self.gamma_cleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
165 if self.cfg_ana.alwaysCleanPhotons:
166 self.cleanJets = self.gamma_cleanJets
167 self.cleanJetsAll = self.gamma_cleanJetsAll
168 self.cleanJetsFwd = self.gamma_cleanJetsFwd
171 leptons = event.inclusiveLeptons
if hasattr(event,
'inclusiveLeptons')
else event.selectedLeptons
175 jet.leptons = [l
for l
in jlpairs
if jlpairs[l] == jet ]
184 taus = getattr(event,
'selectedTaus',[])
188 jet.taus = [l
for l
in jtaupairs
if jtaupairs[l] == jet ]
190 tau.jet = jtaupairs[tau]
193 if self.cfg_comp.isMC:
194 self.deltaMetFromJetSmearing = [0, 0]
195 for j
in self.cleanJetsAll:
196 if hasattr(j,
'deltaMetFromJetSmearing'):
197 self.deltaMetFromJetSmearing[0] += j.deltaMetFromJetSmearing[0]
198 self.deltaMetFromJetSmearing[1] += j.deltaMetFromJetSmearing[1]
201 if self.cfg_ana.cleanGenJetsFromPhoton:
215 setattr(event,
"rho" +self.cfg_ana.collectionPostFix, self.
rho )
216 setattr(event,
"deltaMetFromJEC" +self.cfg_ana.collectionPostFix, self.
deltaMetFromJEC )
217 setattr(event,
"allJetsUsedForMET" +self.cfg_ana.collectionPostFix, self.
allJetsUsedForMET )
218 setattr(event,
"jets" +self.cfg_ana.collectionPostFix, self.jets )
219 setattr(event,
"jetsFailId" +self.cfg_ana.collectionPostFix, self.jetsFailId )
220 setattr(event,
"jetsAllNoID" +self.cfg_ana.collectionPostFix, self.jetsAllNoID )
221 setattr(event,
"jetsIdOnly" +self.cfg_ana.collectionPostFix, self.jetsIdOnly )
222 setattr(event,
"cleanJetsAll" +self.cfg_ana.collectionPostFix, self.cleanJetsAll )
223 setattr(event,
"cleanJets" +self.cfg_ana.collectionPostFix, self.cleanJets )
224 setattr(event,
"cleanJetsFwd" +self.cfg_ana.collectionPostFix, self.cleanJetsFwd )
225 setattr(event,
"discardedJets" +self.cfg_ana.collectionPostFix, self.discardedJets )
226 setattr(event,
"gamma_cleanJetsAll" +self.cfg_ana.collectionPostFix, self.gamma_cleanJetsAll )
227 setattr(event,
"gamma_cleanJets" +self.cfg_ana.collectionPostFix, self.gamma_cleanJets )
228 setattr(event,
"gamma_cleanJetsFwd" +self.cfg_ana.collectionPostFix, self.gamma_cleanJetsFwd )
231 if self.cfg_comp.isMC:
232 setattr(event,
"cleanGenJets" +self.cfg_ana.collectionPostFix, self.cleanGenJets )
233 setattr(event,
"bqObjects" +self.cfg_ana.collectionPostFix, self.
bqObjects )
234 setattr(event,
"cqObjects" +self.cfg_ana.collectionPostFix, self.
cqObjects )
235 setattr(event,
"partons" +self.cfg_ana.collectionPostFix, self.
partons )
236 setattr(event,
"heaviestQCDFlavour" +self.cfg_ana.collectionPostFix, self.
heaviestQCDFlavour )
237 setattr(event,
"deltaMetFromJetSmearing"+self.cfg_ana.collectionPostFix, self.deltaMetFromJetSmearing)
238 setattr(event,
"genJets" +self.cfg_ana.collectionPostFix, self.
genJets )
245 jet.puJetIdPassed = jet.puJetId()
246 jet.pfJetIdPassed = jet.jetID(
'POG_PFID_Loose')
247 if self.cfg_ana.relaxJetId:
250 return jet.pfJetIdPassed
and (jet.puJetIdPassed
or not(self.
doPuId))
254 return jet.pt() > self.cfg_ana.jetPt
and \
255 abs( jet.eta() ) < self.cfg_ana.jetEta;
270 for ii
in range(0, jet.numberOfDaughters()) :
272 part = jet.daughter(ii)
274 if part.charge() == 0 :
276 if part.pt() < 1.:
continue
280 if part.trackHighPurity()==
False:
continue
281 if part.fromPV()<=1:
continue
286 deta = part.eta() - jet.eta()
287 dphi =
deltaPhi(part.phi(), jet.phi())
289 weight = partPt*partPt
292 sum_deta += deta*weight
293 sum_dphi += dphi*weight
294 sum_deta2 += deta*deta*weight
295 sum_detadphi += deta*dphi*weight
296 sum_dphi2 += dphi*dphi*weight
306 jet.ptd = math.sqrt(sum_weight)/sum_pt
307 ave_deta = sum_deta/sum_weight
308 ave_dphi = sum_dphi/sum_weight
309 ave_deta2 = sum_deta2/sum_weight
310 ave_dphi2 = sum_dphi2/sum_weight
311 a = ave_deta2 - ave_deta*ave_deta
312 b = ave_dphi2 - ave_dphi*ave_dphi
313 c = -(sum_detadphi/sum_weight - ave_deta*ave_dphi)
316 delta = math.sqrt(math.fabs((a-b)*(a-b)+4.*c*c))
318 if a+b-delta > 0: jet.axis2 = -math.log(math.sqrt(0.5*(a+b-delta)))
319 else: jet.axis2 = -1.
325 if id > 999:
return (id/1000)%10 == f
326 if id > 99:
return (id/100)%10 == f
331 self.
bqObjects = [ p
for p
in event.genParticles
if (p.status() == 2
and isFlavour(p,5)) ]
332 self.
cqObjects = [ p
for p
in event.genParticles
if (p.status() == 2
and isFlavour(p,4)) ]
334 self.
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]) ) ]
339 for jet
in self.cleanJetsAll:
341 jet.partonId = (parton.pdgId()
if parton !=
None else 0)
342 jet.partonMotherId = (parton.mother(0).
pdgId()
if parton !=
None and parton.numberOfMothers()>0
else 0)
344 for jet
in self.jets:
360 event.genbquarks + event.genwzquarks,
365 jet.mcMatchId = (gen.sourceId
if gen !=
None else 0)
366 jet.mcMatchFlav = (
abs(gen.pdgId())
if gen !=
None else 0)
372 jet.mcJet = match[jet]
381 genpt, jetpt, aeta = gen.pt(), jet.pt(),
abs(jet.eta())
383 factor = 1.052 + self.shiftJER*math.hypot(0.012,0.062);
384 if aeta > 2.3: factor = 1.288 + self.shiftJER*math.hypot(0.127,0.154)
385 elif aeta > 1.7: factor = 1.134 + self.shiftJER*math.hypot(0.035,0.066)
386 elif aeta > 1.1: factor = 1.096 + self.shiftJER*math.hypot(0.017,0.063)
387 elif aeta > 0.5: factor = 1.057 + self.shiftJER*math.hypot(0.012,0.056)
388 ptscale =
max(0.0, (jetpt + (factor-1)*(jetpt-genpt))/jetpt)
390 jet.deltaMetFromJetSmearing = [ -(ptscale-1)*jet.rawFactor()*jet.px(), -(ptscale-1)*jet.rawFactor()*jet.py() ]
392 jet.setP4(jet.p4()*ptscale)
394 jet._rawFactorMultiplier *= (1.0/ptscale)
if ptscale != 0
else 1
399 setattr(JetAnalyzer,
"defaultConfig", cfg.Analyzer(
400 class_object = JetAnalyzer,
401 jetCol =
'slimmedJets',
402 copyJetsByValue =
False,
403 genJetCol =
'slimmedGenJets',
404 rho = (
'fixedGridRhoFastjetAll',
'',
''),
409 jetLepArbitration = (
lambda jet,lepton : lepton),
410 cleanSelectedLeptons =
True,
412 lepSelCut =
lambda lep :
True,
416 recalibrateJets =
False,
417 recalibrationType =
"AK4PFchs",
421 cleanJetsFromFirstPhoton =
False,
422 cleanJetsFromTaus =
False,
423 cleanJetsFromIsoTracks =
False,
424 alwaysCleanPhotons =
False,
426 cleanGenJetsFromPhoton =
False,
427 collectionPostFix =
""
deltaMetFromJEC
Read jets, if necessary recalibrate and shift MET.
def matchObjectCollection
def matchObjectCollection2
Abs< T >::type abs(const T &t)
double deltaR2(const T1 &t1, const T2 &t2)
if(conf.exists("allCellsPositionCalc"))