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

#include <RanshiEngine.h>

+ Inheritance diagram for CLHEP::RanshiEngine:

Public Member Functions

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

Author

Definition at line 53 of file RanshiEngine.h.

Constructor & Destructor Documentation

◆ RanshiEngine() [1/4]

CLHEP::RanshiEngine::RanshiEngine ( )

Definition at line 67 of file RanshiEngine.cc.

69 halfBuff(0), numFlats(0)
70{
71 int numEngines = numberOfEngines++;
72 int i = 0;
73 while (i < numBuff) {
74 buffer[i] = (unsigned int)((numEngines+19780503L*(i+1))& 0xffffffff);
75 ++i;
76 }
77 theSeed = numEngines+19780503L*++i;
78 redSpin = (unsigned int)(theSeed & 0xffffffff);
79
80 for( i = 0; i < 10000; ++i) flat(); // Warm-up by running thorugh 10000 nums
81}

Referenced by setSeed().

◆ RanshiEngine() [2/4]

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

Definition at line 83 of file RanshiEngine.cc.

85 halfBuff(0), numFlats(0)
86{
87 is >> *this;
88}

◆ RanshiEngine() [3/4]

CLHEP::RanshiEngine::RanshiEngine ( long  seed)

Definition at line 90 of file RanshiEngine.cc.

92 halfBuff(0), numFlats(0)
93{
94 for (int i = 0; i < numBuff; ++i) {
95 buffer[i] = (unsigned int)seed&0xffffffff;
96 }
97 theSeed = seed;
98 redSpin = (unsigned int)(theSeed & 0xffffffff);
99 int j;
100 for (j = 0; j < numBuff*20; ++j) { // "warm-up" for engine to hit
101 flat(); // every ball on average 20X.
102 }
103}

◆ RanshiEngine() [4/4]

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

Definition at line 105 of file RanshiEngine.cc.

107 halfBuff(0), numFlats(0)
108{
109 int i = 0;
110 while( i < numBuff ) {
111 buffer[i] = (unsigned int)((rowIndex + (i+1)*(colIndex+8))&0xffffffff);
112 ++i;
113 }
114 theSeed = rowIndex;
115 redSpin = colIndex & 0xffffffff;
116 for( i = 0; i < 100; ++i) flat(); // Warm-up by running thorugh 100 nums
117}

◆ ~RanshiEngine()

CLHEP::RanshiEngine::~RanshiEngine ( )
virtual

Definition at line 119 of file RanshiEngine.cc.

119{ }

Member Function Documentation

◆ beginTag()

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

Definition at line 329 of file RanshiEngine.cc.

329 {
330 return "RanshiEngine-begin";
331}

◆ engineName()

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

Definition at line 97 of file RanshiEngine.h.

