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  boostedTaus=cms.InputTag("finalBoostedTaus"),
45  photons=cms.InputTag("finalPhotons"),
46  vertices=cms.InputTag("slimmedSecondaryVertices")
47 )
48 
49 # Switch to AK4 CHS jets for Run-2
50 run2_nanoAOD_ANY.toModify(
51  linkedObjects, jets="finalJets"
52 )
53 
54 # boosted taus don't exist in 122X MINI
55 run3_nanoAOD_122.toModify(
56  linkedObjects, boostedTaus=None,
57 )
58 
59 from PhysicsTools.NanoAOD.lhcInfoProducer_cfi import lhcInfoProducer
60 lhcInfoTable = lhcInfoProducer.clone()
61 (~run3_common).toModify(
62  lhcInfoTable, useNewLHCInfo=False
63 )
64 
65 nanoTableTaskCommon = cms.Task(
66  cms.Task(nanoMetadata),
67  jetPuppiTask, jetPuppiForMETTask, jetAK8Task,
68  extraFlagsProducersTask, muonTask, tauTask, boostedTauTask,
69  electronTask , lowPtElectronTask, photonTask,
70  vertexTask, isoTrackTask, jetAK8LepTask, # must be after all the leptons
71  softActivityTask,
72  cms.Task(linkedObjects),
73  jetPuppiTablesTask, jetAK8TablesTask,
74  muonTablesTask, fsrTablesTask, tauTablesTask, boostedTauTablesTask,
75  electronTablesTask, lowPtElectronTablesTask, photonTablesTask,
76  globalTablesTask, vertexTablesTask, metTablesTask, extraFlagsTableTask,
77  isoTrackTablesTask,softActivityTablesTask
78 )
79 
80 # Replace AK4 Puppi with AK4 CHS for Run-2
81 _nanoTableTaskCommonRun2 = nanoTableTaskCommon.copy()
82 _nanoTableTaskCommonRun2.replace(jetPuppiTask, jetTask)
83 _nanoTableTaskCommonRun2.replace(jetPuppiForMETTask, jetForMETTask)
84 _nanoTableTaskCommonRun2.replace(jetPuppiTablesTask, jetTablesTask)
85 run2_nanoAOD_ANY.toReplaceWith(
86  nanoTableTaskCommon, _nanoTableTaskCommonRun2
87 )
88 
89 nanoSequenceCommon = cms.Sequence(nanoTableTaskCommon)
90 
91 nanoSequenceOnlyFullSim = cms.Sequence(triggerObjectTablesTask)
92 nanoSequenceOnlyData = cms.Sequence(cms.Sequence(protonTablesTask) + lhcInfoTable)
93 
94 nanoSequence = cms.Sequence(nanoSequenceCommon + nanoSequenceOnlyData + nanoSequenceOnlyFullSim)
95 
96 nanoTableTaskFS = cms.Task(
97  genParticleTask, particleLevelTask, jetMCTask, muonMCTask, electronMCTask, lowPtElectronMCTask, photonMCTask,
98  tauMCTask, boostedTauMCTask,
99  metMCTable, ttbarCatMCProducersTask, globalTablesMCTask, ttbarCategoryTableTask,
100  genWeightsTableTask, genVertexTablesTask, genParticleTablesTask, genProtonTablesTask, particleLevelTablesTask
101 )
102 
103 nanoSequenceFS = cms.Sequence(nanoSequenceCommon + cms.Sequence(nanoTableTaskFS))
104 
105 # GenVertex only stored in newer MiniAOD
106 nanoSequenceMC = nanoSequenceFS.copy()
107 nanoSequenceMC.insert(nanoSequenceFS.index(nanoSequenceCommon)+1,nanoSequenceOnlyFullSim)
108 
109 # modifier which adds new tauIDs (currently only deepTauId2017v2p1 is being added)
110 import RecoTauTag.RecoTau.tools.runTauIdMVA as tauIdConfig
111 def nanoAOD_addTauIds(process, idsToRun=[]):
112  if idsToRun: #no-empty list of tauIDs to run
113  updatedTauName = "slimmedTausUpdated"
114  tauIdEmbedder = tauIdConfig.TauIDEmbedder(process, debug = False,
115  updatedTauName = updatedTauName,
116  postfix = "ForNano",
117  toKeep = idsToRun)
118  tauIdEmbedder.runTauID()
119  process.finalTaus.src = updatedTauName
120  #remember to adjust the selection and tables with added IDs
121 
122  process.tauTask.add( process.rerunMvaIsolationTaskForNano, getattr(process, updatedTauName) )
123 
124  return process
125 
126 def nanoAOD_addBoostedTauIds(process, idsToRun=[]):
127  if idsToRun: #no-empty list of tauIDs to run
128  updatedBoostedTauName = "slimmedTausBoostedNewID"
129  boostedTauIdEmbedder = tauIdConfig.TauIDEmbedder(process, debug = False,
130  originalTauName = "slimmedTausBoosted",
131  updatedTauName = updatedBoostedTauName,
132  postfix = "BoostedForNano",
133  toKeep = idsToRun)
134  boostedTauIdEmbedder.runTauID()
135  process.finalBoostedTaus.src = updatedBoostedTauName
136  #remember to adjust the selection and tables with added IDs
137 
138  process.boostedTauTask.add( process.rerunMvaIsolationTaskBoostedForNano, getattr(process, updatedBoostedTauName))
139 
140  return process
141 
142 def nanoAOD_addUTagToTaus(process, addUTagInfo=False, usePUPPIjets=False):
143 
144  if addUTagInfo:
145  originalTauName = process.finalTaus.src.value()
146 
147  if usePUPPIjets: # option to use PUPPI jets
148  jetCollection = "updatedJetsPuppi"
149  TagName = "pfUnifiedParticleTransformerAK4JetTags"
150  tag_prefix = "byUTagPUPPI"
151  updatedTauName = originalTauName+'WithUTagPUPPI'
152  # Unified ParT Tagger used for PUPPI jets
153  from RecoBTag.ONNXRuntime.pfUnifiedParticleTransformerAK4JetTags_cfi import pfUnifiedParticleTransformerAK4JetTags
154  Discriminators = [TagName+":"+tag for tag in pfUnifiedParticleTransformerAK4JetTags.flav_names.value()]
155  else: # use CHS jets by default
156  jetCollection = "updatedJets"
157  TagName = "pfParticleNetFromMiniAODAK4CHSCentralJetTags"
158  tag_prefix = "byUTagCHS"
159  updatedTauName = originalTauName+'WithUTagCHS'
160  # PNet tagger used for CHS jets
161  from RecoBTag.ONNXRuntime.pfParticleNetFromMiniAODAK4_cff import pfParticleNetFromMiniAODAK4CHSCentralJetTags
162  Discriminators = [TagName+":"+tag for tag in pfParticleNetFromMiniAODAK4CHSCentralJetTags.flav_names.value()]
163 
164  # Define "hybridTau" producer
165  from PhysicsTools.PatAlgos.patTauHybridProducer_cfi import patTauHybridProducer
166  setattr(process, updatedTauName, patTauHybridProducer.clone(
167  src = originalTauName,
168  jetSource = jetCollection,
169  dRMax = 0.4,
170  jetPtMin = 15,
171  jetEtaMax = 2.5,
172  UTagLabel = TagName,
173  UTagScoreNames = Discriminators,
174  tagPrefix = tag_prefix,
175  tauScoreMin = -1,
176  vsJetMin = 0.05,
177  checkTauScoreIsBest = False,
178  chargeAssignmentProbMin = 0.2,
179  addGenJetMatch = False,
180  genJetMatch = ""
181  ))
182  process.finalTaus.src = updatedTauName
183 
184  #remember to adjust the selection and tables with added IDs
185 
186  process.tauTask.add(process.jetTask, getattr(process, updatedTauName))
187 
188  return process
189 
191 def nanoAOD_activateVID(process):
192 
193  switchOnVIDElectronIdProducer(process,DataFormat.MiniAOD,electronTask)
194  for modname in electron_id_modules_WorkingPoints_nanoAOD.modules:
195  setupAllVIDIdsInModule(process,modname,setupVIDElectronSelection)
196 
197  process.electronTask.add( process.egmGsfElectronIDTask )
198 
199  switchOnVIDPhotonIdProducer(process,DataFormat.MiniAOD,photonTask) # do not call this to avoid resetting photon IDs in VID, if called before inside makePuppiesFromMiniAOD
200  for modname in photon_id_modules_WorkingPoints_nanoAOD.modules:
201  setupAllVIDIdsInModule(process,modname,setupVIDPhotonSelection)
202 
203  process.photonTask.add( process.egmPhotonIDTask )
204 
205  return process
206 
208 
209  process = nanoAOD_activateVID(process)
210 
211  run2_nanoAOD_106Xv2.toModify(
212  nanoAOD_addDeepInfoAK4CHS_switch, nanoAOD_addParticleNet_switch=True,
213  nanoAOD_addRobustParTAK4Tag_switch=False,
214  nanoAOD_addUnifiedParTAK4Tag_switch=True,
215  )
216 
217  # enable rerun of PNet for CHS jets for early run3 eras
218  # (it is rerun for run2 within jet tasks while is not needed for newer
219  # run3 eras as it is present in miniAOD)
220  (run3_nanoAOD_122 | run3_nanoAOD_124).toModify(
221  nanoAOD_addDeepInfoAK4CHS_switch, nanoAOD_addParticleNet_switch = True
222  )
223 
224  # This function is defined in jetsAK4_Puppi_cff.py
225  process = nanoAOD_addDeepInfoAK4(process,
226  addParticleNet=nanoAOD_addDeepInfoAK4_switch.nanoAOD_addParticleNet_switch,
227  addRobustParTAK4=nanoAOD_addDeepInfoAK4_switch.nanoAOD_addRobustParTAK4Tag_switch,
228  addUnifiedParTAK4=nanoAOD_addDeepInfoAK4_switch.nanoAOD_addUnifiedParTAK4Tag_switch
229  )
230 
231  # This function is defined in jetsAK4_CHS_cff.py
232  process = nanoAOD_addDeepInfoAK4CHS(process,
233  addDeepBTag=nanoAOD_addDeepInfoAK4CHS_switch.nanoAOD_addDeepBTag_switch,
234  addDeepFlavour=nanoAOD_addDeepInfoAK4CHS_switch.nanoAOD_addDeepFlavourTag_switch,
235  addParticleNet=nanoAOD_addDeepInfoAK4CHS_switch.nanoAOD_addParticleNet_switch,
236  addRobustParTAK4=nanoAOD_addDeepInfoAK4CHS_switch.nanoAOD_addRobustParTAK4Tag_switch,
237  addUnifiedParTAK4=nanoAOD_addDeepInfoAK4CHS_switch.nanoAOD_addUnifiedParTAK4Tag_switch
238  )
239 
240  # This function is defined in jetsAK8_cff.py
241  process = nanoAOD_addDeepInfoAK8(process,
242  addDeepBTag=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepBTag_switch,
243  addDeepBoostedJet=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepBoostedJet_switch,
244  addDeepDoubleX=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepDoubleX_switch,
245  addDeepDoubleXV2=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepDoubleXV2_switch,
246  addParticleNetMassLegacy=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addParticleNetMassLegacy_switch,
247  addParticleNet=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addParticleNet_switch,
248  jecPayload=nanoAOD_addDeepInfoAK8_switch.jecPayload
249  )
250 
251  nanoAOD_tau_switch = cms.PSet(
252  idsToAdd = cms.vstring(),
253  addUParTInfo = cms.bool(True),
254  addPNet = cms.bool(True)
255  )
256  (run2_nanoAOD_106Xv2 | run3_nanoAOD_122).toModify(
257  nanoAOD_tau_switch, idsToAdd = ["deepTau2018v2p5"]
258  ).toModify(
259  process, lambda p : nanoAOD_addTauIds(p, nanoAOD_tau_switch.idsToAdd.value())
260  )
261 
262  # Don't add Unified Tagger for PUPPI jets for Run 2 (as different PUPPI tune
263  # and base jet algorithm) or early Run 3 eras
264  (run3_nanoAOD_122 | run3_nanoAOD_124 | run2_nanoAOD_106Xv2).toModify(
265  nanoAOD_tau_switch, addUParTInfo = False
266  )
267 
268  # Add Unified Tagger For CHS Jets (PNet 2023)
269  nanoAOD_addUTagToTaus(process,
270  addUTagInfo = nanoAOD_tau_switch.addPNet.value(),
271  usePUPPIjets = False
272  )
273 
274  # Add Unified Tagger For PUPPI Jets (UParT 2024)
275  nanoAOD_addUTagToTaus(process,
276  addUTagInfo = nanoAOD_tau_switch.addUParTInfo.value(),
277  usePUPPIjets = True
278  )
279 
280  nanoAOD_boostedTau_switch = cms.PSet(
281  idsToAdd = cms.vstring()
282  )
283  run2_nanoAOD_106Xv2.toModify(
284  nanoAOD_boostedTau_switch, idsToAdd = ["mvaIso", "mvaIsoNewDM", "mvaIsoDR0p3", "againstEle"]
285  ).toModify(
286  process, lambda p : nanoAOD_addBoostedTauIds(p, nanoAOD_boostedTau_switch.idsToAdd.value())
287  )
288 
289  # Add lepton time-life info
290  from PhysicsTools.NanoAOD.leptonTimeLifeInfo_common_cff import addTimeLifeInfoBase
291  process = addTimeLifeInfoBase(process)
292 
293  return process
294 
295 
297  pdgSelection="?(abs(pdgId) == 11|| abs(pdgId)==13 || abs(pdgId)==15 ||abs(pdgId)== 12 || abs(pdgId)== 14 || abs(pdgId)== 16|| abs(pdgId)== 24|| pdgId== 23)"
298  # Keep precision same as default RECO for selected particles
299  ptPrecision="{}?{}:{}".format(pdgSelection, CandVars.pt.precision.value(),genParticleTable.variables.pt.precision.value())
300  process.genParticleTable.variables.pt.precision=cms.string(ptPrecision)
301  phiPrecision="{} ? {} : {}".format(pdgSelection, CandVars.phi.precision.value(), genParticleTable.variables.phi.precision.value())
302  process.genParticleTable.variables.phi.precision=cms.string(phiPrecision)
303  etaPrecision="{} ? {} : {}".format(pdgSelection, CandVars.eta.precision.value(), genParticleTable.variables.eta.precision.value())
304  process.genParticleTable.variables.eta.precision=cms.string(etaPrecision)
305  return process
306 
308  process.nanoTableTaskCommon.add(process.l1TablesTask)
309  process = setL1NanoToReduced(process)
310  return process
311 
313  process.nanoTableTaskCommon.add(process.l1TablesTask)
314  return process
def switchOnVIDElectronIdProducer(process, dataFormat, task=None)
Electrons.
Definition: vid_id_tools.py:66
def nanoL1TrigObjCustomizeFull(process)
Definition: nano_cff.py:312
def setupAllVIDIdsInModule(process, id_module_name, setupFunction, patProducer=None, addUserData=True, task=None)
Definition: vid_id_tools.py:49
L1 objects.
def nanoAOD_activateVID(process)
Definition: nano_cff.py:191
def nanoWmassGenCustomize(process)
increasing the precision of selected GenParticles.
Definition: nano_cff.py:296
def nanoAOD_customizeCommon(process)
Definition: nano_cff.py:207
def nanoAOD_addUTagToTaus(process, addUTagInfo=False, usePUPPIjets=False)
Definition: nano_cff.py:142
def nanoAOD_addTauIds(process, idsToRun=[])
Definition: nano_cff.py:111
def nanoAOD_addDeepInfoAK8(process, addDeepBTag, addDeepBoostedJet, addDeepDoubleX, addDeepDoubleXV2, addParticleNetMassLegacy, addParticleNet, jecPayload)
Definition: jetsAK8_cff.py:218
def nanoAOD_addDeepInfoAK4(process, addParticleNet, addRobustParTAK4=False, addUnifiedParTAK4=False)
def nanoAOD_addBoostedTauIds(process, idsToRun=[])
Definition: nano_cff.py:126
def switchOnVIDPhotonIdProducer(process, dataFormat, task=None)
Photons.
def nanoL1TrigObjCustomize(process)
Definition: nano_cff.py:307
def nanoAOD_addDeepInfoAK4CHS(process, addDeepBTag, addDeepFlavour, addParticleNet, addRobustParTAK4=False, addUnifiedParTAK4=False)
def setL1NanoToReduced(process)
Definition: l1trig_cff.py:159