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
7 import PhysicsTools.HeppyCore.framework.config
as cfg
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):
73 qgdefname=
"{CMSSW_BASE}/src/PhysicsTools/Heppy/data/pdfQG_AK4chs_antib_13TeV_v1.root"
75 if not hasattr(self.cfg_ana ,
"collectionPostFix"):self.cfg_ana.collectionPostFix=
""
79 self.handles[
'jets'] = AutoHandle( self.cfg_ana.jetCol,
'std::vector<pat::Jet>' )
80 self.handles[
'genJet'] = AutoHandle( self.cfg_ana.genJetCol,
'vector<reco::GenJet>' )
81 self.
shiftJER = self.cfg_ana.shiftJER
if hasattr(self.cfg_ana,
'shiftJER')
else 0
82 self.handles[
'rho'] = AutoHandle( self.cfg_ana.rho,
'double' )
88 self.readCollections( event.input )
89 rho = float(self.handles[
'rho'].product()[0])
93 if self.cfg_ana.copyJetsByValue:
95 allJets =
map(
lambda j:
Jet(ROOT.pat.Jet(j)), self.handles[
'jets'].product())
97 allJets =
map(Jet, self.handles[
'jets'].product())
107 if self.cfg_comp.isMC:
108 self.
genJets = [ x
for x
in self.handles[
'genJet'].product() ]
110 if getattr(self.cfg_ana,
'smearJets',
False):
114 allJets.sort(key =
lambda j : j.pt(), reverse =
True)
118 self.jetsAllNoID = []
122 self.jetsAllNoID.append(jet)
125 if(self.cfg_ana.doQG):
127 jet.qgl = self.qglcalc.computeQGLikelihood(jet, rho)
130 self.jets.append(jet)
131 self.jetsIdOnly.append(jet)
133 self.jetsFailId.append(jet)
135 self.jetsIdOnly.append(jet)
139 if hasattr(event,
'selectedLeptons'):
140 leptons = [ l
for l
in event.selectedLeptons
if l.pt() > self.
lepPtMin and self.
lepSelCut(l) ]
141 if self.cfg_ana.cleanJetsFromTaus
and hasattr(event,
'selectedTaus'):
142 leptons = leptons[:] + event.selectedTaus
143 if self.cfg_ana.cleanJetsFromIsoTracks
and hasattr(event,
'selectedIsoCleanTrack'):
144 leptons = leptons[:] + event.selectedIsoCleanTrack
146 self.cleanJets = [j
for j
in self.cleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
147 self.cleanJetsFwd = [j
for j
in self.cleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
148 self.discardedJets = [j
for j
in self.jets
if j
not in self.cleanJetsAll]
149 if hasattr(event,
'selectedLeptons')
and self.cfg_ana.cleanSelectedLeptons:
150 event.discardedLeptons = [ l
for l
in leptons
if l
not in cleanLeptons ]
151 event.selectedLeptons = [ l
for l
in event.selectedLeptons
if l
not in event.discardedLeptons ]
155 if hasattr(event,
'selectedPhotons'):
156 if self.cfg_ana.cleanJetsFromFirstPhoton:
157 photons = event.selectedPhotons[:1]
159 photons = [ g
for g
in event.selectedPhotons ]
162 self.gamma_cleanJets = [j
for j
in self.gamma_cleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
163 self.gamma_cleanJetsFwd = [j
for j
in self.gamma_cleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
166 if self.cfg_ana.alwaysCleanPhotons:
167 self.cleanJets = self.gamma_cleanJets
168 self.cleanJetsAll = self.gamma_cleanJetsAll
169 self.cleanJetsFwd = self.gamma_cleanJetsFwd
172 leptons = event.inclusiveLeptons
if hasattr(event,
'inclusiveLeptons')
else event.selectedLeptons
176 jet.leptons = [l
for l
in jlpairs
if jlpairs[l] == jet ]
185 taus = getattr(event,
'selectedTaus',[])
189 jet.taus = [l
for l
in jtaupairs
if jtaupairs[l] == jet ]
191 tau.jet = jtaupairs[tau]
194 if self.cfg_comp.isMC:
195 self.deltaMetFromJetSmearing = [0, 0]
196 for j
in self.cleanJetsAll:
197 if hasattr(j,
'deltaMetFromJetSmearing'):
198 self.deltaMetFromJetSmearing[0] += j.deltaMetFromJetSmearing[0]
199 self.deltaMetFromJetSmearing[1] += j.deltaMetFromJetSmearing[1]
202 if self.cfg_ana.cleanGenJetsFromPhoton:
216 setattr(event,
"rho" +self.cfg_ana.collectionPostFix, self.
rho )
217 setattr(event,
"deltaMetFromJEC" +self.cfg_ana.collectionPostFix, self.
deltaMetFromJEC )
218 setattr(event,
"allJetsUsedForMET" +self.cfg_ana.collectionPostFix, self.
allJetsUsedForMET )
219 setattr(event,
"jets" +self.cfg_ana.collectionPostFix, self.jets )
220 setattr(event,
"jetsFailId" +self.cfg_ana.collectionPostFix, self.jetsFailId )
221 setattr(event,
"jetsAllNoID" +self.cfg_ana.collectionPostFix, self.jetsAllNoID )
222 setattr(event,
"jetsIdOnly" +self.cfg_ana.collectionPostFix, self.jetsIdOnly )
223 setattr(event,
"cleanJetsAll" +self.cfg_ana.collectionPostFix, self.cleanJetsAll )
224 setattr(event,
"cleanJets" +self.cfg_ana.collectionPostFix, self.cleanJets )
225 setattr(event,
"cleanJetsFwd" +self.cfg_ana.collectionPostFix, self.cleanJetsFwd )
226 setattr(event,
"discardedJets" +self.cfg_ana.collectionPostFix, self.discardedJets )
227 setattr(event,
"gamma_cleanJetsAll" +self.cfg_ana.collectionPostFix, self.gamma_cleanJetsAll )
228 setattr(event,
"gamma_cleanJets" +self.cfg_ana.collectionPostFix, self.gamma_cleanJets )
229 setattr(event,
"gamma_cleanJetsFwd" +self.cfg_ana.collectionPostFix, self.gamma_cleanJetsFwd )
232 if self.cfg_comp.isMC:
233 setattr(event,
"cleanGenJets" +self.cfg_ana.collectionPostFix, self.cleanGenJets )
234 setattr(event,
"bqObjects" +self.cfg_ana.collectionPostFix, self.
bqObjects )
235 setattr(event,
"cqObjects" +self.cfg_ana.collectionPostFix, self.
cqObjects )
236 setattr(event,
"partons" +self.cfg_ana.collectionPostFix, self.
partons )
237 setattr(event,
"heaviestQCDFlavour" +self.cfg_ana.collectionPostFix, self.
heaviestQCDFlavour )
238 setattr(event,
"deltaMetFromJetSmearing"+self.cfg_ana.collectionPostFix, self.deltaMetFromJetSmearing)
239 setattr(event,
"genJets" +self.cfg_ana.collectionPostFix, self.
genJets )
246 jet.puJetIdPassed = jet.puJetId()
247 jet.pfJetIdPassed = jet.jetID(
'POG_PFID_Loose')
248 if self.cfg_ana.relaxJetId:
251 return jet.pfJetIdPassed
and (jet.puJetIdPassed
or not(self.
doPuId))
255 return jet.pt() > self.cfg_ana.jetPt
and \
256 abs( jet.eta() ) < self.cfg_ana.jetEta;
271 for ii
in range(0, jet.numberOfDaughters()) :
273 part = jet.daughter(ii)
275 if part.charge() == 0 :
277 if part.pt() < 1.:
continue
281 if part.trackHighPurity()==
False:
continue
282 if part.fromPV()<=1:
continue
287 deta = part.eta() - jet.eta()
288 dphi =
deltaPhi(part.phi(), jet.phi())
290 weight = partPt*partPt
293 sum_deta += deta*weight
294 sum_dphi += dphi*weight
295 sum_deta2 += deta*deta*weight
296 sum_detadphi += deta*dphi*weight
297 sum_dphi2 += dphi*dphi*weight
307 jet.ptd = math.sqrt(sum_weight)/sum_pt
308 ave_deta = sum_deta/sum_weight
309 ave_dphi = sum_dphi/sum_weight
310 ave_deta2 = sum_deta2/sum_weight
311 ave_dphi2 = sum_dphi2/sum_weight
312 a = ave_deta2 - ave_deta*ave_deta
313 b = ave_dphi2 - ave_dphi*ave_dphi
314 c = -(sum_detadphi/sum_weight - ave_deta*ave_dphi)
317 delta = math.sqrt(math.fabs((a-b)*(a-b)+4.*c*c))
319 if a+b-delta > 0: jet.axis2 = -math.log(math.sqrt(0.5*(a+b-delta)))
320 else: jet.axis2 = -1.
326 if id > 999:
return (id/1000)%10 == f
327 if id > 99:
return (id/100)%10 == f
332 self.
bqObjects = [ p
for p
in event.genParticles
if (p.status() == 2
and isFlavour(p,5)) ]
333 self.
cqObjects = [ p
for p
in event.genParticles
if (p.status() == 2
and isFlavour(p,4)) ]
335 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]) ) ]
340 for jet
in self.cleanJetsAll:
342 jet.partonId = (parton.pdgId()
if parton !=
None else 0)
343 jet.partonMotherId = (parton.mother(0).
pdgId()
if parton !=
None and parton.numberOfMothers()>0
else 0)
345 for jet
in self.jets:
361 event.genbquarks + event.genwzquarks,
366 jet.mcMatchId = (gen.sourceId
if gen !=
None else 0)
367 jet.mcMatchFlav = (
abs(gen.pdgId())
if gen !=
None else 0)
373 jet.mcJet = match[jet]
382 genpt, jetpt, aeta = gen.pt(), jet.pt(),
abs(jet.eta())
384 factor = 1.052 + self.shiftJER*math.hypot(0.012,0.062);
385 if aeta > 2.3: factor = 1.288 + self.shiftJER*math.hypot(0.127,0.154)
386 elif aeta > 1.7: factor = 1.134 + self.shiftJER*math.hypot(0.035,0.066)
387 elif aeta > 1.1: factor = 1.096 + self.shiftJER*math.hypot(0.017,0.063)
388 elif aeta > 0.5: factor = 1.057 + self.shiftJER*math.hypot(0.012,0.056)
389 ptscale =
max(0.0, (jetpt + (factor-1)*(jetpt-genpt))/jetpt)
391 jet.deltaMetFromJetSmearing = [ -(ptscale-1)*jet.rawFactor()*jet.px(), -(ptscale-1)*jet.rawFactor()*jet.py() ]
393 jet.setP4(jet.p4()*ptscale)
395 jet._rawFactorMultiplier *= (1.0/ptscale)
if ptscale != 0
else 1
400 setattr(JetAnalyzer,
"defaultConfig", cfg.Analyzer(
401 class_object = JetAnalyzer,
402 jetCol =
'slimmedJets',
403 copyJetsByValue =
False,
404 genJetCol =
'slimmedGenJets',
405 rho = (
'fixedGridRhoFastjetAll',
'',
''),
410 jetLepArbitration = (
lambda jet,lepton : lepton),
411 cleanSelectedLeptons =
True,
413 lepSelCut =
lambda lep :
True,
417 recalibrateJets =
False,
418 recalibrationType =
"AK4PFchs",
422 cleanJetsFromFirstPhoton =
False,
423 cleanJetsFromTaus =
False,
424 cleanJetsFromIsoTracks =
False,
425 alwaysCleanPhotons =
False,
427 cleanGenJetsFromPhoton =
False,
428 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"))