Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
CLHEP::RandGauss Class Reference

#include <RandGauss.h>

+ Inheritance diagram for CLHEP::RandGauss:

Public Member Functions

 RandGauss (HepRandomEngine &anEngine, double mean=0.0, double stdDev=1.0)
 
 RandGauss (HepRandomEngine *anEngine, double mean=0.0, double stdDev=1.0)
 
virtual ~RandGauss ()
 
double fire ()
 
double fire (double mean, double stdDev)
 
void fireArray (const int size, double *vect)
 
void fireArray (const int size, double *vect, double mean, double stdDev)
 
virtual double operator() ()
 
virtual double operator() (double mean, double stdDev)
 
std::string name () const
 
HepRandomEngineengine ()
 
std::ostream & put (std::ostream &os) const
 
std::istream & get (std::istream &is)
 
bool getF () const
 
void setF (bool val)
 
- 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)
 
virtual double operator() ()
 
virtual std::string name () const
 
virtual HepRandomEngineengine ()
 
virtual std::ostream & put (std::ostream &os) const
 
virtual std::istream & get (std::istream &is)
 

Static Public Member Functions

static double shoot ()
 
static double shoot (double mean, double stdDev)
 
static void shootArray (const int size, double *vect, double mean=0.0, double stdDev=1.0)
 
static double shoot (HepRandomEngine *anEngine)
 
static double shoot (HepRandomEngine *anEngine, double mean, double stdDev)
 
static void shootArray (HepRandomEngine *anEngine, const int size, double *vect, double mean=0.0, double stdDev=1.0)
 
static std::string distributionName ()
 
static bool getFlag ()
 
static void setFlag (bool val)
 
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 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 ()
 

Protected Member Functions

double normal ()
 

Static Protected Member Functions

static double getVal ()
 
static void setVal (double nextVal)
 

Protected Attributes

double defaultMean
 
double defaultStdDev
 
std::shared_ptr< HepRandomEnginelocalEngine
 

Additional Inherited Members

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

Detailed Description

Author

Definition at line 41 of file RandGauss.h.

Constructor & Destructor Documentation

◆ RandGauss() [1/2]

CLHEP::RandGauss::RandGauss ( HepRandomEngine anEngine,
double  mean = 0.0,
double  stdDev = 1.0 
)
inline

◆ RandGauss() [2/2]

CLHEP::RandGauss::RandGauss ( HepRandomEngine anEngine,
double  mean = 0.0,
double  stdDev = 1.0 
)
inline

◆ ~RandGauss()

CLHEP::RandGauss::~RandGauss ( )
virtual

Definition at line 51 of file RandGauss.cc.

51 {
52}

Member Function Documentation

◆ distributionName()

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

Definition at line 99 of file RandGauss.h.

99{return "RandGauss";}

Referenced by restoreDistState(), and saveDistState().

◆ engine()

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

Reimplemented from CLHEP::HepRandom.

Reimplemented in CLHEP::RandGaussQ, and CLHEP::RandGaussZiggurat.

Definition at line 45 of file RandGauss.cc.

45{return *localEngine;}
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandGauss.h:154

Referenced by CLHEP::RandGaussQ::engine(), and CLHEP::RandGaussZiggurat::engine().

◆ fire() [1/2]

double CLHEP::RandGauss::fire ( )

Referenced by fireArray(), and operator()().

◆ fire() [2/2]

double CLHEP::RandGauss::fire ( double  mean,
double  stdDev 
)
inline

◆ fireArray() [1/2]

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

Definition at line 158 of file RandGauss.cc.

159{
160 for( double* v = vect; v != vect + size; ++v )
162}
double defaultStdDev
Definition: RandGauss.h:152
double defaultMean
Definition: RandGauss.h:151

◆ fireArray() [2/2]

void CLHEP::RandGauss::fireArray ( const int  size,
double *  vect,
double  mean,
double  stdDev 
)

Definition at line 164 of file RandGauss.cc.

