CMS 3D CMS Logo

helpers.py

Go to the documentation of this file.
00001 import FWCore.ParameterSet.Config as cms
00002 
00003 ## Helpers to perform some technically boring tasks like looking for all modules with a given parameter
00004 ## and replacing that to a given value
00005 
00006 class MassSearchReplaceParamVisitor(object):
00007     """Visitor that travels within a cms.Sequence, looks for a parameter and replace its value"""
00008     def __init__(self,paramName,paramSearch,paramValue):
00009         self._paramName   = paramName
00010         self._paramValue  = paramValue
00011         self._paramSearch = paramSearch
00012     def enter(self,visitee):
00013         if (hasattr(visitee,self._paramName)):
00014             if getattr(visitee,self._paramName) == self._paramSearch:
00015                 print "Replaced %s.%s: %s => %s" % (visitee,self._paramName,getattr(visitee,self._paramName),self._paramValue)
00016                 setattr(visitee,self._paramName,self._paramValue)
00017     def leave(self,visitee):
00018         pass
00019 
00020 class MassSearchReplaceAnyInputTagVisitor(object):
00021     """Visitor that travels within a cms.Sequence, looks for a parameter and replace its value
00022        It will climb down within PSets, VPSets and VInputTags to find its target"""
00023     def __init__(self,paramSearch,paramReplace):
00024         self._paramSearch  = paramSearch
00025         self._paramReplace = paramReplace
00026         self._moduleName   = ''
00027     def doIt(self,pset,base):
00028         if isinstance(pset, cms._Parameterizable):
00029             for name in pset.parameters_().keys():
00030                 # if I use pset.parameters_().items() I get copies of the parameter values
00031                 # so I can't modify the nested pset
00032                 value = getattr(pset,name) 
00033                 type = value.pythonTypeName()
00034                 if type == 'cms.PSet':  
00035                     self.doIt(value,base+"."+name)
00036                 elif type == 'cms.VPSet':
00037                     for (i,ps) in enumerate(value): self.doIt(ps, "%s.%s[%d]"%(base,name,i) )
00038                 elif type == 'cms.VInputTag':
00039                     for (i,n) in enumerate(value): 
00040                         if (it == self._paramSearch):
00041                             print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, self._paramSearch, self._paramReplace)
00042                             value[i] == self._paramReplace
00043                 elif type == 'cms.InputTag':
00044                     if value == self._paramSearch:
00045                         print "Replace %s.%s %s ==> %s " % (base, name, self._paramSearch, self._paramReplace)
00046                         setattr(pset, name, self._paramReplace)
00047     def enter(self,visitee):
00048         label = ''
00049         try:    label = visitee.label()
00050         except AttributeError: label = '<Module not in a Process>'
00051         self.doIt(visitee, label)
00052     def leave(self,visitee):
00053         pass
00054 
00055 class GatherAllModulesVisitor(object):
00056     """Visitor that travels within a cms.Sequence, and returns a list of modules that have it"""
00057     def __init__(self):
00058         self._modules = []
00059     def enter(self,visitee):
00060         self._modules.append(visitee)
00061     def leave(self,visitee):
00062         pass
00063     def modules(self):
00064         return self._modules
00065  
00066 
00067 class MassSearchParamVisitor(object):
00068     """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it"""
00069     def __init__(self,paramName,paramSearch):
00070         self._paramName   = paramName
00071         self._paramSearch = paramSearch
00072         self._modules = []
00073     def enter(self,visitee):
00074         if (hasattr(visitee,self._paramName)):
00075             if getattr(visitee,self._paramName) == self._paramSearch:
00076                 self._modules.append(visitee)
00077     def leave(self,visitee):
00078         pass
00079     def modules(self):
00080         return self._modules
00081     
00082 def massSearchReplaceParam(sequence,paramName,paramOldValue,paramValue):
00083     sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue))
00084 
00085 def listModules(sequence):
00086     visitor = GatherAllModulesVisitor()
00087     sequence.visit(visitor)
00088     return visitor.modules()
00089 
00090 def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag) : 
00091     """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags..."""
00092     sequence.visit(MassSearchReplaceAnyInputTagVisitor(oldInputTag,newInputTag))
00093     

Generated on Tue Jun 9 17:41:44 2009 for CMSSW by  doxygen 1.5.4