127 self.readCollections( event.input )
128 rho =
float(self.handles[
'rho'].product()[0])
132 if self.cfg_ana.copyJetsByValue:
135 allJets =
map(
lambda j:
Jet(ROOT.heppy.JetUtils.copyJet(j)), self.handles[
'jets'].product())
137 allJets =
map(Jet, self.handles[
'jets'].product())
143 self.deltaMetFromJEC = [0.,0.]
144 self.type1METCorr = [0.,0.,0.]
147 if not self.recalibrateJets:
148 jetsBefore = [ (j.pt(),j.eta(),j.phi(),j.rawFactor())
for j
in allJets ]
149 self.jetReCalibrator.correctAll(allJets, rho, delta=self.shiftJEC,
150 addCorr=
True, addShifts=self.addJECShifts,
151 metShift=self.deltaMetFromJEC, type1METCorr=self.type1METCorr )
152 if not self.recalibrateJets:
153 jetsAfter = [ (j.pt(),j.eta(),j.phi(),j.rawFactor())
for j
in allJets ]
154 if len(jetsBefore) != len(jetsAfter):
155 print(
"ERROR: I had to recompute jet corrections, and they rejected some of the jets:\nold = %s\n new = %s\n" % (jetsBefore,jetsAfter))
157 for (told, tnew)
in zip(jetsBefore,jetsAfter):
158 if (
deltaR2(told[1],told[2],tnew[1],tnew[2])) > 0.0001:
159 print(
"ERROR: I had to recompute jet corrections, and one jet direction moved: old = %s, new = %s\n" % (told, tnew))
160 elif abs(told[0]-tnew[0])/(told[0]+tnew[0]) > 0.5e-3
or abs(told[3]-tnew[3]) > 0.5e-3:
161 print(
"ERROR: I had to recompute jet corrections, and one jet pt or corr changed: old = %s, new = %s\n" % (told, tnew))
162 self.allJetsUsedForMET = allJets
165 if self.cfg_comp.isMC:
166 self.genJets = [ x
for x
in self.handles[
'genJet'].product() ]
167 if self.cfg_ana.do_mc_match:
168 for igj, gj
in enumerate(self.genJets):
171 self.matchJets(event, [ j
for j
in allJets
if j.pt()>self.cfg_ana.jetPt ])
172 if getattr(self.cfg_ana,
'smearJets',
False):
173 self.smearJets(event, allJets)
179 allJets.sort(key =
lambda j : j.pt(), reverse =
True)
182 if hasattr(event,
'selectedLeptons'):
183 leptons = [ l
for l
in event.selectedLeptons
if l.pt() > self.lepPtMin
and self.lepSelCut(l) ]
184 if self.cfg_ana.cleanJetsFromTaus
and hasattr(event,
'selectedTaus'):
185 leptons = leptons[:] + event.selectedTaus
186 if self.cfg_ana.cleanJetsFromIsoTracks
and hasattr(event,
'selectedIsoCleanTrack'):
187 leptons = leptons[:] + event.selectedIsoCleanTrack
192 self.jetsAllNoID = []
196 leps_with_overlaps = []
197 if getattr(self.cfg_ana,
'checkLeptonPFOverlap',
True):
198 for i
in range(jet.numberOfSourceCandidatePtrs()):
199 p1 = jet.sourceCandidatePtr(i)
201 for j
in range(lep.numberOfSourceCandidatePtrs()):
202 p2 = lep.sourceCandidatePtr(j)
203 has_overlaps = p1.key() == p2.key()
and p1.refCore().
id().productIndex() == p2.refCore().
id().productIndex()
and p1.refCore().
id().processIndex() == p2.refCore().
id().processIndex()
205 leps_with_overlaps += [lep]
206 if len(leps_with_overlaps)>0:
207 for lep
in leps_with_overlaps:
209 if self.testJetNoID( jet ):
210 self.jetsAllNoID.
append(jet)
211 if(self.cfg_ana.doQG):
212 jet.qgl_calc = self.qglcalc.computeQGLikelihood
214 if self.testJetID( jet ):
216 self.jetsIdOnly.
append(jet)
218 self.jetsFailId.
append(jet)
219 elif self.testJetID (jet ):
220 self.jetsIdOnly.
append(jet)
222 jetsEtaCut = [j
for j
in self.jets
if abs(j.eta()) < self.cfg_ana.jetEta ]
223 self.cleanJetsAll, cleanLeptons =
cleanJetsAndLeptons(jetsEtaCut, leptons, self.jetLepDR, self.jetLepArbitration)
225 self.cleanJets = [j
for j
in self.cleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
226 self.cleanJetsFwd = [j
for j
in self.cleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
227 self.discardedJets = [j
for j
in self.jets
if j
not in self.cleanJetsAll]
228 if hasattr(event,
'selectedLeptons')
and self.cfg_ana.cleanSelectedLeptons:
229 event.discardedLeptons = [ l
for l
in leptons
if l
not in cleanLeptons ]
230 event.selectedLeptons = [ l
for l
in event.selectedLeptons
if l
not in event.discardedLeptons ]
232 if hasattr(lep,
"jetOverlap"):
233 if lep.jetOverlap
in self.cleanJetsAll:
235 lep.jetOverlapIdx = self.cleanJetsAll.
index(lep.jetOverlap)
236 elif lep.jetOverlap
in self.discardedJets:
238 lep.jetOverlapIdx = 1000 + self.discardedJets.
index(lep.jetOverlap)
241 self.noIdCleanJetsAll, cleanLeptons =
cleanJetsAndLeptons(self.jetsAllNoID, leptons, self.jetLepDR, self.jetLepArbitration)
242 self.noIdCleanJets = [j
for j
in self.noIdCleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
243 self.noIdCleanJetsFwd = [j
for j
in self.noIdCleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
244 self.noIdDiscardedJets = [j
for j
in self.jetsAllNoID
if j
not in self.noIdCleanJetsAll]
248 if hasattr(event,
'selectedPhotons'):
249 if self.cfg_ana.cleanJetsFromFirstPhoton:
250 photons = event.selectedPhotons[:1]
252 photons = [ g
for g
in event.selectedPhotons ]
254 self.gamma_cleanJetaAll = []
255 self.gamma_noIdCleanJetsAll = []
257 if self.cleanFromLepAndGammaSimultaneously:
258 self.gamma_cleanJetsAll =
cleanNearestJetOnly(jetsEtaCut, photons+leptons, self.jetGammaLepDR)
259 self.gamma_noIdCleanJetsAll =
cleanNearestJetOnly(self.jetsAllNoID, photons+leptons, self.jetGammaLepDR)
262 self.gamma_noIdCleanJetsAll =
cleanNearestJetOnly(self.noIdCleanJetsAll, photons, self.jetGammaDR)
264 self.gamma_cleanJets = [j
for j
in self.gamma_cleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
265 self.gamma_cleanJetsFwd = [j
for j
in self.gamma_cleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
267 self.gamma_noIdCleanJets = [j
for j
in self.gamma_noIdCleanJetsAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
268 self.gamma_noIdCleanJetsFwd = [j
for j
in self.gamma_noIdCleanJetsAll
if abs(j.eta()) >= self.cfg_ana.jetEtaCentral ]
271 if self.cfg_ana.alwaysCleanPhotons:
272 self.cleanJets = self.gamma_cleanJets
273 self.cleanJetsAll = self.gamma_cleanJetsAll
274 self.cleanJetsFwd = self.gamma_cleanJetsFwd
276 self.noIdCleanJets = self.gamma_noIdCleanJets
277 self.noIdCleanJetsAll = self.gamma_noIdCleanJetsAll
278 self.noIdCleanJetsFwd = self.gamma_noIdCleanJetsFwd
281 self.cleanJetsFailIdAll = []
282 for jet
in self.noIdCleanJetsAll:
283 if not self.testJetID( jet ):
284 self.cleanJetsFailIdAll.
append(jet)
286 self.cleanJetsFailId = [j
for j
in self.cleanJetsFailIdAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
289 self.gamma_cleanJetsFailIdAll = []
290 for jet
in self.gamma_noIdCleanJetsAll:
291 if not self.testJetID( jet ):
292 self.gamma_cleanJetsFailIdAll.
append(jet)
294 self.gamma_cleanJetsFailId = [j
for j
in self.gamma_cleanJetsFailIdAll
if abs(j.eta()) < self.cfg_ana.jetEtaCentral ]
297 incleptons = event.inclusiveLeptons
if hasattr(event,
'inclusiveLeptons')
else event.selectedLeptons
301 jet.leptons = [l
for l
in jlpairs
if jlpairs[l] == jet ]
302 for lep
in incleptons:
305 setattr(lep,
"jet"+self.cfg_ana.collectionPostFix,lep)
307 setattr(lep,
"jet"+self.cfg_ana.collectionPostFix,jet)
309 taus = getattr(event,
'selectedTaus',[])
313 jet.taus = [l
for l
in jtaupairs
if jtaupairs[l] == jet ]
315 setattr(tau,
"jet"+self.cfg_ana.collectionPostFix,jtaupairs[tau])
318 if self.cfg_comp.isMC:
319 self.deltaMetFromJetSmearing = [0, 0]
320 for j
in self.cleanJetsAll:
321 if hasattr(j,
'deltaMetFromJetSmearing'):
322 self.deltaMetFromJetSmearing[0] += j.deltaMetFromJetSmearing[0]
323 self.deltaMetFromJetSmearing[1] += j.deltaMetFromJetSmearing[1]
327 if self.cfg_ana.cleanGenJetsFromPhoton:
330 if getattr(self.cfg_ana,
'attachNeutrinos',
True)
and hasattr(self.cfg_ana,
"genNuSelection") :
331 jetNus=[x
for x
in event.genParticles
if abs(x.pdgId())
in [12,14,16]
and self.cfg_ana.genNuSelection(x) ]
332 pairs= matchObjectCollection (jetNus, self.genJets, 0.4**2)
334 for (nu,genJet)
in pairs.items() :
335 if genJet
is not None :
336 if not hasattr(genJet,
"nu") :
342 if self.cfg_ana.do_mc_match:
343 self.jetFlavour(event)
345 if hasattr(event,
"jets"+self.cfg_ana.collectionPostFix):
raise RuntimeError(
"Event already contains a jet collection with the following postfix: "+self.cfg_ana.collectionPostFix)
346 setattr(event,
"rho" +self.cfg_ana.collectionPostFix, self.rho )
347 setattr(event,
"deltaMetFromJEC" +self.cfg_ana.collectionPostFix, self.deltaMetFromJEC )
348 setattr(event,
"type1METCorr" +self.cfg_ana.collectionPostFix, self.type1METCorr )
349 setattr(event,
"allJetsUsedForMET" +self.cfg_ana.collectionPostFix, self.allJetsUsedForMET )
350 setattr(event,
"jets" +self.cfg_ana.collectionPostFix, self.jets )
351 setattr(event,
"jetsFailId" +self.cfg_ana.collectionPostFix, self.jetsFailId )
352 setattr(event,
"jetsAllNoID" +self.cfg_ana.collectionPostFix, self.jetsAllNoID )
353 setattr(event,
"jetsIdOnly" +self.cfg_ana.collectionPostFix, self.jetsIdOnly )
354 setattr(event,
"cleanJetsAll" +self.cfg_ana.collectionPostFix, self.cleanJetsAll )
355 setattr(event,
"cleanJets" +self.cfg_ana.collectionPostFix, self.cleanJets )
356 setattr(event,
"cleanJetsFwd" +self.cfg_ana.collectionPostFix, self.cleanJetsFwd )
357 setattr(event,
"cleanJetsFailIdAll" +self.cfg_ana.collectionPostFix, self.cleanJetsFailIdAll )
358 setattr(event,
"cleanJetsFailId" +self.cfg_ana.collectionPostFix, self.cleanJetsFailId )
359 setattr(event,
"discardedJets" +self.cfg_ana.collectionPostFix, self.discardedJets )
360 setattr(event,
"gamma_cleanJetsAll" +self.cfg_ana.collectionPostFix, self.gamma_cleanJetsAll )
361 setattr(event,
"gamma_cleanJets" +self.cfg_ana.collectionPostFix, self.gamma_cleanJets )
362 setattr(event,
"gamma_cleanJetsFwd" +self.cfg_ana.collectionPostFix, self.gamma_cleanJetsFwd )
363 setattr(event,
"gamma_cleanJetsFailIdAll" +self.cfg_ana.collectionPostFix, self.gamma_cleanJetsFailIdAll )
364 setattr(event,
"gamma_cleanJetsFailId" +self.cfg_ana.collectionPostFix, self.gamma_cleanJetsFailId )
367 if self.cfg_comp.isMC:
368 setattr(event,
"deltaMetFromJetSmearing"+self.cfg_ana.collectionPostFix, self.deltaMetFromJetSmearing)
369 setattr(event,
"cleanGenJets" +self.cfg_ana.collectionPostFix, self.cleanGenJets )
370 setattr(event,
"genJets" +self.cfg_ana.collectionPostFix, self.genJets )
371 if self.cfg_ana.do_mc_match:
372 setattr(event,
"bqObjects" +self.cfg_ana.collectionPostFix, self.bqObjects )
373 setattr(event,
"cqObjects" +self.cfg_ana.collectionPostFix, self.cqObjects )
374 setattr(event,
"partons" +self.cfg_ana.collectionPostFix, self.partons )
375 setattr(event,
"heaviestQCDFlavour" +self.cfg_ana.collectionPostFix, self.heaviestQCDFlavour )
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr float zip(ConstView const &tracks, int32_t i)
def matchObjectCollection
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Abs< T >::type abs(const T &t)
def cleanNearestJetOnly(jets, leptons, deltaR)
def cleanJetsAndLeptons(jets, leptons, deltaR, arbitration)
if(threadIdxLocalY==0 &&threadIdxLocalX==0)