CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
testSaveEngineStatus.cc File Reference
#include "CLHEP/Random/Randomize.h"
#include "CLHEP/Random/NonRandomEngine.h"
#include "CLHEP/Random/defs.h"
#include <iostream>
#include <iomanip>
#include <vector>

Go to the source code of this file.

Macros

#define CLEAN_OUTPUT
 
#define TEST_ORIGINAL_SAVE
 
#define VERBOSER
 
#define VERBOSER2
 

Functions

std::ofstream output ("testSaveEngineStatus.cout")
 
bool equals01 (const std::vector< double > &ab)
 
bool equals (double a, double b)
 
int saveStepX ()
 
int restoreStepX ()
 
int BsaveStepX ()
 
int BrestoreStepX ()
 
int saveStep ()
 
int restoreStep ()
 
int BsaveStep ()
 
int BrestoreStep ()
 
template<class E , class D >
int fileNotThere ()
 
template<class E >
int fileNotThereEngine ()
 
int missingFile ()
 
template<class E , class D >
int saveEngine (const char *filename)
 
template<class E , class D >
int checkSaveEngine (const char *filename)
 
int main ()
 

Variables

double remembered_r2
 
double remembered_r1005
 
double remembered_r1006
 
double remembered_r1007
 

Macro Definition Documentation

◆ CLEAN_OUTPUT

#define CLEAN_OUTPUT

Definition at line 9 of file testSaveEngineStatus.cc.

◆ TEST_ORIGINAL_SAVE

#define TEST_ORIGINAL_SAVE

Definition at line 18 of file testSaveEngineStatus.cc.

◆ VERBOSER

#define VERBOSER

Definition at line 28 of file testSaveEngineStatus.cc.

◆ VERBOSER2

#define VERBOSER2

Definition at line 29 of file testSaveEngineStatus.cc.

Function Documentation

◆ BrestoreStep()

int BrestoreStep ( )

Definition at line 212 of file testSaveEngineStatus.cc.

212 {
213 int stat=0;
215 int r = RandFlat::shootBit();
216 output << "restored r(2) = " << r << std::endl;
217 if ( r != remembered_r2 ) {
218 stat += 4;
219 std::cout << "restored r(2) = " << r << std::endl;
220 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
221 #ifdef CLEAN_OUTPUT
222 output << "restored r(2) = " << r << std::endl;
223 output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
224 #endif
225 }
226 r = RandFlat::shootBit();
227 output << "restored r(3) = " << r << std::endl;
228 for (int i=0; i < 1001; i++) {
229 r = RandFlat::shootBit();
230 }
231 r = RandFlat::shootBit();
232 output << "restored r1005= " << r << std::endl;
233 if ( r != remembered_r1005 ) {
234 stat += 8;
235 std::cout << "restored r1005= " << r << std::endl;
236 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
237 #ifdef CLEAN_OUTPUT
238 output << "restored r1005= " << r << std::endl;
239 output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
240 #endif
241 }
242 r = RandFlat::shootBit();
243 output << "restored r1006= " << r << std::endl;
244 if ( r != remembered_r1006 ) {
245 stat += 16;
246 std::cout << "restored r1006= " << r << std::endl;
247 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
248 #ifdef CLEAN_OUTPUT
249 output << "restored r1006= " << r << std::endl;
250 output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
251 #endif
252 }
253 r = RandFlat::shootBit();
254 output << "restored r1007= " << r << std::endl;
255 if ( r != remembered_r1007 ) {
256 stat += 32;
257 std::cout << "restored r1007= " << r << std::endl;
258 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
259 #ifdef CLEAN_OUTPUT
260 output << "restored r1007= " << r << std::endl;
261 output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
262 #endif
263 }
264 return stat;
265}
static int shootBit()
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:122
double remembered_r2
double remembered_r1007
double remembered_r1006
double remembered_r1005
std::ofstream output("testSaveEngineStatus.cout")

Referenced by main().

◆ BrestoreStepX()

int BrestoreStepX ( )

Definition at line 111 of file testSaveEngineStatus.cc.

