CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
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)
 
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 36 of file MTwistEngine.h.

Constructor & Destructor Documentation

◆ MTwistEngine() [1/4]

CLHEP::MTwistEngine::MTwistEngine ( )

Definition at line 67 of file MTwistEngine.cc.

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

◆ MTwistEngine() [2/4]

CLHEP::MTwistEngine::MTwistEngine ( long  seed)

Definition at line 84 of file MTwistEngine.cc.

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

◆ MTwistEngine() [3/4]

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

Definition at line 93 of file MTwistEngine.cc.

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

◆ MTwistEngine() [4/4]

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

Definition at line 109 of file MTwistEngine.cc.

111{
112 is >> *this;
113}

◆ ~MTwistEngine()

CLHEP::MTwistEngine::~MTwistEngine ( )
virtual

Definition at line 115 of file MTwistEngine.cc.

115{}

Member Function Documentation

◆ beginTag()

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

Definition at line 347 of file MTwistEngine.cc.

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

◆ engineName()

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

Definition at line 79 of file MTwistEngine.h.

79{return "MTwistEngine";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::MTwistEngine::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 117 of file MTwistEngine.cc.

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

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

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 150 of file MTwistEngine.cc.

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

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 369 of file MTwistEngine.cc.

369 {
370 if ((v[0] & 0xffffffffUL) != engineIDulong<MTwistEngine>()) {
371 std::cerr <<
372 "\nMTwistEngine get:state vector has wrong ID word - state unchanged\n";
373 return false;
374 }
375 return getState(v);
376}
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 329 of file MTwistEngine.cc.

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

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 378 of file MTwistEngine.cc.

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

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 351 of file MTwistEngine.cc.

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

Referenced by get().

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 65 of file MTwistEngine.cc.

65{return "MTwistEngine";}

◆ operator double()

CLHEP::MTwistEngine::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 236 of file MTwistEngine.cc.

236 {
237 return flat();
238}

◆ operator float()

CLHEP::MTwistEngine::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 240 of file MTwistEngine.cc.

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

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 271 of file MTwistEngine.cc.

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

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 319 of file MTwistEngine.cc.

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

◆ put() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 302 of file MTwistEngine.cc.

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

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 202 of file MTwistEngine.cc.

203{
204 std::ifstream inFile( filename, std::ios::in);
205 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
206 std::cerr << " -- Engine state remains unchanged\n";
207 return;
208 }
209
210 if (!inFile.bad() && !inFile.eof()) {
211 inFile >> theSeed;
212 for (int i=0; i<624; ++i) inFile >> mt[i];
213 inFile >> count624;
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: MTwistEngine.h:79

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 191 of file MTwistEngine.cc.

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

◆ setSeed()

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

Implements CLHEP::HepRandomEngine.

Definition at line 154 of file MTwistEngine.cc.

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

Referenced by setSeeds().

◆ setSeeds()

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

Implements CLHEP::HepRandomEngine.

Definition at line 182 of file MTwistEngine.cc.

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

Referenced by MTwistEngine().

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 217 of file MTwistEngine.cc.

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

Member Data Documentation

◆ VECTOR_STATE_SIZE

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

Definition at line 85 of file MTwistEngine.h.

Referenced by getState().


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