Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:51

0001 import FWCore.ParameterSet.Config as cms
0002 from FWCore.ParameterSet.VarParsing import VarParsing
0003 from PhysicsTools.PatAlgos.tools.helpers import getPatAlgosToolsTask
0004 import sys
0005 import os
0006 
0007 
0008 ## Define the process
0009 process = cms.Process("Filter")
0010 patAlgosToolsTask = getPatAlgosToolsTask(process)
0011 process.options = cms.untracked.PSet(
0012     wantSummary = cms.untracked.bool(True)
0013 )
0014 ## Set up command line options
0015 options = VarParsing ('analysis')
0016 options.register('runOnGenOrAODsim', False, VarParsing.multiplicity.singleton, VarParsing.varType.bool, "GEN SIM")
0017 options.register( "skipEvents", 0, VarParsing.multiplicity.singleton, VarParsing.varType.int, "Number of events to skip" )
0018 options.parseArguments()
0019 
0020 
0021 ## Configure message logger
0022 process.load("FWCore.MessageLogger.MessageLogger_cfi")
0023 process.MessageLogger.cerr.threshold = 'INFO'
0024 process.MessageLogger.cerr.FwkReport.reportEvery = 100
0025 
0026 ## Define input
0027 if not options.inputFiles:
0028     if options.runOnGenOrAODsim:
0029         options.inputFiles=['/store/mc/RunIISummer15GS/TTToSemiLeptonic_TuneCUETP8M1_alphaS01273_13TeV-powheg-scaledown-pythia8/GEN-SIM/MCRUN2_71_V1-v2/40000/DE7952A2-6E2F-E611-A803-001E673D1B21.root']
0030     else:
0031         options.inputFiles=['/store/mc/RunIIFall17MiniAOD/TTToSemiLeptonic_TuneCP5_PSweights_13TeV-powheg-pythia8/MINIAODSIM/94X_mc2017_realistic_v10-v1/50000/DC5D3109-F2E1-E711-A26E-A0369FC5FC9C.root']
0032 
0033 ## Define maximum number of events to loop over
0034 if options.maxEvents is -1: # maxEvents is set in VarParsing class by default to -1
0035     options.maxEvents = 1000 # reset for testing
0036 
0037 process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(int(options.maxEvents)))
0038 process.source = cms.Source(  "PoolSource",
0039                               fileNames = cms.untracked.vstring(options.inputFiles),
0040                               skipEvents=cms.untracked.uint32(int(options.skipEvents)),
0041 )
0042 
0043 ## Set input particle collections to be used by the tools
0044 genParticleCollection = ''
0045 genJetInputParticleCollection = ''
0046 genJetCollection = 'ak4GenJetsCustom'
0047 
0048 if options.runOnGenOrAODsim:
0049     genParticleCollection = 'genParticles'
0050     genJetInputParticleCollection = genParticleCollection
0051 else:
0052     genParticleCollection = 'prunedGenParticles'
0053     genJetInputParticleCollection = 'packedGenParticles'
0054 
0055 ## Supplies PDG ID to real name resolution of MC particles
0056 process.load("SimGeneral.HepPDTESSource.pythiapdt_cfi")
0057 ## producing a subset of particles to be used for jet clustering
0058 
0059 from RecoJets.Configuration.GenJetParticles_cff import genParticlesForJetsNoNu
0060 process.genParticlesForJetsNoNu = genParticlesForJetsNoNu.clone(
0061     src = genJetInputParticleCollection
0062 )
0063 patAlgosToolsTask.add(process.genParticlesForJetsNoNu)
0064 
0065 ## Produce own jets (re-clustering in miniAOD needed at present to avoid crash)
0066 from RecoJets.JetProducers.ak4GenJets_cfi import ak4GenJets
0067 process.ak4GenJetsCustom = ak4GenJets.clone(
0068     src = 'genParticlesForJetsNoNu',
0069     rParam = cms.double(0.4),
0070     jetAlgorithm = cms.string("AntiKt")
0071 )
0072 patAlgosToolsTask.add(process.ak4GenJetsCustom)
0073 
0074 ## Ghost particle collection used for Hadron-Jet association
0075 # MUST use proper input particle collection
0076 from PhysicsTools.JetMCAlgos.HadronAndPartonSelector_cfi import selectedHadronsAndPartons
0077 process.selectedHadronsAndPartons = selectedHadronsAndPartons.clone(
0078     particles = genParticleCollection,
0079 )
0080 patAlgosToolsTask.add(process.selectedHadronsAndPartons)
0081 #Input particle collection for matching to gen jets (partons + leptons)
0082 # MUST use use proper input jet collection: the jets to which hadrons should be associated
0083 # rParam and jetAlgorithm MUST match those used for jets to be associated with hadrons
0084 # More details on the tool: https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideBTagMCTools#New_jet_flavour_definition
0085 from PhysicsTools.JetMCAlgos.AK4PFJetsMCFlavourInfos_cfi import ak4JetFlavourInfos
0086 process.genJetFlavourInfos = ak4JetFlavourInfos.clone(
0087     jets = genJetCollection,
0088 )
0089 patAlgosToolsTask.add(process.genJetFlavourInfos)
0090 
0091 # Plugin for analysing B hadrons
0092 # MUST use the same particle collection as in selectedHadronsAndPartons
0093 from PhysicsTools.JetMCAlgos.GenHFHadronMatcher_cff import matchGenBHadron
0094 process.matchGenBHadron = matchGenBHadron.clone(
0095     genParticles = genParticleCollection,
0096     jetFlavourInfos = "genJetFlavourInfos",
0097     onlyJetClusteredHadrons = cms.bool(False)
0098 )
0099 
0100 # Plugin for analysing C hadrons
0101 # MUST use the same particle collection as in selectedHadronsAndPartons
0102 #from PhysicsTools.JetMCAlgos.GenHFHadronMatcher_cff import matchGenCHadron
0103 #process.matchGenCHadron = matchGenCHadron.clone(
0104 #    genParticles = genParticleCollection,
0105 #    jetFlavourInfos = "genJetFlavourInfos"
0106 #)
0107 
0108 process.load("PhysicsTools/JetMCAlgos/ttHFGenFilter_cfi")
0109 from PhysicsTools.JetMCAlgos.ttHFGenFilter_cfi import ttHFGenFilter
0110 process.ttHFGenFilter = ttHFGenFilter.clone(
0111     genParticles = genParticleCollection,
0112     taggingMode  = cms.bool(True),
0113 )
0114 print("If taggingMode is set to true, the filter will write a branch into the tree instead of filtering the events")
0115 print("taggingMode is set to ", process.ttHFGenFilter.taggingMode)
0116 
0117 
0118 
0119 ## configuring the testing analyzer that produces output tree
0120 #process.matchGenHFHadrons = cms.EDAnalyzer("matchGenHFHadrons",
0121 #    # phase space of jets to be stored
0122 #    genJetPtMin = cms.double(15),
0123 #    genJetAbsEtaMax = cms.double(2.4),
0124 #    # input tags holding information about matching
0125 #    genJets = cms.InputTag(genJetCollection),
0126 #    genBHadJetIndex = cms.InputTag("matchGenBHadron", "genBHadJetIndex"),
0127 #    genBHadFlavour = cms.InputTag("matchGenBHadron", "genBHadFlavour"),
0128 #    genBHadFromTopWeakDecay = cms.InputTag("matchGenBHadron", "genBHadFromTopWeakDecay"),
0129 #    genBHadPlusMothers = cms.InputTag("matchGenBHadron", "genBHadPlusMothers"),
0130 #    genBHadPlusMothersIndices = cms.InputTag("matchGenBHadron", "genBHadPlusMothersIndices"),
0131 #    genBHadIndex = cms.InputTag("matchGenBHadron", "genBHadIndex"),
0132 #    genBHadLeptonHadronIndex = cms.InputTag("matchGenBHadron", "genBHadLeptonHadronIndex"),
0133 #    genBHadLeptonViaTau = cms.InputTag("matchGenBHadron", "genBHadLeptonViaTau"),
0134 #    genCHadJetIndex = cms.InputTag("matchGenCHadron", "genCHadJetIndex"),
0135 #    genCHadFlavour = cms.InputTag("matchGenCHadron", "genCHadFlavour"),
0136 #    genCHadFromTopWeakDecay = cms.InputTag("matchGenCHadron", "genCHadFromTopWeakDecay"),
0137 #    genCHadBHadronId = cms.InputTag("matchGenCHadron", "genCHadBHadronId"),
0138 #    genCHadPlusMothers = cms.InputTag("matchGenCHadron", "genCHadPlusMothers"),
0139 #    genCHadPlusMothersIndices = cms.InputTag("matchGenCHadron", "genCHadPlusMothersIndices"),
0140 #    genCHadIndex = cms.InputTag("matchGenCHadron", "genCHadIndex"),
0141 #    genCHadLeptonHadronIndex = cms.InputTag("matchGenCHadron", "genCHadLeptonHadronIndex"),
0142 #    genCHadLeptonViaTau = cms.InputTag("matchGenCHadron", "genCHadLeptonViaTau"),
0143 #)
0144 
0145 ## Configure test filter
0146 #process.ttHFGenFilter = cms.EDFilter("ttHFGenFilter",
0147 #    genBHadFlavour = cms.InputTag("matchGenBHadron", "genBHadFlavour"),
0148 #    genBHadFromTopWeakDecay = cms.InputTag("matchGenBHadron", "genBHadFromTopWeakDecay"),
0149 #    genBHadPlusMothers = cms.InputTag("matchGenBHadron", "genBHadPlusMothers"),
0150 #    genBHadPlusMothersIndices = cms.InputTag("matchGenBHadron", "genBHadPlusMothersIndices"),
0151 #    genBHadIndex = cms.InputTag("matchGenBHadron", "genBHadIndex"),
0152 #    filter = cms.bool(True)
0153 #)
0154 process.USER = cms.OutputModule("PoolOutputModule",
0155     SelectEvents = cms.untracked.PSet(
0156         SelectEvents = cms.vstring('p1')
0157     ),
0158     fileName = cms.untracked.string('Filtered_Events.root')
0159 )
0160 ## Output root file
0161 #process.TFileService = cms.Service("TFileService",
0162 #    fileName = cms.string("genHFHadronMatcherOutput.root")
0163 #)
0164 
0165 ## Path
0166 process.p1 = cms.Path(
0167     process.matchGenBHadron*
0168     process.ttHFGenFilter
0169 )
0170 
0171 process.endpath = cms.EndPath(process.USER, patAlgosToolsTask)