CMS 3D CMS Logo

customizeMiniAOD_MuEGFixMoriond2017.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 
3 from PhysicsTools.PatAlgos.tools.helpers import MassSearchReplaceAnyInputTagVisitor, cloneProcessingSnippet, addKeepStatement
4 from PhysicsTools.PatAlgos.slimming.extraJets_MuEGFixMoriond2017 import backupJetsFirstStep, backupJetsSecondStep
5 #from RecoEgamma.EgammaTools.egammaGainSwitchFixToolsForPAT_cff import customizeGSFixForPAT
6 from PhysicsTools.PatAlgos.tools.helpers import getPatAlgosToolsTask, addToProcessAndTask
7 import sys
8 import six
9 
10 def addBadMuonFilters(process):
11 
12  task = getPatAlgosToolsTask(process)
13 
14  process.load("RecoMET.METFilters.badGlobalMuonTaggersAOD_cff")
15  task.add(process.badGlobalMuonTagger)
16  task.add(process.cloneGlobalMuonTagger)
17  process.Flag_noBadMuons = cms.Path(process.noBadGlobalMuons)
18  process.Flag_badMuons = cms.Path(process.badGlobalMuonTagger)
19  process.Flag_duplicateMuons = cms.Path(process.cloneGlobalMuonTagger)
20  for P in process.Flag_noBadMuons, process.Flag_badMuons, process.Flag_duplicateMuons:
21  process.schedule.insert(0, P)
22 
23 def cleanPFCandidates(process, badMuons, verbose=False):
24 
25  task = getPatAlgosToolsTask(process)
26 
27  process.load("CommonTools.ParticleFlow.muonsCleaned_cfi")
28  task.add(process.muonsCleaned)
29  process.patMuons.userData.userInts.src = [ cms.InputTag("muonsCleaned:oldPF") ]
30 
31  process.load("CommonTools.ParticleFlow.pfCandidatesBadMuonsCleaned_cfi")
32  task.add(process.pfCandidatesBadMuonsCleaned)
33  process.muonsCleaned.badmuons = badMuons
34  replaceMuons = MassSearchReplaceAnyInputTagVisitor("muons", "muonsCleaned", verbose=verbose)
35  needOriginalMuons = [ process.muonsCleaned ] + [ getattr(process,l.moduleLabel) for l in badMuons ]
36  replacePFCandidates = MassSearchReplaceAnyInputTagVisitor("particleFlow", "pfCandidatesBadMuonsCleaned", verbose=verbose)
37  for everywhere in [ process.producers, process.filters, process.analyzers, process.psets, process.vpsets ]:
38  for name,obj in six.iteritems(everywhere):
39  if obj not in needOriginalMuons:
40  replaceMuons.doIt(obj, name)
41  if obj != process.pfCandidatesBadMuonsCleaned:
42  replacePFCandidates.doIt(obj, name)
43 
44  process.load("CommonTools.ParticleFlow.pfEGammaToCandidateRemapper_cfi")
45  task.add(process.pfEGammaToCandidateRemapper)
46  process.pfEGammaToCandidateRemapper.pf2pf = cms.InputTag("pfCandidatesBadMuonsCleaned")
47  process.reducedEgamma.gsfElectronsPFValMap = cms.InputTag("pfEGammaToCandidateRemapper","electrons")
48  process.reducedEgamma.photonsPFValMap = cms.InputTag("pfEGammaToCandidateRemapper","photons")
49  if hasattr(process,"gedGsfElectronsFixed"):
50  # also reconfigure pfEGammaToCandidateRemapper because of GS Fix
51  # first the old one
52  process.pfEGammaToCandidateRemapperBeforeGSFix = process.pfEGammaToCandidateRemapper.clone()
53  task.add(process.pfEGammaToCandidateRemapperBeforeGSFix)
54  process.reducedEgammaBeforeGSFix.gsfElectronsPFValMap = cms.InputTag("pfEGammaToCandidateRemapperBeforeGSFix","electrons")
55  process.reducedEgammaBeforeGSFix.photonsPFValMap = cms.InputTag("pfEGammaToCandidateRemapperBeforeGSFix","photons")
56  # then the new one
57  process.pfEGammaToCandidateRemapper.electrons = cms.InputTag("gedGsfElectronsFixed")
58  process.pfEGammaToCandidateRemapper.photons = cms.InputTag("gedPhotonsFixed")
59  process.pfEGammaToCandidateRemapper.electron2pf = cms.InputTag("particleBasedIsolationGSFixed","gedGsfElectrons")
60  process.pfEGammaToCandidateRemapper.photon2pf = cms.InputTag("particleBasedIsolationGSFixed","gedPhotons")
61  else:
62  sys.stderr.write("WARNING : attempt to use gain switch corrected electron/photon collection gedGsfElectronsFixed, but the current process does not contain such collection")
63 
64 def addDiscardedPFCandidates(process, inputCollection, verbose=False):
65 
66  task = getPatAlgosToolsTask(process)
67 
68  process.primaryVertexAssociationDiscardedCandidates = process.primaryVertexAssociation.clone(
69  particles = inputCollection,
70  )
71  task.add(process.primaryVertexAssociationDiscardedCandidates)
72  process.packedPFCandidatesDiscarded = process.packedPFCandidates.clone(
73  inputCollection = inputCollection,
74  PuppiNoLepSrc = cms.InputTag(""),
75  PuppiSrc = cms.InputTag(""),
76  secondaryVerticesForWhiteList = cms.VInputTag(),
77  vertexAssociator = cms.InputTag("primaryVertexAssociationDiscardedCandidates","original")
78  )
79  task.add(process.packedPFCandidatesDiscarded)
80  addKeepStatement(process, "keep patPackedCandidates_packedPFCandidates_*_*",
81  ["keep patPackedCandidates_packedPFCandidatesDiscarded_*_*"],
82  verbose=verbose)
83  # Now make the mixed map for rekeying
84  from PhysicsTools.PatAlgos.slimming.packedPFCandidateRefMixer_cfi import packedPFCandidateRefMixer
85  process.oldPFCandToPackedOrDiscarded = packedPFCandidateRefMixer.clone(
86  pf2pf = cms.InputTag(inputCollection.moduleLabel),
87  pf2packed = cms.VInputTag(cms.InputTag("packedPFCandidates"), cms.InputTag("packedPFCandidatesDiscarded"))
88  )
89  task.add(process.oldPFCandToPackedOrDiscarded)
90  # Fix slimmed muon keying
91  process.slimmedMuons.pfCandidates = cms.VInputTag(cms.InputTag(inputCollection.moduleLabel), inputCollection)
92  process.slimmedMuons.packedPFCandidates = cms.VInputTag(cms.InputTag("packedPFCandidates"), cms.InputTag("packedPFCandidatesDiscarded"))
93 
94  #MM point to uncleaned collection for hadronic taus, to avoid remaking them
95  #no impact expected, as no muons are included here
96  process.slimmedTaus.packedPFCandidates=cms.InputTag("packedPFCandidatesBackup")
97 
98 def loadJetMETBTag(process):
99 
100  task = getPatAlgosToolsTask(process)
101 
103  process.ak4PFJetsCHS = RecoJets.Configuration.RecoPFJets_cff.ak4PFJetsCHS.clone()
104  task.add(process.ak4PFJetsCHS)
105  process.ak8PFJetsCHS = RecoJets.Configuration.RecoPFJets_cff.ak8PFJetsCHS.clone()
106  task.add(process.ak8PFJetsCHS)
107  process.load("RecoMET.METProducers.PFMET_cfi")
108  task.add(process.pfMet)
109  process.load("RecoBTag.ImpactParameter.impactParameter_cff")
110  task.add(process.impactParameterTask)
111  process.load("RecoBTag.SecondaryVertex.secondaryVertex_cff")
112  task.add(process.secondaryVertexTask)
113  process.load("RecoBTag.SoftLepton.softLepton_cff")
114  task.add(process.softLeptonTask)
115  process.load("RecoBTag.Combined.combinedMVA_cff")
116  task.add(process.combinedMVATask)
117  process.load("RecoBTag.CTagging.cTagging_cff")
118  task.add(process.cTaggingTask)
119  process.load("RecoVertex.AdaptiveVertexFinder.inclusiveVertexing_cff")
120  task.add(process.inclusiveVertexingTask)
121  task.add(process.inclusiveCandidateVertexingTask)
122  task.add(process.inclusiveCandidateVertexingCvsLTask)
123 
124 def customizeAll(process, verbose=False):
125 
126  #disabled for now, backup in case 90X needs similar fix
127  #process = customizeGSFixForPAT(process)
128 
129  loadJetMETBTag(process)
130  backupJetSequences = backupJetsFirstStep(process)
131 
132  addBadMuonFilters(process)
133  badMuons = cms.VInputTag( cms.InputTag("badGlobalMuonTagger","bad"), cms.InputTag("cloneGlobalMuonTagger","bad") )
134  # clean the muons and PF candidates, and make *everything* point to the new candidates
135  cleanPFCandidates(process, badMuons, verbose=verbose)
136 
137  addDiscardedPFCandidates(process, cms.InputTag("pfCandidatesBadMuonsCleaned","discarded"), verbose=verbose)
138 
139  # now make the backup sequences point to the right place
140  backupJetsSecondStep(process, backupJetSequences, badMuons, verbose=verbose)
141 
142 
143  process.patMuons.embedCaloMETMuonCorrs = False # FIXME
144  ##extra METs and MET corrections ===============================================================
145  from PhysicsTools.PatAlgos.slimming.extraSlimmedMETs_MuEGFixMoriond2017 import addExtraMETCollections,addExtraPuppiMETCorrections
146 
147  ### Gain switch collections not existing in 90X+
148  ### -> corrections are set up to give no change on the MET computation
149  addExtraMETCollections(process,
150  unCleanPFCandidateCollection="particleFlow",
151  cleanElectronCollection="slimmedElectrons",
152  cleanPhotonCollection="slimmedPhotons",
153  unCleanElectronCollection="slimmedElectrons",
154  unCleanPhotonCollection="slimmedPhotons")
155 
157  cleanPFCandidateCollection="particleFlow",
158  unCleanPFCandidateCollection="pfCandidatesBadMuonsCleaned",
159  cleanElectronCollection="slimmedElectrons",
160  cleanPhotonCollection="slimmedPhotons",
161  unCleanElectronCollection="slimmedElectrons",
162  unCleanPhotonCollection="slimmedPhotons")
163 
164  addKeepStatement(process,
165  "keep *_slimmedMETs_*_*",
166  ["keep *_slimmedMETsUncorrected_*_*",
167  "keep *_slimmedMETsEGClean_*_*",
168  "keep *_slimmedMETsMuEGClean_*_*"],
169  verbose=verbose)
170  addKeepStatement(process,
171  "keep *_slimmedMETsPuppi_*_*",
172  ["keep *_puppiMETEGCor_*_*",
173  "keep *_puppiMETMuCor_*_*"],
174  verbose=verbose)
175 
176 
177 
178  #redo the miniAOD data customization for new JEC modules created during the backup process
179  from PhysicsTools.PatAlgos.slimming.miniAOD_tools import miniAOD_customizeData
180  miniAOD_customizeData(process)
181 
182  return process
def addExtraPuppiMETCorrections(process, cleanPFCandidateCollection, unCleanPFCandidateCollection, cleanElectronCollection, cleanPhotonCollection, unCleanElectronCollection, unCleanPhotonCollection)
def addDiscardedPFCandidates(process, inputCollection, verbose=False)
def backupJetsSecondStep(process, sequences, badMuons, verbose=False)
def miniAOD_customizeData(process)
def addKeepStatement(process, oldKeep, newKeeps, verbose=False)
Definition: helpers.py:353
def cleanPFCandidates(process, badMuons, verbose=False)
def getPatAlgosToolsTask(process)
Definition: helpers.py:13
def addExtraMETCollections(process, unCleanPFCandidateCollection, cleanElectronCollection, cleanPhotonCollection, unCleanElectronCollection, unCleanPhotonCollection)