Geant4 11.2.2
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 longSeed, int=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 > &vec)
 
bool getState (const std::vector< unsigned long > &vec)
 
- Public Member Functions inherited from CLHEP::HepRandomEngine
 HepRandomEngine ()
 
virtual ~HepRandomEngine ()
 
bool operator== (const HepRandomEngine &engine)
 
bool operator!= (const HepRandomEngine &engine)
 
long getSeed () const
 
const long * getSeeds () const
 

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 52 of file MixMaxRng.h.

Constructor & Destructor Documentation

◆ MixMaxRng() [1/4]

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

Definition at line 72 of file MixMaxRng.cc.

74{
75 get(is);
76}
virtual std::istream & get(std::istream &is)
Definition MixMaxRng.cc:297

◆ MixMaxRng() [2/4]

CLHEP::MixMaxRng::MixMaxRng ( )

Definition at line 58 of file MixMaxRng.cc.

60{
61 int numEngines = ++numberOfEngines;
62 setSeed(static_cast<long>(numEngines));
63}
void setSeed(long longSeed, int=0)
Definition MixMaxRng.h:81

◆ MixMaxRng() [3/4]

CLHEP::MixMaxRng::MixMaxRng ( long seed)

Definition at line 65 of file MixMaxRng.cc.

67{
68 theSeed=seed;
69 setSeed(seed);
70}

◆ ~MixMaxRng()

CLHEP::MixMaxRng::~MixMaxRng ( )

Definition at line 78 of file MixMaxRng.cc.

79{
80}

◆ MixMaxRng() [4/4]

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

Definition at line 82 of file MixMaxRng.cc.

83 : HepRandomEngine(rng)
84{
85 std::copy(rng.V, rng.V+N, V);
86 sumtot= rng.sumtot;
87 counter= rng.counter;
88}

Member Function Documentation

◆ beginTag()

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

Definition at line 315 of file MixMaxRng.cc.

316{
317 return "MixMaxRng-begin";
318}

◆ engineName()

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

Definition at line 247 of file MixMaxRng.cc.

248{
249 return "MixMaxRng";
250}

◆ flat()

double CLHEP::MixMaxRng::flat ( )
inlinevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 69 of file MixMaxRng.h.

70 {
71 if (counter >= N) iterate();
72 return INV_M61*static_cast<double>(V[counter++]);
73 }

Referenced by flatArray(), operator double(), and operator float().

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 257 of file MixMaxRng.cc.

258{
259 for (int i=0; i<size; ++i) { vect[i] = flat(); }
260}
double flat()
Definition MixMaxRng.h:69

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 353 of file MixMaxRng.cc.

354{
355 if ((vec[0] & 0xffffffffUL) != engineIDulong<MixMaxRng>())
356 {
357 std::cerr <<
358 "\nMixMaxRng::get(): vector has wrong ID word - state unchanged\n";
359 return false;
360 }
361 return getState(vec);
362}
virtual std::istream & getState(std::istream &is)
Definition MixMaxRng.cc:320
unsigned long engineIDulong()

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 297 of file MixMaxRng.cc.

298{
299 char beginMarker [MarkerLen];
300 is >> std::ws;
301 is.width(MarkerLen); // causes the next read to the char* to be <=
302 // that many bytes, INCLUDING A TERMINATION \0
303 // (Stroustrup, section 21.3.2)
304 is >> beginMarker;
305 if (strcmp(beginMarker,"MixMaxRng-begin")) {
306 is.clear(std::ios::badbit | is.rdstate());
307 std::cerr << "\nInput stream mispositioned or"
308 << "\nMixMaxRng state description missing or"
309 << "\nwrong engine type found." << std::endl;
310 return is;
311 }
312 return getState(is);
313}

Referenced by MixMaxRng().

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 364 of file MixMaxRng.cc.