97{return "RanshiEngine";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::RanshiEngine::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 121 of file RanshiEngine.cc.

121 {
122 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
123 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
124 unsigned int boostResult = blkSpin ^ redSpin;
125
126 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
127
128 redSpin = (blkSpin + numFlats++) & 0xffffffff;
129 halfBuff = numBuff/2 - halfBuff;
130
131 return ( blkSpin * twoToMinus_32() + // most significant part
132 (boostResult>>11) * twoToMinus_53() + // fill in remaining bits
133 nearlyTwoToMinus_54()); // non-zero
134}
static double twoToMinus_32()
static double twoToMinus_53()
static double nearlyTwoToMinus_54()

Referenced by flatArray(), main(), and RanshiEngine().

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 136 of file RanshiEngine.cc.

136 {
137 for (int i = 0; i < size; ++i) {
138 vect[i] = flat();
139 }
140}

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 371 of file RanshiEngine.cc.

371 {
372 if ((v[0] & 0xffffffffUL) != engineIDulong<RanshiEngine>()) {
373 std::cerr <<
374 "\nRanshiEngine get:state vector has wrong ID word - state unchanged\n";
375 return false;
376 }
377 return getState(v);
378}
virtual std::istream & getState(std::istream &is)

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 312 of file RanshiEngine.cc.

312 {
313 char beginMarker [MarkerLen];
314 is >> std::ws;
315 is.width(MarkerLen); // causes the next read to the char* to be <=
316 // that many bytes, INCLUDING A TERMINATION \0
317 // (Stroustrup, section 21.3.2)
318 is >> beginMarker;
319 if (strcmp(beginMarker,"RanshiEngine-begin")) {
320 is.clear(std::ios::badbit | is.rdstate());
321 std::cerr << "\nInput mispositioned or"
322 << "\nRanshiEngine state description missing or"
323 << "\nwrong engine type found." << std::endl;
324 return is;
325 }
326 return getState(is);
327}

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 380 of file RanshiEngine.cc.

380 {
381 if (v.size() != VECTOR_STATE_SIZE ) {
382 std::cerr <<
383 "\nRanshiEngine get:state vector has wrong length - state unchanged\n";
384 return false;
385 }
386 for (int i = 0; i < numBuff; ++i) {
387 buffer[i] = (unsigned int)v[i+1];
388 }
389 redSpin = (unsigned int)v[numBuff+1];
390 numFlats = (unsigned int)v[numBuff+2];
391 halfBuff = (unsigned int)v[numBuff+3];
392 return true;
393}

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 333 of file RanshiEngine.cc.

333 {
334 if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
335 std::vector<unsigned long> v;
336 unsigned long uu;
337 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
338 is >> uu;
339 if (!is) {
340 is.clear(std::ios::badbit | is.rdstate());
341 std::cerr << "\nRanshiEngine state (vector) description improper."
342 << "\ngetState() has failed."
343 << "\nInput stream is probably mispositioned now." << std::endl;
344 return is;
345 }
346 v.push_back(uu);
347 }
348 getState(v);
349 return (is);
350 }
351
352// is >> theSeed; Removed, encompassed by possibleKeywordInput()
353
354 char endMarker [MarkerLen];
355 for (int i = 0; i < numBuff; ++i) {
356 is >> buffer[i];
357 }
358 is >> redSpin >> numFlats >> halfBuff;
359 is >> std::ws;
360 is.width(MarkerLen);
361 is >> endMarker;
362 if (strcmp(endMarker,"RanshiEngine-end")) {
363 is.clear(std::ios::badbit | is.rdstate());
364 std::cerr << "\nRanshiEngine state description incomplete."
365 << "\nInput stream is probably mispositioned now." << std::endl;
366 return is;
367 }
368 return is;
369}
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168

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

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 65 of file RanshiEngine.cc.

65{return "RanshiEngine";}

◆ operator double()

CLHEP::RanshiEngine::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 247 of file RanshiEngine.cc.

247 {
248 return flat();
249}

◆ operator float()

CLHEP::RanshiEngine::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 251 of file RanshiEngine.cc.

251 {
252 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
253 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
254
255 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
256
257 redSpin = (blkSpin + numFlats++) & 0xffffffff;
258 halfBuff = numBuff/2 - halfBuff;
259
260 return float(blkSpin * twoToMinus_32());
261}

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 263 of file RanshiEngine.cc.

263 {
264 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
265 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
266
267 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
268
269 redSpin = (blkSpin + numFlats++) & 0xffffffff;
270 halfBuff = numBuff/2 - halfBuff;
271
272 return blkSpin;
273}

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 300 of file RanshiEngine.cc.

300 {
301 std::vector<unsigned long> v;
302 v.push_back (engineIDulong<RanshiEngine>());
303 for (int i = 0; i < numBuff; ++i) {
304 v.push_back(static_cast<unsigned long>(buffer[i]));
305 }
306 v.push_back(static_cast<unsigned long>(redSpin));
307 v.push_back(static_cast<unsigned long>(numFlats));
308 v.push_back(static_cast<unsigned long>(halfBuff));
309 return v;
310}

Referenced by put(), and saveStatus().

◆ put() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 275 of file RanshiEngine.cc.

275 {
276 char beginMarker[] = "RanshiEngine-begin";
277 os << beginMarker << "\nUvec\n";
278 std::vector<unsigned long> v = put();
279 for (unsigned int i=0; i<v.size(); ++i) {
280 os << v[i] << "\n";
281 }
282 return os;
283#ifdef REMOVED
284 char endMarker[] = "RanshiEngine-end";
285 long pr=os.precision(20);
286 os << " " << beginMarker << " ";
287
288 os << theSeed << "\n";
289 for (int i = 0; i < numBuff; ++i) {
290 os << buffer[i] << "\n";
291 }
292 os << redSpin << " " << numFlats << "\n" << halfBuff;
293
294 os << " " << endMarker << "\n";
295 os.precision(pr);
296 return os;
297#endif
298}
std::vector< unsigned long > put() const

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 193 of file RanshiEngine.cc.

