Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions
CLHEP::RanshiEngine Class Reference

#include <RanshiEngine.h>

Inheritance diagram for CLHEP::RanshiEngine:
CLHEP::HepRandomEngine

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 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)
 
long getSeed () const
 
const long * getSeeds () const
 
virtual operator double ()
 

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

Constructor & Destructor Documentation

CLHEP::RanshiEngine::RanshiEngine ( )

Definition at line 48 of file RanshiEngine.cc.

References flat(), int(), and CLHEP::HepRandomEngine::theSeed.

Referenced by setSeed().

49 : HepRandomEngine(),
50  halfBuff(0), numFlats(0)
51 {
52  int i = 0;
53  while (i < numBuff) {
54  buffer[i] = (unsigned int)(numEngines+19780503L*(i+1));
55  ++i;
56  }
57  theSeed = numEngines+19780503L*++i;
58  redSpin = (unsigned int)(theSeed & 0xffffffff);
59  ++numEngines;
60  for( i = 0; i < 10000; ++i) flat(); // Warm-up by running thorugh 10000 nums
61 }
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
CLHEP::RanshiEngine::RanshiEngine ( std::istream &  is)

Definition at line 63 of file RanshiEngine.cc.

64 : HepRandomEngine(),
65  halfBuff(0), numFlats(0)
66 {
67  is >> *this;
68 }
CLHEP::RanshiEngine::RanshiEngine ( long  seed)

Definition at line 70 of file RanshiEngine.cc.

References flat(), int(), and CLHEP::HepRandomEngine::theSeed.

71 : HepRandomEngine(),
72  halfBuff(0), numFlats(0)
73 {
74  for (int i = 0; i < numBuff; ++i) {
75  buffer[i] = (unsigned int)seed&0xffffffff;
76  }
77  theSeed = seed;
78  redSpin = (unsigned int)(theSeed & 0xffffffff);
79  int j;
80  for (j = 0; j < numBuff*20; ++j) { // "warm-up" for engine to hit
81  flat(); // every ball on average 20X.
82  }
83 }
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
CLHEP::RanshiEngine::RanshiEngine ( int  rowIndex,
int  colIndex 
)

Definition at line 85 of file RanshiEngine.cc.

References flat(), int(), and CLHEP::HepRandomEngine::theSeed.

86 : HepRandomEngine(),
87  halfBuff(0), numFlats(0)
88 {
89  int i = 0;
90  while( i < numBuff ) {
91  buffer[i] = (unsigned int)((rowIndex + (i+1)*(colIndex+8))&0xffffffff);
92  ++i;
93  }
94  theSeed = rowIndex;
95  redSpin = colIndex & 0xffffffff;
96  for( i = 0; i < 100; ++i) flat(); // Warm-up by running thorugh 100 nums
97 }
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
CLHEP::RanshiEngine::~RanshiEngine ( )
virtual

Definition at line 99 of file RanshiEngine.cc.

99 { }

Member Function Documentation

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

Definition at line 267 of file RanshiEngine.cc.

267  {
268  return "RanshiEngine-begin";
269 }
static std::string CLHEP::RanshiEngine::engineName ( )
inlinestatic

Definition at line 95 of file RanshiEngine.h.

Referenced by restoreStatus().

95 {return "RanshiEngine";}
double CLHEP::RanshiEngine::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 101 of file RanshiEngine.cc.

References CLHEP::HepRandomEngine::nearlyTwoToMinus_54(), CLHEP::HepRandomEngine::twoToMinus_32(), and CLHEP::HepRandomEngine::twoToMinus_53().

Referenced by flatArray(), and RanshiEngine().