166{
167 for( double* v = vect; v != vect + size; ++v )
168 *v = fire( mean, stdDev );
169}

◆ get()

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

Reimplemented from CLHEP::HepRandom.

Reimplemented in CLHEP::RandGaussQ, and CLHEP::RandGaussZiggurat.

Definition at line 277 of file RandGauss.cc.

277 {
278 std::string inName;
279 is >> inName;
280 if (inName != name()) {
281 is.clear(std::ios::badbit | is.rdstate());
282 std::cerr << "Mismatch when expecting to read state of a "
283 << name() << " distribution\n"
284 << "Name found was " << inName
285 << "\nistream is left in the badbit state\n";
286 return is;
287 }
288 std::string c1;
289 std::string c2;
290 if (possibleKeywordInput(is, "Uvec", c1)) {
291 std::vector<unsigned long> t(2);
292 is >> defaultMean >> t[0] >> t[1]; defaultMean = DoubConv::longs2double(t);
293 is >> defaultStdDev>>t[0]>>t[1]; defaultStdDev = DoubConv::longs2double(t);
294 std::string ng;
295 is >> ng;
296 set = false;
297 if (ng == "nextGauss") {
298 is >> nextGauss >> t[0] >> t[1]; nextGauss = DoubConv::longs2double(t);
299 set = true;
300 }
301 return is;
302 }
303 // is >> c1 encompassed by possibleKeywordInput
304 is >> defaultMean >> c2 >> defaultStdDev;
305 if ( (!is) || (c1 != "Mean:") || (c2 != "Sigma:") ) {
306 std::cerr << "i/o problem while expecting to read state of a "
307 << name() << " distribution\n"
308 << "default mean and/or sigma could not be read\n";
309 return is;
310 }
311 is >> c1 >> c2 >> nextGauss;
312 if ( (!is) || (c1 != "RANDGAUSS") ) {
313 is.clear(std::ios::badbit | is.rdstate());
314 std::cerr << "Failure when reading caching state of RandGauss\n";
315 return is;
316 }
317 if (c2 == "CACHED_GAUSSIAN:") {
318 set = true;
319 } else if (c2 == "NO_CACHED_GAUSSIAN:") {
320 set = false;
321 } else {
322 is.clear(std::ios::badbit | is.rdstate());
323 std::cerr << "Unexpected caching state keyword of RandGauss:" << c2
324 << "\nistream is left in the badbit state\n";
325 }
326 return is;
327} // get
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
std::string name() const
Definition: RandGauss.cc:44
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:166

Referenced by CLHEP::RandGaussQ::get(), and CLHEP::RandGaussZiggurat::get().

◆ getF()

bool CLHEP::RandGauss::getF ( ) const
inline

Definition at line 115 of file RandGauss.h.

115{return set;}

◆ getFlag()

bool CLHEP::RandGauss::getFlag ( )
static

Definition at line 171 of file RandGauss.cc.

172{
173 return set_st;
174}

Referenced by saveDistState(), saveEngineStatus(), and shoot().

◆ getVal()

double CLHEP::RandGauss::getVal ( )
staticprotected

Definition at line 181 of file RandGauss.cc.

182{
183 return nextGauss_st;
184}

Referenced by saveDistState(), saveEngineStatus(), and shoot().

◆ name()

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

Reimplemented from CLHEP::HepRandom.

Reimplemented in CLHEP::RandGaussQ, and CLHEP::RandGaussZiggurat.

Definition at line 44 of file RandGauss.cc.

44{return "RandGauss";}

Referenced by get(), and put().

◆ normal()

double CLHEP::RandGauss::normal ( )
protected

Definition at line 132 of file RandGauss.cc.