193 {
194 std::ifstream inFile(filename, std::ios::in);
195 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
196 std::cerr << " -- Engine state remains unchanged\n";
197 return;
198 }
199 if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
200 std::vector<unsigned long> v;
201 unsigned long xin;
202 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
203 inFile >> xin;
204 #ifdef TRACE_IO
205 std::cout << "ivec = " << ivec << " xin = " << xin << " ";
206 if (ivec%3 == 0) std::cout << "\n";
207 #endif
208 if (!inFile) {
209 inFile.clear(std::ios::badbit | inFile.rdstate());
210 std::cerr << "\nRanshiEngine state (vector) description improper."
211 << "\nrestoreStatus has failed."
212 << "\nInput stream is probably mispositioned now." << std::endl;
213 return;
214 }
215 v.push_back(xin);
216 }
217 getState(v);
218 return;
219 }
220
221 if (!inFile.bad()) {
222// inFile >> theSeed; removed -- encompased by possibleKeywordInput
223 for (int i = 0; i < numBuff; ++i) {
224 inFile >> buffer[i];
225 }
226 inFile >> redSpin >> numFlats >> halfBuff;
227 }
228}
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: RanshiEngine.h:97

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 163 of file RanshiEngine.cc.

163 {
164 std::ofstream outFile(filename, std::ios::out);
165 if (!outFile.bad()) {
166 outFile << "Uvec\n";
167 std::vector<unsigned long> v = put();
168 #ifdef TRACE_IO
169 std::cout << "Result of v = put() is:\n";
170 #endif
171 for (unsigned int i=0; i<v.size(); ++i) {
172 outFile << v[i] << "\n";
173 #ifdef TRACE_IO
174 std::cout << v[i] << " ";
175 if (i%6==0) std::cout << "\n";
176 #endif
177 }
178 #ifdef TRACE_IO
179 std::cout << "\n";
180 #endif
181 }
182#ifdef REMOVED
183 if (!outFile.bad()) {
184 outFile << std::setprecision(20) << theSeed << std::endl;
185 for (int i = 0; i < numBuff; ++i) {
186 outFile << buffer[i] << " ";
187 }
188 outFile << redSpin << " " << numFlats << " " << halfBuff << std::endl;
189 }
190#endif
191}

◆ setSeed()

void CLHEP::RanshiEngine::setSeed ( long  seed,
int   
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 142 of file RanshiEngine.cc.

142 {
143 *this = RanshiEngine(seed);
144}

◆ setSeeds()

void CLHEP::RanshiEngine::setSeeds ( const long *  seeds,
int   
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 146 of file RanshiEngine.cc.

146 {
147 if (*seeds) {
148 int i = 0;
149 while (seeds[i] && i < numBuff) {
150 buffer[i] = (unsigned int)seeds[i];
151 ++i;
152 }
153 while (i < numBuff) {
154 buffer[i] = buffer[i-1];
155 ++i;
156 }
157 theSeed = seeds[0];
158 redSpin = (unsigned int)theSeed;
159 }
160 theSeeds = seeds;
161}

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 230 of file RanshiEngine.cc.

230 {
231 std::cout << std::setprecision(20) << std::endl;
232 std::cout << "----------- Ranshi engine status ----------" << std::endl;
233 std::cout << "Initial seed = " << theSeed << std::endl;
234 std::cout << "Current red spin = " << redSpin << std::endl;
235 std::cout << "Values produced = " << numFlats << std::endl;
236 std::cout << "Side of buffer = " << (halfBuff ? "upper" : "lower")
237 << std::endl;
238 std::cout << "Current buffer = " << std::endl;
239 for (int i = 0; i < numBuff; i+=4) {
240 std::cout << std::setw(10) << std::setiosflags(std::ios::right)
241 << buffer[i] << std::setw(11) << buffer[i+1] << std::setw(11)
242 << buffer[i+2] << std::setw(11) << buffer[i+3] << std::endl;
243 }
244 std::cout << "-------------------------------------------" << std::endl;
245}

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