CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/FWCore/ParameterSet/python/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         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         # the C++ parser can give it a name like "label@prefer".  Get rid of that.
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            # do either type or label
00101            result += 'process.prefer("'+basename+'"'
00102            if self.parameterNames_():
00103                result += ",\n"+_Parameterizable.dumpPython(self,options)+options.indentation()
00104            result +=')\n'
00105        else:
00106            # use the base class Module
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          # return something like "EDAnalyzer("foo", ...)"
00130         typename = format_typename(self)
00131         return "%s('%s', ...)" %(typename, self.type_())
00132 
00133 class EDProducer(_Module):
00134     def __init__(self,type_,*arg,**kargs):
00135         super(EDProducer,self).__init__(type_,*arg,**kargs)
00136     def _placeImpl(self,name,proc):
00137         proc._placeProducer(name,self)
00138 
00139 
00140 class EDFilter(_Module):
00141     def __init__(self,type_,*arg,**kargs):
00142         super(EDFilter,self).__init__(type_,*arg,**kargs)
00143     def _placeImpl(self,name,proc):
00144         proc._placeFilter(name,self)
00145 
00146 
00147 class EDAnalyzer(_Module):
00148     def __init__(self,type_,*arg,**kargs):
00149         super(EDAnalyzer,self).__init__(type_,*arg,**kargs)
00150     def _placeImpl(self,name,proc):
00151         proc._placeAnalyzer(name,self)
00152 
00153 
00154 class OutputModule(_Module):
00155     def __init__(self,type_,*arg,**kargs):
00156         super(OutputModule,self).__init__(type_,*arg,**kargs)
00157     def _placeImpl(self,name,proc):
00158         proc._placeOutputModule(name,self)
00159 
00160 
00161 class Source(_ConfigureComponent,_TypedParameterizable):
00162     def __init__(self,type_,*arg,**kargs):
00163         super(Source,self).__init__(type_,*arg,**kargs)
00164     def _placeImpl(self,name,proc):
00165         proc._placeSource(name,self)
00166     def moduleLabel_(self,myname):
00167         return "@main_input"
00168     def nameInProcessDesc_(self,myname):
00169         return "@main_input"
00170 
00171 
00172 class Looper(_ConfigureComponent,_TypedParameterizable):
00173     def __init__(self,type_,*arg,**kargs):
00174         super(Looper,self).__init__(type_,*arg,**kargs)
00175     def _placeImpl(self,name,proc):
00176         proc._placeLooper(name,self)
00177     def moduleLabel_(self,myname):
00178         return "@main_looper"
00179     def nameInProcessDesc_(self, myname):
00180         return "@main_looper"
00181 
00182 
00183 
00184 if __name__ == "__main__":
00185     import unittest
00186     from Types import *
00187     from SequenceTypes import *
00188     class TestModules(unittest.TestCase):
00189         def testEDAnalyzer(self):
00190             empty = EDAnalyzer("Empty")
00191             withParam = EDAnalyzer("Parameterized",foo=untracked(int32(1)), bar = untracked(string("it")))
00192             self.assertEqual(withParam.foo.value(), 1)
00193             self.assertEqual(withParam.bar.value(), "it")
00194             aCopy = withParam.copy()
00195             self.assertEqual(aCopy.foo.value(), 1)
00196             self.assertEqual(aCopy.bar.value(), "it")
00197             withType = EDAnalyzer("Test",type = int32(1))
00198             self.assertEqual(withType.type.value(),1)
00199             block = PSet(i = int32(9))
00200             m = EDProducer("DumbProducer", block, j = int32(10))
00201             self.assertEqual(9, m.i.value())
00202             self.assertEqual(10, m.j.value())
00203         def testESPrefer(self):
00204             juicer = ESPrefer("JuiceProducer")
00205             options = PrintOptions()
00206             options.isCfg = True
00207             self.assertEqual(juicer.dumpPythonAs("juicer", options), "process.prefer(\"JuiceProducer\")\n")
00208             options.isCfg = False
00209             self.assertEqual(juicer.dumpPythonAs("juicer", options), "es_prefer_JuiceProducer = cms.ESPrefer(\"JuiceProducer\")\n")
00210 
00211             juicer = ESPrefer("JuiceProducer","juicer")
00212             options = PrintOptions()
00213             options.isCfg = True
00214             self.assertEqual(juicer.dumpPythonAs("juicer", options), 'process.prefer("juicer")\n')
00215             options.isCfg = False
00216             self.assertEqual(juicer.dumpPythonAs("juicer", options), 'es_prefer_juicer = cms.ESPrefer("JuiceProducer","juicer")\n')
00217             juicer = ESPrefer("JuiceProducer",fooRcd=vstring())
00218             self.assertEqual(juicer.dumpConfig(options),
00219 """JuiceProducer { 
00220     vstring fooRcd = {
00221     }
00222 
00223 }
00224 """)
00225             options = PrintOptions()
00226             options.isCfg = True
00227             self.assertEqual(juicer.dumpPythonAs("juicer"),
00228 """process.prefer("JuiceProducer",
00229     fooRcd = cms.vstring()
00230 )
00231 """)
00232             options.isCfg = False
00233             self.assertEqual(juicer.dumpPythonAs("juicer", options),
00234 """es_prefer_JuiceProducer = cms.ESPrefer("JuiceProducer",
00235     fooRcd = cms.vstring()
00236 )
00237 """)
00238         
00239         def testService(self):
00240             empty = Service("Empty")
00241             withParam = Service("Parameterized",foo=untracked(int32(1)), bar = untracked(string("it")))
00242             self.assertEqual(withParam.foo.value(), 1)
00243             self.assertEqual(withParam.bar.value(), "it")
00244             self.assertEqual(empty.dumpPython(), "cms.Service(\"Empty\")\n")
00245             self.assertEqual(withParam.dumpPython(), "cms.Service(\"Parameterized\",\n    foo = cms.untracked.int32(1),\n    bar = cms.untracked.string(\'it\')\n)\n")
00246         def testSequences(self):
00247             m = EDProducer("MProducer")
00248             n = EDProducer("NProducer")
00249             m.setLabel("m")
00250             n.setLabel("n")
00251             s1 = Sequence(m*n)
00252             options = PrintOptions()
00253 
00254 
00255 
00256     unittest.main()