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 , process.patTauMVAIDsTask )
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_addPNetToTaus(process, addPNetInfo=False, runPNetCHSAK4=False):
143  if addPNetInfo:
144  originalTauName = process.finalTaus.src.value()
145  updatedTauName = originalTauName+'WithPNet'
146  jetCollection = "updatedJets"
147  process.load('RecoBTag.ONNXRuntime.pfParticleNetFromMiniAODAK4_cff')
148  pnetTagName = "pfParticleNetFromMiniAODAK4CHSCentralJetTag"
149  pnetDiscriminators = [];
150  for tag in getattr(process,pnetTagName+"s").flav_names.value():
151  pnetDiscriminators.append(pnetTagName+"s:"+tag)
152 
153  # Define "hybridTau" producer
154  from PhysicsTools.PatAlgos.patTauHybridProducer_cfi import patTauHybridProducer
155  setattr(process, updatedTauName, patTauHybridProducer.clone(
156  src = originalTauName,
157  jetSource = jetCollection,
158  dRMax = 0.4,
159  jetPtMin = 15,
160  jetEtaMax = 2.5,
161  pnetLabel = pnetTagName+"s",
162  pnetScoreNames = pnetDiscriminators,
163  tauScoreMin = -1,
164  vsJetMin = 0.05,
165  checkTauScoreIsBest = False,
166  chargeAssignmentProbMin = 0.2,
167  addGenJetMatch = False,
168  genJetMatch = ""
169  ))
170  process.finalTaus.src = updatedTauName
171 
172  # run PNet for CHS AK4 jets if requested
173  if runPNetCHSAK4:
174  from PhysicsTools.NanoAOD.jetsAK4_CHS_cff import nanoAOD_addDeepInfoAK4CHS
175  process = nanoAOD_addDeepInfoAK4CHS(process,
176  addDeepBTag = False,
177  addDeepFlavour = False,
178  addParticleNet = True
179  )
180 
181  #remember to adjust the selection and tables with added IDs
182 
183  process.tauTask.add(process.jetTask, getattr(process, updatedTauName))
184 
185  return process
186 
188 def nanoAOD_activateVID(process):
189 
190  switchOnVIDElectronIdProducer(process,DataFormat.MiniAOD,electronTask)
191  for modname in electron_id_modules_WorkingPoints_nanoAOD.modules:
192  setupAllVIDIdsInModule(process,modname,setupVIDElectronSelection)
193 
194  process.electronTask.add( process.egmGsfElectronIDTask )
195 
196  switchOnVIDPhotonIdProducer(process,DataFormat.MiniAOD,photonTask) # do not call this to avoid resetting photon IDs in VID, if called before inside makePuppiesFromMiniAOD
197  for modname in photon_id_modules_WorkingPoints_nanoAOD.modules:
198  setupAllVIDIdsInModule(process,modname,setupVIDPhotonSelection)
199 
200  process.photonTask.add( process.egmPhotonIDTask )
201 
202  return process
203 
205 
206  process = nanoAOD_activateVID(process)
207 
208  run2_nanoAOD_106Xv2.toModify(
209  nanoAOD_addDeepInfoAK4CHS_switch, nanoAOD_addParticleNet_switch=True,
210  nanoAOD_addRobustParTAK4Tag_switch=True,
211  )
212 
213  # This function is defined in jetsAK4_Puppi_cff.py
214  process = nanoAOD_addDeepInfoAK4(process,
215  addParticleNet=nanoAOD_addDeepInfoAK4_switch.nanoAOD_addParticleNet_switch,
216  addRobustParTAK4=nanoAOD_addDeepInfoAK4_switch.nanoAOD_addRobustParTAK4Tag_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  )
226 
227  # This function is defined in jetsAK8_cff.py
228  process = nanoAOD_addDeepInfoAK8(process,
229  addDeepBTag=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepBTag_switch,
230  addDeepBoostedJet=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepBoostedJet_switch,
231  addDeepDoubleX=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepDoubleX_switch,
232  addDeepDoubleXV2=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addDeepDoubleXV2_switch,
233  addParticleNetMassLegacy=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addParticleNetMassLegacy_switch,
234  addParticleNet=nanoAOD_addDeepInfoAK8_switch.nanoAOD_addParticleNet_switch,
235  jecPayload=nanoAOD_addDeepInfoAK8_switch.jecPayload
236  )
237 
238  nanoAOD_tau_switch = cms.PSet(
239  idsToAdd = cms.vstring(),
240  runPNetAK4 = cms.bool(False),
241  addPNet = cms.bool(True)
242  )
243  (run2_nanoAOD_106Xv2 | run3_nanoAOD_122).toModify(
244  nanoAOD_tau_switch, idsToAdd = ["deepTau2018v2p5"]
245  ).toModify(
246  process, lambda p : nanoAOD_addTauIds(p, nanoAOD_tau_switch.idsToAdd.value())
247  )
248  # Add PNet info to taus
249  # enable rerun of PNet for CHS jets for early run3 eras
250  # (it is rerun for run2 within jet tasks while is not needed for newer
251  # run3 eras as it is present in miniAOD)
252  (run3_nanoAOD_122 | run3_nanoAOD_124).toModify(
253  nanoAOD_tau_switch, runPNetAK4 = True
254  )
255  nanoAOD_addPNetToTaus(process,
256  addPNetInfo = nanoAOD_tau_switch.addPNet.value(),
257  runPNetCHSAK4 = nanoAOD_tau_switch.runPNetAK4.value()
258  )
259  nanoAOD_boostedTau_switch = cms.PSet(
260  idsToAdd = cms.vstring()
261  )
262  run2_nanoAOD_106Xv2.toModify(
263  nanoAOD_boostedTau_switch, idsToAdd = ["2017v2", "dR0p32017v2", "newDM2017v2","againstEle2018"]
264  ).toModify(
265  process, lambda p : nanoAOD_addBoostedTauIds(p, nanoAOD_boostedTau_switch.idsToAdd.value())
266  )
267 
268  return process
269 
270 
272  pdgSelection="?(abs(pdgId) == 11|| abs(pdgId)==13 || abs(pdgId)==15 ||abs(pdgId)== 12 || abs(pdgId)== 14 || abs(pdgId)== 16|| abs(pdgId)== 24|| pdgId== 23)"
273  # Keep precision same as default RECO for selected particles
274  ptPrecision="{}?{}:{}".format(pdgSelection, CandVars.pt.precision.value(),genParticleTable.variables.pt.precision.value())
275  process.genParticleTable.variables.pt.precision=cms.string(ptPrecision)
276  phiPrecision="{} ? {} : {}".format(pdgSelection, CandVars.phi.precision.value(), genParticleTable.variables.phi.precision.value())
277  process.genParticleTable.variables.phi.precision=cms.string(phiPrecision)
278  etaPrecision="{} ? {} : {}".format(pdgSelection, CandVars.eta.precision.value(), genParticleTable.variables.eta.precision.value())
279  process.genParticleTable.variables.eta.precision=cms.string(etaPrecision)
280  return process
281 
283  process.nanoTableTaskCommon.add(process.l1TablesTask)
284  process = setL1NanoToReduced(process)
285  return process
286 
288  process.nanoTableTaskCommon.add(process.l1TablesTask)
289  return process
def switchOnVIDElectronIdProducer(process, dataFormat, task=None)
Electrons.
Definition: vid_id_tools.py:66
def nanoL1TrigObjCustomizeFull(process)
Definition: nano_cff.py:287
def nanoAOD_addDeepInfoAK4(process, addParticleNet, addRobustParTAK4=False)
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:188
def nanoWmassGenCustomize(process)
increasing the precision of selected GenParticles.
Definition: nano_cff.py:271
def nanoAOD_customizeCommon(process)
Definition: nano_cff.py:204
def nanoAOD_addDeepInfoAK4CHS(process, addDeepBTag, addDeepFlavour, addParticleNet, addRobustParTAK4=False)
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_addBoostedTauIds(process, idsToRun=[])
Definition: nano_cff.py:126
def switchOnVIDPhotonIdProducer(process, dataFormat, task=None)
Photons.
def nanoL1TrigObjCustomize(process)
Definition: nano_cff.py:282
def nanoAOD_addPNetToTaus(process, addPNetInfo=False, runPNetCHSAK4=False)
Definition: nano_cff.py:142
def setL1NanoToReduced(process)
Definition: l1trig_cff.py:159