CMS 3D CMS Logo

nano_cff.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import FWCore.ParameterSet.Config as cms
23 from PhysicsTools.NanoAOD.met_cff import *
31 
32 nanoMetadata = cms.EDProducer("UniqueStringProducer",
33  strings = cms.PSet(
34  tag = cms.string("untagged"),
35  )
36 )
37 
38 linkedObjects = cms.EDProducer("PATObjectCrossLinker",
39  jets=cms.InputTag("finalJetsPuppi"),
40  muons=cms.InputTag("finalMuons"),
41  electrons=cms.InputTag("finalElectrons"),
42  lowPtElectrons=cms.InputTag("finalLowPtElectrons"),
43  taus=cms.InputTag("finalTaus"),
44  photons=cms.InputTag("finalPhotons"),
45 )
46 
47 # Switch to AK4 CHS jets for Run-2
48 run2_nanoAOD_ANY.toModify(linkedObjects, jets="finalJets")
49 
50 simpleCleanerTable = cms.EDProducer("NanoAODSimpleCrossCleaner",
51  name=cms.string("cleanmask"),
52  doc=cms.string("simple cleaning mask with priority to leptons"),
53  jets=cms.InputTag("linkedObjects","jets"),
54  muons=cms.InputTag("linkedObjects","muons"),
55  electrons=cms.InputTag("linkedObjects","electrons"),
56  lowPtElectrons=cms.InputTag("linkedObjects","lowPtElectrons"),
57  taus=cms.InputTag("linkedObjects","taus"),
58  photons=cms.InputTag("linkedObjects","photons"),
59  jetSel=cms.string("pt>15"),
60  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 )"),
61  electronSel=cms.string(""),
62  lowPtElectronSel=cms.string(""),
63  tauSel=cms.string(""),
64  photonSel=cms.string(""),
65  jetName=cms.string("Jet"),muonName=cms.string("Muon"),electronName=cms.string("Electron"),
66  lowPtElectronName=cms.string("LowPtElectron"),
67  tauName=cms.string("Tau"),photonName=cms.string("Photon")
68 )
69 
70 
71 lhcInfoTable = cms.EDProducer("LHCInfoProducer",
72  precision = cms.int32(10),
73 )
74 
75 nanoTableTaskCommon = cms.Task(
76  cms.Task(nanoMetadata),
77  jetPuppiTask, jetPuppiForMETTask, jetAK8Task,
78  extraFlagsProducersTask, muonTask, tauTask, boostedTauTask,
79  electronTask , lowPtElectronTask, photonTask,
80  vertexTask, isoTrackTask, jetAK8LepTask, # must be after all the leptons
81  softActivityTask,
82  cms.Task(linkedObjects),
83  jetPuppiTablesTask, jetAK8TablesTask,
84  muonTablesTask, fsrTablesTask, tauTablesTask, boostedTauTablesTask,
85  electronTablesTask, lowPtElectronTablesTask, photonTablesTask,
86  globalTablesTask, vertexTablesTask, metTablesTask, simpleCleanerTable, extraFlagsTableTask,
87  isoTrackTablesTask,softActivityTablesTask
88 )
89 
90 # Replace AK4 Puppi with AK4 CHS for Run-2
91 _nanoTableTaskCommonRun2 = nanoTableTaskCommon.copy()
92 _nanoTableTaskCommonRun2.replace(jetPuppiTask, jetTask)
93 _nanoTableTaskCommonRun2.replace(jetPuppiForMETTask, jetForMETTask)
94 _nanoTableTaskCommonRun2.replace(jetPuppiTablesTask, jetTablesTask)
95 run2_nanoAOD_ANY.toReplaceWith(nanoTableTaskCommon, _nanoTableTaskCommonRun2)
96 
97 nanoSequenceCommon = cms.Sequence(nanoTableTaskCommon)
98 
99 nanoSequenceOnlyFullSim = cms.Sequence(triggerObjectTablesTask)
100 nanoSequenceOnlyData = cms.Sequence(cms.Sequence(protonTablesTask) + lhcInfoTable)
101 
102 nanoSequence = cms.Sequence(nanoSequenceCommon + nanoSequenceOnlyData + nanoSequenceOnlyFullSim)
103 
104 nanoTableTaskFS = cms.Task(
105  genParticleTask, particleLevelTask, jetMCTask, muonMCTask, electronMCTask, lowPtElectronMCTask, photonMCTask,
106  tauMCTask, boostedTauMCTask,
107  metMCTable, ttbarCatMCProducersTask, globalTablesMCTask, cms.Task(btagWeightTable), ttbarCategoryTableTask,
108  genWeightsTableTask, genVertexTablesTask, genParticleTablesTask, genProtonTablesTask, particleLevelTablesTask
109 )
110 
111 nanoSequenceFS = cms.Sequence(nanoSequenceCommon + cms.Sequence(nanoTableTaskFS))
112 
113 # GenVertex only stored in newer MiniAOD
114 nanoSequenceMC = nanoSequenceFS.copy()
115 nanoSequenceMC.insert(nanoSequenceFS.index(nanoSequenceCommon)+1,nanoSequenceOnlyFullSim)
116 
117 # modifier which adds new tauIDs (currently only deepTauId2017v2p1 is being added)
118 import RecoTauTag.RecoTau.tools.runTauIdMVA as tauIdConfig
119 def nanoAOD_addTauIds(process, idsToRun=[]):
120  if idsToRun: #no-empty list of tauIDs to run
121  updatedTauName = "slimmedTausUpdated"
122  tauIdEmbedder = tauIdConfig.TauIDEmbedder(process, debug = False,
123  updatedTauName = updatedTauName,
124  postfix = "ForNano",
125  toKeep = idsToRun)
126  tauIdEmbedder.runTauID()
127  _tauTask = patTauMVAIDsTask.copy()
128  _tauTask.add(process.rerunMvaIsolationTaskForNano)
129  _tauTask.add(finalTaus)
130  process.finalTaus.src = updatedTauName
131  #remember to adjust the selection and tables with added IDs
132 
133  process.tauTask = _tauTask.copy()
134 
135  return process
136 
137 def nanoAOD_addBoostedTauIds(process, idsToRun=[]):
138  if idsToRun: #no-empty list of tauIDs to run
139  updatedBoostedTauName = "slimmedTausBoostedNewID"
140  boostedTauIdEmbedder = tauIdConfig.TauIDEmbedder(process, debug = False,
141  originalTauName = "slimmedTausBoosted",
142  updatedTauName = updatedBoostedTauName,
143  postfix = "BoostedForNano",
144  toKeep = idsToRun)
145  boostedTauIdEmbedder.runTauID()
146  _boostedTauTask = process.rerunMvaIsolationTaskBoostedForNano.copy()
147  _boostedTauTask.add(getattr(process, updatedBoostedTauName))
148  _boostedTauTask.add(process.finalBoostedTaus)
149  process.finalBoostedTaus.src = updatedBoostedTauName
150  #remember to adjust the selection and tables with added IDs
151 
152  process.boostedTauTask = _boostedTauTask.copy()
153 
154  return process
155 
156 from PhysicsTools.PatUtils.tools.runMETCorrectionsAndUncertainties import runMetCorAndUncFromMiniAOD
157 def nanoAOD_recalibrateMETs(process,isData):
158  # add DeepMETs
159  nanoAOD_DeepMET_switch = cms.PSet(
160  ResponseTune_Graph = cms.untracked.string('RecoMET/METPUSubtraction/data/deepmet/deepmet_resp_v1_2018.pb')
161  )
162  for modifier in run2_miniAOD_80XLegacy, run2_nanoAOD_94X2016:
163  modifier.toModify(nanoAOD_DeepMET_switch, ResponseTune_Graph=cms.untracked.string("RecoMET/METPUSubtraction/data/deepmet/deepmet_resp_v1_2016.pb"))
164 
165  print("add DeepMET Producers")
166  process.load('RecoMET.METPUSubtraction.deepMETProducer_cfi')
167  process.deepMETsResolutionTune = process.deepMETProducer.clone()
168  process.deepMETsResponseTune = process.deepMETProducer.clone()
169  process.deepMETsResponseTune.graph_path = nanoAOD_DeepMET_switch.ResponseTune_Graph.value()
170 
171  runMetCorAndUncFromMiniAOD(process,isData=isData)
172  process.nanoSequenceCommon.insert(2,cms.Sequence(process.fullPatMetSequence))
173 
174 
175  from PhysicsTools.PatAlgos.slimming.puppiForMET_cff import makePuppiesFromMiniAOD
176  makePuppiesFromMiniAOD(process,True)
177  process.puppiNoLep.useExistingWeights = True
178  process.puppi.useExistingWeights = True
179  run2_nanoAOD_106Xv1.toModify(process.puppiNoLep, useExistingWeights = False)
180  run2_nanoAOD_106Xv1.toModify(process.puppi, useExistingWeights = False)
181  print("will make Puppies on top of MINIAOD")
182 
183 # makePuppiesFromMiniAOD(process,True) # call this before in the global customizer otherwise it would reset photon IDs in VID
184  nanoAOD_PuppiV15_switch = cms.PSet(
185  recoMetFromPFCs = cms.untracked.bool(False),
186  reclusterJets = cms.untracked.bool(False),
187  )
188  run2_nanoAOD_106Xv1.toModify(nanoAOD_PuppiV15_switch,recoMetFromPFCs=True,reclusterJets=True)
189  if nanoAOD_PuppiV15_switch.reclusterJets:
190  from RecoJets.JetProducers.ak4PFJets_cfi import ak4PFJets
191  from PhysicsTools.PatAlgos.tools.helpers import getPatAlgosToolsTask, addToProcessAndTask
192  task = getPatAlgosToolsTask(process)
193  addToProcessAndTask('ak4PuppiJets', ak4PFJets.clone (src = 'puppi', doAreaFastjet = True, jetPtMin = 10.), process, task)
194  from PhysicsTools.PatAlgos.tools.jetTools import addJetCollection
195  addJetCollection(process,
196  labelName = 'Puppi',
197  jetSource = cms.InputTag('ak4PuppiJets'),
198  algo = 'AK', rParam=0.4,
199  genJetCollection=cms.InputTag('slimmedGenJets'),
200  jetCorrections = ('AK4PFPuppi', ['L1FastJet', 'L2Relative', 'L3Absolute','L2L3Residual'], 'None'),
201  pfCandidates = cms.InputTag('packedPFCandidates'),
202  pvSource = cms.InputTag('offlineSlimmedPrimaryVertices'),
203  svSource = cms.InputTag('slimmedSecondaryVertices'),
204  muSource =cms.InputTag( 'slimmedMuons'),
205  elSource = cms.InputTag('slimmedElectrons'),
206  genParticles= cms.InputTag('prunedGenParticles'),
207  getJetMCFlavour= False
208  )
209 
210  process.patJetsPuppi.addGenPartonMatch = cms.bool(False)
211  process.patJetsPuppi.addGenJetMatch = cms.bool(False)
212 
213  print("nanoAOD_PuppiV15_switch.reclusterJets is true")
214 
215  runMetCorAndUncFromMiniAOD(process,isData=isData,metType="Puppi",postfix="Puppi",jetFlavor="AK4PFPuppi", recoMetFromPFCs=bool(nanoAOD_PuppiV15_switch.recoMetFromPFCs), reclusterJets=bool(nanoAOD_PuppiV15_switch.reclusterJets))
216  process.nanoSequenceCommon.insert(2,cms.Sequence(process.puppiMETSequence+process.fullPatMetSequencePuppi))
217 
218  return process
219 
221 def nanoAOD_activateVID(process):
222 
223  switchOnVIDElectronIdProducer(process,DataFormat.MiniAOD,electronTask)
224  for modname in electron_id_modules_WorkingPoints_nanoAOD.modules:
225  setupAllVIDIdsInModule(process,modname,setupVIDElectronSelection)
226 
227  electronTask_ = process.egmGsfElectronIDTask.copy()
228  electronTask_.add(electronTask.copy())
229  process.electronTask = electronTask_.copy()
230  for modifier in run2_miniAOD_80XLegacy,run2_nanoAOD_94XMiniAODv1,run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_94X2016,run2_nanoAOD_102Xv1,run2_nanoAOD_106Xv1:
231  modifier.toModify(process.electronMVAValueMapProducer, src = "slimmedElectronsUpdated")
232  modifier.toModify(process.egmGsfElectronIDs, physicsObjectSrc = "slimmedElectronsUpdated")
233 
234  switchOnVIDPhotonIdProducer(process,DataFormat.MiniAOD,photonTask) # do not call this to avoid resetting photon IDs in VID, if called before inside makePuppiesFromMiniAOD
235  for modname in photon_id_modules_WorkingPoints_nanoAOD.modules:
236  setupAllVIDIdsInModule(process,modname,setupVIDPhotonSelection)
237 
238  photonTask_ = process.egmPhotonIDTask.copy()
239  photonTask_.add(photonTask.copy())
240  process.photonTask = photonTask_.copy()
241  for modifier in run2_miniAOD_80XLegacy,run2_nanoAOD_94XMiniAODv1,run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_94X2016,run2_nanoAOD_102Xv1:
242  modifier.toModify(process.photonMVAValueMapProducer, src = "slimmedPhotonsTo106X")
243  modifier.toModify(process.egmPhotonIDs, physicsObjectSrc = "slimmedPhotonsTo106X")
244  return process
245 
246 from PhysicsTools.PatUtils.tools.runMETCorrectionsAndUncertainties import runMetCorAndUncFromMiniAOD
247 def nanoAOD_runMETfixEE2017(process,isData):
248  runMetCorAndUncFromMiniAOD(process,isData=isData,
249  fixEE2017 = True,
250  fixEE2017Params = {'userawPt': True, 'ptThreshold':50.0, 'minEtaThreshold':2.65, 'maxEtaThreshold': 3.139},
251  postfix = "FixEE2017")
252  process.nanoSequenceCommon.insert(2,process.fullPatMetSequenceFixEE2017)
253 
254 
256 
257  process = nanoAOD_activateVID(process)
258 
259  # This function is defined in jetsAK4_CHS_cff.py
260  process = nanoAOD_addDeepInfoAK4CHS(process,
261  addDeepBTag=nanoAOD_addDeepInfoAK4CHS_switch.nanoAOD_addDeepBTag_switch,
262  addDeepFlavour=nanoAOD_addDeepInfoAK4CHS_switch.nanoAOD_addDeepFlavourTag_switch
263  )
264 
265  # This function is defined in jetsAK8_cff.py
266  process = nanoAOD_addDeepInfoAK8(process,
267  addDeepBTag=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepBTag_switch,
268  addDeepBoostedJet=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepBoostedJet_switch,
269  addDeepDoubleX=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepDoubleX_switch,
270  addDeepDoubleXV2=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepDoubleXV2_switch,
271  addParticleNet=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addParticleNet_switch,
272  addParticleNetMass=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addParticleNetMass_switch,
273  jecPayload=nanoAOD_addDeepInfoAK8_switch.jecPayload
274  )
275 
276  nanoAOD_tau_switch = cms.PSet(
277  idsToAdd = cms.vstring()
278  )
279  (run2_nanoAOD_94XMiniAODv1 | run2_nanoAOD_94X2016 | run2_nanoAOD_94XMiniAODv2 | run2_nanoAOD_102Xv1 | run2_nanoAOD_106Xv1).toModify(nanoAOD_tau_switch, idsToAdd = ["deepTau2017v2p1"])
280  (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()))
281  (run2_nanoAOD_106Xv2 | run3_nanoAOD_122).toModify(nanoAOD_tau_switch, idsToAdd = ["deepTau2018v2p5"])
282  (run2_nanoAOD_106Xv2 | run3_nanoAOD_122).toModify(process, lambda p : nanoAOD_addTauIds(p, nanoAOD_tau_switch.idsToAdd.value()))
283  nanoAOD_boostedTau_switch = cms.PSet(
284  idsToAdd = cms.vstring()
285  )
286  run2_nanoAOD_106Xv2.toModify(nanoAOD_boostedTau_switch, idsToAdd = ["2017v2", "dR0p32017v2", "newDM2017v2","againstEle2018"])
287  run2_nanoAOD_106Xv2.toModify(process, lambda p : nanoAOD_addBoostedTauIds(p, nanoAOD_boostedTau_switch.idsToAdd.value()))
288 
289  return process
290 
292  process = nanoAOD_customizeCommon(process)
293  for modifier in run2_miniAOD_80XLegacy,run2_nanoAOD_94X2016,run2_nanoAOD_94XMiniAODv1,run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_102Xv1,run2_nanoAOD_106Xv1:
294  modifier.toModify(process, lambda p: nanoAOD_recalibrateMETs(p,isData=True))
295  for modifier in run2_nanoAOD_94XMiniAODv1, run2_nanoAOD_94XMiniAODv2:
296  modifier.toModify(process, lambda p: nanoAOD_runMETfixEE2017(p,isData=True))
297  return process
298 
299 def nanoAOD_customizeMC(process):
300  process = nanoAOD_customizeCommon(process)
301  for modifier in run2_miniAOD_80XLegacy,run2_nanoAOD_94X2016,run2_nanoAOD_94XMiniAODv1,run2_nanoAOD_94XMiniAODv2,run2_nanoAOD_102Xv1,run2_nanoAOD_106Xv1:
302  modifier.toModify(process, lambda p: nanoAOD_recalibrateMETs(p,isData=False))
303  for modifier in run2_nanoAOD_94XMiniAODv1, run2_nanoAOD_94XMiniAODv2:
304  modifier.toModify(process, lambda p: nanoAOD_runMETfixEE2017(p,isData=False))
305  return process
306 
307 
309  pdgSelection="?(abs(pdgId) == 11|| abs(pdgId)==13 || abs(pdgId)==15 ||abs(pdgId)== 12 || abs(pdgId)== 14 || abs(pdgId)== 16|| abs(pdgId)== 24|| pdgId== 23)"
310  # Keep precision same as default RECO for selected particles
311  ptPrecision="{}?{}:{}".format(pdgSelection, CandVars.pt.precision.value(),genParticleTable.variables.pt.precision.value())
312  process.genParticleTable.variables.pt.precision=cms.string(ptPrecision)
313  phiPrecision="{} ? {} : {}".format(pdgSelection, CandVars.phi.precision.value(), genParticleTable.variables.phi.precision.value())
314  process.genParticleTable.variables.phi.precision=cms.string(phiPrecision)
315  etaPrecision="{} ? {} : {}".format(pdgSelection, CandVars.eta.precision.value(), genParticleTable.variables.eta.precision.value())
316  process.genParticleTable.variables.eta.precision=cms.string(etaPrecision)
317  return process
318 
319 # lowPtElectrons do not exsit for old nano campaigns (i.e. before v9)
320 _modifiers = ( run2_miniAOD_80XLegacy |
321  run2_nanoAOD_94XMiniAODv1 |
322  run2_nanoAOD_94XMiniAODv2 |
323  run2_nanoAOD_94X2016 |
324  run2_nanoAOD_102Xv1 |
325  run2_nanoAOD_106Xv1 )
326 _modifiers.toModify(linkedObjects,lowPtElectrons="")
327 _modifiers.toModify(simpleCleanerTable,lowPtElectrons="")
def switchOnVIDElectronIdProducer(process, dataFormat, task=None)
Electrons.
Definition: vid_id_tools.py:66
def addToProcessAndTask(label, module, process, task)
Definition: helpers.py:28
def setupAllVIDIdsInModule(process, id_module_name, setupFunction, patProducer=None, addUserData=True, task=None)
Definition: vid_id_tools.py:49
def nanoAOD_addDeepInfoAK8(process, addDeepBTag, addDeepBoostedJet, addDeepDoubleX, addDeepDoubleXV2, addParticleNet, addParticleNetMass, jecPayload)
Definition: jetsAK8_cff.py:182
def nanoAOD_customizeMC(process)
Definition: nano_cff.py:299
def nanoAOD_activateVID(process)
Definition: nano_cff.py:221
def nanoWmassGenCustomize(process)
increasing the precision of selected GenParticles.
Definition: nano_cff.py:308
def nanoAOD_customizeCommon(process)
Definition: nano_cff.py:255
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def nanoAOD_recalibrateMETs(process, isData)
Definition: nano_cff.py:157
def nanoAOD_addTauIds(process, idsToRun=[])
Definition: nano_cff.py:119
def nanoAOD_runMETfixEE2017(process, isData)
Definition: nano_cff.py:247
def nanoAOD_addBoostedTauIds(process, idsToRun=[])
Definition: nano_cff.py:137
def nanoAOD_addDeepInfoAK4CHS(process, addDeepBTag, addDeepFlavour)
def getPatAlgosToolsTask(process)
Definition: helpers.py:13
def switchOnVIDPhotonIdProducer(process, dataFormat, task=None)
Photons.
def runMetCorAndUncFromMiniAOD(process, metType="PF", jetCollUnskimmed="slimmedJets", photonColl="slimmedPhotons", electronColl="slimmedElectrons", muonColl="slimmedMuons", tauColl="slimmedTaus", pfCandColl="packedPFCandidates", jetFlavor="AK4PFchs", jetCleaning="LepClean", isData=False, manualJetConfig=False, reclusterJets=None, jetSelection="pt>15 && abs(eta)<9.9", recoMetFromPFCs=None, jetCorLabelL3="ak4PFCHSL1FastL2L3Corrector", jetCorLabelRes="ak4PFCHSL1FastL2L3ResidualCorrector", CHS=False, reapplyJEC=True, jecUncFile="", computeMETSignificance=True, fixEE2017=False, fixEE2017Params=None, extractDeepMETs=False, postfix="")
def makePuppiesFromMiniAOD(process, createScheduledSequence=False)
def nanoAOD_customizeData(process)
Definition: nano_cff.py:291