Geant4-11
Public Member Functions | Static Public Member Functions | Static Protected Attributes | Static Private Member Functions | Private Attributes
CLHEP::RandGamma Class Reference

#include <RandGamma.h>

Inheritance diagram for CLHEP::RandGamma:
CLHEP::HepRandom

Public Member Functions

HepRandomEngineengine ()
 
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 flat ()
 
double flat (HepRandomEngine *theNewEngine)
 
void flatArray (const int size, double *vect)
 
void flatArray (HepRandomEngine *theNewEngine, const int size, double *vect)
 
std::istream & get (std::istream &is)
 
std::string name () const
 
double operator() ()
 
double operator() (double k, double lambda)
 
std::ostream & put (std::ostream &os) const
 
 RandGamma (HepRandomEngine &anEngine, double k=1.0, double lambda=1.0)
 
 RandGamma (HepRandomEngine *anEngine, double k=1.0, double lambda=1.0)
 
virtual ~RandGamma ()
 

Static Public Member Functions

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

Static Protected Attributes

static const long seedTable [215][2]
 

Static Private Member Functions

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

Private Attributes

double defaultK
 
double defaultLambda
 
std::shared_ptr< HepRandomEnginelocalEngine
 

Detailed Description

Author

Definition at line 36 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 30 of file RandGamma.cc.

30 {
31}

Member Function Documentation

◆ createInstance()

int CLHEP::HepRandom::createInstance ( )
staticinherited

◆ distributionName()

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

Definition at line 98 of file RandGamma.h.

98{return "RandGamma";}

◆ engine()

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

Reimplemented from CLHEP::HepRandom.

Definition at line 28 of file RandGamma.cc.

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

References localEngine.

◆ fire() [1/2]

double CLHEP::RandGamma::fire ( )
inline

Referenced by fireArray().

◆ fire() [2/2]

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

Definition at line 43 of file RandGamma.cc.

43 {
44 return genGamma( localEngine.get(), k, lambda );
45}
static double genGamma(HepRandomEngine *anEngine, double k, double lambda)
Definition: RandGamma.cc:75

References genGamma(), G4InuclParticleNames::lambda, and localEngine.

◆ fireArray() [1/2]

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

Definition at line 62 of file RandGamma.cc.

63{
64 for( double* v = vect; v != vect + size; ++v )
66}
double defaultLambda
Definition: RandGamma.h:109

References defaultK, defaultLambda, and fire().

◆ fireArray() [2/2]

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

Definition at line 68 of file RandGamma.cc.

70{
71 for( double* v = vect; v != vect + size; ++v )
72 *v = fire(k,lambda);
73}

References fire(), and G4InuclParticleNames::lambda.

◆ flat() [1/2]

double CLHEP::HepRandom::flat ( )
inherited

◆ flat() [2/2]

double CLHEP::HepRandom::flat ( HepRandomEngine theNewEngine)
inlineinherited

◆ flatArray() [1/2]

void CLHEP::HepRandom::flatArray ( const int  size,
double *  vect 
)
inherited

◆ flatArray() [2/2]

void CLHEP::HepRandom::flatArray ( HepRandomEngine theNewEngine,
const int  size,
double *  vect 
)
inlineinherited

◆ genGamma()

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

Definition at line 75 of file RandGamma.cc.

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

References CLHEP_THREAD_LOCAL, e1, e2, e3, e4, e5, e6, CLHEP::HepRandomEngine::flat(), G4InuclParticleNames::lambda, and CLHEP::s.

Referenced by fire(), and shoot().

◆ get()

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

Reimplemented from CLHEP::HepRandom.

Definition at line 238 of file RandGamma.cc.

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

References defaultK, defaultLambda, CLHEP::DoubConv::longs2double(), name(), and CLHEP::possibleKeywordInput().

◆ getTheEngine()

HepRandomEngine * CLHEP::HepRandom::getTheEngine ( )
staticinherited

◆ getTheGenerator()

HepRandom * CLHEP::HepRandom::getTheGenerator ( )
staticinherited

◆ getTheSeed()

long CLHEP::HepRandom::getTheSeed ( )
staticinherited

◆ getTheSeeds()

const long * CLHEP::HepRandom::getTheSeeds ( )
staticinherited

◆ getTheTableSeeds()

void CLHEP::HepRandom::getTheTableSeeds ( long *  seeds,
int  index 
)
staticinherited

Definition at line 254 of file Random.cc.

255{
256 if ((index >= 0) && (index < 215)) {
257 seeds[0] = seedTable[index][0];
258 seeds[1] = seedTable[index][1];
259 }
260 else seeds = NULL;
261}
static const long seedTable[215][2]
Definition: Random.h:156

Referenced by CLHEP::HepJamesRandom::HepJamesRandom(), CLHEP::MTwistEngine::MTwistEngine(), CLHEP::RanecuEngine::RanecuEngine(), CLHEP::Ranlux64Engine::Ranlux64Engine(), CLHEP::RanluxEngine::RanluxEngine(), and CLHEP::RanecuEngine::setSeed().

◆ name()

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

Reimplemented from CLHEP::HepRandom.

Definition at line 27 of file RandGamma.cc.

27{return "RandGamma";}

Referenced by source.g4viscp.G4Scene::create_scene(), get(), mcscore.MCParticle::printout(), put(), and source.g4viscp.G4Scene::update_scene().

◆ 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 225 of file RandGamma.cc.

