CMS 3D CMS Logo

Modules.py

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

Generated on Tue Jun 9 17:36:27 2009 for CMSSW by  doxygen 1.5.4