CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

RandGeneral.h

Go to the documentation of this file.
00001 // $Id: RandGeneral.h,v 1.3.4.1 2005/03/18 22:26:48 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 <vector>
00032 
00033 namespace CLHEP {
00034 
00039 class RandGeneral : public HepRandom {
00040 
00041 public:
00042 
00043   RandGeneral ( const double* aProbFunc, 
00044                 int theProbSize, 
00045                 int IntType=0 );
00046   RandGeneral ( HepRandomEngine& anEngine,
00047                 const double* aProbFunc, 
00048                 int theProbSize, 
00049                 int IntType=0 );
00050   RandGeneral ( HepRandomEngine* anEngine, 
00051                 const double* aProbFunc, 
00052                 int theProbSize, 
00053                 int IntType=0 );
00054   // These constructors should be used to instantiate a RandGeneral
00055   // distribution object defining a local engine for it.
00056   // The static generator will be skiped by using the non-static methods
00057   // defined below. In case no engine is specified in the constructor, the
00058   // default engine used by the static generator is applied.
00059   // If the engine is passed by pointer the corresponding engine object
00060   // will be deleted by the RandGeneral destructor.
00061   // If the engine is passed by reference the corresponding engine object
00062   // will not be deleted by the RandGeneral destructor.
00063   // The probability distribution function (Pdf) must be provided by the user
00064   // as an array of positive real number. The array size must also be
00065   // provided. The Pdf doesn't need to be normalized to 1. 
00066   // if IntType = 0 ( default value ) a uniform random number is
00067   // generated using the engine. The uniform number is then transformed
00068   // to the user's distribution using the cumulative probability
00069   // distribution constructed from his histogram. The cumulative
00070   // distribution is inverted using a binary search for the nearest
00071   // bin boundary and a linear interpolation within the
00072   // bin. RandGeneral therefore generates a constant density within
00073   // each bin.
00074   // if IntType = 1 no interpolation is performed and the result is a
00075   // discrete distribution.
00076 
00077   virtual ~RandGeneral();
00078   // Destructor
00079 
00080   // Methods to shoot random values using the static generator
00081   // N.B.: The methods are NOT static since they use nonstatic members
00082   // theIntegralPdf & nBins
00083 
00085         //                 //
00086         // BIG RED WARNING //
00087         //                 //
00089         //
00090         // The above N.B. is telling users that the shoot() methods in this
00091         // class are NOT STATIC.  You cannot do 
00092         //      double x = RandGeneral::shoot();
00093         // It would not make sense to provide a static shoot -- what would 
00094         // the default probability function look like?
00095 
00096   inline double shoot();
00097 
00098   inline void shootArray ( const int size, double* vect);
00099 
00100   //  Methods to shoot random values using a given engine
00101   //  by-passing the static generator.
00102 
00103   double shoot( HepRandomEngine* anEngine );
00104 
00105   void shootArray ( HepRandomEngine* anEngine, const int size,
00106                     double* vect );
00107                             
00108   //  Methods using the localEngine to shoot random values, by-passing
00109   //  the static generator.
00110 
00111   double fire();
00112 
00113   void fireArray ( const int size, double* vect);
00114 
00115   double operator()();
00116 
00117   // Save and restore to/from streams
00118   
00119   std::ostream & put ( std::ostream & os ) const;
00120   std::istream & get ( std::istream & is );
00121 
00122   std::string name() const;
00123   HepRandomEngine & engine();
00124 
00125   static std::string distributionName() {return "RandGeneral";}  
00126   // Provides the name of this distribution class
00127   
00128 
00129 private:
00130 
00131   // Private copy constructor. Declaring it here disallows use.
00132   RandGeneral(const RandGeneral&);
00133 
00134   HepRandomEngine* localEngine;
00135   bool deleteEngine;
00136   std::vector<double> theIntegralPdf;
00137   int nBins;
00138   double oneOverNbins;
00139   int InterpolationType;
00140 
00141   // Private methods to factor out replicated implementation sections
00142   void prepareTable(const double* aProbFunc);
00143   void useFlatDistribution();
00144   double mapRandom(double rand) const;
00145 
00146 };
00147 
00148 }  // namespace CLHEP
00149 
00150 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
00151 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
00152 using namespace CLHEP;
00153 #endif
00154 
00155 #include "CLHEP/Random/RandGeneral.icc"
00156 
00157 #endif

Generated on Thu Jul 1 22:02:30 2010 for CLHEP by  doxygen 1.4.7