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

#include <MixMaxRng.h>

+ Inheritance diagram for CLHEP::MixMaxRng:

Public Member Functions

 MixMaxRng (std::istream &is)
 
 MixMaxRng ()
 
 MixMaxRng (long seed)
 
 ~MixMaxRng ()
 
 MixMaxRng (const MixMaxRng &rng)
 
MixMaxRngoperator= (const MixMaxRng &rng)
 
double flat ()
 
void flatArray (const int size, double *vect)
 
void setSeed (long seed, int dum=0)
 
void setSeeds (const long *seeds, int seedNum=0)
 
void saveStatus (const char filename[]="MixMaxRngState.conf") const
 
void restoreStatus (const char filename[]="MixMaxRngState.conf")
 
void showStatus () const
 
 operator double ()
 
 operator float ()
 
 operator unsigned int ()
 
virtual std::ostream & put (std::ostream &os) const
 
virtual std::istream & get (std::istream &is)
 
virtual std::istream & getState (std::istream &is)
 
std::string name () const
 
std::vector< unsigned long > put () const
 
bool get (const std::vector< unsigned long > &v)
 
bool getState (const std::vector< unsigned long > &v)
 
- Public Member Functions inherited from CLHEP::HepRandomEngine
 HepRandomEngine ()
 
virtual ~HepRandomEngine ()
 
bool operator== (const HepRandomEngine &engine)
 
bool operator!= (const HepRandomEngine &engine)
 
virtual double flat ()=0
 
virtual void flatArray (const int size, double *vect)=0
 
virtual void setSeed (long seed, int)=0
 
virtual void setSeeds (const long *seeds, int)=0
 
virtual void saveStatus (const char filename[]="Config.conf") const =0
 
virtual void restoreStatus (const char filename[]="Config.conf")=0
 
virtual void showStatus () const =0
 
virtual std::string name () const =0
 
virtual std::ostream & put (std::ostream &os) const
 
virtual std::istream & get (std::istream &is)
 
virtual std::istream & getState (std::istream &is)
 
virtual std::vector< unsigned long > put () const
 
virtual bool get (const std::vector< unsigned long > &v)
 
virtual bool getState (const std::vector< unsigned long > &v)
 
long getSeed () const
 
const long * getSeeds () const
 
virtual operator double ()
 
virtual operator float ()
 
virtual operator unsigned int ()
 

Static Public Member Functions

static std::string beginTag ()
 
static std::string engineName ()
 
- Static Public Member Functions inherited from CLHEP::HepRandomEngine
static std::string beginTag ()
 
static HepRandomEnginenewEngine (std::istream &is)
 
static HepRandomEnginenewEngine (const std::vector< unsigned long > &v)
 

Additional Inherited Members

- Static Protected Member Functions inherited from CLHEP::HepRandomEngine
static double exponent_bit_32 ()
 
static double mantissa_bit_12 ()
 
static double mantissa_bit_24 ()
 
static double mantissa_bit_32 ()
 
static double twoToMinus_32 ()
 
static double twoToMinus_48 ()
 
static double twoToMinus_49 ()
 
static double twoToMinus_53 ()
 
static double nearlyTwoToMinus_54 ()
 