111 {
113 int r = RandFlat::shootBit();
114 output << "restored r(2) = " << r << std::endl;
115 if ( r != remembered_r2 ) {
116 output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
117 }
118 r = RandFlat::shootBit();
119 output << "restored r(3) = " << r << std::endl;
120 for (int i=0; i < 1001; i++) {
121 r = RandFlat::shootBit();
122 }
123 r = RandFlat::shootBit();
124 output << "restored r1005= " << r << std::endl;
125 if ( r != remembered_r1005 ) {
126 output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
127 }
128 return 0;
129}
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:285

Referenced by main().

◆ BsaveStep()

int BsaveStep ( )

Definition at line 186 of file testSaveEngineStatus.cc.

186 {
187 int stat=0;
188 int r = RandFlat::shootBit();
189 output << "r(1) = " << r << std::endl;
191 r = RandFlat::shootBit();
192 output << "r(2) = " << r << std::endl;
193 remembered_r2 = r;
194 r = RandFlat::shootBit();
195 output << "r(3) = " << r << std::endl;
196 for (int i=0; i < 1001; i++) {
197 r = RandFlat::shootBit();
198 }
199 r = RandFlat::shootBit();
201 output << "r1005 = " << r << std::endl;
202 r = RandFlat::shootBit();
204 output << "r1006 = " << r << std::endl;
205 r = RandFlat::shootBit();
207 output << "r1007 = " << r << std::endl;
208 r = RandFlat::shootBit();
209 return stat;
210}
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:107

Referenced by main().

◆ BsaveStepX()

int BsaveStepX ( )

Definition at line 89 of file testSaveEngineStatus.cc.

89 {
90 int r = RandFlat::shootBit();
91 output << "r(1) = " << r << std::endl;
94 output << "r(2) = " << r << std::endl;
95 remembered_r2 = r;
97 output << "r(3) = " << r << std::endl;
98 double d;
99 for (int i=0; i < 1001; i++) {
100 d = RandFlat::shoot();
101 if (d > 1) output <<
102 "This line inserted so clever compilers don't warn about not using d\n";
103 }
104 r = RandFlat::shootBit();
106 output << "r1005= " << r << std::endl;
107 r = RandFlat::shootBit();
108 return 0;
109}
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:280
static double shoot()
Definition: RandFlat.cc:63

Referenced by main().

◆ checkSaveEngine()

template<class E , class D >
int checkSaveEngine ( const char *  filename)

Definition at line 346 of file testSaveEngineStatus.cc.

346 {
347 int stat = 0;
348 HepRandomEngine * old = D::getTheEngine();
349
350 // Generate save with current format (default file name is fine)
351 E e(123);
352 D::setTheEngine(&e);
353 double r=0;
354 for (int i=0; i<3; i++) r += D::shoot();
355 D::saveEngineStatus();
356
357 // Figure out what the key variate value should be
358 double keyValue = D::shoot();
359
360 // Restore state based on old file, and check for proper value
361 D::restoreEngineStatus(filename);
362 if (!equals(D::shoot(), keyValue)) {
363 std::cout << "???? Value mismatch from file " << filename << "\n";
364 #ifdef CLEAN_OUTPUT
365 output << "???? Value mismatch from file " << filename << "\n";
366 #endif
367 stat |= 64;
368 }
369
370 // Restore state based on that save, and check for proper value
371 D::restoreEngineStatus();
372 if (!equals(D::shoot(),keyValue)) {
373 std::cout << "???? Value mismatch from new-format file \n";
374 #ifdef CLEAN_OUTPUT
375 output << "???? Value mismatch from new-format file \n";
376 #endif
377 stat |= 128;
378 }
379
380 D::setTheEngine(old);
381 return stat;
382}
bool equals(double a, double b)

◆ equals()

bool equals ( double  a,
double  b 
)

Definition at line 42 of file testSaveEngineStatus.cc.

42 {
43 std::vector<double> ab(2);
44 ab[0]=a; ab[1]=b;
45 return (equals01(ab));
46}
bool equals01(const std::vector< double > &ab)

Referenced by checkSaveEngine(), restoreStep(), and restoreStepX().

◆ equals01()

bool equals01 ( const std::vector< double > &  ab)

Definition at line 39 of file testSaveEngineStatus.cc.

39 {
40 return ab[1]==ab[0];
41}

Referenced by equals().

◆ fileNotThere()

template<class E , class D >
int fileNotThere ( )

