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