2#include "CLHEP/Units/GlobalPhysicalConstants.h"
3#include "CLHEP/Random/Randomize.h"
4#include "CLHEP/Random/NonRandomEngine.h"
5#include "CLHEP/Random/defs.h"
12 std::ofstream
output(
"testAnonymousEngineRestore.cout");
14 std::ostream &
output = std::cout;
22#define TEST_ANONYMOUS_ENGINE_RESTORE
23#define TEST_ANONYMOUS_RESTORE_STATICS
38 std::vector<double> ab(2);
44 std::vector<double> v;
47 for (
int i=0; i<n; i++) {
56 for (
int i=0; i<n; i++) {
79 std::vector<double> c;
84 c.push_back( RandBit::shoot() );
85 for (
int i=0; i<20; i++) {
104 std::ofstream os(filename.c_str());
105 RandGeneral::saveStaticRandomStates(os);
112 std::ifstream is(filename.c_str());
113 RandLandau::restoreStaticRandomStates(is);
122 output <<
"Anonymous restore for " << E::engineName() <<
"\n";
124 double __attribute__ ((unused)) r = 0;
125 for (
int i=0; i<n; i++) r += e.flat();
126 std::ofstream os(
"anonymous.save");
128 for (
int j=0; j<25; j++) v.push_back(e.flat());
130 output <<
"First four of v are: "
131 << v[0] <<
", " << v[1] <<
", " << v[2] <<
", " << v[3] <<
"\n";
141 std::vector<double> nonRand =
aSequence(500);
144 double __attribute__ ((unused)) r = 0;
145 for (
int i=0; i<n; i++) r += e.
flat();
146 std::ofstream os(
"anonymous.save");
148 for (
int j=0; j<25; j++) v.push_back(e.
flat());
150 output <<
"First four of v are: "
151 << v[0] <<
", " << v[1] <<
", " << v[2] <<
", " << v[3] <<
"\n";
159 std::vector<double> k;
160 std::ifstream is(
"anonymous.save");
163 for (
int j=0; j<25; j++) k.push_back(a->
flat());
166 output <<
"First four of k are: "
167 << k[0] <<
", " << k[1] <<
", " << k[2] <<
", " << k[3] <<
"\n";
169 for (
int m1=0; m1<25; m1++) {
170 if ( v[m1] != k[m1] ) {
171 std::cout <<
"???? Incorrect restored value for anonymous engine"
172 << E::engineName() <<
"\n";
174 output <<
"???? Incorrect restored value for anonymous engine"
175 << E::engineName() <<
"\n";
187 std::vector<double> v;
188 anonymousRestore1<E>(n,v);
189 return anonymousRestore2<E>(v);
200 output <<
"\nRandomized, with theEngine = " << e->
name() <<
"\n";
202 output <<
"Saved all static distributions\n";
204 output <<
"Captured output of all static distributions\n";
206 output <<
"Randomized all static distributions\n";
208 output <<
"Restored all static distributions to saved state\n";
210 output <<
"Captured output of all static distributions\n";
211 for (
unsigned int iv=0; iv<c.size(); iv++) {
212 if (c[iv] != d[iv]) {
213 std::cout <<
"???? restoreStaticRandomStates failed at random "
216 output <<
"???? restoreStaticRandomStates failed at random "
222 if ( (stat & 131072) == 0) {
223 output <<
"All captured output agrees with earlier values\n";
230template <
class E1,
class E2>
233 if ( E1::engineName() == E2::engineName() ) {
234 return anonymousRestoreStatics1<E1>();
239 output <<
"\nRandomized, with theEngine = " << e1->
name() <<
"\n";
242 output <<
"Saved all static distributions\n";
246 output <<
"Captured output of all static distributions\n";
251 output <<
"Switched to theEngine = " << e2->
name() <<
"\n";
253 { std::ofstream os(
"anon_engine.save"); os << *e2; }
254 double v1 = e2->
flat();
255 double v2 = e2->
flat();
256 { std::ifstream is(
"anon_engine.save"); is >> *e2; }
258 output <<
"Saved the " << e2->
name() <<
" engine: \n"
259 <<
"Next randoms to be " << v1 <<
" " << v2 <<
"\n"
260 <<
"Restored the " << e2->
name() <<
" engine to that state\n";
264 output <<
"Restored all static distributions to saved state\n"
265 <<
"This changes the engine type back to " << E1::engineName() <<
"\n";
269 output <<
"Captured output of all static distributions\n";
271 for (
unsigned int iv=0; iv<c.size(); iv++) {
272 if (c[iv] != d[iv]) {
273 std::cout <<
"???? restoreStaticRandomStates failed at random "
276 output <<
"???? restoreStaticRandomStates failed at random "
282 if ((stat & 524288) == 0) {
283 output <<
"All captured output agrees with earlier values\n";
285 double k1 = e2->
flat();
286 double k2 = e2->
flat();
288 output <<
"The " << e2->
name() <<
" engine should not have been affected: \n"
289 <<
"Next randoms are " << k1 <<
" " << k2 <<
"\n";
292 std::cout <<
"???? Engine used as theEngine was affected by restoring \n"
293 <<
" static distributions to use engine of a different type.\n";
295 output <<
"???? Engine used as theEngine was affected by restoring \n"
296 <<
" static distributions to use engine of a different type.\n";
311#ifdef TEST_ANONYMOUS_ENGINE_RESTORE
312 output <<
"\n=================================\n";
314 output <<
"Anonymous restore of engines \n";
315 output <<
"=================================\n\n";
317 stat |= anonymousRestore<DualRand>(13);
318 stat |= anonymousRestore<DRand48Engine>(14);
319 stat |= anonymousRestore<Hurd160Engine>(15);
320 stat |= anonymousRestore<Hurd288Engine>(16);
321 stat |= anonymousRestore<HepJamesRandom>(17);
322 stat |= anonymousRestore<MixMaxRng>(49);
323 stat |= anonymousRestore<MTwistEngine>(18);
324 stat |= anonymousRestore<RandEngine>(29);
325 stat |= anonymousRestore<RanecuEngine>(39);
326 stat |= anonymousRestore<Ranlux64Engine>(19);
327 stat |= anonymousRestore<RanluxEngine>(20);
328 stat |= anonymousRestore<RanluxppEngine>(59);
329 stat |= anonymousRestore<RanshiEngine>(21);
330 stat |= anonymousRestore<TripleRand>(22);
331 stat |= anonymousRestore<NonRandomEngine>(22);
334#ifdef TEST_ANONYMOUS_RESTORE_STATICS
335 output <<
"\n======================================\n";
336 output <<
" Part VIII \n";
337 output <<
"Anonymous restore static Distributions \n";
338 output <<
"======================================\n\n";
340 stat |= anonymousRestoreStatics<DualRand, Ranlux64Engine> ( );
341 stat |= anonymousRestoreStatics<DRand48Engine, TripleRand> ( );
342 stat |= anonymousRestoreStatics<RandEngine, Ranlux64Engine> ( );
343 stat |= anonymousRestoreStatics<MTwistEngine, Hurd288Engine> ( );
344 stat |= anonymousRestoreStatics<RanecuEngine, MTwistEngine> ( );
345 stat |= anonymousRestoreStatics<HepJamesRandom, RanshiEngine> ( );
346 stat |= anonymousRestoreStatics<RanecuEngine, RandEngine> ( );
347 stat |= anonymousRestoreStatics<RanshiEngine, Hurd160Engine> ( );
348 stat |= anonymousRestoreStatics<TripleRand, DualRand> ( );
349 stat |= anonymousRestoreStatics<Hurd160Engine, HepJamesRandom> ( );
350 stat |= anonymousRestoreStatics<Hurd288Engine, RanecuEngine> ( );
351 stat |= anonymousRestoreStatics<HepJamesRandom, Ranlux64Engine> ( );
352 stat |= anonymousRestoreStatics<HepJamesRandom, RanluxppEngine> ( );
353 stat |= anonymousRestoreStatics<TripleRand, TripleRand> ( );
354 stat |= anonymousRestoreStatics<HepJamesRandom, HepJamesRandom> ( );
355 stat |= anonymousRestoreStatics<MixMaxRng, MixMaxRng> ( );
359 output <<
"\n=============================================\n\n";
362 std::cout <<
"One or more problems detected: stat = " << stat <<
"\n";
363 output <<
"One or more problems detected: stat = " << stat <<
"\n";
365 output <<
"testAnonymousEngineRestore passed with no problems detected.\n";
368 if (stat == 0)
return 0;
369 if (stat > 0)
return -(stat|1);
virtual std::string name() const =0
static HepRandomEngine * newEngine(std::istream &is)
static void setTheEngine(HepRandomEngine *theNewEngine)
void setRandomSequence(double *s, int n)
static std::string engineName()
static double shoot(double a=1.0, double b=0.2)
static long shoot(double mean=1.0)
static long shoot(double mean=1.0)
static long shoot(double mean=1.0)
int anonymousRestoreStatics()
void saveStatics(std::string filename)
void randomizeStatics(int n)
void anonymousRestore1(int n, std::vector< double > &v)
std::vector< double > aSequence(int n)
int anonymousRestoreStatics1()
void restoreStatics(std::string filename)
bool equals(double a, double b)
std::vector< double > captureStatics()
bool equals01(const std::vector< double > &ab)
std::ofstream output("testAnonymousEngineRestore.cout")
void anonymousRestore1< NonRandomEngine >(int n, std::vector< double > &v)
int anonymousRestore2(const std::vector< double > &v)
int anonymousRestore(int n)