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("testSaveSharedEngines.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_SHARED_ENGINES 00022 00023 // Normally off for routine validation: 00024 00025 #define VERBOSER 00026 #define VERBOSER2 00027 00028 using namespace CLHEP; 00029 00030 00031 // Absolutely Safe Equals Without Registers Screwing Us Up 00032 bool equals01(const std::vector<double> &ab) { 00033 return ab[1]==ab[0]; 00034 } 00035 bool equals(double a, double b) { 00036 std::vector<double> ab(2); 00037 ab[0]=a; ab[1]=b; 00038 return (equals01(ab)); 00039 } 00040 00041 template <class E, class D1, class D2> 00042 int checkSharingDistributions(D1 & d1, D2 & d2, int n1, int n2) { 00043 int stat = 0; 00044 output << "checkSharingDistribution with: \n" 00045 << d1.name() << " using " << d1.engine().name() << " and\n" 00046 << d2.name() << " using " << d2.engine().name() << "\n"; 00047 double r=0; 00048 r = d1(); 00049 r += d2(); 00050 double kv11,kv12,kv13,kv14; 00051 double kv21,kv22,kv23,kv24; 00052 for (int i=0; i<n1; i++) r += d1(); 00053 for (int j=0; j<n2; j++) r += d2(); 00054 {std::ofstream os ("shared.save1"); os << d1.engine() << d1 << d2;} 00055 kv11 = d1(); 00056 kv21 = d2(); 00057 kv12 = d1(); 00058 kv22 = d2(); 00059 r += d1() + d2(); 00060 // A second capture will test non-cached if first tested cached case: 00061 {std::ofstream os ("shared.save2"); os << d1.engine() << d1 << d2;} 00062 kv13 = d1(); 00063 kv23 = d2(); 00064 kv14 = d1(); 00065 kv24 = d2(); 00066 #ifdef VERBOSER2 00067 int pr = output.precision(20); 00068 output << "kv11 = " << kv11 << 00069 " kv21 = " << kv21 << "\n"; 00070 output << "kv12 = " << kv12 << 00071 " kv22 = " << kv22 << "\n"; 00072 output << "kv13 = " << kv13 << 00073 " kv23 = " << kv23 << "\n"; 00074 output << "kv14 = " << kv14 << 00075 " kv24 = " << kv24 << "\n"; 00076 output.precision(pr); 00077 #endif 00078 E e; 00079 D1 d1r(e); 00080 D2 d2r(e); 00081 { std::ifstream is ("shared.save1"); is >> e >> d1r >> d2r;} 00082 double k11 = d1r(); 00083 double k21 = d2r(); 00084 double k12 = d1r(); 00085 double k22 = d2r(); 00086 { std::ifstream is ("shared.save2"); is >> e >> d1r >> d2r;} 00087 double k13 = d1r(); 00088 double k23 = d2r(); 00089 double k14 = d1r(); 00090 double k24 = d2r(); 00091 #ifdef VERBOSER2 00092 pr = output.precision(20); 00093 output << "k11 = " << k11 << 00094 " k21 = " << k21 << "\n"; 00095 output << "k12 = " << k12 << 00096 " k22 = " << k22 << "\n"; 00097 output << "k13 = " << k13 << 00098 " k23 = " << k23 << "\n"; 00099 output << "k14 = " << k14 << 00100 " k24 = " << k24 << "\n"; 00101 output.precision(pr); 00102 #endif 00103 if ( !equals(k11,kv11) || !equals(k21,kv21) || 00104 !equals(k12,kv12) || !equals(k22,kv22) || 00105 !equals(k13,kv13) || !equals(k23,kv23) || 00106 !equals(k14,kv14) || !equals(k24,kv24) ) { 00107 std::cout << "???? Incorrect restored value for distributions " 00108 << d1.name() << " " << d2.name() << "\n"; 00109 #ifdef CLEAN_OUTPUT 00110 output << "???? Incorrect restored value for distributions " 00111 << d1.name() << " " << d2.name() << "\n"; 00112 #endif 00113 stat |= 4096; 00114 } 00115 // if (stat) exit(-1); 00116 return stat; 00117 } 00118 00119 00120 00121 template <class E> 00122 int checkSharing() { 00123 int stat = 0; 00124 E e1(98746); 00125 RandGauss g1(e1,50.0,4.0); 00126 RandPoissonQ p1(e1, 112.0); 00127 RandGauss g2(e1,5.0,44.0); 00128 RandPoissonQ p2(e1, 212.0); 00129 stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p1,5,4); 00130 stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p2,6,6); 00131 stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g2,p1,8,9); 00132 stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p1,7,5); 00133 stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p1,g2,5,4); 00134 stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p2,g1,6,6); 00135 stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p1,g1,8,9); 00136 stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p2,g1,7,5); 00137 return stat; 00138 } 00139 00140 00141 // --------------------------------------------- 00142 // --------------------------------------------- 00143 // --------------------------------------------- 00144 00145 00146 int main() { 00147 int stat = 0; 00148 00149 #ifdef TEST_SHARED_ENGINES 00150 output << "\n=============================================\n"; 00151 output << " Part IV \n"; 00152 output << "Check behavior when engines are shared \n"; 00153 output << "=============================================\n\n"; 00154 00155 stat |= checkSharing<DualRand>(); 00156 stat |= checkSharing<Hurd160Engine>(); 00157 stat |= checkSharing<Hurd288Engine>(); 00158 stat |= checkSharing<HepJamesRandom>(); 00159 stat |= checkSharing<MTwistEngine>(); 00160 stat |= checkSharing<Ranlux64Engine>(); 00161 stat |= checkSharing<RanluxEngine>(); 00162 stat |= checkSharing<RanshiEngine>(); 00163 stat |= checkSharing<TripleRand>(); 00164 #endif 00165 00166 output << "\n=============================================\n\n"; 00167 00168 if (stat != 0) { 00169 std::cout << "One or more problems detected: stat = " << stat << "\n"; 00170 output << "One or more problems detected: stat = " << stat << "\n"; 00171 } else { 00172 output << "testSaveSharedEngines passed with no problems detected.\n"; 00173 } 00174 00175 if (stat == 0) return 0; 00176 if (stat > 0) return -(stat|1); 00177 return stat|1; 00178 return stat > 0 ? -stat : stat; 00179 } 00180