Definition at line 270 of file testSaveEngineStatus.cc.

270 {
271 int stat = 0;
272 HepRandomEngine * old = D::getTheEngine();
273 E e(123);
274 output << "File-not-found test restoring "<<D::distributionName()<<":\n";
275 D::setTheEngine(&e);
276 D::restoreEngineStatus("noSuchFile");
277 D::setTheEngine(old); // If we don't do this, then the static engine shared
278 // by every shoot() method reamins e -- which is about
279 // to go out of scope and be destructed!
280 return stat;
281}

◆ fileNotThereEngine()

template<class E >
int fileNotThereEngine ( )

Definition at line 284 of file testSaveEngineStatus.cc.

284 {
285 int stat = 0;
286 stat |= fileNotThere <E, RandBinomial>();
287 stat |= fileNotThere <E, RandBit>();
288 stat |= fileNotThere <E, RandBreitWigner>();
289 stat |= fileNotThere <E, RandChiSquare>();
290 stat |= fileNotThere <E, RandExponential>();
291 stat |= fileNotThere <E, RandFlat>();
292 stat |= fileNotThere <E, RandGamma>();
293 stat |= fileNotThere <E, RandGauss>();
294 stat |= fileNotThere <E, RandGaussQ>();
295 stat |= fileNotThere <E, RandGaussT>();
296 stat |= fileNotThere <E, RandLandau>();
297 stat |= fileNotThere <E, RandPoisson>();
298 stat |= fileNotThere <E, RandPoissonQ>();
299 stat |= fileNotThere <E, RandPoissonT>();
300 stat |= fileNotThere <E, RandSkewNormal>();
301 stat |= fileNotThere <E, RandStudentT>();
302 return stat;
303}

◆ main()

int main ( )

Definition at line 389 of file testSaveEngineStatus.cc.

