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