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 try: label = visitee.label_()
64 except AttributeError: label =
'<Module not in a Process>' 66 label =
'<Module label not tested>' 67 self.
doIt(visitee, label)
72 """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags...""" 73 sequence.visit(MassSearchReplaceAnyInputTagVisitor(oldInputTag,newInputTag,verbose=verbose,moduleLabelOnly=moduleLabelOnly,skipLabelTest=skipLabelTest))
75 def massReplaceInputTag(process,old="rawDataCollector",new="rawDataRepacker",verbose=False,moduleLabelOnly=False,skipLabelTest=False):
76 for s
in process.paths_().
keys():
78 for s
in process.endpaths_().
keys():
80 if process.schedule_()
is not None:
81 for task
in process.schedule_()._tasks:
86 """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it""" 94 self._modules.append(visitee)
101 """Visitor that travels within a cms.Sequence, looks for a parameter and replaces its value""" 102 def __init__(self,paramName,paramSearch,paramValue,verbose=False):
116 sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue,verbose))
119 for s
in process.paths_().
keys():
121 for s
in process.endpaths_().
keys():
123 if process.schedule_()
is not None:
124 for task
in process.schedule_()._tasks:
128 if __name__==
"__main__":
133 p = cms.Process(
"test")
134 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
135 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
136 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
137 nested = cms.PSet(src = cms.InputTag(
"b"), src2 = cms.InputTag(
"c")),
138 nestedv = cms.VPSet(cms.PSet(src = cms.InputTag(
"b")), cms.PSet(src = cms.InputTag(
"d"))),
139 vec = cms.VInputTag(cms.InputTag(
"a"), cms.InputTag(
"b"), cms.InputTag(
"c"), cms.InputTag(
"d"))
141 p.s = cms.Sequence(p.a*p.b*p.c)
143 self.assertNotEqual(cms.InputTag(
"new"), p.b.src)
144 self.assertEqual(cms.InputTag(
"new"), p.c.src)
145 self.assertEqual(cms.InputTag(
"new"), p.c.nested.src)
146 self.assertEqual(cms.InputTag(
"new"), p.c.nested.src)
147 self.assertNotEqual(cms.InputTag(
"new"), p.c.nested.src2)
148 self.assertEqual(cms.InputTag(
"new"), p.c.nestedv[0].src)
149 self.assertNotEqual(cms.InputTag(
"new"), p.c.nestedv[1].src)
150 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[0])
151 self.assertEqual(cms.InputTag(
"new"), p.c.vec[1])
152 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[2])
153 self.assertNotEqual(cms.InputTag(
"new"), p.c.vec[3])
156 process1 = cms.Process(
"test")
158 self.assertEqual(process1.dumpPython(),
159 """import FWCore.ParameterSet.Config as cms 161 process = cms.Process("test") 164 p = cms.Process(
"test")
165 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
166 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
167 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
168 nested = cms.PSet(src = cms.InputTag(
"a"), src2 = cms.InputTag(
"c")),
169 nestedv = cms.VPSet(cms.PSet(src = cms.InputTag(
"a")), cms.PSet(src = cms.InputTag(
"d"))),
170 vec = cms.VInputTag(cms.InputTag(
"a"), cms.InputTag(
"b"), cms.InputTag(
"c"), cms.InputTag(
"d"))
172 p.d = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
173 p.e = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
174 p.f = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
175 p.g = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
176 p.h = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
177 p.i = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
178 p.s1 = cms.Sequence(p.a*p.b*p.c)
179 p.path1 = cms.Path(p.s1)
180 p.s2 = cms.Sequence(p.d)
181 p.path2 = cms.Path(p.e)
182 p.s3 = cms.Sequence(p.f)
183 p.endpath1 = cms.EndPath(p.s3)
184 p.endpath2 = cms.EndPath(p.g)
187 p.schedule = cms.Schedule()
188 p.schedule.associate(p.t1, p.t2)
190 self.assertEqual(cms.InputTag(
"b"), p.b.src)
191 self.assertEqual(cms.InputTag(
"b"), p.c.vec[0])
192 self.assertEqual(cms.InputTag(
"c"), p.c.vec[2])
193 self.assertEqual(cms.InputTag(
"a"), p.d.src)
194 self.assertEqual(cms.InputTag(
"b"), p.e.src)
195 self.assertEqual(cms.InputTag(
"b"), p.f.src)
196 self.assertEqual(cms.InputTag(
"b"), p.g.src)
197 self.assertEqual(cms.InputTag(
"b"), p.h.src)
198 self.assertEqual(cms.InputTag(
"b"), p.i.src)
201 p = cms.Process(
"test")
202 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
203 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
204 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
205 nested = cms.PSet(src = cms.InputTag(
"c"))
207 p.s = cms.Sequence(p.a*p.b*p.c)
209 self.assertEqual(cms.InputTag(
"a"),p.c.src)
210 self.assertNotEqual(cms.InputTag(
"a"),p.c.nested.src)
213 process1 = cms.Process(
"test")
215 self.assertEqual(process1.dumpPython(),
216 """import FWCore.ParameterSet.Config as cms 218 process = cms.Process("test") 221 p = cms.Process(
"test")
222 p.a = cms.EDProducer(
"a", src=cms.InputTag(
"gen"))
223 p.b = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
224 p.c = cms.EDProducer(
"ac", src=cms.InputTag(
"b"),
225 nested = cms.PSet(src = cms.InputTag(
"a"), src2 = cms.InputTag(
"c")),
226 nestedv = cms.VPSet(cms.PSet(src = cms.InputTag(
"a")), cms.PSet(src = cms.InputTag(
"d"))),
227 vec = cms.VInputTag(cms.InputTag(
"a"), cms.InputTag(
"b"), cms.InputTag(
"c"), cms.InputTag(
"d"))
229 p.d = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
230 p.e = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
231 p.f = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
232 p.g = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
233 p.h = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
234 p.i = cms.EDProducer(
"ab", src=cms.InputTag(
"a"))
235 p.s1 = cms.Sequence(p.a*p.b*p.c)
236 p.path1 = cms.Path(p.s1)
237 p.s2 = cms.Sequence(p.d)
238 p.path2 = cms.Path(p.e)
239 p.s3 = cms.Sequence(p.f)
240 p.endpath1 = cms.EndPath(p.s3)
241 p.endpath2 = cms.EndPath(p.g)
244 p.schedule = cms.Schedule()
245 p.schedule.associate(p.t1, p.t2)
247 self.assertEqual(cms.InputTag(
"gen"), p.a.src)
248 self.assertEqual(cms.InputTag(
"b"), p.b.src)
249 self.assertEqual(cms.InputTag(
"a"), p.c.vec[0])
250 self.assertEqual(cms.InputTag(
"c"), p.c.vec[2])
251 self.assertEqual(cms.InputTag(
"a"), p.d.src)
252 self.assertEqual(cms.InputTag(
"b"), p.e.src)
253 self.assertEqual(cms.InputTag(
"b"), p.f.src)
254 self.assertEqual(cms.InputTag(
"b"), p.g.src)
255 self.assertEqual(cms.InputTag(
"b"), p.h.src)
256 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)
return(e1-e2)*(e1-e2)+dp *dp
def massReplaceParameter(process, name="label", old="rawDataCollector", new="rawDataRepacker", verbose=False)