Geant4 9.6.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
 
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 49 of file RandGauss.cc.

49 {
50}

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.

Definition at line 43 of file RandGauss.cc.

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

Referenced by CLHEP::RandGaussQ::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 156 of file RandGauss.cc.

157{
158 for( double* v = vect; v != vect + size; ++v )
160}
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 162 of file RandGauss.cc.

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

◆ get()

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

Reimplemented from CLHEP::HepRandom.

Reimplemented in CLHEP::RandGaussQ.

Definition at line 255 of file RandGauss.cc.

255 {
256 std::string inName;
257 is >> inName;
258 if (inName != name()) {
259 is.clear(std::ios::badbit | is.rdstate());
260 std::cerr << "Mismatch when expecting to read state of a "
261 << name() << " distribution\n"
262 << "Name found was " << inName
263 << "\nistream is left in the badbit state\n";
264 return is;
265 }
266 std::string c1;
267 std::string c2;
268 if (possibleKeywordInput(is, "Uvec", c1)) {
269 std::vector<unsigned long> t(2);
270 is >> defaultMean >> t[0] >> t[1]; defaultMean = DoubConv::longs2double(t);
271 is >> defaultStdDev>>t[0]>>t[1]; defaultStdDev = DoubConv::longs2double(t);
272 std::string ng;
273 is >> ng;
274 set = false;
275 if (ng == "nextGauss") {
276 is >> nextGauss >> t[0] >> t[1]; nextGauss = DoubConv::longs2double(t);
277 set = true;
278 }
279 return is;
280 }
281 // is >> c1 encompassed by possibleKeywordInput
282 is >> defaultMean >> c2 >> defaultStdDev;
283 if ( (!is) || (c1 != "Mean:") || (c2 != "Sigma:") ) {
284 std::cerr << "i/o problem while expecting to read state of a "
285 << name() << " distribution\n"
286 << "default mean and/or sigma could not be read\n";
287 return is;
288 }
289 is >> c1 >> c2 >> nextGauss;
290 if ( (!is) || (c1 != "RANDGAUSS") ) {
291 is.clear(std::ios::badbit | is.rdstate());
292 std::cerr << "Failure when reading caching state of RandGauss\n";
293 return is;
294 }
295 if (c2 == "CACHED_GAUSSIAN:") {
296 set = true;
297 } else if (c2 == "NO_CACHED_GAUSSIAN:") {
298 set = false;
299 } else {
300 is.clear(std::ios::badbit | is.rdstate());
301 std::cerr << "Unexpected caching state keyword of RandGauss:" << c2
302 << "\nistream is left in the badbit state\n";
303 }
304 return is;
305} // get
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:114
std::string name() const
Definition: RandGauss.cc:42
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167

Referenced by CLHEP::RandGaussQ::get().

◆ getF()

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

Definition at line 115 of file RandGauss.h.

115{return set;}

◆ getFlag()

static bool CLHEP::RandGauss::getFlag ( )
inlinestatic

Definition at line 111 of file RandGauss.h.

111{return set_st;}

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

◆ getVal()

static double CLHEP::RandGauss::getVal ( )
inlinestaticprotected

Definition at line 145 of file RandGauss.h.

145{return nextGauss_st;}

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

◆ name()

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

Reimplemented from CLHEP::HepRandom.

Reimplemented in CLHEP::RandGaussQ.

Definition at line 42 of file RandGauss.cc.

42{return "RandGauss";}

Referenced by get(), and put().

◆ normal()

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}

◆ operator()() [1/2]

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

Reimplemented from CLHEP::HepRandom.

Reimplemented in CLHEP::RandGaussQ.

Definition at line 52 of file RandGauss.cc.

52 {
54}

◆ operator()() [2/2]

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

Reimplemented in CLHEP::RandGaussQ.

Definition at line 56 of file RandGauss.cc.

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

◆ put()

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

Reimplemented from CLHEP::HepRandom.

Reimplemented in CLHEP::RandGaussQ.

Definition at line 236 of file RandGauss.cc.

236 {
237 os << name() << "\n";
238 int prec = os.precision(20);
239 std::vector<unsigned long> t(2);
240 os << "Uvec\n";
242 os << defaultMean << " " << t[0] << " " << t[1] << "\n";
244 os << defaultStdDev << " " << t[0] << " " << t[1] << "\n";
245 if ( set ) {
246 t = DoubConv::dto2longs(nextGauss);
247 os << "nextGauss " << nextGauss << " " << t[0] << " " << t[1] << "\n";
248 } else {
249 os << "no_cached_nextGauss \n";
250 }
251 os.precision(prec);
252 return os;
253} // put
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98

Referenced by CLHEP::RandGaussQ::put().

◆ restoreDistState()

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

Definition at line 324 of file RandGauss.cc.

