Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
CLHEP::MTwistEngine Class Reference

#include <MTwistEngine.h>

+ Inheritance diagram for CLHEP::MTwistEngine:

Public Member Functions

 MTwistEngine ()
 
 MTwistEngine (long seed)
 
 MTwistEngine (int rowIndex, int colIndex)
 
 MTwistEngine (std::istream &is)
 
virtual ~MTwistEngine ()
 
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[]="MTwist.conf") const
 
void restoreStatus (const char filename[]="MTwist.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)
 
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)
 

Static Public Attributes

static const unsigned int VECTOR_STATE_SIZE = 626
 

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 34 of file MTwistEngine.h.

Constructor & Destructor Documentation

◆ MTwistEngine() [1/4]

CLHEP::MTwistEngine::MTwistEngine ( )

Definition at line 66 of file MTwistEngine.cc.

68{
69 int numEngines = numberOfEngines++;
70 int cycle = std::abs(int(numEngines/maxIndex));
71 int curIndex = std::abs(int(numEngines%maxIndex));
72 long mask = ((cycle & 0x007fffff) << 8);
73 long seedlist[2];
74 HepRandom::getTheTableSeeds( seedlist, curIndex );
75 seedlist[0] = (seedlist[0])^mask;
76 seedlist[1] = 0;
77 setSeeds( seedlist, numEngines );
78 count624=0;
79
80 for( int i=0; i < 2000; ++i ) flat(); // Warm up just a bit
81}
static void getTheTableSeeds(long *seeds, int index)
Definition Random.cc:254
void setSeeds(const long *seeds, int)

◆ MTwistEngine() [2/4]

CLHEP::MTwistEngine::MTwistEngine ( long seed)

Definition at line 83 of file MTwistEngine.cc.

85{
86 long seedlist[2]={seed,17587};
87 setSeeds( seedlist, 0 );
88 count624=0;
89 for( int i=0; i < 2000; ++i ) flat(); // Warm up just a bit
90}

◆ MTwistEngine() [3/4]

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

Definition at line 92 of file MTwistEngine.cc.

94{
95 int cycle = std::abs(int(rowIndex/maxIndex));
96 int row = std::abs(int(rowIndex%maxIndex));
97 int col = std::abs(int(colIndex%2));
98 long mask = (( cycle & 0x000007ff ) << 20 );
99 long seedlist[2];
100 HepRandom::getTheTableSeeds( seedlist, row );
101 seedlist[0] = (seedlist[col])^mask;
102 seedlist[1] = 690691;
103 setSeeds(seedlist, 4444772);
104 count624=0;
105 for( int i=0; i < 2000; ++i ) flat(); // Warm up just a bit
106}

◆ MTwistEngine() [4/4]

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

Definition at line 108 of file MTwistEngine.cc.

110{
111 is >> *this;
112}

◆ ~MTwistEngine()

CLHEP::MTwistEngine::~MTwistEngine ( )
virtual

Definition at line 114 of file MTwistEngine.cc.

114{}

Member Function Documentation

◆ beginTag()

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

Definition at line 346 of file MTwistEngine.cc.

346 {
347 return "MTwistEngine-begin";
348}

◆ engineName()

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

Definition at line 77 of file MTwistEngine.h.

