00001 from Mixins import _ConfigureComponent, saveOrigin
00002 from Mixins import _Unlabelable, _Labelable
00003 from Mixins import _TypedParameterizable, _Parameterizable, PrintOptions
00004 from SequenceTypes import _SequenceLeaf
00005 from Types import vstring
00006
00007 from ExceptionHandling import *
00008 class Service(_ConfigureComponent,_TypedParameterizable,_Unlabelable):
00009 def __init__(self,type_,*arg,**kargs):
00010 super(Service,self).__init__(type_,*arg,**kargs)
00011 def _placeImpl(self,name,proc):
00012 proc._placeService(self.type_(),self)
00013 def insertInto(self, processDesc):
00014 newpset = processDesc.newPSet()
00015 newpset.addString(True, "@service_type", self.type_())
00016 self.insertContentsInto(newpset)
00017 processDesc.addService(newpset)
00018
00019
00020 class ESSource(_ConfigureComponent,_TypedParameterizable,_Unlabelable,_Labelable):
00021 def __init__(self,type_,*arg,**kargs):
00022 super(ESSource,self).__init__(type_,*arg,**kargs)
00023 saveOrigin(self, 1)
00024 def _placeImpl(self,name,proc):
00025 if name == '':
00026 name=self.type_()
00027 proc._placeESSource(name,self)
00028 def moduleLabel_(self,myname):
00029 result = myname
00030 if self.type_() == myname:
00031 result = ""
00032 return result
00033 def nameInProcessDesc_(self, myname):
00034 result = self.type_() + "@" + self.moduleLabel_(myname)
00035 return result
00036
00037
00038 class ESProducer(_ConfigureComponent,_TypedParameterizable,_Unlabelable,_Labelable):
00039 def __init__(self,type_,*arg,**kargs):
00040 super(ESProducer,self).__init__(type_,*arg,**kargs)
00041 def _placeImpl(self,name,proc):
00042 if name == '':
00043 name=self.type_()
00044 proc._placeESProducer(name,self)
00045 def moduleLabel_(self,myname):
00046 result = myname
00047 if self.type_() == myname:
00048 result = ''
00049 return result
00050 def nameInProcessDesc_(self, myname):
00051 result = self.type_() + "@" + self.moduleLabel_(myname)
00052 return result
00053
00054
00055 class ESPrefer(_ConfigureComponent,_TypedParameterizable,_Unlabelable,_Labelable):
00056 """Used to set which EventSetup provider should provide a particular data item
00057 in the case where multiple providers are capable of delivering the data.
00058 The first argument specifies the C++ class type of the prodiver.
00059 If the provider has been given a label, you must specify that label as the second argument.
00060 Additional 'vstring' arguments maybe used to specify exactly which EventSetup Records
00061 are being preferred and optionally which data items within that Record.
00062 E.g.,
00063 #prefer all data in record 'OrangeRecord' from 'juicer'
00064 ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring())
00065 or
00066 #prefer only "Orange" data in "OrangeRecord" from "juicer"
00067 ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("ExtraPulp"))
00068 or
00069 #prefer only "Orange" data with label "ExtraPulp" in "OrangeRecord" from "juicer"
00070 ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("Orange/ExtraPulp"))
00071 """
00072 def __init__(self,type_,targetLabel='',*arg,**kargs):
00073 super(ESPrefer,self).__init__(type_,*arg,**kargs)
00074 self._targetLabel = targetLabel
00075 if targetLabel is None:
00076 self._targetLabel = str('')
00077 if kargs:
00078 for k,v in kargs.iteritems():
00079 if not isinstance(v,vstring):
00080 raise RuntimeError('ESPrefer only allows vstring attributes. "'+k+'" is a '+str(type(v)))
00081 def _placeImpl(self,name,proc):
00082 proc._placeESPrefer(name,self)
00083 def nameInProcessDesc_(self, myname):
00084
00085 return "esprefer_" + self.type_() + "@" + self._targetLabel
00086 def copy(self):
00087 returnValue = ESPrefer.__new__(type(self))
00088 returnValue.__init__(self.type_(), self._targetLabel)
00089 return returnValue
00090 def moduleLabel_(self, myname):
00091 return self._targetLabel
00092 def targetLabel_(self):
00093 return self._targetLabel
00094 def dumpPythonAs(self, label, options=PrintOptions()):
00095 result = options.indentation()
00096 basename = self._targetLabel
00097 if basename == '':
00098 basename = self.type_()
00099 if options.isCfg:
00100
00101 result += 'process.prefer("'+basename+'"'
00102 if self.parameterNames_():
00103 result += ",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
00104 result +=')\n'
00105 else:
00106
00107 result += 'es_prefer_'+basename+' = cms.ESPrefer("'+self.type_()+'"'
00108 if self._targetLabel != '':
00109 result += ',"'+self._targetLabel+'"'
00110 if self.parameterNames_():
00111 result += ",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
00112 result += ')\n'
00113 return result
00114
00115 class _Module(_ConfigureComponent,_TypedParameterizable,_Labelable,_SequenceLeaf):
00116 """base class for classes which denote framework event based 'modules'"""
00117 __isStrict__ = False
00118 def __init__(self,type_,*arg,**kargs):
00119 super(_Module,self).__init__(type_,*arg,**kargs)
00120 if _Module.__isStrict__:
00121 self.setIsFrozen()
00122 saveOrigin(self, 2)
00123 def _clonesequence(self, lookuptable):
00124 try:
00125 return lookuptable[id(self)]
00126 except:
00127 raise ModuleCloneError(self._errorstr())
00128 def _errorstr(self):
00129
00130 typename = format_typename(self)
00131 return "%s('%s', ...)" %(typename, self.type_())
00132
00133 def setPrerequisites(self, *libs):
00134 self.__dict__["libraries_"] = libs
00135
00136 def insertInto(self, parameterSet, myname):
00137 if "libraries_" in self.__dict__:
00138 from ctypes import LibraryLoader, CDLL
00139 import platform
00140 loader = LibraryLoader(CDLL)
00141 ext = platform.uname()[0] == "Darwin" and "dylib" or "so"
00142 [loader.LoadLibrary("lib%s.%s" % (l, ext)) for l in self.libraries_]
00143 super(_Module,self).insertInto(parameterSet,myname)
00144
00145 class EDProducer(_Module):
00146 def __init__(self,type_,*arg,**kargs):
00147 super(EDProducer,self).__init__(type_,*arg,**kargs)
00148 def _placeImpl(self,name,proc):
00149 proc._placeProducer(name,self)
00150
00151
00152 class EDFilter(_Module):
00153 def __init__(self,type_,*arg,**kargs):
00154 super(EDFilter,self).__init__(type_,*arg,**kargs)
00155 def _placeImpl(self,name,proc):
00156 proc._placeFilter(name,self)
00157
00158
00159 class EDAnalyzer(_Module):
00160 def __init__(self,type_,*arg,**kargs):
00161 super(EDAnalyzer,self).__init__(type_,*arg,**kargs)
00162 def _placeImpl(self,name,proc):
00163 proc._placeAnalyzer(name,self)
00164
00165
00166 class OutputModule(_Module):
00167 def __init__(self,type_,*arg,**kargs):
00168 super(OutputModule,self).__init__(type_,*arg,**kargs)
00169 def _placeImpl(self,name,proc):
00170 proc._placeOutputModule(name,self)
00171
00172
00173 class Source(_ConfigureComponent,_TypedParameterizable):
00174 def __init__(self,type_,*arg,**kargs):
00175 super(Source,self).__init__(type_,*arg,**kargs)
00176 def _placeImpl(self,name,proc):
00177 proc._placeSource(name,self)
00178 def moduleLabel_(self,myname):
00179 return "@main_input"
00180 def nameInProcessDesc_(self,myname):
00181 return "@main_input"
00182
00183
00184 class Looper(_ConfigureComponent,_TypedParameterizable):
00185 def __init__(self,type_,*arg,**kargs):
00186 super(Looper,self).__init__(type_,*arg,**kargs)
00187 def _placeImpl(self,name,proc):
00188 proc._placeLooper(name,self)
00189 def moduleLabel_(self,myname):
00190 return "@main_looper"
00191 def nameInProcessDesc_(self, myname):
00192 return "@main_looper"
00193
00194
00195
00196 if __name__ == "__main__":
00197 import unittest
00198 from Types import *
00199 from SequenceTypes import *
00200 class TestModules(unittest.TestCase):
00201 def testEDAnalyzer(self):
00202 empty = EDAnalyzer("Empty")
00203 withParam = EDAnalyzer("Parameterized",foo=untracked(int32(1)), bar = untracked(string("it")))
00204 self.assertEqual(withParam.foo.value(), 1)
00205 self.assertEqual(withParam.bar.value(), "it")
00206 aCopy = withParam.copy()
00207 self.assertEqual(aCopy.foo.value(), 1)
00208 self.assertEqual(aCopy.bar.value(), "it")
00209 withType = EDAnalyzer("Test",type = int32(1))
00210 self.assertEqual(withType.type.value(),1)
00211 block = PSet(i = int32(9))
00212 m = EDProducer("DumbProducer", block, j = int32(10))
00213 self.assertEqual(9, m.i.value())
00214 self.assertEqual(10, m.j.value())
00215 def testESPrefer(self):
00216 juicer = ESPrefer("JuiceProducer")
00217 options = PrintOptions()
00218 options.isCfg = True
00219 self.assertEqual(juicer.dumpPythonAs("juicer", options), "process.prefer(\"JuiceProducer\")\n")
00220 options.isCfg = False
00221 self.assertEqual(juicer.dumpPythonAs("juicer", options), "es_prefer_JuiceProducer = cms.ESPrefer(\"JuiceProducer\")\n")
00222
00223 juicer = ESPrefer("JuiceProducer","juicer")
00224 options = PrintOptions()
00225 options.isCfg = True
00226 self.assertEqual(juicer.dumpPythonAs("juicer", options), 'process.prefer("juicer")\n')
00227 options.isCfg = False
00228 self.assertEqual(juicer.dumpPythonAs("juicer", options), 'es_prefer_juicer = cms.ESPrefer("JuiceProducer","juicer")\n')
00229 juicer = ESPrefer("JuiceProducer",fooRcd=vstring())
00230 self.assertEqual(juicer.dumpConfig(options),
00231 """JuiceProducer {
00232 vstring fooRcd = {
00233 }
00234
00235 }
00236 """)
00237 options = PrintOptions()
00238 options.isCfg = True
00239 self.assertEqual(juicer.dumpPythonAs("juicer"),
00240 """process.prefer("JuiceProducer",
00241 fooRcd = cms.vstring()
00242 )
00243 """)
00244 options.isCfg = False
00245 self.assertEqual(juicer.dumpPythonAs("juicer", options),
00246 """es_prefer_JuiceProducer = cms.ESPrefer("JuiceProducer",
00247 fooRcd = cms.vstring()
00248 )
00249 """)
00250
00251 def testService(self):
00252 empty = Service("Empty")
00253 withParam = Service("Parameterized",foo=untracked(int32(1)), bar = untracked(string("it")))
00254 self.assertEqual(withParam.foo.value(), 1)
00255 self.assertEqual(withParam.bar.value(), "it")
00256 self.assertEqual(empty.dumpPython(), "cms.Service(\"Empty\")\n")
00257 self.assertEqual(withParam.dumpPython(), "cms.Service(\"Parameterized\",\n foo = cms.untracked.int32(1),\n bar = cms.untracked.string(\'it\')\n)\n")
00258 def testSequences(self):
00259 m = EDProducer("MProducer")
00260 n = EDProducer("NProducer")
00261 m.setLabel("m")
00262 n.setLabel("n")
00263 s1 = Sequence(m*n)
00264 options = PrintOptions()
00265
00266
00267
00268 unittest.main()