CLHEP 2.0.4.7 Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: RandFlat.h,v 1.3.4.1 2005/03/18 22:26:48 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 00035 namespace CLHEP { 00036 00041 class RandFlat : public HepRandom { 00042 00043 public: 00044 00045 inline RandFlat ( HepRandomEngine& anEngine ); 00046 inline RandFlat ( HepRandomEngine& anEngine, double width ); 00047 inline RandFlat ( HepRandomEngine& anEngine, double a, double b ); 00048 inline RandFlat ( HepRandomEngine* anEngine ); 00049 inline RandFlat ( HepRandomEngine* anEngine, double width ); 00050 inline RandFlat ( HepRandomEngine* anEngine, double a, double b ); 00051 // These constructors should be used to instantiate a RandFlat 00052 // distribution object defining a local engine for it. 00053 // The static generator will be skipped using the non-static methods 00054 // defined below. 00055 // If the engine is passed by pointer the corresponding engine object 00056 // will be deleted by the RandFlat destructor. 00057 // If the engine is passed by reference the corresponding engine object 00058 // will not be deleted by the RandFlat destructor. 00059 00060 virtual ~RandFlat(); 00061 // Destructor 00062 00063 // Static methods to shoot random values using the static generator 00064 00065 static double shoot(); 00066 00067 static inline double shoot( double width ); 00068 00069 static inline double shoot( double a, double b ); 00070 00071 static inline long shootInt( long n ); 00072 00073 static inline long shootInt( long m, long n ); 00074 00075 static inline int shootBit(); 00076 00077 static void shootArray ( const int size, double* vect ); 00078 00079 static void shootArray ( const int size, double* vect, 00080 double lx, double dx ); 00081 00082 // Static methods to shoot random values using a given engine 00083 // by-passing the static generator. 00084 00085 static inline double shoot ( HepRandomEngine* anEngine ); 00086 00087 static inline double shoot( HepRandomEngine* anEngine, double width ); 00088 00089 static inline double shoot( HepRandomEngine* anEngine, 00090 double a, double b ); 00091 static inline long shootInt( HepRandomEngine* anEngine, long n ); 00092 00093 static inline long shootInt( HepRandomEngine* anEngine, long m, long n ); 00094 00095 static inline int shootBit( HepRandomEngine* ); 00096 00097 static inline void shootArray ( HepRandomEngine* anEngine, 00098 const int size, double* vect ); 00099 00100 static void shootArray ( HepRandomEngine* anEngine, 00101 const int size, double* vect, 00102 double lx, double dx ); 00103 00104 // Methods using the localEngine to shoot random values, by-passing 00105 // the static generator. 00106 00107 inline double fire(); 00108 00109 inline double fire( double width ); 00110 00111 inline double fire( double a, double b ); 00112 00113 inline long fireInt( long n ); 00114 00115 inline long fireInt( long m, long n ); 00116 00117 inline int fireBit(); 00118 00119 void fireArray (const int size, double* vect); 00120 00121 void fireArray (const int size, double* vect, 00122 double lx, double dx); 00123 00124 double operator()(); 00125 double operator()( double width ); 00126 double operator()( double a, double b ); 00127 00128 // Save and restore to/from streams 00129 00130 std::ostream & put ( std::ostream & os ) const; 00131 std::istream & get ( std::istream & is ); 00132 00133 std::string name() const; 00134 HepRandomEngine & engine(); 00135 00136 static std::string distributionName() {return "RandFlat";} 00137 // Provides the name of this distribution class 00138 00139 // Methods overriding the base class static saveEngineStatus ones, 00140 // by adding extra data so that save in one program, then further shootBit()s 00141 // will produce the identical sequence to restore in another program, then 00142 // generating shootBit() randoms there 00143 00144 static void saveEngineStatus( const char filename[] = "Config.conf" ); 00145 // Saves to file the current status of the current engine. 00146 00147 static void restoreEngineStatus( const char filename[] = "Config.conf" ); 00148 // Restores a saved status (if any) for the current engine. 00149 00150 static std::ostream& saveFullState ( std::ostream & os ); 00151 // Saves to stream the state of the engine and cached data. 00152 00153 static std::istream& restoreFullState ( std::istream & is ); 00154 // Restores from stream the state of the engine and cached data. 00155 00156 static std::ostream& saveDistState ( std::ostream & os ); 00157 // Saves to stream the state of the cached data. 00158 00159 static std::istream& restoreDistState ( std::istream & is ); 00160 // Restores from stream the state of the cached data. 00161 00162 00163 protected: 00164 00165 // Protected copy constructor. Defining it here disallows use by users. 00166 RandFlat(const RandFlat& d); 00167 00168 private: 00169 00170 // ShootBits generates an integer random number, 00171 // which is used by fireBit(). 00172 // The number is stored in randomInt and firstUnusedBit 00173 00174 inline void fireBits(); 00175 static inline void shootBits(); 00176 static inline void shootBits(HepRandomEngine*); 00177 00178 // In MSB, the most significant bit of the integer random number 00179 // generated by ShootBits() is set. 00180 // Note: 00181 // the number of significant bits must be chosen so that 00182 // - an unsigned long can hold it 00183 // - and it should be less than the number of bits returned 00184 // by Shoot() which are not affected by precision problems 00185 // on _each_ architecture. 00186 // (Aim: the random generators should be machine-independent). 00187 00188 static const unsigned long MSB; 00189 static const int MSBBits; 00190 // These two are set up in RandFlat.cc and need not be saved/restored 00191 00192 unsigned long randomInt; 00193 unsigned long firstUnusedBit; 00194 static unsigned long staticRandomInt; 00195 static unsigned long staticFirstUnusedBit; 00196 00197 HepRandomEngine* localEngine; 00198 bool deleteEngine; 00199 double defaultWidth; 00200 double defaultA; 00201 double defaultB; 00202 00203 }; 00204 00205 } // namespace CLHEP 00206 00207 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 00208 // backwards compatibility will be enabled ONLY in CLHEP 1.9 00209 using namespace CLHEP; 00210 #endif 00211 00212 #include "CLHEP/Random/RandFlat.icc" 00213 00214 #endif