77{return "MTwistEngine";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::MTwistEngine::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 116 of file MTwistEngine.cc.

116 {
117 unsigned int y;
118
119 if( count624 >= N ) {
120 int i;
121
122 for( i=0; i < NminusM; ++i ) {
123 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
124 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
125 }
126
127 for( ; i < N-1 ; ++i ) {
128 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
129 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
130 }
131
132 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
133 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
134
135 count624 = 0;
136 }
137
138 y = mt[count624];
139 y ^= ( y >> 11);
140 y ^= ((y << 7 ) & 0x9d2c5680);
141 y ^= ((y << 15) & 0xefc60000);
142 y ^= ( y >> 18);
143
144 return y * twoToMinus_32() + // Scale to range
145 (mt[count624++] >> 11) * twoToMinus_53() + // fill remaining bits
146 nearlyTwoToMinus_54(); // make sure non-zero
147}
static double twoToMinus_32()
static double twoToMinus_53()
static double nearlyTwoToMinus_54()

Referenced by flatArray(), MTwistEngine(), MTwistEngine(), and MTwistEngine().

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 149 of file MTwistEngine.cc.

149 {
150 for( int i=0; i < size; ++i) vect[i] = flat();
151}

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 368 of file MTwistEngine.cc.

368 {
369 if ((v[0] & 0xffffffffUL) != engineIDulong<MTwistEngine>()) {
370 std::cerr <<
371 "\nMTwistEngine get:state vector has wrong ID word - state unchanged\n";
372 return false;
373 }
374 return getState(v);
375}
virtual std::istream & getState(std::istream &is)
unsigned long engineIDulong()

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 328 of file MTwistEngine.cc.

329{
330 char beginMarker [MarkerLen];
331 is >> std::ws;
332 is.width(MarkerLen); // causes the next read to the char* to be <=
333 // that many bytes, INCLUDING A TERMINATION \0
334 // (Stroustrup, section 21.3.2)
335 is >> beginMarker;
336 if (strcmp(beginMarker,"MTwistEngine-begin")) {
337 is.clear(std::ios::badbit | is.rdstate());
338 std::cerr << "\nInput stream mispositioned or"
339 << "\nMTwistEngine state description missing or"
340 << "\nwrong engine type found." << std::endl;
341 return is;
342 }
343 return getState(is);
344}

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 377 of file MTwistEngine.cc.

377 {
378 if (v.size() != VECTOR_STATE_SIZE ) {
379 std::cerr <<
380 "\nMTwistEngine get:state vector has wrong length - state unchanged\n";
381 return false;
382 }
383 for (int i=0; i<624; ++i) {
384 mt[i]=(unsigned int)v[i+1];
385 }
386 count624 = (int)v[625];
387 return true;
388}
static const unsigned int VECTOR_STATE_SIZE

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 350 of file MTwistEngine.cc.

351{
352 char endMarker [MarkerLen];
353 is >> theSeed;
354 for (int i=0; i<624; ++i) is >> mt[i];
355 is >> count624;
356 is >> std::ws;
357 is.width(MarkerLen);
358 is >> endMarker;
359 if (strcmp(endMarker,"MTwistEngine-end")) {
360 is.clear(std::ios::badbit | is.rdstate());
361 std::cerr << "\nMTwistEngine state description incomplete."
362 << "\nInput stream is probably mispositioned now." << std::endl;
363 return is;
364 }
365 return is;
366}

Referenced by get(), and get().

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 64 of file MTwistEngine.cc.

64{return "MTwistEngine";}

◆ operator double()

CLHEP::MTwistEngine::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 235 of file MTwistEngine.cc.

235 {
236 return flat();
237}

◆ operator float()

CLHEP::MTwistEngine::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 239 of file MTwistEngine.cc.

239 {
240 unsigned int y;
241
242 if( count624 >= N ) {
243 int i;
244
245 for( i=0; i < NminusM; ++i ) {
246 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
247 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
248 }
249
250 for( ; i < N-1 ; ++i ) {
251 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
252 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
253 }
254
255 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
256 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
257
258 count624 = 0;
259 }
260
261 y = mt[count624++];
262 y ^= ( y >> 11);
263 y ^= ((y << 7 ) & 0x9d2c5680);
264 y ^= ((y << 15) & 0xefc60000);
265 y ^= ( y >> 18);
266
267 return (float)(y * twoToMinus_32());
268}

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 270 of file MTwistEngine.cc.

270 {
271 unsigned int y;
272
273 if( count624 >= N ) {
274 int i;
275
276 for( i=0; i < NminusM; ++i ) {
277 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
278 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
279 }
280
281 for( ; i < N-1 ; ++i ) {
282 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
283 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
284 }
285
286 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
287 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
288
289 count624 = 0;
290 }
291
292 y = mt[count624++];
293 y ^= ( y >> 11);
294 y ^= ((y << 7 ) & 0x9d2c5680);
295 y ^= ((y << 15) & 0xefc60000);
296 y ^= ( y >> 18);
297
298 return y;
299}

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 318 of file MTwistEngine.cc.

318 {
319 std::vector<unsigned long> v;
320 v.push_back (engineIDulong<MTwistEngine>());
321 for (int i=0; i<624; ++i) {
322 v.push_back(static_cast<unsigned long>(mt[i]));
323 }
324 v.push_back(count624);
325 return v;
326}

◆ put() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 301 of file MTwistEngine.cc.

302{
303 char beginMarker[] = "MTwistEngine-begin";
304 char endMarker[] = "MTwistEngine-end";
305
306 long pr = os.precision(20);
307 os << " " << beginMarker << " ";
308 os << theSeed << " ";
309 for (int i=0; i<624; ++i) {
310 os << mt[i] << "\n";
311 }
312 os << count624 << " ";
313 os << endMarker << "\n";
314 os.precision(pr);
315 return os;
316}

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 201 of file MTwistEngine.cc.

202{
203 std::ifstream inFile( filename, std::ios::in);
204 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
205 std::cerr << " -- Engine state remains unchanged\n";
206 return;
207 }
208
209 if (!inFile.bad() && !inFile.eof()) {
210 inFile >> theSeed;
211 for (int i=0; i<624; ++i) inFile >> mt[i];
212 inFile >> count624;
213 }
214}
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
static std::string engineName()

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 190 of file MTwistEngine.cc.