133{
134 // Gaussian random numbers are generated two at the time, so every other
135 // time this is called we just return a number generated the time before.
136
137 if ( set ) {
138 set = false;
139 return nextGauss;
140 }
141
142 double r;
143 double v1,v2,fac,val;
144
145 do {
146 v1 = 2.0 * localEngine->flat() - 1.0;
147 v2 = 2.0 * localEngine->flat() - 1.0;
148 r = v1*v1 + v2*v2;
149 } while ( r > 1.0 );
150
151 fac = std::sqrt(-2.0*std::log(r)/r);
152 val = v1*fac;
153 nextGauss = val;
154 set = true;
155 return v2*fac;
156}

◆ operator()() [1/2]

double CLHEP::RandGauss::operator() ( )
virtual

Reimplemented from CLHEP::HepRandom.

Reimplemented in CLHEP::RandGaussQ, and CLHEP::RandGaussZiggurat.

Definition at line 54 of file RandGauss.cc.

54 {
56}

◆ operator()() [2/2]

double CLHEP::RandGauss::operator() ( double  mean,
double  stdDev 
)
virtual

Reimplemented in CLHEP::RandGaussQ, and CLHEP::RandGaussZiggurat.

Definition at line 58 of file RandGauss.cc.

58 {
59 return fire( mean, stdDev );
60}

◆ put()

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

Reimplemented from CLHEP::HepRandom.

Reimplemented in CLHEP::RandGaussQ, and CLHEP::RandGaussZiggurat.

Definition at line 258 of file RandGauss.cc.

258 {
259 os << name() << "\n";
260 int prec = os.precision(20);
261 std::vector<unsigned long> t(2);
262 os << "Uvec\n";
264 os << defaultMean << " " << t[0] << " " << t[1] << "\n";
266 os << defaultStdDev << " " << t[0] << " " << t[1] << "\n";
267 if ( set ) {
268 t = DoubConv::dto2longs(nextGauss);
269 os << "nextGauss " << nextGauss << " " << t[0] << " " << t[1] << "\n";
270 } else {
271 os << "no_cached_nextGauss \n";
272 }
273 os.precision(prec);
274 return os;
275} // put
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94

Referenced by CLHEP::RandGaussQ::put(), and CLHEP::RandGaussZiggurat::put().

◆ restoreDistState()

std::istream & CLHEP::RandGauss::restoreDistState ( std::istream &  is)
static

Definition at line 346 of file RandGauss.cc.

346 {
347 std::string inName;
348 is >> inName;
349 if (inName != distributionName()) {
350 is.clear(std::ios::badbit | is.rdstate());
351 std::cerr << "Mismatch when expecting to read static state of a "
352 << distributionName() << " distribution\n"
353 << "Name found was " << inName
354 << "\nistream is left in the badbit state\n";
355 return is;
356 }
357 std::string c1;
358 std::string c2;
359 if (possibleKeywordInput(is, "Uvec", c1)) {
360 std::vector<unsigned long> t(2);
361 std::string ng;
362 is >> ng;
363 setFlag (false);
364 if (ng == "nextGauss_st") {
365 is >> nextGauss_st >> t[0] >> t[1];
366 nextGauss_st = DoubConv::longs2double(t);
367 setFlag (true);
368 }
369 return is;
370 }
371 // is >> c1 encompassed by possibleKeywordInput
372 is >> c2 >> nextGauss_st;
373 if ( (!is) || (c1 != "RANDGAUSS") ) {
374 is.clear(std::ios::badbit | is.rdstate());
375 std::cerr << "Failure when reading caching state of static RandGauss\n";
376 return is;
377 }
378 if (c2 == "CACHED_GAUSSIAN:") {
379 setFlag(true);
380 } else if (c2 == "NO_CACHED_GAUSSIAN:") {
381 setFlag(false);
382 } else {
383 is.clear(std::ios::badbit | is.rdstate());
384 std::cerr << "Unexpected caching state keyword of static RandGauss:" << c2
385 << "\nistream is left in the badbit state\n";
386 }
387 return is;
388}
static void setFlag(bool val)
Definition: RandGauss.cc:176
static std::string distributionName()
Definition: RandGauss.h:99

Referenced by CLHEP::StaticRandomStates::restore(), and restoreFullState().

