CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // ---------------------------------------------------------------------- 00002 #include "CLHEP/Random/Randomize.h" 00003 #include "CLHEP/Random/NonRandomEngine.h" 00004 #include "CLHEP/Random/defs.h" 00005 #include <iostream> 00006 #include <iomanip> 00007 #include <vector> 00008 00009 #define CLEAN_OUTPUT 00010 #ifdef CLEAN_OUTPUT 00011 std::ofstream output("testStaticStreamSave.cout"); 00012 #else 00013 std::ostream & output = std::cout; 00014 #endif 00015 00016 // Normally on for routine validation: 00017 00018 #ifdef TURNOFF 00019 #endif 00020 00021 #define TEST_STATIC_SAVE 00022 #define TEST_SAVE_STATIC_STATES 00023 00024 00025 #define VERBOSER 00026 #define VERBOSER2 00027 00028 using namespace CLHEP; 00029 00030 // Absolutely Safe Equals Without Registers Screwing Us Up 00031 bool equals01(const std::vector<double> &ab) { 00032 return ab[1]==ab[0]; 00033 } 00034 bool equals(double a, double b) { 00035 std::vector<double> ab(2); 00036 ab[0]=a; ab[1]=b; 00037 return (equals01(ab)); 00038 } 00039 00040 // ----------- Tests for static methods ----------- 00041 00042 template <class D> 00043 int staticSave(int n) { 00044 int stat = 0; 00045 int i; 00046 output << "staticSave for distribution " << D::distributionName() << "\n"; 00047 double r = 0; 00048 double v1, v2, k1, k2; 00049 for (i=0; i < n; i++) r += D::shoot(); 00050 { 00051 std::ofstream file ("distribution.save1"); 00052 D::saveFullState(file); 00053 v1 = D::shoot(); 00054 D::saveFullState(file); 00055 v2 = D::shoot(); 00056 #ifdef VERBOSER2 00057 int pr = output.precision(20); 00058 output << "v1 = " << v1 << " v2 = " << v2 << "\n"; 00059 output.precision(pr); 00060 #endif 00061 } 00062 for (i=0; i < n; i++) r += D::shoot(); 00063 { 00064 std::ifstream file ("distribution.save1"); 00065 D::restoreFullState(file); 00066 k1 = D::shoot(); 00067 for (i=0; i < n; i++) r += D::shoot(); 00068 D::restoreFullState(file); 00069 k2 = D::shoot(); 00070 #ifdef VERBOSER2 00071 int pr = output.precision(20); 00072 output << "k1 = " << k1 << " k2 = " << k2 << "\n"; 00073 output.precision(pr); 00074 #endif 00075 } 00076 if ( (k1 != v1) || (k2 != v2) ) { 00077 std::cout << "???? restoreFullState failed for " << D::distributionName() << "\n"; 00078 #ifdef CLEAN_OUTPUT 00079 output << "???? restoreFullState failed for " << D::distributionName() << "\n"; 00080 #endif 00081 stat |= 8192; 00082 } 00083 00084 for (i=0; i < n; i++) r += D::shoot(); 00085 { 00086 std::ofstream file ("distribution.save2"); 00087 D::saveDistState(file) << *D::getTheEngine(); 00088 v1 = D::shoot(); 00089 D::saveDistState(file) << *D::getTheEngine(); 00090 v2 = D::shoot(); 00091 #ifdef VERBOSER2 00092 int pr = output.precision(20); 00093 output << "v1 = " << v1 << " v2 = " << v2 << "\n"; 00094 output.precision(pr); 00095 #endif 00096 } 00097 for (i=0; i < n; i++) r += D::shoot(); 00098 { 00099 std::ifstream file ("distribution.save2"); 00100 D::restoreDistState(file) >> *D::getTheEngine(); 00101 k1 = D::shoot(); 00102 for (i=0; i < n; i++) r += D::shoot(); 00103 D::restoreDistState(file) >> *D::getTheEngine(); 00104 k2 = D::shoot(); 00105 #ifdef VERBOSER2 00106 int pr = output.precision(20); 00107 output << "k1 = " << k1 << " k2 = " << k2 << "\n"; 00108 output.precision(pr); 00109 #endif 00110 } 00111 if ( (k1 != v1) || (k2 != v2) ) { 00112 std::cout << "???? restoreDistState failed for " << D::distributionName() << "\n"; 00113 #ifdef CLEAN_OUTPUT 00114 output << "???? restoreDistState failed for " << D::distributionName() << "\n"; 00115 #endif 00116 stat |= 16384; 00117 } 00118 00119 return stat; 00120 } 00121 00122 template <class D> 00123 int staticSaveShootBit(int n) { 00124 int stat = 0; 00125 int i; 00126 output << "staticSaveShootBit for " << D::distributionName() << "\n"; 00127 double r = 0; 00128 int bit = 0; 00129 int v1, v2, k1, k2; 00130 for (i=0; i < n; i++) r += D::shoot(); 00131 for (i=0; i < n; i++) bit |= D::shootBit(); 00132 { 00133 std::ofstream file ("distribution.save1"); 00134 D::saveFullState(file); 00135 v1=0; 00136 for (i=0; i<25; i++) { 00137 v1 <<=1; 00138 v1 += D::shootBit(); 00139 } 00140 for (i=0; i < n; i++) bit |= D::shootBit(); 00141 D::saveFullState(file); 00142 v2=0; 00143 for (i=0; i<25; i++) { 00144 v2 <<=1; 00145 v2 += D::shootBit(); 00146 } 00147 #ifdef VERBOSER2 00148 int pr = output.precision(20); 00149 output << std::hex << "v1 = " << v1 << " v2 = " << v2 << std::dec << "\n"; 00150 output.precision(pr); 00151 #endif 00152 } 00153 for (i=0; i < n; i++) r += D::shoot(); 00154 { 00155 std::ifstream file ("distribution.save1"); 00156 D::restoreFullState(file); 00157 k1=0; 00158 for (i=0; i<25; i++) { 00159 k1 <<=1; 00160 k1 += D::shootBit(); 00161 } 00162 for (i=0; i < n; i++) r += D::shoot(); 00163 D::restoreFullState(file); 00164 k2=0; 00165 for (i=0; i<25; i++) { 00166 k2 <<=1; 00167 k2 += D::shootBit(); 00168 } 00169 #ifdef VERBOSER2 00170 int pr = output.precision(20); 00171 output << std::hex << "k1 = " << k1 << " k2 = " << k2 << std::dec << "\n"; 00172 output.precision(pr); 00173 #endif 00174 } 00175 if ( (k1 != v1) || (k2 != v2) ) { 00176 std::cout << "???? restoreFullState failed for D shootBit()\n"; 00177 #ifdef CLEAN_OUTPUT 00178 output << "???? restoreFullState failed for D shootBit()\n"; 00179 #endif 00180 stat |= 32768; 00181 } 00182 00183 for (i=0; i < n; i++) r += D::shoot(); 00184 for (i=0; i < n; i++) bit |= D::shootBit(); 00185 { 00186 std::ofstream file ("distribution.save2"); 00187 D::saveDistState(file) << *D::getTheEngine(); 00188 v1=0; 00189 for (i=0; i<25; i++) { 00190 v1 <<=1; 00191 v1 += D::shootBit(); 00192 } 00193 for (i=0; i < n; i++) bit |= D::shootBit(); 00194 D::saveDistState(file) << *D::getTheEngine(); 00195 v2=0; 00196 for (i=0; i<25; i++) { 00197 v2 <<=1; 00198 v2 += D::shootBit(); 00199 } 00200 #ifdef VERBOSER2 00201 int pr = output.precision(20); 00202 output << std::hex << "v1 = " << v1 << " v2 = " << v2 << std::dec << "\n"; 00203 output.precision(pr); 00204 #endif 00205 } 00206 for (i=0; i < n; i++) r += D::shoot(); 00207 { 00208 std::ifstream file ("distribution.save2"); 00209 D::restoreDistState(file) >> *D::getTheEngine(); 00210 k1=0; 00211 for (i=0; i<25; i++) { 00212 k1 <<=1; 00213 k1 += D::shootBit(); 00214 } 00215 for (i=0; i < n; i++) r += D::shoot(); 00216 for (i=0; i < n; i++) r += D::shootBit(); 00217 D::restoreDistState(file) >> *D::getTheEngine(); 00218 k2=0; 00219 for (i=0; i<25; i++) { 00220 k2 <<=1; 00221 k2 += D::shootBit(); 00222 } 00223 #ifdef VERBOSER2 00224 int pr = output.precision(20); 00225 output << std::hex << "k1 = " << k1 << " k2 = " << k2 << std::dec << "\n"; 00226 output.precision(pr); 00227 #endif 00228 } 00229 if ( (k1 != v1) || (k2 != v2) ) { 00230 std::cout << "???? restoreDistState failed for RnadFlat::shootBit()\n"; 00231 #ifdef CLEAN_OUTPUT 00232 output << "???? restoreDistState failed for RnadFlat::shootBit()\n"; 00233 #endif 00234 stat |= 65536; 00235 } 00236 00237 return stat; 00238 } 00239 00240 // ----------- Tests saving all statics together ----------- 00241 00242 void randomizeStatics(int n) { 00243 for (int i=0; i<n; i++) { 00244 RandGauss::shoot(); 00245 RandGaussQ::shoot(); 00246 RandGaussT::shoot(); 00247 RandFlat::shoot(); 00248 RandBit::shoot(); 00249 RandFlat::shootBit(); 00250 RandBit::shootBit(); 00251 RandPoisson::shoot(); 00252 RandPoissonQ::shoot(); 00253 RandPoissonT::shoot(); 00254 RandBinomial::shoot(); 00255 RandBreitWigner::shoot(); 00256 RandChiSquare::shoot(); 00257 RandExponential::shoot(); 00258 RandGamma::shoot(); 00259 RandLandau::shoot(); 00260 RandSkewNormal::shoot(); 00261 RandStudentT::shoot(); 00262 } 00263 } 00264 00265 std::vector<double> captureStatics() { 00266 std::vector<double> c; 00267 c.push_back( RandGauss::shoot() ); 00268 c.push_back( RandGaussQ::shoot() ); 00269 c.push_back( RandGaussT::shoot() ); 00270 c.push_back( RandFlat::shoot() ); 00271 c.push_back( RandBit::shoot() ); 00272 for (int i=0; i<20; i++) { 00273 c.push_back( RandFlat::shootBit() ); 00274 c.push_back( RandBit::shootBit() ); 00275 } 00276 c.push_back( RandPoisson::shoot() ); 00277 c.push_back( RandPoissonQ::shoot() ); 00278 c.push_back( RandPoissonT::shoot() ); 00279 c.push_back( RandBinomial::shoot() ); 00280 c.push_back( RandBreitWigner::shoot() ); 00281 c.push_back( RandChiSquare::shoot() ); 00282 c.push_back( RandExponential::shoot() ); 00283 c.push_back( RandGamma::shoot() ); 00284 c.push_back( RandLandau::shoot() ); 00285 c.push_back( RandSkewNormal::shoot() ); 00286 c.push_back( RandStudentT::shoot() ); 00287 return c; 00288 } 00289 00290 void saveStatics(std::string filename) { 00291 std::ofstream os(filename.c_str()); 00292 RandGeneral::saveStaticRandomStates(os); 00293 // It should be possible to call this from HepRandom, or any distribution. 00294 // RandGeneral, which is meaningless as a static distribution, should be the 00295 // toughest test, so we use that here. 00296 } 00297 00298 void restoreStatics(std::string filename) { 00299 std::ifstream is(filename.c_str()); 00300 RandLandau::restoreStaticRandomStates(is); 00301 } 00302 00303 // --------------------------------------------- 00304 // --------------------------------------------- 00305 // --------------------------------------------- 00306 00307 00308 int main() { 00309 int stat = 0; 00310 00311 #ifdef TEST_STATIC_SAVE 00312 output << "\n=========================================\n"; 00313 output << " Part V \n"; 00314 output << "Static Save/restore to/from streams \n"; 00315 output << "=========================================\n\n"; 00316 00317 stat |= staticSave <RandGauss>(7); 00318 stat |= staticSave <RandFlat>(7); 00319 stat |= staticSaveShootBit<RandFlat> (19); 00320 stat |= staticSaveShootBit<RandBit> (23); 00321 for (int ibinom=0; ibinom<15; ibinom++) { 00322 stat |= staticSave <RandBinomial>(7+3*ibinom); 00323 } 00324 stat |= staticSave <RandBreitWigner>(7); 00325 stat |= staticSave <RandChiSquare>(7); 00326 stat |= staticSave <RandExponential>(7); 00327 stat |= staticSave <RandGamma>(7); 00328 stat |= staticSave <RandGaussQ>(7); 00329 stat |= staticSave <RandGaussT>(7); 00330 stat |= staticSave <RandLandau>(7); 00331 stat |= staticSave <RandPoisson>(7); 00332 stat |= staticSave <RandPoissonQ>(7); 00333 stat |= staticSave <RandPoissonT>(7); 00334 stat |= staticSave <RandSkewNormal>(7); 00335 stat |= staticSave <RandStudentT>(7); 00336 #endif 00337 00338 #ifdef TEST_SAVE_STATIC_STATES 00339 output << "\n==============================================\n"; 00340 output << " Part VI \n"; 00341 output << "Save/restore all static states to/from streams \n"; 00342 output << "==============================================\n\n"; 00343 00344 randomizeStatics(15); 00345 saveStatics("distribution.save"); 00346 output << "Saved all static distributions\n"; 00347 std::vector<double> c = captureStatics(); 00348 output << "Captured output of all static distributions\n"; 00349 randomizeStatics(11); 00350 output << "Randomized all static distributions\n"; 00351 restoreStatics("distribution.save"); 00352 output << "Restored all static distributions to saved state\n"; 00353 std::vector<double> d = captureStatics(); 00354 output << "Captured output of all static distributions\n"; 00355 for (unsigned int iv=0; iv<c.size(); iv++) { 00356 if (c[iv] != d[iv]) { 00357 std::cout << "???? restoreStaticRandomStates failed at random " 00358 << iv <<"\n"; 00359 #ifdef CLEAN_OUTPUT 00360 output << "???? restoreStaticRandomStates failed at random " 00361 << iv <<"\n"; 00362 #endif 00363 stat |= 131072; 00364 } 00365 } 00366 if ((stat & 131072) == 0) { 00367 output << "All captured output agrees with earlier values\n"; 00368 } 00369 #endif 00370 00371 output << "\n=============================================\n\n"; 00372 00373 if (stat != 0) { 00374 std::cout << "One or more problems detected: stat = " << stat << "\n"; 00375 output << "One or more problems detected: stat = " << stat << "\n"; 00376 } else { 00377 output << "testStaticStreamSave passed with no problems detected.\n"; 00378 } 00379 00380 if (stat == 0) return 0; 00381 if (stat > 0) return -(stat|1); 00382 return stat|1; 00383 } 00384