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(
",")
371 modules.append(myname)
373 if isinstance(self.__dict__[case], EDAlias):
380 producer = self.__dict__[case]
381 producer.insertInto(parameterSet, self.
caseLabel_(myname, case))
382 newpset = parameterSet.newPSet()
383 newpset.addString(
True,
"@module_label", self.
moduleLabel_(myname))
384 newpset.addString(
True,
"@module_type",
"SwitchProducer")
385 newpset.addString(
True,
"@module_edm_type",
"EDProducer")
386 newpset.addVString(
True,
"@all_cases", [myname+
"@"+p
for p
in self.
parameterNames_()])
387 newpset.addString(
False,
"@chosen_case", myname+
"@"+self.
_chooseCase())
391 proc._placeSwitchProducer(name,self)
407 return lookuptable[
id(self)]
411 return "SwitchProducer" 414 if __name__ ==
"__main__":
417 from .SequenceTypes
import *
421 super(SwitchProducerTest,self).
__init__(
423 test1 =
lambda: (
True, -10),
424 test2 =
lambda: (
True, -9),
425 test3 =
lambda: (
True, -8)
429 super(SwitchProducerTest1Dis,self).
__init__(
431 test1 =
lambda: (
False, -10),
432 test2 =
lambda: (
True, -9)
436 super(SwitchProducerTest2Dis,self).
__init__(
438 test1 =
lambda: (
True, -10),
439 test2 =
lambda: (
False, -9)
443 super(SwitchProducerPickleable,self).
__init__(
444 dict(cpu = SwitchProducer.getCpu()), **kargs)
450 self.assertEqual(withParam.foo.value(), 1)
451 self.assertEqual(withParam.bar.value(),
"it")
452 aCopy = withParam.copy()
453 self.assertEqual(aCopy.foo.value(), 1)
454 self.assertEqual(aCopy.bar.value(),
"it")
456 self.assertEqual(withType.type.value(),1)
459 self.assertEqual(9, m.i.value())
460 self.assertEqual(10, m.j.value())
465 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
"process.prefer(\"JuiceProducer\")\n")
466 options.isCfg =
False 467 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
"es_prefer_JuiceProducer = cms.ESPrefer(\"JuiceProducer\")\n")
469 juicer =
ESPrefer(
"JuiceProducer",
"juicer")
472 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
'process.prefer("juicer")\n')
473 options.isCfg =
False 474 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
'es_prefer_juicer = cms.ESPrefer("JuiceProducer","juicer")\n')
476 self.assertEqual(juicer.dumpConfig(options),
485 self.assertEqual(juicer.dumpPythonAs(
"juicer"),
486 """process.prefer("JuiceProducer", 487 fooRcd = cms.vstring() 490 options.isCfg =
False 491 self.assertEqual(juicer.dumpPythonAs(
"juicer", options),
492 """es_prefer_JuiceProducer = cms.ESPrefer("JuiceProducer", 493 fooRcd = cms.vstring() 499 withParam =
Service(
"Parameterized",foo=untracked(
int32(1)), bar = untracked(
string(
"it")))
500 self.assertEqual(withParam.foo.value(), 1)
501 self.assertEqual(withParam.bar.value(),
"it")
502 self.assertEqual(empty.dumpPython(),
"cms.Service(\"Empty\")\n")
503 self.assertEqual(withParam.dumpPython(),
"cms.Service(\"Parameterized\",\n bar = cms.untracked.string(\'it\'),\n foo = cms.untracked.int32(1)\n)\n")
514 self.assertTrue(m._isTaskComponent())
515 self.assertTrue(m.isLeaf())
517 self.assertTrue(m._isTaskComponent())
518 self.assertTrue(m.isLeaf())
520 self.assertTrue(m._isTaskComponent())
521 self.assertTrue(m.isLeaf())
523 self.assertFalse(m._isTaskComponent())
524 self.assertTrue(m.isLeaf())
526 self.assertFalse(m._isTaskComponent())
527 self.assertTrue(m.isLeaf())
529 self.assertTrue(m._isTaskComponent())
530 self.assertTrue(m.isLeaf())
532 self.assertTrue(m._isTaskComponent())
533 self.assertTrue(m.isLeaf())
535 self.assertTrue(m._isTaskComponent())
536 self.assertTrue(m.isLeaf())
538 self.assertFalse(m._isTaskComponent())
539 self.assertFalse(m.isLeaf())
541 self.assertFalse(m._isTaskComponent())
543 self.assertFalse(m._isTaskComponent())
545 self.assertTrue(m._isTaskComponent())
546 self.assertFalse(m.isLeaf())
551 self.assertEqual(sp.test1.type_(),
"Foo")
552 self.assertEqual(sp.test2.type_(),
"Bar")
567 self.assertEqual(sp._getProducer().type_(),
"Bar")
569 self.assertEqual(sp._getProducer().type_(),
"Bar")
571 self.assertEqual(sp._getProducer().type_(),
"Foo")
573 self.assertEqual(sp._getProducer().type_(),
"Bar")
575 self.assertRaises(RuntimeError, sp._getProducer)
578 from .Types
import int32, string, PSet
587 self.assertEqual(cl.test1.type_(),
"Foo")
588 self.assertEqual(cl.test1.a.value(), 1)
589 self.assertEqual(cl.test1.b.c.value(), 2)
590 self.assertEqual(cl.test2.type_(),
"Bar")
591 self.assertEqual(cl.test2.aa.value(), 11)
592 self.assertEqual(cl.test2.bb.cc.value(), 12)
593 self.assertEqual(sp._getProducer().type_(),
"Bar")
596 self.assertEqual(cl.test1.a.value(), 3)
598 self.assertEqual(cl.test1.type_(),
"Fred")
599 def _assignEDAnalyzer():
601 self.assertRaises(TypeError, _assignEDAnalyzer)
602 def _assignSwitchProducer():
604 self.assertRaises(TypeError, _assignSwitchProducer)
606 cl = sp.clone(test1 =
dict(a = 4, b =
dict(c =
None)),
608 self.assertEqual(cl.test1.a.value(), 4)
609 self.assertEqual(cl.test1.b.hasParameter(
"c"),
False)
610 self.assertEqual(cl.test2.aa.value(), 15)
611 self.assertEqual(cl.test2.bb.cc.value(), 45)
612 self.assertEqual(cl.test2.bb.dd.value(),
"foo")
617 self.assertEqual(cl.test1.type_(),
"Fred")
618 self.assertEqual(cl.test1.x.value(), 42)
619 self.assertEqual(cl.test3.type_(),
"Wilma")
620 self.assertEqual(cl.test3.y.value(), 24)
621 self.assertEqual(hasattr(cl,
"test2"),
False)
622 self.assertRaises(TypeError,
lambda: sp.clone(test1 =
EDAnalyzer(
"Foo")))
632 self.assertEqual(sp.dumpPython(),
633 """SwitchProducerTest( 634 test1 = cms.EDProducer("Bar", 640 test2 = cms.EDProducer("Foo", 651 pkl = pickle.dumps(sp)
652 unpkl = pickle.loads(pkl)
653 self.assertEqual(unpkl.cpu.type_(),
"Foo")
658 self.assertEqual(sp.test1.type_(),
"Foo")
659 self.assertTrue(isinstance(sp.test2, EDAlias))
662 from .Types
import int32, string, PSet, VPSet
668 self.assertTrue(hasattr(cl.test2,
"foo"))
670 cl.test2.foo[0].type =
"Foo3" 671 self.assertEqual(cl.test2.foo[0].type,
"Foo3")
673 cl = sp.clone(test2 =
dict(foo = {0:
dict(type =
"Foo4")}))
674 self.assertEqual(cl.test2.foo[0].type,
"Foo4")
678 self.assertEqual(cl.test1.foo[0].type,
"Foo5")
679 self.assertEqual(cl.test3.foo[0].type,
"Foo6")
682 self.assertEqual(cl.test1.type_(),
"Xyzzy")
684 self.assertEqual(cl.test1.foo[0].type,
"Foo7")
688 from .Types
import int32, string, PSet, VPSet
692 self.assertEqual(sp.dumpPython(),
693 """SwitchProducerTest( 694 test1 = cms.EDProducer("Foo"), 696 foo = cms.VPSet(cms.PSet( 697 type = cms.string('Foo2') 706 pkl = pickle.dumps(sp)
707 unpkl = pickle.loads(pkl)
708 self.assertEqual(sp.cpu.foo[0].type,
"Foo2")
def _placeImpl(self, name, proc)
def moduleLabel_(self, myname)
def nameInProcessDesc_(self, myname)
def moduleLabel_(self, myname)
def _isTaskComponent(self)
def _placeImpl(self, name, proc)
def testSwitchProducer(self)
def _isTaskComponent(self)
def _placeImpl(self, name, proc)
def __setParameters(self, parameters)
def appendToProcessDescLists_(self, modules, aliases, myname)
def __addParameter(self, name, value)
def nameInProcessDesc_(self, myname)
def _isTaskComponent(self)
def nameInProcessDesc_(self, myname)
def __setParameters(self, parameters)
def setPrerequisites(self, libs)
def __init__(self, type_, arg, kargs)
def __init__(self, kargs)
def dumpPython(self, options=PrintOptions())
def testIsTaskComponent(self)
def moduleLabel_(self, myname)
def __init__(self, type_, targetLabel='', arg, kargs)
def __init__(self, kargs)
def nameInProcessDesc_(self, myname)
def _placeImpl(self, name, proc)
def format_typename(object)
def __init__(self, type_, arg, kargs)
def _clonesequence(self, lookuptable)
def insertInto(self, processDesc)
def dumpPython(self, options=PrintOptions())
def __init__(self, type_, arg, kargs)
def __init__(self, type_, arg, kargs)
def _placeImpl(self, name, proc)
def __init__(self, type_, arg, kargs)
def moduleLabel_(self, myname)
def _placeImpl(self, name, proc)
def __init__(self, kargs)
def _isTaskComponent(self)
def nameInProcessDesc_(self, myname)
def _placeImpl(self, name, proc)
def nameInProcessDesc_(self, myname)
def __addParameter(self, name, value)
def saveOrigin(obj, level)
def insertInto(self, parameterSet, myname)
def moduleLabel_(self, myname)
def __init__(self, type_, arg, kargs)
def _placeImpl(self, name, proc)
static std::string join(char **cmd)
def _isTaskComponent(self)
def _placeImpl(self, name, proc)
def _placeImpl(self, name, proc)
def parameterNames_(self)
def dumpPythonAs(self, label, options=PrintOptions())
def insertInto(self, parameterSet, myname)
def __init__(self, type_, arg, kargs)
def __init__(self, caseFunctionDict, kargs)
def caseLabel_(self, name, case)
def __setattr__(self, name, value)
def _placeImpl(self, name, proc)
def moduleLabel_(self, myname)
def moduleLabel_(self, myname)
def __init__(self, type_, arg, kargs)
def _clonesequence(self, lookuptable)
def nameInProcessDesc_(self, myname)
def testSwithProducerWithAlias(self)
def dumpSequencePython(self, options=PrintOptions())
def __init__(self, type_, arg, kargs)
def insertContentsInto(self, parameterSet)
def __init__(self, type_, arg, kargs)
def __init__(self, kargs)