CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testStaticStreamSave.cc

Go to the documentation of this file.
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     RandStudentT::shoot();
00261   }
00262 }
00263 
00264 std::vector<double> captureStatics() {
00265   std::vector<double> c;
00266   c.push_back( RandGauss::shoot() );
00267   c.push_back( RandGaussQ::shoot() );
00268   c.push_back( RandGaussT::shoot() );
00269   c.push_back( RandFlat::shoot() );
00270   c.push_back( RandBit::shoot() );
00271   for (int i=0; i<20; i++)  {
00272     c.push_back( RandFlat::shootBit() );
00273     c.push_back( RandBit::shootBit() );
00274   }
00275   c.push_back( RandPoisson::shoot() );      
00276   c.push_back( RandPoissonQ::shoot() );     
00277   c.push_back( RandPoissonT::shoot() );     
00278   c.push_back( RandBinomial::shoot() );     
00279   c.push_back( RandBreitWigner::shoot() );  
00280   c.push_back( RandChiSquare::shoot() );    
00281   c.push_back( RandExponential::shoot() );  
00282   c.push_back( RandGamma::shoot() );         
00283   c.push_back( RandLandau::shoot() );       
00284   c.push_back( RandStudentT::shoot() );
00285   return c;     
00286 }
00287 
00288 void saveStatics(std::string filename) {
00289   std::ofstream os(filename.c_str());
00290   RandGeneral::saveStaticRandomStates(os);
00291   // It should be possible to call this from HepRandom, or any distribution.
00292   // RandGeneral, which is meaningless as a static distribution, should be the
00293   // toughest test, so we use that here.
00294 }
00295 
00296 void restoreStatics(std::string filename) {
00297   std::ifstream is(filename.c_str());
00298   RandLandau::restoreStaticRandomStates(is);
00299 }
00300 
00301 // ---------------------------------------------
00302 // ---------------------------------------------
00303 // ---------------------------------------------
00304 
00305 
00306 int main() {
00307   int stat = 0;
00308 
00309 #ifdef TEST_STATIC_SAVE
00310   output << "\n=========================================\n";
00311   output << "              Part V \n";
00312   output << "Static Save/restore to/from streams \n";
00313   output << "=========================================\n\n";
00314  
00315   stat |= staticSave <RandGauss>(7);
00316   stat |= staticSave <RandFlat>(7);
00317   stat |= staticSaveShootBit<RandFlat> (19);
00318   stat |= staticSaveShootBit<RandBit> (23);
00319   for (int ibinom=0; ibinom<15; ibinom++) {
00320     stat |= staticSave <RandBinomial>(7+3*ibinom);
00321   }
00322   stat |= staticSave <RandBreitWigner>(7);
00323   stat |= staticSave <RandChiSquare>(7);
00324   stat |= staticSave <RandExponential>(7);
00325   stat |= staticSave <RandGamma>(7);
00326   stat |= staticSave <RandGaussQ>(7);
00327   stat |= staticSave <RandGaussT>(7);
00328   stat |= staticSave <RandLandau>(7);
00329   stat |= staticSave <RandPoisson>(7);
00330   stat |= staticSave <RandPoissonQ>(7);
00331   stat |= staticSave <RandPoissonT>(7);
00332   stat |= staticSave <RandStudentT>(7);
00333 #endif
00334 
00335 #ifdef TEST_SAVE_STATIC_STATES
00336   output << "\n==============================================\n";
00337   output << "                 Part VI  \n";
00338   output << "Save/restore all static states to/from streams \n";
00339   output << "==============================================\n\n";
00340  
00341   randomizeStatics(15);
00342   saveStatics("distribution.save");
00343   output << "Saved all static distributions\n";
00344   std::vector<double> c = captureStatics();
00345   output << "Captured output of all static distributions\n";
00346   randomizeStatics(11);
00347   output << "Randomized all static distributions\n";
00348   restoreStatics("distribution.save");
00349   output << "Restored all static distributions to saved state\n";
00350   std::vector<double> d = captureStatics();
00351   output << "Captured output of all static distributions\n";
00352   for (unsigned int iv=0; iv<c.size(); iv++) {
00353     if (c[iv] != d[iv]) {
00354       std::cout << "???? restoreStaticRandomStates failed at random " 
00355                 << iv <<"\n";
00356       #ifdef CLEAN_OUTPUT
00357       output << "???? restoreStaticRandomStates failed at random " 
00358                 << iv <<"\n";
00359       #endif
00360       stat |= 131072;
00361     }
00362   }
00363   if ((stat & 131072) == 0) {
00364     output << "All captured output agrees with earlier values\n";
00365   }
00366 #endif
00367   
00368   output << "\n=============================================\n\n";
00369 
00370   if (stat != 0) {
00371      std::cout << "One or more problems detected: stat = " << stat << "\n";
00372      output << "One or more problems detected: stat = " << stat << "\n";
00373   }  else {
00374      output << "testStaticStreamSave passed with no problems detected.\n";    
00375   }
00376 
00377   if (stat == 0) return 0;
00378   if (stat > 0) return -(stat|1);
00379   return stat|1;
00380 }       
00381 

Generated on Thu Jul 1 22:02:31 2010 for CLHEP by  doxygen 1.4.7