CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: RandFlat.h,v 1.5 2010/06/16 17:24:53 garren Exp $ 00002 // -*- C++ -*- 00003 // 00004 // ----------------------------------------------------------------------- 00005 // HEP Random 00006 // --- RandFlat --- 00007 // class header file 00008 // ----------------------------------------------------------------------- 00009 // This file is part of Geant4 (simulation toolkit for HEP). 00010 00011 // Class defining methods for shooting flat random numbers, double or 00012 // integers. 00013 // It provides methods to fill with double flat values arrays of 00014 // specified size, as well as methods for shooting sequences of 0,1 (bits). 00015 // Default boundaries ]0.1[ for operator()(). 00016 00017 // ======================================================================= 00018 // Gabriele Cosmo - Created: 5th September 1995 00019 // Peter Urban - ShootBit() and related stuff added: 5th Sep 1996 00020 // Gabriele Cosmo - Added operator() and additional methods to fill 00021 // arrays specifying boundaries: 24th Jul 1997 00022 // J.Marraffino - Added default arguments as attributes and 00023 // operator() with arguments: 16th Feb 1998 00024 // M. Fischler - Moved copy constructor to protected so that 00025 // derived RandBit can get at it. 00026 // M Fischler - put and get to/from streams 12/10/04 00027 // ======================================================================= 00028 00029 #ifndef RandFlat_h 00030 #define RandFlat_h 1 00031 00032 #include "CLHEP/Random/defs.h" 00033 #include "CLHEP/Random/Random.h" 00034 #include "CLHEP/Utility/memory.h" 00035 00036 namespace CLHEP { 00037 00042 class RandFlat : public HepRandom { 00043 00044 public: 00045 00046 inline RandFlat ( HepRandomEngine& anEngine ); 00047 inline RandFlat ( HepRandomEngine& anEngine, double width ); 00048 inline RandFlat ( HepRandomEngine& anEngine, double a, double b ); 00049 inline RandFlat ( HepRandomEngine* anEngine ); 00050 inline RandFlat ( HepRandomEngine* anEngine, double width ); 00051 inline RandFlat ( HepRandomEngine* anEngine, double a, double b ); 00052 // These constructors should be used to instantiate a RandFlat 00053 // distribution object defining a local engine for it. 00054 // The static generator will be skipped using the non-static methods 00055 // defined below. 00056 // If the engine is passed by pointer the corresponding engine object 00057 // will be deleted by the RandFlat destructor. 00058 // If the engine is passed by reference the corresponding engine object 00059 // will not be deleted by the RandFlat destructor. 00060 00061 virtual ~RandFlat(); 00062 // Destructor 00063 00064 // Static methods to shoot random values using the static generator 00065 00066 static double shoot(); 00067 00068 static inline double shoot( double width ); 00069 00070 static inline double shoot( double a, double b ); 00071 00072 static inline long shootInt( long n ); 00073 00074 static inline long shootInt( long a1, long n ); 00075 00076 static inline int shootBit(); 00077 00078 static void shootArray ( const int size, double* vect ); 00079 00080 static void shootArray ( const int size, double* vect, 00081 double lx, double dx ); 00082 00083 // Static methods to shoot random values using a given engine 00084 // by-passing the static generator. 00085 00086 static inline double shoot ( HepRandomEngine* anEngine ); 00087 00088 static inline double shoot( HepRandomEngine* anEngine, double width ); 00089 00090 static inline double shoot( HepRandomEngine* anEngine, 00091 double a, double b ); 00092 static inline long shootInt( HepRandomEngine* anEngine, long n ); 00093 00094 static inline long shootInt( HepRandomEngine* anEngine, long a1, long n ); 00095 00096 static inline int shootBit( HepRandomEngine* ); 00097 00098 static inline void shootArray ( HepRandomEngine* anEngine, 00099 const int size, double* vect ); 00100 00101 static void shootArray ( HepRandomEngine* anEngine, 00102 const int size, double* vect, 00103 double lx, double dx ); 00104 00105 // Methods using the localEngine to shoot random values, by-passing 00106 // the static generator. 00107 00108 inline double fire(); 00109 00110 inline double fire( double width ); 00111 00112 inline double fire( double a, double b ); 00113 00114 inline long fireInt( long n ); 00115 00116 inline long fireInt( long a1, long n ); 00117 00118 inline int fireBit(); 00119 00120 void fireArray (const int size, double* vect); 00121 00122 void fireArray (const int size, double* vect, 00123 double lx, double dx); 00124 00125 double operator()(); 00126 double operator()( double width ); 00127 double operator()( double a, double b ); 00128 00129 // Save and restore to/from streams 00130 00131 std::ostream & put ( std::ostream & os ) const; 00132 std::istream & get ( std::istream & is ); 00133 00134 std::string name() const; 00135 HepRandomEngine & engine(); 00136 00137 static std::string distributionName() {return "RandFlat";} 00138 // Provides the name of this distribution class 00139 00140 // Methods overriding the base class static saveEngineStatus ones, 00141 // by adding extra data so that save in one program, then further shootBit()s 00142 // will produce the identical sequence to restore in another program, then 00143 // generating shootBit() randoms there 00144 00145 static void saveEngineStatus( const char filename[] = "Config.conf" ); 00146 // Saves to file the current status of the current engine. 00147 00148 static void restoreEngineStatus( const char filename[] = "Config.conf" ); 00149 // Restores a saved status (if any) for the current engine. 00150 00151 static std::ostream& saveFullState ( std::ostream & os ); 00152 // Saves to stream the state of the engine and cached data. 00153 00154 static std::istream& restoreFullState ( std::istream & is ); 00155 // Restores from stream the state of the engine and cached data. 00156 00157 static std::ostream& saveDistState ( std::ostream & os ); 00158 // Saves to stream the state of the cached data. 00159 00160 static std::istream& restoreDistState ( std::istream & is ); 00161 // Restores from stream the state of the cached data. 00162 00163 00164 protected: 00165 00166 #if 0 00167 // Protected copy constructor. Defining it here disallows use by users. 00168 RandFlat(const RandFlat& d); 00169 #endif // 0 00170 00171 private: 00172 00173 // ShootBits generates an integer random number, 00174 // which is used by fireBit(). 00175 // The number is stored in randomInt and firstUnusedBit 00176 00177 inline void fireBits(); 00178 static inline void shootBits(); 00179 static inline void shootBits(HepRandomEngine*); 00180 00181 // In MSB, the most significant bit of the integer random number 00182 // generated by ShootBits() is set. 00183 // Note: 00184 // the number of significant bits must be chosen so that 00185 // - an unsigned long can hold it 00186 // - and it should be less than the number of bits returned 00187 // by Shoot() which are not affected by precision problems 00188 // on _each_ architecture. 00189 // (Aim: the random generators should be machine-independent). 00190 00191 static const unsigned long MSB; 00192 static const int MSBBits; 00193 // These two are set up in RandFlat.cc and need not be saved/restored 00194 00195 unsigned long randomInt; 00196 unsigned long firstUnusedBit; 00197 static unsigned long staticRandomInt; 00198 static unsigned long staticFirstUnusedBit; 00199 00200 shared_ptr<HepRandomEngine> localEngine; 00201 double defaultWidth; 00202 double defaultA; 00203 double defaultB; 00204 00205 }; 00206 00207 } // namespace CLHEP 00208 00209 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 00210 // backwards compatibility will be enabled ONLY in CLHEP 1.9 00211 using namespace CLHEP; 00212 #endif 00213 00214 #include "CLHEP/Random/RandFlat.icc" 00215 00216 #endif