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 ######
5 # Tools to adapt Tau sequences to run tau ReReco+PAT at MiniAOD samples
6 # M. Bluj, NCBJ Warsaw
7 # based on work of J. Steggemann, CERN
8 # Created: 9 Nov. 2017
9 ######
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  process.miniAODTausTask.remove(getattr(process, moduleName))
155 
156  # Instead add against-mu discriminants which are MiniAOD compatible
157  from RecoTauTag.RecoTau.hpsPFTauDiscriminationByAMuonRejectionSimple_cff import hpsPFTauDiscriminationByLooseMuonRejectionSimple, hpsPFTauDiscriminationByTightMuonRejectionSimple
158 
159  process.hpsPFTauDiscriminationByLooseMuonRejectionSimple = hpsPFTauDiscriminationByLooseMuonRejectionSimple
160  process.hpsPFTauDiscriminationByTightMuonRejectionSimple = hpsPFTauDiscriminationByTightMuonRejectionSimple
161  process.miniAODTausTask.add(process.hpsPFTauDiscriminationByLooseMuonRejectionSimple)
162  process.miniAODTausTask.add(process.hpsPFTauDiscriminationByTightMuonRejectionSimple)
163 
164  #####
165  # PAT part in the following
166 
167  process.tauGenJets.GenParticles = cms.InputTag("prunedGenParticles")
168  process.tauMatch.matched = cms.InputTag("prunedGenParticles")
169 
170  # Remove unsupported tauIDs
171  for name, src in six.iteritems(process.patTaus.tauIDSources.parameters_()):
172  if name.find('againstElectron') > -1 or name.find('againstMuon') > -1:
173  delattr(process.patTaus.tauIDSources,name)
174  # Add MiniAOD specific ones
175  setattr(process.patTaus.tauIDSources,'againstMuonLooseSimple',cms.InputTag('hpsPFTauDiscriminationByLooseMuonRejectionSimple'))
176  setattr(process.patTaus.tauIDSources,'againstMuonTightSimple',cms.InputTag('hpsPFTauDiscriminationByTightMuonRejectionSimple'))
177 
178  #print '[adaptTauToMiniAODReReco]: Done!'
179 
180 #####
181 def setOutputModule(mode=0):
182  #mode = 0: store original MiniAOD and new selectedPatTaus
183  #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)
184 
186  output = cms.OutputModule(
187  'PoolOutputModule',
188  fileName=cms.untracked.string('miniAOD_TauReco.root'),
189  fastCloning=cms.untracked.bool(False),
190  dataset=cms.untracked.PSet(
191  dataTier=cms.untracked.string('MINIAODSIM'),
192  filterName=cms.untracked.string('')
193  ),
194  outputCommands = evtContent.MINIAODSIMEventContent.outputCommands,
195  SelectEvents=cms.untracked.PSet(
196  SelectEvents=cms.vstring('*',)
197  )
198  )
199  output.outputCommands.append('keep *_selectedPatTaus_*_*')
200  if mode==1:
201  for prod in evtContent.RecoTauTagAOD.outputCommands:
202  if prod.find('ElectronRejection') > -1:
203  continue
204  if prod.find('MuonRejection') > -1:
205  continue
206  output.outputCommands.append(prod)
207  output.outputCommands.append('keep *_hpsPFTauDiscriminationByLooseMuonRejectionSimple_*_*')
208  output.outputCommands.append('keep *_hpsPFTauDiscriminationByTightMuonRejectionSimple_*_*')
209  output.outputCommands.append('keep *_combinatoricReco*_*_*')
210  output.outputCommands.append('keep *_ak4PFJetsRecoTauChargedHadrons_*_*')
211  output.outputCommands.append('keep *_ak4PFJetsLegacyHPSPiZeros_*_*')
212  output.outputCommands.append('keep *_patJetsPAT_*_*')
213 
214  return output
215 
216 #####
def adaptTauToMiniAODReReco(process, reclusterJets=True)
def convertModuleToMiniAODInput(process, name)
def addTauReReco(process)
Tools to adapt Tau sequences to run tau ReReco+PAT at MiniAOD samples M.