CMS 3D CMS Logo

Modules.py
Go to the documentation of this file.
1 from Mixins import _ConfigureComponent, saveOrigin
2 from Mixins import _Unlabelable, _Labelable
3 from Mixins import _TypedParameterizable, _Parameterizable, PrintOptions
4 from SequenceTypes import _SequenceLeaf
5 from Types import vstring
6 
7 from ExceptionHandling import *
9  def __init__(self,type_,*arg,**kargs):
10  super(Service,self).__init__(type_,*arg,**kargs)
11  self._inProcess = False
12  def _placeImpl(self,name,proc):
13  self._inProcess = True
14  proc._placeService(self.type_(),self)
15  def insertInto(self, processDesc):
16  newpset = processDesc.newPSet()
17  newpset.addString(True, "@service_type", self.type_())
18  self.insertContentsInto(newpset)
19  processDesc.addService(newpset)
20  def dumpSequencePython(self, options=PrintOptions()):
21  return "process." + self.type_()
22  def _isTaskComponent(self):
23  return True
24  def isLeaf(self):
25  return True
26  def __str__(self):
27  return str(self.type_())
28 
30  def __init__(self,type_,*arg,**kargs):
31  super(ESSource,self).__init__(type_,*arg,**kargs)
32  saveOrigin(self, 1)
33  def _placeImpl(self,name,proc):
34  if name == '':
35  name=self.type_()
36  proc._placeESSource(name,self)
37  def moduleLabel_(self,myname):
38  result = myname
39  if self.type_() == myname:
40  result = ""
41  return result
42  def nameInProcessDesc_(self, myname):
43  result = self.type_() + "@" + self.moduleLabel_(myname)
44  return result
45  def _isTaskComponent(self):
46  return True
47  def isLeaf(self):
48  return True
49 
51  def __init__(self,type_,*arg,**kargs):
52  super(ESProducer,self).__init__(type_,*arg,**kargs)
53  def _placeImpl(self,name,proc):
54  if name == '':
55  name=self.type_()
56  proc._placeESProducer(name,self)
57  def moduleLabel_(self,myname):
58  result = myname
59  if self.type_() == myname:
60  result = ''
61  return result
62  def nameInProcessDesc_(self, myname):
63  result = self.type_() + "@" + self.moduleLabel_(myname)
64  return result
65  def _isTaskComponent(self):
66  return True
67  def isLeaf(self):
68  return True
69 
71  """Used to set which EventSetup provider should provide a particular data item
72  in the case where multiple providers are capable of delivering the data.
73  The first argument specifies the C++ class type of the prodiver.
74  If the provider has been given a label, you must specify that label as the second argument.
75  Additional 'vstring' arguments maybe used to specify exactly which EventSetup Records
76  are being preferred and optionally which data items within that Record.
77  E.g.,
78  #prefer all data in record 'OrangeRecord' from 'juicer'
79  ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring())
80  or
81  #prefer only "Orange" data in "OrangeRecord" from "juicer"
82  ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("ExtraPulp"))
83  or
84  #prefer only "Orange" data with label "ExtraPulp" in "OrangeRecord" from "juicer"
85  ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("Orange/ExtraPulp"))
86  """
87  def __init__(self,type_,targetLabel='',*arg,**kargs):
88  super(ESPrefer,self).__init__(type_,*arg,**kargs)
89  self._targetLabel = targetLabel
90  if targetLabel is None:
91  self._targetLabel = str('')
92  if kargs:
93  for k,v in kargs.iteritems():
94  if not isinstance(v,vstring):
95  raise RuntimeError('ESPrefer only allows vstring attributes. "'+k+'" is a '+str(type(v)))
96  def _placeImpl(self,name,proc):
97  proc._placeESPrefer(name,self)
98  def nameInProcessDesc_(self, myname):
99  # the C++ parser can give it a name like "label@prefer". Get rid of that.
100  return "esprefer_" + self.type_() + "@" + self._targetLabel
101  def copy(self):
102  returnValue = ESPrefer.__new__(type(self))
103  returnValue.__init__(self.type_(), self._targetLabel)
104  return returnValue
105  def moduleLabel_(self, myname):
106  return self._targetLabel
107  def targetLabel_(self):
108  return self._targetLabel
109  def dumpPythonAs(self, label, options=PrintOptions()):
110  result = options.indentation()
111  basename = self._targetLabel
112  if basename == '':
113  basename = self.type_()
114  if options.isCfg:
115  # do either type or label
116  result += 'process.prefer("'+basename+'"'
117  if self.parameterNames_():
118  result += ",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
119  result +=')\n'
120  else:
121  # use the base class Module
122  result += 'es_prefer_'+basename+' = cms.ESPrefer("'+self.type_()+'"'
123  if self._targetLabel != '':
124  result += ',"'+self._targetLabel+'"'
125  if self.parameterNames_():
126  result += ",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
127  result += ')\n'
128  return result
129 
131  """base class for classes which denote framework event based 'modules'"""
132  __isStrict__ = False
133  def __init__(self,type_,*arg,**kargs):
134  super(_Module,self).__init__(type_,*arg,**kargs)
135  if _Module.__isStrict__:
136  self.setIsFrozen()
137  saveOrigin(self, 2)
138  def _clonesequence(self, lookuptable):
139  try:
140  return lookuptable[id(self)]
141  except:
142  raise ModuleCloneError(self._errorstr())
143  def _errorstr(self):
144  # return something like "EDAnalyzer("foo", ...)"
145  typename = format_typename(self)
146  return "%s('%s', ...)" %(typename, self.type_())
147 
148  def setPrerequisites(self, *libs):
149  self.__dict__["libraries_"] = libs
150 
151  def insertInto(self, parameterSet, myname):
152  if "libraries_" in self.__dict__:
153  from ctypes import LibraryLoader, CDLL
154  import platform
155  loader = LibraryLoader(CDLL)
156  ext = platform.uname()[0] == "Darwin" and "dylib" or "so"
157  [loader.LoadLibrary("lib%s.%s" % (l, ext)) for l in self.libraries_]
158  super(_Module,self).insertInto(parameterSet,myname)
159 
161  def __init__(self,type_,*arg,**kargs):
162  super(EDProducer,self).__init__(type_,*arg,**kargs)
163  def _placeImpl(self,name,proc):
164  proc._placeProducer(name,self)
165  def _isTaskComponent(self):
166  return True
167 
169  def __init__(self,type_,*arg,**kargs):
170  super(EDFilter,self).__init__(type_,*arg,**kargs)
171  def _placeImpl(self,name,proc):
172  proc._placeFilter(name,self)
173  def _isTaskComponent(self):
174  return True
175 
177  def __init__(self,type_,*arg,**kargs):
178  super(EDAnalyzer,self).__init__(type_,*arg,**kargs)
179  def _placeImpl(self,name,proc):
180  proc._placeAnalyzer(name,self)
181 
182 
184  def __init__(self,type_,*arg,**kargs):
185  super(OutputModule,self).__init__(type_,*arg,**kargs)
186  def _placeImpl(self,name,proc):
187  proc._placeOutputModule(name,self)
188 
189 
191  def __init__(self,type_,*arg,**kargs):
192  super(Source,self).__init__(type_,*arg,**kargs)
193  def _placeImpl(self,name,proc):
194  proc._placeSource(name,self)
195  def moduleLabel_(self,myname):
196  return "@main_input"
197  def nameInProcessDesc_(self,myname):
198  return "@main_input"
199 
200 
202  def __init__(self,type_,*arg,**kargs):
203  super(Looper,self).__init__(type_,*arg,**kargs)
204  def _placeImpl(self,name,proc):
205  proc._placeLooper(name,self)
206  def moduleLabel_(self,myname):
207  return "@main_looper"
208  def nameInProcessDesc_(self, myname):
209  return "@main_looper"
210 
211 
212 
213 if __name__ == "__main__":
214  import unittest
215  from Types import *
216  from SequenceTypes import *
217  class TestModules(unittest.TestCase):
218  def testEDAnalyzer(self):
219  empty = EDAnalyzer("Empty")
220  withParam = EDAnalyzer("Parameterized",foo=untracked(int32(1)), bar = untracked(string("it")))
221  self.assertEqual(withParam.foo.value(), 1)
222  self.assertEqual(withParam.bar.value(), "it")
223  aCopy = withParam.copy()
224  self.assertEqual(aCopy.foo.value(), 1)
225  self.assertEqual(aCopy.bar.value(), "it")
226  withType = EDAnalyzer("Test",type = int32(1))
227  self.assertEqual(withType.type.value(),1)
228  block = PSet(i = int32(9))
229  m = EDProducer("DumbProducer", block, j = int32(10))
230  self.assertEqual(9, m.i.value())
231  self.assertEqual(10, m.j.value())
232  def testESPrefer(self):
233  juicer = ESPrefer("JuiceProducer")
234  options = PrintOptions()
235  options.isCfg = True
236  self.assertEqual(juicer.dumpPythonAs("juicer", options), "process.prefer(\"JuiceProducer\")\n")
237  options.isCfg = False
238  self.assertEqual(juicer.dumpPythonAs("juicer", options), "es_prefer_JuiceProducer = cms.ESPrefer(\"JuiceProducer\")\n")
239 
240  juicer = ESPrefer("JuiceProducer","juicer")
241  options = PrintOptions()
242  options.isCfg = True
243  self.assertEqual(juicer.dumpPythonAs("juicer", options), 'process.prefer("juicer")\n')
244  options.isCfg = False
245  self.assertEqual(juicer.dumpPythonAs("juicer", options), 'es_prefer_juicer = cms.ESPrefer("JuiceProducer","juicer")\n')
246  juicer = ESPrefer("JuiceProducer",fooRcd=vstring())
247  self.assertEqual(juicer.dumpConfig(options),
248 """JuiceProducer {
249  vstring fooRcd = {
250  }
251 
252 }
253 """)
254  options = PrintOptions()
255  options.isCfg = True
256  self.assertEqual(juicer.dumpPythonAs("juicer"),
257 """process.prefer("JuiceProducer",
258  fooRcd = cms.vstring()
259 )
260 """)
261  options.isCfg = False
262  self.assertEqual(juicer.dumpPythonAs("juicer", options),
263 """es_prefer_JuiceProducer = cms.ESPrefer("JuiceProducer",
264  fooRcd = cms.vstring()
265 )
266 """)
267 
268  def testService(self):
269  empty = Service("Empty")
270  withParam = Service("Parameterized",foo=untracked(int32(1)), bar = untracked(string("it")))
271  self.assertEqual(withParam.foo.value(), 1)
272  self.assertEqual(withParam.bar.value(), "it")
273  self.assertEqual(empty.dumpPython(), "cms.Service(\"Empty\")\n")
274  self.assertEqual(withParam.dumpPython(), "cms.Service(\"Parameterized\",\n bar = cms.untracked.string(\'it\'),\n foo = cms.untracked.int32(1)\n)\n")
275  def testSequences(self):
276  m = EDProducer("MProducer")
277  n = EDProducer("NProducer")
278  m.setLabel("m")
279  n.setLabel("n")
280  s1 = Sequence(m*n)
281  options = PrintOptions()
282 
284  m = EDProducer("x")
285  self.assertTrue(m._isTaskComponent())
286  self.assertTrue(m.isLeaf())
287  m = EDFilter("x")
288  self.assertTrue(m._isTaskComponent())
289  self.assertTrue(m.isLeaf())
290  m = OutputModule("x")
291  self.assertFalse(m._isTaskComponent())
292  self.assertTrue(m.isLeaf())
293  m = EDAnalyzer("x")
294  self.assertFalse(m._isTaskComponent())
295  self.assertTrue(m.isLeaf())
296  m = Service("x")
297  self.assertTrue(m._isTaskComponent())
298  self.assertTrue(m.isLeaf())
299  m = ESProducer("x")
300  self.assertTrue(m._isTaskComponent())
301  self.assertTrue(m.isLeaf())
302  m = ESSource("x")
303  self.assertTrue(m._isTaskComponent())
304  self.assertTrue(m.isLeaf())
305  m = Sequence()
306  self.assertFalse(m._isTaskComponent())
307  self.assertFalse(m.isLeaf())
308  m = Path()
309  self.assertFalse(m._isTaskComponent())
310  m = EndPath()
311  self.assertFalse(m._isTaskComponent())
312  m = Task()
313  self.assertTrue(m._isTaskComponent())
314  self.assertFalse(m.isLeaf())
315 
316  unittest.main()
def _placeImpl(self, name, proc)
Definition: Modules.py:204
def moduleLabel_(self, myname)
Definition: Mixins.py:436
def nameInProcessDesc_(self, myname)
Definition: Modules.py:208
def moduleLabel_(self, myname)
Definition: Modules.py:57
def _isTaskComponent(self)
Definition: Modules.py:45
def _placeImpl(self, name, proc)
Definition: Modules.py:193
def _isTaskComponent(self)
Definition: Modules.py:22
def testEDAnalyzer(self)
Definition: Modules.py:218
def isLeaf(self)
Definition: Modules.py:47
def _placeImpl(self, name, proc)
Definition: Modules.py:96
def _errorstr(self)
Definition: Modules.py:143
def nameInProcessDesc_(self, myname)
Definition: Modules.py:98
def _isTaskComponent(self)
Definition: Modules.py:165
def nameInProcessDesc_(self, myname)
Definition: Modules.py:197
def setPrerequisites(self, libs)
Definition: Modules.py:148
def __init__(self, type_, arg, kargs)
Definition: Modules.py:184
def testIsTaskComponent(self)
Definition: Modules.py:283
def moduleLabel_(self, myname)
Definition: Modules.py:105
def __init__(self, type_, targetLabel='', arg, kargs)
Definition: Modules.py:87
def format_typename(object)
def __init__(self, type_, arg, kargs)
Definition: Modules.py:177
def _clonesequence(self, lookuptable)
Definition: Modules.py:138
def insertInto(self, processDesc)
Definition: Modules.py:15
def __init__(self, type_, arg, kargs)
Definition: Modules.py:191
def __init__(self, type_, arg, kargs)
Definition: Modules.py:133
def _placeImpl(self, name, proc)
Definition: Modules.py:171
def setIsFrozen(self)
Definition: Mixins.py:236
def __init__(self, type_, arg, kargs)
Definition: Modules.py:30
def moduleLabel_(self, myname)
Definition: Modules.py:195
def targetLabel_(self)
Definition: Modules.py:107
def _placeImpl(self, name, proc)
Definition: Modules.py:53
def _isTaskComponent(self)
Definition: Modules.py:65
def _placeImpl(self, name, proc)
Definition: Modules.py:179
def isLeaf(self)
Definition: Modules.py:24
def nameInProcessDesc_(self, myname)
Definition: Modules.py:42
def saveOrigin(obj, level)
Definition: Mixins.py:621
def moduleLabel_(self, myname)
Definition: Modules.py:206
def __init__(self, type_, arg, kargs)
Definition: Modules.py:51
def _placeImpl(self, name, proc)
Definition: Modules.py:12
def _isTaskComponent(self)
Definition: Modules.py:173
def __str__(self)
Definition: Modules.py:26
def _placeImpl(self, name, proc)
Definition: Modules.py:33
def testSequences(self)
Definition: Modules.py:275
def _placeImpl(self, name, proc)
Definition: Modules.py:163
def parameterNames_(self)
Definition: Mixins.py:140
def dumpPythonAs(self, label, options=PrintOptions())
Definition: Modules.py:109
def insertInto(self, parameterSet, myname)
Definition: Modules.py:151
def __init__(self, type_, arg, kargs)
Definition: Modules.py:169
def testService(self)
Definition: Modules.py:268
def _placeImpl(self, name, proc)
Definition: Modules.py:186
def moduleLabel_(self, myname)
Definition: Modules.py:37
def __init__(self, type_, arg, kargs)
Definition: Modules.py:9
def testESPrefer(self)
Definition: Modules.py:232
def copy(self)
Definition: Modules.py:101
def nameInProcessDesc_(self, myname)
Definition: Modules.py:62
def isLeaf(self)
Definition: Modules.py:67
def dumpSequencePython(self, options=PrintOptions())
Definition: Modules.py:20
def __init__(self, type_, arg, kargs)
Definition: Modules.py:202
def insertContentsInto(self, parameterSet)
Definition: Mixins.py:317
def __init__(self, type_, arg, kargs)
Definition: Modules.py:161