191{
192 std::ofstream outFile( filename, std::ios::out ) ;
193 if (!outFile.bad()) {
194 outFile << theSeed << std::endl;
195 for (int i=0; i<624; ++i) outFile <<std::setprecision(20) << mt[i] << " ";
196 outFile << std::endl;
197 outFile << count624 << std::endl;
198 }
199}

◆ setSeed()

void CLHEP::MTwistEngine::setSeed ( long seed,
int k )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 153 of file MTwistEngine.cc.

153 {
154
155 // MF 11/15/06 - Change seeding algorithm to a newer one recommended
156 // by Matsumoto: The original algorithm was
157 // mt[i] = (69069 * mt[i-1]) & 0xffffffff and this gives
158 // problems when the seed bit pattern has lots of zeros
159 // (for example, 0x08000000). Savanah bug #17479.
160
161 theSeed = seed ? seed : 4357;
162 int mti;
163 const int N1=624;
164 mt[0] = (unsigned int) (theSeed&0xffffffffUL);
165 for (mti=1; mti<N1; mti++) {
166 mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
167 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
168 /* In the previous versions, MSBs of the seed affect */
169 /* only MSBs of the array mt[]. */
170 /* 2002/01/09 modified by Makoto Matsumoto */
171 mt[mti] &= 0xffffffffUL;
172 /* for >32 bit machines */
173 }
174 for( int i=1; i < 624; ++i ) {
175 mt[i] ^= k; // MF 9/16/98: distinguish starting points
176 }
177 // MF 11/15/06 This distinction of starting points based on values of k
178 // is kept even though the seeding algorithm itself is improved.
179}

Referenced by setSeeds().

◆ setSeeds()

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

Implements CLHEP::HepRandomEngine.

Definition at line 181 of file MTwistEngine.cc.

181 {
182 setSeed( (*seeds ? *seeds : 43571346), k );
183 int i;
184 for( i=1; i < 624; ++i ) {
185 mt[i] = ( seeds[1] + mt[i] ) & 0xffffffff; // MF 9/16/98
186 }
187 theSeeds = seeds;
188}
void setSeed(long seed, int)

Referenced by MTwistEngine(), MTwistEngine(), and MTwistEngine().

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 216 of file MTwistEngine.cc.

217{
218 std::cout << std::endl;
219 std::cout << "--------- MTwist engine status ---------" << std::endl;
220 std::cout << std::setprecision(20);
221 std::cout << " Initial seed = " << theSeed << std::endl;
222 std::cout << " Current index = " << count624 << std::endl;
223 std::cout << " Array status mt[] = " << std::endl;
224 // 2014/06/06 L Garren
225 // the final line has 4 elements, not 5
226 for (int i=0; i<620; i+=5) {
227 std::cout << mt[i] << " " << mt[i+1] << " " << mt[i+2] << " "
228 << mt[i+3] << " " << mt[i+4] << "\n";
229 }
230 std::cout << mt[620] << " " << mt[621] << " " << mt[622] << " "
231 << mt[623] << std::endl;
232 std::cout << "----------------------------------------" << std::endl;
233}

Member Data Documentation

◆ VECTOR_STATE_SIZE

const unsigned int CLHEP::MTwistEngine::VECTOR_STATE_SIZE = 626
static

Definition at line 83 of file MTwistEngine.h.

Referenced by getState().


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