CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
RandomServiceHelper.RandomNumberServiceHelper Class Reference
Inheritance diagram for RandomServiceHelper.RandomNumberServiceHelper:

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.

Author:   Dave Evans
Modified: Eric Vaandering

Definition at line 9 of file RandomServiceHelper.py.

Constructor & Destructor Documentation

def RandomServiceHelper.RandomNumberServiceHelper.__init__ (   self,
  randService 
)

Member Function Documentation

def RandomServiceHelper.RandomNumberServiceHelper.__containsSeed (   self,
  psetInstance 
)
private
_keeper_

True/False if the psetInstance has seeds in it

Definition at line 26 of file RandomServiceHelper.py.

Referenced by RandomServiceHelper.RandomNumberServiceHelper.__psetsWithSeeds().

26 
27  def __containsSeed(self,psetInstance):
28  """
29  _keeper_
30 
31  True/False if the psetInstance has seeds in it
32 
33  """
34  if psetInstance is None:
35  return False
36  if not isinstance(psetInstance,CfgTypes.PSet):
37  return False
38  seedList = getattr(psetInstance, "initialSeedSet", None)
39  if seedList != None:
40  return True
41  seedVal = getattr(psetInstance, "initialSeed", None)
42  if seedVal != None:
43  return True
44  return False
45 
def RandomServiceHelper.RandomNumberServiceHelper.__psetsWithSeeds (   self)
private
_psetsWithSeeds_

*private method*

return the list of PSet instances with seeds in them

Definition at line 46 of file RandomServiceHelper.py.

References RandomServiceHelper.RandomNumberServiceHelper.__containsSeed(), RandomServiceHelper.RandomNumberServiceHelper._lockedSeeds, RandomServiceHelper.RandomNumberServiceHelper._randService, and alcazmumu_cfi.filter.

Referenced by RandomServiceHelper.RandomNumberServiceHelper.countSeeds(), and RandomServiceHelper.RandomNumberServiceHelper.insertSeeds().

46 
47  def __psetsWithSeeds(self):
48  """
49  _psetsWithSeeds_
50 
51  *private method*
52 
53  return the list of PSet instances with seeds in them
54 
55  """
56  svcAttrs = [getattr(self._randService, item, None)
57  for item in self._randService.parameters_()
58  if item not in self._lockedSeeds]
59 
60  #print svcAttrs
61 
62  return list(filter(self.__containsSeed, svcAttrs))
63 
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 64 of file RandomServiceHelper.py.

References RandomServiceHelper.RandomNumberServiceHelper.__psetsWithSeeds().

Referenced by RandomServiceHelper.RandomNumberServiceHelper.insertSeeds(), RandomServiceHelper.RandomNumberServiceHelper.populate(), and RandomServiceHelper.RandomNumberServiceHelper.resetSeeds().

64 
65  def countSeeds(self):
66  """
67  _countSeeds_
68 
69  Count the number of seeds required by this service by
70  summing up the initialSeed and initialSeedSet entries
71  in all PSets in the service that contain those parameters.
72 
73  """
74  count = 0
75 
76  for itemRef in self.__psetsWithSeeds():
77  # //
78  # // PSet has list of seeds
79  #//
80  seedSet = getattr(itemRef, "initialSeedSet", None)
81  if seedSet != None:
82  count += len( seedSet.value())
83  continue
84  # //
85  # // PSet has single seed
86  #//
87  seedVal = getattr(itemRef, "initialSeed", None)
88  if seedVal != None:
89  count += 1
90 
91  # //
92  # // PSet has no recognisable seed, therfore do nothing
93  #// with it
94  return count
95 
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 138 of file RandomServiceHelper.py.

References RandomServiceHelper.RandomNumberServiceHelper._randService.

139  def getNamedSeed(self, psetName):
140  """
141  _getNamedSeed_
142 
143  This method returns the seeds in a PSet in this service. Returned
144 
145  - *psetName* : Name of the pset containing the seeds
146 
147  """
148  pset = getattr(self._randService, psetName, None)
149  if pset == None:
150  msg = "No PSet named %s belongs to this instance of the" % (
151  psetName,)
152  msg += "Random Seed Service"
153  raise RuntimeError(msg)
154 
155  seedVal = getattr(pset, "initialSeed", None)
156  if seedVal != None:
157  return [pset.initialSeed.value()]
158 
159  seedSet = getattr(pset, "initialSeedSet", None)
160  if seedSet != None:
161  return pset.initialSeedSet
162 
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 163 of file RandomServiceHelper.py.

References RandomServiceHelper.RandomNumberServiceHelper.__psetsWithSeeds(), and RandomServiceHelper.RandomNumberServiceHelper.countSeeds().

Referenced by RandomServiceHelper.RandomNumberServiceHelper.populate(), and RandomServiceHelper.RandomNumberServiceHelper.resetSeeds().

164  def insertSeeds(self, *seeds):
165  """
166  _insertSeeds_
167 
168  Given some list of specific seeds, insert them into the
169  service.
170 
171  Length of seed list is required to be same as the seed count for
172  the service.
173 
174  Usage: WM Tools.
175 
176  """
177  seeds = list(seeds)
178  if len(seeds) < self.countSeeds():
179  msg = "Not enough seeds provided\n"
180  msg += "Service requires %s seeds, only %s provided\n"
181  msg += "to RandomeService.insertSeeds method\n"
182  raise RuntimeError(msg)
183 
184  for item in self.__psetsWithSeeds():
185  seedSet = getattr(item, "initialSeedSet", None)
186  if seedSet != None:
187  numSeeds = len(seedSet.value())
188  useSeeds = seeds[:numSeeds]
189  seeds = seeds[numSeeds:]
190  item.initialSeedSet = CfgTypes.untracked(
191  CfgTypes.vuint32(*useSeeds))
192  continue
193  useSeed = seeds[0]
194  seeds = seeds[1:]
195  item.initialSeed = CfgTypes.untracked(
196  CfgTypes.uint32(useSeed)
197  )
198  continue
199  return
200 
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 201 of file RandomServiceHelper.py.

References RandomServiceHelper.RandomNumberServiceHelper._lockedSeeds, RandomServiceHelper.RandomNumberServiceHelper.countSeeds(), RandomServiceHelper.RandomNumberServiceHelper.insertSeeds(), and sistrip::SpyUtilities.range().

202  def populate(self, *excludePSets):
203  """
204  _populate_
205 
206  generate a bunch of seeds and stick them into this service
207  This is the lazy user method.
208 
209  Optional args are names of PSets to *NOT* alter seeds.
210 
211  Eg:
212  populate() will set all seeds
213  populate("pset1", "pset2") will set all seeds but not those in
214  psets named pset1 and pset2
215 
216  """
217 
218  import random
219  from random import SystemRandom
220  _inst = SystemRandom()
221  _MAXINT = 900000000
222 
223  # //
224  # // count seeds and create the required number of seeds
225  #//
226  newSeeds = [ _inst.randint(1, _MAXINT)
227  for i in range(self.countSeeds())]
228 
229 
230  self._lockedSeeds = list(excludePSets)
231  self.insertSeeds(*newSeeds)
232  self._lockedSeeds = []
233  return
234 
const uint16_t range(const Frame &aFrame)
def RandomServiceHelper.RandomNumberServiceHelper.resetSeeds (   self,
  value 
)
_resetSeeds_

reset all seeds to given value

Definition at line 235 of file RandomServiceHelper.py.

References RandomServiceHelper.RandomNumberServiceHelper.countSeeds(), RandomServiceHelper.RandomNumberServiceHelper.insertSeeds(), and sistrip::SpyUtilities.range().

236  def resetSeeds(self, value):
237  """
238  _resetSeeds_
239 
240  reset all seeds to given value
241 
242  """
243  newSeeds = [ value for i in range(self.countSeeds())]
244  self.insertSeeds(*newSeeds)
245  return
246 
247 
const uint16_t range(const Frame &aFrame)
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 96 of file RandomServiceHelper.py.

References RandomServiceHelper.RandomNumberServiceHelper._randService.

96 
97  def setNamedSeed(self, psetName, *seeds):
98  """
99  _setNamedSeed_
100 
101  If a specific set of seeds is needed for a PSet in this
102  service, they can be set by name using this method.
103 
104  - *psetName* : Name of the pset containing the seeds
105 
106  - *seeds* : list of seeds to be added, should be a single seed
107  for initialSeed values.
108 
109  """
110  pset = getattr(self._randService, psetName, None)
111  if pset == None:
112  msg = "No PSet named %s belongs to this instance of the" % (
113  psetName,)
114  msg += "Random Seed Service"
115  raise RuntimeError(msg)
116 
117  seedVal = getattr(pset, "initialSeed", None)
118  if seedVal != None:
119  pset.initialSeed = CfgTypes.untracked(
120  CfgTypes.uint32(seeds[0])
121  )
122 
123  return
124  seedSet = getattr(pset, "initialSeedSet", None)
125  if seedSet != None:
126  # //
127  # // Do we want to check the number of seeds??
128  #//
129  #if len(seeds) != len( seedSet.value()): pass
130  pset.initialSeedSet = CfgTypes.untracked(
131  CfgTypes.vuint32(*seeds))
132  return
133  # //
134  # // No seeds for that PSet
135  #// Error throw?
136  return
137 

Member Data Documentation

RandomServiceHelper.RandomNumberServiceHelper._lockedSeeds
private

Definition at line 23 of file RandomServiceHelper.py.

Referenced by RandomServiceHelper.RandomNumberServiceHelper.__psetsWithSeeds(), and RandomServiceHelper.RandomNumberServiceHelper.populate().

RandomServiceHelper.RandomNumberServiceHelper._randService
private

Definition at line 22 of file RandomServiceHelper.py.

Referenced by RandomServiceHelper.RandomNumberServiceHelper.__psetsWithSeeds(), RandomServiceHelper.RandomNumberServiceHelper.getNamedSeed(), and RandomServiceHelper.RandomNumberServiceHelper.setNamedSeed().