CMS 3D CMS Logo

RandomServiceHelper::RandomNumberServiceHelper Class Reference

List of all members.

Public Member Functions

def __init__
def countSeeds
def getNamedSeed
def insertSeeds
def populate
def resetSeeds
def setNamedSeed

Private Member Functions

def __containsSeed
def __psetsWithSeeds

Private Attributes

 _lockedSeeds
 _randService


Detailed Description

_RandomNumberServiceHelper_

Helper class to hold and handle the Random number generator service.

Provide both user level and WM APIs.

Revision: "$Id: RandomServiceHelper.py,v 1.4 2008/06/10 19:49:49 ewv Exp $"
Version   "$Revision: 1.4 $"
Author:   Dave Evans
Modified: Eric Vaandering

Definition at line 8 of file RandomServiceHelper.py.


Member Function Documentation

def RandomServiceHelper::RandomNumberServiceHelper::__containsSeed (   self,
  psetInstance 
) [private]

_keeper_

True/False if the psetInstance has seeds in it

Definition at line 27 of file RandomServiceHelper.py.

00027                                          :
00028         """
00029         _keeper_
00030 
00031         True/False if the psetInstance has seeds in it
00032 
00033         """
00034         if psetInstance == None:
00035             return False
00036         if not isinstance(psetInstance,CfgTypes.PSet):
00037             return False
00038         seedList = getattr(psetInstance, "initialSeedSet", None)
00039         if seedList != None:
00040             return True
00041         seedVal = getattr(psetInstance, "initialSeed", None)
00042         if seedVal != None:
00043             return True
00044         return False
00045 
00046 
    def __psetsWithSeeds(self):

def RandomServiceHelper::RandomNumberServiceHelper::__init__ (   self,
  randService 
)

Definition at line 22 of file RandomServiceHelper.py.

00022                                   :
00023         self._randService = randService
00024         self._lockedSeeds = []
00025 
00026 
    def __containsSeed(self,psetInstance):

def RandomServiceHelper::RandomNumberServiceHelper::__psetsWithSeeds (   self  )  [private]

_psetsWithSeeds_

*private method*

return the list of PSet instances with seeds in them

Definition at line 47 of file RandomServiceHelper.py.

00047                               :
00048         """
00049         _psetsWithSeeds_
00050 
00051         *private method*
00052 
00053         return the list of PSet instances with seeds in them
00054 
00055         """
00056         svcAttrs = [getattr(self._randService, item, None)
00057                     for item in self._randService.parameters_()
00058                     if item not in self._lockedSeeds]
00059 
00060         #print svcAttrs
00061 
00062         return filter(self.__containsSeed, svcAttrs)
00063 
00064 
    def countSeeds(self):

def RandomServiceHelper::RandomNumberServiceHelper::countSeeds (   self  ) 

_countSeeds_

Count the number of seeds required by this service by
summing up the initialSeed and initialSeedSet entries
in all PSets in the service that contain those parameters.

Definition at line 65 of file RandomServiceHelper.py.

00065                         :
00066         """
00067         _countSeeds_
00068 
00069         Count the number of seeds required by this service by
00070         summing up the initialSeed and initialSeedSet entries
00071         in all PSets in the service that contain those parameters.
00072 
00073         """
00074         count = 0
00075 
00076         for itemRef in self.__psetsWithSeeds():
00077             #  //
00078             # // PSet has list of seeds
00079             #//
00080             seedSet = getattr(itemRef, "initialSeedSet", None)
00081             if seedSet != None:
00082                 count += len( seedSet.value())
00083                 continue
00084             #  //
00085             # // PSet has single seed
00086             #//
00087             seedVal =  getattr(itemRef, "initialSeed", None)
00088             if seedVal != None:
00089                 count += 1
00090 
00091             #  //
00092             # // PSet has no recognisable seed, therfore do nothing
00093             #//  with it
00094         return count
00095 
00096 
    def setNamedSeed(self, psetName, *seeds):

def RandomServiceHelper::RandomNumberServiceHelper::getNamedSeed (   self,
  psetName 
)

_getNamedSeed_

This method returns the seeds in a PSet in this service. Returned

- *psetName* : Name of the pset containing the seeds

Definition at line 139 of file RandomServiceHelper.py.

00139                                     :
00140         """
00141         _getNamedSeed_
00142 
00143         This method returns the seeds in a PSet in this service. Returned
00144 
00145         - *psetName* : Name of the pset containing the seeds
00146 
00147         """
00148         pset = getattr(self._randService, psetName, None)
00149         if pset == None:
00150             msg = "No PSet named %s belongs to this instance of the" % (
00151                 psetName,)
00152             msg += "Random Seed Service"
00153             raise RuntimeError, msg
00154 
00155         seedVal = getattr(pset, "initialSeed", None)
00156         if seedVal != None:
00157             return [pset.initialSeed.value()]
00158 
00159         seedSet = getattr(pset, "initialSeedSet", None)
00160         if seedSet != None:
00161             return pset.initialSeedSet
00162 
00163 
    def insertSeeds(self, *seeds):

def RandomServiceHelper::RandomNumberServiceHelper::insertSeeds (   self,
  seeds 
)

_insertSeeds_

Given some list of specific seeds, insert them into the
service.

Length of seed list is required to be same as the seed count for
the service.

Usage: WM Tools.

Definition at line 164 of file RandomServiceHelper.py.