365{
366 if (vec.size() != VECTOR_STATE_SIZE ) {
367 std::cerr <<
368 "\nMixMaxRng::getState(): vector has wrong length - state unchanged\n";
369 return false;
370 }
371 for (int i=1; i<2*rng_get_N() ; i=i+2) {
372 V[i/2]= ( (vec[i] & MASK32) | ( (myuint_t)(vec[i+1]) << 32 ) );
373 // unpack from a data structure which is 32-bit on some platforms
374 }
375 counter = (int)vec[2*rng_get_N()+1];
376 precalc();
377 if ( ( (vec[2*rng_get_N()+2] & MASK32)
378 | ( (myuint_t)(vec[2*rng_get_N()+3]) << 32 ) ) != sumtot) {
379 std::cerr << "\nMixMaxRng::getState(): vector has wrong checksum!"
380 << "\nInput vector is probably mispositioned now.\n";
381 return false;
382 }
383 return true;
384}
const unsigned long MASK32
Definition MixMaxRng.cc:47
std::uint64_t myuint_t
Definition MixMaxRng.h:50

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 320 of file MixMaxRng.cc.

321{
322 char endMarker[MarkerLen];
323 is >> theSeed;
324 for (int i=0; i<rng_get_N(); ++i) is >> V[i];
325 is >> counter;
326 myuint_t checksum;
327 is >> checksum;
328 is >> std::ws;
329 is.width(MarkerLen);
330 is >> endMarker;
331 if (strcmp(endMarker,"MixMaxRng-end")) {
332 is.clear(std::ios::badbit | is.rdstate());
333 std::cerr << "\nMixMaxRng state description incomplete."
334 << "\nInput stream is probably mispositioned now.\n";
335 return is;
336 }
337 if ( counter < 0 || counter > rng_get_N() ) {
338 std::cerr << "\nMixMaxRng::getState(): "
339 << "vector read wrong value of counter from file!"
340 << "\nInput stream is probably mispositioned now.\n";
341 return is;
342 }
343 precalc();
344 if ( checksum != sumtot) {
345 std::cerr << "\nMixMaxRng::getState(): "
346 << "checksum disagrees with value stored in file!"
347 << "\nInput stream is probably mispositioned now.\n";
348 return is;
349 }
350 return is;
351}

Referenced by get(), and get().

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 118 of file MixMaxRng.h.

118{ return "MixMaxRng"; }

◆ operator double()

CLHEP::MixMaxRng::operator double ( )
inlinevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 103 of file MixMaxRng.h.

103{ return flat(); }

◆ operator float()

CLHEP::MixMaxRng::operator float ( )
inlinevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 106 of file MixMaxRng.h.

106{ return float( flat() ); }

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 109 of file MixMaxRng.h.

109{ return static_cast<unsigned int>(get_next()); }

◆ operator=()

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

Definition at line 90 of file MixMaxRng.cc.

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

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 280 of file MixMaxRng.cc.

281{
282 std::vector<unsigned long> vec;
283 vec.push_back (engineIDulong<MixMaxRng>());
284 for (int i=0; i<rng_get_N(); ++i)
285 {
286 vec.push_back(static_cast<unsigned long>(V[i] & MASK32));
287 // little-ended order on all platforms
288 vec.push_back(static_cast<unsigned long>(V[i] >> 32 ));
289 // pack uint64 into a data structure which is 32-bit on some platforms
290 }
291 vec.push_back(static_cast<unsigned long>(counter));
292 vec.push_back(static_cast<unsigned long>(sumtot & MASK32));
293 vec.push_back(static_cast<unsigned long>(sumtot >> 32));
294 return vec;
295}

◆ put() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 262 of file MixMaxRng.cc.

263{
264 char beginMarker[] = "MixMaxRng-begin";
265 char endMarker[] = "MixMaxRng-end";
266
267 long pr = os.precision(24);
268 os << beginMarker << " ";
269 os << theSeed << "\n";
270 for (int i=0; i<rng_get_N(); ++i) {
271 os << V[i] << "\n";
272 }
273 os << counter << "\n";
274 os << sumtot << "\n";
275 os << endMarker << "\n";
276 os.precision(pr);
277 return os;
278}

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 127 of file MixMaxRng.cc.

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

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 107 of file MixMaxRng.cc.

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

◆ setSeed()

void CLHEP::MixMaxRng::setSeed ( long longSeed,
int = 0 )
inlinevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 81 of file MixMaxRng.h.

82 {
83 seed_spbox(theSeed = longSeed);
84 }

Referenced by MixMaxRng(), and MixMaxRng().

◆ setSeeds()

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

Implements CLHEP::HepRandomEngine.

Definition at line 220 of file MixMaxRng.cc.

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

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 206 of file MixMaxRng.cc.

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

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