CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
ranRestoreTest.cc
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id: ranRestoreTest.cc,v 1.6 2011/05/31 20:57:01 garren Exp $
3// ----------------------------------------------------------------------
4#include "CLHEP/Random/Randomize.h"
5#include "CLHEP/Random/NonRandomEngine.h"
6#include "CLHEP/Random/defs.h"
7#include <iostream>
8#include <iomanip>
9#include <vector>
10
11#define CLEAN_OUTPUT
12#ifdef CLEAN_OUTPUT
13 std::ofstream output("ranRestoreTest.cout");
14#else
15 std::ostream & output = std::cout;
16#endif
17
18// Normally on for routine validation:
19
20#define TEST_ORIGINAL_SAVE
21
22#ifdef TURNOFF
23#endif
24
25#define TEST_ENGINE_NAMES
26#define TEST_INSTANCE_METHODS
27#define TEST_SHARED_ENGINES
28#define TEST_STATIC_SAVE
29#define TEST_SAVE_STATIC_STATES
30#define TEST_ANONYMOUS_ENGINE_RESTORE
31#define TEST_ANONYMOUS_RESTORE_STATICS
32#define TEST_VECTOR_ENGINE_RESTORE
33
34// Normally off for routine validation:
35
36#ifdef TURNOFF
37#define TEST_MISSING_FILES
38#define CREATE_OLD_SAVES
39#define VERIFY_OLD_SAVES
40#endif
41
42//#define VERBOSER
43//#define VERBOSER2
44
45using namespace CLHEP;
46
51
52// Absolutely Safe Equals Without Registers Screwing Us Up
53bool equals01(const std::vector<double> &ab) {
54 return ab[1]==ab[0];
55}
56bool equals(double a, double b) {
57 std::vector<double> ab(2);
58 ab[0]=a; ab[1]=b;
59 return (equals01(ab));
60}
61
62// ------------------- The following should all FAIL ------------
63
64int saveStepX() {
65 double r = RandGauss::shoot();
66 output << "r(1) = " << r << std::endl;
68 r = RandGauss::shoot();
69 output << "r(2) = " << r << std::endl;
70 remembered_r2 = r;
71 r = RandGauss::shoot();
72 output << "r(3) = " << r << std::endl;
73 for (int i=0; i < 1001; i++) {
74 r = RandGauss::shoot();
75 }
76 r = RandGauss::shoot();
78 output << "r1005= " << r << std::endl;
79 r = RandGauss::shoot();
80 return 0;
81}
82
85 double r = RandGauss::shoot();
86 output << "restored r(2) = " << r << std::endl;
87 if ( ! equals(r,remembered_r2) ) {
88 output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
89 }
90 r = RandGauss::shoot();
91 output << "restored r(3) = " << r << std::endl;
92 for (int i=0; i < 1001; i++) {
93 r = RandGauss::shoot();
94 }
95 r = RandGauss::shoot();
96 output << "restored r1005= " << r << std::endl;
97 if ( !equals(r,remembered_r1005) ) {
98 output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
99 }
100 return 0;
101}
102
104 int r = RandFlat::shootBit();
105 output << "r(1) = " << r << std::endl;
107 r = RandFlat::shootBit();
108 output << "r(2) = " << r << std::endl;
109 remembered_r2 = r;
110 r = RandFlat::shootBit();
111 output << "r(3) = " << r << std::endl;
112 double d;
113 for (int i=0; i < 1001; i++) {
114 d = RandFlat::shoot();
115 if (d > 1) output <<
116 "This line inserted so clever compilers don't warn about not using d\n";
117 }
118 r = RandFlat::shootBit();
120 output << "r1005= " << r << std::endl;
121 r = RandFlat::shootBit();
122 return 0;
123}
124
127 int r = RandFlat::shootBit();
128 output << "restored r(2) = " << r << std::endl;
129 if ( r != remembered_r2 ) {
130 output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
131 }
132 r = RandFlat::shootBit();
133 output << "restored r(3) = " << r << std::endl;
134 for (int i=0; i < 1001; i++) {
135 r = RandFlat::shootBit();
136 }
137 r = RandFlat::shootBit();
138 output << "restored r1005= " << r << std::endl;
139 if ( r != remembered_r1005 ) {
140 output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
141 }
142 return 0;
143}
144
145// ------------------- The following should all WORK ------------
146
147int saveStep() {
148 int stat=0;
149 double r = RandGauss::shoot();
150 output << "r(1) = " << r << std::endl;
152 r = RandGauss::shoot();
153 output << "r(2) = " << r << std::endl;
154 remembered_r2 = r;
155 r = RandGauss::shoot();
156 output << "r(3) = " << r << std::endl;
157 for (int i=0; i < 1001; i++) {
158 r = RandGauss::shoot();
159 }
160 r = RandGauss::shoot();
162 output << "r1005= " << r << std::endl;
163 r = RandGauss::shoot();
164 return stat;
165}
166
168 int stat=0;
170 double r = RandGauss::shoot();
171 output << "restored r(2) = " << r << std::endl;
172 if ( !equals(r,remembered_r2) ) {
173 std::cout << "restored r(2) = " << r << std::endl;
174 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
175 stat += 1;
176 }
177 r = RandGauss::shoot();
178 output << "restored r(3) = " << r << std::endl;
179 for (int i=0; i < 1001; i++) {
180 r = RandGauss::shoot();
181 }
182 r = RandGauss::shoot();
183 output << "restored r1005= " << r << std::endl;
184 if ( !equals(r,remembered_r1005) ) {
185 std::cout << "restored r1005= " << r << std::endl;
186 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
187 stat += 2;
188 }
189 return stat;
190}
191
193 int stat=0;
194 int r = RandFlat::shootBit();
195 output << "r(1) = " << r << std::endl;
197 r = RandFlat::shootBit();
198 output << "r(2) = " << r << std::endl;
199 remembered_r2 = r;
200 r = RandFlat::shootBit();
201 output << "r(3) = " << r << std::endl;
202 for (int i=0; i < 1001; i++) {
203 r = RandFlat::shootBit();
204 }
205 r = RandFlat::shootBit();
207 output << "r1005 = " << r << std::endl;
208 r = RandFlat::shootBit();
210 output << "r1006 = " << r << std::endl;
211 r = RandFlat::shootBit();
213 output << "r1007 = " << r << std::endl;
214 r = RandFlat::shootBit();
215 return stat;
216}
217
219 int stat=0;
221 int r = RandFlat::shootBit();
222 output << "restored r(2) = " << r << std::endl;
223 if ( r != remembered_r2 ) {
224 stat += 4;
225 std::cout << "restored r(2) = " << r << std::endl;
226 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
227 }
228 r = RandFlat::shootBit();
229 output << "restored r(3) = " << r << std::endl;
230 for (int i=0; i < 1001; i++) {
231 r = RandFlat::shootBit();
232 }
233 r = RandFlat::shootBit();
234 output << "restored r1005= " << r << std::endl;
235 if ( r != remembered_r1005 ) {
236 stat += 8;
237 std::cout << "restored r1005= " << r << std::endl;
238 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
239 }
240 r = RandFlat::shootBit();
241 output << "restored r1006= " << r << std::endl;
242 if ( r != remembered_r1006 ) {
243 stat += 16;
244 std::cout << "restored r1006= " << r << std::endl;
245 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
246 }
247 r = RandFlat::shootBit();
248 output << "restored r1007= " << r << std::endl;
249 if ( r != remembered_r1007 ) {
250 stat += 32;
251 std::cout << "restored r1007= " << r << std::endl;
252 std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
253 }
254 return stat;
255}
256
257// --- The following should work, by failing in an expected way -------
258
259template <class E, class D>
261 int stat = 0;
262 HepRandomEngine * old = D::getTheEngine();
263 E e(123);
264 output << "File-not-found test restoring "<<D::distributionName()<<":\n";
265 D::setTheEngine(&e);
266 D::restoreEngineStatus("noSuchFile");
267 D::setTheEngine(old); // If we don't do this, then the static engine shared
268 // by every shoot() method reamins e -- which is about
269 // to go out of scope and be destructed!
270 return stat;
271}
272
273template <class E>
275 int stat = 0;
276 stat |= fileNotThere <E, RandBinomial>();
277 stat |= fileNotThere <E, RandBit>();
278 stat |= fileNotThere <E, RandBreitWigner>();
279 stat |= fileNotThere <E, RandChiSquare>();
280 stat |= fileNotThere <E, RandExponential>();
281 stat |= fileNotThere <E, RandFlat>();
282 stat |= fileNotThere <E, RandGamma>();
283 stat |= fileNotThere <E, RandGauss>();
284 stat |= fileNotThere <E, RandGaussQ>();
285 stat |= fileNotThere <E, RandGaussT>();
286 stat |= fileNotThere <E, RandLandau>();
287 stat |= fileNotThere <E, RandPoisson>();
288 stat |= fileNotThere <E, RandPoissonQ>();
289 stat |= fileNotThere <E, RandPoissonT>();
290 stat |= fileNotThere <E, RandSkewNormal>();
291 stat |= fileNotThere <E, RandStudentT>();
292 return stat;
293}
294
296 int stat = 0;
297 stat |= fileNotThereEngine<DRand48Engine>();
298 stat |= fileNotThereEngine<DualRand>();
299 stat |= fileNotThereEngine<Hurd160Engine>();
300 stat |= fileNotThereEngine<Hurd288Engine>();
301 stat |= fileNotThereEngine<HepJamesRandom>();
302 stat |= fileNotThereEngine<MixMaxRng>();
303 stat |= fileNotThereEngine<MTwistEngine>();
304 stat |= fileNotThereEngine<RandEngine>();
305 stat |= fileNotThereEngine<RanecuEngine>();
306 stat |= fileNotThereEngine<Ranlux64Engine>();
307 stat |= fileNotThereEngine<RanluxEngine>();
308 stat |= fileNotThereEngine<RanluxppEngine>();
309 stat |= fileNotThereEngine<RanshiEngine>();
310 stat |= fileNotThereEngine<TripleRand>();
311 return stat;
312}
313
314// -- The following was used to capture old-form engine states (sans name) --
315
316template <class E, class D>
317int saveEngine(const char* filename) {
318 int stat = 0;
319 HepRandomEngine * old = D::getTheEngine();
320 E e(123);
321 D::setTheEngine(&e);
322 double r=0;
323 for (int i=0; i<3; i++) r += D::shoot();
324 D::saveEngineStatus(filename);
325 if (r == -99999999.1) stat = 999; // This prevents clever compilers from
326 // deducing that r is never needed
327 D::setTheEngine(old); // If we don't do this, then the static engine shared
328 // by every shoot() method reamins e -- which is about
329 // to go out of scope and be destructed!
330 return stat;
331}
332
333// -- The following checks on static engine restores, from old and new forms --
334
335template <class E, class D>
336int checkSaveEngine(const char* filename) {
337 int stat = 0;
338 HepRandomEngine * old = D::getTheEngine();
339
340 // Generate save with current format (default file name is fine)
341 E e(123);
342 D::setTheEngine(&e);
343 double r=0;
344 for (int i=0; i<3; i++) r += D::shoot();
345 D::saveEngineStatus();
346
347 // Figure out what the key variate value should be
348 double keyValue = D::shoot();
349
350 // Restore state based on old file, and check for proper value
351 D::restoreEngineStatus(filename);
352 if (!equals(D::shoot(), keyValue)) {
353 std::cout << "???? Value mismatch from file " << filename << "\n";
354 stat |= 64;
355 }
356
357 // Restore state based on that save, and check for proper value
358 D::restoreEngineStatus();
359 if (!equals(D::shoot(),keyValue)) {
360 std::cout << "???? Value mismatch from new-format file \n";
361 stat |= 128;
362 }
363
364 D::setTheEngine(old);
365 return stat;
366}
367
368
369// ----------- Tests for instance methods -----------
370
371template <class E>
372int checkEngineName(const std::string & name) {
373 int stat = 0;
374 output << E::engineName() << "\n";
375 if (E::engineName() != name) {
376 std::cout << "???? engineName mismatch for " << name << " <--> "
377 << E::engineName() << "\n";
378 stat |= 256;
379 }
380 E e(123);
381 if (e.name() != name) {
382 std::cout << "???? name mismatch for " << name << " <--> "
383 << e.name() << "\n";
384 stat |= 256;
385 }
386 return stat;
387}
388
389template <class E, class D>
391 int stat = 0;
392 E e(1234);
393 D d(e);
394 if (d.engine().name() != e.name()) {
395 std::cout << "???? Improper d.engine() \n";
396 stat |= 512;
397 }
398 return stat;
399}
400
401template <class E>
403 int stat = 0;
404 output << "checkEngineInstanceSave for " << e.name() << "\n";
405 int pr=output.precision(20);
406 double r=0;
407 for (int i=0; i<100; i++) r += e.flat();
408 {std::ofstream os ("engine.save"); os << e;}
409 for (int i=0; i<100; i++) r += e.flat();
410 double keyValue1 = e.flat();
411 double keyValue2 = e.flat();
412#ifdef VERBOSER
413 output << keyValue1 << " " << keyValue2 << "\n";
414#endif
415 E e2;
416 {std::ifstream is ("engine.save"); is >> e2;}
417 for (int i=0; i<100; i++) r += e2.flat();
418 double k1 = e2.flat();
419 double k2 = e2.flat();
420#ifdef VERBOSER
421 output << k1 << " " << k2 << "\n";
422#endif
423 if ( !(equals(k1,keyValue1)) || !(equals(k2,keyValue2)) ) {
424 std::cout << "???? checkInstanceSave failed for " << e.name() << "\n";
425 stat |= 1024;
426 }
427 output.precision(pr);
428 return stat;
429}
430
431template <class E, class D>
432int checkSaveDistribution(D & d, int nth) {
433 dynamic_cast<E &>(d.engine());
434 int stat = 0;
435 output << "checkSaveDistribution with " << d.engine().name()
436 << ", " << d.name() << "\n";
437 double r=0;
438 r = d();
439 double keyValue1, keyValue2, keyValue3, keyValue4;
440 for (int i=0; i<nth; i++) r += d();
441 {std::ofstream os ("distribution.save1"); os << d.engine() << d;}
442 keyValue1 = d();
443 keyValue2 = d();
444 r += d();
445 // A second capture will test non-cached if first tested cached case:
446 {std::ofstream os ("distribution.save2"); os << d.engine() << d;}
447 keyValue3 = d();
448 keyValue4 = d();
449 int pr = output.precision(20);
450#ifdef VERBOSER
451 output << "keyValue1 = " << keyValue1 <<
452 " keyValue2 = " << keyValue2 << "\n";
453 output << "keyValue3 = " << keyValue3 <<
454 " keyValue3 = " << keyValue4 << "\n";
455#endif
456 output.precision(pr);
457 E e;
458 D d2(e);
459 { std::ifstream is ("distribution.save1"); is >> e >> d2;}
460 double k1 = d2();
461 double k2 = d2();
462 { std::ifstream is ("distribution.save2"); is >> e >> d2;}
463 double k3 = d2();
464 double k4 = d2();
465#ifdef VERBOSER
466 pr = output.precision(20);
467 output << "k1 = " << k1 <<
468 " k2 = " << k2 << "\n";
469 output << "k3 = " << k3 <<
470 " k4 = " << k4 << "\n";
471 output.precision(pr);
472#endif
473 if ( !equals(k1,keyValue1) || !equals(k2,keyValue2) ||
474 !equals(k3,keyValue3) || !equals(k4,keyValue4) ) {
475 std::cout << "???? Incorrect restored value for distribution "
476 << d.name() << "\n";
477 stat |= 2048;
478 }
479// if (stat) exit(-1);
480 return stat;
481}
482
483template <class E>
485 dynamic_cast<E &>(d.engine());
486 int stat = 0;
487 output << "checkSaveDistribution with " << d.engine().name()
488 << ", " << d.name() << "\n";
489 double r=0;
490 r = d();
491 double keyValue1, keyValue2, keyValue3, keyValue4;
492 for (int i=0; i<nth; i++) r += d();
493 {std::ofstream os ("distribution.save1"); os << d.engine() << d;}
494 keyValue1 = d();
495 keyValue2 = d();
496 r += d();
497 // A second capture will test non-cached if first tested cached case:
498 {std::ofstream os ("distribution.save2"); os << d.engine() << d;}
499 keyValue3 = d();
500 keyValue4 = d();
501 int pr = output.precision(20);
502#ifdef VERBOSER
503 output << "keyValue1 = " << keyValue1 <<
504 " keyValue2 = " << keyValue2 << "\n";
505 output << "keyValue3 = " << keyValue3 <<
506 " keyValue3 = " << keyValue4 << "\n";
507#endif
508 output.precision(pr);
509 E e;
510 double temp = 1;
511 RandGeneral d2(e, &temp, 1);
512 { std::ifstream is ("distribution.save1"); is >> e >> d2;}
513 double k1 = d2();
514 double k2 = d2();
515 { std::ifstream is ("distribution.save2"); is >> e >> d2;}
516 double k3 = d2();
517 double k4 = d2();
518#ifdef VERBOSER
519 pr = output.precision(20);
520 output << "k1 = " << k1 <<
521 " k2 = " << k2 << "\n";
522 output << "k3 = " << k3 <<
523 " k4 = " << k4 << "\n";
524 output.precision(pr);
525#endif
526 if ( !equals(k1,keyValue1) || !equals(k2,keyValue2) ||
527 !equals(k3,keyValue3) || !equals(k4,keyValue4) ) {
528 std::cout << "???? Incorrect restored value for distribution "
529 << d.name() << "\n";
530 stat |= 2048;
531 }
532// if (stat) exit(-1);
533 return stat;
534}
535
536template <class E>
538 int stat = 0;
539 {RandGauss d(new E(12561),100.0,3.0);
540 stat |= checkSaveDistribution<E,RandGauss> (d,33); }
541 {RandGauss d(new E(12572),100.0,3.0);
542 stat |= checkSaveDistribution<E,RandGauss> (d,34); }
543 {RandGaussQ d(new E(12563),10.0,4.0);
544 stat |= checkSaveDistribution<E,RandGaussQ> (d,33); }
545 {RandGaussT d(new E(12564),5.0,2.0);
546 stat |= checkSaveDistribution<E,RandGaussT> (d,33); }
547 {RandBinomial d(new E(12565),4,0.6);
548 stat |= checkSaveDistribution<E,RandBinomial> (d,33); }
549 {RandFlat d(new E(12576),12.5,35.0);
550 stat |= checkSaveDistribution<E,RandFlat> (d,33); }
551 {RandBit d(new E(12567));
552 stat |= checkSaveDistribution<E,RandBit> (d,31); }
553 {RandBit d(new E(12578));
554 stat |= checkSaveDistribution<E,RandBit> (d,32); }
555 {RandBit d(new E(12589));
556 stat |= checkSaveDistribution<E,RandBit> (d,33); }
557 {RandBreitWigner d(new E(125611),50.0,15.0);
558 stat |= checkSaveDistribution<E,RandBreitWigner> (d,33); }
559 {RandChiSquare d(new E(125612),5.0);
560 stat |= checkSaveDistribution<E,RandChiSquare> (d,33); }
561 {RandExponential d(new E(125713),8.00);
562 stat |= checkSaveDistribution<E,RandExponential> (d,33); }
563 {RandGamma d(new E(125713),6.0,2.0);
564 stat |= checkSaveDistribution<E,RandGamma> (d,33); }
565 {RandLandau d(new E(125714));
566 stat |= checkSaveDistribution<E,RandLandau> (d,33); }
567 {RandSkewNormal d(new E(125713),8.00);
568 stat |= checkSaveDistribution<E,RandSkewNormal> (d,33); }
569 {RandStudentT d(new E(125715),5);
570 stat |= checkSaveDistribution<E,RandStudentT> (d,33); }
571
572 // Multiple tests of Poisson distributions for small desired, since
573 // the answer in each test is a small int, and coincidental agreement
574 // is very possible.
575
576 {RandPoisson d(new E(125616),2.5);
577 stat |= checkSaveDistribution<E,RandPoisson> (d,33); }
578 {RandPoisson d(new E(125617),105.0);
579 stat |= checkSaveDistribution<E,RandPoisson> (d,34); }
580 {RandPoisson d(new E(125618),2.5);
581 stat |= checkSaveDistribution<E,RandPoisson> (d,35); }
582 {RandPoisson d(new E(325618),2.5);
583 stat |= checkSaveDistribution<E,RandPoisson> (d,36); }
584 {RandPoisson d(new E(425618),2.5);
585 stat |= checkSaveDistribution<E,RandPoisson> (d,37); }
586 {RandPoisson d(new E(525618),2.5);
587 stat |= checkSaveDistribution<E,RandPoisson> (d,38); }
588 {RandPoisson d(new E(125619),110.0);
589 stat |= checkSaveDistribution<E,RandPoisson> (d,39); }
590 {RandPoissonQ d(new E(124616),2.5);
591 stat |= checkSaveDistribution<E,RandPoissonQ> (d,33); }
592 {RandPoissonQ d(new E(126616),2.5);
593 stat |= checkSaveDistribution<E,RandPoissonQ> (d,32); }
594 {RandPoissonQ d(new E(127616),2.5);
595 stat |= checkSaveDistribution<E,RandPoissonQ> (d,31); }
596 {RandPoissonQ d(new E(129616),2.5);
597 stat |= checkSaveDistribution<E,RandPoissonQ> (d,30); }
598 {RandPoissonQ d(new E(125616),110.0);
599 stat |= checkSaveDistribution<E,RandPoissonQ> (d,33); }
600 {RandPoissonQ d(new E(125616),2.5);
601 stat |= checkSaveDistribution<E,RandPoissonQ> (d,34); }
602 {RandPoissonQ d(new E(125616),110.0);
603 stat |= checkSaveDistribution<E,RandPoissonQ> (d,34); }
604 {RandPoissonT d(new E(125616),2.5);
605 stat |= checkSaveDistribution<E,RandPoissonT> (d,33); }
606 {RandPoissonT d(new E(125616),110.0);
607 stat |= checkSaveDistribution<E,RandPoissonT> (d,33); }
608 {RandPoissonT d(new E(125616),2.5);
609 stat |= checkSaveDistribution<E,RandPoissonT> (d,34); }
610 {RandPoissonT d(new E(125616),110.0);
611 stat |= checkSaveDistribution<E,RandPoissonT> (d,34); }
612 {RandPoissonT d(new E(125916),2.5);
613 stat |= checkSaveDistribution<E,RandPoissonT> (d,10); }
614 {RandPoissonT d(new E(125816),2.5);
615 stat |= checkSaveDistribution<E,RandPoissonT> (d,11); }
616 {RandPoissonT d(new E(125716),2.5);
617 stat |= checkSaveDistribution<E,RandPoissonT> (d,12); }
618
619 {std::vector<double> pdf;
620 int nbins = 20;
621 for (int i = 0; i < nbins; ++i)
622 pdf.push_back( 5*i + (10.5-i) * (10.5-i) );
623 RandGeneral d(new E(125917), &pdf[0], 20);
624 stat |= checkRandGeneralDistribution<E> (d,33); }
625
626 return stat;
627}
628
629template <class E, class D1, class D2>
630int checkSharingDistributions(D1 & d1, D2 & d2, int n1, int n2) {
631 int stat = 0;
632 output << "checkSharingDistribution with: \n"
633 << d1.name() << " using " << d1.engine().name() << " and\n"
634 << d2.name() << " using " << d2.engine().name() << "\n";
635 double r=0;
636 r = d1();
637 r += d2();
638 double kv11,kv12,kv13,kv14;
639 double kv21,kv22,kv23,kv24;
640 for (int i=0; i<n1; i++) r += d1();
641 for (int j=0; j<n2; j++) r += d2();
642 {std::ofstream os ("shared.save1"); os << d1.engine() << d1 << d2;}
643 kv11 = d1();
644 kv21 = d2();
645 kv12 = d1();
646 kv22 = d2();
647 r += d1() + d2();
648 // A second capture will test non-cached if first tested cached case:
649 {std::ofstream os ("shared.save2"); os << d1.engine() << d1 << d2;}
650 kv13 = d1();
651 kv23 = d2();
652 kv14 = d1();
653 kv24 = d2();
654#ifdef VERBOSER2
655 int pr = output.precision(20);
656 output << "kv11 = " << kv11 <<
657 " kv21 = " << kv21 << "\n";
658 output << "kv12 = " << kv12 <<
659 " kv22 = " << kv22 << "\n";
660 output << "kv13 = " << kv13 <<
661 " kv23 = " << kv23 << "\n";
662 output << "kv14 = " << kv14 <<
663 " kv24 = " << kv24 << "\n";
664 output.precision(pr);
665#endif
666 E e;
667 D1 d1r(e);
668 D2 d2r(e);
669 { std::ifstream is ("shared.save1"); is >> e >> d1r >> d2r;}
670 double k11 = d1r();
671 double k21 = d2r();
672 double k12 = d1r();
673 double k22 = d2r();
674 { std::ifstream is ("shared.save2"); is >> e >> d1r >> d2r;}
675 double k13 = d1r();
676 double k23 = d2r();
677 double k14 = d1r();
678 double k24 = d2r();
679#ifdef VERBOSER2
680 pr = output.precision(20);
681 output << "k11 = " << k11 <<
682 " k21 = " << k21 << "\n";
683 output << "k12 = " << k12 <<
684 " k22 = " << k22 << "\n";
685 output << "k13 = " << k13 <<
686 " k23 = " << k23 << "\n";
687 output << "k14 = " << k14 <<
688 " k24 = " << k24 << "\n";
689 output.precision(pr);
690#endif
691 if ( !equals(k11,kv11) || !equals(k21,kv21) ||
692 !equals(k12,kv12) || !equals(k22,kv22) ||
693 !equals(k13,kv13) || !equals(k23,kv23) ||
694 !equals(k14,kv14) || !equals(k24,kv24) ) {
695 std::cout << "???? Incorrect restored value for distributions "
696 << d1.name() << " " << d2.name() << "\n";
697 stat |= 4096;
698 }
699// if (stat) exit(-1);
700 return stat;
701}
702
703
704
705template <class E>
707 int stat = 0;
708 E e1(98746);
709 RandGauss g1(e1,50.0,4.0);
710 RandPoissonQ p1(e1, 112.0);
711 RandGauss g2(e1,5.0,44.0);
712 RandPoissonQ p2(e1, 212.0);
713 stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p1,5,4);
714 stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p2,6,6);
715 stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g2,p1,8,9);
716 stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p1,7,5);
717 stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p1,g2,5,4);
718 stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p2,g1,6,6);
719 stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p1,g1,8,9);
720 stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p2,g1,7,5);
721 return stat;
722}
723
724std::vector<double> aSequence(int n) {
725 std::vector<double> v;
726 DualRand e(13542);
727 RandFlat f(e);
728 for (int i=0; i<n; i++) {
729 v.push_back(f());
730 }
731 return v;
732}
733
734// ----------- Tests for static methods -----------
735
736template <class D>
737int staticSave(int n) {
738 int stat = 0;
739 int i;
740 output << "staticSave for distribution " << D::distributionName() << "\n";
741 double r = 0;
742 double v1, v2, k1, k2;
743 for (i=0; i < n; i++) r += D::shoot();
744 {
745 std::ofstream file ("distribution.save1");
746 D::saveFullState(file);
747 v1 = D::shoot();
748 D::saveFullState(file);
749 v2 = D::shoot();
750#ifdef VERBOSER2
751 int pr = output.precision(20);
752 output << "v1 = " << v1 << " v2 = " << v2 << "\n";
753 output.precision(pr);
754#endif
755 }
756 for (i=0; i < n; i++) r += D::shoot();
757 {
758 std::ifstream file ("distribution.save1");
759 D::restoreFullState(file);
760 k1 = D::shoot();
761 for (i=0; i < n; i++) r += D::shoot();
762 D::restoreFullState(file);
763 k2 = D::shoot();
764#ifdef VERBOSER2
765 int pr = output.precision(20);
766 output << "k1 = " << k1 << " k2 = " << k2 << "\n";
767 output.precision(pr);
768#endif
769 }
770 if ( (k1 != v1) || (k2 != v2) ) {
771 std::cout << "???? restoreFullState failed for " << D::distributionName() << "\n";
772 stat |= 8192;
773 }
774
775 for (i=0; i < n; i++) r += D::shoot();
776 {
777 std::ofstream file ("distribution.save2");
778 D::saveDistState(file) << *D::getTheEngine();
779 v1 = D::shoot();
780 D::saveDistState(file) << *D::getTheEngine();
781 v2 = D::shoot();
782#ifdef VERBOSER2
783 int pr = output.precision(20);
784 output << "v1 = " << v1 << " v2 = " << v2 << "\n";
785 output.precision(pr);
786#endif
787 }
788 for (i=0; i < n; i++) r += D::shoot();
789 {
790 std::ifstream file ("distribution.save2");
791 D::restoreDistState(file) >> *D::getTheEngine();
792 k1 = D::shoot();
793 for (i=0; i < n; i++) r += D::shoot();
794 D::restoreDistState(file) >> *D::getTheEngine();
795 k2 = D::shoot();
796#ifdef VERBOSER2
797 int pr = output.precision(20);
798 output << "k1 = " << k1 << " k2 = " << k2 << "\n";
799 output.precision(pr);
800#endif
801 }
802 if ( (k1 != v1) || (k2 != v2) ) {
803 std::cout << "???? restoreDistState failed for " << D::distributionName() << "\n";
804 stat |= 16384;
805 }
806
807 return stat;
808}
809
810template <class D>
812 int stat = 0;
813 int i;
814 output << "staticSaveShootBit for " << D::distributionName() << "\n";
815 double r = 0;
816 int bit = 0;
817 int v1, v2, k1, k2;
818 for (i=0; i < n; i++) r += D::shoot();
819 for (i=0; i < n; i++) bit |= D::shootBit();
820 {
821 std::ofstream file ("distribution.save1");
822 D::saveFullState(file);
823 v1=0;
824 for (i=0; i<25; i++) {
825 v1 <<=1;
826 v1 += D::shootBit();
827 }
828 for (i=0; i < n; i++) bit |= D::shootBit();
829 D::saveFullState(file);
830 v2=0;
831 for (i=0; i<25; i++) {
832 v2 <<=1;
833 v2 += D::shootBit();
834 }
835#ifdef VERBOSER2
836 int pr = output.precision(20);
837 output << std::hex << "v1 = " << v1 << " v2 = " << v2 << std::dec << "\n";
838 output.precision(pr);
839#endif
840 }
841 for (i=0; i < n; i++) r += D::shoot();
842 {
843 std::ifstream file ("distribution.save1");
844 D::restoreFullState(file);
845 k1=0;
846 for (i=0; i<25; i++) {
847 k1 <<=1;
848 k1 += D::shootBit();
849 }
850 for (i=0; i < n; i++) r += D::shoot();
851 D::restoreFullState(file);
852 k2=0;
853 for (i=0; i<25; i++) {
854 k2 <<=1;
855 k2 += D::shootBit();
856 }
857#ifdef VERBOSER2
858 int pr = output.precision(20);
859 output << std::hex << "k1 = " << k1 << " k2 = " << k2 << std::dec << "\n";
860 output.precision(pr);
861#endif
862 }
863 if ( (k1 != v1) || (k2 != v2) ) {
864 std::cout << "???? restoreFullState failed for D shootBit()\n";
865 stat |= 32768;
866 }
867
868 for (i=0; i < n; i++) r += D::shoot();
869 for (i=0; i < n; i++) bit |= D::shootBit();
870 {
871 std::ofstream file ("distribution.save2");
872 D::saveDistState(file) << *D::getTheEngine();
873 v1=0;
874 for (i=0; i<25; i++) {
875 v1 <<=1;
876 v1 += D::shootBit();
877 }
878 for (i=0; i < n; i++) bit |= D::shootBit();
879 D::saveDistState(file) << *D::getTheEngine();
880 v2=0;
881 for (i=0; i<25; i++) {
882 v2 <<=1;
883 v2 += D::shootBit();
884 }
885#ifdef VERBOSER2
886 int pr = output.precision(20);
887 output << std::hex << "v1 = " << v1 << " v2 = " << v2 << std::dec << "\n";
888 output.precision(pr);
889#endif
890 }
891 for (i=0; i < n; i++) r += D::shoot();
892 {
893 std::ifstream file ("distribution.save2");
894 D::restoreDistState(file) >> *D::getTheEngine();
895 k1=0;
896 for (i=0; i<25; i++) {
897 k1 <<=1;
898 k1 += D::shootBit();
899 }
900 for (i=0; i < n; i++) r += D::shoot();
901 for (i=0; i < n; i++) r += D::shootBit();
902 D::restoreDistState(file) >> *D::getTheEngine();
903 k2=0;
904 for (i=0; i<25; i++) {
905 k2 <<=1;
906 k2 += D::shootBit();
907 }
908#ifdef VERBOSER2
909 int pr = output.precision(20);
910 output << std::hex << "k1 = " << k1 << " k2 = " << k2 << std::dec << "\n";
911 output.precision(pr);
912#endif
913 }
914 if ( (k1 != v1) || (k2 != v2) ) {
915 std::cout << "???? restoreDistState failed for RnadFlat::shootBit()\n";
916 stat |= 65536;
917 }
918
919 return stat;
920}
921
922// ----------- Tests saving all statics together -----------
923
924void randomizeStatics(int n) {
925 for (int i=0; i<n; i++) {
930 RandBit::shoot();
944 }
945}
946
947std::vector<double> captureStatics() {
948 std::vector<double> c;
949 c.push_back( RandGauss::shoot() );
950 c.push_back( RandGaussQ::shoot() );
951 c.push_back( RandGaussT::shoot() );
952 c.push_back( RandFlat::shoot() );
953 c.push_back( RandBit::shoot() );
954 for (int i=0; i<20; i++) {
955 c.push_back( RandFlat::shootBit() );
956 c.push_back( RandBit::shootBit() );
957 }
958 c.push_back( RandPoisson::shoot() );
959 c.push_back( RandPoissonQ::shoot() );
960 c.push_back( RandPoissonT::shoot() );
961 c.push_back( RandBinomial::shoot() );
962 c.push_back( RandBreitWigner::shoot() );
963 c.push_back( RandChiSquare::shoot() );
964 c.push_back( RandExponential::shoot() );
965 c.push_back( RandGamma::shoot() );
966 c.push_back( RandLandau::shoot() );
967 c.push_back( RandSkewNormal::shoot() );
968 c.push_back( RandStudentT::shoot() );
969 return c;
970}
971
972void saveStatics(std::string filename) {
973 std::ofstream os(filename.c_str());
974 RandGeneral::saveStaticRandomStates(os);
975 // It should be possible to call this from HepRandom, or any distribution.
976 // RandGeneral, which is meaningless as a static distribution, should be the
977 // toughest test, so we use that here.
978}
979
980void restoreStatics(std::string filename) {
981 std::ifstream is(filename.c_str());
982 RandLandau::restoreStaticRandomStates(is);
983}
984
985// ----------- Anonymous restore of engines -----------
986
987template <class E>
988void anonymousRestore1(int n, std::vector<double> & v) {
989 output << "Anonymous restore for " << E::engineName() << "\n";
990 E e(12349876);
991 double r=0;
992 for (int i=0; i<n; i++) r += e.flat();
993 std::ofstream os("anonymous.save");
994 os << e;
995 for (int j=0; j<25; j++) v.push_back(e.flat());
996#ifdef VERBOSER2
997 output << "First four of v are: "
998 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
999#endif
1000 return;
1001}
1002
1003template <>
1004void anonymousRestore1<NonRandomEngine> (int n, std::vector<double> & v) {
1005#ifdef VERBOSER
1006 output << "Anonymous restore for " << NonRandomEngine::engineName() << "\n";
1007#endif
1008 std::vector<double> nonRand = aSequence(500);
1009 NonRandomEngine e;
1010 e.setRandomSequence(&nonRand[0], nonRand.size());
1011 double r=0;
1012 for (int i=0; i<n; i++) r += e.flat();
1013 std::ofstream os("anonymous.save");
1014 os << e;
1015 for (int j=0; j<25; j++) v.push_back(e.flat());
1016#ifdef VERBOSER2
1017 output << "First four of v are: "
1018 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
1019#endif
1020 return;
1021}
1022
1023template <class E>
1024int anonymousRestore2(const std::vector<double> & v) {
1025 int stat = 0;
1026 std::vector<double> k;
1027 std::ifstream is("anonymous.save");
1028 HepRandomEngine * a;
1030 for (int j=0; j<25; j++) k.push_back(a->flat());
1031 delete a;
1032#ifdef VERBOSER2
1033 output << "First four of k are: "
1034 << k[0] << ", " << k[1] << ", " << k[2] << ", " << k[3] << "\n";
1035#endif
1036 for (int m=0; m<25; m++) {
1037 if ( v[m] != k[m] ) {
1038 std::cout << "???? Incorrect restored value for anonymous engine"
1039 << E::engineName() << "\n";
1040 stat |= 262144;
1041 return stat;
1042 }
1043 }
1044 return stat;
1045}
1046
1047
1048template <class E>
1050 std::vector<double> v;
1051 anonymousRestore1<E>(n,v);
1052 return anonymousRestore2<E>(v);
1053}
1054
1055// ----------- Anonymous restore of all static distributions -----------
1056
1057template <class E>
1059 int stat = 0;
1060 HepRandomEngine *e = new E(12456);
1062 randomizeStatics(15);
1063 output << "\nRandomized, with theEngine = " << e->name() << "\n";
1064 saveStatics("distribution.save");
1065 output << "Saved all static distributions\n";
1066 std::vector<double> c = captureStatics();
1067 output << "Captured output of all static distributions\n";
1068 randomizeStatics(11);
1069 output << "Randomized all static distributions\n";
1070 restoreStatics("distribution.save");
1071 output << "Restored all static distributions to saved state\n";
1072 std::vector<double> d = captureStatics();
1073 output << "Captured output of all static distributions\n";
1074 for (unsigned int iv=0; iv<c.size(); iv++) {
1075 if (c[iv] != d[iv]) {
1076 std::cout << "???? restoreStaticRandomStates failed at random "
1077 << iv <<"\n";
1078 stat |= 131072;
1079 }
1080 }
1081 if (stat & 131072 == 0) {
1082 output << "All captured output agrees with earlier values\n";
1083 }
1084 return stat;
1085}
1086
1087
1088
1089template <class E1, class E2>
1091 int stat = 0;
1092 if ( E1::engineName() == E2::engineName() ) {
1093 return anonymousRestoreStatics1<E1>();
1094 }
1095 HepRandomEngine *e1 = new E1(12456);
1097 randomizeStatics(15);
1098 output << "\nRandomized, with theEngine = " << e1->name() << "\n";
1099 saveStatics("distribution.save");
1100#ifdef VERBOSER2
1101 output << "Saved all static distributions\n";
1102#endif
1103 std::vector<double> c = captureStatics();
1104#ifdef VERBOSER2
1105 output << "Captured output of all static distributions\n";
1106#endif
1107 delete e1;
1108 HepRandomEngine *e2 = new E2(24653);
1110 output << "Switched to theEngine = " << e2->name() << "\n";
1111 randomizeStatics(19);
1112 { std::ofstream os("engine.save"); os << *e2; }
1113 double v1 = e2->flat();
1114 double v2 = e2->flat();
1115 { std::ifstream is("engine.save"); is >> *e2; }
1116#ifdef VERBOSER2
1117 output << "Saved the " << e2->name() << " engine: \n"
1118 << "Next randoms to be " << v1 << " " << v2 << "\n"
1119 << "Restored the " << e2->name() << " engine to that state\n";
1120#endif
1121 restoreStatics("distribution.save");
1122#ifdef VERBOSER2
1123 output << "Restored all static distributions to saved state\n"
1124 << "This changes the engine type back to " << E1::engineName() << "\n";
1125#endif
1126 std::vector<double> d = captureStatics();
1127#ifdef VERBOSER2
1128 output << "Captured output of all static distributions\n";
1129#endif
1130 for (unsigned int iv=0; iv<c.size(); iv++) {
1131 if (c[iv] != d[iv]) {
1132 std::cout << "???? restoreStaticRandomStates failed at random "
1133 << iv <<"\n";
1134 stat |= 524288;
1135 }
1136 }
1137 if (stat & 524288 == 0) {
1138 output << "All captured output agrees with earlier values\n";
1139 }
1140 double k1 = e2->flat();
1141 double k2 = e2->flat();
1142#ifdef VERBOSER2
1143 output << "The " << e2->name() << " engine should not have been affected: \n"
1144 << "Next randoms are " << k1 << " " << k2 << "\n";
1145#endif
1146 if ( !equals(v1,k1) || !equals(v2,k2) ) {
1147 std::cout << "???? Engine used as theEngine was affected by restoring \n"
1148 << " static distributions to use engine of a different type.\n";
1149 stat |= 1048576;
1150 }
1151 return stat;
1152}
1153
1154// ----------- Vector restore of engines -----------
1155
1156template <class E>
1157std::vector<unsigned long> vectorRestore1(int n, std::vector<double> & v) {
1158 output << "Vector restore for " << E::engineName() << "\n";
1159 E e(97538466);
1160 double r=0;
1161 for (int i=0; i<n; i++) r += e.flat();
1162 std::vector<unsigned long> state = e.put();
1163 for (int j=0; j<25; j++) v.push_back(e.flat());
1164#ifdef VERBOSER2
1165 output << "First four of v are: "
1166 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
1167#endif
1168 return state;
1169}
1170
1171template <>
1172std::vector<unsigned long>
1173vectorRestore1<NonRandomEngine> (int n, std::vector<double> & v) {
1174#ifdef VERBOSER2
1175 output << "Vector restore for " << NonRandomEngine::engineName() << "\n";
1176#endif
1177 std::vector<double> nonRand = aSequence(500);
1178 NonRandomEngine e;
1179 e.setRandomSequence(&nonRand[0], nonRand.size());
1180 double r=0;
1181 for (int i=0; i<n; i++) r += e.flat();
1182 std::vector<unsigned long> state = e.put();
1183 for (int j=0; j<25; j++) v.push_back(e.flat());
1184#ifdef VERBOSER2
1185 output << "First four of v are: "
1186 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
1187#endif
1188 return state;
1189}
1190
1191template <class E>
1192int vectorRestore2(const std::vector<unsigned long> state,
1193 const std::vector<double> & v) {
1194 int stat = 0;
1195 std::vector<double> k;
1196 HepRandomEngine * a;
1197 a = HepRandomEngine::newEngine(state);
1198 if (!a) {
1199 std::cout << "???? could not restore engine state from vector for "
1200 << E::engineName() << "\n";
1201 stat |= 1048576;
1202 return stat;
1203 }
1204 if (a->name() != E::engineName()) {
1205 std::cout << "???? restored engine state from vector for "
1206 << E::engineName() << "to different type of engine: "
1207 << a->name() << "\n"
1208 << "There is probably a clash in CRC hashes for these two names!\n";
1209 stat |= 1048576;
1210 return stat;
1211 }
1212 for (int j=0; j<25; j++) k.push_back(a->flat());
1213 delete a;
1214#ifdef VERBOSER2
1215 output << "First four of k are: "
1216 << k[0] << ", " << k[1] << ", " << k[2] << ", " << k[3] << "\n";
1217#endif
1218 for (int m=0; m<25; m++) {
1219 if ( v[m] != k[m] ) {
1220 std::cout << "???? Incorrect vector restored value for anonymous engine: "
1221 << E::engineName() << "\n";
1222 stat |= 1048576;
1223 return stat;
1224 }
1225 }
1226 return stat;
1227}
1228
1229
1230template <class E>
1231int vectorRestore(int n) {
1232 std::vector<double> v;
1233 std::vector<unsigned long> state = vectorRestore1<E>(n,v);
1234 return vectorRestore2<E>(state, v);
1235}
1236
1237
1238
1239// ---------------------------------------------
1240// ---------------------------------------------
1241// ---------------------------------------------
1242
1243
1244int main() {
1245 int stat = 0;
1246
1247#ifdef TEST_ORIGINAL_SAVE
1248 output << "=====================================\n";
1249 output << " Part I \n";
1250 output << "Original tests of static save/restore\n";
1251 output << "=====================================\n\n";
1252
1253 output << "Using old method or HepRandom::saveEngineStatus:\n";
1254 output << "All these tests should have a chance of failure.\n";
1255
1256 output << RandGauss:: getTheEngine()->name();
1257 output << RandGaussQ::getTheEngine()->name();
1258
1259 stat |= saveStepX();
1260 stat |= restoreStepX();
1261 stat |= BsaveStepX();
1262 stat |= BrestoreStepX();
1263
1264 output << "Using the class-specific RandGauss::saveEngineStatus:\n";
1265 output << "All these tests should work properly.\n";
1266
1267 stat |= saveStep();
1268 stat |= restoreStep();
1269 stat |= BsaveStep();
1270 stat |= BrestoreStep();
1271#endif
1272
1273#ifdef TEST_MISSING_FILES
1274 output << "\n=======================================\n";
1275 output << " Part Ia \n";
1276 output << "Test of behavior when a file is missing \n";
1277 output << "=======================================\n\n";
1278
1279 output << "Testing restoreEngineStatus with missing file:\n";
1280 output << "Expect a number of <Failure to find or open> messages!\n";
1281 stat |= missingFile();
1282#endif
1283
1284#ifdef CREATE_OLD_SAVES
1285 stat |= saveEngine<DRand48Engine, RandPoisson>("DRand48Engine.oldsav");
1286 stat |= saveEngine<DualRand, RandPoisson>("DualRand.oldsav");
1287 stat |= saveEngine<Hurd160Engine, RandPoisson>("Hurd160Engine.oldsav");
1288 stat |= saveEngine<Hurd288Engine, RandPoisson>("Hurd288Engine.oldsav");
1289 stat |= saveEngine<HepJamesRandom,RandPoisson>("HepJamesRandom.oldsav");
1290 stat |= saveEngine<MixMaxRng, RandPoisson>("MixMaxRng.oldsav");
1291 stat |= saveEngine<MTwistEngine, RandPoisson>("MTwistEngine.oldsav");
1292 stat |= saveEngine<RanecuEngine, RandPoisson>("RanecuEngine.oldsav");
1293 stat |= saveEngine<Ranlux64Engine,RandPoisson>("Ranlux64Engine.oldsav");
1294 stat |= saveEngine<RanluxppEngine,RandPoisson>("RanluxppEngine.oldsav");
1295 stat |= saveEngine<RanluxEngine, RandPoisson>("RanluxEngine.oldsav");
1296 stat |= saveEngine<RanshiEngine, RandPoisson>("RanshiEngine.oldsav");
1297 stat |= saveEngine<TripleRand, RandPoisson>("TripleRand.oldsav");
1298#endif
1299
1300#ifdef VERIFY_OLD_SAVES
1301 output << "\n==============================================\n";
1302 output << " Part Ib \n";
1303 output << " Verification that changes wont invalidate \n";
1304 output << "invalidate engine saves from previous versions \n";
1305 output << "==============================================\n\n";
1306
1307 stat |= checkSaveEngine<DRand48Engine, RandPoisson>("DRand48Engine.oldsav");
1308 stat |= checkSaveEngine<DualRand, RandPoisson>("DualRand.oldsav");
1309 stat |= checkSaveEngine<Hurd160Engine, RandPoisson>("Hurd160Engine.oldsav");
1310 stat |= checkSaveEngine<Hurd288Engine, RandPoisson>("Hurd288Engine.oldsav");
1311 stat |= checkSaveEngine<HepJamesRandom,RandPoisson>("HepJamesRandom.oldsav");
1312 stat |= checkSaveEngine<MixMaxRng, RandPoisson>("MixMaxRng.oldsav");
1313 stat |= checkSaveEngine<MTwistEngine, RandPoisson>("MTwistEngine.oldsav");
1314 stat |= checkSaveEngine<Ranlux64Engine,RandPoisson>("Ranlux64Engine.oldsav");
1315 stat |= checkSaveEngine<RanluxEngine, RandPoisson>("RanluxEngine.oldsav");
1316 stat |= checkSaveEngine<RanluxppEngine,RandPoisson>("RanluxppEngine.oldsav");
1317 stat |= checkSaveEngine<RanshiEngine, RandPoisson>("RanshiEngine.oldsav");
1318 stat |= checkSaveEngine<TripleRand, RandPoisson>("TripleRand.oldsav");
1319 stat |= checkSaveEngine<RanecuEngine, RandPoisson>("RanecuEngine.oldsav");
1320#endif
1321
1322#ifdef TEST_ENGINE_NAMES
1323 output << "\n=============================================\n";
1324 output << " Part II \n";
1325 output << "Check all engine names were entered correctly \n";
1326 output << "=============================================\n\n";
1327
1328 stat |= checkEngineName<DRand48Engine >("DRand48Engine");
1329 stat |= checkEngineName<DualRand >("DualRand");
1330 stat |= checkEngineName<Hurd160Engine >("Hurd160Engine");
1331 stat |= checkEngineName<Hurd288Engine >("Hurd288Engine");
1332 stat |= checkEngineName<HepJamesRandom>("HepJamesRandom");
1333 stat |= checkEngineName<MixMaxRng >("MixMaxRng");
1334 stat |= checkEngineName<MTwistEngine >("MTwistEngine");
1335 stat |= checkEngineName<RandEngine >("RandEngine");
1336 stat |= checkEngineName<RanecuEngine >("RanecuEngine");
1337 stat |= checkEngineName<Ranlux64Engine>("Ranlux64Engine");
1338 stat |= checkEngineName<RanluxEngine >("RanluxEngine");
1339 stat |= checkEngineName<RanluxppEngine>("RanluxppEngine");
1340 stat |= checkEngineName<RanshiEngine >("RanshiEngine");
1341 stat |= checkEngineName<TripleRand >("TripleRand");
1342#endif
1343
1344#ifdef TEST_INSTANCE_METHODS
1345 output << "===========================================\n\n";
1346 output << " Part III\n";
1347 output << "Check instance methods for specific engines \n";
1348 output << " specific engines and distributions\n";
1349 output << "===========================================\n\n";
1350
1351 {DualRand e(234); stat |= checkEngineInstanceSave(e);}
1352 {Hurd160Engine e(234); stat |= checkEngineInstanceSave(e);}
1353 {Hurd288Engine e(234); stat |= checkEngineInstanceSave(e);}
1354 {HepJamesRandom e(234); stat |= checkEngineInstanceSave(e);}
1355 {MixMaxRng e(234); stat |= checkEngineInstanceSave(e);}
1356 {MTwistEngine e(234); stat |= checkEngineInstanceSave(e);}
1357 {RandEngine e(234); stat |= checkEngineInstanceSave(e);}
1358 {RanecuEngine e(234); stat |= checkEngineInstanceSave(e);}
1359 {Ranlux64Engine e(234); stat |= checkEngineInstanceSave(e);}
1360 {RanluxEngine e(234); stat |= checkEngineInstanceSave(e);}
1361 {RanluxppEngine e(234); stat |= checkEngineInstanceSave(e);}
1362 {RanshiEngine e(234); stat |= checkEngineInstanceSave(e);}
1363 {TripleRand e(234); stat |= checkEngineInstanceSave(e);}
1364
1365 {std::vector<double> nonRand = aSequence(500);
1366 NonRandomEngine e;
1367 e.setRandomSequence(&nonRand[0], nonRand.size());
1368 stat |= checkEngineInstanceSave(e);}
1369
1370 stat |= checkDistributions<DualRand>();
1371 stat |= checkDistributions<Hurd160Engine>();
1372 stat |= checkDistributions<Hurd288Engine>();
1373 stat |= checkDistributions<HepJamesRandom>();
1374 stat |= checkDistributions<MixMaxRng>();
1375 stat |= checkDistributions<MTwistEngine>();
1376 stat |= checkDistributions<Ranlux64Engine>();
1377 stat |= checkDistributions<RanluxEngine>();
1378 stat |= checkDistributions<RanluxppEngine>();
1379 stat |= checkDistributions<RanshiEngine>();
1380 stat |= checkDistributions<TripleRand>();
1381
1382 RandGaussQ::shoot(); // Just to verify that the static engine is OK
1383#endif
1384
1385#ifdef TEST_SHARED_ENGINES
1386 output << "\n=============================================\n";
1387 output << " Part IV \n";
1388 output << "Check behavior when engines are shared \n";
1389 output << "=============================================\n\n";
1390
1391 stat |= checkSharing<DualRand>();
1392 stat |= checkSharing<Hurd160Engine>();
1393 stat |= checkSharing<Hurd288Engine>();
1394 stat |= checkSharing<HepJamesRandom>();
1395 stat |= checkSharing<MixMaxRng>();
1396 stat |= checkSharing<MTwistEngine>();
1397 stat |= checkSharing<Ranlux64Engine>();
1398 stat |= checkSharing<RanluxEngine>();
1399 stat |= checkSharing<RanluxppEngine>();
1400 stat |= checkSharing<RanshiEngine>();
1401 stat |= checkSharing<TripleRand>();
1402#endif
1403
1404#ifdef TEST_STATIC_SAVE
1405 output << "\n=========================================\n";
1406 output << " Part V \n";
1407 output << "Static Save/restore to/from streams \n";
1408 output << "=========================================\n\n";
1409
1410 stat |= staticSave <RandGauss>(7);
1411 stat |= staticSave <RandFlat>(7);
1412 stat |= staticSaveShootBit<RandFlat> (19);
1413 stat |= staticSaveShootBit<RandBit> (23);
1414 for (int ibinom=0; ibinom<15; ibinom++) {
1415 stat |= staticSave <RandBinomial>(7+3*ibinom);
1416 }
1417 stat |= staticSave <RandBreitWigner>(7);
1418 stat |= staticSave <RandChiSquare>(7);
1419 stat |= staticSave <RandExponential>(7);
1420 stat |= staticSave <RandGamma>(7);
1421 stat |= staticSave <RandGaussQ>(7);
1422 stat |= staticSave <RandGaussT>(7);
1423 stat |= staticSave <RandLandau>(7);
1424 stat |= staticSave <RandPoisson>(7);
1425 stat |= staticSave <RandPoissonQ>(7);
1426 stat |= staticSave <RandPoissonT>(7);
1427 stat |= staticSave <RandSkewNormal>(7);
1428 stat |= staticSave <RandStudentT>(7);
1429#endif
1430
1431#ifdef TEST_SAVE_STATIC_STATES
1432 output << "\n==============================================\n";
1433 output << " Part VI \n";
1434 output << "Save/restore all static states to/from streams \n";
1435 output << "==============================================\n\n";
1436
1437 randomizeStatics(15);
1438 saveStatics("distribution.save");
1439 output << "Saved all static distributions\n";
1440 std::vector<double> c = captureStatics();
1441 output << "Captured output of all static distributions\n";
1442 randomizeStatics(11);
1443 output << "Randomized all static distributions\n";
1444 restoreStatics("distribution.save");
1445 output << "Restored all static distributions to saved state\n";
1446 std::vector<double> d = captureStatics();
1447 output << "Captured output of all static distributions\n";
1448 for (unsigned int iv=0; iv<c.size(); iv++) {
1449 if (c[iv] != d[iv]) {
1450 std::cout << "???? restoreStaticRandomStates failed at random "
1451 << iv <<"\n";
1452 stat |= 131072;
1453 }
1454 }
1455 if (stat & 131072 == 0) {
1456 output << "All captured output agrees with earlier values\n";
1457 }
1458#endif
1459
1460#ifdef TEST_ANONYMOUS_ENGINE_RESTORE
1461 output << "\n=================================\n";
1462 output << " Part VII \n";
1463 output << "Anonymous restore of engines \n";
1464 output << "=================================\n\n";
1465
1466 stat |= anonymousRestore<DualRand>(13);
1467 stat |= anonymousRestore<DRand48Engine>(14);
1468 stat |= anonymousRestore<Hurd160Engine>(15);
1469 stat |= anonymousRestore<Hurd288Engine>(16);
1470 stat |= anonymousRestore<HepJamesRandom>(17);
1471 stat |= anonymousRestore<MixMaxRng>(49);
1472 stat |= anonymousRestore<MTwistEngine>(18);
1473 stat |= anonymousRestore<RandEngine>(29);
1474 stat |= anonymousRestore<RanecuEngine>(39);
1475 stat |= anonymousRestore<Ranlux64Engine>(19);
1476 stat |= anonymousRestore<RanluxEngine>(20);
1477 stat |= anonymousRestore<RanluxppEngine>(19);
1478 stat |= anonymousRestore<RanshiEngine>(21);
1479 stat |= anonymousRestore<TripleRand>(22);
1480 stat |= anonymousRestore<NonRandomEngine>(22);
1481#endif
1482
1483#ifdef TEST_ANONYMOUS_RESTORE_STATICS
1484 output << "\n======================================\n";
1485 output << " Part VIII \n";
1486 output << "Anonymous restore static Distributions \n";
1487 output << "======================================\n\n";
1488
1489 stat |= anonymousRestoreStatics<DualRand, Ranlux64Engine> ( );
1490 stat |= anonymousRestoreStatics<DRand48Engine, TripleRand> ( );
1491 stat |= anonymousRestoreStatics<RandEngine, Ranlux64Engine> ( );
1492 stat |= anonymousRestoreStatics<MTwistEngine, Hurd288Engine> ( );
1493 stat |= anonymousRestoreStatics<RanecuEngine, MTwistEngine> ( );
1494 stat |= anonymousRestoreStatics<HepJamesRandom, RanshiEngine> ( );
1495 stat |= anonymousRestoreStatics<RanecuEngine, RandEngine> ( );
1496 stat |= anonymousRestoreStatics<RanshiEngine, Hurd160Engine> ( );
1497 stat |= anonymousRestoreStatics<TripleRand, DualRand> ( );
1498 stat |= anonymousRestoreStatics<Hurd160Engine, HepJamesRandom> ( );
1499 stat |= anonymousRestoreStatics<Hurd288Engine, RanecuEngine> ( );
1500 stat |= anonymousRestoreStatics<HepJamesRandom, Ranlux64Engine> ( );
1501 stat |= anonymousRestoreStatics<HepJamesRandom, RanluxppEngine> ( );
1502 stat |= anonymousRestoreStatics<TripleRand, TripleRand> ( );
1503 stat |= anonymousRestoreStatics<HepJamesRandom, HepJamesRandom> ( );
1504 stat |= anonymousRestoreStatics<MixMaxRng, MixMaxRng> ( );
1505#endif
1506
1507#ifdef TEST_VECTOR_ENGINE_RESTORE
1508 output << "\n=================================\n";
1509 output << " Part IX \n";
1510 output << "Save/restore of engines to vectors\n";
1511 output << "=================================\n\n";
1512
1513 stat |= vectorRestore<DualRand>(113);
1514 stat |= vectorRestore<DRand48Engine>(114);
1515 stat |= vectorRestore<Hurd160Engine>(115);
1516 stat |= vectorRestore<Hurd288Engine>(116);
1517 stat |= vectorRestore<HepJamesRandom>(117);
1518 stat |= vectorRestore<MixMaxRng>(149);
1519 stat |= vectorRestore<MTwistEngine>(118);
1520 stat |= vectorRestore<RanecuEngine>(139);
1521 stat |= vectorRestore<Ranlux64Engine>(119);
1522 stat |= vectorRestore<RanluxEngine>(120);
1523 stat |= vectorRestore<RanluxppEngine>(159);
1524 stat |= vectorRestore<RanluxEngine>(120);
1525 stat |= vectorRestore<RanshiEngine>(121);
1526 stat |= vectorRestore<TripleRand>(122);
1527 stat |= vectorRestore<NonRandomEngine>(123);
1528 stat |= vectorRestore<RandEngine>(129);
1529#endif
1530
1531
1532
1533
1534 output << "\n=============================================\n\n";
1535
1536 if (stat != 0) {
1537 std::cout << "One or more problems detected: stat = " << stat << "\n";
1538 } else {
1539 output << "ranRestoreTest passed with no problems detected.\n";
1540 }
1541
1542 return stat;
1543}
1544
virtual double flat()=0
virtual std::string name() const =0
static HepRandomEngine * newEngine(std::istream &is)
Definition: RandomEngine.cc:93
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:285
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition: Random.cc:275
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:280
virtual std::ostream & put(std::ostream &os) const
void setRandomSequence(double *s, int n)
static std::string engineName()
static double shoot()
static int shootBit()
static double shoot(double a=1.0, double b=0.2)
static double shoot()
static int shootBit()
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:122
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:107
static double shoot()
Definition: RandFlat.cc:63
static double shoot()
static double shoot()
static double shoot()
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: RandGauss.cc:213
static double shoot()
Definition: RandGauss.cc:64
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: RandGauss.cc:193
std::string name() const
Definition: RandGeneral.cc:60
HepRandomEngine & engine()
Definition: RandGeneral.cc:61
static double shoot()
static long shoot(double mean=1.0)
static long shoot(double mean=1.0)
Definition: RandPoissonT.cc:60
static long shoot(double mean=1.0)
Definition: RandPoisson.cc:95
static double shoot()
static double shoot()
void f(void g())
Definition: excDblThrow.cc:38
std::ofstream output("ranRestoreTest.cout")
int checkEngineInstanceSave(E &e)
int saveStepX()
int checkDistributions()
int checkSaveDistribution(D &d, int nth)
int restoreStep()
int checkSharing()
int staticSave(int n)
int checkEngine()
int saveEngine(const char *filename)
int anonymousRestoreStatics()
int checkEngineName(const std::string &name)
int checkRandGeneralDistribution(RandGeneral &d, int nth)
int missingFile()
int BrestoreStep()
int fileNotThereEngine()
void saveStatics(std::string filename)
int BsaveStep()
int staticSaveShootBit(int n)
double remembered_r2
double remembered_r1007
void randomizeStatics(int n)
void anonymousRestore1(int n, std::vector< double > &v)
double remembered_r1006
int checkSaveEngine(const char *filename)
std::vector< double > aSequence(int n)
int BsaveStepX()
int vectorRestore(int n)
int anonymousRestoreStatics1()
int restoreStepX()
int checkSharingDistributions(D1 &d1, D2 &d2, int n1, int n2)
void restoreStatics(std::string filename)
bool equals(double a, double b)
std::vector< double > captureStatics()
bool equals01(const std::vector< double > &ab)
int saveStep()
int fileNotThere()
double remembered_r1005
int vectorRestore2(const std::vector< unsigned long > state, const std::vector< double > &v)
void anonymousRestore1< NonRandomEngine >(int n, std::vector< double > &v)
int anonymousRestore2(const std::vector< double > &v)
std::vector< unsigned long > vectorRestore1< NonRandomEngine >(int n, std::vector< double > &v)
std::vector< unsigned long > vectorRestore1(int n, std::vector< double > &v)
int main()
int anonymousRestore(int n)
int BrestoreStepX()
Definition: excDblThrow.cc:17