Geant4  10.02.p03
CLHEP::RandGamma Class Reference

#include <RandGamma.h>

Inheritance diagram for CLHEP::RandGamma:
Collaboration diagram for CLHEP::RandGamma:

Public Member Functions

 RandGamma (HepRandomEngine &anEngine, double k=1.0, double lambda=1.0)
 
 RandGamma (HepRandomEngine *anEngine, double k=1.0, double lambda=1.0)
 
virtual ~RandGamma ()
 
double fire ()
 
double fire (double k, double lambda)
 
void fireArray (const int size, double *vect)
 
void fireArray (const int size, double *vect, double k, double lambda)
 
double operator() ()
 
double operator() (double k, double lambda)
 
std::ostream & put (std::ostream &os) const
 
std::istream & get (std::istream &is)
 
std::string name () const
 
HepRandomEngineengine ()
 
- Public Member Functions inherited from CLHEP::HepRandom
 HepRandom ()
 
 HepRandom (long seed)
 
 HepRandom (HepRandomEngine &algorithm)
 
 HepRandom (HepRandomEngine *algorithm)
 
virtual ~HepRandom ()
 
double flat ()
 
void flatArray (const int size, double *vect)
 
double flat (HepRandomEngine *theNewEngine)
 
void flatArray (HepRandomEngine *theNewEngine, const int size, double *vect)
 

Static Public Member Functions

static double shoot ()
 
static double shoot (double k, double lambda)
 
static void shootArray (const int size, double *vect, double k=1.0, double lambda=1.0)
 
static double shoot (HepRandomEngine *anEngine)
 
static double shoot (HepRandomEngine *anEngine, double k, double lambda)
 
static void shootArray (HepRandomEngine *anEngine, const int size, double *vect, double k=1.0, double lambda=1.0)
 
static std::string distributionName ()
 
- Static Public Member Functions inherited from CLHEP::HepRandom
static void setTheSeed (long seed, int lux=3)
 
static long getTheSeed ()
 
static void setTheSeeds (const long *seeds, int aux=-1)
 
static const long * getTheSeeds ()
 
static void getTheTableSeeds (long *seeds, int index)
 
static HepRandomgetTheGenerator ()
 
static void setTheEngine (HepRandomEngine *theNewEngine)
 
static HepRandomEnginegetTheEngine ()
 
static void saveEngineStatus (const char filename[]="Config.conf")
 
static void restoreEngineStatus (const char filename[]="Config.conf")
 
static std::ostream & saveFullState (std::ostream &os)
 
static std::istream & restoreFullState (std::istream &is)
 
static std::ostream & saveDistState (std::ostream &os)
 
static std::istream & restoreDistState (std::istream &is)
 
static std::ostream & saveStaticRandomStates (std::ostream &os)
 
static std::istream & restoreStaticRandomStates (std::istream &is)
 
static void showEngineStatus ()
 
static int createInstance ()
 
static std::string distributionName ()
 

Static Private Member Functions

static double genGamma (HepRandomEngine *anEngine, double k, double lambda)
 

Private Attributes

std::shared_ptr< HepRandomEnginelocalEngine
 
double defaultK
 
double defaultLambda
 

Additional Inherited Members

- Static Protected Attributes inherited from CLHEP::HepRandom
static const long seedTable [215][2]
 

Detailed Description

Author

Definition at line 37 of file RandGamma.h.

Constructor & Destructor Documentation

◆ RandGamma() [1/2]

CLHEP::RandGamma::RandGamma ( HepRandomEngine anEngine,
double  k = 1.0,
double  lambda = 1.0 
)
inline

◆ RandGamma() [2/2]

CLHEP::RandGamma::RandGamma ( HepRandomEngine anEngine,
double  k = 1.0,
double  lambda = 1.0 
)
inline

◆ ~RandGamma()

CLHEP::RandGamma::~RandGamma ( )
virtual

Definition at line 28 of file RandGamma.cc.

28  {
29 }

Member Function Documentation

◆ distributionName()

static std::string CLHEP::RandGamma::distributionName ( )
inlinestatic

Definition at line 99 of file RandGamma.h.

99 {return "RandGamma";}
Here is the call graph for this function:

◆ engine()

