Geant4 Cross Reference |
1 // -*- C++ -*- 1 // -*- C++ -*- 2 // 2 // 3 // ------------------------------------------- 3 // ----------------------------------------------------------------------- 4 // HEP Random 4 // HEP Random 5 // --- RandGeneral -- 5 // --- RandGeneral --- 6 // class header file 6 // class header file 7 // ------------------------------------------- 7 // ----------------------------------------------------------------------- 8 8 9 // Class defining methods for shooting general 9 // Class defining methods for shooting generally distributed random values, 10 // given a user-defined probability distributi 10 // given a user-defined probability distribution function. 11 11 12 // =========================================== 12 // ======================================================================= 13 // S.Magni & G.Pieri - Created: 29 April 1998 13 // S.Magni & G.Pieri - Created: 29 April 1998 14 // G.Cosmo - Added constructor usin 14 // G.Cosmo - Added constructor using default engine from the 15 // static generator: 20 A 15 // static generator: 20 Aug 1998 16 // S.Magni & G.Pieri - Added linear interpola 16 // S.Magni & G.Pieri - Added linear interpolation: 24 March 1999 17 // M. Fischler - Added private methods 17 // M. Fischler - Added private methods that simplify the implementaion 18 // prepareTables(), useFlatDistribution() 18 // prepareTables(), useFlatDistribution(), mapRandom() 19 // - Added private variable oneOverNb 19 // - Added private variable oneOverNbins. 20 // - Made the warning about shoot 20 // - Made the warning about shoot() not being static a tad 21 // more prominent. 14 May 1999 21 // more prominent. 14 May 1999 22 // M Fischler - put and get to/from st 22 // M Fischler - put and get to/from streams 12/15/04 23 // =========================================== 23 // ======================================================================= 24 24 25 #ifndef RandGeneral_h 25 #ifndef RandGeneral_h 26 #define RandGeneral_h 1 26 #define RandGeneral_h 1 27 27 28 #include "CLHEP/Random/Random.h" 28 #include "CLHEP/Random/Random.h" 29 #include "CLHEP/Utility/memory.h" 29 #include "CLHEP/Utility/memory.h" 30 #include <vector> 30 #include <vector> 31 31 32 namespace CLHEP { 32 namespace CLHEP { 33 33 34 /** 34 /** 35 * @author 35 * @author 36 * @ingroup random 36 * @ingroup random 37 */ 37 */ 38 class RandGeneral : public HepRandom { 38 class RandGeneral : public HepRandom { 39 39 40 public: 40 public: 41 41 42 RandGeneral ( const double* aProbFunc, 42 RandGeneral ( const double* aProbFunc, 43 int theProbSize, 43 int theProbSize, 44 int IntType=0 ); 44 int IntType=0 ); 45 RandGeneral ( HepRandomEngine& anEngine, 45 RandGeneral ( HepRandomEngine& anEngine, 46 const double* aProbFunc, 46 const double* aProbFunc, 47 int theProbSize, 47 int theProbSize, 48 int IntType=0 ); 48 int IntType=0 ); 49 RandGeneral ( HepRandomEngine* anEngine, 49 RandGeneral ( HepRandomEngine* anEngine, 50 const double* aProbFunc, 50 const double* aProbFunc, 51 int theProbSize, 51 int theProbSize, 52 int IntType=0 ); 52 int IntType=0 ); 53 // These constructors should be used to inst 53 // These constructors should be used to instantiate a RandGeneral 54 // distribution object defining a local engi 54 // distribution object defining a local engine for it. 55 // The static generator will be skipped by u 55 // The static generator will be skipped by using the non-static methods 56 // defined below. In case no engine is speci 56 // defined below. In case no engine is specified in the constructor, the 57 // default engine used by the static generat 57 // default engine used by the static generator is applied. 58 // If the engine is passed by pointer the co 58 // If the engine is passed by pointer the corresponding engine object 59 // will be deleted by the RandGeneral destru 59 // will be deleted by the RandGeneral destructor. 60 // If the engine is passed by reference the 60 // If the engine is passed by reference the corresponding engine object 61 // will not be deleted by the RandGeneral de 61 // will not be deleted by the RandGeneral destructor. 62 // The probability distribution function (Pd 62 // The probability distribution function (Pdf) must be provided by the user 63 // as an array of positive real number. The 63 // as an array of positive real number. The array size must also be 64 // provided. The Pdf doesn't need to be norm 64 // provided. The Pdf doesn't need to be normalized to 1. 65 // if IntType = 0 ( default value ) a unifor 65 // if IntType = 0 ( default value ) a uniform random number is 66 // generated using the engine. The uniform n 66 // generated using the engine. The uniform number is then transformed 67 // to the user's distribution using the cumu 67 // to the user's distribution using the cumulative probability 68 // distribution constructed from his histogr 68 // distribution constructed from his histogram. The cumulative 69 // distribution is inverted using a binary s 69 // distribution is inverted using a binary search for the nearest 70 // bin boundary and a linear interpolation w 70 // bin boundary and a linear interpolation within the 71 // bin. RandGeneral therefore generates a co 71 // bin. RandGeneral therefore generates a constant density within 72 // each bin. 72 // each bin. 73 // if IntType = 1 no interpolation is perfor 73 // if IntType = 1 no interpolation is performed and the result is a 74 // discrete distribution. 74 // discrete distribution. 75 75 76 virtual ~RandGeneral(); 76 virtual ~RandGeneral(); 77 // Destructor 77 // Destructor 78 78 79 // Methods to shoot random values using the 79 // Methods to shoot random values using the static generator 80 // N.B.: The methods are NOT static since th 80 // N.B.: The methods are NOT static since they use nonstatic members 81 // theIntegralPdf & nBins 81 // theIntegralPdf & nBins 82 82 83 ///////////////////// 83 ///////////////////// 84 // // 84 // // 85 // BIG RED WARNING // 85 // BIG RED WARNING // 86 // // 86 // // 87 ///////////////////// 87 ///////////////////// 88 // 88 // 89 // The above N.B. is telling users that the 89 // The above N.B. is telling users that the shoot() methods in this 90 // class are NOT STATIC. You cannot do 90 // class are NOT STATIC. You cannot do 91 // double x = RandGeneral::shoot(); 91 // double x = RandGeneral::shoot(); 92 // It would not make sense to provide a stat 92 // It would not make sense to provide a static shoot -- what would 93 // the default probability function look lik 93 // the default probability function look like? 94 94 95 inline double shoot(); 95 inline double shoot(); 96 96 97 inline void shootArray ( const int size, dou 97 inline void shootArray ( const int size, double* vect); 98 98 99 // Methods to shoot random values using a g 99 // Methods to shoot random values using a given engine 100 // by-passing the static generator. 100 // by-passing the static generator. 101 101 102 double shoot( HepRandomEngine* anEngine ); 102 double shoot( HepRandomEngine* anEngine ); 103 103 104 void shootArray ( HepRandomEngine* anEngine, 104 void shootArray ( HepRandomEngine* anEngine, const int size, 105 double* vect ); 105 double* vect ); 106 106 107 // Methods using the localEngine to shoot r 107 // Methods using the localEngine to shoot random values, by-passing 108 // the static generator. 108 // the static generator. 109 109 110 double fire(); 110 double fire(); 111 111 112 void fireArray ( const int size, double* vec 112 void fireArray ( const int size, double* vect); 113 113 114 double operator()(); 114 double operator()(); 115 115 116 // Save and restore to/from streams 116 // Save and restore to/from streams 117 117 118 std::ostream & put ( std::ostream & os ) con 118 std::ostream & put ( std::ostream & os ) const; 119 std::istream & get ( std::istream & is ); 119 std::istream & get ( std::istream & is ); 120 120 121 std::string name() const; 121 std::string name() const; 122 HepRandomEngine & engine(); 122 HepRandomEngine & engine(); 123 123 124 static std::string distributionName() {retur 124 static std::string distributionName() {return "RandGeneral";} 125 // Provides the name of this distribution cl 125 // Provides the name of this distribution class 126 126 127 127 128 private: 128 private: 129 129 130 std::shared_ptr<HepRandomEngine> localEngine 130 std::shared_ptr<HepRandomEngine> localEngine; 131 std::vector<double> theIntegralPdf; 131 std::vector<double> theIntegralPdf; 132 int nBins; 132 int nBins; 133 double oneOverNbins; 133 double oneOverNbins; 134 int InterpolationType; 134 int InterpolationType; 135 135 136 // Private methods to factor out replicated 136 // Private methods to factor out replicated implementation sections 137 void prepareTable(const double* aProbFunc); 137 void prepareTable(const double* aProbFunc); 138 void useFlatDistribution(); 138 void useFlatDistribution(); 139 double mapRandom(double rand) const; 139 double mapRandom(double rand) const; 140 140 141 }; 141 }; 142 142 143 } // namespace CLHEP 143 } // namespace CLHEP 144 144 145 #include "CLHEP/Random/RandGeneral.icc" 145 #include "CLHEP/Random/RandGeneral.icc" 146 146 147 #endif 147 #endif 148 148