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