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