1 // $Id: RandGauss.h,v 1.5 2010/06/16 17:24:53 garren Exp $ 2 // -*- C++ -*- 3 // 4 // ----------------------------------------------------------------------- 5 // HEP Random 6 // --- RandGauss --- 7 // class header file 8 // ----------------------------------------------------------------------- 9 // This file is part of Geant4 (simulation toolkit for HEP). 10 11 // Class defining methods for shooting gaussian distributed random values, 12 // given a mean (default=0) or specifying also a deviation (default=1). 13 // Gaussian random numbers are generated two at the time, so every 14 // other time shoot is called the number returned is the one generated the 15 // time before. 16 // Default values are used for operator()(). 17 18 // ======================================================================= 19 // Gabriele Cosmo - Created: 5th September 1995 20 // - Minor corrections: 31st October 1996 21 // - Added methods to shoot arrays: 28th July 1997 22 // J.Marraffino - Added default arguments as attributes and 23 // operator() with arguments. Introduced method normal() 24 // for computation in fire(): 16th Feb 1998 25 // Gabriele Cosmo - Relocated static data from HepRandom: 5th Jan 1999 26 // M Fischler - put and get to/from streams 12/8/04 27 // ======================================================================= 28 29 #ifndef RandGauss_h 30 #define RandGauss_h 1 31 32 #include "CLHEP/Random/defs.h" 33 #include "CLHEP/Random/Random.h" 34 #include "CLHEP/Utility/memory.h" 35 #include "CLHEP/Utility/thread_local.h" 36 37 namespace CLHEP { 38 39 /** 40 * @author 41 * @ingroup random 42 */ 43 class RandGauss : public HepRandom { 44 45 public: 46 47 inline RandGauss ( HepRandomEngine& anEngine, double mean=0.0, 48 double stdDev=1.0 ); 49 inline RandGauss ( HepRandomEngine* anEngine, double mean=0.0, 50 double stdDev=1.0 ); 51 // These constructors should be used to instantiate a RandGauss 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 RandGauss destructor. 57 // If the engine is passed by reference the corresponding engine object 58 // will not be deleted by the RandGauss destructor. 59 60 virtual ~RandGauss(); 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 mean, double stdDev ); 68 69 static void shootArray ( const int size, double* vect, 70 double mean=0.0, double stdDev=1.0 ); 71 72 // Static methods to shoot random values using a given engine 73 // by-passing the static generator. 74 75 static double shoot( HepRandomEngine* anEngine ); 76 77 static inline double shoot( HepRandomEngine* anEngine, 78 double mean, double stdDev ); 79 80 static void shootArray ( HepRandomEngine* anEngine, const int size, 81 double* vect, double mean=0.0, 82 double stdDev=1.0 ); 83 84 // Methods using the localEngine to shoot random values, by-passing 85 // the static generator. 86 87 double fire(); 88 89 inline double fire( double mean, double stdDev ); 90 91 void fireArray ( const int size, double* vect); 92 void fireArray ( const int size, double* vect, 93 double mean, double stdDev ); 94 95 virtual double operator()(); 96 virtual double operator()( double mean, double stdDev ); 97 98 std::string name() const; 99 HepRandomEngine & engine(); 100 distributionName()101 static std::string distributionName() {return "RandGauss";} 102 // Provides the name of this distribution class 103 104 // Save and restore to/from streams 105 106 std::ostream & put ( std::ostream & os ) const; 107 std::istream & get ( std::istream & is ); 108 109 // Methods setFlag(false) and setF(false) if invoked in the client 110 // code before shoot/fire will force generation of a new couple of 111 // values. 112 113 static bool getFlag(); 114 115 static void setFlag( bool val ); 116 getF()117 bool getF() const {return set;} 118 setF(bool val)119 void setF( bool val ) {set = val;} 120 121 // Methods overriding the base class static saveEngineStatus ones, 122 // by adding extra data so that save in one program, then further gaussians, 123 // will produce the identical sequence to restore in another program, then 124 // generating gaussian randoms there 125 126 static void saveEngineStatus( const char filename[] = "Config.conf" ); 127 // Saves to file the current status of the current engine. 128 129 static void restoreEngineStatus( const char filename[] = "Config.conf" ); 130 // Restores a saved status (if any) for the current engine. 131 132 static std::ostream& saveFullState ( std::ostream & os ); 133 // Saves to stream the state of the engine and cached data. 134 135 static std::istream& restoreFullState ( std::istream & is ); 136 // Restores from stream the state of the engine and cached data. 137 138 static std::ostream& saveDistState ( std::ostream & os ); 139 // Saves to stream the state of the cached data. 140 141 static std::istream& restoreDistState ( std::istream & is ); 142 // Restores from stream the state of the cached data. 143 144 145 protected: 146 147 static double getVal(); 148 149 static void setVal( double nextVal ); 150 151 double normal(); 152 153 double defaultMean; 154 double defaultStdDev; 155 156 std::shared_ptr<HepRandomEngine> localEngine; 157 158 private: 159 160 bool set; 161 double nextGauss; 162 163 // static data 164 static CLHEP_THREAD_LOCAL bool set_st; 165 static CLHEP_THREAD_LOCAL double nextGauss_st; 166 167 }; 168 169 } // namespace CLHEP 170 171 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 172 // backwards compatibility will be enabled ONLY in CLHEP 1.9 173 using namespace CLHEP; 174 #endif 175 176 #include "CLHEP/Random/RandGauss.icc" 177 178 #endif 179