CMS 3D CMS Logo

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