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
10 from .ExceptionHandling
import *
13 super(Service,self).
__init__(type_,*arg,**kargs)
17 proc._placeService(self.
type_(),self)
19 newpset = processDesc.newPSet()
20 newpset.addString(
True,
"@service_type", self.
type_())
22 processDesc.addService(newpset)
24 return "process." + self.
type_()
34 super(ESSource,self).
__init__(type_,*arg,**kargs)
39 proc._placeESSource(name,self)
42 if self.
type_() == myname:
55 super(ESProducer,self).
__init__(type_,*arg,**kargs)
59 proc._placeESProducer(name,self)
62 if self.
type_() == myname:
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. 81 #prefer all data in record 'OrangeRecord' from 'juicer' 82 ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring()) 84 #prefer only "Orange" data in "OrangeRecord" from "juicer" 85 ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("ExtraPulp")) 87 #prefer only "Orange" data with label "ExtraPulp" in "OrangeRecord" from "juicer" 88 ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("Orange/ExtraPulp")) 90 def __init__(self,type_,targetLabel:str=
'',*arg,**kargs):
91 super(ESPrefer,self).
__init__(type_,*arg,**kargs)
93 if targetLabel
is None:
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)))
100 proc._placeESPrefer(name,self)
105 returnValue = ESPrefer.__new__(type(self))
113 result = options.indentation()
116 basename = self.
type_()
119 result +=
'process.prefer("'+basename+
'"' 121 result +=
",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
125 result +=
'es_prefer_'+basename+
' = cms.ESPrefer("'+self.
type_()+
'"' 129 result +=
",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
134 """base class for classes which denote framework event based 'modules'""" 137 super(_Module,self).
__init__(type_,*arg,**kargs)
138 if _Module.__isStrict__:
143 return lookuptable[
id(self)]
149 return "%s('%s', ...)" %(typename, self.type_())
152 self.__dict__[
"libraries_"] = libs
155 if "libraries_" in self.__dict__:
156 from ctypes
import LibraryLoader, CDLL
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)
165 super(EDProducer,self).
__init__(type_,*arg,**kargs)
167 proc._placeProducer(name,self)
173 super(EDFilter,self).
__init__(type_,*arg,**kargs)
175 proc._placeFilter(name,self)
181 super(EDAnalyzer,self).
__init__(type_,*arg,**kargs)
183 proc._placeAnalyzer(name,self)
188 super(OutputModule,self).
__init__(type_,*arg,**kargs)
190 proc._placeOutputModule(name,self)
195 super(Source,self).
__init__(type_,*arg,**kargs)
197 proc._placeSource(name,self)
206 super(Looper,self).
__init__(type_,*arg,**kargs)
208 proc._placeLooper(name,self)
210 return "@main_looper" 212 return "@main_looper" 221 """This purpose class is to provide a switch of EDProducers for a single module/product label. 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: 227 class SwitchProducerFoo(SwitchProducer): 228 def __init__(self, **kargs): 229 super(SwitchProducerFoo,self).__init__( 230 dict(case1 = case1Func, case2 = case2Func), 234 foo = SwitchProducerFoo( 235 case1 = EDProducer("Producer1"), 236 case2 = EDProducer("Producer2") 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. 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 250 super(SwitchProducer,self).
__init__(
None)
260 producer = self.__dict__[case]
261 producer.setLabel(self.
caseLabel_(label, case)
if label
is not None else None)
265 """Returns a function that returns the priority for a CPU "computing device". Intended to be used by deriving classes.""" 269 """Returns the name of the chosen case.""" 274 if enabled
and (bestCase
is None or bestCase[0] < priority):
275 bestCase = (priority, case)
277 raise RuntimeError(
"All cases '%s' were disabled" % (
str(cases)))
281 """Returns the EDroducer of the chosen case""" 282 return self.__dict__[self.
_chooseCase(accelerators)]
286 return (isinstance(typ, EDProducer)
and not isinstance(typ, SwitchProducer))
or isinstance(typ, EDAlias)
290 raise TypeError(name+
" does not already exist, so it can only be set to a cms.EDProducer or cms.EDAlias")
293 if name
in self.__dict__:
294 message =
"Duplicate insert of member " + name
295 message +=
"\nThe original parameters are:\n" 297 raise ValueError(message)
298 self.__dict__[name]=value
301 self._Parameterizable__parameterNames.
append(name)
305 for name, value
in parameters.items():
314 if self.
isFrozen()
and not (name
in [
"_Labelable__label",
"_isFrozen"]
or name.startswith(
'_')):
315 message =
"Object already added to a process. It is read only now\n" 316 message +=
" %s = %s" %(name, value)
317 message +=
"\nThe original parameters are:\n" 319 raise ValueError(message)
322 super(SwitchProducer, self).
__setattr__(name,value)
323 elif not name
in self.__dict__:
328 raise TypeError(name+
" can only be set to a cms.EDProducer or cms.EDAlias")
330 self.__dict__[name] = value
336 returnValue = SwitchProducer.__new__(type(self))
340 for name, value
in params.items():
343 elif isinstance(value, dict):
344 myparams[name] = self.__dict__[name].
clone(**value)
346 myparams[name] = value.clone()
350 if name
not in params:
351 myparams[name] = self.__dict__[name].
clone()
352 returnValue.__init__(**myparams)
353 returnValue._isModified =
False 354 returnValue._isFrozen =
False 362 specialImportRegistry.registerUse(self)
363 result =
"%s(" % self.__class__.__name__
366 result +=
"\n" + options.indentation() + resource +
" = " + getattr(self, resource).
dumpPython(options).
rstrip() +
"," 367 if result[-1] ==
",":
368 result = result.rstrip(
",")
387 caseobj = self.__dict__[case]
388 if not isinstance(caseobj, EDAlias):
394 modules.append(myname)
396 if isinstance(self.__dict__[case], EDAlias):
401 def insertInto(self, parameterSet, myname:str, accelerators):
403 producer = self.__dict__[case]
404 producer.insertInto(parameterSet, self.
caseLabel_(myname, case))
405 newpset = parameterSet.newPSet()
406 newpset.addString(
True,
"@module_label", self.
moduleLabel_(myname))
407 newpset.addString(
True,
"@module_type",
"SwitchProducer")
408 newpset.addString(
True,
"@module_edm_type",
"EDProducer")
414 proc._placeSwitchProducer(name,self)
430 return lookuptable[
id(self)]
434 return "SwitchProducer" 437 if __name__ ==
"__main__":
440 from .SequenceTypes
import *
444 super(SwitchProducerTest,self).
__init__(
446 test1 =
lambda accelerators: (
"test1" in accelerators, -10),
447 test2 =
lambda accelerators: (
"test2" in accelerators, -9),
448 test3 =
lambda accelerators: (
"test3" in accelerators, -8)
452 super(SwitchProducerPickleable,self).
__init__(
453 dict(cpu = SwitchProducer.getCpu()), **kargs)
459 self.assertEqual(withParam.foo.value(), 1)
460 self.assertEqual(withParam.bar.value(),
"it")
461 aCopy = withParam.copy()
462 self.assertEqual(aCopy.foo.value(), 1)
463 self.assertEqual(aCopy.bar.value(),
"it")
465 self.assertEqual(withType.type.value(),1)
468 self.assertEqual(9, m.i.value())
469 self.assertEqual(10, m.j.value())
474 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
"process.prefer(\"JuiceProducer\")\n")
475 options.isCfg =
False 476 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
"es_prefer_JuiceProducer = cms.ESPrefer(\"JuiceProducer\")\n")
478 juicer =
ESPrefer(
"JuiceProducer",
"juicer")
481 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
'process.prefer("juicer")\n')
482 options.isCfg =
False 483 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
'es_prefer_juicer = cms.ESPrefer("JuiceProducer","juicer")\n')
485 self.assertEqual(juicer.dumpConfig(options),
494 self.assertEqual(juicer.dumpPythonAs(
"juicer"),
495 """process.prefer("JuiceProducer", 496 fooRcd = cms.vstring() 499 options.isCfg =
False 500 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
501 """es_prefer_JuiceProducer = cms.ESPrefer("JuiceProducer", 502 fooRcd = cms.vstring() 509 self.assertEqual(withParam.foo.value(), 1)
510 self.assertEqual(withParam.bar.value(),
"it")
511 self.assertEqual(empty.dumpPython(),
"cms.Service(\"Empty\")\n")
512 self.assertEqual(withParam.dumpPython(),
"cms.Service(\"Parameterized\",\n bar = cms.untracked.string(\'it\'),\n foo = cms.untracked.int32(1)\n)\n")
523 self.assertTrue(m._isTaskComponent())
524 self.assertTrue(m.isLeaf())
526 self.assertTrue(m._isTaskComponent())
527 self.assertTrue(m.isLeaf())
529 self.assertTrue(m._isTaskComponent())
530 self.assertTrue(m.isLeaf())
532 self.assertFalse(m._isTaskComponent())
533 self.assertTrue(m.isLeaf())
535 self.assertFalse(m._isTaskComponent())
536 self.assertTrue(m.isLeaf())
538 self.assertTrue(m._isTaskComponent())
539 self.assertTrue(m.isLeaf())
541 self.assertTrue(m._isTaskComponent())
542 self.assertTrue(m.isLeaf())
544 self.assertTrue(m._isTaskComponent())
545 self.assertTrue(m.isLeaf())
547 self.assertFalse(m._isTaskComponent())
548 self.assertFalse(m.isLeaf())
550 self.assertFalse(m._isTaskComponent())
552 self.assertFalse(m._isTaskComponent())
554 self.assertTrue(m._isTaskComponent())
555 self.assertFalse(m.isLeaf())
560 self.assertEqual(sp.test1.type_(),
"Foo")
561 self.assertEqual(sp.test2.type_(),
"Bar")
576 self.assertEqual(sp.label_(),
"sp")
577 self.assertEqual(sp.test1.label_(),
"sp@test1")
578 self.assertEqual(sp.test2.label_(),
"sp@test2")
580 self.assertEqual(sp.test3.label_(),
"sp@test3")
582 self.assertEqual(sp.test1.label_(),
"sp@test1")
585 self.assertEqual(sp.test1.label_(),
"sp@test1")
588 self.assertEqual(sp.label_(),
"other")
589 self.assertEqual(sp.test1.label_(),
"other@test1")
590 self.assertEqual(sp.test2.label_(),
"other@test2")
593 accelerators = [
"test1",
"test2",
"test3"]
595 self.assertEqual(sp._getProducer([
"test1",
"test2",
"test3"]).type_(),
"Bar")
596 self.assertEqual(sp._getProducer([
"test2",
"test3"]).type_(),
"Bar")
597 self.assertEqual(sp._getProducer([
"test1",
"test3"]).type_(),
"Foo")
599 self.assertEqual(sp._getProducer([
"test1",
"test2",
"test3"]).type_(),
"Bar")
600 self.assertRaises(RuntimeError, sp._getProducer, [
"test2",
"test3"])
603 from .Types
import int32, string, PSet
612 self.assertEqual(cl.test1.type_(),
"Foo")
613 self.assertEqual(cl.test1.a.value(), 1)
614 self.assertEqual(cl.test1.b.c.value(), 2)
615 self.assertEqual(cl.test2.type_(),
"Bar")
616 self.assertEqual(cl.test2.aa.value(), 11)
617 self.assertEqual(cl.test2.bb.cc.value(), 12)
618 self.assertEqual(sp._getProducer(accelerators).type_(),
"Bar")
621 self.assertEqual(cl.test1.a.value(), 3)
623 self.assertEqual(cl.test1.type_(),
"Fred")
624 def _assignEDAnalyzer():
626 self.assertRaises(TypeError, _assignEDAnalyzer)
627 def _assignSwitchProducer():
629 self.assertRaises(TypeError, _assignSwitchProducer)
631 cl = sp.clone(test1 = dict(a = 4, b = dict(c =
None)),
632 test2 = dict(aa = 15, bb = dict(cc = 45, dd =
string(
"foo"))))
633 self.assertEqual(cl.test1.a.value(), 4)
634 self.assertEqual(cl.test1.b.hasParameter(
"c"),
False)
635 self.assertEqual(cl.test2.aa.value(), 15)
636 self.assertEqual(cl.test2.bb.cc.value(), 45)
637 self.assertEqual(cl.test2.bb.dd.value(),
"foo")
642 self.assertEqual(cl.test1.type_(),
"Fred")
643 self.assertEqual(cl.test1.x.value(), 42)
644 self.assertEqual(cl.test3.type_(),
"Wilma")
645 self.assertEqual(cl.test3.y.value(), 24)
646 self.assertEqual(hasattr(cl,
"test2"),
False)
647 self.assertRaises(TypeError,
lambda: sp.clone(test1 =
EDAnalyzer(
"Foo")))
657 self.assertEqual(sp.dumpPython(),
658 """SwitchProducerTest( 659 test1 = cms.EDProducer("Bar", 665 test2 = cms.EDProducer("Foo", 676 pkl = pickle.dumps(sp)
677 unpkl = pickle.loads(pkl)
678 self.assertEqual(unpkl.cpu.type_(),
"Foo")
683 self.assertEqual(sp.test1.type_(),
"Foo")
684 self.assertTrue(isinstance(sp.test2, EDAlias))
687 from .Types
import int32, string, PSet, VPSet
693 self.assertTrue(hasattr(cl.test2,
"foo"))
695 cl.test2.foo[0].type =
"Foo3" 696 self.assertEqual(cl.test2.foo[0].type,
"Foo3")
698 cl = sp.clone(test2 = dict(foo = {0: dict(type =
"Foo4")}))
699 self.assertEqual(cl.test2.foo[0].type,
"Foo4")
703 self.assertEqual(cl.test1.foo[0].type,
"Foo5")
704 self.assertEqual(cl.test3.foo[0].type,
"Foo6")
707 self.assertEqual(cl.test1.type_(),
"Xyzzy")
709 self.assertEqual(cl.test1.foo[0].type,
"Foo7")
713 from .Types
import int32, string, PSet, VPSet
717 self.assertEqual(sp.dumpPython(),
718 """SwitchProducerTest( 719 test1 = cms.EDProducer("Foo"), 721 foo = cms.VPSet(cms.PSet( 722 type = cms.string('Foo2') 731 pkl = pickle.dumps(sp)
732 unpkl = pickle.loads(pkl)
733 self.assertEqual(sp.cpu.foo[0].type,
"Foo2")
def _isTaskComponent(self)
def testSwitchProducer(self)
def _isTaskComponent(self)
def __setParameters(self, parameters)
def _isTaskComponent(self)
def __setParameters(self, parameters)
def setPrerequisites(self, libs)
def __init__(self, type_, arg, kargs)
void rstrip(TString &in, TString separator="#")
def testIsTaskComponent(self)
def modulesForConditionalTask_(self)
def __init__(self, kargs)
def _getProducer(self, accelerators)
def format_typename(object)
def __init__(self, type_, arg, kargs)
def _clonesequence(self, lookuptable)
def insertInto(self, processDesc)
def __init__(self, type_, arg, kargs)
def __init__(self, type_, arg, kargs)
def __init__(self, type_, arg, kargs)
def __init__(self, kargs)
def _isTaskComponent(self)
def _chooseCase(self, accelerators)
def saveOrigin(obj, level)
def __init__(self, type_, arg, kargs)
def directDependencies(self)
def _switch_cpu(accelerators)
static std::string join(char **cmd)
def _isTaskComponent(self)
def parameterNames_(self)
def __init__(self, type_, arg, kargs)
def __init__(self, caseFunctionDict, kargs)
def __init__(self, type_, arg, kargs)
def _clonesequence(self, lookuptable)
def appendToProcessDescLists_
def testSwithProducerWithAlias(self)
def __init__(self, type_, arg, kargs)
def insertContentsInto(self, parameterSet)
def __init__(self, type_, arg, kargs)