CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
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 lxr=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 43 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 53 of file RandGauss.cc.

53 {
54}

Member Function Documentation

◆ distributionName()

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

Definition at line 101 of file RandGauss.h.

101{return "RandGauss";}

Referenced by restoreDistState(), and saveDistState().

◆ engine()

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 47 of file RandGauss.cc.

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

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

◆ fire() [1/2]

double CLHEP::RandGauss::fire ( )

◆ 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 160 of file RandGauss.cc.

161{
162 for( double* v = vect; v != vect + size; ++v )
164}
double defaultStdDev
Definition: RandGauss.h:154
double defaultMean
Definition: RandGauss.h:153

◆ fireArray() [2/2]

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

Definition at line 166 of file RandGauss.cc.

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

◆ get()

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 285 of file RandGauss.cc.

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

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

◆ getF()

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

Definition at line 117 of file RandGauss.h.

117{return set;}

◆ getFlag()

bool CLHEP::RandGauss::getFlag ( )
static

Definition at line 173 of file RandGauss.cc.

174{
175 return set_st;
176}

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

◆ getVal()

double CLHEP::RandGauss::getVal ( )
staticprotected

Definition at line 183 of file RandGauss.cc.

184{
185 return nextGauss_st;
186}

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

◆ name()

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 46 of file RandGauss.cc.

46{return "RandGauss";}

Referenced by get(), and put().

◆ normal()

double CLHEP::RandGauss::normal ( )
protected

Definition at line 134 of file RandGauss.cc.

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

◆ operator()() [1/2]

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 56 of file RandGauss.cc.

56 {
58}

◆ operator()() [2/2]

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

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

Definition at line 60 of file RandGauss.cc.

60 {
61 return fire( mean, stdDev );
62}

◆ put()

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

Reimplemented from CLHEP::HepRandom.

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

Definition at line 260 of file RandGauss.cc.

260 {
261 os << name() << "\n";
262 long prec = os.precision(20);
263 std::vector<unsigned long> t(2);
264 os << "Uvec\n";
266 os << defaultMean << " " << t[0] << " " << t[1] << "\n";
268 os << defaultStdDev << " " << t[0] << " " << t[1] << "\n";
269 if ( set ) {
270 t = DoubConv::dto2longs(nextGauss);
271 os << "nextGauss " << nextGauss << " " << t[0] << " " << t[1] << "\n";
272 #ifdef TRACE_IO
273 std::cout << "put(): nextGauss = " << nextGauss << "\n";
274 #endif
275 } else {
276 #ifdef TRACE_IO
277 std::cout << "put(): No nextGauss \n";
278 #endif
279 os << "no_cached_nextGauss \n";
280 }
281 os.precision(prec);
282 return os;
283} // put
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94

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

◆ restoreDistState()

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

Definition at line 361 of file RandGauss.cc.

361 {
362 std::string inName;
363 is >> inName;
364 if (inName != distributionName()) {
365 is.clear(std::ios::badbit | is.rdstate());
366 std::cerr << "Mismatch when expecting to read static state of a "
367 << distributionName() << " distribution\n"
368 << "Name found was " << inName
369 << "\nistream is left in the badbit state\n";
370 return is;
371 }
372 std::string c1;
373 std::string c2;
374 if (possibleKeywordInput(is, "Uvec", c1)) {
375 std::vector<unsigned long> t(2);
376 std::string ng;
377 is >> ng;
378 setFlag (false);
379 if (ng == "nextGauss_st") {
380 is >> nextGauss_st >> t[0] >> t[1];
381 nextGauss_st = DoubConv::longs2double(t);
382 setFlag (true);
383 }
384 return is;
385 }
386 // is >> c1 encompassed by possibleKeywordInput
387 is >> c2 >> nextGauss_st;
388 if ( (!is) || (c1 != "RANDGAUSS") ) {
389 is.clear(std::ios::badbit | is.rdstate());
390 std::cerr << "Failure when reading caching state of static RandGauss\n";
391 return is;
392 }
393 if (c2 == "CACHED_GAUSSIAN:") {
394 setFlag(true);
395 } else if (c2 == "NO_CACHED_GAUSSIAN:") {
396 setFlag(false);
397 } else {
398 is.clear(std::ios::badbit | is.rdstate());
399 std::cerr << "Unexpected caching state keyword of static RandGauss:" << c2
400 << "\nistream is left in the badbit state\n";
401 }
402 return is;
403}
static void setFlag(bool val)
Definition: RandGauss.cc:178
static std::string distributionName()
Definition: RandGauss.h:101

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

◆ restoreEngineStatus()

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

Definition at line 213 of file RandGauss.cc.

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

Referenced by restoreStep().

◆ restoreFullState()

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

Definition at line 411 of file RandGauss.cc.

411 {
414 return is;
415}
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:295
static std::istream & restoreDistState(std::istream &is)
Definition: RandGauss.cc:361

◆ saveDistState()

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

Definition at line 346 of file RandGauss.cc.

346 {
347 long prec = os.precision(20);
348 std::vector<unsigned long> t(2);
349 os << distributionName() << "\n";
350 os << "Uvec\n";
351 if ( getFlag() ) {
353 os << "nextGauss_st " << getVal() << " " << t[0] << " " << t[1] << "\n";
354 } else {
355 os << "no_cached_nextGauss_st \n";
356 }
357 os.precision(prec);
358 return os;
359}
static bool getFlag()
Definition: RandGauss.cc:173
static double getVal()
Definition: RandGauss.cc:183

Referenced by saveFullState().

◆ saveEngineStatus()

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

Definition at line 193 of file RandGauss.cc.

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

Referenced by saveStep().

◆ saveFullState()

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

Definition at line 405 of file RandGauss.cc.

405 {
407 saveDistState(os);
408 return os;
409}
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:290
static std::ostream & saveDistState(std::ostream &os)
Definition: RandGauss.cc:346

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

◆ setF()

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

Definition at line 119 of file RandGauss.h.

119{set = val;}

◆ setFlag()

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

Definition at line 178 of file RandGauss.cc.

179{
180 set_st = val;
181}

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

◆ setVal()

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

Definition at line 188 of file RandGauss.cc.

189{
190 nextGauss_st = nextVal;
191}

Referenced by shoot().

◆ shoot() [1/4]

double CLHEP::RandGauss::shoot ( )
static

Definition at line 64 of file RandGauss.cc.

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

Referenced by captureStatics(), layout(), main(), randomizeStatics(), restoreStep(), restoreStepX(), saveStep(), saveStepX(), shootArray(), testRandGauss(), and user_layout().

◆ 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 100 of file RandGauss.cc.

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

◆ 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 93 of file RandGauss.cc.

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

◆ 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 126 of file RandGauss.cc.

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

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: