-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
jetCollectionTools.py
509 lines (461 loc) · 17.2 KB
/
jetCollectionTools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import FWCore.ParameterSet.Config as cms
from PhysicsTools.PatAlgos.tools.ConfigToolBase import *
from Configuration.Eras.Modifier_run2_miniAOD_80XLegacy_cff import run2_miniAOD_80XLegacy
from Configuration.Eras.Modifier_run2_nanoAOD_94X2016_cff import run2_nanoAOD_94X2016
from RecoJets.JetProducers.PFJetParameters_cfi import PFJetParameters
from RecoJets.JetProducers.GenJetParameters_cfi import GenJetParameters
from RecoJets.JetProducers.AnomalousCellParameters_cfi import AnomalousCellParameters
from RecoJets.JetProducers.ak4GenJets_cfi import ak4GenJets
from RecoJets.JetProducers.ak4PFJets_cfi import ak4PFJetsCS
from PhysicsTools.PatAlgos.tools.jetTools import addJetCollection, supportedJetAlgos
from PhysicsTools.PatAlgos.producersLayer1.jetUpdater_cfi import updatedPatJets
from PhysicsTools.PatAlgos.recoLayer0.jetCorrFactors_cfi import patJetCorrFactors
from PhysicsTools.PatAlgos.mcMatchLayer0.jetFlavourId_cff import patJetFlavourAssociation
from CommonTools.PileupAlgos.Puppi_cff import puppi
from CommonTools.PileupAlgos.softKiller_cfi import softKiller
import re
#============================================
#
# GenJetInfo
#
#============================================
class GenJetInfo(object):
"""
Class to hold information of a genjet collection
"""
def __init__(self, jet, inputCollection):
self.jet = jet
self.jetLower = jet.lower()
self.jetUpper = jet.upper()
self.jetTagName = self.jetUpper
self.inputCollection = inputCollection
algoKey = 'algo'
sizeKey = 'size'
recoKey = 'reco'
jetRegex = re.compile(
r'(?P<{algo}>({algoList}))(?P<{size}>[0-9]+)gen'.format(
algo = algoKey,
algoList = '|'.join(supportedJetAlgos.keys()),
size = sizeKey,
)
)
jetMatch = jetRegex.match(jet.lower())
if not jetMatch:
raise RuntimeError('Invalid jet collection: %s' % jet)
self.jetAlgo = jetMatch.group(algoKey)
self.jetSize = jetMatch.group(sizeKey)
self.jetSizeNr = float(self.jetSize) / 10.
#============================================
#
# GenJetAdder
#
#============================================
class GenJetAdder(object):
"""
Tool to schedule modules for building a genjet collection with input MiniAODs
"""
def __init__(self):
self.prerequisites = []
self.main = []
self.gpLabel = "prunedGenParticles"
def getSequence(self, proc):
tasks = self.prerequisites + self.main
resultSequence = cms.Sequence()
for idx, task in enumerate(tasks):
if idx == 0:
resultSequence = cms.Sequence(getattr(proc, task))
else:
resultSequence.insert(idx, getattr(proc, task))
return resultSequence
def addGenJetCollection(self,
proc,
jet,
inputCollection = "",
genName = "",
minPt = 5.,
):
print("jetCollectionTools::GenJetAdder::addGenJetCollection: Adding Gen Jet Collection: {}".format(jet))
currentTasks = []
#
# Decide which jet collection we're dealing with
#
jetLower = jet.lower()
jetUpper = jet.upper()
tagName = jetUpper
genJetInfo = GenJetInfo(jet,inputCollection)
#=======================================================
#
# If gen jet collection in MiniAOD is not
# specified, build the genjet collection.
#
#========================================================
if not inputCollection:
print("jetCollectionTools::GenJetAdder::addGenJetCollection: inputCollection not specified. Building genjet collection now")
#
# Setup GenParticles
#
packedGenPartNoNu = "packedGenParticlesForJetsNoNu"
if packedGenPartNoNu not in self.prerequisites:
setattr(proc, packedGenPartNoNu, cms.EDFilter("CandPtrSelector",
src = cms.InputTag("packedGenParticles"),
cut = cms.string("abs(pdgId) != 12 && abs(pdgId) != 14 && abs(pdgId) != 16"),
)
)
self.prerequisites.append(packedGenPartNoNu)
#
# Create the GenJet collection
#
genJetsCollection = "{}{}{}".format(genJetInfo.jetAlgo.upper(), genJetInfo.jetSize, 'GenJetsNoNu')
setattr(proc, genJetsCollection, ak4GenJets.clone(
src = packedGenPartNoNu,
jetAlgorithm = cms.string(supportedJetAlgos[genJetInfo.jetAlgo]),
rParam = cms.double(genJetInfo.jetSizeNr),
)
)
self.prerequisites.append(genJetsCollection)
#
# GenJet Flavour Labelling
#
genFlavour = "{}Flavour".format(genJetInfo.jetTagName)
setattr(proc, genFlavour, patJetFlavourAssociation.clone(
jets = cms.InputTag(genJetsCollection),
jetAlgorithm = cms.string(supportedJetAlgos[genJetInfo.jetAlgo]),
rParam = cms.double(genJetInfo.jetSizeNr),
)
)
currentTasks.append(genFlavour)
self.main.extend(currentTasks)
return genJetInfo
#============================================
#
# RecoJetInfo
#
#============================================
class RecoJetInfo(object):
"""
Class to hold information of a recojet collection
"""
def __init__(self, jet, inputCollection):
self.jet = jet
self.jetLower = jet.lower()
self.jetUpper = jet.upper()
self.jetTagName = self.jetUpper
self.inputCollection = inputCollection
algoKey = 'algo'
sizeKey = 'size'
recoKey = 'reco'
puMethodKey = 'puMethod'
jetRegex = re.compile(
r'(?P<{algo}>({algoList}))(?P<{size}>[0-9]+)(?P<{reco}>(pf|calo))(?P<{puMethod}>(chs|puppi|sk|cs|))'.format(
algo = algoKey,
algoList = '|'.join(supportedJetAlgos.keys()),
size = sizeKey,
reco = recoKey,
puMethod = puMethodKey,
)
)
jetMatch = jetRegex.match(jet.lower())
if not jetMatch:
raise RuntimeError('Invalid jet collection: %s' % jet)
self.jetAlgo = jetMatch.group(algoKey)
self.jetSize = jetMatch.group(sizeKey)
self.jetReco = jetMatch.group(recoKey)
self.jetPUMethod = jetMatch.group(puMethodKey)
self.jetSizeNr = float(self.jetSize) / 10.
self.doCalo = self.jetReco == "calo"
self.doCS = self.jetPUMethod == "cs"
self.skipUserData = self.doCalo or (self.jetPUMethod in [ "puppi", "sk" ] and inputCollection == "")
self.jetCorrPayload = "{}{}{}".format(
self.jetAlgo.upper(), self.jetSize, "Calo" if self.doCalo else self.jetReco.upper()
)
if self.jetPUMethod == "puppi":
self.jetCorrPayload += "Puppi"
elif self.jetPUMethod in [ "cs", "sk" ]:
self.jetCorrPayload += "chs"
else:
self.jetCorrPayload += self.jetPUMethod.lower()
#============================================
#
# RecoJetAdder
#
#============================================
class RecoJetAdder(object):
"""
Tool to schedule modules for building a recojet collection with input MiniAODs
"""
def __init__(self):
self.prerequisites = []
self.main = []
self.bTagDiscriminators = ["None"] # No b-tagging by default
self.JETCorrLevels = [ "L1FastJet", "L2Relative", "L3Absolute" ]
self.pfLabel = "packedPFCandidates"
self.pvLabel = "offlineSlimmedPrimaryVertices"
self.svLabel = "slimmedSecondaryVertices"
self.muLabel = "slimmedMuons"
self.elLabel = "slimmedElectrons"
self.gpLabel = "prunedGenParticles"
def getSequence(self, proc):
tasks = self.prerequisites + self.main
resultSequence = cms.Sequence()
for idx, task in enumerate(tasks):
if idx == 0:
resultSequence = cms.Sequence(getattr(proc, task))
else:
resultSequence.insert(idx, getattr(proc, task))
return resultSequence
def addRecoJetCollection(self,
proc,
jet,
inputCollection = "",
genJetsCollection = "",
minPt = 5.,
bTagDiscriminators = None,
JETCorrLevels = None,
):
print("jetCollectionTools::RecoJetAdder::addRecoJetCollection: Adding Reco Jet Collection: {}".format(jet))
currentTasks = []
if inputCollection and inputCollection not in [
"slimmedJets", "slimmedJetsAK8", "slimmedJetsPuppi", "slimmedCaloJets",
]:
raise RuntimeError("Invalid input collection: %s" % inputCollection)
if bTagDiscriminators is None:
bTagDiscriminators = self.bTagDiscriminators
if JETCorrLevels is None:
JETCorrLevels = self.JETCorrLevels
#
# Decide which jet collection we're dealing with
#
recoJetInfo = RecoJetInfo(jet, inputCollection)
jetLower = recoJetInfo.jetLower
jetUpper = recoJetInfo.jetUpper
tagName = recoJetInfo.jetTagName
if inputCollection == "slimmedJets":
assert(jetLower == "ak4pfchs")
elif inputCollection == "slimmedJetsAK8":
assert(jetLower == "ak8pfpuppi")
elif inputCollection == "slimmedJetsPuppi":
assert(jetLower == "ak4pfpuppi")
elif inputCollection == "slimmedCaloJets":
assert(jetLower == "ak4calo")
#=======================================================
#
# If jet collection in MiniAOD is not
# specified, build the jet collection.
#
#========================================================
if not inputCollection or recoJetInfo.doCalo:
print("jetCollectionTools::RecoJetAdder::addRecoJetCollection: inputCollection not specified. Building recojet collection now")
#=======================================================
#
# Prepare the inputs to jet clustering
#
#========================================================
#
# Set up PF candidates
#
pfCand = self.pfLabel
#
# Setup PU method for PF candidates
#
if recoJetInfo.jetPUMethod not in [ "", "cs" ]:
pfCand += recoJetInfo.jetPUMethod
#
#
#
if pfCand not in self.prerequisites:
#
# Skip if no PU Method or CS specified
#
if recoJetInfo.jetPUMethod in [ "", "cs" ]:
pass
#
# CHS
#
elif recoJetInfo.jetPUMethod == "chs":
setattr(proc, pfCand,
cms.EDFilter("CandPtrSelector",
src = cms.InputTag(self.pfLabel),
cut = cms.string("fromPV"),
)
)
self.prerequisites.append(pfCand)
#
# PUPPI
#
elif recoJetInfo.jetPUMethod == "puppi":
setattr(proc, pfCand,
puppi.clone(
candName = self.pfLabel,
vertexName = self.pvLabel,
)
)
self.prerequisites.append(pfCand)
#
# Softkiller
#
elif recoJetInfo.jetPUMethod == "sk":
setattr(proc, pfCand,
softKiller.clone(
PFCandidates = self.pfLabel,
rParam = recoJetInfo.jetSizeNr,
)
)
self.prerequisites.append(pfCand)
else:
raise RuntimeError("Currently unsupported PU method: '%s'" % recoJetInfo.jetPUMethod)
#============================================
#
# Create the recojet collection
#
#============================================
if not recoJetInfo.doCalo:
jetCollection = '{}Collection'.format(tagName)
if jetCollection in self.main:
raise ValueError("Step '%s' already implemented" % jetCollection)
setattr(proc, jetCollection, ak4PFJetsCS.clone(
src = pfCand,
doAreaFastjet = True,
jetPtMin = minPt,
jetAlgorithm = supportedJetAlgos[recoJetInfo.jetAlgo],
rParam = recoJetInfo.jetSizeNr,
useConstituentSubtraction = recoJetInfo.doCS,
csRParam = 0.4 if recoJetInfo.doCS else -1.,
csRho_EtaMax = PFJetParameters.Rho_EtaMax if recoJetInfo.doCS else -1.,
useExplicitGhosts = recoJetInfo.doCS or recoJetInfo.jetPUMethod == "sk",
)
)
currentTasks.append(jetCollection)
else:
jetCollection = inputCollection
#
# PATify
#
if recoJetInfo.jetPUMethod == "puppi":
jetCorrLabel = "Puppi"
elif recoJetInfo.jetPUMethod in [ "cs", "sk" ]:
jetCorrLabel = "chs"
else:
jetCorrLabel = recoJetInfo.jetPUMethod
#
# Jet correction
#
jetCorrections = (
"{}{}{}{}".format(
recoJetInfo.jetAlgo.upper(),
recoJetInfo.jetSize,
"Calo" if recoJetInfo.doCalo else recoJetInfo.jetReco.upper(),
jetCorrLabel
),
JETCorrLevels,
"None",
)
addJetCollection(
proc,
labelName = tagName,
jetSource = cms.InputTag(jetCollection),
algo = recoJetInfo.jetAlgo,
rParam = recoJetInfo.jetSizeNr,
pvSource = cms.InputTag(self.pvLabel),
pfCandidates = cms.InputTag(self.pfLabel),
svSource = cms.InputTag(self.svLabel),
muSource = cms.InputTag(self.muLabel),
elSource = cms.InputTag(self.elLabel),
btagDiscriminators = bTagDiscriminators if not recoJetInfo.doCalo else [ "None" ],
jetCorrections = jetCorrections,
genJetCollection = cms.InputTag(genJetsCollection),
genParticles = cms.InputTag(self.gpLabel),
)
getJetMCFlavour = not recoJetInfo.doCalo and recoJetInfo.jetPUMethod != "cs"
setattr(getattr(proc, "patJets{}".format(tagName)), "getJetMCFlavour", cms.bool(getJetMCFlavour))
setattr(getattr(proc, "patJetCorrFactors{}".format(tagName)), "payload", cms.string(recoJetInfo.jetCorrPayload))
selJet = "selectedPatJets{}".format(tagName)
else:
selJet = inputCollection
if not recoJetInfo.skipUserData:
#
#
#
jercVar = "jercVars{}".format(tagName)
if jercVar in self.main:
raise ValueError("Step '%s' already implemented" % jercVar)
setattr(proc, jercVar, proc.jercVars.clone(srcJet = selJet))
currentTasks.append(jercVar)
#
#
#
looseJetId = "looseJetId{}".format(tagName)
if looseJetId in self.main:
raise ValueError("Step '%s' already implemented" % looseJetId)
setattr(proc, looseJetId, proc.looseJetId.clone(src = selJet))
#
#
#
tightJetId = "tightJetId{}".format(tagName)
if tightJetId in self.main:
raise ValueError("Step '%s' already implemented" % tightJetId)
setattr(proc, tightJetId, proc.tightJetId.clone(src = selJet))
currentTasks.append(tightJetId)
#
#
#
tightJetIdLepVeto = "tightJetIdLepVeto{}".format(tagName)
if tightJetIdLepVeto in self.main:
raise ValueError("Step '%s' already implemented" % tightJetIdLepVeto)
setattr(proc, tightJetIdLepVeto, proc.tightJetIdLepVeto.clone(src = selJet))
currentTasks.append(tightJetIdLepVeto)
#
#
#
selectedPatJetsWithUserData = "{}WithUserData".format(selJet)
if selectedPatJetsWithUserData in self.main:
raise ValueError("Step '%s' already implemented" % selectedPatJetsWithUserData)
setattr(proc, selectedPatJetsWithUserData,
cms.EDProducer("PATJetUserDataEmbedder",
src = cms.InputTag(selJet),
userFloats = cms.PSet(
jercCHPUF = cms.InputTag("{}:chargedHadronPUEnergyFraction".format(jercVar)),
jercCHF = cms.InputTag("{}:chargedHadronCHSEnergyFraction".format(jercVar)),
),
userInts = cms.PSet(
tightId = cms.InputTag(tightJetId),
tightIdLepVeto = cms.InputTag(tightJetIdLepVeto),
),
)
)
for modifier in run2_miniAOD_80XLegacy, run2_nanoAOD_94X2016:
selectedPatJetsWithUserDataObj = getattr(proc, selectedPatJetsWithUserData)
modifier.toModify(selectedPatJetsWithUserDataObj.userInts,
looseId = looseJetId,
tightIdLepVeto = None,
)
currentTasks.append(selectedPatJetsWithUserData)
else:
selectedPatJetsWithUserData = "selectedPatJets{}".format(tagName)
#
# Not sure why we can't re-use patJetCorrFactors* created by addJetCollection()
# (even cloning doesn't work) Let's just create our own
#
jetCorrFactors = "jetCorrFactors{}".format(tagName)
if jetCorrFactors in self.main:
raise ValueError("Step '%s' already implemented" % jetCorrFactors)
setattr(proc, jetCorrFactors, patJetCorrFactors.clone(
src = selectedPatJetsWithUserData,
levels = JETCorrLevels,
primaryVertices = self.pvLabel,
payload = recoJetInfo.jetCorrPayload,
rho = "fixedGridRhoFastjetAll{}".format("Calo" if recoJetInfo.doCalo else ""),
)
)
currentTasks.append(jetCorrFactors)
updatedJets = "updatedJets{}".format(tagName)
if updatedJets in self.main:
raise ValueError("Step '%s' already implemented" % updatedJets)
setattr(proc, updatedJets, updatedPatJets.clone(
addBTagInfo = False,
jetSource = selectedPatJetsWithUserData,
jetCorrFactorsSource = [jetCorrFactors],
)
)
currentTasks.append(updatedJets)
self.main.extend(currentTasks)
return recoJetInfo