324 {
325 std::string inName;
326 is >> inName;
327 if (inName != distributionName()) {
328 is.clear(std::ios::badbit | is.rdstate());
329 std::cerr << "Mismatch when expecting to read static state of a "
330 << distributionName() << " distribution\n"
331 << "Name found was " << inName
332 << "\nistream is left in the badbit state\n";
333 return is;
334 }
335 std::string c1;
336 std::string c2;
337 if (possibleKeywordInput(is, "Uvec", c1)) {
338 std::vector<unsigned long> t(2);
339 std::string ng;
340 is >> ng;
341 setFlag (false);
342 if (ng == "nextGauss_st") {
343 is >> nextGauss_st >> t[0] >> t[1];
344 nextGauss_st = DoubConv::longs2double(t);
345 setFlag (true);
346 }
347 return is;
348 }
349 // is >> c1 encompassed by possibleKeywordInput
350 is >> c2 >> nextGauss_st;
351 if ( (!is) || (c1 != "RANDGAUSS") ) {
352 is.clear(std::ios::badbit | is.rdstate());
353 std::cerr << "Failure when reading caching state of static RandGauss\n";
354 return is;
355 }
356 if (c2 == "CACHED_GAUSSIAN:") {
357 setFlag(true);
358 } else if (c2 == "NO_CACHED_GAUSSIAN:") {
359 setFlag(false);
360 } else {
361 is.clear(std::ios::badbit | is.rdstate());
362 std::cerr << "Unexpected caching state keyword of static RandGauss:" << c2
363 << "\nistream is left in the badbit state\n";
364 }
365 return is;
366}
static std::string distributionName()
Definition: RandGauss.h:99
static void setFlag(bool val)
Definition: RandGauss.h:113

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

◆ restoreEngineStatus()

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

Definition at line 189 of file RandGauss.cc.

189 {
190
191 // First restore the engine status just like the base class would do:
192 getTheEngine()->restoreStatus( filename );
193
194 // Now find the line describing the cached variate:
195
196 std::ifstream infile ( filename, std::ios::in );
197 if (!infile) return;
198
199 char inputword[] = "NO_KEYWORD "; // leaves room for 14 characters plus \0
200 while (true) {
201 infile.width(13);
202 infile >> inputword;
203 if (strcmp(inputword,"RANDGAUSS")==0) break;
204 if (infile.eof()) break;
205 // If the file ends without the RANDGAUSS line, that means this
206 // was a file produced by an earlier version of RandGauss. We will
207 // replicated the old behavior in that case: set_st is cleared.
208 }
209
210 // Then read and use the caching info:
211
212 if (strcmp(inputword,"RANDGAUSS")==0) {
213 char setword[40]; // the longest, staticFirstUnusedBit: has length 21
214 infile.width(39);
215 infile >> setword; // setword should be CACHED_GAUSSIAN:
216 if (strcmp(setword,"CACHED_GAUSSIAN:") ==0) {
217 if (possibleKeywordInput(infile, "Uvec", nextGauss_st)) {
218 std::vector<unsigned long> t(2);
219 infile >> nextGauss_st >> t[0] >> t[1];
220 nextGauss_st = DoubConv::longs2double(t);
221 }
222 // is >> nextGauss_st encompassed by possibleKeywordInput
223 setFlag(true);
224 } else {
225 setFlag(false);
226 infile >> nextGauss_st; // because a 0 will have been output
227 }
228 } else {
229 setFlag(false);
230 }
231
232} // restoreEngineStatus
virtual void restoreStatus(const char filename[]="Config.conf")=0
static HepRandomEngine * getTheEngine()
Definition: Random.cc:165

◆ restoreFullState()

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

Definition at line 374 of file RandGauss.cc.

374 {
377 return is;
378}
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:190
static std::istream & restoreDistState(std::istream &is)
Definition: RandGauss.cc:324

◆ saveDistState()

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

Definition at line 309 of file RandGauss.cc.

309 {
310 int prec = os.precision(20);
311 std::vector<unsigned long> t(2);
312 os << distributionName() << "\n";
313 os << "Uvec\n";
314 if ( getFlag() ) {
316 os << "nextGauss_st " << getVal() << " " << t[0] << " " << t[1] << "\n";
317 } else {
318 os << "no_cached_nextGauss_st \n";
319 }
320 os.precision(prec);
321 return os;
322}
static double getVal()
Definition: RandGauss.h:145
static bool getFlag()
Definition: RandGauss.h:111

Referenced by saveFullState().

◆ saveEngineStatus()

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

Definition at line 169 of file RandGauss.cc.

169 {
170
171 // First save the engine status just like the base class would do:
172 getTheEngine()->saveStatus( filename );
173
174 // Now append the cached variate, if any:
175
176 std::ofstream outfile ( filename, std::ios::app );
177
178 if ( getFlag() ) {
179 std::vector<unsigned long> t(2);
181 outfile << "RANDGAUSS CACHED_GAUSSIAN: Uvec "
182 << getVal() << " " << t[0] << " " << t[1] << "\n";
183 } else {
184 outfile << "RANDGAUSS NO_CACHED_GAUSSIAN: 0 \n" ;
185 }
186
187} // saveEngineStatus
virtual void saveStatus(const char filename[]="Config.conf") const =0

◆ saveFullState()

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

Definition at line 368 of file RandGauss.cc.

368 {
370 saveDistState(os);
371 return os;
372}
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:185
static std::ostream & saveDistState(std::ostream &os)
Definition: RandGauss.cc:309

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()

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

Definition at line 113 of file RandGauss.h.

113{set_st = val;}

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

◆ setVal()

static void CLHEP::RandGauss::setVal ( double  nextVal)
inlinestaticprotected

Definition at line 147 of file RandGauss.h.

147{nextGauss_st = nextVal;}

Referenced by shoot().

◆ shoot() [1/4]

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 void setVal(double nextVal)
Definition: RandGauss.h:147

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 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}

◆ 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 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

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

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

Member Data Documentation

◆ defaultMean

double CLHEP::RandGauss::defaultMean
protected

◆ defaultStdDev

double CLHEP::RandGauss::defaultStdDev
protected

◆ localEngine

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

Definition at line 154 of file RandGauss.h.

Referenced by engine(), normal(), and CLHEP::RandGaussQ::operator()().


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