389 {
390 int stat = 0;
391
392#ifdef TEST_ORIGINAL_SAVE
393 output << "=====================================\n";
394 output << " Part I \n";
395 output << "Original tests of static save/restore\n";
396 output << "=====================================\n\n";
397
398 output << "Using old method or HepRandom::saveEngineStatus:\n";
399 output << "All these tests should have a chance of failure.\n";
400
401 output << RandGauss:: getTheEngine()->name();
402 output << RandGaussQ::getTheEngine()->name();
403
404 stat |= saveStepX();
405 stat |= restoreStepX();
406 stat |= BsaveStepX();
407 stat |= BrestoreStepX();
408
409 output << "Using the class-specific RandGauss::saveEngineStatus:\n";
410 output << "All these tests should work properly.\n";
411
412 stat |= saveStep();
413 stat |= restoreStep();
414 stat |= BsaveStep();
415 stat |= BrestoreStep();
416#endif
417
418#ifdef TEST_MISSING_FILES
419 output << "\n=======================================\n";
420 output << " Part Ia \n";
421 output << "Test of behavior when a file is missing \n";
422 output << "=======================================\n\n";
423
424 output << "Testing restoreEngineStatus with missing file:\n";
425 output << "Expect a number of <Failure to find or open> messages!\n";
426 stat |= missingFile();
427#endif
428
429#ifdef CREATE_OLD_SAVES
430 stat |= saveEngine<DRand48Engine, RandPoisson>("DRand48Engine.oldsav");
431 stat |= saveEngine<DualRand, RandPoisson>("DualRand.oldsav");
432 stat |= saveEngine<Hurd160Engine, RandPoisson>("Hurd160Engine.oldsav");
433 stat |= saveEngine<Hurd288Engine, RandPoisson>("Hurd288Engine.oldsav");
434 stat |= saveEngine<HepJamesRandom,RandPoisson>("HepJamesRandom.oldsav");
435 stat |= saveEngine<MixMaxRng, RandPoisson>("MixMaxRng.oldsav");
436 stat |= saveEngine<MTwistEngine, RandPoisson>("MTwistEngine.oldsav");
437 stat |= saveEngine<RanecuEngine, RandPoisson>("RanecuEngine.oldsav");
438 stat |= saveEngine<Ranlux64Engine,RandPoisson>("Ranlux64Engine.oldsav");
439 stat |= saveEngine<RanluxEngine, RandPoisson>("RanluxEngine.oldsav");
440 stat |= saveEngine<RanluxppEngine,RandPoisson>("RanluxppEngine.oldsav");
441 stat |= saveEngine<RanshiEngine, RandPoisson>("RanshiEngine.oldsav");
442 stat |= saveEngine<TripleRand, RandPoisson>("TripleRand.oldsav");
443#endif
444
445#ifdef VERIFY_OLD_SAVES
446 output << "\n==============================================\n";
447 output << " Part Ib \n";
448 output << " Verification that changes wont invalidate \n";
449 output << "invalidate engine saves from previous versions \n";
450 output << "==============================================\n\n";
451
452 stat |= checkSaveEngine<DRand48Engine, RandPoisson>("DRand48Engine.oldsav");
453 stat |= checkSaveEngine<DualRand, RandPoisson>("DualRand.oldsav");
454 stat |= checkSaveEngine<Hurd160Engine, RandPoisson>("Hurd160Engine.oldsav");
455 stat |= checkSaveEngine<Hurd288Engine, RandPoisson>("Hurd288Engine.oldsav");
456 stat |= checkSaveEngine<HepJamesRandom,RandPoisson>("HepJamesRandom.oldsav");
457 stat |= checkSaveEngine<MixMaxRng, RandPoisson>("MixMaxRng.oldsav");
458 stat |= checkSaveEngine<MTwistEngine, RandPoisson>("MTwistEngine.oldsav");
459 stat |= checkSaveEngine<Ranlux64Engine,RandPoisson>("Ranlux64Engine.oldsav");
460 stat |= checkSaveEngine<RanluxEngine, RandPoisson>("RanluxEngine.oldsav");
461 stat |= checkSaveEngine<RanluxppEngine,RandPoisson>("RanluxppEngine.oldsav");
462 stat |= checkSaveEngine<RanshiEngine, RandPoisson>("RanshiEngine.oldsav");
463 stat |= checkSaveEngine<TripleRand, RandPoisson>("TripleRand.oldsav");
464 stat |= checkSaveEngine<RanecuEngine, RandPoisson>("RanecuEngine.oldsav");
465#endif
466
467 output << "\n=============================================\n\n";
468
469 if (stat != 0) {
470 std::cout << "One or more problems detected: stat = " << stat << "\n";
471 output << "One or more problems detected: stat = " << stat << "\n";
472 } else {
473 output << "testSaveEngineStatus passed with no problems detected.\n";
474 }
475
476 if (stat == 0) return 0;
477 if (stat > 0) return -(stat|1);
478 return stat|1;
479}
int saveStepX()
int restoreStep()
int missingFile()
int BrestoreStep()
int BsaveStep()
int BsaveStepX()
int restoreStepX()
int saveStep()
int BrestoreStepX()

◆ missingFile()

int missingFile ( )

Definition at line 305 of file testSaveEngineStatus.cc.

305 {
306 int stat = 0;
307 stat |= fileNotThereEngine<DRand48Engine>();
308 stat |= fileNotThereEngine<DualRand>();
309 stat |= fileNotThereEngine<Hurd160Engine>();
310 stat |= fileNotThereEngine<Hurd288Engine>();
311 stat |= fileNotThereEngine<HepJamesRandom>();
312 stat |= fileNotThereEngine<MixMaxRng>();
313 stat |= fileNotThereEngine<MTwistEngine>();
314 stat |= fileNotThereEngine<RandEngine>();
315 stat |= fileNotThereEngine<RanecuEngine>();
316 stat |= fileNotThereEngine<Ranlux64Engine>();
317 stat |= fileNotThereEngine<RanluxEngine>();
318 stat |= fileNotThereEngine<RanluxppEngine>();
319 stat |= fileNotThereEngine<RanshiEngine>();
320 stat |= fileNotThereEngine<TripleRand>();
321 return stat;
322}

Referenced by main().

◆ output()

std::ofstream output ( "testSaveEngineStatus.cout"  )

◆ restoreStep()

int restoreStep ( )

Definition at line 153 of file testSaveEngineStatus.cc.

