1 import FWCore.ParameterSet.Config
as cms
4 """Visitor that travels within a cms.Sequence, looks for a parameter and replace its value 5 It will climb down within PSets, VPSets and VInputTags to find its target""" 6 def __init__(self,paramSearch,paramReplace,verbose=False,moduleLabelOnly=False,skipLabelTest=False):
14 if isinstance(pset, cms._Parameterizable):
15 for name
in pset.parameterNames_():
18 value = getattr(pset,name)
19 type = value.pythonTypeName()
20 if type ==
'cms.PSet':
21 self.
doIt(value,base+
"."+name)
22 elif type ==
'cms.VPSet':
23 for (i,ps)
in enumerate(value): self.
doIt(ps,
"%s.%s[%d]"%(base,name,i) )
24 elif type ==
'cms.VInputTag':
25 for (i,n)
in enumerate(value):
31 elif self.
_moduleLabelOnly and n.moduleLabel == self._paramSearch.moduleLabel:
32 nrep = n; nrep.moduleLabel = self._paramReplace.moduleLabel
33 if self.
_verbose:
print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, n, nrep)
35 elif type.endswith(
'.InputTag'):
38 from copy
import deepcopy
39 if 'untracked' in type:
40 setattr(pset, name, cms.untracked.InputTag(self._paramReplace.getModuleLabel(),
41 self._paramReplace.getProductInstanceLabel(),
42 self._paramReplace.getProcessName()))
45 elif self.
_moduleLabelOnly and value.moduleLabel == self._paramSearch.moduleLabel:
46 from copy
import deepcopy
47 repl = deepcopy(getattr(pset, name))
48 repl.moduleLabel = self._paramReplace.moduleLabel
49 setattr(pset, name, repl)
50 if self.
_verbose:
print "Replace %s.%s %s ==> %s " % (base, name, value, repl)
55 ''' helper function to ensure that the InputTag is defined as cms.InputTag(str) and not as a plain str ''' 56 if not isinstance(inputTag, cms.InputTag):
57 return cms.InputTag(inputTag)
63 if hasattr(visitee,
"hasLabel_")
and visitee.hasLabel_():
64 label = visitee.label_()
65 else: label =
'<Module not in a Process>' 67 label =
'<Module label not tested>' 68 self.
doIt(visitee, label)
73 """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags...""" 74 sequence.visit(MassSearchReplaceAnyInputTagVisitor(oldInputTag,newInputTag,verbose=verbose,moduleLabelOnly=moduleLabelOnly,skipLabelTest=skipLabelTest))
76 def massReplaceInputTag(process,old="rawDataCollector",new="rawDataRepacker",verbose=False,moduleLabelOnly=False,skipLabelTest=False):
77 for s
in process.paths_().
keys():
79 for s
in process.endpaths_().
keys():
81 if process.schedule_()
is not None:
82 for task
in process.schedule_()._tasks:
87 """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it""" 95 self._modules.append(visitee)
102 """Visitor that travels within a cms.Sequence, looks for a parameter and replaces its value""" 103 def __init__(self,paramName,paramSearch,paramValue,verbose=False):
117 sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue,verbose))
120 for s
in process.paths_().
keys():
122 for s
in process.endpaths_().
keys():
124 if process.schedule_()
is not None:
125 for task
in process.schedule_()._tasks:
129 if __name__==
"__main__":
134 p = cms.Process(
"test")
135 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
136 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
137 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
138 nested = cms.PSet(src = cms.InputTag(
"b"), src2 = cms.InputTag(
"c")),
139 nestedv = cms.VPSet(cms.PSet(src = cms.InputTag(
"b")), cms.PSet(src = cms.InputTag(
"d"))),
140 vec = cms.VInputTag(cms.InputTag(
"a"), cms.InputTag(
"b"), cms.InputTag(
"c"), cms.InputTag(
"d"))
142 p.s = cms.Sequence(p.a*p.b*p.c)
144 self.assertNotEqual(cms.InputTag(
"new"), p.b.src)
145 self.assertEqual(cms.InputTag(
"new"), p.c.src)
146 self.assertEqual(cms.InputTag(
"new"), p.c.nested.src)
147 self.assertEqual(cms.InputTag(
"new"), p.c.nested.src)
148 self.assertNotEqual(cms.InputTag(
"new"), p.c.nested.src2)
149 self.assertEqual(cms.InputTag(
"new"), p.c.nestedv[0].src)
150 self.assertNotEqual(cms.InputTag(
"new"), p.c.nestedv[1].src)
151 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[0])
152 self.assertEqual(cms.InputTag(
"new"), p.c.vec[1])
153 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[2])
154 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[3])
157 process1 = cms.Process(
"test")
159 self.assertEqual(process1.dumpPython(),
160 """import FWCore.ParameterSet.Config as cms 162 process = cms.Process("test") 165 p = cms.Process(
"test")
166 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
167 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
168 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
169 nested = cms.PSet(src = cms.InputTag(
"a"), src2 = cms.InputTag(
"c")),
170 nestedv = cms.VPSet(cms.PSet(src = cms.InputTag(
"a")), cms.PSet(src = cms.InputTag(
"d"))),
171 vec = cms.VInputTag(cms.InputTag(
"a"), cms.InputTag(
"b"), cms.InputTag(
"c"), cms.InputTag(
"d"))
173 p.d = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
174 p.e = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
175 p.f = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
176 p.g = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
177 p.h = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
178 p.i = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
179 p.s1 = cms.Sequence(p.a*p.b*p.c)
180 p.path1 = cms.Path(p.s1)
181 p.s2 = cms.Sequence(p.d)
182 p.path2 = cms.Path(p.e)
183 p.s3 = cms.Sequence(p.f)
184 p.endpath1 = cms.EndPath(p.s3)
185 p.endpath2 = cms.EndPath(p.g)
188 p.schedule = cms.Schedule()
189 p.schedule.associate(p.t1, p.t2)
191 self.assertEqual(cms.InputTag(
"b"), p.b.src)
192 self.assertEqual(cms.InputTag(
"b"), p.c.vec[0])
193 self.assertEqual(cms.InputTag(
"c"), p.c.vec[2])
194 self.assertEqual(cms.InputTag(
"a"), p.d.src)
195 self.assertEqual(cms.InputTag(
"b"), p.e.src)
196 self.assertEqual(cms.InputTag(
"b"), p.f.src)
197 self.assertEqual(cms.InputTag(
"b"), p.g.src)
198 self.assertEqual(cms.InputTag(
"b"), p.h.src)
199 self.assertEqual(cms.InputTag(
"b"), p.i.src)
202 p = cms.Process(
"test")
203 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
204 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
205 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
206 nested = cms.PSet(src = cms.InputTag(
"c"))
208 p.s = cms.Sequence(p.a*p.b*p.c)
210 self.assertEqual(cms.InputTag(
"a"),p.c.src)
211 self.assertNotEqual(cms.InputTag(
"a"),p.c.nested.src)
214 process1 = cms.Process(
"test")
216 self.assertEqual(process1.dumpPython(),
217 """import FWCore.ParameterSet.Config as cms 219 process = cms.Process("test") 222 p = cms.Process(
"test")
223 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
224 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
225 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
226 nested = cms.PSet(src = cms.InputTag(
"a"), src2 = cms.InputTag(
"c")),
227 nestedv = cms.VPSet(cms.PSet(src = cms.InputTag(
"a")), cms.PSet(src = cms.InputTag(
"d"))),
228 vec = cms.VInputTag(cms.InputTag(
"a"), cms.InputTag(
"b"), cms.InputTag(
"c"), cms.InputTag(
"d"))
230 p.d = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
231 p.e = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
232 p.f = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
233 p.g = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
234 p.h = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
235 p.i = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
236 p.s1 = cms.Sequence(p.a*p.b*p.c)
237 p.path1 = cms.Path(p.s1)
238 p.s2 = cms.Sequence(p.d)
239 p.path2 = cms.Path(p.e)
240 p.s3 = cms.Sequence(p.f)
241 p.endpath1 = cms.EndPath(p.s3)
242 p.endpath2 = cms.EndPath(p.g)
245 p.schedule = cms.Schedule()
246 p.schedule.associate(p.t1, p.t2)
248 self.assertEqual(cms.InputTag(
"gen"), p.a.src)
249 self.assertEqual(cms.InputTag(
"b"), p.b.src)
250 self.assertEqual(cms.InputTag(
"a"), p.c.vec[0])
251 self.assertEqual(cms.InputTag(
"c"), p.c.vec[2])
252 self.assertEqual(cms.InputTag(
"a"), p.d.src)
253 self.assertEqual(cms.InputTag(
"b"), p.e.src)
254 self.assertEqual(cms.InputTag(
"b"), p.f.src)
255 self.assertEqual(cms.InputTag(
"b"), p.g.src)
256 self.assertEqual(cms.InputTag(
"b"), p.h.src)
257 self.assertEqual(cms.InputTag(
"b"), p.i.src)
def __init__(self, paramName, paramSearch, paramValue, verbose=False)
def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag, verbose=False, moduleLabelOnly=False, skipLabelTest=False)
def massSearchReplaceParam(sequence, paramName, paramOldValue, paramValue, verbose=False)
def testMassSearchReplaceParam(self)
def testMassSearchReplaceAnyInputTag(self)
def massReplaceInputTag(process, old="rawDataCollector", new="rawDataRepacker", verbose=False, moduleLabelOnly=False, skipLabelTest=False)
def __init__(self, paramName, paramSearch)
def testMassReplaceInputTag(self)
def testMassReplaceParam(self)
def massReplaceParameter(process, name="label", old="rawDataCollector", new="rawDataRepacker", verbose=False)