StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Random.h
1 /***************************************************************************
2  *
3  * $Id: Random.h,v 1.2 1999/12/07 23:43:04 ullrich Exp $
4  *
5  * Author: Gabriele Cosmo - Created: 5th Sep 1995
6  * modified SCL bl
7  ***************************************************************************
8  *
9  * Description:
10  * Random.h,v 1.7 1997/10/16 20:40:24
11  * -----------------------------------------------------------------------
12  * HEP Random
13  * --- HepRandom ---
14  * class header file
15  * -----------------------------------------------------------------------
16  * This file is part of Geant4 (simulation toolkit for HEP).
17  *
18  * An object of this class is instantiated by default within the HEP Random
19  * module and uses an instantiated HepJamesRandom engine as default algorithm
20  * for pseudo-random number generation. HepRandom defines a static private
21  * data member theGenerator and a set of static inlined methods to manipulate
22  * it. By means of theGenerator the user can change the underlying engine
23  * algorithm, get and set the seeds and use any kind of defined random
24  * distribution.
25  * Distribution classes inherit from HepRandom and define both static and
26  * not-static interfaces.
27  *
28  ***************************************************************************
29  *
30  * $Log: Random.h,v $
31  * Revision 1.2 1999/12/07 23:43:04 ullrich
32  * Modified to get rid of warnings on Linux.
33  *
34  * Revision 1.1 1999/01/30 03:59:00 fisyak
35  * Root Version of StarClassLibrary
36  *
37  * Revision 1.1 1999/01/23 00:27:41 ullrich
38  * Initial Revision
39  *
40  **************************************************************************/
41 #ifndef HepRandom_h
42 #define HepRandom_h 1
43 
44 #include <vector>
45 
46 #include "RandomEngine.h"
47 
48 class HepRandom {
49 
50 public:
51 
52  HepRandom();
53  HepRandom(long seed);
54  // Contructors with and without a seed using the default engine
55  // (JamesRandom).
56 
57  HepRandom(HepRandomEngine & algorithm);
58  HepRandom(HepRandomEngine * algorithm);
59  // Constructor taking an alternative engine as argument. If a pointer is
60  // given the corresponding object will be deleted by the HepRandom
61  // destructor.
62 
63  virtual ~HepRandom();
64  // Destructor
65 
66  inline HepDouble flat();
67  // Returns the flat value ( interval ]0.1[ ).
68 
69  inline HepDouble flat (HepRandomEngine* theNewEngine);
70  // Returns a flat value, given a defined Random Engine.
71 
72  inline void flatArray(const HepInt size, HepDouble* vect);
73 
74 #ifndef ST_NO_TEMPLATE_DEF_ARGS
75  void flatArray(vector<HepDouble>&);
76 #else
77  void flatArray(vector<HepDouble, allocator<HepDouble> >&);
78 #endif
79  // Fills "vect" array of flat random values, given the size.
80 
81  inline void flatArray(HepRandomEngine* theNewEngine,
82  const HepInt size, HepDouble* vect);
83 #ifndef ST_NO_TEMPLATE_DEF_ARGS
84  inline void flatArray(HepRandomEngine* theNewEngine,
85  vector<HepDouble>& vec);
86 #else
87  inline void flatArray(HepRandomEngine* theNewEngine,
88  vector<HepDouble, allocator<HepDouble> >& vec);
89 #endif
90  // Fills "vect" array of flat random values, given the size
91  // and a defined Random Engine.
92 
93  virtual HepDouble operator()();
94  // To get a flat random number using the operator ().
95 
96  // --------------------------------------------------
97  // Static member functions using the static generator
98  // --------------------------------------------------
99 
100  static void setTheSeed(long seed, HepInt lux=3);
101  // The static definition for setSeed().
102 
103  static long getTheSeed();
104  // The static definition for getSeed().
105 
106  static void setTheSeeds(const long* seeds, HepInt aux=-1);
107  // The static definition for setSeeds().
108 
109  static const long* getTheSeeds();
110  // The static definition for getSeeds().
111 
112  static void getTheTableSeeds (long* seeds, HepInt index);
113  // The static definition for getTableSeeds().
114 
115  static HepRandom * getTheGenerator() { return theGenerator; }
116  // Return the current static generator.
117 
118  static void setTheEngine (HepRandomEngine* theNewEngine);
119  // The static definition for setEngine()
120 
121  static HepRandomEngine * getTheEngine();
122  // The static definition for getEngine().
123 
124  static void saveEngineStatus();
125  // The static definition for saveStatus().
126 
127  static void restoreEngineStatus();
128  // The static definition for restoreStatus().
129 
130  static void showEngineStatus();
131  // The static definition for showStatus().
132 
133  // ------------------------------------------------------------
134  // Utility methods to get/set attributes for Gauss distribution
135  // ------------------------------------------------------------
136 
137  void setFlag( HepBoolean val ){set = val;}
138 
139  HepBoolean getFlag() {return set;}
140 
141  void setVal( HepDouble nextVal ){nextGauss = nextVal;}
142 
143  HepDouble getVal() {return nextGauss;}
144 
145  // --------------------------------------------------------------
146  // Utility methods to get/set attributes for Poisson distribution
147  // --------------------------------------------------------------
148 
149  void setOldMean( HepDouble val ){oldm = val;}
150 
151  HepDouble getOldMean() {return oldm;}
152 
153  HepDouble getMaxMean() {return meanMax;}
154 
155  void setPStatus(HepDouble sq, HepDouble alxm, HepDouble g) {
156  status[0] = sq; status[1] = alxm; status[2] = g;
157  }
158 
159  HepDouble* getPStatus() {return status;}
160 
161 private: // -------- Methods ---------
162 
163  inline void setSeed(long seed, HepInt lux);
164  // (Re)Initializes the generator with a seed.
165 
166  inline long getSeed() const;
167  // Gets the current seed of the current generator.
168 
169  inline void setSeeds(const long* seeds, HepInt aux);
170  // (Re)Initializes the generator with a zero terminated list of seeds.
171 
172  inline const long* getSeeds () const;
173  // Gets the current array of seeds of the current generator.
174 
175  inline void getTableSeeds (long* seeds, HepInt index) const;
176  // Gets the array of seeds from the global seedTable at "index" position.
177 
178  void setEngine (HepRandomEngine* engine) { theEngine = engine; }
179  // To set the underlying algorithm object
180 
181  HepRandomEngine * getEngine() const { return theEngine; }
182  // Returns a pointer to the underlying algorithm object.
183 
184  void saveStatus() const;
185  // Saves to file the current status of the current engine.
186 
187  void restoreStatus();
188  // Restores a saved status (if any) for the current engine.
189 
190  void showStatus() const;
191  // Dumps the current engine status on screen.
192 
193 protected: // -------- Data members ---------
194 
195  HepRandomEngine * theEngine;
196  // The corresponding algorithm.
197 
198  HepBoolean deleteEngine;
199  // True if the engine should be deleted on destruction.
200 
201  HepBoolean set;
202 
203  HepDouble status[3], oldm;
204  HepDouble nextGauss;
205  // For Gaussian random numbers which are generated two at a time.
206 
207  const HepDouble meanMax;
208  // For Poisson, to re-initialize if mean is the same as the previous.
209 
210 private: // -------- Data members ---------
211 
212  static HepRandom * theGenerator;
213  // The common shared static generator
214 };
215 
216 inline void HepRandom::setSeed(long seed, HepInt lux) {
217  theEngine->setSeed(seed,lux);
218 }
219 
220 inline void HepRandom::setSeeds(const long* seeds, HepInt aux) {
221  theEngine->setSeeds(seeds,aux);
222 }
223 
224 inline long HepRandom::getSeed() const {
225  return theEngine->getSeed();
226 }
227 
228 inline const long* HepRandom::getSeeds() const {
229  return theEngine->getSeeds();
230 }
231 
232 inline void HepRandom::getTableSeeds(long* seeds, HepInt index) const {
233  theEngine->getTableSeeds(seeds,index);
234 }
235 
236 inline void HepRandom::saveStatus() const {
237  theEngine->saveStatus();
238 }
239 
240 inline void HepRandom::restoreStatus() {
241  theEngine->restoreStatus();
242 }
243 
244 inline void HepRandom::showStatus() const {
245  theEngine->showStatus();
246 }
247 
248 inline HepDouble HepRandom::flat() {
249  return theEngine->flat();
250 }
251 
252 inline void HepRandom::flatArray(const HepInt size, HepDouble* vect) {
253  theEngine->flatArray(size,vect);
254 }
255 
256 #ifdef ST_NO_TEMPLATE_DEF_ARGS
257 inline void HepRandom::flatArray(vector<HepDouble, allocator<HepDouble> >& vec)
258 #else
259 inline void HepRandom::flatArray(vector<HepDouble>& vec)
260 #endif
261 {
262  theEngine->flatArray(vec);
263 }
264 
265 inline HepDouble HepRandom::flat(HepRandomEngine* theNewEngine)
266 {
267  return theNewEngine->flat();
268 }
269 
270 inline void HepRandom::flatArray(HepRandomEngine* theNewEngine,
271  const HepInt size, HepDouble* vect)
272 {
273  theNewEngine->flatArray(size,vect);
274 }
275 
276 #ifdef ST_NO_TEMPLATE_DEF_ARGS
277 inline void HepRandom::flatArray(HepRandomEngine* theNewEngine,
278  vector<HepDouble, allocator<HepDouble> >& vec)
279 #else
280 inline void HepRandom::flatArray(HepRandomEngine* theNewEngine,
281  vector<HepDouble>& vec)
282 #endif
283 {
284  theNewEngine->flatArray(vec);
285 }
286 
287 #endif