CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: RandGeneral.h,v 1.5 2010/06/16 17:24:53 garren Exp $ 00002 // -*- C++ -*- 00003 // 00004 // ----------------------------------------------------------------------- 00005 // HEP Random 00006 // --- RandGeneral --- 00007 // class header file 00008 // ----------------------------------------------------------------------- 00009 00010 // Class defining methods for shooting generally distributed random values, 00011 // given a user-defined probability distribution function. 00012 00013 // ======================================================================= 00014 // S.Magni & G.Pieri - Created: 29 April 1998 00015 // G.Cosmo - Added constructor using default engine from the 00016 // static generator: 20 Aug 1998 00017 // S.Magni & G.Pieri - Added linear interpolation: 24 March 1999 00018 // M. Fischler - Added private methods that simplify the implementaion 00019 // prepareTables(), useFlatDistribution(), mapRandom() 00020 // - Added private variable oneOverNbins. 00021 // - Made the warning about shoot() not being static a tad 00022 // more prominent. 14 May 1999 00023 // M Fischler - put and get to/from streams 12/15/04 00024 // ======================================================================= 00025 00026 #ifndef RandGeneral_h 00027 #define RandGeneral_h 1 00028 00029 #include "CLHEP/Random/defs.h" 00030 #include "CLHEP/Random/Random.h" 00031 #include "CLHEP/Utility/memory.h" 00032 #include <vector> 00033 00034 namespace CLHEP { 00035 00040 class RandGeneral : public HepRandom { 00041 00042 public: 00043 00044 RandGeneral ( const double* aProbFunc, 00045 int theProbSize, 00046 int IntType=0 ); 00047 RandGeneral ( HepRandomEngine& anEngine, 00048 const double* aProbFunc, 00049 int theProbSize, 00050 int IntType=0 ); 00051 RandGeneral ( HepRandomEngine* anEngine, 00052 const double* aProbFunc, 00053 int theProbSize, 00054 int IntType=0 ); 00055 // These constructors should be used to instantiate a RandGeneral 00056 // distribution object defining a local engine for it. 00057 // The static generator will be skipped by using the non-static methods 00058 // defined below. In case no engine is specified in the constructor, the 00059 // default engine used by the static generator is applied. 00060 // If the engine is passed by pointer the corresponding engine object 00061 // will be deleted by the RandGeneral destructor. 00062 // If the engine is passed by reference the corresponding engine object 00063 // will not be deleted by the RandGeneral destructor. 00064 // The probability distribution function (Pdf) must be provided by the user 00065 // as an array of positive real number. The array size must also be 00066 // provided. The Pdf doesn't need to be normalized to 1. 00067 // if IntType = 0 ( default value ) a uniform random number is 00068 // generated using the engine. The uniform number is then transformed 00069 // to the user's distribution using the cumulative probability 00070 // distribution constructed from his histogram. The cumulative 00071 // distribution is inverted using a binary search for the nearest 00072 // bin boundary and a linear interpolation within the 00073 // bin. RandGeneral therefore generates a constant density within 00074 // each bin. 00075 // if IntType = 1 no interpolation is performed and the result is a 00076 // discrete distribution. 00077 00078 virtual ~RandGeneral(); 00079 // Destructor 00080 00081 // Methods to shoot random values using the static generator 00082 // N.B.: The methods are NOT static since they use nonstatic members 00083 // theIntegralPdf & nBins 00084 00086 // // 00087 // BIG RED WARNING // 00088 // // 00090 // 00091 // The above N.B. is telling users that the shoot() methods in this 00092 // class are NOT STATIC. You cannot do 00093 // double x = RandGeneral::shoot(); 00094 // It would not make sense to provide a static shoot -- what would 00095 // the default probability function look like? 00096 00097 inline double shoot(); 00098 00099 inline void shootArray ( const int size, double* vect); 00100 00101 // Methods to shoot random values using a given engine 00102 // by-passing the static generator. 00103 00104 double shoot( HepRandomEngine* anEngine ); 00105 00106 void shootArray ( HepRandomEngine* anEngine, const int size, 00107 double* vect ); 00108 00109 // Methods using the localEngine to shoot random values, by-passing 00110 // the static generator. 00111 00112 double fire(); 00113 00114 void fireArray ( const int size, double* vect); 00115 00116 double operator()(); 00117 00118 // Save and restore to/from streams 00119 00120 std::ostream & put ( std::ostream & os ) const; 00121 std::istream & get ( std::istream & is ); 00122 00123 std::string name() const; 00124 HepRandomEngine & engine(); 00125 00126 static std::string distributionName() {return "RandGeneral";} 00127 // Provides the name of this distribution class 00128 00129 00130 private: 00131 00132 shared_ptr<HepRandomEngine> localEngine; 00133 std::vector<double> theIntegralPdf; 00134 int nBins; 00135 double oneOverNbins; 00136 int InterpolationType; 00137 00138 // Private methods to factor out replicated implementation sections 00139 void prepareTable(const double* aProbFunc); 00140 void useFlatDistribution(); 00141 double mapRandom(double rand) const; 00142 00143 }; 00144 00145 } // namespace CLHEP 00146 00147 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 00148 // backwards compatibility will be enabled ONLY in CLHEP 1.9 00149 using namespace CLHEP; 00150 #endif 00151 00152 #include "CLHEP/Random/RandGeneral.icc" 00153 00154 #endif