CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
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 51 of file MixMaxRng.h.

Constructor & Destructor Documentation

◆ MixMaxRng() [1/4]

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

Definition at line 70 of file MixMaxRng.cc.

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

◆ MixMaxRng() [2/4]

CLHEP::MixMaxRng::MixMaxRng ( )

Definition at line 56 of file MixMaxRng.cc.

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

◆ MixMaxRng() [3/4]

CLHEP::MixMaxRng::MixMaxRng ( long  seed)

Definition at line 63 of file MixMaxRng.cc.

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

◆ ~MixMaxRng()

CLHEP::MixMaxRng::~MixMaxRng ( )

Definition at line 76 of file MixMaxRng.cc.

77{
78}

◆ MixMaxRng() [4/4]

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

Definition at line 80 of file MixMaxRng.cc.

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

Member Function Documentation

◆ beginTag()

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

Definition at line 403 of file MixMaxRng.cc.

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

◆ engineName()

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

Definition at line 253 of file MixMaxRng.cc.

254{
255 return "MixMaxRng";
256}

◆ flat()

double CLHEP::MixMaxRng::flat ( )
inlinevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 67 of file MixMaxRng.h.

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

Referenced by flatArray(), and main().

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 327 of file MixMaxRng.cc.

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

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 441 of file MixMaxRng.cc.

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

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 385 of file MixMaxRng.cc.

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

Referenced by MixMaxRng().

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 451 of file MixMaxRng.cc.

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

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 408 of file MixMaxRng.cc.

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

Referenced by get().

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 106 of file MixMaxRng.h.

106{ return "MixMaxRng"; }

◆ operator double()

CLHEP::MixMaxRng::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 333 of file MixMaxRng.cc.

334{
335 return flat();
336}

◆ operator float()

CLHEP::MixMaxRng::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 338 of file MixMaxRng.cc.

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

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 343 of file MixMaxRng.cc.

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

◆ operator=()

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

Definition at line 88 of file MixMaxRng.cc.

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

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 368 of file MixMaxRng.cc.

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

◆ put() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 350 of file MixMaxRng.cc.

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

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 125 of file MixMaxRng.cc.

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

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 105 of file MixMaxRng.cc.

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

◆ setSeed()

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

Implements CLHEP::HepRandomEngine.

Definition at line 215 of file MixMaxRng.cc.

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

Referenced by MixMaxRng().

◆ setSeeds()

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

Implements CLHEP::HepRandomEngine.

Definition at line 226 of file MixMaxRng.cc.

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

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 205 of file MixMaxRng.cc.

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

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