101  {
102  unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
103  unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
104  unsigned int boostResult = blkSpin ^ redSpin;
105 
106  buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
107 
108  redSpin = (blkSpin + numFlats++) & 0xffffffff;
109  halfBuff = numBuff/2 - halfBuff;
110 
111  return ( blkSpin * twoToMinus_32() + // most significant part
112  (boostResult>>11) * twoToMinus_53() + // fill in remaining bits
113  nearlyTwoToMinus_54()); // non-zero
114 }
static double nearlyTwoToMinus_54()
static double twoToMinus_32()
static double twoToMinus_53()
void CLHEP::RanshiEngine::flatArray ( const int  size,
double *  vect 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 116 of file RanshiEngine.cc.

References flat().

116  {
117  for (int i = 0; i < size; ++i) {
118  vect[i] = flat();
119  }
120 }
std::istream & CLHEP::RanshiEngine::get ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 250 of file RanshiEngine.cc.

References getState().

250  {
251  char beginMarker [MarkerLen];
252  is >> std::ws;
253  is.width(MarkerLen); // causes the next read to the char* to be <=
254  // that many bytes, INCLUDING A TERMINATION \0
255  // (Stroustrup, section 21.3.2)
256  is >> beginMarker;
257  if (strcmp(beginMarker,"RanshiEngine-begin")) {
258  is.clear(std::ios::badbit | is.rdstate());
259  std::cerr << "\nInput mispositioned or"
260  << "\nRanshiEngine state description missing or"
261  << "\nwrong engine type found." << std::endl;
262  return is;
263  }
264  return getState(is);
265 }
virtual std::istream & getState(std::istream &is)
bool CLHEP::RanshiEngine::get ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 309 of file RanshiEngine.cc.

References getState().

309  {
310  if ((v[0] & 0xffffffffUL) != engineIDulong<RanshiEngine>()) {
311  std::cerr <<
312  "\nRanshiEngine get:state vector has wrong ID word - state unchanged\n";
313  return false;
314  }
315  return getState(v);
316 }
virtual std::istream & getState(std::istream &is)
std::istream & CLHEP::RanshiEngine::getState ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 271 of file RanshiEngine.cc.

References CLHEP::possibleKeywordInput(), CLHEP::HepRandomEngine::theSeed, and test::v.

Referenced by get(), and restoreStatus().

271  {
272  if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
273  std::vector<unsigned long> v;
274  unsigned long uu;
275  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
276  is >> uu;
277  if (!is) {
278  is.clear(std::ios::badbit | is.rdstate());
279  std::cerr << "\nRanshiEngine state (vector) description improper."
280  << "\ngetState() has failed."
281  << "\nInput stream is probably mispositioned now." << std::endl;
282  return is;
283  }
284  v.push_back(uu);
285  }
286  getState(v);
287  return (is);
288  }
289 
290 // is >> theSeed; Removed, encompassed by possibleKeywordInput()
291 
292  char endMarker [MarkerLen];
293  for (int i = 0; i < numBuff; ++i) {
294  is >> buffer[i];
295  }
296  is >> redSpin >> numFlats >> halfBuff;
297  is >> std::ws;
298  is.width(MarkerLen);
299  is >> endMarker;
300  if (strcmp(endMarker,"RanshiEngine-end")) {
301  is.clear(std::ios::badbit | is.rdstate());
302  std::cerr << "\nRanshiEngine state description incomplete."
303  << "\nInput stream is probably mispositioned now." << std::endl;
304  return is;
305  }
306  return is;
307 }
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167
virtual std::istream & getState(std::istream &is)
bool CLHEP::RanshiEngine::getState ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 318 of file RanshiEngine.cc.

318  {
319  if (v.size() != VECTOR_STATE_SIZE ) {
320  std::cerr <<
321  "\nRanshiEngine get:state vector has wrong length - state unchanged\n";
322  return false;
323  }
324  for (int i = 0; i < numBuff; ++i) {
325  buffer[i] = v[i+1];
326  }
327  redSpin = v[numBuff+1];
328  numFlats = v[numBuff+2];
329  halfBuff = v[numBuff+3];
330  return true;
331 }
std::string CLHEP::RanshiEngine::name ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 43 of file RanshiEngine.cc.

43 {return "RanshiEngine";}
CLHEP::RanshiEngine::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 204 of file RanshiEngine.cc.

References buffer.

204  {
205  unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
206  unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
207 
208  buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
209 
210  redSpin = (blkSpin + numFlats++) & 0xffffffff;
211  halfBuff = numBuff/2 - halfBuff;
212 
213  return float(blkSpin * twoToMinus_32());
214 }
static double twoToMinus_32()
CLHEP::RanshiEngine::operator unsigned int ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 216 of file RanshiEngine.cc.

References buffer.

216  {
217  unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
218  unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
219 
220  buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
221 
222  redSpin = (blkSpin + numFlats++) & 0xffffffff;
223  halfBuff = numBuff/2 - halfBuff;
224 
225  return blkSpin;
226 }
std::ostream & CLHEP::RanshiEngine::put ( std::ostream &  os) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 228 of file RanshiEngine.cc.

References put(), and test::v.

228  {
229  char beginMarker[] = "RanshiEngine-begin";
230  os << beginMarker << "\nUvec\n";
231  std::vector<unsigned long> v = put();
232  for (unsigned int i=0; i<v.size(); ++i) {
233  os << v[i] << "\n";
234  }
235  return os;
236 }
std::vector< unsigned long > put() const
std::vector< unsigned long > CLHEP::RanshiEngine::put ( ) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 238 of file RanshiEngine.cc.