static bool checkFile (std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
 
- Protected Attributes inherited from CLHEP::HepRandomEngine
long theSeed
 
const long * theSeeds
 

Detailed Description

Definition at line 50 of file MixMaxRng.h.

Constructor & Destructor Documentation

◆ MixMaxRng() [1/4]

CLHEP::MixMaxRng::MixMaxRng ( std::istream &  is)

Definition at line 69 of file MixMaxRng.cc.

71{
72 get(is);
73}
virtual std::istream & get(std::istream &is)
Definition: MixMaxRng.cc:384

◆ MixMaxRng() [2/4]

CLHEP::MixMaxRng::MixMaxRng ( )

Definition at line 55 of file MixMaxRng.cc.

57{
58 int numEngines = ++numberOfEngines;
59 setSeed(static_cast<long>(numEngines));
60}
void setSeed(long seed, int dum=0)
Definition: MixMaxRng.cc:214

◆ MixMaxRng() [3/4]

CLHEP::MixMaxRng::MixMaxRng ( long  seed)

Definition at line 62 of file MixMaxRng.cc.

64{
65 theSeed=seed;
66 setSeed(seed);
67}

◆ ~MixMaxRng()

CLHEP::MixMaxRng::~MixMaxRng ( )

Definition at line 75 of file MixMaxRng.cc.

76{
77}

◆ MixMaxRng() [4/4]

CLHEP::MixMaxRng::MixMaxRng ( const MixMaxRng rng)

Definition at line 79 of file MixMaxRng.cc.

80 : HepRandomEngine(rng)
81{
82 S.V = rng.S.V;
83 S.sumtot= rng.S.sumtot;
84 S.counter= rng.S.counter;
85}

Member Function Documentation

◆ beginTag()

std::string CLHEP::MixMaxRng::beginTag ( )
static

Definition at line 402 of file MixMaxRng.cc.

403{
404 return "MixMaxRng-begin";
405}

◆ engineName()

std::string CLHEP::MixMaxRng::engineName ( )
static

Definition at line 252 of file MixMaxRng.cc.

253{
254 return "MixMaxRng";
255}

◆ flat()

double CLHEP::MixMaxRng::flat ( )
inlinevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 66 of file MixMaxRng.h.

66{ return (S.counter<=(N-1)) ? generate(S.counter):iterate(); }

Referenced by flatArray().

◆ flatArray()

void CLHEP::MixMaxRng::flatArray ( const int  size,
double *  vect 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 326 of file MixMaxRng.cc.

327{
328 // fill_array( S, size, arrayDbl );
329 for (int i=0; i<size; ++i) { vect[i] = flat(); }
330}
double flat()
Definition: MixMaxRng.h:66

◆ get() [1/2]

bool CLHEP::MixMaxRng::get ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 440 of file MixMaxRng.cc.

441{
442 if ((v[0] & 0xffffffffUL) != engineIDulong<MixMaxRng>()) {
443 std::cerr <<
444 "\nMixMaxRng::get(): vector has wrong ID word - state unchanged\n";
445 return false;
446 }
447 return getState(v);
448}
virtual std::istream & getState(std::istream &is)
Definition: MixMaxRng.cc:407

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 384 of file MixMaxRng.cc.

385{
386 char beginMarker [MarkerLen];
387 is >> std::ws;
388 is.width(MarkerLen); // causes the next read to the char* to be <=
389 // that many bytes, INCLUDING A TERMINATION \0
390 // (Stroustrup, section 21.3.2)
391 is >> beginMarker;
392 if (strcmp(beginMarker,"MixMaxRng-begin")) {
393 is.clear(std::ios::badbit | is.rdstate());
394 std::cerr << "\nInput stream mispositioned or"
395 << "\nMixMaxRng state description missing or"
396 << "\nwrong engine type found." << std::endl;
397 return is;
398 }
399 return getState(is);
400}

Referenced by MixMaxRng().

◆ getState() [1/2]

bool CLHEP::MixMaxRng::getState ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 450 of file MixMaxRng.cc.

451{
452 if (v.size() != VECTOR_STATE_SIZE ) {
453 std::cerr <<
454 "\nMixMaxRng::getState(): vector has wrong length - state unchanged\n";
455 return false;
456 }
457 for (int i=1; i<2*rng_get_N() ; i=i+2) {
458 S.V[i/2]= ( (v[i] & MASK32) | ( (myuint_t)(v[i+1]) << 32 ) );
459 // unpack from a data structure which is 32-bit on some platforms
460 }
461 S.counter = v[2*rng_get_N()+1];
462 precalc();
463 if ( ( (v[2*rng_get_N()+2] & MASK32)
464 | ( (myuint_t)(v[2*rng_get_N()+3]) << 32 ) ) != S.sumtot) {
465 std::cerr << "\nMixMaxRng::getState(): vector has wrong checksum!"
466 << "\nInput vector is probably mispositioned now.\n";
467 return false;
468 }
469 return true;
470}
const unsigned long MASK32
Definition: MixMaxRng.cc:44
unsigned long long int myuint_t
Definition: MixMaxRng.h:48

◆ getState() [2/2]

std::istream & CLHEP::MixMaxRng::getState ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 407 of file MixMaxRng.cc.

408{
409 char endMarker[MarkerLen];
410 is >> theSeed;
411 for (int i=0; i<rng_get_N(); ++i) is >> S.V[i];
412 is >> S.counter;
413 myuint_t checksum;
414 is >> checksum;
415 is >> std::ws;
416 is.width(MarkerLen);
417 is >> endMarker;
418 if (strcmp(endMarker,"MixMaxRng-end")) {
419 is.clear(std::ios::badbit | is.rdstate());
420 std::cerr << "\nMixMaxRng state description incomplete."
421 << "\nInput stream is probably mispositioned now.\n";
422 return is;
423 }
424 if ( S.counter < 0 || S.counter > rng_get_N() ) {
425 std::cerr << "\nMixMaxRng::getState(): "
426 << "vector read wrong value of counter from file!"
427 << "\nInput stream is probably mispositioned now.\n";
428 return is;
429 }
430 precalc();
431 if ( checksum != S.sumtot) {
432 std::cerr << "\nMixMaxRng::getState(): "
433 << "checksum disagrees with value stored in file!"
434 << "\nInput stream is probably mispositioned now.\n";
435 return is;
436 }
437 return is;
438}

Referenced by get().

◆ name()

std::string CLHEP::MixMaxRng::name ( ) const
inlinevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 105 of file MixMaxRng.h.

105{ return "MixMaxRng"; }

◆ operator double()

CLHEP::MixMaxRng::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 332 of file MixMaxRng.cc.

333{
334 return flat();
335}

◆ operator float()

CLHEP::MixMaxRng::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 337 of file MixMaxRng.cc.

338{
339 return float( flat() );
340}

◆ operator unsigned int()

CLHEP::MixMaxRng::operator unsigned int ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 342 of file MixMaxRng.cc.

343{
344 return static_cast<unsigned int>(get_next());
345 // clhep_get_next returns a 64-bit integer, of which the lower 61 bits
346 // are random and upper 3 bits are zero
347}

◆ operator=()

MixMaxRng & CLHEP::MixMaxRng::operator= ( const MixMaxRng rng)

Definition at line 87 of file MixMaxRng.cc.

88{
89 // Check assignment to self
90 //
91 if (this == &rng) { return *this; }
92
93 // Copy base class data
94 //
95 HepRandomEngine::operator=(rng);
96
97 S.V = rng.S.V;
98 S.sumtot= rng.S.sumtot;
99 S.counter= rng.S.counter;
100
101 return *this;
102}

◆ put() [1/2]

std::vector< unsigned long > CLHEP::MixMaxRng::put ( ) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 367 of file MixMaxRng.cc.

368{
369 std::vector<unsigned long> v;
370 v.push_back (engineIDulong<MixMaxRng>());
371 for (int i=0; i<rng_get_N(); ++i)
372 {
373 v.push_back(static_cast<unsigned long>(S.V[i] & MASK32));
374 // little-ended order on all platforms
375 v.push_back(static_cast<unsigned long>(S.V[i] >> 32 ));
376 // pack uint64 into a data structure which is 32-bit on some platforms
377 }
378 v.push_back(static_cast<unsigned long>(S.counter));
379 v.push_back(static_cast<unsigned long>(S.sumtot & MASK32));
380 v.push_back(static_cast<unsigned long>(S.sumtot >> 32));
381 return v;
382}

◆ put() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 349 of file MixMaxRng.cc.

350{
351 char beginMarker[] = "MixMaxRng-begin";
352 char endMarker[] = "MixMaxRng-end";
353
354 int pr = os.precision(24);
355 os << beginMarker << " ";
356 os << theSeed << "\n";
357 for (int i=0; i<rng_get_N(); ++i) {
358 os << S.V[i] << "\n";
359 }
360 os << S.counter << "\n";
361 os << S.sumtot << "\n";
362 os << endMarker << "\n";
363 os.precision(pr);
364 return os;
365}

◆ restoreStatus()

void CLHEP::MixMaxRng::restoreStatus ( const char  filename[] = "MixMaxRngState.conf")
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 124 of file MixMaxRng.cc.

125{
126 // a function for reading the state from a file
127 FILE* fin;
128 if( ( fin = fopen(filename, "r") ) )
129 {
130 char l=0;
131 while ( l != '{' ) { // 0x7B = "{"
132 l=fgetc(fin); // proceed until hitting opening bracket
133 }
134 ungetc(' ', fin);
135 }
136 else
137 {
138 fprintf(stderr, "mixmax -> read_state: error reading file %s\n", filename);
139 throw std::runtime_error("Error in reading state file");
140 }
141
142 myuint_t vecVal;
143 //printf("mixmax -> read_state: starting to read state from file\n");
144 if (!fscanf(fin, "%llu", &S.V[0]) )
145 {
146 fprintf(stderr, "mixmax -> read_state: error reading file %s\n", filename);
147 throw std::runtime_error("Error in reading state file");
148 }
149
150 int i;
151 for( i = 1; i < rng_get_N(); i++)
152 {
153 if (!fscanf(fin, ", %llu", &vecVal) )
154 {
155 fprintf(stderr, "mixmax -> read_state: error reading vector component i=%d from file %s\n", i, filename);
156 throw std::runtime_error("Error in reading state file");
157 }
158 if( vecVal <= MixMaxRng::M61 )
159 {
160 S.V[i] = vecVal;
161 }
162 else
163 {
164 fprintf(stderr, "mixmax -> read_state: Invalid state vector value= %llu"
165 " ( must be less than %llu ) "
166 " obtained from reading file %s\n"
167 , vecVal, MixMaxRng::M61, filename);
168 }
169 }
170
171 int counter;
172 if (!fscanf( fin, "}; counter=%i; ", &counter))
173 {
174 fprintf(stderr, "mixmax -> read_state: error reading counter from file %s\n", filename);
175 throw std::runtime_error("Error in reading state file");
176 }
177 if( counter <= rng_get_N() )
178 {
179 S.counter= counter;
180 }
181 else
182 {
183 fprintf(stderr, "mixmax -> read_state: Invalid counter = %d"
184 " Must be 0 <= counter < %u\n" , counter, rng_get_N());
185 print_state();
186 throw std::runtime_error("Error in reading state counter");
187 }
188 precalc();
189 myuint_t sumtot;
190 if (!fscanf( fin, "sumtot=%llu\n", &sumtot))
191 {
192 fprintf(stderr, "mixmax -> read_state: error reading checksum from file %s\n", filename);
193 throw std::runtime_error("Error in reading state file");
194 }
195
196 if (S.sumtot != sumtot)
197 {
198 fprintf(stderr, "mixmax -> checksum error while reading state from file %s - corrupted?\n", filename);
199 throw std::runtime_error("Error in reading state checksum");
200 }
201 fclose(fin);
202}

◆ saveStatus()

void CLHEP::MixMaxRng::saveStatus ( const char  filename[] = "MixMaxRngState.conf") const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 104 of file MixMaxRng.cc.

105{
106 // Create a C file-handle or an object that can accept the same output
107 FILE *fh= fopen(filename, "w");
108 if( fh )
109 {
110 int j;
111 fprintf(fh, "mixmax state, file version 1.0\n" );
112 fprintf(fh, "N=%u; V[N]={", rng_get_N() );
113 for (j=0; (j< (rng_get_N()-1) ); j++) {
114 fprintf(fh, "%llu, ", S.V[j] );
115 }
116 fprintf(fh, "%llu", S.V[rng_get_N()-1] );
117 fprintf(fh, "}; " );
118 fprintf(fh, "counter=%u; ", S.counter );
119 fprintf(fh, "sumtot=%llu;\n", S.sumtot );
120 fclose(fh);
121 }
122}

◆ setSeed()

void CLHEP::MixMaxRng::setSeed ( long  seed,
int  dum = 0 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 214 of file MixMaxRng.cc.

215{
216 //seed_uniquestream(0,0,0,longSeed);
217 theSeed = longSeed;
218 seed_spbox(longSeed);
219}

Referenced by MixMaxRng().

◆ setSeeds()

void CLHEP::MixMaxRng::setSeeds ( const long *  seeds,
int  seedNum = 0 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 225 of file MixMaxRng.cc.

226{
227 myID_t seed0, seed1= 0, seed2= 0, seed3= 0;
228
229 if( seedNum < 1 ) { // Assuming at least 2 seeds in vector...
230 seed0= static_cast<myID_t>(Seeds[0]) & MASK32;
231 seed1= static_cast<myID_t>(Seeds[1]) & MASK32;
232 }
233 else
234 {
235 if( seedNum < 4 ) {
236 seed0= static_cast<myID_t>(Seeds[0]) & MASK32;
237 if( seedNum > 1){ seed1= static_cast<myID_t>(Seeds[1]) & MASK32; }
238 if( seedNum > 2){ seed2= static_cast<myID_t>(Seeds[2]) & MASK32; }
239 }
240 if( seedNum >= 4 ) {
241 seed0= static_cast<myID_t>(Seeds[0]) & MASK32;
242 seed1= static_cast<myID_t>(Seeds[1]) & MASK32;
243 seed2= static_cast<myID_t>(Seeds[2]) & MASK32;
244 seed3= static_cast<myID_t>(Seeds[3]) & MASK32;
245 }
246 }
247 theSeed = Seeds[0];
248 theSeeds = Seeds;
249 seed_uniquestream(seed3, seed2, seed1, seed0);
250}
std::uint32_t myID_t
Definition: MixMaxRng.h:47

◆ showStatus()

void CLHEP::MixMaxRng::showStatus ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 204 of file MixMaxRng.cc.

205{
206 std::cout << std::endl;
207 std::cout << "------- MixMaxRng engine status -------" << std::endl;
208
209 std::cout << " Current state vector is:" << std::endl;
210 print_state();
211 std::cout << "---------------------------------------" << std::endl;
212}

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