Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-16 05:06:29

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