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