Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CLHEP::RandGauss Class Reference

#include <RandGauss.h>

Inheritance diagram for CLHEP::RandGauss:
Collaboration 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)
 

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 42 of file RandGauss.h.

Constructor & Destructor Documentation

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

Definition at line 49 of file RandGauss.cc.

49  {
50 }

Member Function Documentation

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

Definition at line 100 of file RandGauss.h.

100 {return "RandGauss";}

Here is the caller graph for this function:

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 43 of file RandGauss.cc.

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

Here is the caller graph for this function:

double CLHEP::RandGauss::fire ( )

Here is the caller graph for this function:

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

Definition at line 156 of file RandGauss.cc.

157 {
158  for( double* v = vect; v != vect + size; ++v )
159  *v = fire( defaultMean, defaultStdDev );
160 }
double defaultStdDev
Definition: RandGauss.h:153
double defaultMean
Definition: RandGauss.h:152

Here is the call graph for this function:

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

Definition at line 162 of file RandGauss.cc.

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

Here is the call graph for this function:

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 275 of file RandGauss.cc.

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 116 of file RandGauss.h.

116 {return set;}
bool CLHEP::RandGauss::getFlag ( )
static

Definition at line 169 of file RandGauss.cc.

170 {
171  return set_st;
172 }

Here is the caller graph for this function:

double CLHEP::RandGauss::getVal ( )
staticprotected

Definition at line 179 of file RandGauss.cc.

180 {
181  return nextGauss_st;
182 }

Here is the caller graph for this function:

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 42 of file RandGauss.cc.

42 {return "RandGauss";}

Here is the caller graph for this function:

double CLHEP::RandGauss::normal ( )
protected

Definition at line 130 of file RandGauss.cc.

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 52 of file RandGauss.cc.

52  {
53  return fire( defaultMean, defaultStdDev );
54 }
double defaultStdDev
Definition: RandGauss.h:153
double defaultMean
Definition: RandGauss.h:152

Here is the call graph for this function:

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

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

Definition at line 56 of file RandGauss.cc.

56  {
57  return fire( mean, stdDev );
58 }

Here is the call graph for this function:

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 256 of file RandGauss.cc.

256  {
257  os << name() << "\n";
258  int prec = os.precision(20);
259  std::vector<unsigned long> t(2);
260  os << "Uvec\n";
262  os << defaultMean << " " << t[0] << " " << t[1] << "\n";
264  os << defaultStdDev << " " << t[0] << " " << t[1] << "\n";
265  if ( set ) {
266  t = DoubConv::dto2longs(nextGauss);
267  os << "nextGauss " << nextGauss << " " << t[0] << " " << t[1] << "\n";
268  } else {
269  os << "no_cached_nextGauss \n";
270  }
271  os.precision(prec);
272  return os;
273 } // put
double defaultStdDev
Definition: RandGauss.h:153
static const double prec
Definition: RanecuEngine.cc:58
double defaultMean
Definition: RandGauss.h:152
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98
std::string name() const
Definition: RandGauss.cc:42

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 344 of file RandGauss.cc.

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 209 of file RandGauss.cc.

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

Here is the call graph for this function:

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

Definition at line 394 of file RandGauss.cc.

394  {
396  restoreDistState(is);
397  return is;
398 }
static std::istream & restoreDistState(std::istream &is)
Definition: RandGauss.cc:344
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:290

Here is the call graph for this function:

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

Definition at line 329 of file RandGauss.cc.

329  {
330  int prec = os.precision(20);
331  std::vector<unsigned long> t(2);
332  os << distributionName() << "\n";
333  os << "Uvec\n";
334  if ( getFlag() ) {
336  os << "nextGauss_st " << getVal() << " " << t[0] << " " << t[1] << "\n";
337  } else {
338  os << "no_cached_nextGauss_st \n";
339  }
340  os.precision(prec);
341  return os;
342 }
static bool getFlag()
Definition: RandGauss.cc:169
static double getVal()
Definition: RandGauss.cc:179
static const double prec
Definition: RanecuEngine.cc:58
static std::string distributionName()
Definition: RandGauss.h:100
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 189 of file RandGauss.cc.

189  {
190 
191  // First save the engine status just like the base class would do:
192  getTheEngine()->saveStatus( filename );
193 
194  // Now append the cached variate, if any:
195 
196  std::ofstream outfile ( filename, std::ios::app );
197 
198  if ( getFlag() ) {
199  std::vector<unsigned long> t(2);
201  outfile << "RANDGAUSS CACHED_GAUSSIAN: Uvec "
202  << getVal() << " " << t[0] << " " << t[1] << "\n";
203  } else {
204  outfile << "RANDGAUSS NO_CACHED_GAUSSIAN: 0 \n" ;
205  }
206 
207 } // saveEngineStatus
static bool getFlag()
Definition: RandGauss.cc:169
static double getVal()
Definition: RandGauss.cc:179
static HepRandomEngine * getTheEngine()
Definition: Random.cc:265
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98
virtual void saveStatus(const char filename[]="Config.conf") const =0

Here is the call graph for this function:

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

Definition at line 388 of file RandGauss.cc.

388  {
390  saveDistState(os);
391  return os;
392 }
static std::ostream & saveDistState(std::ostream &os)
Definition: RandGauss.cc:329
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:285

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 118 of file RandGauss.h.

118 {set = val;}
void CLHEP::RandGauss::setFlag ( bool  val)
static

Definition at line 174 of file RandGauss.cc.

175 {
176  set_st = val;
177 }

Here is the caller graph for this function:

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

Definition at line 184 of file RandGauss.cc.

185 {
186  nextGauss_st = nextVal;
187 }

Here is the caller graph for this function:

double CLHEP::RandGauss::shoot ( )
static

Definition at line 60 of file RandGauss.cc.

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 96 of file RandGauss.cc.

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

Here is the call graph for this function:

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

Definition at line 89 of file RandGauss.cc.

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

Here is the call graph for this function:

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

Definition at line 122 of file RandGauss.cc.

125 {
126  for( double* v = vect; v != vect + size; ++v )
127  *v = shoot(anEngine,mean,stdDev);
128 }
static double shoot()
Definition: RandGauss.cc:60

Here is the call graph for this function:

Member Data Documentation

double CLHEP::RandGauss::defaultMean
protected

Definition at line 152 of file RandGauss.h.

double CLHEP::RandGauss::defaultStdDev
protected

Definition at line 153 of file RandGauss.h.

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

Definition at line 155 of file RandGauss.h.


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