CMS 3D CMS Logo

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