00164                                  :
00165         """
00166         _insertSeeds_
00167 
00168         Given some list of specific seeds, insert them into the
00169         service.
00170 
00171         Length of seed list is required to be same as the seed count for
00172         the service.
00173 
00174         Usage: WM Tools.
00175 
00176         """
00177         seeds = list(seeds)
00178         if len(seeds) < self.countSeeds():
00179             msg = "Not enough seeds provided\n"
00180             msg += "Service requires %s seeds, only %s provided\n"
00181             msg += "to RandomeService.insertSeeds method\n"
00182             raise RuntimeError, msg
00183 
00184         for item in self.__psetsWithSeeds():
00185             seedSet = getattr(item, "initialSeedSet", None)
00186             if seedSet != None:
00187                 numSeeds = len(seedSet.value())
00188                 useSeeds = seeds[:numSeeds]
00189                 seeds = seeds[numSeeds:]
00190                 item.initialSeedSet = CfgTypes.untracked(
00191                     CfgTypes.vuint32(*useSeeds))
00192                 continue
00193             useSeed = seeds[0]
00194             seeds = seeds[1:]
00195             item.initialSeed = CfgTypes.untracked(
00196                 CfgTypes.uint32(useSeed)
00197                 )
00198             continue
00199         return
00200 
00201 
    def populate(self, *excludePSets):

def RandomServiceHelper::RandomNumberServiceHelper::populate (   self,
  excludePSets 
)

_populate_

generate a bunch of seeds and stick them into this service
This is the lazy user method.

Optional args are names of PSets to *NOT* alter seeds.

Eg:
populate() will set all seeds
populate("pset1", "pset2") will set all seeds but not those in
psets named pset1 and pset2

Definition at line 202 of file RandomServiceHelper.py.

00202                                      :
00203         """
00204         _populate_
00205 
00206         generate a bunch of seeds and stick them into this service
00207         This is the lazy user method.
00208 
00209         Optional args are names of PSets to *NOT* alter seeds.
00210 
00211         Eg:
00212         populate() will set all seeds
00213         populate("pset1", "pset2") will set all seeds but not those in
00214         psets named pset1 and pset2
00215 
00216         """
00217 
00218         import random
00219         from random import SystemRandom
00220         _inst = SystemRandom()
00221         _MAXINT = 900000000
00222 
00223         #  //
00224         # // count seeds and create the required number of seeds
00225         #//
00226         newSeeds = [ _inst.randint(1, _MAXINT)
00227                      for i in range(self.countSeeds())]
00228 
00229 
00230         self._lockedSeeds = list(excludePSets)
00231         self.insertSeeds(*newSeeds)
00232         self._lockedSeeds = []
00233         return
00234 
00235 
    def resetSeeds(self, value):

def RandomServiceHelper::RandomNumberServiceHelper::resetSeeds (   self,
  value 
)

_resetSeeds_

reset all seeds to given value

Definition at line 236 of file RandomServiceHelper.py.

00236                                :
00237         """
00238         _resetSeeds_
00239 
00240         reset all seeds to given value
00241 
00242         """
00243         newSeeds = [ value for i in range(self.countSeeds())]
00244         self.insertSeeds(*newSeeds)
00245         return
00246 
00247 
00248 
if __name__ == '__main__':

def RandomServiceHelper::RandomNumberServiceHelper::setNamedSeed (   self,
  psetName,
  seeds 
)

_setNamedSeed_

If a specific set of seeds is needed for a PSet in this
service, they can be set by name using this method.

- *psetName* : Name of the pset containing the seeds

- *seeds*    : list of seeds to be added, should be a single seed
for initialSeed values.

Definition at line 97 of file RandomServiceHelper.py.

00097                                             :
00098         """
00099         _setNamedSeed_
00100 
00101         If a specific set of seeds is needed for a PSet in this
00102         service, they can be set by name using this method.
00103 
00104         - *psetName* : Name of the pset containing the seeds
00105 
00106         - *seeds*    : list of seeds to be added, should be a single seed
00107         for initialSeed values.
00108 
00109         """
00110         pset = getattr(self._randService, psetName, None)
00111         if pset == None:
00112             msg = "No PSet named %s belongs to this instance of the" % (
00113                 psetName,)
00114             msg += "Random Seed Service"
00115             raise RuntimeError, msg
00116 
00117         seedVal = getattr(pset, "initialSeed", None)
00118         if seedVal != None:
00119             pset.initialSeed = CfgTypes.untracked(
00120                 CfgTypes.uint32(seeds[0])
00121                 )
00122 
00123             return
00124         seedSet = getattr(pset, "initialSeedSet", None)
00125         if seedSet != None:
00126             #  //
00127             # // Do we want to check the number of seeds??
00128             #//
00129             #if len(seeds) != len( seedSet.value()): pass
00130             pset.initialSeedSet = CfgTypes.untracked(
00131                 CfgTypes.vuint32(*seeds))
00132             return
00133         #  //
00134         # // No seeds for that PSet
00135         #//  Error throw?
00136         return
00137 
00138 
    def getNamedSeed(self, psetName):


Member Data Documentation

RandomServiceHelper::RandomNumberServiceHelper::_lockedSeeds [private]

Definition at line 24 of file RandomServiceHelper.py.

RandomServiceHelper::RandomNumberServiceHelper::_randService [private]

Definition at line 23 of file RandomServiceHelper.py.


The documentation for this class was generated from the following file:
Generated on Tue Jun 9 18:50:49 2009 for CMSSW by  doxygen 1.5.4