Geant4 9.6.0
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 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 = 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 35 of file MTwistEngine.h.

Constructor & Destructor Documentation

◆ MTwistEngine() [1/4]

CLHEP::MTwistEngine::MTwistEngine ( )

Definition at line 57 of file MTwistEngine.cc.

59{
60 int cycle = std::abs(int(numEngines/maxIndex));
61 int curIndex = std::abs(int(numEngines%maxIndex));
62 long mask = ((cycle & 0x007fffff) << 8);
63 long seedlist[2];
64 HepRandom::getTheTableSeeds( seedlist, curIndex );
65 seedlist[0] = (seedlist[0])^mask;
66 seedlist[1] = 0;
67 setSeeds( seedlist, numEngines );
68 count624=0;
69 ++numEngines;
70 for( int i=0; i < 2000; ++i ) flat(); // Warm up just a bit
71}
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:151
void setSeeds(const long *seeds, int)

◆ MTwistEngine() [2/4]

CLHEP::MTwistEngine::MTwistEngine ( long  seed)

Definition at line 73 of file MTwistEngine.cc.

75{
76 long seedlist[2]={seed,17587};
77 setSeeds( seedlist, 0 );
78 count624=0;
79 for( int i=0; i < 2000; ++i ) flat(); // Warm up just a bit
80}

◆ MTwistEngine() [3/4]

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

Definition at line 82 of file MTwistEngine.cc.

84{
85 int cycle = std::abs(int(rowIndex/maxIndex));
86 int row = std::abs(int(rowIndex%maxIndex));
87 int col = std::abs(int(colIndex%2));
88 long mask = (( cycle & 0x000007ff ) << 20 );
89 long seedlist[2];
90 HepRandom::getTheTableSeeds( seedlist, row );
91 seedlist[0] = (seedlist[col])^mask;
92 seedlist[1] = 690691;
93 setSeeds(seedlist, 4444772);
94 count624=0;
95 for( int i=0; i < 2000; ++i ) flat(); // Warm up just a bit
96}

◆ MTwistEngine() [4/4]

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

Definition at line 98 of file MTwistEngine.cc.

100{
101 is >> *this;
102}

◆ ~MTwistEngine()

CLHEP::MTwistEngine::~MTwistEngine ( )
virtual

Definition at line 104 of file MTwistEngine.cc.

104{}

Member Function Documentation

◆ beginTag()

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

Definition at line 328 of file MTwistEngine.cc.

328 {
329 return "MTwistEngine-begin";
330}

◆ 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 106 of file MTwistEngine.cc.

106 {
107 unsigned int y;
108
109 if( count624 >= N ) {
110 register int i;
111
112 for( i=0; i < NminusM; ++i ) {
113 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
114 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
115 }
116
117 for( ; i < N-1 ; ++i ) {
118 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
119 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
120 }
121
122 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
123 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
124
125 count624 = 0;
126 }
127
128 y = mt[count624];
129 y ^= ( y >> 11);
130 y ^= ((y << 7 ) & 0x9d2c5680);
131 y ^= ((y << 15) & 0xefc60000);
132 y ^= ( y >> 18);
133
134 return y * twoToMinus_32() + // Scale to range
135 (mt[count624++] >> 11) * twoToMinus_53() + // fill remaining bits
136 nearlyTwoToMinus_54(); // make sure non-zero
137}
static double twoToMinus_32()
static double twoToMinus_53()
static double nearlyTwoToMinus_54()

Referenced by flatArray(), and MTwistEngine().

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 139 of file MTwistEngine.cc.

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

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 350 of file MTwistEngine.cc.

350 {
351 if ((v[0] & 0xffffffffUL) != engineIDulong<MTwistEngine>()) {
352 std::cerr <<
353 "\nMTwistEngine get:state vector has wrong ID word - state unchanged\n";
354 return false;
355 }
356 return getState(v);
357}
virtual std::istream & getState(std::istream &is)

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 310 of file MTwistEngine.cc.

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

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 359 of file MTwistEngine.cc.

359 {
360 if (v.size() != VECTOR_STATE_SIZE ) {
361 std::cerr <<
362 "\nMTwistEngine get:state vector has wrong length - state unchanged\n";
363 return false;
364 }
365 for (int i=0; i<624; ++i) {
366 mt[i]=v[i+1];
367 }
368 count624 = v[625];
369 return true;
370}
static const unsigned int VECTOR_STATE_SIZE
Definition: MTwistEngine.h:83

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 332 of file MTwistEngine.cc.

333{
334 char endMarker [MarkerLen];
335 is >> theSeed;
336 for (int i=0; i<624; ++i) is >> mt[i];
337 is >> count624;
338 is >> std::ws;
339 is.width(MarkerLen);
340 is >> endMarker;
341 if (strcmp(endMarker,"MTwistEngine-end")) {
342 is.clear(std::ios::badbit | is.rdstate());
343 std::cerr << "\nMTwistEngine state description incomplete."
344 << "\nInput stream is probably mispositioned now." << std::endl;
345 return is;
346 }
347 return is;
348}

