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

#include <DualRand.h>

+ Inheritance diagram for CLHEP::DualRand:

Public Member Functions

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

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 51 of file DualRand.h.

Constructor & Destructor Documentation

◆ DualRand() [1/4]

CLHEP::DualRand::DualRand ( )

Definition at line 81 of file DualRand.cc.

83 numEngines(numberOfEngines++),
84 tausworthe (1234567 + numEngines + 175321),
85 integerCong(69607 * tausworthe + 54329, numEngines)
86{
87 theSeed = 1234567;
88}

◆ DualRand() [2/4]

CLHEP::DualRand::DualRand ( long  seed)

Definition at line 90 of file DualRand.cc.

92 numEngines(0),
93 tausworthe ((unsigned int)seed + 175321),
94 integerCong(69607 * tausworthe + 54329, 8043) // MF - not numEngines
95{
96 theSeed = seed;
97}

◆ DualRand() [3/4]

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

Definition at line 99 of file DualRand.cc.

100 : HepRandomEngine(),
101 numEngines(0)
102{
103 is >> *this;
104}

◆ DualRand() [4/4]

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

Definition at line 106 of file DualRand.cc.

108 numEngines(0),
109 tausworthe (rowIndex + 1000 * colIndex + 85329),
110 integerCong(69607 * tausworthe + 54329, 1123) // MF - not numengines
111{
112 theSeed = rowIndex;
113}

◆ ~DualRand()

CLHEP::DualRand::~DualRand ( )
virtual

Definition at line 115 of file DualRand.cc.

115{ }

Member Function Documentation

◆ beginTag()

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

Definition at line 282 of file DualRand.cc.

282 {
283 return "DualRand-begin";
284}

◆ engineName()

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

Definition at line 98 of file DualRand.h.

98{return "DualRand";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::DualRand::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 117 of file DualRand.cc.

117 {
118 unsigned int ic ( integerCong );
119 unsigned int t ( tausworthe );
120 return ( (t ^ ic) * twoToMinus_32() + // most significant part
121 (t >> 11) * twoToMinus_53() + // fill in remaining bits
122 nearlyTwoToMinus_54() // make sure non-zero
123 );
124}
static double twoToMinus_32()
static double twoToMinus_53()
static double nearlyTwoToMinus_54()

Referenced by flatArray(), and main().

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 126 of file DualRand.cc.

126 {
127 for (int i = 0; i < size; ++i) {
128 vect[i] = flat();
129 }
130}
double flat()
Definition: DualRand.cc:117

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 322 of file DualRand.cc.

322 {
323 if ((v[0] & 0xffffffffUL) != engineIDulong<DualRand>()) {
324 std::cerr <<
325 "\nDualRand get:state vector has wrong ID word - state unchanged\n";
326 return false;
327 }
328 if (v.size() != VECTOR_STATE_SIZE) {
329 std::cerr << "\nDualRand get:state vector has wrong size: "
330 << v.size() << " - state unchanged\n";
331 return false;
332 }
333 return getState(v);
334}
static const unsigned int VECTOR_STATE_SIZE
Definition: DualRand.h:104
virtual std::istream & getState(std::istream &is)
Definition: DualRand.cc:286

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 265 of file DualRand.cc.

265 {
266 char beginMarker [MarkerLen];
267 is >> std::ws;
268 is.width(MarkerLen); // causes the next read to the char* to be <=
269 // that many bytes, INCLUDING A TERMINATION \0
270 // (Stroustrup, section 21.3.2)
271 is >> beginMarker;
272 if (strcmp(beginMarker,"DualRand-begin")) {
273 is.clear(std::ios::badbit | is.rdstate());
274 std::cerr << "\nInput mispositioned or"
275 << "\nDualRand state description missing or"
276 << "\nwrong engine type found." << std::endl;
277 return is;
278 }
279 return getState(is);
280}

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 336 of file DualRand.cc.

336 {
337 std::vector<unsigned long>::const_iterator iv = v.begin()+1;
338 if (!tausworthe.get(iv)) return false;
339 if (!integerCong.get(iv)) return false;
340 if (iv != v.end()) {
341 std::cerr <<
342 "\nDualRand get:state vector has wrong size: " << v.size()
343 << "\n Apparently " << iv-v.begin() << " words were consumed\n";
344 return false;
345 }
346 return true;
347}

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 286 of file DualRand.cc.

286 {
287 if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
288 std::vector<unsigned long> v;
289 unsigned long uu;
290 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
291 is >> uu;
292 if (!is) {
293 is.clear(std::ios::badbit | is.rdstate());
294 std::cerr << "\nDualRand state (vector) description improper."
295 << "\ngetState() has failed."
296 << "\nInput stream is probably mispositioned now." << std::endl;
297 return is;
298 }
299 v.push_back(uu);
300 }
301 getState(v);
302 return (is);
303 }
304
305// is >> theSeed; Removed, encompassed by possibleKeywordInput()
306
307 char endMarker [MarkerLen];
308 tausworthe.get(is);
309 integerCong.get(is);
310 is >> std::ws;
311 is.width(MarkerLen);
312 is >> endMarker;
313 if (strcmp(endMarker,"DualRand-end")) {
314 is.clear(std::ios::badbit | is.rdstate());
315 std::cerr << "DualRand state description incomplete."
316 << "\nInput stream is probably mispositioned now." << std::endl;
317 return is;
318 }
319 return is;
320}
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168

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

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 73 of file DualRand.cc.