HepRandomEngine & CLHEP::RandGamma::engine ( )
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 26 of file RandGamma.cc.

26 {return *localEngine;}
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandGamma.h:108

◆ fire() [1/2]

double CLHEP::RandGamma::fire ( )
inline
Here is the caller graph for this function:

◆ fire() [2/2]

double CLHEP::RandGamma::fire ( double  k,
double  lambda 
)

Definition at line 41 of file RandGamma.cc.

41  {
42  return genGamma( localEngine.get(), k, lambda );
43 }
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandGamma.h:108
static double genGamma(HepRandomEngine *anEngine, double k, double lambda)
Definition: RandGamma.cc:73
Here is the call graph for this function:

◆ fireArray() [1/2]

void CLHEP::RandGamma::fireArray ( const int  size,
double *  vect 
)

Definition at line 60 of file RandGamma.cc.

61 {
62  for( double* v = vect; v != vect + size; ++v )
64 }
double defaultLambda
Definition: RandGamma.h:110
Here is the call graph for this function:

◆ fireArray() [2/2]

void CLHEP::RandGamma::fireArray ( const int  size,
double *  vect,
double  k,
double  lambda 
)

Definition at line 66 of file RandGamma.cc.

68 {
69  for( double* v = vect; v != vect + size; ++v )
70  *v = fire(k,lambda);
71 }
Here is the call graph for this function:

◆ genGamma()

double CLHEP::RandGamma::genGamma ( HepRandomEngine anEngine,
double  k,
double  lambda 
)
staticprivate

Definition at line 73 of file RandGamma.cc.

