CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
CLHEP::RandEngine Class Reference

#include <RandEngine.h>

+ Inheritance diagram for CLHEP::RandEngine:

Public Member Functions

 RandEngine (std::istream &is)
 
 RandEngine ()
 
 RandEngine (long seed)
 
 RandEngine (int rowIndex, int colIndex)
 
virtual ~RandEngine ()
 
double flat ()
 
void flatArray (const int size, double *vect)
 
void setSeed (long seed, int dum=0)
 
void setSeeds (const long *seeds, int dum=0)
 
void saveStatus (const char filename[]="Rand.conf") const
 
void restoreStatus (const char filename[]="Rand.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)
 

Static Public Attributes

static const unsigned int VECTOR_STATE_SIZE = 3
 

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

Constructor & Destructor Documentation

◆ RandEngine() [1/4]

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

Definition at line 122 of file RandEngine.cc.

124{
125 is >> *this;
126}

◆ RandEngine() [2/4]

CLHEP::RandEngine::RandEngine ( )

Definition at line 104 of file RandEngine.cc.

106{
107 long seeds[2];
108 long seed;
109 int cycle,curIndex;
110
111 cycle = std::abs(int(numEngines/maxIndex));
112 curIndex = std::abs(int(numEngines%maxIndex));
113 numEngines += 1;
114 long mask = ((cycle & 0x007fffff) << 8);
115 HepRandom::getTheTableSeeds( seeds, curIndex );
116 seed = seeds[0]^mask;
117 setSeed(seed,0);
118 setSeeds(&theSeed,0);
119 seq = 0;
120}
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:256
void setSeed(long seed, int dum=0)
Definition: RandEngine.cc:130
void setSeeds(const long *seeds, int dum=0)
Definition: RandEngine.cc:137

◆ RandEngine() [3/4]

CLHEP::RandEngine::RandEngine ( long  seed)

Definition at line 79 of file RandEngine.cc.

81{
82 setSeed(seed,0);
83 setSeeds(&theSeed,0);
84 seq = 0;
85}

◆ RandEngine() [4/4]

CLHEP::RandEngine::RandEngine ( int  rowIndex,
int  colIndex 
)

Definition at line 87 of file RandEngine.cc.

89{
90 long seeds[2];
91 long seed;
92
93 int cycle = std::abs(int(rowIndex/maxIndex));
94 int row = std::abs(int(rowIndex%maxIndex));
95 int col = std::abs(int(colIndex%2));
96 long mask = ((cycle & 0x000007ff) << 20 );
97 HepRandom::getTheTableSeeds( seeds, row );
98 seed = (seeds[col])^mask;
99 setSeed(seed,0);
100 setSeeds(&theSeed,0);
101 seq = 0;
102}

◆ ~RandEngine()

CLHEP::RandEngine::~RandEngine ( )
virtual

Definition at line 128 of file RandEngine.cc.

128{}

Member Function Documentation

◆ beginTag()

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

Definition at line 425 of file RandEngine.cc.

425 {
426 return "RandEngine-begin";
427}

◆ engineName()

static std::string CLHEP::RandEngine::engineName ( )
inlinestatic

Definition at line 103 of file RandEngine.h.

103{return "RandEngine";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::RandEngine::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 356 of file RandEngine.cc.

357{
358 double r;
360 } while ( r == 0 );
361 return r/4294967296.0;
362}
static unsigned int thirtyTwoRandomBits(long &seq)
Definition: RandEngine.cc:246

Referenced by flatArray(), getState(), and restoreStatus().

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 364 of file RandEngine.cc.

365{
366 int i;
367
368 for (i=0; i<size; ++i)
369 vect[i]=flat();
370}

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 468 of file RandEngine.cc.

468 {
469 if ((v[0] & 0xffffffffUL) != engineIDulong<RandEngine>()) {
470 std::cerr <<
471 "\nRandEngine get:state vector has wrong ID word - state unchanged\n";
472 return false;
473 }
474 return getState(v);
475}
virtual std::istream & getState(std::istream &is)
Definition: RandEngine.cc:429

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 403 of file RandEngine.cc.

404{
405 // The only way to restore the status of RandEngine is to
406 // keep track of the number of shooted random sequences, reset
407 // the engine and re-shoot them again. The Rand algorithm does
408 // not provide any way of getting its internal status.
409 char beginMarker [MarkerLen];
410 is >> std::ws;
411 is.width(MarkerLen); // causes the next read to the char* to be <=
412 // that many bytes, INCLUDING A TERMINATION \0
413 // (Stroustrup, section 21.3.2)
414 is >> beginMarker;
415 if (strcmp(beginMarker,"RandEngine-begin")) {
416 is.clear(std::ios::badbit | is.rdstate());
417 std::cout << "\nInput stream mispositioned or"
418 << "\nRandEngine state description missing or"
419 << "\nwrong engine type found." << std::endl;
420 return is;
421 }
422 return getState(is);
423}

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 477 of file RandEngine.cc.

477 {
478 if (v.size() != VECTOR_STATE_SIZE ) {
479 std::cerr <<
480 "\nRandEngine get:state vector has wrong length - state unchanged\n";
481 return false;
482 }
483 theSeed = v[1];
484 int count = (int)v[2];
485 setSeed(theSeed,0);
486 while (seq < count) flat();
487 return true;
488}
static const unsigned int VECTOR_STATE_SIZE
Definition: RandEngine.h:109

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 429 of file RandEngine.cc.

430{
431 if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
432 std::vector<unsigned long> v;
433 unsigned long uu;
434 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
435 is >> uu;
436 if (!is) {
437 is.clear(std::ios::badbit | is.rdstate());
438 std::cerr << "\nRandEngine state (vector) description improper."
439 << "\ngetState() has failed."
440 << "\nInput stream is probably mispositioned now." << std::endl;
441 return is;
442 }
443 v.push_back(uu);
444 }
445 getState(v);
446 return (is);
447 }
448
449// is >> theSeed; Removed, encompassed by possibleKeywordInput()
450
451 char endMarker [MarkerLen];
452 long count;
453 is >> count;
454 is >> std::ws;
455 is.width(MarkerLen);
456 is >> endMarker;
457 if (strcmp(endMarker,"RandEngine-end")) {
458 is.clear(std::ios::badbit | is.rdstate());
459 std::cerr << "\nRandEngine state description incomplete."
460 << "\nInput stream is probably mispositioned now." << std::endl;
461 return is;
462 }
463 setSeed(theSeed,0);
464 while (seq < count) flat();
465 return is;
466}
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168

