35 #include "CLHEP/Random/RandGauss.h"
36 #include "CLHEP/Random/DoubConv.h"
46 CLHEP_THREAD_LOCAL
bool RandGauss::set_st =
false;
47 CLHEP_THREAD_LOCAL
double RandGauss::nextGauss_st = 0.0;
49 RandGauss::~RandGauss() {
52 double RandGauss::operator()() {
53 return fire( defaultMean, defaultStdDev );
56 double RandGauss::operator()(
double mean,
double stdDev ) {
57 return fire( mean, stdDev );
74 HepRandomEngine* anEngine = HepRandom::getTheEngine();
77 v1 = 2.0 * anEngine->flat() - 1.0;
78 v2 = 2.0 * anEngine->flat() - 1.0;
82 fac = std::sqrt(-2.0*std::log(r)/r);
89 void RandGauss::shootArray(
const int size,
double* vect,
90 double mean,
double stdDev )
92 for(
double* v = vect; v != vect + size; ++v )
93 *v =
shoot(mean,stdDev);
107 double v1,v2,
fac,val;
110 v1 = 2.0 * anEngine->flat() - 1.0;
111 v2 = 2.0 * anEngine->flat() - 1.0;
115 fac = std::sqrt( -2.0*std::log(r)/r);
122 void RandGauss::shootArray( HepRandomEngine* anEngine,
123 const int size,
double* vect,
124 double mean,
double stdDev )
126 for(
double* v = vect; v != vect + size; ++v )
127 *v =
shoot(anEngine,mean,stdDev);
141 double v1,v2,
fac,val;
144 v1 = 2.0 * localEngine->flat() - 1.0;
145 v2 = 2.0 * localEngine->flat() - 1.0;
149 fac = std::sqrt(-2.0*std::log(r)/r);
156 void RandGauss::fireArray(
const int size,
double* vect)
158 for(
double* v = vect; v != vect + size; ++v )
159 *v = fire( defaultMean, defaultStdDev );
162 void RandGauss::fireArray(
const int size,
double* vect,
163 double mean,
double stdDev )
165 for(
double* v = vect; v != vect + size; ++v )
166 *v = fire( mean, stdDev );
169 bool RandGauss::getFlag()
174 void RandGauss::setFlag(
bool val )
179 double RandGauss::getVal()
184 void RandGauss::setVal(
double nextVal )
186 nextGauss_st = nextVal;
189 void RandGauss::saveEngineStatus (
const char filename[] ) {
192 getTheEngine()->saveStatus( filename );
196 std::ofstream outfile ( filename, std::ios::app );
199 std::vector<unsigned long> t(2);
200 t = DoubConv::dto2longs(getVal());
201 outfile <<
"RANDGAUSS CACHED_GAUSSIAN: Uvec "
202 << getVal() <<
" " << t[0] <<
" " << t[1] <<
"\n";
204 outfile <<
"RANDGAUSS NO_CACHED_GAUSSIAN: 0 \n" ;
209 void RandGauss::restoreEngineStatus(
const char filename[] ) {
212 getTheEngine()->restoreStatus( filename );
216 std::ifstream infile ( filename, std::ios::in );
219 char inputword[] =
"NO_KEYWORD ";
223 if (strcmp(inputword,
"RANDGAUSS")==0)
break;
224 if (infile.eof())
break;
232 if (strcmp(inputword,
"RANDGAUSS")==0) {
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);
246 infile >> nextGauss_st;
256 std::ostream & RandGauss::put ( std::ostream & os )
const {
257 os <<
name() <<
"\n";
258 int prec = os.precision(20);
259 std::vector<unsigned long> t(2);
261 t = DoubConv::dto2longs(defaultMean);
262 os << defaultMean <<
" " << t[0] <<
" " << t[1] <<
"\n";
263 t = DoubConv::dto2longs(defaultStdDev);
264 os << defaultStdDev <<
" " << t[0] <<
" " << t[1] <<
"\n";
266 t = DoubConv::dto2longs(nextGauss);
267 os <<
"nextGauss " << nextGauss <<
" " << t[0] <<
" " << t[1] <<
"\n";
269 os <<
"no_cached_nextGauss \n";
275 std::istream & RandGauss::get ( std::istream & is ) {
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";
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);
295 if (ng ==
"nextGauss") {
296 is >> nextGauss >> t[0] >> t[1]; nextGauss = DoubConv::longs2double(t);
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";
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";
315 if (c2 ==
"CACHED_GAUSSIAN:") {
317 }
else if (c2 ==
"NO_CACHED_GAUSSIAN:") {
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";
329 std::ostream & RandGauss::saveDistState ( std::ostream & os ) {
330 int prec = os.precision(20);
331 std::vector<unsigned long> t(2);
332 os << distributionName() <<
"\n";
335 t = DoubConv::dto2longs(getVal());
336 os <<
"nextGauss_st " << getVal() <<
" " << t[0] <<
" " << t[1] <<
"\n";
338 os <<
"no_cached_nextGauss_st \n";
344 std::istream & RandGauss::restoreDistState ( std::istream & is ) {
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";
357 if (possibleKeywordInput(is,
"Uvec", c1)) {
358 std::vector<unsigned long> t(2);
362 if (ng ==
"nextGauss_st") {
363 is >> nextGauss_st >> t[0] >> t[1];
364 nextGauss_st = DoubConv::longs2double(t);
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";
376 if (c2 ==
"CACHED_GAUSSIAN:") {
378 }
else if (c2 ==
"NO_CACHED_GAUSSIAN:") {
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";
388 std::ostream & RandGauss::saveFullState ( std::ostream & os ) {
389 HepRandom::saveFullState(os);
394 std::istream & RandGauss::restoreFullState ( std::istream & is ) {
395 HepRandom::restoreFullState(is);
396 restoreDistState(is);
ThreeVector shoot(const G4int Ap, const G4int Af)
static c2_factory< G4double > c2
static int engine(pchar, pchar, double &, pchar &, const dic_type &)
static double normal(HepRandomEngine *eptr)
static const G4double fac