CMS 3D CMS Logo

MassReplace.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 
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):
7  self._paramSearch = self.standardizeInputTagFmt(paramSearch)
8  self._paramReplace = self.standardizeInputTagFmt(paramReplace)
9  self._moduleName = ''
10  self._verbose=verbose
11  self._moduleLabelOnly=moduleLabelOnly
12  self._skipLabelTest=skipLabelTest
13  def doIt(self,pset,base):
14  if isinstance(pset, cms._Parameterizable):
15  for name in pset.parameterNames_():
16  # if I use pset.parameters_().items() I get copies of the parameter values
17  # so I can't modify the nested pset
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):
26  # VInputTag can be declared as a list of strings, so ensure that n is formatted correctly
27  n = self.standardizeInputTagFmt(n)
28  if (n == self._paramSearch):
29  if self._verbose:print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, self._paramSearch, self._paramReplace)
30  value[i] = self._paramReplace
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)
34  value[i] = nrep
35  elif type.endswith('.InputTag'):
36  if value == self._paramSearch:
37  if self._verbose:print "Replace %s.%s %s ==> %s " % (base, name, self._paramSearch, self._paramReplace)
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()))
43  else:
44  setattr(pset, name, deepcopy(self._paramReplace) )
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)
51 
52 
53  @staticmethod
54  def standardizeInputTagFmt(inputTag):
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)
58  return inputTag
59 
60  def enter(self,visitee):
61  label = ''
62  if (not self._skipLabelTest):
63  if hasattr(visitee,"hasLabel_") and visitee.hasLabel_():
64  label = visitee.label_()
65  else: label = '<Module not in a Process>'
66  else:
67  label = '<Module label not tested>'
68  self.doIt(visitee, label)
69  def leave(self,visitee):
70  pass
71 
72 def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag,verbose=False,moduleLabelOnly=False,skipLabelTest=False) :
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))
75 
76 def massReplaceInputTag(process,old="rawDataCollector",new="rawDataRepacker",verbose=False,moduleLabelOnly=False,skipLabelTest=False):
77  for s in process.paths_().keys():
78  massSearchReplaceAnyInputTag(getattr(process,s), old, new, verbose, moduleLabelOnly, skipLabelTest)
79  for s in process.endpaths_().keys():
80  massSearchReplaceAnyInputTag(getattr(process,s), old, new, verbose, moduleLabelOnly, skipLabelTest)
81  if process.schedule_() is not None:
82  for task in process.schedule_()._tasks:
83  massSearchReplaceAnyInputTag(task, old, new, verbose, moduleLabelOnly, skipLabelTest)
84  return(process)
85 
87  """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it"""
88  def __init__(self,paramName,paramSearch):
89  self._paramName = paramName
90  self._paramSearch = paramSearch
91  self._modules = []
92  def enter(self,visitee):
93  if (hasattr(visitee,self._paramName)):
94  if getattr(visitee,self._paramName) == self._paramSearch:
95  self._modules.append(visitee)
96  def leave(self,visitee):
97  pass
98  def modules(self):
99  return self._modules
100 
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):
104  self._paramName = paramName
105  self._paramValue = paramValue
106  self._paramSearch = paramSearch
107  self._verbose = verbose
108  def enter(self,visitee):
109  if (hasattr(visitee,self._paramName)):
110  if getattr(visitee,self._paramName) == self._paramSearch:
111  if self._verbose:print "Replaced %s.%s: %s => %s" % (visitee,self._paramName,getattr(visitee,self._paramName),self._paramValue)
112  setattr(visitee,self._paramName,self._paramValue)
113  def leave(self,visitee):
114  pass
115 
116 def massSearchReplaceParam(sequence,paramName,paramOldValue,paramValue,verbose=False):
117  sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue,verbose))
118 
119 def massReplaceParameter(process,name="label",old="rawDataCollector",new="rawDataRepacker",verbose=False):
120  for s in process.paths_().keys():
121  massSearchReplaceParam(getattr(process,s),name,old,new,verbose)
122  for s in process.endpaths_().keys():
123  massSearchReplaceParam(getattr(process,s),name,old,new,verbose)
124  if process.schedule_() is not None:
125  for task in process.schedule_()._tasks:
126  massSearchReplaceParam(task, name, old, new, verbose)
127  return(process)
128 
129 if __name__=="__main__":
130  import unittest
131  class TestModuleCommand(unittest.TestCase):
132 
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"))
141  )
142  p.s = cms.Sequence(p.a*p.b*p.c)
143  massSearchReplaceAnyInputTag(p.s, cms.InputTag("b"), cms.InputTag("new"))
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])
155 
157  process1 = cms.Process("test")
158  massReplaceInputTag(process1, "a", "b", False, False, False)
159  self.assertEqual(process1.dumpPython(),
160 """import FWCore.ParameterSet.Config as cms
161 
162 process = cms.Process("test")
163 
164 """)
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"))
172  )
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)
186  p.t1 = cms.Task(p.h)
187  p.t2 = cms.Task(p.i)
188  p.schedule = cms.Schedule()
189  p.schedule.associate(p.t1, p.t2)
190  massReplaceInputTag(p, "a", "b", False, False, False)
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)
200 
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"))
207  )
208  p.s = cms.Sequence(p.a*p.b*p.c)
209  massSearchReplaceParam(p.s,"src",cms.InputTag("b"),"a")
210  self.assertEqual(cms.InputTag("a"),p.c.src)
211  self.assertNotEqual(cms.InputTag("a"),p.c.nested.src)
212 
214  process1 = cms.Process("test")
215  massReplaceParameter(process1, "src", cms.InputTag("a"), "b", False)
216  self.assertEqual(process1.dumpPython(),
217 """import FWCore.ParameterSet.Config as cms
218 
219 process = cms.Process("test")
220 
221 """)
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"))
229  )
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)
243  p.t1 = cms.Task(p.h)
244  p.t2 = cms.Task(p.i)
245  p.schedule = cms.Schedule()
246  p.schedule.associate(p.t1, p.t2)
247  massReplaceParameter(p, "src",cms.InputTag("a"), "b", False)
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)
258  unittest.main()
def __init__(self, paramName, paramSearch, paramValue, verbose=False)
Definition: MassReplace.py:103
def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag, verbose=False, moduleLabelOnly=False, skipLabelTest=False)
Definition: MassReplace.py:72
def massSearchReplaceParam(sequence, paramName, paramOldValue, paramValue, verbose=False)
Definition: MassReplace.py:116
def testMassSearchReplaceAnyInputTag(self)
Definition: MassReplace.py:133
def massReplaceInputTag(process, old="rawDataCollector", new="rawDataRepacker", verbose=False, moduleLabelOnly=False, skipLabelTest=False)
Definition: MassReplace.py:76
def __init__(self, paramName, paramSearch)
Definition: MassReplace.py:88
return(e1-e2)*(e1-e2)+dp *dp
def massReplaceParameter(process, name="label", old="rawDataCollector", new="rawDataRepacker", verbose=False)
Definition: MassReplace.py:119
def __init__(self, paramSearch, paramReplace, verbose=False, moduleLabelOnly=False, skipLabelTest=False)
Definition: MassReplace.py:6