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