Referenced by get(), getState(), and restoreStatus().

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 77 of file RandEngine.cc.

77{return "RandEngine";}

◆ operator double()

CLHEP::RandEngine::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 372 of file RandEngine.cc.

372 {
373 return flat();
374}

◆ operator float()

CLHEP::RandEngine::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 376 of file RandEngine.cc.

376 {
377 return float( flat() );
378}

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 380 of file RandEngine.cc.

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 395 of file RandEngine.cc.

395 {
396 std::vector<unsigned long> v;
397 v.push_back (engineIDulong<RandEngine>());
398 v.push_back(static_cast<unsigned long>(theSeed));
399 v.push_back(static_cast<unsigned long>(seq));
400 return v;
401}

Referenced by saveStatus().

◆ put() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 384 of file RandEngine.cc.

385{
386 char beginMarker[] = "RandEngine-begin";
387 char endMarker[] = "RandEngine-end";
388
389 os << " " << beginMarker << "\n";
390 os << theSeed << " " << seq << " ";
391 os << endMarker << "\n";
392 return os;
393}

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 172 of file RandEngine.cc.

173{
174 // The only way to restore the status of RandEngine is to
175 // keep track of the number of shooted random sequences, reset
176 // the engine and re-shoot them again. The Rand algorithm does
177 // not provide any way of getting its internal status.
178
179 std::ifstream inFile( filename, std::ios::in);
180 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
181 std::cout << " -- Engine state remains unchanged\n";
182 return;
183 }
184 if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
185 std::vector<unsigned long> v;
186 unsigned long xin;
187 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
188 inFile >> xin;
189 #ifdef TRACE_IO
190 std::cout << "ivec = " << ivec << " xin = " << xin << " ";
191 if (ivec%3 == 0) std::cout << "\n";
192 #endif
193 if (!inFile) {
194 inFile.clear(std::ios::badbit | inFile.rdstate());
195 std::cerr << "\nRandEngine state (vector) description improper."
196 << "\nrestoreStatus has failed."
197 << "\nInput stream is probably mispositioned now." << std::endl;
198 return;
199 }
200 v.push_back(xin);
201 }
202 getState(v);
203 return;
204 }
205
206 long count;
207
208 if (!inFile.bad() && !inFile.eof()) {
209// inFile >> theSeed; removed -- encompased by possibleKeywordInput
210 inFile >> count;
211 setSeed(theSeed,0);
212 seq = 0;
213 while (seq < count) flat();
214 }
215}
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:49
static std::string engineName()
Definition: RandEngine.h:103

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 143 of file RandEngine.cc.

144{
145 std::ofstream outFile( filename, std::ios::out ) ;
146
147 if (!outFile.bad()) {
148 outFile << "Uvec\n";
149 std::vector<unsigned long> v = put();
150 #ifdef TRACE_IO
151 std::cout << "Result of v = put() is:\n";
152 #endif
153 for (unsigned int i=0; i<v.size(); ++i) {
154 outFile << v[i] << "\n";
155 #ifdef TRACE_IO
156 std::cout << v[i] << " ";
157 if (i%6==0) std::cout << "\n";
158 #endif
159 }
160 #ifdef TRACE_IO
161 std::cout << "\n";
162 #endif
163 }
164#ifdef REMOVED
165 if (!outFile.bad()) {
166 outFile << theSeed << std::endl;
167 outFile << seq << std::endl;
168 }
169#endif
170}
std::vector< unsigned long > put() const
Definition: RandEngine.cc:395

◆ setSeed()

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

Implements CLHEP::HepRandomEngine.

Definition at line 130 of file RandEngine.cc.

131{
132 theSeed = seed;
133 srand( int(seed) );
134 seq = 0;
135}

Referenced by getState(), RandEngine(), restoreStatus(), and setSeeds().

◆ setSeeds()

void CLHEP::RandEngine::setSeeds ( const long *  seeds,
int  dum = 0 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 137 of file RandEngine.cc.

138{
139 setSeed(seeds ? *seeds : 19780503L, 0);
140 theSeeds = seeds;
141}

Referenced by RandEngine().

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 217 of file RandEngine.cc.

218{
219 std::cout << std::endl;
220 std::cout << "---------- Rand engine status ----------" << std::endl;
221 std::cout << " Initial seed = " << theSeed << std::endl;
222 std::cout << " Shooted sequences = " << seq << std::endl;
223 std::cout << "----------------------------------------" << std::endl;
224}

Member Data Documentation

◆ VECTOR_STATE_SIZE

const unsigned int CLHEP::RandEngine::VECTOR_STATE_SIZE = 3
static

Definition at line 109 of file RandEngine.h.

Referenced by getState(), and restoreStatus().


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