Referenced by get().

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 52 of file MTwistEngine.cc.

52{return "MTwistEngine";}

◆ operator float()

CLHEP::MTwistEngine::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 221 of file MTwistEngine.cc.

221 {
222 unsigned int y;
223
224 if( count624 >= N ) {
225 register int i;
226
227 for( i=0; i < NminusM; ++i ) {
228 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
229 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
230 }
231
232 for( ; i < N-1 ; ++i ) {
233 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
234 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
235 }
236
237 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
238 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
239
240 count624 = 0;
241 }
242
243 y = mt[count624++];
244 y ^= ( y >> 11);
245 y ^= ((y << 7 ) & 0x9d2c5680);
246 y ^= ((y << 15) & 0xefc60000);
247 y ^= ( y >> 18);
248
249 return (float)(y * twoToMinus_32());
250}

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 252 of file MTwistEngine.cc.

252 {
253 unsigned int y;
254
255 if( count624 >= N ) {
256 register int i;
257
258 for( i=0; i < NminusM; ++i ) {
259 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
260 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
261 }
262
263 for( ; i < N-1 ; ++i ) {
264 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
265 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
266 }
267
268 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
269 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
270
271 count624 = 0;
272 }
273
274 y = mt[count624++];
275 y ^= ( y >> 11);
276 y ^= ((y << 7 ) & 0x9d2c5680);
277 y ^= ((y << 15) & 0xefc60000);
278 y ^= ( y >> 18);
279
280 return y;
281}

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 300 of file MTwistEngine.cc.

300 {
301 std::vector<unsigned long> v;
302 v.push_back (engineIDulong<MTwistEngine>());
303 for (int i=0; i<624; ++i) {
304 v.push_back(static_cast<unsigned long>(mt[i]));
305 }
306 v.push_back(count624);
307 return v;
308}

◆ put() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 283 of file MTwistEngine.cc.

284{
285 char beginMarker[] = "MTwistEngine-begin";
286 char endMarker[] = "MTwistEngine-end";
287
288 int pr = os.precision(20);
289 os << " " << beginMarker << " ";
290 os << theSeed << " ";
291 for (int i=0; i<624; ++i) {
292 os << mt[i] << "\n";
293 }
294 os << count624 << " ";
295 os << endMarker << "\n";
296 os.precision(pr);
297 return os;
298}

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 191 of file MTwistEngine.cc.

192{
193 std::ifstream inFile( filename, std::ios::in);
194 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
195 std::cerr << " -- Engine state remains unchanged\n";
196 return;
197 }
198
199 if (!inFile.bad() && !inFile.eof()) {
200 inFile >> theSeed;
201 for (int i=0; i<624; ++i) inFile >> mt[i];
202 inFile >> count624;
203 }
204}
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:45
static std::string engineName()
Definition: MTwistEngine.h:77

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 180 of file MTwistEngine.cc.

181{
182 std::ofstream outFile( filename, std::ios::out ) ;
183 if (!outFile.bad()) {
184 outFile << theSeed << std::endl;
185 for (int i=0; i<624; ++i) outFile <<std::setprecision(20) << mt[i] << " ";
186 outFile << std::endl;
187 outFile << count624 << std::endl;
188 }
189}

◆ setSeed()

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

Implements CLHEP::HepRandomEngine.

Definition at line 143 of file MTwistEngine.cc.

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

Referenced by setSeeds().

◆ setSeeds()

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

Implements CLHEP::HepRandomEngine.

Definition at line 171 of file MTwistEngine.cc.

171 {
172 setSeed( (*seeds ? *seeds : 43571346), k );
173 int i;
174 for( i=1; i < 624; ++i ) {
175 mt[i] = ( seeds[1] + mt[i] ) & 0xffffffff; // MF 9/16/98
176 }
177 theSeeds = seeds;
178}
void setSeed(long seed, int)

Referenced by MTwistEngine().

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 206 of file MTwistEngine.cc.

207{
208 std::cout << std::endl;
209 std::cout << "--------- MTwist engine status ---------" << std::endl;
210 std::cout << std::setprecision(20);
211 std::cout << " Initial seed = " << theSeed << std::endl;
212 std::cout << " Current index = " << count624 << std::endl;
213 std::cout << " Array status mt[] = " << std::endl;
214 for (int i=0; i<624; i+=5) {
215 std::cout << mt[i] << " " << mt[i+1] << " " << mt[i+2] << " "
216 << mt[i+3] << " " << mt[i+4] << std::endl;
217 }
218 std::cout << "----------------------------------------" << std::endl;
219}

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: