CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RandomServiceHelper.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 from FWCore.ParameterSet.Config import Service
5 import FWCore.ParameterSet.Types as CfgTypes
6 
7 
9  """
10  _RandomNumberServiceHelper_
11 
12  Helper class to hold and handle the Random number generator service.
13 
14  Provide both user level and WM APIs.
15 
16  Revision: "$Id: RandomServiceHelper.py,v 1.6 2012/10/30 11:18:57 fabiocos Exp $"
17  Version "$Revision: 1.6 $"
18  Author: Dave Evans
19  Modified: Eric Vaandering
20  """
21 
22  def __init__(self,randService):
23  self._randService = randService
24  self._lockedSeeds = []
25 
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 
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 filter(self.__containsSeed, svcAttrs)
63 
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 
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 
138 
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 
163 
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 
201 
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 
235 
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 
248 
249 if __name__ == '__main__':
250  # //
251  # // Setup a test service and populate it
252  #//
253  randSvc = Service("RandomNumberGeneratorService")
254  randHelper = RandomNumberServiceHelper(randSvc)
255 
256  randSvc.i1 = CfgTypes.untracked(CfgTypes.uint32(1))
257  randSvc.t1 = CfgTypes.PSet()
258  randSvc.t2 = CfgTypes.PSet()
259  randSvc.t3 = CfgTypes.PSet()
260 
261  randSvc.t1.initialSeed = CfgTypes.untracked(
262  CfgTypes.uint32(123455678)
263  )
264 
265  randSvc.t2.initialSeedSet = CfgTypes.untracked(
266  CfgTypes.vuint32(12345,234567,345677)
267  )
268 
269 
270  randSvc.t3.initialSeed = CfgTypes.untracked(
271  CfgTypes.uint32(987654321)
272  )
273 
274  print "Inital PSet"
275  print randSvc
276 
277 
278  # //
279  # // Autofill seeds
280  #//
281  print "Totally Random PSet"
282  randHelper.populate()
283  print randSvc
284 
285 
286  # //
287  # // Set all seeds with reset method
288  #//
289  print "All seeds 9999"
290  randHelper.resetSeeds(9999)
291  print randSvc
292 
293  # //
294  # // test setting named seeds
295  #//
296  print "t1,t3 9998"
297  randHelper.setNamedSeed("t1", 9998)
298  randHelper.setNamedSeed("t3", 9998, 9998)
299  print randSvc
300 
301  print "t1 seed(s)",randHelper.getNamedSeed("t1")
302  print "t2 seed(s)",randHelper.getNamedSeed("t2")
303 
304 
305  # //
306  # // Autofill seeds with exclusion list
307  #//
308  randHelper.populate("t1", "t3")
309  print "t2 randomized"
310  print randSvc
list object
Definition: dbtoconf.py:77
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run