1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
// ----------------------------------------------------------------------
#include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings
#include "CLHEP/Random/Randomize.h"
#include "CLHEP/Random/NonRandomEngine.h"
#include "CLHEP/Random/defs.h"
#include <iostream>
#include <iomanip>
#include <vector>
#define CLEAN_OUTPUT
#ifdef CLEAN_OUTPUT
std::ofstream output("testAnonymousEngineRestore.cout");
#else
std::ostream & output = std::cout;
#endif
// Normally on for routine validation:
#ifdef TURNOFF
#endif
#define TEST_ANONYMOUS_ENGINE_RESTORE
#define TEST_ANONYMOUS_RESTORE_STATICS
#define VERBOSER
#define VERBOSER2
using namespace CLHEP;
template <class E1, class E2> int anonymousRestoreStatics();
// Absolutely Safe Equals Without Registers Screwing Us Up
bool equals01(const std::vector<double> &ab) {
return ab[1]==ab[0];
}
bool equals(double a, double b) {
std::vector<double> ab(2);
ab[0]=a; ab[1]=b;
return (equals01(ab));
}
std::vector<double> aSequence(int n) {
std::vector<double> v;
DualRand e(13542);
RandFlat f(e);
for (int i=0; i<n; i++) {
v.push_back(f());
}
return v;
}
// ----------- Tests saving all statics together -----------
void randomizeStatics(int n) {
for (int i=0; i<n; i++) {
RandGauss::shoot();
RandGaussQ::shoot();
RandGaussT::shoot();
RandFlat::shoot();
RandBit::shoot();
RandFlat::shootBit();
RandBit::shootBit();
RandPoisson::shoot();
RandPoissonQ::shoot();
RandPoissonT::shoot();
RandBinomial::shoot();
RandBreitWigner::shoot();
RandChiSquare::shoot();
RandExponential::shoot();
RandGamma::shoot();
RandLandau::shoot();
RandSkewNormal::shoot();
RandStudentT::shoot();
}
}
std::vector<double> captureStatics() {
std::vector<double> c;
c.push_back( RandGauss::shoot() );
c.push_back( RandGaussQ::shoot() );
c.push_back( RandGaussT::shoot() );
c.push_back( RandFlat::shoot() );
c.push_back( RandBit::shoot() );
for (int i=0; i<20; i++) {
c.push_back( RandFlat::shootBit() );
c.push_back( RandBit::shootBit() );
}
c.push_back( RandPoisson::shoot() );
c.push_back( RandPoissonQ::shoot() );
c.push_back( RandPoissonT::shoot() );
c.push_back( RandBinomial::shoot() );
c.push_back( RandBreitWigner::shoot() );
c.push_back( RandChiSquare::shoot() );
c.push_back( RandExponential::shoot() );
c.push_back( RandGamma::shoot() );
c.push_back( RandLandau::shoot() );
c.push_back( RandSkewNormal::shoot() );
c.push_back( RandStudentT::shoot() );
return c;
}
void saveStatics(std::string filename) {
std::ofstream os(filename.c_str());
RandGeneral::saveStaticRandomStates(os);
// It should be possible to call this from HepRandom, or any distribution.
// RandGeneral, which is meaningless as a static distribution, should be the
// toughest test, so we use that here.
}
void restoreStatics(std::string filename) {
std::ifstream is(filename.c_str());
RandLandau::restoreStaticRandomStates(is);
}
// ----------- Anonymous restore of engines -----------
template <class E>
void anonymousRestore1(int n, std::vector<double> & v) {
output << "Anonymous restore for " << E::engineName() << "\n";
E e(12349876);
double r=0;
for (int i=0; i<n; i++) r += e.flat();
std::ofstream os("anonymous.save");
os << e;
for (int j=0; j<25; j++) v.push_back(e.flat());
#ifdef VERBOSER2
output << "First four of v are: "
<< v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
#endif
return;
}
template <>
void anonymousRestore1<NonRandomEngine> (int n, std::vector<double> & v) {
#ifdef VERBOSER
output << "Anonymous restore for " << NonRandomEngine::engineName() << "\n";
#endif
std::vector<double> nonRand = aSequence(500);
NonRandomEngine e;
e.setRandomSequence(&nonRand[0], nonRand.size());
double r=0;
for (int i=0; i<n; i++) r += e.flat();
std::ofstream os("anonymous.save");
os << e;
for (int j=0; j<25; j++) v.push_back(e.flat());
#ifdef VERBOSER2
output << "First four of v are: "
<< v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
#endif
return;
}
template <class E>
int anonymousRestore2(const std::vector<double> & v) {
int stat = 0;
std::vector<double> k;
std::ifstream is("anonymous.save");
HepRandomEngine * a;
a = HepRandomEngine::newEngine(is);
for (int j=0; j<25; j++) k.push_back(a->flat());
delete a;
#ifdef VERBOSER2
output << "First four of k are: "
<< k[0] << ", " << k[1] << ", " << k[2] << ", " << k[3] << "\n";
#endif
for (int m1=0; m1<25; m1++) {
if ( v[m1] != k[m1] ) {
std::cout << "???? Incorrect restored value for anonymous engine"
<< E::engineName() << "\n";
#ifdef CLEAN_OUTPUT
output << "???? Incorrect restored value for anonymous engine"
<< E::engineName() << "\n";
#endif
stat |= 262144;
return stat;
}
}
return stat;
}
template <class E>
int anonymousRestore(int n) {
std::vector<double> v;
anonymousRestore1<E>(n,v);
return anonymousRestore2<E>(v);
}
// ----------- Anonymous restore of all static distributions -----------
template <class E>
int anonymousRestoreStatics1() {
int stat = 0;
HepRandomEngine *e = new E(12456);
HepRandom::setTheEngine(e);
randomizeStatics(15);
output << "\nRandomized, with theEngine = " << e->name() << "\n";
saveStatics("anon_distribution.save");
output << "Saved all static distributions\n";
std::vector<double> c = captureStatics();
output << "Captured output of all static distributions\n";
randomizeStatics(11);
output << "Randomized all static distributions\n";
restoreStatics("anon_distribution.save");
output << "Restored all static distributions to saved state\n";
std::vector<double> d = captureStatics();
output << "Captured output of all static distributions\n";
for (unsigned int iv=0; iv<c.size(); iv++) {
if (c[iv] != d[iv]) {
std::cout << "???? restoreStaticRandomStates failed at random "
<< iv <<"\n";
#ifdef CLEAN_OUTPUT
output << "???? restoreStaticRandomStates failed at random "
<< iv <<"\n";
#endif
stat |= 131072;
}
}
if ( (stat & 131072) == 0) {
output << "All captured output agrees with earlier values\n";
}
return stat;
}
template <class E1, class E2>
int anonymousRestoreStatics() {
int stat = 0;
if ( E1::engineName() == E2::engineName() ) {
return anonymousRestoreStatics1<E1>();
}
HepRandomEngine *e1 = new E1(12456);
HepRandom::setTheEngine(e1);
randomizeStatics(15);
output << "\nRandomized, with theEngine = " << e1->name() << "\n";
saveStatics("anon_distribution.save");
#ifdef VERBOSER2
output << "Saved all static distributions\n";
#endif
std::vector<double> c = captureStatics();
#ifdef VERBOSER2
output << "Captured output of all static distributions\n";
#endif
delete e1;
HepRandomEngine *e2 = new E2(24653);
HepRandom::setTheEngine(e2);
output << "Switched to theEngine = " << e2->name() << "\n";
randomizeStatics(19);
{ std::ofstream os("anon_engine.save"); os << *e2; }
double v1 = e2->flat();
double v2 = e2->flat();
{ std::ifstream is("anon_engine.save"); is >> *e2; }
#ifdef VERBOSER2
output << "Saved the " << e2->name() << " engine: \n"
<< "Next randoms to be " << v1 << " " << v2 << "\n"
<< "Restored the " << e2->name() << " engine to that state\n";
#endif
restoreStatics("anon_distribution.save");
#ifdef VERBOSER2
output << "Restored all static distributions to saved state\n"
<< "This changes the engine type back to " << E1::engineName() << "\n";
#endif
std::vector<double> d = captureStatics();
#ifdef VERBOSER2
output << "Captured output of all static distributions\n";
#endif
for (unsigned int iv=0; iv<c.size(); iv++) {
if (c[iv] != d[iv]) {
std::cout << "???? restoreStaticRandomStates failed at random "
<< iv <<"\n";
#ifdef CLEAN_OUTPUT
output << "???? restoreStaticRandomStates failed at random "
<< iv <<"\n";
#endif
stat |= 524288;
}
}
if ((stat & 524288) == 0) {
output << "All captured output agrees with earlier values\n";
}
double k1 = e2->flat();
double k2 = e2->flat();
#ifdef VERBOSER2
output << "The " << e2->name() << " engine should not have been affected: \n"
<< "Next randoms are " << k1 << " " << k2 << "\n";
#endif
if ( !equals(v1,k1) || !equals(v2,k2) ) {
std::cout << "???? Engine used as theEngine was affected by restoring \n"
<< " static distributions to use engine of a different type.\n";
#ifdef CLEAN_OUTPUT
output << "???? Engine used as theEngine was affected by restoring \n"
<< " static distributions to use engine of a different type.\n";
#endif
stat |= 1048576;
}
return stat;
}
// ---------------------------------------------
// ---------------------------------------------
// ---------------------------------------------
int main() {
int stat = 0;
#ifdef TEST_ANONYMOUS_ENGINE_RESTORE
output << "\n=================================\n";
output << " Part VII \n";
output << "Anonymous restore of engines \n";
output << "=================================\n\n";
stat |= anonymousRestore<DualRand>(13);
stat |= anonymousRestore<DRand48Engine>(14);
stat |= anonymousRestore<Hurd160Engine>(15);
stat |= anonymousRestore<Hurd288Engine>(16);
stat |= anonymousRestore<HepJamesRandom>(17);
stat |= anonymousRestore<MTwistEngine>(18);
stat |= anonymousRestore<RandEngine>(29);
stat |= anonymousRestore<RanecuEngine>(39);
stat |= anonymousRestore<Ranlux64Engine>(19);
stat |= anonymousRestore<RanluxEngine>(20);
stat |= anonymousRestore<RanshiEngine>(21);
stat |= anonymousRestore<TripleRand>(22);
stat |= anonymousRestore<NonRandomEngine>(22);
#endif
#ifdef TEST_ANONYMOUS_RESTORE_STATICS
output << "\n======================================\n";
output << " Part VIII \n";
output << "Anonymous restore static Distributions \n";
output << "======================================\n\n";
stat |= anonymousRestoreStatics<DualRand, Ranlux64Engine> ( );
stat |= anonymousRestoreStatics<DRand48Engine, TripleRand> ( );
stat |= anonymousRestoreStatics<RandEngine, Ranlux64Engine> ( );
stat |= anonymousRestoreStatics<MTwistEngine, Hurd288Engine> ( );
stat |= anonymousRestoreStatics<RanecuEngine, MTwistEngine> ( );
stat |= anonymousRestoreStatics<HepJamesRandom, RanshiEngine> ( );
stat |= anonymousRestoreStatics<RanecuEngine, RandEngine> ( );
stat |= anonymousRestoreStatics<RanshiEngine, Hurd160Engine> ( );
stat |= anonymousRestoreStatics<TripleRand, DualRand> ( );
stat |= anonymousRestoreStatics<Hurd160Engine, HepJamesRandom> ( );
stat |= anonymousRestoreStatics<Hurd288Engine, RanecuEngine> ( );
stat |= anonymousRestoreStatics<HepJamesRandom, Ranlux64Engine> ( );
stat |= anonymousRestoreStatics<TripleRand, TripleRand> ( );
stat |= anonymousRestoreStatics<HepJamesRandom, HepJamesRandom> ( );
#endif
output << "\n=============================================\n\n";
if (stat != 0) {
std::cout << "One or more problems detected: stat = " << stat << "\n";
output << "One or more problems detected: stat = " << stat << "\n";
} else {
output << "testAnonymousEngineRestore passed with no problems detected.\n";
}
if (stat == 0) return 0;
if (stat > 0) return -(stat|1);
return stat|1;
}
|