Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RandFlat.h
Go to the documentation of this file.
1 // $Id:$
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // HEP Random
6 // --- RandFlat ---
7 // class header file
8 // -----------------------------------------------------------------------
9 // This file is part of Geant4 (simulation toolkit for HEP).
10 
11 // Class defining methods for shooting flat random numbers, double or
12 // integers.
13 // It provides methods to fill with double flat values arrays of
14 // specified size, as well as methods for shooting sequences of 0,1 (bits).
15 // Default boundaries ]0.1[ for operator()().
16 
17 // =======================================================================
18 // Gabriele Cosmo - Created: 5th September 1995
19 // Peter Urban - ShootBit() and related stuff added: 5th Sep 1996
20 // Gabriele Cosmo - Added operator() and additional methods to fill
21 // arrays specifying boundaries: 24th Jul 1997
22 // J.Marraffino - Added default arguments as attributes and
23 // operator() with arguments: 16th Feb 1998
24 // M. Fischler - Moved copy constructor to protected so that
25 // derived RandBit can get at it.
26 // M Fischler - put and get to/from streams 12/10/04
27 // =======================================================================
28 
29 #ifndef RandFlat_h
30 #define RandFlat_h 1
31 
32 #include "CLHEP/Random/Random.h"
33 #include "CLHEP/Utility/memory.h"
34 
35 namespace CLHEP {
36 
41 class RandFlat : public HepRandom {
42 
43 public:
44 
45  inline RandFlat ( HepRandomEngine& anEngine );
46  inline RandFlat ( HepRandomEngine& anEngine, double width );
47  inline RandFlat ( HepRandomEngine& anEngine, double a, double b );
48  inline RandFlat ( HepRandomEngine* anEngine );
49  inline RandFlat ( HepRandomEngine* anEngine, double width );
50  inline RandFlat ( HepRandomEngine* anEngine, double a, double b );
51  // These constructors should be used to instantiate a RandFlat
52  // distribution object defining a local engine for it.
53  // The static generator will be skipped using the non-static methods
54  // defined below.
55  // If the engine is passed by pointer the corresponding engine object
56  // will be deleted by the RandFlat destructor.
57  // If the engine is passed by reference the corresponding engine object
58  // will not be deleted by the RandFlat destructor.
59 
60  virtual ~RandFlat();
61  // Destructor
62 
63  // Static methods to shoot random values using the static generator
64 
65  static double shoot();
66 
67  static inline double shoot( double width );
68 
69  static inline double shoot( double a, double b );
70 
71  static inline long shootInt( long n );
72 
73  static inline long shootInt( long m, long n );
74 
75  static inline int shootBit();
76 
77  static void shootArray ( const int size, double* vect );
78 
79  static void shootArray ( const int size, double* vect,
80  double lx, double dx );
81 
82  // Static methods to shoot random values using a given engine
83  // by-passing the static generator.
84 
85  static inline double shoot ( HepRandomEngine* anEngine );
86 
87  static inline double shoot( HepRandomEngine* anEngine, double width );
88 
89  static inline double shoot( HepRandomEngine* anEngine,
90  double a, double b );
91  static inline long shootInt( HepRandomEngine* anEngine, long n );
92 
93  static inline long shootInt( HepRandomEngine* anEngine, long m, long n );
94 
95  static inline int shootBit( HepRandomEngine* );
96 
97  static inline void shootArray ( HepRandomEngine* anEngine,
98  const int size, double* vect );
99 
100  static void shootArray ( HepRandomEngine* anEngine,
101  const int size, double* vect,
102  double lx, double dx );
103 
104  // Methods using the localEngine to shoot random values, by-passing
105  // the static generator.
106 
107  inline double fire();
108 
109  inline double fire( double width );
110 
111  inline double fire( double a, double b );
112 
113  inline long fireInt( long n );
114 
115  inline long fireInt( long m, long n );
116 
117  inline int fireBit();
118 
119  void fireArray (const int size, double* vect);
120 
121  void fireArray (const int size, double* vect,
122  double lx, double dx);
123 
124  double operator()();
125  double operator()( double width );
126  double operator()( double a, double b );
127 
128  // Save and restore to/from streams
129 
130  std::ostream & put ( std::ostream & os ) const;
131  std::istream & get ( std::istream & is );
132 
133  std::string name() const;
135 
136  static std::string distributionName() {return "RandFlat";}
137  // Provides the name of this distribution class
138 
139  // Methods overriding the base class static saveEngineStatus ones,
140  // by adding extra data so that save in one program, then further shootBit()s
141  // will produce the identical sequence to restore in another program, then
142  // generating shootBit() randoms there
143 
144  static void saveEngineStatus( const char filename[] = "Config.conf" );
145  // Saves to file the current status of the current engine.
146 
147  static void restoreEngineStatus( const char filename[] = "Config.conf" );
148  // Restores a saved status (if any) for the current engine.
149 
150  static std::ostream& saveFullState ( std::ostream & os );
151  // Saves to stream the state of the engine and cached data.
152 
153  static std::istream& restoreFullState ( std::istream & is );
154  // Restores from stream the state of the engine and cached data.
155 
156  static std::ostream& saveDistState ( std::ostream & os );
157  // Saves to stream the state of the cached data.
158 
159  static std::istream& restoreDistState ( std::istream & is );
160  // Restores from stream the state of the cached data.
161 
162 
163 protected:
164 
165 #if 0
166  // Protected copy constructor. Defining it here disallows use by users.
167  RandFlat(const RandFlat& d);
168 #endif // 0
169 
170 private:
171 
172  // ShootBits generates an integer random number,
173  // which is used by fireBit().
174  // The number is stored in randomInt and firstUnusedBit
175 
176  inline void fireBits();
177  static inline void shootBits();
178  static inline void shootBits(HepRandomEngine*);
179 
180  // In MSB, the most significant bit of the integer random number
181  // generated by ShootBits() is set.
182  // Note:
183  // the number of significant bits must be chosen so that
184  // - an unsigned long can hold it
185  // - and it should be less than the number of bits returned
186  // by Shoot() which are not affected by precision problems
187  // on _each_ architecture.
188  // (Aim: the random generators should be machine-independent).
189 
190  static const unsigned long MSB;
191  static const int MSBBits;
192  // These two are set up in RandFlat.cc and need not be saved/restored
193 
194  unsigned long randomInt;
195  unsigned long firstUnusedBit;
196  static unsigned long staticRandomInt;
197  static unsigned long staticFirstUnusedBit;
198 
199  shared_ptr<HepRandomEngine> localEngine;
200  double defaultWidth;
201  double defaultA;
202  double defaultB;
203 
204 };
205 
206 } // namespace CLHEP
207 
208 #include "CLHEP/Random/RandFlat.icc"
209 
210 #endif