Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to automate use of updateJetCollection for PAT-dependent discriminators #85

Draft
wants to merge 3 commits into
base: jetToolbox_102X
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 95 additions & 36 deletions python/jetToolbox_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from PhysicsTools.PatAlgos.tools.helpers import getPatAlgosToolsTask, addToProcessAndTask
from PhysicsTools.NanoAOD.common_cff import *
from collections import OrderedDict
from copy import deepcopy

def jetToolbox( proc, jetType, jetSequence, outputFile,
updateCollection='', updateCollectionSubjets='',
Expand Down Expand Up @@ -121,6 +122,64 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
if runOnData: subJETCorrLevels.append('L2L3Residual')
#################################################################################

#################################################################################
####### Helper function for dealing with addJetCollection vs. updateJetCollection for btagging
def addJetCollectionSmart(proc, **kwargs):
# there are probably some other conditions where this should be used
hasDeepDoubleB = "btagDiscriminators" in kwargs and any("DeepDouble" in x for x in kwargs["btagDiscriminators"])
patJetsInterim = ""
if "postfix" not in kwargs:
kwargs["postfix"] = ""
addTagInfos = False
tagInfoSources = None
if "addTagInfos" in kwargs:
addTagInfos = kwargs["addTagInfos"]
kwargs.pop("addTagInfos",None)
if "tagInfoSources" in kwargs:
tagInfoSources = kwargs["tagInfoSources"]
kwargs.pop("tagInfoSources",None)
if hasDeepDoubleB:
# "clone and rename" based on applyDeepBtagging_cff
kwargsOrig = deepcopy(kwargs)
kwargs["btagDiscriminators"] = [x for x in kwargsOrig["btagDiscriminators"] if "DeepDouble" not in x]
kwargs["postfix"] = kwargsOrig["postfix"] + "NoDeep"
addJetCollection(proc, **kwargs)
patJetsInterim = patJets+kwargs["labelName"]+kwargs["postfix"]
# kwargsOrig["jetSource"] = cms.InputTag(selPatJets+kwargs["labelName"]+kwargs["postfix"])
kwargsOrig["jetSource"] = cms.InputTag(patJetsInterim)
kwargsOrig["btagDiscriminators"] = [x for x in kwargsOrig["btagDiscriminators"] if "DeepDouble" in x]
unneeded = ["getJetMCFlavour","genJetCollection","genParticles","outputModules"]
for v in unneeded:
kwargsOrig.pop(v,None)
updateJetCollection(proc, **kwargsOrig)
patJetsFinal = patJets+kwargsOrig["labelName"]+kwargsOrig["postfix"]
patJetsUpdated = 'updated'+patJetsFinal[:1].upper()+patJetsFinal[1:]
selPatJetsFinal = selPatJets+kwargsOrig["labelName"]+kwargsOrig["postfix"]
selPatJetsUpdated = selPatJetsFinal.replace("selected","selectedUpdated")
# print proc.dumpPython()
# fix names
def swap_module(proc, new, old):
_addProcessAndTask(proc, new, getattr(proc, old).clone())
delattr(proc, old)
setattr(proc, old, cms.EDAlias( **{new : cms.VPSet(cms.PSet(type = cms.string('patJets')))} ))
swap_module(proc, patJetsFinal, patJetsUpdated)
swap_module(proc, selPatJetsFinal, selPatJetsUpdated)
# to keep userfloat keys consistent
if "PATJets" not in mod:
mod["PATJetsFirst"] = patJetsInterim
else:
# pass through
addJetCollection(proc, **kwargs)
patJetsInterim = patJets+kwargs["labelName"]+kwargs["postfix"]
if "PATJets" not in mod:
mod["PATJetsFirst"] = patJetsInterim

# common modification
if addTagInfos:
getattr(proc, patJetsInterim).addTagInfos = cms.bool(True)
if tagInfoSources:
getattr(proc, patJetsInterim).tagInfoSources = tagInfoSources

#################################################################################
####### Setting b-tag discriminators
## adding the default taggers for 94X
Expand Down Expand Up @@ -396,7 +455,10 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
else: _addProcessAndTask( proc, mod["PFJetsConstituents"], cms.EDProducer("PFJetConstituentSelector", src = cms.InputTag( mod["PFJets"] ), cut = cms.string( Cut ) ))
jetSeq += getattr(proc, mod["PFJetsConstituents"])

addJetCollection(
patJets = 'patJets'
patSubJets = ''
selPatJets = 'selectedPatJets'
addJetCollectionSmart(
proc,
labelName = mod["PATJetsLabel"],
jetSource = cms.InputTag(mod["PFJets"]),
Expand All @@ -414,16 +476,13 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
btagInfos = bTagInfos,
getJetMCFlavour = GetJetMCFlavour,
genParticles = cms.InputTag(genParticlesLabel),
outputModules = ['outputFile']
outputModules = ['outputFile'],
addTagInfos = True,
)
patJets = 'patJets'
patSubJets = ''
selPatJets = 'selectedPatJets'
mod["PATJets"] = patJets+mod["PATJetsLabelPost"]
mod["selPATJets"] = selPatJets+mod["PATJetsLabelPost"]
getattr(proc, mod["PATJets"]).addTagInfos = cms.bool(True)

if 'CS' in PUMethod: getattr( proc, mod["PATJets"] ).getJetMCFlavour = False # CS jets cannot be re-clustered from their constituents
if 'CS' in PUMethod: getattr( proc, mod["PATJetsFirst"] ).getJetMCFlavour = False # CS jets cannot be re-clustered from their constituents
#########################################################################

####### update jets from existing collection
Expand Down Expand Up @@ -531,7 +590,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
jetSeq += getattr(proc, mod["SoftDropMass"] )

#### Adding softdrop mass as userFloat
getattr( proc, mod["PATJets"]).userData.userFloats.src += [ mod["SoftDropMass"] ]
getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [ mod["SoftDropMass"] ]
jetVariables['softdropMass'] = Var("userFloat('"+mod["SoftDropMass"]+"')", float, doc='Softdrop mass', precision=10)
toolsUsed.append( mod["SoftDropMass"] )
#########################################################################
Expand Down Expand Up @@ -560,7 +619,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,

#### Creatig a PATJetCollection with softdrop jets
mod["PATJetsSoftDropLabel"] = mod["PATJetsLabelPost"]+'SoftDrop'
addJetCollection(
addJetCollectionSmart(
proc,
labelName = mod["PATJetsSoftDropLabel"],
jetSource = cms.InputTag( mod["PFJetsSoftDrop"]),
Expand All @@ -581,7 +640,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,

##### Creating the softdrop subjet PATJetCollection
mod["PATSubjetsSoftDropLabel"] = mod["PATJetsSoftDropLabel"]+'Subjets'
addJetCollection(
addJetCollectionSmart(
proc,
labelName = mod["PATSubjetsSoftDropLabel"],
jetSource = cms.InputTag( mod["PFJetsSoftDrop"], 'SubJets'),
Expand Down Expand Up @@ -669,7 +728,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
jetSeq += getattr(proc, mod["PrunedMass"])

#### Adding softdrop mass as userFloat
getattr( proc, mod["PATJets"]).userData.userFloats.src += [ mod["PrunedMass"] ]
getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [ mod["PrunedMass"] ]
jetVariables['prunedMass'] = Var("userFloat('"+mod["PrunedMass"]+"')", float, doc='Pruned mass', precision=10)
elemToKeep += [ 'keep *_'+mod["PrunedMass"]+'_*_*']
toolsUsed.append( mod["PrunedMass"] )
Expand All @@ -694,7 +753,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,

#### Creatig a PATJetCollection with pruning jets
mod["PATJetsPrunedLabel"] = mod["PATJetsLabelPost"]+'Pruned'
addJetCollection(
addJetCollectionSmart(
proc,
labelName = mod["PATJetsPrunedLabel"],
jetSource = cms.InputTag( mod["PFJetsPruned"]),
Expand All @@ -715,7 +774,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,

##### Creating the pruning subjet PATJetCollection
mod["PATSubjetsPrunedLabel"] = mod["PATJetsPrunedLabel"]+'Subjets'
addJetCollection(
addJetCollectionSmart(
proc,
labelName = mod["PATSubjetsPrunedLabel"],
jetSource = cms.InputTag( mod["PFJetsPruned"], 'SubJets'),
Expand Down Expand Up @@ -795,7 +854,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
elemToKeep += [ 'keep *_'+mod["TrimmedMass"]+'_*_*']
jetSeq += getattr(proc, mod["PFJetsTrimmed"])
jetSeq += getattr(proc, mod["TrimmedMass"])
getattr( proc, mod["PATJets"]).userData.userFloats.src += [mod["TrimmedMass"]]
getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [mod["TrimmedMass"]]
jetVariables['trimmedMass'] = Var("userFloat('"+mod["TrimmedMass"]+"')", float, doc='Trimmed mass', precision=10)
toolsUsed.append( mod["TrimmedMass"] )
#################################################################################
Expand All @@ -822,7 +881,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
elemToKeep += [ 'keep *_'+mod["FilteredMass"]+'_*_*']
jetSeq += getattr(proc, mod["PFJetsFiltered"])
jetSeq += getattr(proc, mod["FilteredMass"])
getattr( proc, patJets+jetALGO+'PF'+PUMethod+postFix).userData.userFloats.src += [mod["FilteredMass"]]
getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [mod["FilteredMass"]]
jetVariables['filteredMass'] = Var("userFloat('"+mod["FilteredMass"]+"')", float, doc='Filtered mass', precision=10)
toolsUsed.append( mod["FilteredMass"] )
#################################################################################
Expand Down Expand Up @@ -878,7 +937,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
)
)
mod["PATJetsCMSTopTagLabel"] = 'CMSTopTag'+PUMethod+postFix
addJetCollection(
addJetCollectionSmart(
proc,
labelName = mod["PATJetsCMSTopTagLabel"],
jetSource = cms.InputTag(mod["PFJetsCMSTopTag"]),
Expand All @@ -892,16 +951,16 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
btagInfos = bTagInfos,
genJetCollection = cms.InputTag(mod["GenJetsNoNu"]),
getJetMCFlavour = False, # jet flavor should always be disabled for groomed jets
genParticles = cms.InputTag(genParticlesLabel)
genParticles = cms.InputTag(genParticlesLabel),
addTagInfos = True,
tagInfoSources = cms.VInputTag( cms.InputTag(mod["CATopTagInfos"])),
)
mod["PATJetsCMSTopTag"] = patJets+mod["PATJetsCMSTopTagLabel"]
mod["selPATJetsCMSTopTag"] = selPatJets+mod["PATJetsCMSTopTagLabel"]
getattr(proc,mod["PATJetsCMSTopTag"]).addTagInfos = True
getattr(proc,mod["PATJetsCMSTopTag"]).tagInfoSources = cms.VInputTag( cms.InputTag(mod["CATopTagInfos"]))
_addProcessAndTask( proc, mod["selPATJetsCMSTopTag"], selectedPatJets.clone( src = mod["PATJetsCMSTopTag"], cut = Cut ) )

mod["PATSubjetsCMSTopTagLabel"] = mod["PATJetsCMSTopTagLabel"]+'Subjets'
addJetCollection(
addJetCollectionSmart(
proc,
labelName = mod["PATSubjetsCMSTopTagLabel"],
jetSource = cms.InputTag(mod["PFJetsCMSTopTag"], 'SubJets'),
Expand Down Expand Up @@ -958,7 +1017,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
_addProcessAndTask( proc, mod["MassDropFilteredMass"], ak8PFJetsCHSPrunedMass.clone( src = cms.InputTag( mod["PFJets"]),
matched = cms.InputTag(mod["PFJetsMassDrop"]), distMax = cms.double( jetSize ) ) )
elemToKeep += [ 'keep *_'+mod["MassDropFilteredMass"]+'_*_*' ]
getattr( proc, mod["PATJets"]).userData.userFloats.src += [ mod["MassDropFilteredMass"] ]
getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [ mod["MassDropFilteredMass"] ]
jetVariables['massDropFilteredMass'] = Var("userFloat('"+mod["MassDropFilteredMass"]+"')", float, doc='MassDropFiltered mass', precision=10)
jetSeq += getattr(proc, mod["PFJetsMassDrop"])
jetSeq += getattr(proc, mod["MassDropFilteredMass"])
Expand Down Expand Up @@ -1005,15 +1064,15 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
#valueLabels = cms.vstring(["mass","fRec"])
) )
elemToKeep += [ 'keep *_'+mod["PFJetsHEPTopTagMass"]+'_*_*' ]
getattr( proc, mod["PATJets"] ).userData.userFloats.src += [ mod["PFJetsHEPTopTagMass"] ]
#getattr( proc, mod["PATJets"] ).userData.userFloats.src += [ "fRec" ]
getattr( proc, mod["PATJetsFirst"] ).userData.userFloats.src += [ mod["PFJetsHEPTopTagMass"] ]
#getattr( proc, mod["PATJetsFirst"] ).userData.userFloats.src += [ "fRec" ]
jetVariables['HEPTopTagMass'] = Var("userFloat('"+mod["PFJetsHEPTopTagMass"]+"')", float, doc='HEPTopTagMass', precision=10)
jetSeq += getattr(proc, mod["PFJetsHEPTopTagMass"])
toolsUsed.append( mod["PFJetsHEPTopTagMass"] )


mod["PATJetsHEPTopTagLabel"] = 'HEPTopTag'+PUMethod+postFix
addJetCollection(
addJetCollectionSmart(
proc,
labelName = mod["PATJetsHEPTopTagLabel"],
jetSource = cms.InputTag(mod["PFJetsHEPTopTag"]),
Expand All @@ -1027,19 +1086,19 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
btagInfos = bTagInfos,
genJetCollection = cms.InputTag(mod["GenJetsNoNu"]),
getJetMCFlavour = False, # jet flavor should always be disabled for groomed jets
genParticles = cms.InputTag(genParticlesLabel)
genParticles = cms.InputTag(genParticlesLabel),
addTagInfos = True,
tagInfoSources = cms.VInputTag( cms.InputTag(mod["PFJetsHEPTopTag"])),
)
mod["PATJetsHEPTopTag"] = patJets+mod["PATJetsHEPTopTagLabel"]
mod["selPATJetsHEPTopTag"] = selPatJets+mod["PATJetsHEPTopTagLabel"]
getattr(proc,mod["PATJetsHEPTopTag"]).addTagInfos = True
getattr(proc,mod["PATJetsHEPTopTag"]).tagInfoSources = cms.VInputTag( cms.InputTag(mod["PFJetsHEPTopTag"]))
_addProcessAndTask( proc, mod["selPATJetsHEPTopTag"],
selectedPatJets.clone( src = mod["PATJetsHEPTopTag"], cut = Cut ) )
jetSeq += getattr(proc, mod["selPATJetsHEPTopTag"])
elemToKeep += [ 'keep *_'+mod["selPATJetsHEPTopTag"]+'_*_*' ]

mod["PATSubjetsHEPTopTagLabel"] = mod["PATJetsHEPTopTagLabel"]+'Subjets'
addJetCollection(
addJetCollectionSmart(
proc,
labelName = mod["PATSubjetsHEPTopTagLabel"],
jetSource = cms.InputTag(mod["PFJetsHEPTopTag"], 'SubJets'),
Expand Down Expand Up @@ -1099,7 +1158,7 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,

elemToKeep += [ 'keep *_'+mod["Njettiness"]+'_*_*' ]
for tau in rangeTau:
getattr( proc, mod["PATJets"]).userData.userFloats.src += [mod["Njettiness"]+':tau'+str(tau) ]
getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [mod["Njettiness"]+':tau'+str(tau) ]
jetVariables[mod["Njettiness"]+':tau'+str(tau)] = Var("userFloat('"+mod["Njettiness"]+':tau'+str(tau)+"')", float, doc='Nsubjetiness tau'+str(tau), precision=10)
jetSeq += getattr(proc, mod["Njettiness"])
toolsUsed.append( mod["Njettiness"] )
Expand Down Expand Up @@ -1166,10 +1225,10 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
)
)
elemToKeep += [ 'keep *_'+mod["QGTagger"]+'_*_*' ]
getattr( proc, mod["PATJets"]).userData.userFloats.src += [mod["QGTagger"]+':qgLikelihood']
#getattr( proc, mod["PATJets"]).userData.userFloats.src += [ mod["QGTagger"]+':axis2']
#getattr( proc, mod["PATJets"]).userData.userFloats.src += [ mod["QGTagger"]+':mult']
#getattr( proc, mod["PATJets"]).userData.userFloats.src += [ mod["QGTagger"]+':ptD']
getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [mod["QGTagger"]+':qgLikelihood']
#getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [ mod["QGTagger"]+':axis2']
#getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [ mod["QGTagger"]+':mult']
#getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [ mod["QGTagger"]+':ptD']
jetVariables[mod["QGTagger"]+':qgLikelihood'] = Var("userFloat('"+mod["QGTagger"]+':qgLikelihood'+"')", float, doc='Quark/Gluon likelihood', precision=10)
#jetVariables[mod["QGTagger"]+':axis2'] = Var("userFloat('"+mod["QGTagger"]+':axis2'+"')", float, doc='QGL variable: minor axis of jet ellipse', precision=10)
#jetVariables[mod["QGTagger"]+':mult'] = Var("userFloat('"+mod["QGTagger"]+':mult'+"')", float, doc='QGL variable: jet constituent multiplicity', precision=10)
Expand Down Expand Up @@ -1209,9 +1268,9 @@ def jetToolbox( proc, jetType, jetSequence, outputFile,
)
)

getattr( proc, mod["PATJets"]).userData.userFloats.src += [mod["PUJetIDEval"]+':fullDiscriminant']
getattr( proc, mod["PATJetsFirst"]).userData.userFloats.src += [mod["PUJetIDEval"]+':fullDiscriminant']
jetVariables[mod["PUJetIDEval"]+':fullDiscriminant'] = Var("userFloat('"+mod["PUJetIDEval"]+':fullDiscriminant'+"')", float, doc='PUJetID fulldiscriminant', precision=10)
getattr( proc, mod["PATJets"]).userData.userInts.src += [mod["PUJetIDEval"]+':cutbasedId',mod["PUJetIDEval"]+':fullId']
getattr( proc, mod["PATJetsFirst"]).userData.userInts.src += [mod["PUJetIDEval"]+':cutbasedId',mod["PUJetIDEval"]+':fullId']
jetVariables[mod["PUJetIDEval"]+':cutbasedId'] = Var("userInt('"+mod["PUJetIDEval"]+':cutbasedId'+"')", int, doc='PUJetID cutbased Id', precision=10)
jetVariables[mod["PUJetIDEval"]+':fullId'] = Var("userInt('"+mod["PUJetIDEval"]+':fullId'+"')", int, doc='PUJetID full Id', precision=10)
elemToKeep += ['keep *_'+mod["PUJetIDEval"]+'_*_*']
Expand Down Expand Up @@ -1294,7 +1353,7 @@ def getNjetsInfoECF(ecfT,cutN3):
valueLabels = cms.vstring(ecfLabels),
)
)
getattr(proc, mod["PATJets"]).userData.userFloats.src += [mod["PFJetsSoftDropValueMap"]+':'+ecfLabel for ecfLabel in ecfLabels]
getattr(proc, mod["PATJetsFirst"]).userData.userFloats.src += [mod["PFJetsSoftDropValueMap"]+':'+ecfLabel for ecfLabel in ecfLabels]
#################################################################################


Expand Down
13 changes: 2 additions & 11 deletions test/ClusterWithToolboxAndMakeHistos.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_condDBv2_cff')
process.GlobalTag.globaltag = '92X_upgrade2017_realistic_v1'
process.GlobalTag.globaltag = '94X_mc2017_realistic_v12'
process.options = cms.untracked.PSet( wantSummary = cms.untracked.bool(True) )
process.options = cms.untracked.PSet( wantSummary = cms.untracked.bool(False) )
process.options.allowUnscheduled = cms.untracked.bool(True)
process.MessageLogger.cerr.FwkReport.reportEvery = 1000

### INPUT
process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )

process.source = cms.Source("PoolSource",
fileNames = cms.untracked.vstring('/store/mc/RunIIAutumn18MiniAOD/TTJets_TuneCP5_13TeV-amcatnloFXFX-pythia8/MINIAODSIM/102X_upgrade2018_realistic_v15_ext1-v2/20000/7E65457A-87E5-C146-8321-9A48B4F56ED1.root'),
fileNames = cms.untracked.vstring('root://cmsxrootd.fnal.gov//store/test/xrootd/T2_US_MIT/store/mc/RunIISummer20UL18MiniAODv2/TTJets_TuneCP5_13TeV-madgraphMLM-pythia8/MINIAODSIM/106X_upgrade2018_realistic_v16_L1v1-v2/2430000/0984A792-8B13-1543-AA86-063CC14B1678.root'),
)

#from PhysicsTools.PatAlgos.patInputFiles_cff import filesRelValTTbarPileUpMINIAODSIM
Expand Down Expand Up @@ -148,15 +148,6 @@
JETCorrPayload = 'AK8PFchs', JETCorrLevels = ['L2Relative', 'L3Absolute']
)

# KT R=0.8 jets from CHS
jetToolbox( process, 'kt8', 'kt8JetSubs', 'noOutput',
Cut="pt>170 && abs(eta)<2.5",
dataTier="miniAOD",
PUMethod='CHS',
addPruning=True, addSoftDrop=True , # add basic grooming
JETCorrPayload = 'AK8PFchs', JETCorrLevels = ['L2Relative', 'L3Absolute']
)

# KT R=0.8 jets from CHS
jetToolbox( process, 'kt8', 'kt8JetSubs', 'noOutput',
dataTier="miniAOD",
Expand Down