File indexing completed on 2024-11-27 03:17:59
0001 from builtins import range
0002 from PhysicsTools.PatAlgos.tools.ConfigToolBase import *
0003
0004 from PhysicsTools.PatAlgos.tools.helpers import *
0005
0006 class RunOnData(ConfigToolBase):
0007
0008 """ Remove monte carlo matching from a given collection or all PAT
0009 candidate collections and adapt the JEC's:
0010 """
0011 _label='runOnData'
0012 _defaultParameters=dicttypes.SortedKeysDict()
0013 def __init__(self):
0014 ConfigToolBase.__init__(self)
0015 self.addParameter(self._defaultParameters,'names',['All'], "collection name; supported are 'Photons', 'Electrons', 'LowPtElectrons', 'Muons', 'Taus', 'TausBoosted', 'Jets', 'METs', 'All', 'PFAll', 'PFElectrons','PFTaus','PFMuons'", allowedValues=['Photons', 'Electrons', 'LowPtElectrons', 'Muons', 'Taus', 'TausBoosted', 'Jets', 'METs', 'All', 'PFAll', 'PFElectrons','PFTaus','PFMuons'])
0016 self.addParameter(self._defaultParameters,'postfix',"", "postfix of default sequence")
0017 self.addParameter(self._defaultParameters,'outputModules',['out'], "names of all output modules specified to be adapted (default is ['out'])")
0018 self._parameters=copy.deepcopy(self._defaultParameters)
0019 self._comment = ""
0020
0021 def getDefaultParameters(self):
0022 return self._defaultParameters
0023
0024 def __call__(self,process,
0025 names = None,
0026 postfix = None,
0027 outputModules = None) :
0028 if names is None:
0029 names=self._defaultParameters['names'].value
0030 if postfix is None:
0031 postfix=self._defaultParameters['postfix'].value
0032 if outputModules is None:
0033 outputModules=self._defaultParameters['outputModules'].value
0034 self.setParameter('names',names)
0035 self.setParameter('postfix',postfix)
0036 self.setParameter('outputModules',outputModules)
0037 self.apply(process)
0038
0039 def toolCode(self, process):
0040 names=self._parameters['names'].value
0041 postfix=self._parameters['postfix'].value
0042 outputModules=self._parameters['outputModules'].value
0043
0044 print('******************* RunOnData *******************')
0045 removeMCMatching(process, names=names, postfix=postfix, outputModules=outputModules)
0046 for mod in process.producerNames().split():
0047 if mod.startswith('patJetCorrFactors'):
0048 prefix = getattr(process, mod).payload.pythonValue().replace("'","")
0049 if 'L3Absolute' in getattr(process,mod).levels:
0050 if not 'L2L3Residual' in getattr(process,mod).levels:
0051 getattr(process,mod).levels.insert(getattr(process,mod).levels.index('L3Absolute')+1, 'L2L3Residual')
0052 print('adding L2L3Residual JEC for:', getattr(process,mod).label_())
0053 if hasattr(process, prefix+'CombinedCorrector'+postfix):
0054 if prefix+'L3Absolute' in getattr(process,prefix+'CombinedCorrector'+postfix).correctors:
0055 if not prefix+'L2L3Residual' in getattr(process,prefix+'CombinedCorrector'+postfix).correctors:
0056 idx = getattr(process,prefix+'CombinedCorrector'+postfix).correctors.index(prefix+'L3Absolute')+1
0057 getattr(process,prefix+'CombinedCorrector'+postfix).correctors.insert(idx, prefix+'L2L3Residual')
0058 print('adding L2L3Residual for TypeI MET correction:', getattr(process,prefix+'CombinedCorrector'+postfix).label_())
0059
0060 runOnData=RunOnData()
0061
0062
0063 class RemoveMCMatching(ConfigToolBase):
0064
0065 """ Remove monte carlo matching from a given collection or all PAT
0066 candidate collections:
0067 """
0068 _label='removeMCMatching'
0069 _defaultParameters=dicttypes.SortedKeysDict()
0070 def __init__(self):
0071 ConfigToolBase.__init__(self)
0072 self.addParameter(self._defaultParameters,'names',['All'],
0073 "collection name; supported are 'Photons', 'OOTPhotons', 'Electrons', 'LowPtElectrons', 'Muons', 'Taus', 'TausBoosted', 'Jets', 'JetsAK8', 'METs', 'All', 'PFAll', 'PFElectrons','PFTaus','PFMuons'",
0074 allowedValues=['Photons', 'OOTPhotons' ,'Electrons', 'LowPtElectrons', 'Muons', 'Taus', 'TausBoosted', 'Jets', 'JetsAK8', 'METs', 'All', 'PFAll', 'PFElectrons','PFTaus','PFMuons'])
0075 self.addParameter(self._defaultParameters,'postfix',"", "postfix of default sequence")
0076 self.addParameter(self._defaultParameters,'outputModules',['out'], "names of all output modules specified to be adapted (default is ['out'])")
0077 self._parameters=copy.deepcopy(self._defaultParameters)
0078 self._comment = ""
0079
0080 def getDefaultParameters(self):
0081 return self._defaultParameters
0082
0083 def __call__(self,process,
0084 names = None,
0085 postfix = None,
0086 outputModules = None) :
0087 if names is None:
0088 names=self._defaultParameters['names'].value
0089 if postfix is None:
0090 postfix=self._defaultParameters['postfix'].value
0091 if outputModules is None:
0092 outputModules=self._defaultParameters['outputModules'].value
0093 self.setParameter('names',names)
0094 self.setParameter('postfix',postfix)
0095 self.setParameter('outputModules',outputModules)
0096 self.apply(process)
0097
0098 def toolCode(self, process):
0099 names=self._parameters['names'].value
0100 postfix=self._parameters['postfix'].value
0101 outputModules=self._parameters['outputModules'].value
0102
0103 print("************** MC dependence removal ************")
0104 attrsToDelete = []
0105 for obj in range(len(names)):
0106 if( names[obj] == 'Photons' or names[obj] == 'All' ):
0107 print("removing MC dependencies for photons")
0108 _removeMCMatchingForPATObject(process, 'photonMatch', 'patPhotons', postfix)
0109 if( names[obj] == 'OOTPhotons' or names[obj] == 'All' ):
0110 print("removing MC dependencies for out of time photons")
0111 _removeMCMatchingForPATObject(process, 'ootPhotonMatch', 'patOOTPhotons', postfix)
0112 if( names[obj] == 'Electrons' or names[obj] == 'All' ):
0113 print("removing MC dependencies for electrons")
0114 _removeMCMatchingForPATObject(process, 'electronMatch', 'patElectrons', postfix)
0115 if( names[obj] == 'LowPtElectrons' or names[obj] == 'All' ):
0116 print("removing MC dependencies for LowPtElectrons")
0117 _removeMCMatchingForPATObject(process, 'lowPtElectronMatch', 'patLowPtElectrons', postfix)
0118 if( names[obj] == 'Muons' or names[obj] == 'All' ):
0119 print("removing MC dependencies for muons")
0120 _removeMCMatchingForPATObject(process, 'muonMatch', 'patMuons', postfix)
0121 if( names[obj] == 'Taus' or names[obj] == 'All' ):
0122 print("removing MC dependencies for taus")
0123 _removeMCMatchingForPATObject(process, 'tauMatch', 'patTaus', postfix)
0124
0125 tauProducer = getattr(process,'patTaus'+postfix)
0126 if hasattr(tauProducer,"addGenJetMatch"):
0127 tauProducer.addGenJetMatch = False
0128 if hasattr(tauProducer,"embedGenJetMatch"):
0129 tauProducer.embedGenJetMatch = False
0130 attrsToDelete += [tauProducer.genJetMatch.getModuleLabel()]
0131 if hasattr(tauProducer,"genJetMatch"):
0132 tauProducer.genJetMatch = ''
0133 attrsToDelete += ['tauGenJets'+postfix]
0134 attrsToDelete += ['tauGenJetsSelectorAllHadrons'+postfix]
0135
0136 if( names[obj] == 'TausBoosted' or names[obj] == 'All' ):
0137 print("removing MC dependencies for taus boosted %s" %postfix)
0138 if hasattr(process, 'tauMatchBoosted'+postfix) and hasattr(process, 'patTausBoosted'+postfix) :
0139 _removeMCMatchingForPATObject(process, 'tauMatchBoosted', 'patTausBoosted', postfix)
0140
0141 tauProducer = getattr(process,'patTausBoosted'+postfix)
0142 if hasattr(tauProducer,"addGenJetMatch"):
0143 tauProducer.addGenJetMatch = False
0144 if hasattr(tauProducer,"embedGenJetMatch"):
0145 tauProducer.embedGenJetMatch = False
0146 attrsToDelete += [tauProducer.genJetMatch.getModuleLabel()]
0147 if hasattr(tauProducer,"genJetMatch"):
0148 tauProducer.genJetMatch = ''
0149 attrsToDelete += ['tauGenJetsBoosted'+postfix]
0150 attrsToDelete += ['tauGenJetsSelectorAllHadronsBoosted'+postfix]
0151 else :
0152 print("...skipped since taus boosted %s" %postfix, "are not part of process.")
0153 if( names[obj] == 'Jets' or names[obj] == 'All' ):
0154 print("removing MC dependencies for jets")
0155 jetPostfixes = []
0156 for mod in process.producerNames().split():
0157 if mod.startswith('patJets') and getattr(process,mod).type_() == "PATJetProducer":
0158 jetPostfixes.append(getattr(process, mod).label_().replace("patJets",""))
0159 for pfix in jetPostfixes:
0160
0161 jetProducer = getattr(process, jetCollectionString()+pfix)
0162 if hasattr(jetProducer,"addGenPartonMatch"):
0163 jetProducer.addGenPartonMatch = False
0164 if hasattr(jetProducer,"embedGenPartonMatch"):
0165 jetProducer.embedGenPartonMatch = False
0166
0167 if hasattr(jetProducer,"genPartonMatch"):
0168 jetProducer.genPartonMatch = ''
0169 if hasattr(jetProducer,"addGenJetMatch"):
0170 jetProducer.addGenJetMatch = False
0171
0172 if hasattr(jetProducer,"genJetMatch"):
0173 jetProducer.genJetMatch = ''
0174 if hasattr(jetProducer,"getJetMCFlavour"):
0175 jetProducer.getJetMCFlavour = False
0176 if hasattr(jetProducer,"useLegacyJetMCFlavour"):
0177 jetProducer.useLegacyJetMCFlavour = False
0178 if hasattr(jetProducer,"addJetFlavourInfo"):
0179 jetProducer.addJetFlavourInfo = False
0180
0181 if hasattr(jetProducer,"JetPartonMapSource"):
0182 jetProducer.JetPartonMapSource = ''
0183
0184 if hasattr(jetProducer,"JetFlavourInfoSource"):
0185 jetProducer.JetFlavourInfoSource = ''
0186 attrsToDelete += ['slimmedGenJets'+pfix]
0187
0188 for outMod in outputModules:
0189 if hasattr(process,outMod):
0190 getattr(process,outMod).outputCommands.append("drop *_selectedPatJets*_genJets_*")
0191 getattr(process,outMod).outputCommands.append("drop recoGenJets_*_*_*")
0192 else:
0193 raise KeyError("process has no OutModule named " + outMod)
0194 if ( names[obj] == 'JetsAK8' or names[obj] == 'All' ):
0195 print("removing MC dependencies for AK8 jets")
0196 attrsToDelete += ['slimmedGenJetsAK8' + postfix]
0197
0198 if( names[obj] == 'METs' or names[obj] == 'All' ):
0199 for mod in process.producerNames().split():
0200 if mod.startswith('pat') and getattr(process,mod).type_() == "PATMETProducer":
0201
0202 metProducer = getattr(process, mod)
0203 metProducer.addGenMET = False
0204 attrsToDelete += [metProducer.genMETSource.getModuleLabel()]
0205 metProducer.genMETSource = ''
0206 attrsToDelete += [
0207 'prunedGenParticles',
0208 'prunedGenParticlesWithStatusOne',
0209 'packedGenParticles',
0210 'packedPFCandidateToGenAssociation',
0211 'lostTracksToGenAssociation',
0212 ]
0213 for attr in attrsToDelete:
0214 if hasattr(process,attr): delattr(process,attr)
0215
0216 removeMCMatching=RemoveMCMatching()
0217
0218 def _removeMCMatchingForPATObject(process, matcherName, producerName, postfix=""):
0219 if hasattr(process, matcherName+postfix):
0220 objectMatcher = getattr(process, matcherName+postfix)
0221 if hasattr(process, producerName+postfix):
0222 objectProducer = getattr(process, producerName+postfix)
0223 if hasattr(objectProducer,"addGenMatch"):
0224 objectProducer.addGenMatch = False
0225 if hasattr(objectProducer,"addGenMatch"):
0226 objectProducer.addGenMatch = False
0227 attr = objectProducer.genParticleMatch.getModuleLabel()
0228 if hasattr(objectProducer,"genParticleMatch"):
0229 objectProducer.genParticleMatch = ''
0230 if hasattr(process,attr): delattr(process,attr)
0231