References test::v.

Referenced by put(), and saveStatus().

238  {
239  std::vector<unsigned long> v;
240  v.push_back (engineIDulong<RanshiEngine>());
241  for (int i = 0; i < numBuff; ++i) {
242  v.push_back(static_cast<unsigned long>(buffer[i]));
243  }
244  v.push_back(static_cast<unsigned long>(redSpin));
245  v.push_back(static_cast<unsigned long>(numFlats));
246  v.push_back(static_cast<unsigned long>(halfBuff));
247  return v;
248 }
void CLHEP::RanshiEngine::restoreStatus ( const char  filename[] = "RanshiEngine.conf")
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 154 of file RanshiEngine.cc.

References CLHEP::HepRandomEngine::checkFile(), engineName(), getState(), CLHEP::possibleKeywordInput(), CLHEP::HepRandomEngine::theSeed, and test::v.

154  {
155  std::ifstream inFile(filename, std::ios::in);
156  if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
157  std::cerr << " -- Engine state remains unchanged\n";
158  return;
159  }
160  if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
161  std::vector<unsigned long> v;
162  unsigned long xin;
163  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
164  inFile >> xin;
165  if (!inFile) {
166  inFile.clear(std::ios::badbit | inFile.rdstate());
167  std::cerr << "\nRanshiEngine state (vector) description improper."
168  << "\nrestoreStatus has failed."
169  << "\nInput stream is probably mispositioned now." << std::endl;
170  return;
171  }
172  v.push_back(xin);
173  }
174  getState(v);
175  return;
176  }
177 
178  if (!inFile.bad()) {
179 // inFile >> theSeed; removed -- encompased by possibleKeywordInput
180  for (int i = 0; i < numBuff; ++i) {
181  inFile >> buffer[i];
182  }
183  inFile >> redSpin >> numFlats >> halfBuff;
184  }
185 }
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:45
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167
virtual std::istream & getState(std::istream &is)
static std::string engineName()
Definition: RanshiEngine.h:95
void CLHEP::RanshiEngine::saveStatus ( const char  filename[] = "RanshiEngine.conf") const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 143 of file RanshiEngine.cc.

References outFile, put(), and test::v.

143  {
144  std::ofstream outFile(filename, std::ios::out);
145  if (!outFile.bad()) {
146  outFile << "Uvec\n";
147  std::vector<unsigned long> v = put();
148  for (unsigned int i=0; i<v.size(); ++i) {
149  outFile << v[i] << "\n";
150  }
151  }
152 }
std::ofstream outFile
Definition: GammaRayTel.cc:68
std::vector< unsigned long > put() const
void CLHEP::RanshiEngine::setSeed ( long  seed,
int   
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 122 of file RanshiEngine.cc.

References RanshiEngine().

122  {
123  *this = RanshiEngine(seed);
124 }
void CLHEP::RanshiEngine::setSeeds ( const long *  seeds,
int   
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 126 of file RanshiEngine.cc.

References int(), CLHEP::HepRandomEngine::theSeed, and CLHEP::HepRandomEngine::theSeeds.

126  {
127  if (*seeds) {
128  int i = 0;
129  while (seeds[i] && i < numBuff) {
130  buffer[i] = (unsigned int)seeds[i];
131  ++i;
132  }
133  while (i < numBuff) {
134  buffer[i] = buffer[i-1];
135  ++i;
136  }
137  theSeed = seeds[0];
138  redSpin = (unsigned int)theSeed;
139  }
140  theSeeds = seeds;
141 }
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
void CLHEP::RanshiEngine::showStatus ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 187 of file RanshiEngine.cc.

References right, and CLHEP::HepRandomEngine::theSeed.

187  {
188  std::cout << std::setprecision(20) << std::endl;
189  std::cout << "----------- Ranshi engine status ----------" << std::endl;
190  std::cout << "Initial seed = " << theSeed << std::endl;
191  std::cout << "Current red spin = " << redSpin << std::endl;
192  std::cout << "Values produced = " << numFlats << std::endl;
193  std::cout << "Side of buffer = " << (halfBuff ? "upper" : "lower")
194  << std::endl;
195  std::cout << "Current buffer = " << std::endl;
196  for (int i = 0; i < numBuff; i+=4) {
197  std::cout << std::setw(10) << std::setiosflags(std::ios::right)
198  << buffer[i] << std::setw(11) << buffer[i+1] << std::setw(11)
199  << buffer[i+2] << std::setw(11) << buffer[i+3] << std::endl;
200  }
201  std::cout << "-------------------------------------------" << std::endl;
202 }

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