73{return "DualRand";}

◆ operator double()

CLHEP::DualRand::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 222 of file DualRand.cc.

222 {
223 return flat();
224}

◆ operator float()

CLHEP::DualRand::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 226 of file DualRand.cc.

226 {
227 return (float) ( (integerCong ^ tausworthe) * twoToMinus_32()
228 + nearlyTwoToMinus_54() );
229 // add this so that zero never happens
230}

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 232 of file DualRand.cc.

232 {
233 return (integerCong ^ tausworthe) & 0xffffffff;
234}

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 257 of file DualRand.cc.

257 {
258 std::vector<unsigned long> v;
259 v.push_back (engineIDulong<DualRand>());
260 tausworthe.put(v);
261 integerCong.put(v);
262 return v;
263}

Referenced by put(), and saveStatus().

◆ put() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 236 of file DualRand.cc.

236 {
237 char beginMarker[] = "DualRand-begin";
238 os << beginMarker << "\nUvec\n";
239 std::vector<unsigned long> v = put();
240 for (unsigned int i=0; i<v.size(); ++i) {
241 os << v[i] << "\n";
242 }
243 return os;
244#ifdef REMOVED
245 char endMarker[] = "DualRand-end";
246 long pr=os.precision(20);
247 os << " " << beginMarker << " ";
248 os << theSeed << " ";
249 tausworthe.put(os);
250 integerCong.put(os);
251 os << " " << endMarker << "\n";
252 os.precision(pr);
253 return os;
254#endif
255}
std::vector< unsigned long > put() const
Definition: DualRand.cc:257

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 172 of file DualRand.cc.

172 {
173 std::ifstream inFile(filename, std::ios::in);
174 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
175 std::cerr << " -- Engine state remains unchanged\n";
176 return;
177 }
178 if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
179 std::vector<unsigned long> v;
180 unsigned long xin;
181 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
182 inFile >> xin;
183 #ifdef TRACE_IO
184 std::cout << "ivec = " << ivec << " xin = " << xin << " ";
185 if (ivec%3 == 0) std::cout << "\n";
186 #endif
187 if (!inFile) {
188 inFile.clear(std::ios::badbit | inFile.rdstate());
189 std::cerr << "\nDualRand state (vector) description improper."
190 << "\nrestoreStatus has failed."
191 << "\nInput stream is probably mispositioned now." << std::endl;
192 return;
193 }
194 v.push_back(xin);
195 }
196 getState(v);
197 return;
198 }
199
200 if (!inFile.bad()) {
201// inFile >> theSeed; removed -- encompased by possibleKeywordInput
202 tausworthe.get(inFile);
203 integerCong.get(inFile);
204 }
205}
static std::string engineName()
Definition: DualRand.h:98
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:49

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 143 of file DualRand.cc.

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 #ifdef TRACE_IO
149 std::cout << "Result of v = put() is:\n";
150 #endif
151 for (unsigned int i=0; i<v.size(); ++i) {
152 outFile << v[i] << "\n";
153 #ifdef TRACE_IO
154 std::cout << v[i] << " ";
155 if (i%6==0) std::cout << "\n";
156 #endif
157 }
158 #ifdef TRACE_IO
159 std::cout << "\n";
160 #endif
161 }
162#ifdef REMOVED
163 long pr=outFile.precision(20);
164 outFile << theSeed << std::endl;
165 tausworthe.put(outFile);
166 integerCong.put(outFile);
167 outFile << std::endl; // This is superfluous but harmless
168 outFile.precision(pr);
169#endif
170}

◆ setSeed()

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

Implements CLHEP::HepRandomEngine.

Definition at line 132 of file DualRand.cc.

132 {
133 theSeed = seed;
134 tausworthe = Tausworthe((unsigned int)seed + 175321);
135 integerCong = IntegerCong(69607 * tausworthe + 54329, 8043);
136}

Referenced by setSeeds().

◆ setSeeds()

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

Implements CLHEP::HepRandomEngine.

Definition at line 138 of file DualRand.cc.

138 {
139 setSeed(seeds ? *seeds : 1234567, 0);
140 theSeeds = seeds;
141}
void setSeed(long seed, int)
Definition: DualRand.cc:132

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 207 of file DualRand.cc.

207 {
208 long pr=std::cout.precision(20);
209 std::cout << std::endl;
210 std::cout << "-------- DualRand engine status ---------"
211 << std::endl;
212 std::cout << "Initial seed = " << theSeed << std::endl;
213 std::cout << "Tausworthe generator = " << std::endl;
214 tausworthe.put(std::cout);
215 std::cout << "\nIntegerCong generator = " << std::endl;
216 integerCong.put(std::cout);
217 std::cout << std::endl << "-----------------------------------------"
218 << std::endl;
219 std::cout.precision(pr);
220}

Member Data Documentation

◆ VECTOR_STATE_SIZE

const unsigned int CLHEP::DualRand::VECTOR_STATE_SIZE = 9
static

Definition at line 104 of file DualRand.h.

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


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