74  {
75 /*************************************************************************
76  * Gamma Distribution - Rejection algorithm gs combined with *
77  * Acceptance complement method gd *
78  *************************************************************************/
79 
80  static CLHEP_THREAD_LOCAL double aa = -1.0, aaa = -1.0, b, c, d, e, r, s, si, ss, q0;
81  static const double q1 = 0.0416666664, q2 = 0.0208333723, q3 = 0.0079849875,
82  q4 = 0.0015746717, q5 = -0.0003349403, q6 = 0.0003340332,
83  q7 = 0.0006053049, q8 = -0.0004701849, q9 = 0.0001710320,
84  a1 = 0.333333333, a2 = -0.249999949, a3 = 0.199999867,
85  a4 =-0.166677482, a5 = 0.142873973, a6 =-0.124385581,
86  a7 = 0.110368310, a8 = -0.112750886, a9 = 0.104089866,
87  e1 = 1.000000000, e2 = 0.499999994, e3 = 0.166666848,
88  e4 = 0.041664508, e5 = 0.008345522, e6 = 0.001353826,
89  e7 = 0.000247453;
90 
91 double gds,p,q,t,sign_u,u,v,w,x;
92 double v1,v2,v12;
93 
94 // Check for invalid input values
95 
96  if( a <= 0.0 ) return (-1.0);
97  if( lambda <= 0.0 ) return (-1.0);
98 
99  if (a < 1.0)
100  { // CASE A: Acceptance rejection algorithm gs
101  b = 1.0 + 0.36788794412 * a; // Step 1
102  for(;;)
103  {
104  p = b * anEngine->flat();
105  if (p <= 1.0)
106  { // Step 2. Case gds <= 1
107  gds = std::exp(std::log(p) / a);
108  if (std::log(anEngine->flat()) <= -gds) return(gds/lambda);
109  }
110  else
111  { // Step 3. Case gds > 1
112  gds = - std::log ((b - p) / a);
113  if (std::log(anEngine->flat()) <= ((a - 1.0) * std::log(gds))) return(gds/lambda);
114  }
115  }
116  }
117  else
118  { // CASE B: Acceptance complement algorithm gd
119  if (a != aa)
120  { // Step 1. Preparations
121  aa = a;
122  ss = a - 0.5;
123  s = std::sqrt(ss);
124  d = 5.656854249 - 12.0 * s;
125  }
126  // Step 2. Normal deviate
127  do {
128  v1 = 2.0 * anEngine->flat() - 1.0;
129  v2 = 2.0 * anEngine->flat() - 1.0;
130  v12 = v1*v1 + v2*v2;
131  } while ( v12 > 1.0 );
132  t = v1*std::sqrt(-2.0*std::log(v12)/v12);
133  x = s + 0.5 * t;
134  gds = x * x;
135  if (t >= 0.0) return(gds/lambda); // Immediate acceptance
136 
137  u = anEngine->flat(); // Step 3. Uniform random number
138  if (d * u <= t * t * t) return(gds/lambda); // Squeeze acceptance
139 
140  if (a != aaa)
141  { // Step 4. Set-up for hat case
142  aaa = a;
143  r = 1.0 / a;
144  q0 = ((((((((q9 * r + q8) * r + q7) * r + q6) * r + q5) * r + q4) *
145  r + q3) * r + q2) * r + q1) * r;
146  if (a > 3.686)
147  {
148  if (a > 13.022)
149  {
150  b = 1.77;
151  si = 0.75;
152  c = 0.1515 / s;
153  }
154  else
155  {
156  b = 1.654 + 0.0076 * ss;
157  si = 1.68 / s + 0.275;
158  c = 0.062 / s + 0.024;
159  }
160  }
161  else
162  {
163  b = 0.463 + s - 0.178 * ss;
164  si = 1.235;
165  c = 0.195 / s - 0.079 + 0.016 * s;
166  }
167  }
168  if (x > 0.0) // Step 5. Calculation of q
169  {
170  v = t / (s + s); // Step 6.
171  if (std::fabs(v) > 0.25)
172  {
173  q = q0 - s * t + 0.25 * t * t + (ss + ss) * std::log(1.0 + v);
174  }
175  else
176  {
177  q = q0 + 0.5 * t * t * ((((((((a9 * v + a8) * v + a7) * v + a6) *
178  v + a5) * v + a4) * v + a3) * v + a2) * v + a1) * v;
179  } // Step 7. Quotient acceptance
180  if (std::log(1.0 - u) <= q) return(gds/lambda);
181  }
182 
183  for(;;)
184  { // Step 8. Double exponential deviate t
185  do
186  {
187  e = -std::log(anEngine->flat());
188  u = anEngine->flat();
189  u = u + u - 1.0;
190  sign_u = (u > 0)? 1.0 : -1.0;
191  t = b + (e * si) * sign_u;
192  }
193  while (t <= -0.71874483771719); // Step 9. Rejection of t
194  v = t / (s + s); // Step 10. New q(t)
195  if (std::fabs(v) > 0.25)
196  {
197  q = q0 - s * t + 0.25 * t * t + (ss + ss) * std::log(1.0 + v);
198  }
199  else
200  {
201  q = q0 + 0.5 * t * t * ((((((((a9 * v + a8) * v + a7) * v + a6) *
202  v + a5) * v + a4) * v + a3) * v + a2) * v + a1) * v;
203  }
204  if (q <= 0.0) continue; // Step 11.
205  if (q > 0.5)
206  {
207  w = std::exp(q) - 1.0;
208  }
209  else
210  {
211  w = ((((((e7 * q + e6) * q + e5) * q + e4) * q + e3) * q + e2) *
212  q + e1) * q;
213  } // Step 12. Hat acceptance
214  if ( c * u * sign_u <= w * std::exp(e - 0.5 * t * t))
215  {
216  x = s + 0.5 * t;
217  return(x*x/lambda);
218  }
219  }
220  }
221 }
Float_t d
static const G4double a1
static const G4double e2
static const G4double a4
static const G4double e4
static const G4double a3
static const G4double e1
#define CLHEP_THREAD_LOCAL
Definition: thread_local.h:25
static const double s
static const G4double a5
static const G4double e3
static const G4double a2
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get()

std::istream & CLHEP::RandGamma::get ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 236 of file RandGamma.cc.

