Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-17 02:28:33

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                 tauProducer.addGenJetMatch   = False
0128                 tauProducer.embedGenJetMatch = False
0129                 attrsToDelete += [tauProducer.genJetMatch.getModuleLabel()]
0130                 tauProducer.genJetMatch      = ''
0131                 attrsToDelete += ['tauGenJets'+postfix]
0132                 attrsToDelete += ['tauGenJetsSelectorAllHadrons'+postfix]
0133             #Boosted Taus
0134             if( names[obj] == 'TausBoosted'      or names[obj] == 'All' ):
0135                 print("removing MC dependencies for taus boosted %s" %postfix)
0136                 if hasattr(process, 'tauMatchBoosted'+postfix) and hasattr(process, 'patTausBoosted'+postfix) :
0137                     _removeMCMatchingForPATObject(process, 'tauMatchBoosted', 'patTausBoosted', postfix)
0138                     ## remove mc extra configs for taus
0139                     tauProducer = getattr(process,'patTausBoosted'+postfix)
0140                     tauProducer.addGenJetMatch   = False
0141                     tauProducer.embedGenJetMatch = False
0142                     attrsToDelete += [tauProducer.genJetMatch.getModuleLabel()]
0143                     tauProducer.genJetMatch      = ''
0144                     attrsToDelete += ['tauGenJetsBoosted'+postfix]
0145                     attrsToDelete += ['tauGenJetsSelectorAllHadronsBoosted'+postfix]
0146                 else :
0147                     print("...skipped since taus boosted %s" %postfix, "are not part of process.")  
0148             if( names[obj] == 'Jets'      or names[obj] == 'All' ):
0149                 print("removing MC dependencies for jets")
0150                 jetPostfixes = []
0151                 for mod in process.producerNames().split():
0152                     if mod.startswith('patJets') and getattr(process,mod).type_() == "PATJetProducer":
0153                         jetPostfixes.append(getattr(process, mod).label_().replace("patJets",""))
0154                 for pfix in jetPostfixes:
0155                     ## remove mc extra configs for jets
0156                     jetProducer = getattr(process, jetCollectionString()+pfix)
0157                     jetProducer.addGenPartonMatch   = False
0158                     jetProducer.embedGenPartonMatch = False
0159                     #attrsToDelete += [jetProducer.genPartonMatch.getModuleLabel()] #MM needed for potential jet backuping
0160                     jetProducer.genPartonMatch      = ''
0161                     jetProducer.addGenJetMatch      = False
0162                     #attrsToDelete += [jetProducer.genJetMatch.getModuleLabel()]  #MM needed for potential jet backuping
0163                     jetProducer.genJetMatch         = ''
0164                     jetProducer.getJetMCFlavour     = False
0165                     jetProducer.useLegacyJetMCFlavour = False
0166                     jetProducer.addJetFlavourInfo   = False
0167                     #attrsToDelete += [jetProducer.JetPartonMapSource.getModuleLabel()]  #MM needed for potential jet backuping
0168                     jetProducer.JetPartonMapSource  = ''
0169                     #attrsToDelete += [jetProducer.JetFlavourInfoSource.getModuleLabel()]  #MM needed for potential jet backuping
0170                     jetProducer.JetFlavourInfoSource = ''
0171                     attrsToDelete += ['slimmedGenJets'+pfix]
0172                 ## adjust output
0173                 for outMod in outputModules:
0174                     if hasattr(process,outMod):
0175                         getattr(process,outMod).outputCommands.append("drop *_selectedPatJets*_genJets_*")
0176                         getattr(process,outMod).outputCommands.append("drop recoGenJets_*_*_*")
0177                     else:
0178                         raise KeyError("process has no OutModule named " + outMod)
0179             
0180             if ( names[obj] == 'JetsAK8' or names[obj] == 'All' ):
0181                 print("removing MC dependencies for AK8 jets")
0182                 attrsToDelete += ['slimmedGenJetsAK8' + postfix]
0183 
0184             if( names[obj] == 'METs'      or names[obj] == 'All' ):
0185                 for mod in process.producerNames().split():
0186                     if mod.startswith('pat') and getattr(process,mod).type_() == "PATMETProducer":
0187                         ## remove mc extra configs for MET
0188                         metProducer = getattr(process, mod)
0189                         metProducer.addGenMET           = False
0190                         attrsToDelete += [metProducer.genMETSource.getModuleLabel()]
0191                         metProducer.genMETSource        = ''
0192         attrsToDelete += [
0193             'prunedGenParticles',
0194             'prunedGenParticlesWithStatusOne',
0195             'packedGenParticles',
0196             'packedPFCandidateToGenAssociation',
0197             'lostTracksToGenAssociation',
0198         ]
0199         for attr in attrsToDelete:
0200             if hasattr(process,attr): delattr(process,attr)
0201 
0202 removeMCMatching=RemoveMCMatching()
0203 
0204 def _removeMCMatchingForPATObject(process, matcherName, producerName, postfix=""):
0205     if hasattr(process, matcherName+postfix):
0206         objectMatcher = getattr(process, matcherName+postfix)
0207     if hasattr(process, producerName+postfix):
0208         objectProducer = getattr(process, producerName+postfix)
0209         objectProducer.addGenMatch      = False
0210         objectProducer.embedGenMatch    = False
0211         attr = objectProducer.genParticleMatch.getModuleLabel()
0212         objectProducer.genParticleMatch = ''
0213     if hasattr(process,attr): delattr(process,attr)
0214