225 {
226 int pr=os.precision(20);
227 std::vector<unsigned long> t(2);
228 os << " " << name() << "\n";
229 os << "Uvec" << "\n";
231 os << defaultK << " " << t[0] << " " << t[1] << "\n";
233 os << defaultLambda << " " << t[0] << " " << t[1] << "\n";
234 os.precision(pr);
235 return os;
236}
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94

References defaultK, defaultLambda, CLHEP::DoubConv::dto2longs(), and name().

◆ restoreDistState()

static std::istream & CLHEP::HepRandom::restoreDistState ( std::istream &  is)
inlinestaticinherited

Definition at line 136 of file Random.h.

136{return is;}

◆ restoreEngineStatus()

void CLHEP::HepRandom::restoreEngineStatus ( const char  filename[] = "Config.conf")
staticinherited

Definition at line 283 of file Random.cc.

284{
285 theDefaults().theEngine->restoreStatus( filename );
286}

References CLHEP::anonymous_namespace{Random.cc}::theDefaults(), and CLHEP::anonymous_namespace{Random.cc}::defaults::theEngine.

◆ restoreFullState()

std::istream & CLHEP::HepRandom::restoreFullState ( std::istream &  is)
staticinherited

Definition at line 293 of file Random.cc.

293 {
294 is >> *getTheEngine();
295 return is;
296}
static HepRandomEngine * getTheEngine()
Definition: Random.cc:268

Referenced by CLHEP::RandFlat::restoreFullState(), and CLHEP::RandGauss::restoreFullState().

◆ restoreStaticRandomStates()

std::istream & CLHEP::HepRandom::restoreStaticRandomStates ( std::istream &  is)
staticinherited

Definition at line 302 of file Random.cc.

302 {
304}
static std::istream & restore(std::istream &is)

References CLHEP::StaticRandomStates::restore().

◆ saveDistState()

static std::ostream & CLHEP::HepRandom::saveDistState ( std::ostream &  os)
inlinestaticinherited

Definition at line 133 of file Random.h.

133{return os;}

◆ saveEngineStatus()

void CLHEP::HepRandom::saveEngineStatus ( const char  filename[] = "Config.conf")
staticinherited

◆ saveFullState()

std::ostream & CLHEP::HepRandom::saveFullState ( std::ostream &  os)
staticinherited

◆ saveStaticRandomStates()

std::ostream & CLHEP::HepRandom::saveStaticRandomStates ( std::ostream &  os)
staticinherited

Definition at line 298 of file Random.cc.

298 {
299 return StaticRandomStates::save(os);
300}
static std::ostream & save(std::ostream &os)

References CLHEP::StaticRandomStates::save().

◆ setTheEngine()

void CLHEP::HepRandom::setTheEngine ( HepRandomEngine theNewEngine)
staticinherited

Definition at line 273 of file Random.cc.

274{
275 theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() );
276}

References CLHEP::anonymous_namespace{Random.cc}::theDefaults(), and CLHEP::anonymous_namespace{Random.cc}::defaults::theEngine.

Referenced by CLHEP::StaticRandomStates::restore().

◆ setTheSeed()

void CLHEP::HepRandom::setTheSeed ( long  seed,
int  lxr = 3 
)
staticinherited

Definition at line 234 of file Random.cc.

235{
236 theDefaults().theEngine->setSeed(seed,lux);
237}
static constexpr double lux

References CLHEP::lux, CLHEP::anonymous_namespace{Random.cc}::theDefaults(), and CLHEP::anonymous_namespace{Random.cc}::defaults::theEngine.

◆ setTheSeeds()

void CLHEP::HepRandom::setTheSeeds ( const long *  seeds,
int  aux = -1 
)
staticinherited

◆ shoot() [1/4]

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

Referenced by shootArray().

◆ shoot() [2/4]

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

Definition at line 38 of file RandGamma.cc.

38 {
39 HepRandomEngine *anEngine = HepRandom::getTheEngine();
40 return genGamma( anEngine, k, lambda );
41}

References genGamma(), CLHEP::HepRandom::getTheEngine(), and G4InuclParticleNames::lambda.

◆ 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 33 of file RandGamma.cc.

34 {
35 return genGamma( anEngine, k, lambda );
36}

References genGamma(), and G4InuclParticleNames::lambda.

◆ shootArray() [1/2]

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

Definition at line 47 of file RandGamma.cc.

49{
50 for( double* v = vect; v != vect + size; ++v )
51 *v = shoot(k,lambda);
52}
static double shoot()

References G4InuclParticleNames::lambda, and shoot().

◆ 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 54 of file RandGamma.cc.

57{
58 for( double* v = vect; v != vect + size; ++v )
59 *v = shoot(anEngine,k,lambda);
60}

References G4InuclParticleNames::lambda, and shoot().

◆ showEngineStatus()

void CLHEP::HepRandom::showEngineStatus ( )
staticinherited

Field Documentation

◆ defaultK

double CLHEP::RandGamma::defaultK
private

Definition at line 108 of file RandGamma.h.

Referenced by fireArray(), get(), and put().

◆ defaultLambda

double CLHEP::RandGamma::defaultLambda
private

Definition at line 109 of file RandGamma.h.

Referenced by fireArray(), get(), and put().

◆ localEngine

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

Definition at line 107 of file RandGamma.h.

Referenced by engine(), and fire().

◆ seedTable

const long CLHEP::HepRandom::seedTable
staticprotectedinherited

Definition at line 156 of file Random.h.


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