◆ restoreEngineStatus()

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

Definition at line 211 of file RandGauss.cc.

211 {
212
213 // First restore the engine status just like the base class would do:
214 getTheEngine()->restoreStatus( filename );
215
216 // Now find the line describing the cached variate:
217
218 std::ifstream infile ( filename, std::ios::in );
219 if (!infile) return;
220
221 char inputword[] = "NO_KEYWORD "; // leaves room for 14 characters plus \0
222 while (true) {
223 infile.width(13);
224 infile >> inputword;
225 if (strcmp(inputword,"RANDGAUSS")==0) break;
226 if (infile.eof()) break;
227 // If the file ends without the RANDGAUSS line, that means this
228 // was a file produced by an earlier version of RandGauss. We will
229 // replicated the old behavior in that case: set_st is cleared.
230 }
231
232 // Then read and use the caching info:
233
234 if (strcmp(inputword,"RANDGAUSS")==0) {
235 char setword[40]; // the longest, staticFirstUnusedBit: has length 21
236 infile.width(39);
237 infile >> setword; // setword should be CACHED_GAUSSIAN:
238 if (strcmp(setword,"CACHED_GAUSSIAN:") ==0) {
239 if (possibleKeywordInput(infile, "Uvec", nextGauss_st)) {
240 std::vector<unsigned long> t(2);
241 infile >> nextGauss_st >> t[0] >> t[1];
242 nextGauss_st = DoubConv::longs2double(t);
243 }
244 // is >> nextGauss_st encompassed by possibleKeywordInput
245 setFlag(true);
246 } else {
247 setFlag(false);
248 infile >> nextGauss_st; // because a 0 will have been output
249 }
250 } else {
251 setFlag(false);
252 }
253
254} // restoreEngineStatus
virtual void restoreStatus(const char filename[]="Config.conf")=0
static HepRandomEngine * getTheEngine()
Definition: Random.cc:268

◆ restoreFullState()

std::istream & CLHEP::RandGauss::restoreFullState ( std::istream &  is)
static

Definition at line 396 of file RandGauss.cc.

396 {
399 return is;
400}
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:293
static std::istream & restoreDistState(std::istream &is)
Definition: RandGauss.cc:346

◆ saveDistState()

std::ostream & CLHEP::RandGauss::saveDistState ( std::ostream &  os)
static

Definition at line 331 of file RandGauss.cc.

331 {
332 int prec = os.precision(20);
333 std::vector<unsigned long> t(2);
334 os << distributionName() << "\n";
335 os << "Uvec\n";
336 if ( getFlag() ) {
338 os << "nextGauss_st " << getVal() << " " << t[0] << " " << t[1] << "\n";
339 } else {
340 os << "no_cached_nextGauss_st \n";
341 }
342 os.precision(prec);
343 return os;
344}
static bool getFlag()
Definition: RandGauss.cc:171
static double getVal()
Definition: RandGauss.cc:181

Referenced by saveFullState().

◆ saveEngineStatus()

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

Definition at line 191 of file RandGauss.cc.

191 {
192
193 // First save the engine status just like the base class would do:
194 getTheEngine()->saveStatus( filename );
195
196 // Now append the cached variate, if any:
197
198 std::ofstream outfile ( filename, std::ios::app );
199
200 if ( getFlag() ) {
201 std::vector<unsigned long> t(2);
203 outfile << "RANDGAUSS CACHED_GAUSSIAN: Uvec "
204 << getVal() << " " << t[0] << " " << t[1] << "\n";
205 } else {
206 outfile << "RANDGAUSS NO_CACHED_GAUSSIAN: 0 \n" ;
207 }
208
209} // saveEngineStatus
virtual void saveStatus(const char filename[]="Config.conf") const =0

◆ saveFullState()

std::ostream & CLHEP::RandGauss::saveFullState ( std::ostream &  os)
static

Definition at line 390 of file RandGauss.cc.

