Geant4 Cross Reference |
1 // -*- C++ -*- 2 // 3 // ----------------------------------------------------------------------- 4 // HEP Random 5 // --- RandFlat --- 6 // class header file 7 // ----------------------------------------------------------------------- 8 // This file is part of Geant4 (simulation toolkit for HEP). 9 10 // Class defining methods for shooting flat random numbers, double or 11 // integers. 12 // It provides methods to fill with double flat values arrays of 13 // specified size, as well as methods for shooting sequences of 0,1 (bits). 14 // Default boundaries ]0.1[ for operator()(). 15 16 // ======================================================================= 17 // Gabriele Cosmo - Created: 5th September 1995 18 // Peter Urban - ShootBit() and related stuff added: 5th Sep 1996 19 // Gabriele Cosmo - Added operator() and additional methods to fill 20 // arrays specifying boundaries: 24th Jul 1997 21 // J.Marraffino - Added default arguments as attributes and 22 // operator() with arguments: 16th Feb 1998 23 // M. Fischler - Moved copy constructor to protected so that 24 // derived RandBit can get at it. 25 // M Fischler - put and get to/from streams 12/10/04 26 // ======================================================================= 27 28 #ifndef RandFlat_h 29 #define RandFlat_h 1 30 31 #include "CLHEP/Random/Random.h" 32 #include "CLHEP/Utility/memory.h" 33 #include "CLHEP/Utility/thread_local.h" 34 35 namespace CLHEP { 36 37 /** 38 * @author <Gabriele.Cosmo@cern.ch> 39 * @ingroup random 40 */ 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 a1, 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 a1, 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 a1, 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; 134 HepRandomEngine & engine(); 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 CLHEP_THREAD_LOCAL unsigned long staticRandomInt; 197 static CLHEP_THREAD_LOCAL unsigned long staticFirstUnusedBit; 198 199 std::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 211