1 |
dgele |
1.1 |
import FWCore.ParameterSet.Config as cms
|
2 |
|
|
|
3 |
|
|
## Helpers to perform some technically boring tasks like looking for all modules with a given parameter
|
4 |
|
|
## and replacing that to a given value
|
5 |
|
|
|
6 |
|
|
class MassSearchReplaceParamVisitor(object):
|
7 |
|
|
"""Visitor that travels within a cms.Sequence, looks for a parameter and replace its value"""
|
8 |
|
|
def __init__(self,paramName,paramSearch,paramValue):
|
9 |
|
|
self._paramName = paramName
|
10 |
|
|
self._paramValue = paramValue
|
11 |
|
|
self._paramSearch = paramSearch
|
12 |
|
|
def enter(self,visitee):
|
13 |
|
|
if (hasattr(visitee,self._paramName)):
|
14 |
|
|
if getattr(visitee,self._paramName) == self._paramSearch:
|
15 |
|
|
print "Replaced %s.%s: %s => %s" % (visitee,self._paramName,getattr(visitee,self._paramName),self._paramValue)
|
16 |
|
|
setattr(visitee,self._paramName,self._paramValue)
|
17 |
|
|
def leave(self,visitee):
|
18 |
|
|
pass
|
19 |
|
|
|
20 |
|
|
class MassSearchReplaceAnyInputTagVisitor(object):
|
21 |
|
|
"""Visitor that travels within a cms.Sequence, looks for a parameter and replace its value
|
22 |
|
|
It will climb down within PSets, VPSets and VInputTags to find its target"""
|
23 |
|
|
def __init__(self,paramSearch,paramReplace):
|
24 |
|
|
self._paramSearch = paramSearch
|
25 |
|
|
self._paramReplace = paramReplace
|
26 |
|
|
self._moduleName = ''
|
27 |
|
|
def doIt(self,pset,base):
|
28 |
|
|
if isinstance(pset, cms._Parameterizable):
|
29 |
|
|
for name in pset.parameters_().keys():
|
30 |
|
|
# if I use pset.parameters_().items() I get copies of the parameter values
|
31 |
|
|
# so I can't modify the nested pset
|
32 |
|
|
value = getattr(pset,name)
|
33 |
|
|
type = value.pythonTypeName()
|
34 |
|
|
if type == 'cms.PSet':
|
35 |
|
|
self.doIt(value,base+"."+name)
|
36 |
|
|
elif type == 'cms.VPSet':
|
37 |
|
|
for (i,ps) in enumerate(value): self.doIt(ps, "%s.%s[%d]"%(base,name,i) )
|
38 |
|
|
elif type == 'cms.VInputTag':
|
39 |
|
|
for (i,n) in enumerate(value):
|
40 |
|
|
if (it == self._paramSearch):
|
41 |
|
|
print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, self._paramSearch, self._paramReplace)
|
42 |
|
|
value[i] == self._paramReplace
|
43 |
|
|
elif type == 'cms.InputTag':
|
44 |
|
|
if value == self._paramSearch:
|
45 |
|
|
print "Replace %s.%s %s ==> %s " % (base, name, self._paramSearch, self._paramReplace)
|
46 |
|
|
setattr(pset, name, self._paramReplace)
|
47 |
|
|
def enter(self,visitee):
|
48 |
|
|
label = ''
|
49 |
|
|
try: label = visitee.label()
|
50 |
|
|
except AttributeError: label = '<Module not in a Process>'
|
51 |
|
|
self.doIt(visitee, label)
|
52 |
|
|
def leave(self,visitee):
|
53 |
|
|
pass
|
54 |
|
|
|
55 |
|
|
class GatherAllModulesVisitor(object):
|
56 |
|
|
"""Visitor that travels within a cms.Sequence, and returns a list of modules that have it"""
|
57 |
|
|
def __init__(self):
|
58 |
|
|
self._modules = []
|
59 |
|
|
def enter(self,visitee):
|
60 |
|
|
self._modules.append(visitee)
|
61 |
|
|
def leave(self,visitee):
|
62 |
|
|
pass
|
63 |
|
|
def modules(self):
|
64 |
|
|
return self._modules
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
class MassSearchParamVisitor(object):
|
68 |
|
|
"""Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it"""
|
69 |
|
|
def __init__(self,paramName,paramSearch):
|
70 |
|
|
self._paramName = paramName
|
71 |
|
|
self._paramSearch = paramSearch
|
72 |
|
|
self._modules = []
|
73 |
|
|
def enter(self,visitee):
|
74 |
|
|
if (hasattr(visitee,self._paramName)):
|
75 |
|
|
if getattr(visitee,self._paramName) == self._paramSearch:
|
76 |
|
|
self._modules.append(visitee)
|
77 |
|
|
def leave(self,visitee):
|
78 |
|
|
pass
|
79 |
|
|
def modules(self):
|
80 |
|
|
return self._modules
|
81 |
|
|
|
82 |
|
|
def massSearchReplaceParam(sequence,paramName,paramOldValue,paramValue):
|
83 |
|
|
sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue))
|
84 |
|
|
|
85 |
|
|
def listModules(sequence):
|
86 |
|
|
visitor = GatherAllModulesVisitor()
|
87 |
|
|
sequence.visit(visitor)
|
88 |
|
|
return visitor.modules()
|
89 |
|
|
|
90 |
|
|
def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag) :
|
91 |
|
|
"""Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags..."""
|
92 |
|
|
sequence.visit(MassSearchReplaceAnyInputTagVisitor(oldInputTag,newInputTag))
|
93 |
|
|
|