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
11 from .ExceptionHandling
import *
14 super(Service,self).
__init__(type_,*arg,**kargs)
18 proc._placeService(self.
type_(),self)
20 newpset = processDesc.newPSet()
21 newpset.addString(
True,
"@service_type", self.
type_())
23 processDesc.addService(newpset)
25 return "process." + self.
type_()
35 super(ESSource,self).
__init__(type_,*arg,**kargs)
40 proc._placeESSource(name,self)
43 if self.
type_() == myname:
56 super(ESProducer,self).
__init__(type_,*arg,**kargs)
60 proc._placeESProducer(name,self)
63 if self.
type_() == myname:
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.
82 #prefer all data in record 'OrangeRecord' from 'juicer'
83 ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring())
85 #prefer only "Orange" data in "OrangeRecord" from "juicer"
86 ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("ExtraPulp"))
88 #prefer only "Orange" data with label "ExtraPulp" in "OrangeRecord" from "juicer"
89 ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("Orange/ExtraPulp"))
91 def __init__(self,type_,targetLabel='',*arg,**kargs):
92 super(ESPrefer,self).
__init__(type_,*arg,**kargs)
94 if targetLabel
is None:
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)))
101 proc._placeESPrefer(name,self)
106 returnValue = ESPrefer.__new__(type(self))
114 result = options.indentation()
117 basename = self.
type_()
120 result +=
'process.prefer("'+basename+
'"'
122 result +=
",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
126 result +=
'es_prefer_'+basename+
' = cms.ESPrefer("'+self.
type_()+
'"'
130 result +=
",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
135 """base class for classes which denote framework event based 'modules'"""
138 super(_Module,self).
__init__(type_,*arg,**kargs)
139 if _Module.__isStrict__:
144 return lookuptable[
id(self)]
150 return "%s('%s', ...)" %(typename, self.type_())
153 self.__dict__[
"libraries_"] = libs
156 if "libraries_" in self.__dict__:
157 from ctypes
import LibraryLoader, CDLL
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)
166 super(EDProducer,self).
__init__(type_,*arg,**kargs)
168 proc._placeProducer(name,self)
174 super(EDFilter,self).
__init__(type_,*arg,**kargs)
176 proc._placeFilter(name,self)
182 super(EDAnalyzer,self).
__init__(type_,*arg,**kargs)
184 proc._placeAnalyzer(name,self)
189 super(OutputModule,self).
__init__(type_,*arg,**kargs)
191 proc._placeOutputModule(name,self)
196 super(Source,self).
__init__(type_,*arg,**kargs)
198 proc._placeSource(name,self)
207 super(Looper,self).
__init__(type_,*arg,**kargs)
209 proc._placeLooper(name,self)
211 return "@main_looper"
213 return "@main_looper"
222 """This purpose class is to provide a switch of EDProducers for a single module/product label.
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:
228 class SwitchProducerFoo(SwitchProducer):
229 def __init__(self, **kargs):
230 super(SwitchProducerFoo,self).__init__(
231 dict(case1 = case1Func, case2 = case2Func),
235 foo = SwitchProducerFoo(
236 case1 = EDProducer("Producer1"),
237 case2 = EDProducer("Producer2")
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.
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
251 super(SwitchProducer,self).
__init__(
None)
258 """Returns a function that returns the priority for a CPU "computing device". Intended to be used by deriving classes."""
262 """Returns the name of the chosen case."""
267 if enabled
and (bestCase
is None or bestCase[0] < priority):
268 bestCase = (priority, case)
270 raise RuntimeError(
"All cases '%s' were disabled" % (
str(cases)))
274 """Returns the EDroducer of the chosen case"""
279 return (isinstance(typ, EDProducer)
and not isinstance(typ, SwitchProducer))
or isinstance(typ, EDAlias)
283 raise TypeError(name+
" does not already exist, so it can only be set to a cms.EDProducer or cms.EDAlias")
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"
290 raise ValueError(message)
291 self.__dict__[name]=value
292 self._Parameterizable__parameterNames.
append(name)
296 for name, value
in six.iteritems(parameters):
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"
310 raise ValueError(message)
313 super(SwitchProducer, self).
__setattr__(name,value)
314 elif not name
in self.__dict__:
319 raise TypeError(name+
" can only be set to a cms.EDProducer or cms.EDAlias")
321 self.__dict__[name] = value
325 returnValue = SwitchProducer.__new__(type(self))
329 for name, value
in six.iteritems(params):
332 elif isinstance(value, dict):
333 myparams[name] = self.__dict__[name].
clone(**value)
335 myparams[name] = value.clone()
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
351 specialImportRegistry.registerUse(self)
352 result =
"%s(" % self.__class__.__name__
355 result +=
"\n" + options.indentation() + resource +
" = " + getattr(self, resource).
dumpPython(options).rstrip() +
","
356 if result[-1] ==
",":
357 result = result.rstrip(
",")
375 modules.append(myname)
377 if isinstance(self.__dict__[case], EDAlias):
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())
395 proc._placeSwitchProducer(name,self)
411 return lookuptable[
id(self)]
415 return "SwitchProducer"
418 if __name__ ==
"__main__":
421 from .SequenceTypes
import *
425 super(SwitchProducerTest,self).
__init__(
427 test1 =
lambda: (
True, -10),
428 test2 =
lambda: (
True, -9),
429 test3 =
lambda: (
True, -8)
433 super(SwitchProducerTest1Dis,self).
__init__(
435 test1 =
lambda: (
False, -10),
436 test2 =
lambda: (
True, -9)
440 super(SwitchProducerTest2Dis,self).
__init__(
442 test1 =
lambda: (
True, -10),
443 test2 =
lambda: (
False, -9)
447 super(SwitchProducerPickleable,self).
__init__(
448 dict(cpu = SwitchProducer.getCpu()), **kargs)
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")
460 self.assertEqual(withType.type.value(),1)
463 self.assertEqual(9, m.i.value())
464 self.assertEqual(10, m.j.value())
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")
473 juicer =
ESPrefer(
"JuiceProducer",
"juicer")
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')
480 self.assertEqual(juicer.dumpConfig(options),
489 self.assertEqual(juicer.dumpPythonAs(
"juicer"),
490 """process.prefer("JuiceProducer",
491 fooRcd = cms.vstring()
494 options.isCfg =
False
495 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
496 """es_prefer_JuiceProducer = cms.ESPrefer("JuiceProducer",
497 fooRcd = cms.vstring()
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")
518 self.assertTrue(m._isTaskComponent())
519 self.assertTrue(m.isLeaf())
521 self.assertTrue(m._isTaskComponent())
522 self.assertTrue(m.isLeaf())
524 self.assertTrue(m._isTaskComponent())
525 self.assertTrue(m.isLeaf())
527 self.assertFalse(m._isTaskComponent())
528 self.assertTrue(m.isLeaf())
530 self.assertFalse(m._isTaskComponent())
531 self.assertTrue(m.isLeaf())
533 self.assertTrue(m._isTaskComponent())
534 self.assertTrue(m.isLeaf())
536 self.assertTrue(m._isTaskComponent())
537 self.assertTrue(m.isLeaf())
539 self.assertTrue(m._isTaskComponent())
540 self.assertTrue(m.isLeaf())
542 self.assertFalse(m._isTaskComponent())
543 self.assertFalse(m.isLeaf())
545 self.assertFalse(m._isTaskComponent())
547 self.assertFalse(m._isTaskComponent())
549 self.assertTrue(m._isTaskComponent())
550 self.assertFalse(m.isLeaf())
555 self.assertEqual(sp.test1.type_(),
"Foo")
556 self.assertEqual(sp.test2.type_(),
"Bar")
571 self.assertEqual(sp._getProducer().type_(),
"Bar")
573 self.assertEqual(sp._getProducer().type_(),
"Bar")
575 self.assertEqual(sp._getProducer().type_(),
"Foo")
577 self.assertEqual(sp._getProducer().type_(),
"Bar")
579 self.assertRaises(RuntimeError, sp._getProducer)
582 from .Types
import int32, string, PSet
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")
600 self.assertEqual(cl.test1.a.value(), 3)
602 self.assertEqual(cl.test1.type_(),
"Fred")
603 def _assignEDAnalyzer():
605 self.assertRaises(TypeError, _assignEDAnalyzer)
606 def _assignSwitchProducer():
608 self.assertRaises(TypeError, _assignSwitchProducer)
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")
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")))
636 self.assertEqual(sp.dumpPython(),
637 """SwitchProducerTest(
638 test1 = cms.EDProducer("Bar",
644 test2 = cms.EDProducer("Foo",
655 pkl = pickle.dumps(sp)
656 unpkl = pickle.loads(pkl)
657 self.assertEqual(unpkl.cpu.type_(),
"Foo")
662 self.assertEqual(sp.test1.type_(),
"Foo")
663 self.assertTrue(isinstance(sp.test2, EDAlias))
666 from .Types
import int32, string, PSet, VPSet
672 self.assertTrue(hasattr(cl.test2,
"foo"))
674 cl.test2.foo[0].type =
"Foo3"
675 self.assertEqual(cl.test2.foo[0].type,
"Foo3")
677 cl = sp.clone(test2 = dict(foo = {0: dict(type =
"Foo4")}))
678 self.assertEqual(cl.test2.foo[0].type,
"Foo4")
682 self.assertEqual(cl.test1.foo[0].type,
"Foo5")
683 self.assertEqual(cl.test3.foo[0].type,
"Foo6")
686 self.assertEqual(cl.test1.type_(),
"Xyzzy")
688 self.assertEqual(cl.test1.foo[0].type,
"Foo7")
692 from .Types
import int32, string, PSet, VPSet
696 self.assertEqual(sp.dumpPython(),
697 """SwitchProducerTest(
698 test1 = cms.EDProducer("Foo"),
700 foo = cms.VPSet(cms.PSet(
701 type = cms.string('Foo2')
710 pkl = pickle.dumps(sp)
711 unpkl = pickle.loads(pkl)
712 self.assertEqual(sp.cpu.foo[0].type,
"Foo2")