Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-03 00:12:33

0001 import FWCore.ParameterSet.Config as cms
0002 
0003 from PhysicsTools.NanoAOD.common_cff import *
0004 from PhysicsTools.NanoAOD.simpleCandidateFlatTableProducer_cfi import simpleCandidateFlatTableProducer
0005 from PhysicsTools.NanoAOD.jetsAK8_cff import fatJetTable, subJetTable
0006 from PhysicsTools.NanoAOD.jetsAK4_Puppi_cff import jetPuppiTable
0007 from PhysicsTools.NanoAOD.genparticles_cff import *
0008 # when storing the flat table, always do "T"able in the naming convention
0009 
0010 def addGenCands(process, allPF = False, addAK4=False, addAK8=False):
0011     process.btvGenTask = cms.Task()
0012     process.schedule.associate(process.btvGenTask)
0013 
0014     process.genWeightsTable.keepAllPSWeights = True
0015 
0016     process.finalGenParticles.select +=[
0017             "keep (4 <= abs(pdgId) <= 5) && statusFlags().isLastCopy()", # BTV: keep b/c quarks in their last copy
0018             "keep (abs(pdgId) == 310 || abs(pdgId) == 3122) && statusFlags().isLastCopy()", # BTV: keep K0s and Lambdas in their last copy
0019             "++keep (400 < abs(pdgId) < 600) || (4000 < abs(pdgId) < 6000)", # keep all B and C hadron + their decay products
0020         ]
0021 
0022     process.btvGenTable =  cms.EDProducer(
0023         "SimpleGenParticleFlatTableProducer",
0024         src = cms.InputTag("finalGenParticles"),
0025         name= cms.string("GenPart"),
0026         doc = cms.string("interesting gen particles "),
0027         singleton=cms.bool(False),
0028         variables=
0029         cms.PSet(
0030             genParticleTable.variables,
0031         vx = Var("vx", "float", doc="x coordinate of vertex position"),
0032         vy = Var("vy", "float", doc="y coordinate of vertex position"),
0033         vz = Var("vz", "float", doc="z coordinate of vertex position"),
0034         genPartIdxMother2 = Var("?numberOfMothers>1?motherRef(1).key():-1", "int", doc="index of the second mother particle, if valid")
0035         )
0036     )
0037     process.btvGenTask.add(process.btvGenTable)
0038     process.genParticleTablesTask.replace(process.genParticleTable,process.btvGenTable)
0039 
0040     if addAK4:
0041         process.btvMCTable = cms.EDProducer("BTVMCFlavourTableProducer",name=jetPuppiTable.name,src=cms.InputTag("linkedObjects","jets"),genparticles=cms.InputTag("prunedGenParticles"))
0042         process.btvGenTask.add(process.btvMCTable)
0043 
0044         process.btvAK4JetExtTable = cms.EDProducer(
0045             "SimplePATJetFlatTableProducer",
0046             src=jetPuppiTable.src,
0047             cut=jetPuppiTable.cut,
0048             name=jetPuppiTable.name,
0049             doc=jetPuppiTable.doc,
0050             singleton=cms.bool(False),  # the number of entries is variable
0051             extension=cms.bool(True),  # this is the extension table for Jets
0052             variables=cms.PSet(
0053             nBHadrons=Var("jetFlavourInfo().getbHadrons().size()",
0054                         int, doc="number of b-hadrons"),
0055             nCHadrons=Var("jetFlavourInfo().getcHadrons().size()",
0056                         int, doc="number of c-hadrons"),
0057             )
0058         )
0059         process.btvGenTask.add(process.btvAK4JetExtTable)
0060 
0061     if addAK4:
0062         process.genJetsAK4Constituents = cms.EDProducer("GenJetPackedConstituentPtrSelector",
0063             src = cms.InputTag("slimmedGenJets"),
0064             cut = cms.string("pt > 20")
0065         )
0066         process.btvGenTask.add(process.genJetsAK4Constituents)
0067     if addAK8:
0068         process.genJetsAK8Constituents = cms.EDProducer("GenJetPackedConstituentPtrSelector",
0069             src = cms.InputTag("slimmedGenJetsAK8"),
0070             cut = cms.string("pt > 100.")
0071         )
0072         process.btvGenTask.add(process.genJetsAK8Constituents)
0073 
0074     if allPF:
0075         genCandInput = cms.InputTag("packedGenParticles")
0076     elif not addAK8:
0077         process.genJetsConstituentsTable = cms.EDProducer("PackedGenParticlePtrMerger", src = cms.VInputTag(cms.InputTag("genJetsAK4Constituents", "constituents")), skipNulls = cms.bool(True), warnOnSkip = cms.bool(True))
0078         genCandInput = cms.InputTag("genJetsConstituentsTable")
0079     elif not addAK4:
0080         process.genJetsConstituentsTable = cms.EDProducer("PackedGenParticlePtrMerger", src = cms.VInputTag(cms.InputTag("genJetsAK8Constituents", "constituents")), skipNulls = cms.bool(True), warnOnSkip = cms.bool(True))
0081         genCandInput = cms.InputTag("genJetsConstituentsTable")
0082     else:
0083         process.genJetsConstituentsTable = cms.EDProducer("PackedGenParticlePtrMerger", src = cms.VInputTag(cms.InputTag("genJetsAK4Constituents", "constituents"), cms.InputTag("genJetsAK8Constituents", "constituents")), skipNulls = cms.bool(True), warnOnSkip = cms.bool(True))
0084         genCandInput = cms.InputTag("genJetsConstituentsTable")
0085     if not allPF:
0086         process.btvGenTask.add(process.genJetsConstituentsTable)
0087 
0088     GenCandVars = CandVars.clone()
0089     GenCandVars.pdgId.doc = cms.string("PDG id")
0090 
0091     process.genCandsTable = cms.EDProducer("SimpleCandidateFlatTableProducer",
0092         src = genCandInput,
0093         cut = cms.string(""), #we should not filter after pruning
0094         name= cms.string("GenCands"),
0095         doc = cms.string("Final-state gen particles"),
0096         singleton = cms.bool(False), # the number of entries is variable
0097         extension = cms.bool(False), # this is the main table for the GEN constituents
0098         variables = cms.PSet(GenCandVars)
0099     )
0100     process.btvGenTask.add(process.genCandsTable)
0101 
0102     kwargs = { }
0103     import os
0104     sv_sort = os.getenv('CMSSW_NANOAOD_SV_SORT')
0105     if sv_sort is not None: kwargs['sv_sort'] = cms.untracked.string(sv_sort)
0106     pf_sort = os.getenv('CMSSW_NANOAOD_PF_SORT')
0107     if pf_sort is not None: kwargs['pf_sort'] = cms.untracked.string(pf_sort)
0108 
0109     if addAK4:
0110         process.genAK4ConstituentsTable = cms.EDProducer("GenJetConstituentTableProducer",
0111             candidates = genCandInput,
0112             jets = cms.InputTag("genJetsAK4Constituents"), # Note: The name has "Constituents" in it, but these are the jets
0113             name = cms.string("GenJetCands"),
0114             nameSV = cms.string("GenJetSVs"),
0115             idx_name = cms.string("genCandsIdx"),
0116             idx_nameSV = cms.string("sVIdx"),
0117             readBtag = cms.bool(False),
0118             **kwargs,
0119         )
0120         process.btvGenTask.add(process.genAK4ConstituentsTable)
0121     if addAK8:
0122         process.genAK8ConstituentsTable = cms.EDProducer("GenJetConstituentTableProducer",
0123             candidates = genCandInput,
0124             jets = cms.InputTag("genJetsAK8Constituents"), # Note: The name has "Constituents" in it, but these are the jets
0125             name = cms.string("GenFatJetCands"),
0126             nameSV = cms.string("GenFatJetSVs"),
0127             idx_name = cms.string("genCandsIdx"),
0128             idx_nameSV = cms.string("sVIdx"),
0129             readBtag = cms.bool(False),
0130             **kwargs,
0131         )
0132         process.btvGenTask.add(process.genAK8ConstituentsTable)
0133 
0134     process.genCandMotherTable = cms.EDProducer("GenCandMotherTableProducer",
0135         src     = genCandInput,
0136         mcMap   = cms.InputTag("finalGenParticles"),
0137         objName = cms.string("GenCands"),
0138         branchName = cms.string("genPart"),
0139     )
0140     process.btvGenTask.add(process.genCandMotherTable)
0141 
0142     if allPF:
0143         pfCandInput = cms.InputTag("packedPFCandidates")
0144     else:
0145         pfCandInput = cms.InputTag("finalJetsConstituentsTable")
0146 
0147     process.genCandMCMatchTable = cms.EDProducer("PackedCandMCMatchTableProducer",
0148         src = pfCandInput,
0149         genparticles = genCandInput,
0150         objName = cms.string("PFCands"),
0151         branchName = cms.string("genCand"),
0152         docString = cms.string("MC matching to status==1 genCands"),
0153     )
0154     process.btvGenTask.add(process.genCandMCMatchTable)
0155 
0156     return process