CMS 3D CMS Logo

Modules.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 from .Mixins import _ConfigureComponent, saveOrigin
3 from .Mixins import _Unlabelable, _Labelable
4 from .Mixins import _TypedParameterizable, _Parameterizable, PrintOptions, specialImportRegistry
5 from .SequenceTypes import _SequenceLeaf
6 from .Types import vstring, EDAlias
7 
8 
9 import six
10 import copy
11 from .ExceptionHandling import *
13  def __init__(self,type_,*arg,**kargs):
14  super(Service,self).__init__(type_,*arg,**kargs)
15  self._inProcess = False
16  def _placeImpl(self,name,proc):
17  self._inProcess = True
18  proc._placeService(self.type_(),self)
19  def insertInto(self, processDesc):
20  newpset = processDesc.newPSet()
21  newpset.addString(True, "@service_type", self.type_())
22  self.insertContentsInto(newpset)
23  processDesc.addService(newpset)
24  def dumpSequencePython(self, options=PrintOptions()):
25  return "process." + self.type_()
26  def _isTaskComponent(self):
27  return True
28  def isLeaf(self):
29  return True
30  def __str__(self):
31  return str(self.type_())
32 
34  def __init__(self,type_,*arg,**kargs):
35  super(ESSource,self).__init__(type_,*arg,**kargs)
36  saveOrigin(self, 1)
37  def _placeImpl(self,name,proc):
38  if name == '':
39  name=self.type_()
40  proc._placeESSource(name,self)
41  def moduleLabel_(self,myname):
42  result = myname
43  if self.type_() == myname:
44  result = ""
45  return result
46  def nameInProcessDesc_(self, myname):
47  result = self.type_() + "@" + self.moduleLabel_(myname)
48  return result
49  def _isTaskComponent(self):
50  return True
51  def isLeaf(self):
52  return True
53 
55  def __init__(self,type_,*arg,**kargs):
56  super(ESProducer,self).__init__(type_,*arg,**kargs)
57  def _placeImpl(self,name,proc):
58  if name == '':
59  name=self.type_()
60  proc._placeESProducer(name,self)
61  def moduleLabel_(self,myname):
62  result = myname
63  if self.type_() == myname:
64  result = ''
65  return result
66  def nameInProcessDesc_(self, myname):
67  result = self.type_() + "@" + self.moduleLabel_(myname)
68  return result
69  def _isTaskComponent(self):
70  return True
71  def isLeaf(self):
72  return True
73 
75  """Used to set which EventSetup provider should provide a particular data item
76  in the case where multiple providers are capable of delivering the data.
77  The first argument specifies the C++ class type of the prodiver.
78  If the provider has been given a label, you must specify that label as the second argument.
79  Additional 'vstring' arguments maybe used to specify exactly which EventSetup Records
80  are being preferred and optionally which data items within that Record.
81  E.g.,
82  #prefer all data in record 'OrangeRecord' from 'juicer'
83  ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring())
84  or
85  #prefer only "Orange" data in "OrangeRecord" from "juicer"
86  ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("ExtraPulp"))
87  or
88  #prefer only "Orange" data with label "ExtraPulp" in "OrangeRecord" from "juicer"
89  ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("Orange/ExtraPulp"))
90  """
91  def __init__(self,type_,targetLabel='',*arg,**kargs):
92  super(ESPrefer,self).__init__(type_,*arg,**kargs)
93  self._targetLabel = targetLabel
94  if targetLabel is None:
95  self._targetLabel = str('')
96  if kargs:
97  for k,v in six.iteritems(kargs):
98  if not isinstance(v,vstring):
99  raise RuntimeError('ESPrefer only allows vstring attributes. "'+k+'" is a '+str(type(v)))
100  def _placeImpl(self,name,proc):
101  proc._placeESPrefer(name,self)
102  def nameInProcessDesc_(self, myname):
103  # the C++ parser can give it a name like "label@prefer". Get rid of that.
104  return "esprefer_" + self.type_() + "@" + self._targetLabel
105  def copy(self):
106  returnValue = ESPrefer.__new__(type(self))
107  returnValue.__init__(self.type_(), self._targetLabel)
108  return returnValue
109  def moduleLabel_(self, myname):
110  return self._targetLabel
111  def targetLabel_(self):
112  return self._targetLabel
113  def dumpPythonAs(self, label, options=PrintOptions()):
114  result = options.indentation()
115  basename = self._targetLabel
116  if basename == '':
117  basename = self.type_()
118  if options.isCfg:
119  # do either type or label
120  result += 'process.prefer("'+basename+'"'
121  if self.parameterNames_():
122  result += ",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
123  result +=')\n'
124  else:
125  # use the base class Module
126  result += 'es_prefer_'+basename+' = cms.ESPrefer("'+self.type_()+'"'
127  if self._targetLabel != '':
128  result += ',"'+self._targetLabel+'"'
129  if self.parameterNames_():
130  result += ",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
131  result += ')\n'
132  return result
133 
135  """base class for classes which denote framework event based 'modules'"""
136  __isStrict__ = False
137  def __init__(self,type_,*arg,**kargs):
138  super(_Module,self).__init__(type_,*arg,**kargs)
139  if _Module.__isStrict__:
140  self.setIsFrozen()
141  saveOrigin(self, 2)
142  def _clonesequence(self, lookuptable):
143  try:
144  return lookuptable[id(self)]
145  except:
146  raise ModuleCloneError(self._errorstr())
147  def _errorstr(self):
148  # return something like "EDAnalyzer("foo", ...)"
149  typename = format_typename(self)
150  return "%s('%s', ...)" %(typename, self.type_())
151 
152  def setPrerequisites(self, *libs):
153  self.__dict__["libraries_"] = libs
154 
155  def insertInto(self, parameterSet, myname):
156  if "libraries_" in self.__dict__:
157  from ctypes import LibraryLoader, CDLL
158  import platform
159  loader = LibraryLoader(CDLL)
160  ext = platform.uname()[0] == "Darwin" and "dylib" or "so"
161  [loader.LoadLibrary("lib%s.%s" % (l, ext)) for l in self.libraries_]
162  super(_Module,self).insertInto(parameterSet,myname)
163 
165  def __init__(self,type_,*arg,**kargs):
166  super(EDProducer,self).__init__(type_,*arg,**kargs)
167  def _placeImpl(self,name,proc):
168  proc._placeProducer(name,self)
169  def _isTaskComponent(self):
170  return True
171 
173  def __init__(self,type_,*arg,**kargs):
174  super(EDFilter,self).__init__(type_,*arg,**kargs)
175  def _placeImpl(self,name,proc):
176  proc._placeFilter(name,self)
177  def _isTaskComponent(self):
178  return True
179 
181  def __init__(self,type_,*arg,**kargs):
182  super(EDAnalyzer,self).__init__(type_,*arg,**kargs)
183  def _placeImpl(self,name,proc):
184  proc._placeAnalyzer(name,self)
185 
186 
188  def __init__(self,type_,*arg,**kargs):
189  super(OutputModule,self).__init__(type_,*arg,**kargs)
190  def _placeImpl(self,name,proc):
191  proc._placeOutputModule(name,self)
192 
193 
195  def __init__(self,type_,*arg,**kargs):
196  super(Source,self).__init__(type_,*arg,**kargs)
197  def _placeImpl(self,name,proc):
198  proc._placeSource(name,self)
199  def moduleLabel_(self,myname):
200  return "@main_input"
201  def nameInProcessDesc_(self,myname):
202  return "@main_input"
203 
204 
206  def __init__(self,type_,*arg,**kargs):
207  super(Looper,self).__init__(type_,*arg,**kargs)
208  def _placeImpl(self,name,proc):
209  proc._placeLooper(name,self)
210  def moduleLabel_(self,myname):
211  return "@main_looper"
212  def nameInProcessDesc_(self, myname):
213  return "@main_looper"
214 
215 
216 # Need to be a module-level function for the configuration with a
217 # SwitchProducer to be pickleable.
219  return (True, 1)
220 
222  """This purpose class is to provide a switch of EDProducers for a single module/product label.
223 
224  The decision is done at the time when the python configuration is
225  translated to C++. This class is generic, and intended to be
226  inherited for concrete switches. Example:
227 
228  class SwitchProducerFoo(SwitchProducer):
229  def __init__(self, **kargs):
230  super(SwitchProducerFoo,self).__init__(
231  dict(case1 = case1Func, case2 = case2Func),
232  **kargs
233  )
234 
235  foo = SwitchProducerFoo(
236  case1 = EDProducer("Producer1"),
237  case2 = EDProducer("Producer2")
238  )
239 
240  Here case1Func and case2Func are functions that return a (bool,
241  int) tuple, where the bool tells whether that case is enabled or
242  not, and the int tells the priority of that case. The case with
243  the highest priority among those that are enabled will get chosen.
244 
245  The end result is that the product(s) labeled as "foo" will be
246  produced with one of the producers. It would be good if their
247  output product types and instance names would be the same (or very
248  close).
249  """
250  def __init__(self, caseFunctionDict, **kargs):
251  super(SwitchProducer,self).__init__(None)
252  self._caseFunctionDict = copy.copy(caseFunctionDict)
253  self.__setParameters(kargs)
254  self._isModified = False
255 
256  @staticmethod
257  def getCpu():
258  """Returns a function that returns the priority for a CPU "computing device". Intended to be used by deriving classes."""
259  return _switch_cpu
260 
261  def _chooseCase(self):
262  """Returns the name of the chosen case."""
263  cases = self.parameterNames_()
264  bestCase = None
265  for case in cases:
266  (enabled, priority) = self._caseFunctionDict[case]()
267  if enabled and (bestCase is None or bestCase[0] < priority):
268  bestCase = (priority, case)
269  if bestCase is None:
270  raise RuntimeError("All cases '%s' were disabled" % (str(cases)))
271  return bestCase[1]
272 
273  def _getProducer(self):
274  """Returns the EDroducer of the chosen case"""
275  return self.__dict__[self._chooseCase()]
276 
277  @staticmethod
278  def __typeIsValid(typ):
279  return (isinstance(typ, EDProducer) and not isinstance(typ, SwitchProducer)) or isinstance(typ, EDAlias)
280 
281  def __addParameter(self, name, value):
282  if not self.__typeIsValid(value):
283  raise TypeError(name+" does not already exist, so it can only be set to a cms.EDProducer or cms.EDAlias")
284  if name not in self._caseFunctionDict:
285  raise ValueError("Case '%s' is not allowed (allowed ones are %s)" % (name, ",".join(six.iterkeys(self._caseFunctionDict))))
286  if name in self.__dict__:
287  message = "Duplicate insert of member " + name
288  message += "\nThe original parameters are:\n"
289  message += self.dumpPython() + '\n'
290  raise ValueError(message)
291  self.__dict__[name]=value
292  self._Parameterizable__parameterNames.append(name)
293  self._isModified = True
294 
295  def __setParameters(self, parameters):
296  for name, value in six.iteritems(parameters):
297  self.__addParameter(name, value)
298 
299  def __setattr__(self, name, value):
300  # Following snippet copied and customized from
301  # _Parameterizable in order to support Modifier.toModify
302  #
303  #since labels are not supposed to have underscores at the beginning
304  # I will assume that if we have such then we are setting an internal variable
305  if self.isFrozen() and not (name in ["_Labelable__label","_isFrozen"] or name.startswith('_')):
306  message = "Object already added to a process. It is read only now\n"
307  message += " %s = %s" %(name, value)
308  message += "\nThe original parameters are:\n"
309  message += self.dumpPython() + '\n'
310  raise ValueError(message)
311  # underscored names bypass checking for _ParameterTypeBase
312  if name[0]=='_':
313  super(SwitchProducer, self).__setattr__(name,value)
314  elif not name in self.__dict__:
315  self.__addParameter(name, value)
316  self._isModified = True
317  else:
318  if not self.__typeIsValid(value):
319  raise TypeError(name+" can only be set to a cms.EDProducer or cms.EDAlias")
320  # We should always receive an cms.EDProducer
321  self.__dict__[name] = value
322  self._isModified = True
323 
324  def clone(self, **params):
325  returnValue = SwitchProducer.__new__(type(self))
326 
327  # Need special treatment as cms.EDProducer is not a valid parameter type (except in this case)
328  myparams = dict()
329  for name, value in six.iteritems(params):
330  if value is None:
331  continue
332  elif isinstance(value, dict):
333  myparams[name] = self.__dict__[name].clone(**value)
334  else: # value is an EDProducer
335  myparams[name] = value.clone()
336 
337  # Add the ones that were not customized
338  for name in self.parameterNames_():
339  if name not in params:
340  myparams[name] = self.__dict__[name].clone()
341  returnValue.__init__(**myparams)
342  returnValue._isModified = False
343  returnValue._isFrozen = False
344  saveOrigin(returnValue, 1)
345  return returnValue
346 
347  def dumpPython(self, options=PrintOptions()):
348  # Note that if anyone uses the generic SwitchProducer instead
349  # of a derived-one, the information on the functions for the
350  # producer decision is lost
351  specialImportRegistry.registerUse(self)
352  result = "%s(" % self.__class__.__name__ # not including cms. since the deriving classes are not in cms "namespace"
353  options.indent()
354  for resource in sorted(self.parameterNames_()):
355  result += "\n" + options.indentation() + resource + " = " + getattr(self, resource).dumpPython(options).rstrip() + ","
356  if result[-1] == ",":
357  result = result.rstrip(",")
358  options.unindent()
359  result += "\n)\n"
360  return result
361 
363  # XXX FIXME handle SwitchProducer dependencies
364  return []
365 
366  def nameInProcessDesc_(self, myname):
367  return myname
368  def moduleLabel_(self, myname):
369  return myname
370  def caseLabel_(self, name, case):
371  return name+"@"+case
372  def appendToProcessDescLists_(self, modules, aliases, myname):
373  # This way we can insert the chosen EDProducer to @all_modules
374  # so that we get easily a worker for it
375  modules.append(myname)
376  for case in self.parameterNames_():
377  if isinstance(self.__dict__[case], EDAlias):
378  aliases.append(self.caseLabel_(myname, case))
379  else:
380  modules.append(self.caseLabel_(myname, case))
381 
382  def insertInto(self, parameterSet, myname):
383  for case in self.parameterNames_():
384  producer = self.__dict__[case]
385  producer.insertInto(parameterSet, self.caseLabel_(myname, case))
386  newpset = parameterSet.newPSet()
387  newpset.addString(True, "@module_label", self.moduleLabel_(myname))
388  newpset.addString(True, "@module_type", "SwitchProducer")
389  newpset.addString(True, "@module_edm_type", "EDProducer")
390  newpset.addVString(True, "@all_cases", [myname+"@"+p for p in self.parameterNames_()])
391  newpset.addString(False, "@chosen_case", myname+"@"+self._chooseCase())
392  parameterSet.addPSet(True, self.nameInProcessDesc_(myname), newpset)
393 
394  def _placeImpl(self,name,proc):
395  proc._placeSwitchProducer(name,self)
396 # for case in self.parameterNames_():
397 # caseLabel = self.caseLabel_(name, case)
398 # caseObj = self.__dict__[case]
399 #
400 # if isinstance(caseObj, EDAlias):
401 # # EDAliases end up in @all_aliases automatically
402 # proc._placeAlias(caseLabel, caseObj)
403 # else:
404 # # Note that these don't end up in @all_modules
405 # # automatically because they're not part of any
406 # # Task/Sequence/Path
407 # proc._placeProducer(caseLabel, caseObj)
408 
409  def _clonesequence(self, lookuptable):
410  try:
411  return lookuptable[id(self)]
412  except:
413  raise ModuleCloneError(self._errorstr())
414  def _errorstr(self):
415  return "SwitchProducer"
416 
417 
418 if __name__ == "__main__":
419  import unittest
420  from .Types import *
421  from .SequenceTypes import *
422 
424  def __init__(self, **kargs):
425  super(SwitchProducerTest,self).__init__(
426  dict(
427  test1 = lambda: (True, -10),
428  test2 = lambda: (True, -9),
429  test3 = lambda: (True, -8)
430  ), **kargs)
432  def __init__(self, **kargs):
433  super(SwitchProducerTest1Dis,self).__init__(
434  dict(
435  test1 = lambda: (False, -10),
436  test2 = lambda: (True, -9)
437  ), **kargs)
439  def __init__(self, **kargs):
440  super(SwitchProducerTest2Dis,self).__init__(
441  dict(
442  test1 = lambda: (True, -10),
443  test2 = lambda: (False, -9)
444  ), **kargs)
446  def __init__(self, **kargs):
447  super(SwitchProducerPickleable,self).__init__(
448  dict(cpu = SwitchProducer.getCpu()), **kargs)
449 
450  class TestModules(unittest.TestCase):
451  def testEDAnalyzer(self):
452  empty = EDAnalyzer("Empty")
453  withParam = EDAnalyzer("Parameterized",foo=untracked(int32(1)), bar = untracked(string("it")))
454  self.assertEqual(withParam.foo.value(), 1)
455  self.assertEqual(withParam.bar.value(), "it")
456  aCopy = withParam.copy()
457  self.assertEqual(aCopy.foo.value(), 1)
458  self.assertEqual(aCopy.bar.value(), "it")
459  withType = EDAnalyzer("Test",type = int32(1))
460  self.assertEqual(withType.type.value(),1)
461  block = PSet(i = int32(9))
462  m = EDProducer("DumbProducer", block, j = int32(10))
463  self.assertEqual(9, m.i.value())
464  self.assertEqual(10, m.j.value())
465  def testESPrefer(self):
466  juicer = ESPrefer("JuiceProducer")
467  options = PrintOptions()
468  options.isCfg = True
469  self.assertEqual(juicer.dumpPythonAs("juicer", options), "process.prefer(\"JuiceProducer\")\n")
470  options.isCfg = False
471  self.assertEqual(juicer.dumpPythonAs("juicer", options), "es_prefer_JuiceProducer = cms.ESPrefer(\"JuiceProducer\")\n")
472 
473  juicer = ESPrefer("JuiceProducer","juicer")
474  options = PrintOptions()
475  options.isCfg = True
476  self.assertEqual(juicer.dumpPythonAs("juicer", options), 'process.prefer("juicer")\n')
477  options.isCfg = False
478  self.assertEqual(juicer.dumpPythonAs("juicer", options), 'es_prefer_juicer = cms.ESPrefer("JuiceProducer","juicer")\n')
479  juicer = ESPrefer("JuiceProducer",fooRcd=vstring())
480  self.assertEqual(juicer.dumpConfig(options),
481 """JuiceProducer {
482  vstring fooRcd = {
483  }
484 
485 }
486 """)
487  options = PrintOptions()
488  options.isCfg = True
489  self.assertEqual(juicer.dumpPythonAs("juicer"),
490 """process.prefer("JuiceProducer",
491  fooRcd = cms.vstring()
492 )
493 """)
494  options.isCfg = False
495  self.assertEqual(juicer.dumpPythonAs("juicer", options),
496 """es_prefer_JuiceProducer = cms.ESPrefer("JuiceProducer",
497  fooRcd = cms.vstring()
498 )
499 """)
500 
501  def testService(self):
502  empty = Service("Empty")
503  withParam = Service("Parameterized",foo=untracked(int32(1)), bar = untracked(string("it")))
504  self.assertEqual(withParam.foo.value(), 1)
505  self.assertEqual(withParam.bar.value(), "it")
506  self.assertEqual(empty.dumpPython(), "cms.Service(\"Empty\")\n")
507  self.assertEqual(withParam.dumpPython(), "cms.Service(\"Parameterized\",\n bar = cms.untracked.string(\'it\'),\n foo = cms.untracked.int32(1)\n)\n")
508  def testSequences(self):
509  m = EDProducer("MProducer")
510  n = EDProducer("NProducer")
511  m.setLabel("m")
512  n.setLabel("n")
513  s1 = Sequence(m*n)
514  options = PrintOptions()
515 
517  m = EDProducer("x")
518  self.assertTrue(m._isTaskComponent())
519  self.assertTrue(m.isLeaf())
520  m = SwitchProducerTest(test1=EDProducer("x"))
521  self.assertTrue(m._isTaskComponent())
522  self.assertTrue(m.isLeaf())
523  m = EDFilter("x")
524  self.assertTrue(m._isTaskComponent())
525  self.assertTrue(m.isLeaf())
526  m = OutputModule("x")
527  self.assertFalse(m._isTaskComponent())
528  self.assertTrue(m.isLeaf())
529  m = EDAnalyzer("x")
530  self.assertFalse(m._isTaskComponent())
531  self.assertTrue(m.isLeaf())
532  m = Service("x")
533  self.assertTrue(m._isTaskComponent())
534  self.assertTrue(m.isLeaf())
535  m = ESProducer("x")
536  self.assertTrue(m._isTaskComponent())
537  self.assertTrue(m.isLeaf())
538  m = ESSource("x")
539  self.assertTrue(m._isTaskComponent())
540  self.assertTrue(m.isLeaf())
541  m = Sequence()
542  self.assertFalse(m._isTaskComponent())
543  self.assertFalse(m.isLeaf())
544  m = Path()
545  self.assertFalse(m._isTaskComponent())
546  m = EndPath()
547  self.assertFalse(m._isTaskComponent())
548  m = Task()
549  self.assertTrue(m._isTaskComponent())
550  self.assertFalse(m.isLeaf())
551 
553  # Constructor
554  sp = SwitchProducerTest(test1 = EDProducer("Foo"), test2 = EDProducer("Bar"))
555  self.assertEqual(sp.test1.type_(), "Foo")
556  self.assertEqual(sp.test2.type_(), "Bar")
557  self.assertRaises(ValueError, lambda: SwitchProducerTest(nonexistent = EDProducer("Foo")))
558  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = EDAnalyzer("Foo")))
559  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = EDFilter("Foo")))
560  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = Source("Foo")))
561  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = OutputModule("Foo")))
562  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = Looper("Foo")))
563  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = Service("Foo")))
564  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = ESSource("Foo")))
565  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = ESProducer("Foo")))
566  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = ESPrefer("Foo")))
567  self.assertRaises(TypeError, lambda: SwitchProducerTest(test1 = SwitchProducerTest(test1 = EDProducer("Foo"))))
568 
569  # Case decision
570  sp = SwitchProducerTest(test1 = EDProducer("Foo"), test2 = EDProducer("Bar"))
571  self.assertEqual(sp._getProducer().type_(), "Bar")
572  sp = SwitchProducerTest1Dis(test1 = EDProducer("Foo"), test2 = EDProducer("Bar"))
573  self.assertEqual(sp._getProducer().type_(), "Bar")
574  sp = SwitchProducerTest2Dis(test1 = EDProducer("Foo"), test2 = EDProducer("Bar"))
575  self.assertEqual(sp._getProducer().type_(), "Foo")
576  sp = SwitchProducerTest(test1 = EDProducer("Bar"))
577  self.assertEqual(sp._getProducer().type_(), "Bar")
578  sp = SwitchProducerTest1Dis(test1 = EDProducer("Bar"))
579  self.assertRaises(RuntimeError, sp._getProducer)
580 
581  # Mofications
582  from .Types import int32, string, PSet
583  sp = SwitchProducerTest(test1 = EDProducer("Foo",
584  a = int32(1),
585  b = PSet(c = int32(2))),
586  test2 = EDProducer("Bar",
587  aa = int32(11),
588  bb = PSet(cc = int32(12))))
589  # Simple clone
590  cl = sp.clone()
591  self.assertEqual(cl.test1.type_(), "Foo")
592  self.assertEqual(cl.test1.a.value(), 1)
593  self.assertEqual(cl.test1.b.c.value(), 2)
594  self.assertEqual(cl.test2.type_(), "Bar")
595  self.assertEqual(cl.test2.aa.value(), 11)
596  self.assertEqual(cl.test2.bb.cc.value(), 12)
597  self.assertEqual(sp._getProducer().type_(), "Bar")
598  # Modify clone
599  cl.test1.a = 3
600  self.assertEqual(cl.test1.a.value(), 3)
601  cl.test1 = EDProducer("Fred")
602  self.assertEqual(cl.test1.type_(), "Fred")
603  def _assignEDAnalyzer():
604  cl.test1 = EDAnalyzer("Foo")
605  self.assertRaises(TypeError, _assignEDAnalyzer)
606  def _assignSwitchProducer():
607  cl.test1 = SwitchProducerTest(test1 = SwitchProducerTest(test1 = EDProducer("Foo")))
608  self.assertRaises(TypeError, _assignSwitchProducer)
609  # Modify values with a dict
610  cl = sp.clone(test1 = dict(a = 4, b = dict(c = None)),
611  test2 = dict(aa = 15, bb = dict(cc = 45, dd = string("foo"))))
612  self.assertEqual(cl.test1.a.value(), 4)
613  self.assertEqual(cl.test1.b.hasParameter("c"), False)
614  self.assertEqual(cl.test2.aa.value(), 15)
615  self.assertEqual(cl.test2.bb.cc.value(), 45)
616  self.assertEqual(cl.test2.bb.dd.value(), "foo")
617  # Replace/add/remove EDProducers
618  cl = sp.clone(test1 = EDProducer("Fred", x = int32(42)),
619  test3 = EDProducer("Wilma", y = int32(24)),
620  test2 = None)
621  self.assertEqual(cl.test1.type_(), "Fred")
622  self.assertEqual(cl.test1.x.value(), 42)
623  self.assertEqual(cl.test3.type_(), "Wilma")
624  self.assertEqual(cl.test3.y.value(), 24)
625  self.assertEqual(hasattr(cl, "test2"), False)
626  self.assertRaises(TypeError, lambda: sp.clone(test1 = EDAnalyzer("Foo")))
627  self.assertRaises(TypeError, lambda: sp.clone(test1 = SwitchProducerTest(test1 = SwitchProducerTest(test1 = EDProducer("Foo")))))
628 
629  # Dump
630  sp = SwitchProducerTest(test2 = EDProducer("Foo",
631  a = int32(1),
632  b = PSet(c = int32(2))),
633  test1 = EDProducer("Bar",
634  aa = int32(11),
635  bb = PSet(cc = int32(12))))
636  self.assertEqual(sp.dumpPython(),
637 """SwitchProducerTest(
638  test1 = cms.EDProducer("Bar",
639  aa = cms.int32(11),
640  bb = cms.PSet(
641  cc = cms.int32(12)
642  )
643  ),
644  test2 = cms.EDProducer("Foo",
645  a = cms.int32(1),
646  b = cms.PSet(
647  c = cms.int32(2)
648  )
649  )
650 )
651 """)
652  # Pickle
653  import pickle
654  sp = SwitchProducerPickleable(cpu = EDProducer("Foo"))
655  pkl = pickle.dumps(sp)
656  unpkl = pickle.loads(pkl)
657  self.assertEqual(unpkl.cpu.type_(), "Foo")
658 
660  # Constructor
661  sp = SwitchProducerTest(test1 = EDProducer("Foo"), test2 = EDAlias())
662  self.assertEqual(sp.test1.type_(), "Foo")
663  self.assertTrue(isinstance(sp.test2, EDAlias))
664 
665  # Modifications
666  from .Types import int32, string, PSet, VPSet
667  sp = SwitchProducerTest(test1 = EDProducer("Foo"),
668  test2 = EDAlias(foo = VPSet(PSet(type = string("Foo2")))))
669 
670  # Simple clone
671  cl = sp.clone()
672  self.assertTrue(hasattr(cl.test2, "foo"))
673  # Modify clone
674  cl.test2.foo[0].type = "Foo3"
675  self.assertEqual(cl.test2.foo[0].type, "Foo3")
676  # Modify values with a dict
677  cl = sp.clone(test2 = dict(foo = {0: dict(type = "Foo4")}))
678  self.assertEqual(cl.test2.foo[0].type, "Foo4")
679  # Replace or add EDAlias
680  cl = sp.clone(test1 = EDAlias(foo = VPSet(PSet(type = string("Foo5")))),
681  test3 = EDAlias(foo = VPSet(PSet(type = string("Foo6")))))
682  self.assertEqual(cl.test1.foo[0].type, "Foo5")
683  self.assertEqual(cl.test3.foo[0].type, "Foo6")
684  # Modify clone
685  cl.test1 = EDProducer("Xyzzy")
686  self.assertEqual(cl.test1.type_(), "Xyzzy")
687  cl.test1 = EDAlias(foo = VPSet(PSet(type = string("Foo7"))))
688  self.assertEqual(cl.test1.foo[0].type, "Foo7")
689 
690 
691  # Dump
692  from .Types import int32, string, PSet, VPSet
693  sp = SwitchProducerTest(test1 = EDProducer("Foo"),
694  test2 = EDAlias(foo = VPSet(PSet(type = string("Foo2")))))
695 
696  self.assertEqual(sp.dumpPython(),
697 """SwitchProducerTest(
698  test1 = cms.EDProducer("Foo"),
699  test2 = cms.EDAlias(
700  foo = cms.VPSet(cms.PSet(
701  type = cms.string('Foo2')
702  ))
703  )
704 )
705 """)
706 
707  # Pickle
708  import pickle
709  sp = SwitchProducerPickleable(cpu = EDAlias(foo = VPSet(PSet(type = string("Foo2")))))
710  pkl = pickle.dumps(sp)
711  unpkl = pickle.loads(pkl)
712  self.assertEqual(sp.cpu.foo[0].type, "Foo2")
713  unittest.main()
Modules.SwitchProducer.nameInProcessDesc_
def nameInProcessDesc_(self, myname)
Definition: Modules.py:366
Modules.ESPrefer.dumpPythonAs
def dumpPythonAs(self, label, options=PrintOptions())
Definition: Modules.py:113
Modules.Looper
Definition: Modules.py:205
Modules.TestModules.testSequences
def testSequences(self)
Definition: Modules.py:508
Mixins._Parameterizable.__setParameters
def __setParameters(self, parameters)
Definition: Mixins.py:254
Modules.Source.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:195
Modules.EDAnalyzer.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:181
Modules.EDFilter.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:173
Modules.EDProducer._isTaskComponent
def _isTaskComponent(self)
Definition: Modules.py:169
Modules.ESProducer.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:55
Modules.SwitchProducer.directDependencies
def directDependencies(self)
Definition: Modules.py:362
SequenceTypes._SequenceLeaf
Definition: SequenceTypes.py:123
Modules.Service._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:16
SequenceTypes.Path
Definition: SequenceTypes.py:644
Modules._Module.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:137
Modules._switch_cpu
def _switch_cpu()
Definition: Modules.py:218
Modules.Source._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:197
Modules.ESPrefer.moduleLabel_
def moduleLabel_(self, myname)
Definition: Modules.py:109
Modules.Service
Definition: Modules.py:12
Mixins._Parameterizable.parameterNames_
def parameterNames_(self)
Definition: Mixins.py:180
Modules.Source.nameInProcessDesc_
def nameInProcessDesc_(self, myname)
Definition: Modules.py:201
Mixins._Labelable
Definition: Mixins.py:513
Modules.SwitchProducerTest1Dis
Definition: Modules.py:431
Modules.ESPrefer.__init__
def __init__(self, type_, targetLabel='', *arg, **kargs)
Definition: Modules.py:91
Mixins._Parameterizable.isFrozen
def isFrozen(self)
Definition: Mixins.py:286
Modules.TestModules.testSwithProducerWithAlias
def testSwithProducerWithAlias(self)
Definition: Modules.py:659
Modules.SwitchProducerTest.__init__
def __init__(self, **kargs)
Definition: Modules.py:424
Modules.OutputModule._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:190
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
Modules.ESSource
Definition: Modules.py:33
Modules.SwitchProducer._chooseCase
def _chooseCase(self)
Definition: Modules.py:261
Modules.Service.__str__
def __str__(self)
Definition: Modules.py:30
Modules.Looper.nameInProcessDesc_
def nameInProcessDesc_(self, myname)
Definition: Modules.py:212
Modules._Module
Definition: Modules.py:134
Mixins.PrintOptions
Definition: Mixins.py:11
Modules.ESSource.isLeaf
def isLeaf(self)
Definition: Modules.py:51
Modules.SwitchProducerTest2Dis
Definition: Modules.py:438
Modules.SwitchProducerPickleable
Definition: Modules.py:445
Modules.TestModules
Definition: Modules.py:450
Modules.SwitchProducer.__setattr__
def __setattr__(self, name, value)
Definition: Modules.py:299
Modules.ESPrefer.nameInProcessDesc_
def nameInProcessDesc_(self, myname)
Definition: Modules.py:102
Modules.SwitchProducer.__setParameters
def __setParameters(self, parameters)
Definition: Modules.py:295
ExceptionHandling.ModuleCloneError
Definition: ExceptionHandling.py:9
Modules.ESSource.moduleLabel_
def moduleLabel_(self, myname)
Definition: Modules.py:41
Modules.SwitchProducer._caseFunctionDict
_caseFunctionDict
Definition: Modules.py:252
Modules.ESPrefer.targetLabel_
def targetLabel_(self)
Definition: Modules.py:111
Modules._Module.setPrerequisites
def setPrerequisites(self, *libs)
Definition: Modules.py:152
Modules.SwitchProducer._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:394
Modules.EDAnalyzer
Definition: Modules.py:180
Types.vstring
Definition: Types.py:1011
Mixins._TypedParameterizable
Definition: Mixins.py:380
Modules.SwitchProducerPickleable.__init__
def __init__(self, **kargs)
Definition: Modules.py:446
Modules.Source.moduleLabel_
def moduleLabel_(self, myname)
Definition: Modules.py:199
Mixins._TypedParameterizable.type_
def type_(self)
Definition: Mixins.py:394
Mixins._TypedParameterizable.nameInProcessDesc_
def nameInProcessDesc_(self, myname)
Definition: Mixins.py:497
Modules._Module.insertInto
def insertInto(self, parameterSet, myname)
Definition: Modules.py:155
Modules.ESProducer.nameInProcessDesc_
def nameInProcessDesc_(self, myname)
Definition: Modules.py:66
Modules.SwitchProducerTest1Dis.__init__
def __init__(self, **kargs)
Definition: Modules.py:432
Modules.Source
Definition: Modules.py:194
Mixins._Parameterizable.setIsFrozen
def setIsFrozen(self)
Definition: Mixins.py:288
Modules.ESProducer._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:57
Modules.ESSource.nameInProcessDesc_
def nameInProcessDesc_(self, myname)
Definition: Modules.py:46
Modules.Looper.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:206
Modules.SwitchProducer.__init__
def __init__(self, caseFunctionDict, **kargs)
Definition: Modules.py:250
Modules.ESProducer.moduleLabel_
def moduleLabel_(self, myname)
Definition: Modules.py:61
Modules.Looper.moduleLabel_
def moduleLabel_(self, myname)
Definition: Modules.py:210
Modules.ESPrefer.copy
def copy(self)
Definition: Modules.py:105
str
#define str(s)
Definition: TestProcessor.cc:52
Modules.TestModules.testEDAnalyzer
def testEDAnalyzer(self)
Definition: Modules.py:451
SequenceTypes.Sequence
Definition: SequenceTypes.py:656
Modules.EDProducer.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:165
Modules.EDFilter
Definition: Modules.py:172
Modules.TestModules.testSwitchProducer
def testSwitchProducer(self)
Definition: Modules.py:552
Modules.EDFilter._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:175
Mixins._Unlabelable
Definition: Mixins.py:569
Modules.SwitchProducer.appendToProcessDescLists_
def appendToProcessDescLists_(self, modules, aliases, myname)
Definition: Modules.py:372
Modules.TestModules.testService
def testService(self)
Definition: Modules.py:501
Modules.Service.insertInto
def insertInto(self, processDesc)
Definition: Modules.py:19
Types.PSet
Definition: Types.py:860
Modules.Service.dumpSequencePython
def dumpSequencePython(self, options=PrintOptions())
Definition: Modules.py:24
Modules.TestModules.testIsTaskComponent
def testIsTaskComponent(self)
Definition: Modules.py:516
Modules.ESPrefer._targetLabel
_targetLabel
Definition: Modules.py:93
Modules.Service._inProcess
_inProcess
Definition: Modules.py:15
Modules.EDAnalyzer._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:183
Modules.TestModules.testESPrefer
def testESPrefer(self)
Definition: Modules.py:465
Modules.ESSource._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:37
mps_setup.append
append
Definition: mps_setup.py:85
Modules.SwitchProducer._clonesequence
def _clonesequence(self, lookuptable)
Definition: Modules.py:409
Modules.ESProducer._isTaskComponent
def _isTaskComponent(self)
Definition: Modules.py:69
Mixins._ConfigureComponent
Definition: Mixins.py:6
Modules.ESPrefer
Definition: Modules.py:74
Modules.ESPrefer._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:100
Modules.Service.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:13
Modules.ESProducer
Definition: Modules.py:54
Modules.SwitchProducer.moduleLabel_
def moduleLabel_(self, myname)
Definition: Modules.py:368
Types.string
Definition: Types.py:324
Modules.SwitchProducer
Definition: Modules.py:221
Modules.SwitchProducerTest2Dis.__init__
def __init__(self, **kargs)
Definition: Modules.py:439
Modules.SwitchProducerTest
Definition: Modules.py:423
Modules.EDProducer
Definition: Modules.py:164
Types.EDAlias
Definition: Types.py:1392
Mixins.saveOrigin
def saveOrigin(obj, level)
Definition: Mixins.py:697
Mixins._TypedParameterizable.moduleLabel_
def moduleLabel_(self, myname)
Definition: Mixins.py:499
Modules.OutputModule
Definition: Modules.py:187
Mixins._Parameterizable._isModified
_isModified
Definition: Mixins.py:178
Modules._Module._clonesequence
def _clonesequence(self, lookuptable)
Definition: Modules.py:142
Types.VPSet
Definition: Types.py:1202
Modules.Looper._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:208
Modules.SwitchProducer._getProducer
def _getProducer(self)
Definition: Modules.py:273
Modules.SwitchProducer.clone
def clone(self, **params)
Definition: Modules.py:324
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:29
Modules.SwitchProducer.getCpu
def getCpu()
Definition: Modules.py:257
SequenceTypes.Task
Definition: SequenceTypes.py:1410
Types.untracked
untracked
Definition: Types.py:35
Mixins._Parameterizable.insertContentsInto
def insertContentsInto(self, parameterSet)
Definition: Mixins.py:374
Modules.ESSource.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:34
Modules.SwitchProducer._errorstr
def _errorstr(self)
Definition: Modules.py:414
Modules.EDProducer._placeImpl
def _placeImpl(self, name, proc)
Definition: Modules.py:167
Modules.SwitchProducer.dumpPython
def dumpPython(self, options=PrintOptions())
Definition: Modules.py:347
Modules.Service._isTaskComponent
def _isTaskComponent(self)
Definition: Modules.py:26
Types.int32
Definition: Types.py:202
Mixins._Parameterizable.dumpPython
def dumpPython(self, options=PrintOptions())
Definition: Mixins.py:300
Modules._Module._errorstr
def _errorstr(self)
Definition: Modules.py:147
Modules.OutputModule.__init__
def __init__(self, type_, *arg, **kargs)
Definition: Modules.py:188
Modules.SwitchProducer.caseLabel_
def caseLabel_(self, name, case)
Definition: Modules.py:370
SequenceTypes.EndPath
Definition: SequenceTypes.py:650
Modules.SwitchProducer.__addParameter
def __addParameter(self, name, value)
Definition: Modules.py:281
Modules.SwitchProducer.__typeIsValid
def __typeIsValid(typ)
Definition: Modules.py:278
Modules.ESSource._isTaskComponent
def _isTaskComponent(self)
Definition: Modules.py:49
Modules.SwitchProducer.insertInto
def insertInto(self, parameterSet, myname)
Definition: Modules.py:382
Modules.EDFilter._isTaskComponent
def _isTaskComponent(self)
Definition: Modules.py:177
Modules.Service.isLeaf
def isLeaf(self)
Definition: Modules.py:28
Mixins._Parameterizable.__addParameter
def __addParameter(self, name, value)
Definition: Mixins.py:235
Modules.ESProducer.isLeaf
def isLeaf(self)
Definition: Modules.py:71
ExceptionHandling.format_typename
def format_typename(object)
Definition: ExceptionHandling.py:24