390 {
392 saveDistState(os);
393 return os;
394}
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:288
static std::ostream & saveDistState(std::ostream &os)
Definition: RandGauss.cc:331

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

◆ setF()

void CLHEP::RandGauss::setF ( bool  val)
inline

Definition at line 117 of file RandGauss.h.

117{set = val;}

◆ setFlag()

void CLHEP::RandGauss::setFlag ( bool  val)
static

Definition at line 176 of file RandGauss.cc.

177{
178 set_st = val;
179}

Referenced by restoreDistState(), restoreEngineStatus(), and shoot().

◆ setVal()

void CLHEP::RandGauss::setVal ( double  nextVal)
staticprotected

Definition at line 186 of file RandGauss.cc.

187{
188 nextGauss_st = nextVal;
189}

Referenced by shoot().

◆ shoot() [1/4]

double CLHEP::RandGauss::shoot ( )
static

Definition at line 62 of file RandGauss.cc.

63{
64 // Gaussian random numbers are generated two at the time, so every other
65 // time this is called we just return a number generated the time before.
66
67 if ( getFlag() ) {
68 setFlag(false);
69 double x = getVal();
70 return x;
71 // return getVal();
72 }
73
74 double r;
75 double v1,v2,fac,val;
76 HepRandomEngine* anEngine = HepRandom::getTheEngine();
77
78 do {
79 v1 = 2.0 * anEngine->flat() - 1.0;
80 v2 = 2.0 * anEngine->flat() - 1.0;
81 r = v1*v1 + v2*v2;
82 } while ( r > 1.0 );
83
84 fac = std::sqrt(-2.0*std::log(r)/r);
85 val = v1*fac;
86 setVal(val);
87 setFlag(true);
88 return v2*fac;
89}
static void setVal(double nextVal)
Definition: RandGauss.cc:186

Referenced by shootArray().

◆ shoot() [2/4]

static double CLHEP::RandGauss::shoot ( double  mean,
double  stdDev 
)
inlinestatic

◆ shoot() [3/4]

double CLHEP::RandGauss::shoot ( HepRandomEngine anEngine)
static

Definition at line 98 of file RandGauss.cc.

99{
100 // Gaussian random numbers are generated two at the time, so every other
101 // time this is called we just return a number generated the time before.
102
103 if ( getFlag() ) {
104 setFlag(false);
105 return getVal();
106 }
107
108 double r;
109 double v1,v2,fac,val;
110
111 do {
112 v1 = 2.0 * anEngine->flat() - 1.0;
113 v2 = 2.0 * anEngine->flat() - 1.0;
114 r = v1*v1 + v2*v2;
115 } while ( r > 1.0 );
116
117 fac = std::sqrt( -2.0*std::log(r)/r);
118 val = v1*fac;
119 setVal(val);
120 setFlag(true);
121 return v2*fac;
122}

◆ shoot() [4/4]

static double CLHEP::RandGauss::shoot ( HepRandomEngine anEngine,
double  mean,
double  stdDev 
)
inlinestatic

◆ shootArray() [1/2]

void CLHEP::RandGauss::shootArray ( const int  size,
double *  vect,
double  mean = 0.0,
double  stdDev = 1.0 
)
static

Definition at line 91 of file RandGauss.cc.

93{
94 for( double* v = vect; v != vect + size; ++v )
95 *v = shoot(mean,stdDev);
96}
static double shoot()
Definition: RandGauss.cc:62

◆ shootArray() [2/2]

void CLHEP::RandGauss::shootArray ( HepRandomEngine anEngine,
const int  size,
double *  vect,
double  mean = 0.0,
double  stdDev = 1.0 
)
static

Definition at line 124 of file RandGauss.cc.

127{
128 for( double* v = vect; v != vect + size; ++v )
129 *v = shoot(anEngine,mean,stdDev);
130}

Member Data Documentation

◆ defaultMean

◆ defaultStdDev

◆ localEngine

std::shared_ptr<HepRandomEngine> CLHEP::RandGauss::localEngine
protected

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