Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-04 01:26:11

0001 from __future__ import print_function
0002 import FWCore.ParameterSet.Config as cms
0003 from PhysicsTools.NanoAOD.common_cff import *
0004 from PhysicsTools.NanoAOD.nano_eras_cff import *
0005 from PhysicsTools.NanoAOD.jetsAK4_CHS_cff import *
0006 from PhysicsTools.NanoAOD.jetsAK4_Puppi_cff import *
0007 from PhysicsTools.NanoAOD.jetsAK8_cff import *
0008 from PhysicsTools.NanoAOD.jetMC_cff import *
0009 from PhysicsTools.NanoAOD.muons_cff import *
0010 from PhysicsTools.NanoAOD.taus_cff import *
0011 from PhysicsTools.NanoAOD.boostedTaus_cff import *
0012 from PhysicsTools.NanoAOD.electrons_cff import *
0013 from PhysicsTools.NanoAOD.lowPtElectrons_cff import *
0014 from PhysicsTools.NanoAOD.photons_cff import *
0015 from PhysicsTools.NanoAOD.globals_cff import *
0016 from PhysicsTools.NanoAOD.extraflags_cff import *
0017 from PhysicsTools.NanoAOD.ttbarCategorization_cff import *
0018 from PhysicsTools.NanoAOD.genparticles_cff import *
0019 from PhysicsTools.NanoAOD.particlelevel_cff import *
0020 from PhysicsTools.NanoAOD.genWeightsTable_cfi import *
0021 from PhysicsTools.NanoAOD.genVertex_cff import *
0022 from PhysicsTools.NanoAOD.vertices_cff import *
0023 from PhysicsTools.NanoAOD.met_cff import *
0024 from PhysicsTools.NanoAOD.triggerObjects_cff import *
0025 from PhysicsTools.NanoAOD.isotracks_cff import *
0026 from PhysicsTools.NanoAOD.protons_cff import *
0027 from PhysicsTools.NanoAOD.btagWeightTable_cff import *
0028 from PhysicsTools.NanoAOD.NanoAODEDMEventContent_cff import *
0029 from PhysicsTools.NanoAOD.fsrPhotons_cff import *
0030 from PhysicsTools.NanoAOD.softActivity_cff import *
0031 
0032 nanoMetadata = cms.EDProducer("UniqueStringProducer",
0033     strings = cms.PSet(
0034         tag = cms.string("untagged"),
0035     )
0036 )
0037 
0038 linkedObjects = cms.EDProducer("PATObjectCrossLinker",
0039    jets=cms.InputTag("finalJetsPuppi"),
0040    muons=cms.InputTag("finalMuons"),
0041    electrons=cms.InputTag("finalElectrons"),
0042    lowPtElectrons=cms.InputTag("finalLowPtElectrons"),
0043    taus=cms.InputTag("finalTaus"),
0044    photons=cms.InputTag("finalPhotons"),
0045 )
0046 
0047 # Switch to AK4 CHS jets for Run-2
0048 run2_nanoAOD_ANY.toModify(linkedObjects, jets="finalJets")
0049 
0050 simpleCleanerTable = cms.EDProducer("NanoAODSimpleCrossCleaner",
0051    name=cms.string("cleanmask"),
0052    doc=cms.string("simple cleaning mask with priority to leptons"),
0053    jets=cms.InputTag("linkedObjects","jets"),
0054    muons=cms.InputTag("linkedObjects","muons"),
0055    electrons=cms.InputTag("linkedObjects","electrons"),
0056    lowPtElectrons=cms.InputTag("linkedObjects","lowPtElectrons"),
0057    taus=cms.InputTag("linkedObjects","taus"),
0058    photons=cms.InputTag("linkedObjects","photons"),
0059    jetSel=cms.string("pt>15"),
0060    muonSel=cms.string("track.isNonnull && isLooseMuon && isPFMuon && innerTrack.validFraction >= 0.49 && ( isGlobalMuon && globalTrack.normalizedChi2 < 3 && combinedQuality.chi2LocalPosition < 12 && combinedQuality.trkKink < 20 && segmentCompatibility >= 0.303 || segmentCompatibility >= 0.451 )"),
0061    electronSel=cms.string(""),
0062    lowPtElectronSel=cms.string(""),
0063    tauSel=cms.string(""),
0064    photonSel=cms.string(""),
0065    jetName=cms.string("Jet"),muonName=cms.string("Muon"),electronName=cms.string("Electron"),
0066    lowPtElectronName=cms.string("LowPtElectron"),
0067    tauName=cms.string("Tau"),photonName=cms.string("Photon")
0068 )
0069 
0070 
0071 lhcInfoTable = cms.EDProducer("LHCInfoProducer",
0072     precision = cms.int32(10),
0073 )
0074 
0075 nanoTableTaskCommon = cms.Task(
0076     cms.Task(nanoMetadata), 
0077     jetPuppiTask, jetPuppiForMETTask, jetAK8Task,
0078     extraFlagsProducersTask, muonTask, tauTask, boostedTauTask,
0079     electronTask , lowPtElectronTask, photonTask,
0080     vertexTask, isoTrackTask, jetAK8LepTask,  # must be after all the leptons
0081     softActivityTask,
0082     cms.Task(linkedObjects),
0083     jetPuppiTablesTask, jetAK8TablesTask,
0084     muonTablesTask, fsrTablesTask, tauTablesTask, boostedTauTablesTask,
0085     electronTablesTask, lowPtElectronTablesTask, photonTablesTask,
0086     globalTablesTask, vertexTablesTask, metTablesTask, simpleCleanerTable, extraFlagsTableTask,
0087     isoTrackTablesTask,softActivityTablesTask
0088 )
0089 
0090 # Replace AK4 Puppi with AK4 CHS for Run-2
0091 _nanoTableTaskCommonRun2 = nanoTableTaskCommon.copy()
0092 _nanoTableTaskCommonRun2.replace(jetPuppiTask, jetTask)
0093 _nanoTableTaskCommonRun2.replace(jetPuppiForMETTask, jetForMETTask)
0094 _nanoTableTaskCommonRun2.replace(jetPuppiTablesTask, jetTablesTask)
0095 run2_nanoAOD_ANY.toReplaceWith(nanoTableTaskCommon, _nanoTableTaskCommonRun2)
0096 
0097 nanoSequenceCommon = cms.Sequence(nanoTableTaskCommon)
0098 
0099 nanoSequenceOnlyFullSim = cms.Sequence(triggerObjectTablesTask)
0100 nanoSequenceOnlyData = cms.Sequence(cms.Sequence(protonTablesTask) + lhcInfoTable)
0101 
0102 nanoSequence = cms.Sequence(nanoSequenceCommon + nanoSequenceOnlyData + nanoSequenceOnlyFullSim)
0103 
0104 nanoTableTaskFS = cms.Task(
0105     genParticleTask, particleLevelTask, jetMCTask, muonMCTask, electronMCTask, lowPtElectronMCTask, photonMCTask,
0106     tauMCTask, boostedTauMCTask,
0107     metMCTable, ttbarCatMCProducersTask, globalTablesMCTask, cms.Task(btagWeightTable), ttbarCategoryTableTask,
0108     genWeightsTableTask, genVertexTablesTask, genParticleTablesTask, genProtonTablesTask, particleLevelTablesTask
0109 )
0110 
0111 nanoSequenceFS = cms.Sequence(nanoSequenceCommon + cms.Sequence(nanoTableTaskFS))
0112 
0113 # GenVertex only stored in newer MiniAOD
0114 nanoSequenceMC = nanoSequenceFS.copy()
0115 nanoSequenceMC.insert(nanoSequenceFS.index(nanoSequenceCommon)+1,nanoSequenceOnlyFullSim)
0116 
0117 # modifier which adds new tauIDs (currently only deepTauId2017v2p1 is being added)
0118 import RecoTauTag.RecoTau.tools.runTauIdMVA as tauIdConfig
0119 def nanoAOD_addTauIds(process, idsToRun=[]):
0120     if idsToRun: #no-empty list of tauIDs to run
0121         updatedTauName = "slimmedTausUpdated"
0122         tauIdEmbedder = tauIdConfig.TauIDEmbedder(process, debug = False,
0123                                                   updatedTauName = updatedTauName,
0124             toKeep = idsToRun)
0125         tauIdEmbedder.runTauID()
0126         _tauTask = patTauMVAIDsTask.copy()
0127         _tauTask.add(process.rerunMvaIsolationTask)
0128         _tauTask.add(finalTaus)
0129         process.finalTaus.src = updatedTauName
0130         #remember to adjust the selection and tables with added IDs
0131 
0132         process.tauTask = _tauTask.copy()
0133 
0134     return process
0135 
0136 def nanoAOD_addBoostedTauIds(process, idsToRun=[]):
0137     if idsToRun: #no-empty list of tauIDs to run
0138         updatedBoostedTauName = "slimmedTausBoostedNewID"
0139         boostedTauIdEmbedder = tauIdConfig.TauIDEmbedder(process, debug = False,
0140                                                          originalTauName = "slimmedTausBoosted",
0141                                                          updatedTauName = updatedBoostedTauName,
0142                                                          postfix = "Boosted",
0143                                                          toKeep = idsToRun)
0144         boostedTauIdEmbedder.runTauID()
0145         _boostedTauTask = process.rerunMvaIsolationTaskBoosted.copy()
0146         _boostedTauTask.add(getattr(process, updatedBoostedTauName))
0147         _boostedTauTask.add(process.finalBoostedTaus)
0148         process.finalBoostedTaus.src = updatedBoostedTauName
0149         #remember to adjust the selection and tables with added IDs
0150 
0151         process.boostedTauTask = _boostedTauTask.copy()
0152 
0153     return process
0154 
0155 from PhysicsTools.PatUtils.tools.runMETCorrectionsAndUncertainties import runMetCorAndUncFromMiniAOD
0156 def nanoAOD_recalibrateMETs(process,isData):
0157     # add DeepMETs
0158     nanoAOD_DeepMET_switch = cms.PSet(
0159         ResponseTune_Graph = cms.untracked.string('RecoMET/METPUSubtraction/data/deepmet/deepmet_resp_v1_2018.pb')
0160     )
0161     for modifier in run2_miniAOD_80XLegacy, run2_nanoAOD_94X2016:
0162         modifier.toModify(nanoAOD_DeepMET_switch, ResponseTune_Graph=cms.untracked.string("RecoMET/METPUSubtraction/data/deepmet/deepmet_resp_v1_2016.pb"))
0163 
0164     print("add DeepMET Producers")
0165     process.load('RecoMET.METPUSubtraction.deepMETProducer_cfi')
0166     process.deepMETsResolutionTune = process.deepMETProducer.clone()
0167     process.deepMETsResponseTune = process.deepMETProducer.clone()
0168     process.deepMETsResponseTune.graph_path = nanoAOD_DeepMET_switch.ResponseTune_Graph.value()
0169 
0170     runMetCorAndUncFromMiniAOD(process,isData=isData)
0171     process.nanoSequenceCommon.insert(2,cms.Sequence(process.fullPatMetSequence))
0172 
0173 
0174     from PhysicsTools.PatAlgos.slimming.puppiForMET_cff import makePuppiesFromMiniAOD
0175     makePuppiesFromMiniAOD(process,True)
0176     process.puppiNoLep.useExistingWeights = True
0177     process.puppi.useExistingWeights = True
0178     run2_nanoAOD_106Xv1.toModify(process.puppiNoLep, useExistingWeights = False)
0179     run2_nanoAOD_106Xv1.toModify(process.puppi, useExistingWeights = False)
0180     print("will make Puppies on top of MINIAOD")
0181 
0182 #    makePuppiesFromMiniAOD(process,True) # call this before in the global customizer otherwise it would reset photon IDs in VID
0183     nanoAOD_PuppiV15_switch = cms.PSet(
0184             recoMetFromPFCs = cms.untracked.bool(False),
0185             reclusterJets = cms.untracked.bool(False),
0186             )
0187     run2_nanoAOD_106Xv1.toModify(nanoAOD_PuppiV15_switch,recoMetFromPFCs=True,reclusterJets=True)
0188     if nanoAOD_PuppiV15_switch.reclusterJets:
0189         from RecoJets.JetProducers.ak4PFJets_cfi import ak4PFJets
0190         from PhysicsTools.PatAlgos.tools.helpers import getPatAlgosToolsTask, addToProcessAndTask
0191         task = getPatAlgosToolsTask(process)
0192         addToProcessAndTask('ak4PuppiJets', ak4PFJets.clone (src = 'puppi', doAreaFastjet = True, jetPtMin = 10.), process, task)
0193         from PhysicsTools.PatAlgos.tools.jetTools import addJetCollection
0194         addJetCollection(process,
0195             labelName = 'Puppi',
0196             jetSource = cms.InputTag('ak4PuppiJets'),
0197             algo = 'AK', rParam=0.4,
0198             genJetCollection=cms.InputTag('slimmedGenJets'),
0199             jetCorrections = ('AK4PFPuppi', ['L1FastJet', 'L2Relative', 'L3Absolute','L2L3Residual'], 'None'),
0200             pfCandidates = cms.InputTag('packedPFCandidates'),
0201             pvSource = cms.InputTag('offlineSlimmedPrimaryVertices'),
0202             svSource = cms.InputTag('slimmedSecondaryVertices'),
0203             muSource =cms.InputTag( 'slimmedMuons'),
0204             elSource = cms.InputTag('slimmedElectrons'),
0205             genParticles= cms.InputTag('prunedGenParticles'),
0206             getJetMCFlavour= False
0207         )
0208 
0209         process.patJetsPuppi.addGenPartonMatch = cms.bool(False)
0210         process.patJetsPuppi.addGenJetMatch = cms.bool(False)
0211 
0212         print("nanoAOD_PuppiV15_switch.reclusterJets is true")
0213 
0214     runMetCorAndUncFromMiniAOD(process,isData=isData,metType="Puppi",postfix="Puppi",jetFlavor="AK4PFPuppi", recoMetFromPFCs=bool(nanoAOD_PuppiV15_switch.recoMetFromPFCs), reclusterJets=bool(nanoAOD_PuppiV15_switch.reclusterJets))
0215     process.nanoSequenceCommon.insert(2,cms.Sequence(process.puppiMETSequence+process.fullPatMetSequencePuppi))
0216 
0217     return process
0218 
0219 from PhysicsTools.SelectorUtils.tools.vid_id_tools import *
0220 def nanoAOD_activateVID(process):
0221 
0222     switchOnVIDElectronIdProducer(process,DataFormat.MiniAOD,electronTask)
0223     for modname in electron_id_modules_WorkingPoints_nanoAOD.modules:
0224         setupAllVIDIdsInModule(process,modname,setupVIDElectronSelection)
0225 
0226     electronTask_ = process.egmGsfElectronIDTask.copy()
0227     electronTask_.add(electronTask.copy())
0228     process.electronTask = electronTask_.copy()
0229     for modifier in run2_miniAOD_80XLegacy,run2_nanoAOD_94XMiniAODv1,run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_94X2016,run2_nanoAOD_102Xv1,run2_nanoAOD_106Xv1:
0230         modifier.toModify(process.electronMVAValueMapProducer, src = "slimmedElectronsUpdated")
0231         modifier.toModify(process.egmGsfElectronIDs, physicsObjectSrc = "slimmedElectronsUpdated")
0232 
0233     switchOnVIDPhotonIdProducer(process,DataFormat.MiniAOD,photonTask) # do not call this to avoid resetting photon IDs in VID, if called before inside makePuppiesFromMiniAOD
0234     for modname in photon_id_modules_WorkingPoints_nanoAOD.modules:
0235         setupAllVIDIdsInModule(process,modname,setupVIDPhotonSelection)
0236 
0237     photonTask_ = process.egmPhotonIDTask.copy()
0238     photonTask_.add(photonTask.copy())
0239     process.photonTask = photonTask_.copy()
0240     for modifier in run2_miniAOD_80XLegacy,run2_nanoAOD_94XMiniAODv1,run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_94X2016,run2_nanoAOD_102Xv1:
0241         modifier.toModify(process.photonMVAValueMapProducer, src = "slimmedPhotonsTo106X")
0242         modifier.toModify(process.egmPhotonIDs, physicsObjectSrc = "slimmedPhotonsTo106X")
0243     return process
0244 
0245 from PhysicsTools.PatUtils.tools.runMETCorrectionsAndUncertainties import runMetCorAndUncFromMiniAOD
0246 def nanoAOD_runMETfixEE2017(process,isData):
0247     runMetCorAndUncFromMiniAOD(process,isData=isData,
0248                                fixEE2017 = True,
0249                                fixEE2017Params = {'userawPt': True, 'ptThreshold':50.0, 'minEtaThreshold':2.65, 'maxEtaThreshold': 3.139},
0250                                postfix = "FixEE2017")
0251     process.nanoSequenceCommon.insert(2,process.fullPatMetSequenceFixEE2017)
0252 
0253 
0254 def nanoAOD_customizeCommon(process):
0255 
0256     process = nanoAOD_activateVID(process)
0257 
0258     # This function is defined in jetsAK4_CHS_cff.py
0259     process = nanoAOD_addDeepInfoAK4CHS(process,
0260         addDeepBTag=nanoAOD_addDeepInfoAK4CHS_switch.nanoAOD_addDeepBTag_switch,
0261         addDeepFlavour=nanoAOD_addDeepInfoAK4CHS_switch.nanoAOD_addDeepFlavourTag_switch
0262     )
0263 
0264     # This function is defined in jetsAK8_cff.py
0265     process = nanoAOD_addDeepInfoAK8(process,
0266         addDeepBTag=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepBTag_switch,
0267         addDeepBoostedJet=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepBoostedJet_switch,
0268         addDeepDoubleX=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepDoubleX_switch,
0269         addDeepDoubleXV2=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepDoubleXV2_switch,
0270         addParticleNet=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addParticleNet_switch,
0271         addParticleNetMass=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addParticleNetMass_switch,
0272         jecPayload=nanoAOD_addDeepInfoAK8_switch.jecPayload
0273     )
0274 
0275     nanoAOD_tau_switch = cms.PSet(
0276         idsToAdd = cms.vstring()
0277     )
0278     (run2_nanoAOD_94XMiniAODv1 | run2_nanoAOD_94X2016 | run2_nanoAOD_94XMiniAODv2 | run2_nanoAOD_102Xv1 | run2_nanoAOD_106Xv1).toModify(nanoAOD_tau_switch, idsToAdd = ["deepTau2017v2p1"])
0279     (run2_nanoAOD_94XMiniAODv1 | run2_nanoAOD_94X2016 | run2_nanoAOD_94XMiniAODv2 | run2_nanoAOD_102Xv1 | run2_nanoAOD_106Xv1).toModify(process, lambda p : nanoAOD_addTauIds(p, nanoAOD_tau_switch.idsToAdd.value()))
0280     nanoAOD_boostedTau_switch = cms.PSet(
0281         idsToAdd = cms.vstring()
0282     )
0283     run2_nanoAOD_106Xv2.toModify(nanoAOD_boostedTau_switch, idsToAdd = ["2017v2", "dR0p32017v2", "newDM2017v2","againstEle2018"])
0284     run2_nanoAOD_106Xv2.toModify(process, lambda p : nanoAOD_addBoostedTauIds(p, nanoAOD_boostedTau_switch.idsToAdd.value()))
0285 
0286     return process
0287 
0288 def nanoAOD_customizeData(process):
0289     process = nanoAOD_customizeCommon(process)
0290     for modifier in run2_miniAOD_80XLegacy,run2_nanoAOD_94X2016,run2_nanoAOD_94XMiniAODv1,run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_102Xv1,run2_nanoAOD_106Xv1:
0291         modifier.toModify(process, lambda p: nanoAOD_recalibrateMETs(p,isData=True))
0292     for modifier in run2_nanoAOD_94XMiniAODv1, run2_nanoAOD_94XMiniAODv2:
0293         modifier.toModify(process, lambda p: nanoAOD_runMETfixEE2017(p,isData=True))
0294     return process
0295 
0296 def nanoAOD_customizeMC(process):
0297     process = nanoAOD_customizeCommon(process)
0298     for modifier in run2_miniAOD_80XLegacy,run2_nanoAOD_94X2016,run2_nanoAOD_94XMiniAODv1,run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_102Xv1,run2_nanoAOD_106Xv1:
0299         modifier.toModify(process, lambda p: nanoAOD_recalibrateMETs(p,isData=False))
0300     for modifier in run2_nanoAOD_94XMiniAODv1, run2_nanoAOD_94XMiniAODv2:
0301         modifier.toModify(process, lambda p: nanoAOD_runMETfixEE2017(p,isData=False))
0302     return process
0303 
0304 ###increasing the precision of selected GenParticles.
0305 def nanoWmassGenCustomize(process):
0306     pdgSelection="?(abs(pdgId) == 11|| abs(pdgId)==13 || abs(pdgId)==15 ||abs(pdgId)== 12 || abs(pdgId)== 14 || abs(pdgId)== 16|| abs(pdgId)== 24|| pdgId== 23)"
0307     # Keep precision same as default RECO for selected particles
0308     ptPrecision="{}?{}:{}".format(pdgSelection, CandVars.pt.precision.value(),genParticleTable.variables.pt.precision.value())
0309     process.genParticleTable.variables.pt.precision=cms.string(ptPrecision)
0310     phiPrecision="{} ? {} : {}".format(pdgSelection, CandVars.phi.precision.value(), genParticleTable.variables.phi.precision.value())
0311     process.genParticleTable.variables.phi.precision=cms.string(phiPrecision)
0312     etaPrecision="{} ? {} : {}".format(pdgSelection, CandVars.eta.precision.value(), genParticleTable.variables.eta.precision.value())
0313     process.genParticleTable.variables.eta.precision=cms.string(etaPrecision)
0314     return process
0315 
0316 # lowPtElectrons do not exsit for old nano campaigns (i.e. before v9)
0317 _modifiers = ( run2_miniAOD_80XLegacy |
0318                run2_nanoAOD_94XMiniAODv1 |
0319                run2_nanoAOD_94XMiniAODv2 |
0320                run2_nanoAOD_94X2016 |
0321                run2_nanoAOD_102Xv1 |
0322                run2_nanoAOD_106Xv1 )
0323 _modifiers.toModify(linkedObjects,lowPtElectrons="")
0324 _modifiers.toModify(simpleCleanerTable,lowPtElectrons="")