CMS 3D CMS Logo

adaptToRunAtMiniAOD.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 
3 
9 
10 
11 def addTauReReco(process):
12  #PAT
13  process.load('PhysicsTools.PatAlgos.producersLayer1.tauProducer_cff')
14  process.load('PhysicsTools.PatAlgos.selectionLayer1.tauSelector_cfi')
15  process.selectedPatTaus.cut="pt > 18. && tauID(\'decayModeFindingNewDMs\')> 0.5" #Cut as in MiniAOD
16  #Tau RECO
17  process.load("RecoTauTag.Configuration.RecoPFTauTag_cff")
18  #Task/Sequence for tau rereco
19  process.miniAODTausTask = cms.Task(
20  process.PFTauTask,
21  process.makePatTausTask,
22  process.selectedPatTaus
23  )
24  process.miniAODTausSequence = cms.Sequence(process.miniAODTausTask)
25  #Path with tau rereco (Needed?)
26  process.TauReco = cms.Path(process.miniAODTausSequence)
27 
28 
29 def convertModuleToMiniAODInput(process, name):
30  module = getattr(process, name)
31  if hasattr(module, 'particleFlowSrc'):
32  module.particleFlowSrc = cms.InputTag("packedPFCandidates", "", "")
33  if hasattr(module, 'vertexSrc'):
34  module.vertexSrc = cms.InputTag('offlineSlimmedPrimaryVertices')
35  if hasattr(module, 'qualityCuts') and hasattr(module.qualityCuts, 'primaryVertexSrc'):
36  module.qualityCuts.primaryVertexSrc = cms.InputTag('offlineSlimmedPrimaryVertices')
37 
38 
39 def adaptTauToMiniAODReReco(process, reclusterJets=True):
40  # TRYING TO MAKE THINGS MINIAOD COMPATIBLE, FROM THE START, TO THE END, 1 BY 1
41  #print '[adaptTauToMiniAODReReco]: Start'
42 
43  jetCollection = 'slimmedJets'
44  # Add new jet collections if reclustering is demanded
45  if reclusterJets:
46  jetCollection = 'patJetsPAT'
47  from RecoJets.JetProducers.ak4PFJets_cfi import ak4PFJets
48  process.ak4PFJetsPAT = ak4PFJets.clone(
49  src=cms.InputTag("packedPFCandidates")
50  )
51  # trivial PATJets
53  process.patJetsPAT = _patJets.clone(
54  jetSource = cms.InputTag("ak4PFJetsPAT"),
55  addJetCorrFactors = cms.bool(False),
56  jetCorrFactorsSource = cms.VInputTag(),
57  addBTagInfo = cms.bool(False),
58  addDiscriminators = cms.bool(False),
59  discriminatorSources = cms.VInputTag(),
60  addAssociatedTracks = cms.bool(False),
61  addJetCharge = cms.bool(False),
62  addGenPartonMatch = cms.bool(False),
63  embedGenPartonMatch = cms.bool(False),
64  addGenJetMatch = cms.bool(False),
65  getJetMCFlavour = cms.bool(False),
66  addJetFlavourInfo = cms.bool(False),
67  )
68  process.miniAODTausTask.add(process.ak4PFJetsPAT)
69  process.miniAODTausTask.add(process.patJetsPAT)
70 
71  # so this adds all tracks to jet in some deltaR region. we don't have tracks so don't need it :D
72  # process.ak4PFJetTracksAssociatorAtVertex.jets = cms.InputTag(jetCollection)
73 
74  # Remove ak4PFJetTracksAssociatorAtVertex from recoTauCommonSequence
75  # Remove pfRecoTauTagInfoProducer from recoTauCommonSequence since it uses the jet-track association
76  # HOWEVER, may use https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookMiniAOD2017#Isolated_Tracks
77  # probably needs recovery of the two modules above
78 
79  process.recoTauAK4Jets08RegionPAT = cms.EDProducer("RecoTauPatJetRegionProducer",
80  deltaR = process.recoTauAK4PFJets08Region.deltaR,
81  maxJetAbsEta = process.recoTauAK4PFJets08Region.maxJetAbsEta,
82  minJetPt = process.recoTauAK4PFJets08Region.minJetPt,
83  pfCandAssocMapSrc = cms.InputTag(""),
84  pfCandSrc = cms.InputTag("packedPFCandidates"),
85  src = cms.InputTag(jetCollection)
86  )
87 
88  process.recoTauPileUpVertices.src = cms.InputTag("offlineSlimmedPrimaryVertices")
89  # Redefine recoTauCommonTask
90  # with redefined region and PU vertices, and w/o track-to-vertex associator and tauTagInfo (the two latter are probably obsolete and not needed at all)
91  process.recoTauCommonTask = cms.Task(
92  process.recoTauAK4Jets08RegionPAT,
93  process.recoTauPileUpVertices
94  )
95 
96  for moduleName in process.TauReco.moduleNames():
97  convertModuleToMiniAODInput(process, moduleName)
98 
99 
100  # Adapt TauPiZeros producer
101  process.ak4PFJetsLegacyHPSPiZeros.builders[0].qualityCuts.primaryVertexSrc = cms.InputTag("offlineSlimmedPrimaryVertices")
102  process.ak4PFJetsLegacyHPSPiZeros.jetSrc = cms.InputTag(jetCollection)
103 
104  # Adapt TauChargedHadrons producer
105  for builder in process.ak4PFJetsRecoTauChargedHadrons.builders:
106  builder.qualityCuts.primaryVertexSrc = cms.InputTag("offlineSlimmedPrimaryVertices")
107  if builder.name.value() == 'tracks': #replace plugin based on generalTracks by one based on lostTracks
108  builder.name = 'lostTracks'
109  builder.plugin = 'PFRecoTauChargedHadronFromLostTrackPlugin'
110  builder.srcTracks = cms.InputTag("lostTracks")
111  process.ak4PFJetsRecoTauChargedHadrons.jetSrc = cms.InputTag(jetCollection)
112 
113  # Adapt combinatoricRecoTau producer
114  process.combinatoricRecoTaus.jetRegionSrc = 'recoTauAK4Jets08RegionPAT'
115  process.combinatoricRecoTaus.jetSrc = jetCollection
116  # Adapt builders
117  for builder in process.combinatoricRecoTaus.builders:
118  for name,value in builder.parameters_().items():
119  if name == 'qualityCuts':
120  builder.qualityCuts.primaryVertexSrc = 'offlineSlimmedPrimaryVertices'
121  elif name == 'pfCandSrc':
122  builder.pfCandSrc = 'packedPFCandidates'
123  # Adapt supported modifiers and remove unsupported ones
124  modifiersToRemove_ = cms.VPSet()
125  for mod in process.combinatoricRecoTaus.modifiers:
126  if mod.name.value() == 'elec_rej':
127  modifiersToRemove_.append(mod)
128  continue
129  elif mod.name.value() == 'TTIworkaround':
130  modifiersToRemove_.append(mod)
131  continue
132  for name,value in mod.parameters_().items():
133  if name == 'qualityCuts':
134  mod.qualityCuts.primaryVertexSrc = 'offlineSlimmedPrimaryVertices'
135  for mod in modifiersToRemove_:
136  process.combinatoricRecoTaus.modifiers.remove(mod)
137  #print "\t\t Removing '%s' modifier from 'combinatoricRecoTaus'" %mod.name.value()
138 
139  # Redefine tau PV producer
140  process.hpsPFTauPrimaryVertexProducer.__dict__['_TypedParameterizable__type'] = 'PFTauMiniAODPrimaryVertexProducer'
141  process.hpsPFTauPrimaryVertexProducer.PVTag = 'offlineSlimmedPrimaryVertices'
142  process.hpsPFTauPrimaryVertexProducer.packedCandidatesTag = cms.InputTag("packedPFCandidates")
143  process.hpsPFTauPrimaryVertexProducer.lostCandidatesTag = cms.InputTag("lostTracks")
144 
145  # Redefine tau SV producer
146  process.hpsPFTauSecondaryVertexProducer = cms.EDProducer("PFTauSecondaryVertexProducer",
147  PFTauTag = cms.InputTag("hpsPFTauProducer")
148  )
149 
150  # Remove RecoTau producers which are not supported (yet?), i.e. against-e/mu discriminats
151  for moduleName in process.TauReco.moduleNames():
152  if 'ElectronRejection' in moduleName or 'MuonRejection' in moduleName:
153  if 'ByDeadECALElectronRejection' in moduleName: continue
154  process.miniAODTausTask.remove(getattr(process, moduleName))
155 
156  # Instead add against-mu discriminants which are MiniAOD compatible
157  from RecoTauTag.RecoTau.hpsPFTauDiscriminationByMuonRejectionSimple_cff import hpsPFTauDiscriminationByMuonRejectionSimple
158 
159  process.hpsPFTauDiscriminationByMuonRejectionSimple = hpsPFTauDiscriminationByMuonRejectionSimple
160  process.miniAODTausTask.add(process.hpsPFTauDiscriminationByMuonRejectionSimple)
161 
162 
164 
165  process.tauGenJets.GenParticles = cms.InputTag("prunedGenParticles")
166  process.tauMatch.matched = cms.InputTag("prunedGenParticles")
167 
168  # Remove unsupported tauIDs
169  for name, src in process.patTaus.tauIDSources.parameters_().items():
170  if name.find('againstElectron') > -1 or name.find('againstMuon') > -1:
171  if name.find('againstElectronDeadECAL') > -1: continue
172  delattr(process.patTaus.tauIDSources,name)
173  # Add MiniAOD specific ones
174  setattr(process.patTaus.tauIDSources,'againstMuonLooseSimple',
175  cms.PSet(inputTag = cms.InputTag('hpsPFTauDiscriminationByMuonRejectionSimple'),
176  provenanceConfigLabel = cms.string('IDWPdefinitions'),
177  idLabel = cms.string('ByLooseMuonRejectionSimple')
178  ))
179  setattr(process.patTaus.tauIDSources,'againstMuonTightSimple',
180  cms.PSet(inputTag = cms.InputTag('hpsPFTauDiscriminationByMuonRejectionSimple'),
181  provenanceConfigLabel = cms.string('IDWPdefinitions'),
182  idLabel = cms.string('ByTightMuonRejectionSimple')
183  ))
184 
185  # Run TauIDs (anti-e && deepTau) on top of selectedPatTaus
186  _updatedTauName = 'selectedPatTausNewIDs'
187  _noUpdatedTauName = 'selectedPatTausNoNewIDs'
188  import RecoTauTag.RecoTau.tools.runTauIdMVA as tauIdConfig
189  tauIdEmbedder = tauIdConfig.TauIDEmbedder(
190  process, debug = False,
191  updatedTauName = _updatedTauName,
192  toKeep = ['againstEle2018','deepTau2017v2p1']
193  )
194  tauIdEmbedder.runTauID()
195  setattr(process, _noUpdatedTauName, process.selectedPatTaus.clone())
196  process.miniAODTausTask.add(getattr(process,_noUpdatedTauName))
197  delattr(process, 'selectedPatTaus')
198  process.deepTau2017v2p1.taus = _noUpdatedTauName
199  process.patTauDiscriminationByElectronRejectionMVA62018Raw.PATTauProducer = _noUpdatedTauName
200  process.patTauDiscriminationByElectronRejectionMVA62018.PATTauProducer = _noUpdatedTauName
201  process.selectedPatTaus = getattr(process, _updatedTauName).clone(
202  src = _noUpdatedTauName
203  )
204  process.newTauIDsTask = cms.Task(
205  process.rerunMvaIsolationTask,
206  process.selectedPatTaus
207  )
208  process.miniAODTausTask.add(process.newTauIDsTask)
209 
210  #print '[adaptTauToMiniAODReReco]: Done!'
211 
212 
213 def setOutputModule(mode=0):
214  #mode = 0: store original MiniAOD and new selectedPatTaus
215  #mode = 1: store original MiniAOD, new selectedPatTaus, and all PFTau products as in AOD (except of unsuported ones), plus a few additional collections (charged hadrons, pi zeros, combinatoric reco taus)
216 
218  output = cms.OutputModule(
219  'PoolOutputModule',
220  fileName=cms.untracked.string('miniAOD_TauReco.root'),
221  fastCloning=cms.untracked.bool(False),
222  dataset=cms.untracked.PSet(
223  dataTier=cms.untracked.string('MINIAODSIM'),
224  filterName=cms.untracked.string('')
225  ),
226  outputCommands = evtContent.MINIAODSIMEventContent.outputCommands,
227  SelectEvents=cms.untracked.PSet(
228  SelectEvents=cms.vstring('*',)
229  )
230  )
231  output.outputCommands.append('keep *_selectedPatTaus_*_*')
232  if mode==1:
233  for prod in evtContent.RecoTauTagAOD.outputCommands:
234  if prod.find('ElectronRejection') > -1 and prod.find('DeadECAL') == -1:
235  continue
236  if prod.find('MuonRejection') > -1:
237  continue
238  output.outputCommands.append(prod)
239  output.outputCommands.append('keep *_hpsPFTauDiscriminationByMuonRejectionSimple_*_*')
240  output.outputCommands.append('keep *_combinatoricReco*_*_*')
241  output.outputCommands.append('keep *_ak4PFJetsRecoTauChargedHadrons_*_*')
242  output.outputCommands.append('keep *_ak4PFJetsLegacyHPSPiZeros_*_*')
243  output.outputCommands.append('keep *_patJetsPAT_*_*')
244 
245  return output
246 
247 
adaptToRunAtMiniAOD.adaptTauToMiniAODReReco
def adaptTauToMiniAODReReco(process, reclusterJets=True)
Definition: adaptToRunAtMiniAOD.py:39
mps_monitormerge.items
list items
Definition: mps_monitormerge.py:29
clone
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
EventContent_cff
ak4PFJets_cfi
jetProducer_cfi
adaptToRunAtMiniAOD.convertModuleToMiniAODInput
def convertModuleToMiniAODInput(process, name)
Definition: adaptToRunAtMiniAOD.py:29
adaptToRunAtMiniAOD.addTauReReco
def addTauReReco(process)
Tools to adapt Tau sequences to run tau ReReco+PAT at MiniAOD samples M.
Definition: adaptToRunAtMiniAOD.py:11
adaptToRunAtMiniAOD.setOutputModule
def setOutputModule(mode=0)
Definition: adaptToRunAtMiniAOD.py:213