diff --git a/Configuration/Applications/python/ConfigBuilder.py b/Configuration/Applications/python/ConfigBuilder.py index 7d2f371d03ef7..6f610ef3d001a 100644 --- a/Configuration/Applications/python/ConfigBuilder.py +++ b/Configuration/Applications/python/ConfigBuilder.py @@ -5,6 +5,11 @@ import FWCore.ParameterSet.Config as cms from FWCore.ParameterSet.Modules import _Module + +# The following import is provided for backward compatibility reasons. +# The function used to be defined in this file. +from FWCore.ParameterSet.MassReplace import massReplaceInputTag as MassReplaceInputTag + import sys import re import collections @@ -170,10 +175,6 @@ def filesFromDASQuery(query,option="",s=None): print "found parent files:",sec return (prim,sec) -def MassReplaceInputTag(aProcess,oldT="rawDataCollector",newT="rawDataRepacker"): - from PhysicsTools.PatAlgos.tools.helpers import massReplaceInputTag - massReplaceInputTag(aProcess, oldT, newT) - def anyOf(listOfKeys,dict,opt=None): for k in listOfKeys: if k in dict: diff --git a/FWCore/ParameterSet/python/MassReplace.py b/FWCore/ParameterSet/python/MassReplace.py new file mode 100644 index 0000000000000..963fed16e5d8c --- /dev/null +++ b/FWCore/ParameterSet/python/MassReplace.py @@ -0,0 +1,257 @@ +import FWCore.ParameterSet.Config as cms + +class MassSearchReplaceAnyInputTagVisitor(object): + """Visitor that travels within a cms.Sequence, looks for a parameter and replace its value + It will climb down within PSets, VPSets and VInputTags to find its target""" + def __init__(self,paramSearch,paramReplace,verbose=False,moduleLabelOnly=False,skipLabelTest=False): + self._paramSearch = self.standardizeInputTagFmt(paramSearch) + self._paramReplace = self.standardizeInputTagFmt(paramReplace) + self._moduleName = '' + self._verbose=verbose + self._moduleLabelOnly=moduleLabelOnly + self._skipLabelTest=skipLabelTest + def doIt(self,pset,base): + if isinstance(pset, cms._Parameterizable): + for name in pset.parameterNames_(): + # if I use pset.parameters_().items() I get copies of the parameter values + # so I can't modify the nested pset + value = getattr(pset,name) + type = value.pythonTypeName() + if type == 'cms.PSet': + self.doIt(value,base+"."+name) + elif type == 'cms.VPSet': + for (i,ps) in enumerate(value): self.doIt(ps, "%s.%s[%d]"%(base,name,i) ) + elif type == 'cms.VInputTag': + for (i,n) in enumerate(value): + # VInputTag can be declared as a list of strings, so ensure that n is formatted correctly + n = self.standardizeInputTagFmt(n) + if (n == self._paramSearch): + if self._verbose:print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, self._paramSearch, self._paramReplace) + value[i] = self._paramReplace + elif self._moduleLabelOnly and n.moduleLabel == self._paramSearch.moduleLabel: + nrep = n; nrep.moduleLabel = self._paramReplace.moduleLabel + if self._verbose:print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, n, nrep) + value[i] = nrep + elif type.endswith('.InputTag'): + if value == self._paramSearch: + if self._verbose:print "Replace %s.%s %s ==> %s " % (base, name, self._paramSearch, self._paramReplace) + from copy import deepcopy + if 'untracked' in type: + setattr(pset, name, cms.untracked.InputTag(self._paramReplace.getModuleLabel(), + self._paramReplace.getProductInstanceLabel(), + self._paramReplace.getProcessName())) + else: + setattr(pset, name, deepcopy(self._paramReplace) ) + elif self._moduleLabelOnly and value.moduleLabel == self._paramSearch.moduleLabel: + from copy import deepcopy + repl = deepcopy(getattr(pset, name)) + repl.moduleLabel = self._paramReplace.moduleLabel + setattr(pset, name, repl) + if self._verbose:print "Replace %s.%s %s ==> %s " % (base, name, value, repl) + + + @staticmethod + def standardizeInputTagFmt(inputTag): + ''' helper function to ensure that the InputTag is defined as cms.InputTag(str) and not as a plain str ''' + if not isinstance(inputTag, cms.InputTag): + return cms.InputTag(inputTag) + return inputTag + + def enter(self,visitee): + label = '' + if (not self._skipLabelTest): + try: label = visitee.label_() + except AttributeError: label = '' + else: + label = '' + self.doIt(visitee, label) + def leave(self,visitee): + pass + +def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag,verbose=False,moduleLabelOnly=False,skipLabelTest=False) : + """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags...""" + sequence.visit(MassSearchReplaceAnyInputTagVisitor(oldInputTag,newInputTag,verbose=verbose,moduleLabelOnly=moduleLabelOnly,skipLabelTest=skipLabelTest)) + +def massReplaceInputTag(process,old="rawDataCollector",new="rawDataRepacker",verbose=False,moduleLabelOnly=False,skipLabelTest=False): + for s in process.paths_().keys(): + massSearchReplaceAnyInputTag(getattr(process,s), old, new, verbose, moduleLabelOnly, skipLabelTest) + for s in process.endpaths_().keys(): + massSearchReplaceAnyInputTag(getattr(process,s), old, new, verbose, moduleLabelOnly, skipLabelTest) + if process.schedule_() is not None: + for task in process.schedule_()._tasks: + massSearchReplaceAnyInputTag(task, old, new, verbose, moduleLabelOnly, skipLabelTest) + return(process) + +class MassSearchParamVisitor(object): + """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it""" + def __init__(self,paramName,paramSearch): + self._paramName = paramName + self._paramSearch = paramSearch + self._modules = [] + def enter(self,visitee): + if (hasattr(visitee,self._paramName)): + if getattr(visitee,self._paramName) == self._paramSearch: + self._modules.append(visitee) + def leave(self,visitee): + pass + def modules(self): + return self._modules + +class MassSearchReplaceParamVisitor(object): + """Visitor that travels within a cms.Sequence, looks for a parameter and replaces its value""" + def __init__(self,paramName,paramSearch,paramValue,verbose=False): + self._paramName = paramName + self._paramValue = paramValue + self._paramSearch = paramSearch + self._verbose = verbose + def enter(self,visitee): + if (hasattr(visitee,self._paramName)): + if getattr(visitee,self._paramName) == self._paramSearch: + if self._verbose:print "Replaced %s.%s: %s => %s" % (visitee,self._paramName,getattr(visitee,self._paramName),self._paramValue) + setattr(visitee,self._paramName,self._paramValue) + def leave(self,visitee): + pass + +def massSearchReplaceParam(sequence,paramName,paramOldValue,paramValue,verbose=False): + sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue,verbose)) + +def massReplaceParameter(process,name="label",old="rawDataCollector",new="rawDataRepacker",verbose=False): + for s in process.paths_().keys(): + massSearchReplaceParam(getattr(process,s),name,old,new,verbose) + for s in process.endpaths_().keys(): + massSearchReplaceParam(getattr(process,s),name,old,new,verbose) + if process.schedule_() is not None: + for task in process.schedule_()._tasks: + massSearchReplaceParam(task, name, old, new, verbose) + return(process) + +if __name__=="__main__": + import unittest + class TestModuleCommand(unittest.TestCase): + + def testMassSearchReplaceAnyInputTag(self): + p = cms.Process("test") + p.a = cms.EDProducer("a", src=cms.InputTag("gen")) + p.b = cms.EDProducer("ab", src=cms.InputTag("a")) + p.c = cms.EDProducer("ac", src=cms.InputTag("b"), + nested = cms.PSet(src = cms.InputTag("b"), src2 = cms.InputTag("c")), + nestedv = cms.VPSet(cms.PSet(src = cms.InputTag("b")), cms.PSet(src = cms.InputTag("d"))), + vec = cms.VInputTag(cms.InputTag("a"), cms.InputTag("b"), cms.InputTag("c"), cms.InputTag("d")) + ) + p.s = cms.Sequence(p.a*p.b*p.c) + massSearchReplaceAnyInputTag(p.s, cms.InputTag("b"), cms.InputTag("new")) + self.assertNotEqual(cms.InputTag("new"), p.b.src) + self.assertEqual(cms.InputTag("new"), p.c.src) + self.assertEqual(cms.InputTag("new"), p.c.nested.src) + self.assertEqual(cms.InputTag("new"), p.c.nested.src) + self.assertNotEqual(cms.InputTag("new"), p.c.nested.src2) + self.assertEqual(cms.InputTag("new"), p.c.nestedv[0].src) + self.assertNotEqual(cms.InputTag("new"), p.c.nestedv[1].src) + self.assertNotEqual(cms.InputTag("new"), p.c.vec[0]) + self.assertEqual(cms.InputTag("new"), p.c.vec[1]) + self.assertNotEqual(cms.InputTag("new"), p.c.vec[2]) + self.assertNotEqual(cms.InputTag("new"), p.c.vec[3]) + + def testMassReplaceInputTag(self): + process1 = cms.Process("test") + massReplaceInputTag(process1, "a", "b", False, False, False) + self.assertEqual(process1.dumpPython(), +"""import FWCore.ParameterSet.Config as cms + +process = cms.Process("test") + +""") + p = cms.Process("test") + p.a = cms.EDProducer("a", src=cms.InputTag("gen")) + p.b = cms.EDProducer("ab", src=cms.InputTag("a")) + p.c = cms.EDProducer("ac", src=cms.InputTag("b"), + nested = cms.PSet(src = cms.InputTag("a"), src2 = cms.InputTag("c")), + nestedv = cms.VPSet(cms.PSet(src = cms.InputTag("a")), cms.PSet(src = cms.InputTag("d"))), + vec = cms.VInputTag(cms.InputTag("a"), cms.InputTag("b"), cms.InputTag("c"), cms.InputTag("d")) + ) + p.d = cms.EDProducer("ab", src=cms.InputTag("a")) + p.e = cms.EDProducer("ab", src=cms.InputTag("a")) + p.f = cms.EDProducer("ab", src=cms.InputTag("a")) + p.g = cms.EDProducer("ab", src=cms.InputTag("a")) + p.h = cms.EDProducer("ab", src=cms.InputTag("a")) + p.i = cms.EDProducer("ab", src=cms.InputTag("a")) + p.s1 = cms.Sequence(p.a*p.b*p.c) + p.path1 = cms.Path(p.s1) + p.s2 = cms.Sequence(p.d) + p.path2 = cms.Path(p.e) + p.s3 = cms.Sequence(p.f) + p.endpath1 = cms.EndPath(p.s3) + p.endpath2 = cms.EndPath(p.g) + p.t1 = cms.Task(p.h) + p.t2 = cms.Task(p.i) + p.schedule = cms.Schedule() + p.schedule.associate(p.t1, p.t2) + massReplaceInputTag(p, "a", "b", False, False, False) + self.assertEqual(cms.InputTag("b"), p.b.src) + self.assertEqual(cms.InputTag("b"), p.c.vec[0]) + self.assertEqual(cms.InputTag("c"), p.c.vec[2]) + self.assertEqual(cms.InputTag("a"), p.d.src) + self.assertEqual(cms.InputTag("b"), p.e.src) + self.assertEqual(cms.InputTag("b"), p.f.src) + self.assertEqual(cms.InputTag("b"), p.g.src) + self.assertEqual(cms.InputTag("b"), p.h.src) + self.assertEqual(cms.InputTag("b"), p.i.src) + + def testMassSearchReplaceParam(self): + p = cms.Process("test") + p.a = cms.EDProducer("a", src=cms.InputTag("gen")) + p.b = cms.EDProducer("ab", src=cms.InputTag("a")) + p.c = cms.EDProducer("ac", src=cms.InputTag("b"), + nested = cms.PSet(src = cms.InputTag("c")) + ) + p.s = cms.Sequence(p.a*p.b*p.c) + massSearchReplaceParam(p.s,"src",cms.InputTag("b"),"a") + self.assertEqual(cms.InputTag("a"),p.c.src) + self.assertNotEqual(cms.InputTag("a"),p.c.nested.src) + + def testMassReplaceParam(self): + process1 = cms.Process("test") + massReplaceParameter(process1, "src", cms.InputTag("a"), "b", False) + self.assertEqual(process1.dumpPython(), +"""import FWCore.ParameterSet.Config as cms + +process = cms.Process("test") + +""") + p = cms.Process("test") + p.a = cms.EDProducer("a", src=cms.InputTag("gen")) + p.b = cms.EDProducer("ab", src=cms.InputTag("a")) + p.c = cms.EDProducer("ac", src=cms.InputTag("b"), + nested = cms.PSet(src = cms.InputTag("a"), src2 = cms.InputTag("c")), + nestedv = cms.VPSet(cms.PSet(src = cms.InputTag("a")), cms.PSet(src = cms.InputTag("d"))), + vec = cms.VInputTag(cms.InputTag("a"), cms.InputTag("b"), cms.InputTag("c"), cms.InputTag("d")) + ) + p.d = cms.EDProducer("ab", src=cms.InputTag("a")) + p.e = cms.EDProducer("ab", src=cms.InputTag("a")) + p.f = cms.EDProducer("ab", src=cms.InputTag("a")) + p.g = cms.EDProducer("ab", src=cms.InputTag("a")) + p.h = cms.EDProducer("ab", src=cms.InputTag("a")) + p.i = cms.EDProducer("ab", src=cms.InputTag("a")) + p.s1 = cms.Sequence(p.a*p.b*p.c) + p.path1 = cms.Path(p.s1) + p.s2 = cms.Sequence(p.d) + p.path2 = cms.Path(p.e) + p.s3 = cms.Sequence(p.f) + p.endpath1 = cms.EndPath(p.s3) + p.endpath2 = cms.EndPath(p.g) + p.t1 = cms.Task(p.h) + p.t2 = cms.Task(p.i) + p.schedule = cms.Schedule() + p.schedule.associate(p.t1, p.t2) + massReplaceParameter(p, "src",cms.InputTag("a"), "b", False) + self.assertEqual(cms.InputTag("gen"), p.a.src) + self.assertEqual(cms.InputTag("b"), p.b.src) + self.assertEqual(cms.InputTag("a"), p.c.vec[0]) + self.assertEqual(cms.InputTag("c"), p.c.vec[2]) + self.assertEqual(cms.InputTag("a"), p.d.src) + self.assertEqual(cms.InputTag("b"), p.e.src) + self.assertEqual(cms.InputTag("b"), p.f.src) + self.assertEqual(cms.InputTag("b"), p.g.src) + self.assertEqual(cms.InputTag("b"), p.h.src) + self.assertEqual(cms.InputTag("b"), p.i.src) + unittest.main() diff --git a/HLTrigger/Configuration/python/CustomConfigs.py b/HLTrigger/Configuration/python/CustomConfigs.py index 41b9bae44d179..eb0c88716d5ff 100644 --- a/HLTrigger/Configuration/python/CustomConfigs.py +++ b/HLTrigger/Configuration/python/CustomConfigs.py @@ -1,5 +1,10 @@ import FWCore.ParameterSet.Config as cms +# The following 2 imports are provided for backward compatibility reasons. +# The functions used to be defined in this file. +from FWCore.ParameterSet.MassReplace import massReplaceInputTag as MassReplaceInputTag +from FWCore.ParameterSet.MassReplace import massReplaceParameter as MassReplaceParameter + def ProcessName(process): # processname modifications @@ -109,16 +114,6 @@ def HLTDropPrevious(process): return(process) -def MassReplaceInputTag(process,old="rawDataCollector",new="rawDataRepacker",verbose=False,moduleLabelOnly=False,skipLabelTest=False): - from PhysicsTools.PatAlgos.tools.helpers import massReplaceInputTag - massReplaceInputTag(process, old, new, verbose, moduleLabelOnly, skipLabelTest) - return(process) - -def MassReplaceParameter(process,name="label",old="rawDataCollector",new="rawDataRepacker",verbose=False): - from PhysicsTools.PatAlgos.tools.helpers import massReplaceParameter - massReplaceParameter(process, name, old, new, verbose) - return(process) - def L1REPACK(process,sequence="Full"): from Configuration.StandardSequences.Eras import eras diff --git a/PhysicsTools/PatAlgos/python/tools/helpers.py b/PhysicsTools/PatAlgos/python/tools/helpers.py index 2a3c345d5e995..5e90d98b2a612 100644 --- a/PhysicsTools/PatAlgos/python/tools/helpers.py +++ b/PhysicsTools/PatAlgos/python/tools/helpers.py @@ -4,6 +4,11 @@ ## Helpers to perform some technically boring tasks like looking for all modules with a given parameter ## and replacing that to a given value +# Next two lines are for backward compatibility, the imported functions and +# classes used to be defined in this file. +from FWCore.ParameterSet.MassReplace import massSearchReplaceAnyInputTag, MassSearchReplaceAnyInputTagVisitor +from FWCore.ParameterSet.MassReplace import massSearchReplaceParam, MassSearchParamVisitor, MassSearchReplaceParamVisitor + def getPatAlgosToolsTask(process): taskName = "patAlgosToolsTask" if hasattr(process, taskName): @@ -128,89 +133,6 @@ def __labelsInSequence(process, sequenceLabel, postfix=""): result.extend([ m.label() for m in listSequences( getattr(process,sequenceLabel+postfix))] ) return result -class MassSearchReplaceParamVisitor(object): - """Visitor that travels within a cms.Sequence, looks for a parameter and replaces its value""" - def __init__(self,paramName,paramSearch,paramValue,verbose=False): - self._paramName = paramName - self._paramValue = paramValue - self._paramSearch = paramSearch - self._verbose = verbose - def enter(self,visitee): - if (hasattr(visitee,self._paramName)): - if getattr(visitee,self._paramName) == self._paramSearch: - if self._verbose:print "Replaced %s.%s: %s => %s" % (visitee,self._paramName,getattr(visitee,self._paramName),self._paramValue) - setattr(visitee,self._paramName,self._paramValue) - def leave(self,visitee): - pass - -class MassSearchReplaceAnyInputTagVisitor(object): - """Visitor that travels within a cms.Sequence, looks for a parameter and replace its value - It will climb down within PSets, VPSets and VInputTags to find its target""" - def __init__(self,paramSearch,paramReplace,verbose=False,moduleLabelOnly=False,skipLabelTest=False): - self._paramSearch = self.standardizeInputTagFmt(paramSearch) - self._paramReplace = self.standardizeInputTagFmt(paramReplace) - self._moduleName = '' - self._verbose=verbose - self._moduleLabelOnly=moduleLabelOnly - self._skipLabelTest=skipLabelTest - def doIt(self,pset,base): - if isinstance(pset, cms._Parameterizable): - for name in pset.parameterNames_(): - # if I use pset.parameters_().items() I get copies of the parameter values - # so I can't modify the nested pset - value = getattr(pset,name) - type = value.pythonTypeName() - if type == 'cms.PSet': - self.doIt(value,base+"."+name) - elif type == 'cms.VPSet': - for (i,ps) in enumerate(value): self.doIt(ps, "%s.%s[%d]"%(base,name,i) ) - elif type == 'cms.VInputTag': - for (i,n) in enumerate(value): - # VInputTag can be declared as a list of strings, so ensure that n is formatted correctly - n = self.standardizeInputTagFmt(n) - if (n == self._paramSearch): - if self._verbose:print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, self._paramSearch, self._paramReplace) - value[i] = self._paramReplace - elif self._moduleLabelOnly and n.moduleLabel == self._paramSearch.moduleLabel: - nrep = n; nrep.moduleLabel = self._paramReplace.moduleLabel - if self._verbose:print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, n, nrep) - value[i] = nrep - elif type.endswith('.InputTag'): - if value == self._paramSearch: - if self._verbose:print "Replace %s.%s %s ==> %s " % (base, name, self._paramSearch, self._paramReplace) - from copy import deepcopy - if 'untracked' in type: - setattr(pset, name, cms.untracked.InputTag(self._paramReplace.getModuleLabel(), - self._paramReplace.getProductInstanceLabel(), - self._paramReplace.getProcessName())) - else: - setattr(pset, name, deepcopy(self._paramReplace) ) - elif self._moduleLabelOnly and value.moduleLabel == self._paramSearch.moduleLabel: - from copy import deepcopy - repl = deepcopy(getattr(pset, name)) - repl.moduleLabel = self._paramReplace.moduleLabel - setattr(pset, name, repl) - if self._verbose:print "Replace %s.%s %s ==> %s " % (base, name, value, repl) - - - @staticmethod - def standardizeInputTagFmt(inputTag): - ''' helper function to ensure that the InputTag is defined as cms.InputTag(str) and not as a plain str ''' - if not isinstance(inputTag, cms.InputTag): - return cms.InputTag(inputTag) - return inputTag - - def enter(self,visitee): - label = '' - if (not self._skipLabelTest): - try: label = visitee.label_() - except AttributeError: label = '' - else: - label = '' - self.doIt(visitee, label) - def leave(self,visitee): - pass - #FIXME name is not generic enough now class GatherAllModulesVisitor(object): """Visitor that travels within a cms.Sequence, and returns a list of objects of type gatheredInance(e.g. modules) that have it""" @@ -276,35 +198,6 @@ def _newLabel(self, label): def __appendToTopSequence(self, visitee): self._clonedSequence += visitee -class MassSearchParamVisitor(object): - """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it""" - def __init__(self,paramName,paramSearch): - self._paramName = paramName - self._paramSearch = paramSearch - self._modules = [] - def enter(self,visitee): - if (hasattr(visitee,self._paramName)): - if getattr(visitee,self._paramName) == self._paramSearch: - self._modules.append(visitee) - def leave(self,visitee): - pass - def modules(self): - return self._modules - - -def massSearchReplaceParam(sequence,paramName,paramOldValue,paramValue,verbose=False): - sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue,verbose)) - -def massReplaceParameter(process,name="label",old="rawDataCollector",new="rawDataRepacker",verbose=False): - for s in process.paths_().keys(): - massSearchReplaceParam(getattr(process,s),name,old,new,verbose) - for s in process.endpaths_().keys(): - massSearchReplaceParam(getattr(process,s),name,old,new,verbose) - if process.schedule_() is not None: - for task in process.schedule_()._tasks: - massSearchReplaceParam(task, name, old, new, verbose) - return(process) - def listModules(sequence): visitor = GatherAllModulesVisitor(gatheredInstance=cms._Module) sequence.visit(visitor) @@ -315,20 +208,6 @@ def listSequences(sequence): sequence.visit(visitor) return visitor.modules() -def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag,verbose=False,moduleLabelOnly=False,skipLabelTest=False) : - """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags...""" - sequence.visit(MassSearchReplaceAnyInputTagVisitor(oldInputTag,newInputTag,verbose=verbose,moduleLabelOnly=moduleLabelOnly,skipLabelTest=skipLabelTest)) - -def massReplaceInputTag(process,old="rawDataCollector",new="rawDataRepacker",verbose=False,moduleLabelOnly=False,skipLabelTest=False): - for s in process.paths_().keys(): - massSearchReplaceAnyInputTag(getattr(process,s), old, new, verbose, moduleLabelOnly, skipLabelTest) - for s in process.endpaths_().keys(): - massSearchReplaceAnyInputTag(getattr(process,s), old, new, verbose, moduleLabelOnly, skipLabelTest) - if process.schedule_() is not None: - for task in process.schedule_()._tasks: - massSearchReplaceAnyInputTag(task, old, new, verbose, moduleLabelOnly, skipLabelTest) - return(process) - def jetCollectionString(prefix='', algo='', type=''): """ ------------------------------------------------------------------ @@ -461,128 +340,5 @@ def testListModules(self): p.c = cms.EDProducer("ac", src=cms.InputTag("b")) p.s = cms.Sequence(p.a*p.b*p.c) self.assertEqual([p.a,p.b,p.c], listModules(p.s)) - def testMassSearchReplaceParam(self): - p = cms.Process("test") - p.a = cms.EDProducer("a", src=cms.InputTag("gen")) - p.b = cms.EDProducer("ab", src=cms.InputTag("a")) - p.c = cms.EDProducer("ac", src=cms.InputTag("b"), - nested = cms.PSet(src = cms.InputTag("c")) - ) - p.s = cms.Sequence(p.a*p.b*p.c) - massSearchReplaceParam(p.s,"src",cms.InputTag("b"),"a") - self.assertEqual(cms.InputTag("a"),p.c.src) - self.assertNotEqual(cms.InputTag("a"),p.c.nested.src) - def testMassSearchReplaceAnyInputTag(self): - p = cms.Process("test") - p.a = cms.EDProducer("a", src=cms.InputTag("gen")) - p.b = cms.EDProducer("ab", src=cms.InputTag("a")) - p.c = cms.EDProducer("ac", src=cms.InputTag("b"), - nested = cms.PSet(src = cms.InputTag("b"), src2 = cms.InputTag("c")), - nestedv = cms.VPSet(cms.PSet(src = cms.InputTag("b")), cms.PSet(src = cms.InputTag("d"))), - vec = cms.VInputTag(cms.InputTag("a"), cms.InputTag("b"), cms.InputTag("c"), cms.InputTag("d")) - ) - p.s = cms.Sequence(p.a*p.b*p.c) - massSearchReplaceAnyInputTag(p.s, cms.InputTag("b"), cms.InputTag("new")) - self.assertNotEqual(cms.InputTag("new"), p.b.src) - self.assertEqual(cms.InputTag("new"), p.c.src) - self.assertEqual(cms.InputTag("new"), p.c.nested.src) - self.assertEqual(cms.InputTag("new"), p.c.nested.src) - self.assertNotEqual(cms.InputTag("new"), p.c.nested.src2) - self.assertEqual(cms.InputTag("new"), p.c.nestedv[0].src) - self.assertNotEqual(cms.InputTag("new"), p.c.nestedv[1].src) - self.assertNotEqual(cms.InputTag("new"), p.c.vec[0]) - self.assertEqual(cms.InputTag("new"), p.c.vec[1]) - self.assertNotEqual(cms.InputTag("new"), p.c.vec[2]) - self.assertNotEqual(cms.InputTag("new"), p.c.vec[3]) - - def testMassReplaceInputTag(self): - process1 = cms.Process("test") - massReplaceInputTag(process1, "a", "b", False, False, False) - self.assertEqual(process1.dumpPython(), -"""import FWCore.ParameterSet.Config as cms - -process = cms.Process("test") -""") - p = cms.Process("test") - p.a = cms.EDProducer("a", src=cms.InputTag("gen")) - p.b = cms.EDProducer("ab", src=cms.InputTag("a")) - p.c = cms.EDProducer("ac", src=cms.InputTag("b"), - nested = cms.PSet(src = cms.InputTag("a"), src2 = cms.InputTag("c")), - nestedv = cms.VPSet(cms.PSet(src = cms.InputTag("a")), cms.PSet(src = cms.InputTag("d"))), - vec = cms.VInputTag(cms.InputTag("a"), cms.InputTag("b"), cms.InputTag("c"), cms.InputTag("d")) - ) - p.d = cms.EDProducer("ab", src=cms.InputTag("a")) - p.e = cms.EDProducer("ab", src=cms.InputTag("a")) - p.f = cms.EDProducer("ab", src=cms.InputTag("a")) - p.g = cms.EDProducer("ab", src=cms.InputTag("a")) - p.h = cms.EDProducer("ab", src=cms.InputTag("a")) - p.i = cms.EDProducer("ab", src=cms.InputTag("a")) - p.s1 = cms.Sequence(p.a*p.b*p.c) - p.path1 = cms.Path(p.s1) - p.s2 = cms.Sequence(p.d) - p.path2 = cms.Path(p.e) - p.s3 = cms.Sequence(p.f) - p.endpath1 = cms.EndPath(p.s3) - p.endpath2 = cms.EndPath(p.g) - p.t1 = cms.Task(p.h) - p.t2 = cms.Task(p.i) - p.schedule = cms.Schedule() - p.schedule.associate(p.t1, p.t2) - massReplaceInputTag(p, "a", "b", False, False, False) - self.assertEqual(cms.InputTag("b"), p.b.src) - self.assertEqual(cms.InputTag("b"), p.c.vec[0]) - self.assertEqual(cms.InputTag("c"), p.c.vec[2]) - self.assertEqual(cms.InputTag("a"), p.d.src) - self.assertEqual(cms.InputTag("b"), p.e.src) - self.assertEqual(cms.InputTag("b"), p.f.src) - self.assertEqual(cms.InputTag("b"), p.g.src) - self.assertEqual(cms.InputTag("b"), p.h.src) - self.assertEqual(cms.InputTag("b"), p.i.src) - - def testMassReplaceParam(self): - process1 = cms.Process("test") - massReplaceParameter(process1, "src", cms.InputTag("a"), "b", False) - self.assertEqual(process1.dumpPython(), -"""import FWCore.ParameterSet.Config as cms - -process = cms.Process("test") - -""") - p = cms.Process("test") - p.a = cms.EDProducer("a", src=cms.InputTag("gen")) - p.b = cms.EDProducer("ab", src=cms.InputTag("a")) - p.c = cms.EDProducer("ac", src=cms.InputTag("b"), - nested = cms.PSet(src = cms.InputTag("a"), src2 = cms.InputTag("c")), - nestedv = cms.VPSet(cms.PSet(src = cms.InputTag("a")), cms.PSet(src = cms.InputTag("d"))), - vec = cms.VInputTag(cms.InputTag("a"), cms.InputTag("b"), cms.InputTag("c"), cms.InputTag("d")) - ) - p.d = cms.EDProducer("ab", src=cms.InputTag("a")) - p.e = cms.EDProducer("ab", src=cms.InputTag("a")) - p.f = cms.EDProducer("ab", src=cms.InputTag("a")) - p.g = cms.EDProducer("ab", src=cms.InputTag("a")) - p.h = cms.EDProducer("ab", src=cms.InputTag("a")) - p.i = cms.EDProducer("ab", src=cms.InputTag("a")) - p.s1 = cms.Sequence(p.a*p.b*p.c) - p.path1 = cms.Path(p.s1) - p.s2 = cms.Sequence(p.d) - p.path2 = cms.Path(p.e) - p.s3 = cms.Sequence(p.f) - p.endpath1 = cms.EndPath(p.s3) - p.endpath2 = cms.EndPath(p.g) - p.t1 = cms.Task(p.h) - p.t2 = cms.Task(p.i) - p.schedule = cms.Schedule() - p.schedule.associate(p.t1, p.t2) - massReplaceParameter(p, "src",cms.InputTag("a"), "b", False) - self.assertEqual(cms.InputTag("gen"), p.a.src) - self.assertEqual(cms.InputTag("b"), p.b.src) - self.assertEqual(cms.InputTag("a"), p.c.vec[0]) - self.assertEqual(cms.InputTag("c"), p.c.vec[2]) - self.assertEqual(cms.InputTag("a"), p.d.src) - self.assertEqual(cms.InputTag("b"), p.e.src) - self.assertEqual(cms.InputTag("b"), p.f.src) - self.assertEqual(cms.InputTag("b"), p.g.src) - self.assertEqual(cms.InputTag("b"), p.h.src) - self.assertEqual(cms.InputTag("b"), p.i.src) unittest.main() diff --git a/RecoHI/HiEgammaAlgos/python/HiHelperTools.py b/RecoHI/HiEgammaAlgos/python/HiHelperTools.py index 8431225e70916..301efed3e181d 100644 --- a/RecoHI/HiEgammaAlgos/python/HiHelperTools.py +++ b/RecoHI/HiEgammaAlgos/python/HiHelperTools.py @@ -3,6 +3,11 @@ ## Helpers to perform some technically boring tasks like looking for all modules with a given parameter ## and replacing that to a given value +# Next two lines are for backward compatibility, the imported functions and +# classes used to be defined in this file. +from FWCore.ParameterSet.MassReplace import massSearchReplaceAnyInputTag, MassSearchReplaceAnyInputTagVisitor +from FWCore.ParameterSet.MassReplace import massSearchReplaceParam, MassSearchParamVisitor, MassSearchReplaceParamVisitor + def applyPostfix(process, label, postfix): ''' If a module is in patHeavyIonDefaultSequence use the cloned module. Will crash if patHeavyIonDefaultSequence has not been cloned with 'postfix' beforehand''' @@ -32,80 +37,6 @@ def __labelsInSequence(process, sequenceLabel, postfix=""): result.extend([ m.label() for m in listSequences( getattr(process,sequenceLabel+postfix))] ) return result -class MassSearchReplaceParamVisitor(object): - """Visitor that travels within a cms.Sequence, looks for a parameter and replaces its value""" - def __init__(self,paramName,paramSearch,paramValue,verbose=False): - self._paramName = paramName - self._paramValue = paramValue - self._paramSearch = paramSearch - self._verbose = verbose - def enter(self,visitee): - if (hasattr(visitee,self._paramName)): - if getattr(visitee,self._paramName) == self._paramSearch: - if self._verbose:print "Replaced %s.%s: %s => %s" % (visitee,self._paramName,getattr(visitee,self._paramName),self._paramValue) - setattr(visitee,self._paramName,self._paramValue) - def leave(self,visitee): - pass - -class MassSearchReplaceAnyInputTagVisitor(object): - """Visitor that travels within a cms.Sequence, looks for a parameter and replace its value - It will climb down within PSets, VPSets and VInputTags to find its target""" - def __init__(self,paramSearch,paramReplace,verbose=False,moduleLabelOnly=False): - self._paramSearch = self.standardizeInputTagFmt(paramSearch) - self._paramReplace = self.standardizeInputTagFmt(paramReplace) - self._moduleName = '' - self._verbose=verbose - self._moduleLabelOnly=moduleLabelOnly - def doIt(self,pset,base): - if isinstance(pset, cms._Parameterizable): - for name in pset.parameters_().keys(): - # if I use pset.parameters_().items() I get copies of the parameter values - # so I can't modify the nested pset - value = getattr(pset,name) - type = value.pythonTypeName() - if type == 'cms.PSet': - self.doIt(value,base+"."+name) - elif type == 'cms.VPSet': - for (i,ps) in enumerate(value): self.doIt(ps, "%s.%s[%d]"%(base,name,i) ) - elif type == 'cms.VInputTag': - for (i,n) in enumerate(value): - # VInputTag can be declared as a list of strings, so ensure that n is formatted correctly - n = self.standardizeInputTagFmt(n) - if (n == self._paramSearch): - if self._verbose:print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, self._paramSearch, self._paramReplace) - value[i] = self._paramReplace - elif self._moduleLabelOnly and n.moduleLabel == self._paramSearch.moduleLabel: - nrep = n; nrep.moduleLabel = self._paramReplace.moduleLabel - if self._verbose:print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, n, nrep) - value[i] = nrep - elif type == 'cms.InputTag': - if value == self._paramSearch: - if self._verbose:print "Replace %s.%s %s ==> %s " % (base, name, self._paramSearch, self._paramReplace) - from copy import deepcopy - setattr(pset, name, deepcopy(self._paramReplace) ) - elif self._moduleLabelOnly and value.moduleLabel == self._paramSearch.moduleLabel: - from copy import deepcopy - repl = deepcopy(getattr(pset, name)) - repl.moduleLabel = self._paramReplace.moduleLabel - setattr(pset, name, repl) - if self._verbose:print "Replace %s.%s %s ==> %s " % (base, name, value, repl) - - - @staticmethod - def standardizeInputTagFmt(inputTag): - ''' helper function to ensure that the InputTag is defined as cms.InputTag(str) and not as a plain str ''' - if not isinstance(inputTag, cms.InputTag): - return cms.InputTag(inputTag) - return inputTag - - def enter(self,visitee): - label = '' - try: label = visitee.label() - except AttributeError: label = '' - self.doIt(visitee, label) - def leave(self,visitee): - pass - #FIXME name is not generic enough now class GatherAllModulesVisitor(object): """Visitor that travels within a cms.Sequence, and returns a list of objects of type gatheredInance(e.g. modules) that have it""" @@ -181,25 +112,6 @@ def __appendToTopSequence(self, visitee):#this is darn ugly because empty cms.Se else: self._sequenceStack[-1] += visitee -class MassSearchParamVisitor(object): - """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it""" - def __init__(self,paramName,paramSearch): - self._paramName = paramName - self._paramSearch = paramSearch - self._modules = [] - def enter(self,visitee): - if (hasattr(visitee,self._paramName)): - if getattr(visitee,self._paramName) == self._paramSearch: - self._modules.append(visitee) - def leave(self,visitee): - pass - def modules(self): - return self._modules - - -def massSearchReplaceParam(sequence,paramName,paramOldValue,paramValue): - sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue)) - def listModules(sequence): visitor = GatherAllModulesVisitor(gatheredInstance=cms._Module) sequence.visit(visitor) @@ -210,10 +122,6 @@ def listSequences(sequence): sequence.visit(visitor) return visitor.modules() -def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag,verbose=False,moduleLabelOnly=False) : - """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags...""" - sequence.visit(MassSearchReplaceAnyInputTagVisitor(oldInputTag,newInputTag,verbose=verbose,moduleLabelOnly=moduleLabelOnly)) - def jetCollectionString(prefix='', algo='', type=''): """ ------------------------------------------------------------------ @@ -302,16 +210,5 @@ def testListModules(self): p.c = cms.EDProducer("ac", src=cms.InputTag("b")) p.s = cms.Sequence(p.a*p.b*p.c) self.assertEqual([p.a,p.b,p.c], listModules(p.s)) - def testMassSearchReplaceParam(self): - p = cms.Process("test") - p.a = cms.EDProducer("a", src=cms.InputTag("gen")) - p.b = cms.EDProducer("ab", src=cms.InputTag("a")) - p.c = cms.EDProducer("ac", src=cms.InputTag("b"), - nested = cms.PSet(src = cms.InputTag("c")) - ) - p.s = cms.Sequence(p.a*p.b*p.c) - massSearchReplaceParam(p.s,"src",cms.InputTag("b"),"a") - self.assertEqual(cms.InputTag("a"),p.c.src) - self.assertNotEqual(cms.InputTag("a"),p.c.nested.src) unittest.main()