153 {
154 int stat=0;
156 double r = RandGauss::shoot();
157 output << "restored r(2) = " << r << std::endl;
158 if ( !equals(r,remembered_r2) ) {
159 std::cout << "restored r(2) = " << r << std::endl;
160 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
161 #ifdef CLEAN_OUTPUT
162 output << "restored r(2) = " << r << std::endl;
163 output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
164 #endif
165 stat += 1;
166 }
167 r = RandGauss::shoot();
168 output << "restored r(3) = " << r << std::endl;
169 for (int i=0; i < 1001; i++) {
170 r = RandGauss::shoot();
171 }
172 r = RandGauss::shoot();
173 output << "restored r1005= " << r << std::endl;
174 if ( !equals(r,remembered_r1005) ) {
175 std::cout << "restored r1005= " << r << std::endl;
176 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
177 #ifdef CLEAN_OUTPUT
178 output << "restored r1005= " << r << std::endl;
179 output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
180 #endif
181 stat += 2;
182 }
183 return stat;
184}
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: RandGauss.cc:213
static double shoot()
Definition: RandGauss.cc:64

Referenced by main().

◆ restoreStepX()

int restoreStepX ( )

Definition at line 69 of file testSaveEngineStatus.cc.

69 {
71 double r = RandGauss::shoot();
72 output << "restored r(2) = " << r << std::endl;
73 if ( ! equals(r,remembered_r2) ) {
74 output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
75 }
76 r = RandGauss::shoot();
77 output << "restored r(3) = " << r << std::endl;
78 for (int i=0; i < 1001; i++) {
79 r = RandGauss::shoot();
80 }
81 r = RandGauss::shoot();
82 output << "restored r1005= " << r << std::endl;
83 if ( !equals(r,remembered_r1005) ) {
84 output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
85 }
86 return 0;
87}

Referenced by main().

◆ saveEngine()

template<class E , class D >
int saveEngine ( const char *  filename)

Definition at line 327 of file testSaveEngineStatus.cc.

327 {
328 int stat = 0;
329 HepRandomEngine * old = D::getTheEngine();
330 E e(123);
331 D::setTheEngine(&e);
332 double r=0;
333 for (int i=0; i<3; i++) r += D::shoot();
334 D::saveEngineStatus(filename);
335 if (r == -99999999.1) stat = 999; // This prevents clever compilers from
336 // deducing that r is never needed
337 D::setTheEngine(old); // If we don't do this, then the static engine shared
338 // by every shoot() method reamins e -- which is about
339 // to go out of scope and be destructed!
340 return stat;
341}

◆ saveStep()

int saveStep ( )

Definition at line 133 of file testSaveEngineStatus.cc.

133 {
134 int stat=0;
135 double r = RandGauss::shoot();
136 output << "r(1) = " << r << std::endl;
138 r = RandGauss::shoot();
139 output << "r(2) = " << r << std::endl;
140 remembered_r2 = r;
141 r = RandGauss::shoot();
142 output << "r(3) = " << r << std::endl;
143 for (int i=0; i < 1001; i++) {
144 r = RandGauss::shoot();
145 }
146 r = RandGauss::shoot();
148 output << "r1005= " << r << std::endl;
149 r = RandGauss::shoot();
150 return stat;
151}
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: RandGauss.cc:193

Referenced by main().

◆ saveStepX()

int saveStepX ( )

Definition at line 50 of file testSaveEngineStatus.cc.

50 {
51 double r = RandGauss::shoot();
52 output << "r(1) = " << r << std::endl;
54 r = RandGauss::shoot();
55 output << "r(2) = " << r << std::endl;
56 remembered_r2 = r;
57 r = RandGauss::shoot();
58 output << "r(3) = " << r << std::endl;
59 for (int i=0; i < 1001; i++) {
60 r = RandGauss::shoot();
61 }
62 r = RandGauss::shoot();
64 output << "r1005= " << r << std::endl;
65 r = RandGauss::shoot();
66 return 0;
67}

Referenced by main().

Variable Documentation

◆ remembered_r1005

◆ remembered_r1006

double remembered_r1006

Definition at line 35 of file testSaveEngineStatus.cc.

Referenced by BrestoreStep(), and BsaveStep().

◆ remembered_r1007

double remembered_r1007

Definition at line 36 of file testSaveEngineStatus.cc.

Referenced by BrestoreStep(), and BsaveStep().

◆ remembered_r2