236  {
237  std::string inName;
238  is >> inName;
239  if (inName != name()) {
240  is.clear(std::ios::badbit | is.rdstate());
241  std::cerr << "Mismatch when expecting to read state of a "
242  << name() << " distribution\n"
243  << "Name found was " << inName
244  << "\nistream is left in the badbit state\n";
245  return is;
246  }
247  if (possibleKeywordInput(is, "Uvec", defaultK)) {
248  std::vector<unsigned long> t(2);
249  is >> defaultK >> t[0] >> t[1]; defaultK = DoubConv::longs2double(t);
250  is >> defaultLambda>>t[0]>>t[1]; defaultLambda = DoubConv::longs2double(t);
251  return is;
252  }
253  // is >> defaultK encompassed by possibleKeywordInput
254  is >> defaultLambda;
255  return is;
256 }
double defaultLambda
Definition: RandGamma.h:110
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167
std::string name() const
Definition: RandGamma.cc:25
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:114
Here is the call graph for this function:

◆ name()

std::string CLHEP::RandGamma::name ( ) const
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 25 of file RandGamma.cc.

25 {return "RandGamma";}
Here is the caller graph for this function:

◆ operator()() [1/2]

double CLHEP::RandGamma::operator() ( )
inlinevirtual

Reimplemented from CLHEP::HepRandom.

◆ operator()() [2/2]

double CLHEP::RandGamma::operator() ( double  k,
double  lambda 
)
inline

◆ put()

std::ostream & CLHEP::RandGamma::put ( std::ostream &  os) const
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 223 of file RandGamma.cc.

223  {
224  int pr=os.precision(20);
225  std::vector<unsigned long> t(2);
226  os << " " << name() << "\n";
227  os << "Uvec" << "\n";
229  os << defaultK << " " << t[0] << " " << t[1] << "\n";
231  os << defaultLambda << " " << t[0] << " " << t[1] << "\n";
232  os.precision(pr);
233  return os;
234 }
double defaultLambda
Definition: RandGamma.h:110
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98
std::string name() const
Definition: RandGamma.cc:25
Here is the call graph for this function:

◆ shoot() [1/4]

static double CLHEP::RandGamma::shoot ( )
inlinestatic
Here is the caller graph for this function:

◆ shoot() [2/4]

double CLHEP::RandGamma::shoot ( double  k,
double  lambda 
)
static

Definition at line 36 of file RandGamma.cc.

36  {
37  HepRandomEngine *anEngine = HepRandom::getTheEngine();
38  return genGamma( anEngine, k, lambda );
39 }
static HepRandomEngine * getTheEngine()
Definition: Random.cc:166
static double genGamma(HepRandomEngine *anEngine, double k, double lambda)
Definition: RandGamma.cc:73
Here is the call graph for this function:

◆ shoot() [3/4]

static double CLHEP::RandGamma::shoot ( HepRandomEngine anEngine)
inlinestatic

◆ shoot() [4/4]

double CLHEP::RandGamma::shoot ( HepRandomEngine anEngine,
double  k,
double  lambda 
)
static

Definition at line 31 of file RandGamma.cc.

32  {
33  return genGamma( anEngine, k, lambda );
34 }
static double genGamma(HepRandomEngine *anEngine, double k, double lambda)
Definition: RandGamma.cc:73
Here is the call graph for this function:

◆ shootArray() [1/2]

void CLHEP::RandGamma::shootArray ( const int  size,
double *  vect,
double  k = 1.0,
double  lambda = 1.0 
)
static

Definition at line 45 of file RandGamma.cc.

47 {
48  for( double* v = vect; v != vect + size; ++v )
49  *v = shoot(k,lambda);
50 }
static double shoot()
Here is the call graph for this function:

◆ shootArray() [2/2]

void CLHEP::RandGamma::shootArray ( HepRandomEngine anEngine,
const int  size,
double *  vect,
double  k = 1.0,
double  lambda = 1.0 
)
static

Definition at line 52 of file RandGamma.cc.

55 {
56  for( double* v = vect; v != vect + size; ++v )
57  *v = shoot(anEngine,k,lambda);
58 }
static double shoot()
Here is the call graph for this function:

Member Data Documentation

◆ defaultK

double CLHEP::RandGamma::defaultK
private

Definition at line 109 of file RandGamma.h.

◆ defaultLambda

double CLHEP::RandGamma::defaultLambda
private

Definition at line 110 of file RandGamma.h.

◆ localEngine

std::shared_ptr<HepRandomEngine> CLHEP::RandGamma::localEngine
private

Definition at line 108 of file RandGamma.h.


The documentation for this class was generated from the following files: