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