CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // -*- C++ -*- 00002 // $Id: testDistCopy.cc,v 1.3 2011/05/31 20:57:01 garren Exp $ 00003 // ---------------------------------------------------------------------- 00004 00005 // ====================================================================== 00006 // 00007 // 00008 // testDistCopy -- test copied random distributions 00009 // 00010 // ====================================================================== 00011 00012 #include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings 00013 // ---------------------------------------------------------------------- 00014 // Engines: 00015 #include "CLHEP/Random/DualRand.h" // CLHEP::DualRand 00016 #include "CLHEP/Random/MTwistEngine.h" // CLHEP::MTwistEngine 00017 00018 // ---------------------------------------------------------------------- 00019 // Distributions: 00020 #include "CLHEP/Random/RandBinomial.h" // CLHEP::RandBinomial 00021 #include "CLHEP/Random/RandBreitWigner.h" // CLHEP::RandBreitWigner 00022 #include "CLHEP/Random/RandChiSquare.h" // CLHEP::RandChiSquare 00023 #include "CLHEP/Random/RandExponential.h" // CLHEP::RandExponential 00024 #include "CLHEP/Random/RandFlat.h" // CLHEP::RandFlat 00025 #include "CLHEP/Random/RandGamma.h" // CLHEP::RandGamma 00026 #include "CLHEP/Random/RandGauss.h" // CLHEP::RandGauss 00027 #include "CLHEP/Random/RandGaussQ.h" // CLHEP::RandGaussQ 00028 #include "CLHEP/Random/RandGaussT.h" // CLHEP::RandGaussT 00029 #include "CLHEP/Random/RandGeneral.h" // CLHEP::RandGeneral 00030 #include "CLHEP/Random/RandLandau.h" // CLHEP::RandLandau 00031 #include "CLHEP/Random/RandPoissonQ.h" // CLHEP::RandPoissonQ 00032 #include "CLHEP/Random/RandPoissonT.h" // CLHEP::RandPoissonT 00033 #include "CLHEP/Random/RandSkewNormal.h" // CLHEP::RandSkewNormal 00034 #include "CLHEP/Random/RandStudentT.h" // CLHEP::RandStudentT 00035 00036 // ---------------------------------------------------------------------- 00037 // Standard library: 00038 #include <sstream> // for ostringstream 00039 #include <string> // for string 00040 00041 00042 using namespace CLHEP; 00043 typedef unsigned int uint; 00044 00045 00046 // ---------------------------------------------------------------------- 00047 // copy-construction test 00048 00049 template< typename Dist > 00050 bool 00051 copy_constructor_is_okay( Dist & d1 ) 00052 { 00053 // prime the distribution 00054 for( uint i = 0u; i != 17u; ++i ) 00055 (void) d1.fire(); 00056 00057 // capture its state 00058 std::ostringstream os1; 00059 d1.put( os1 ); 00060 HepRandomEngine * e1 = & d1.engine(); 00061 00062 // make a copy and capture the copy's state 00063 Dist d2( d1 ); 00064 std::ostringstream os2; 00065 d2.put( os2 ); 00066 HepRandomEngine * e2 = & d2.engine(); 00067 00068 // do the saved states match and is the underlying engine shared? 00069 return os1.str() == os2.str() && e1 == e2; 00070 } // copy_constructor_is_okay<>() 00071 00072 00073 // ---------------------------------------------------------------------- 00074 // copy-construction test 00075 00076 template< typename Dist > 00077 bool 00078 copy_assignment_is_okay( Dist & d1, Dist & d2 ) 00079 { 00080 // prime the distributions 00081 for( uint i = 0u; i != 17u; ++i ) 00082 (void) d1.fire(); 00083 for( uint i = 0u; i != 19u; ++i ) 00084 (void) d2.fire(); 00085 00086 // capture d1's state 00087 std::ostringstream os1; 00088 d1.put( os1 ); 00089 HepRandomEngine * e1 = & d1.engine(); 00090 00091 // make a copy and capture the copy's state 00092 d2 = d1; 00093 std::ostringstream os2; 00094 d2.put( os2 ); 00095 HepRandomEngine * e2 = & d2.engine(); 00096 00097 // do the saved states match and is the underlying engine shared? 00098 return os1.str() == os2.str() && e1 == e2; 00099 } // copy_assignment_is_okay<>() 00100 00101 00102 // ---------------------------------------------------------------------- 00103 // Mask bits to form a word identifying dists that failed their test 00104 00105 static uint const success = 0u; 00106 static uint const Binomial_failure = 1u << 1; 00107 static uint const BreitWigner_failure = 1u << 2; 00108 static uint const ChiSquare_failure = 1u << 3; 00109 static uint const Exponential_failure = 1u << 4; 00110 static uint const Flat_failure = 1u << 5; 00111 static uint const Gamma_failure = 1u << 6; 00112 static uint const Gauss_failure = 1u << 7; 00113 static uint const GaussQ_failure = 1u << 8; 00114 static uint const GaussT_failure = 1u << 9; 00115 static uint const General_failure = 1u << 10; 00116 static uint const Landau_failure = 1u << 11; 00117 static uint const Poisson_failure = 1u << 12; 00118 static uint const PoissonQ_failure = 1u << 13; 00119 static uint const PoissonT_failure = 1u << 14; 00120 static uint const StudentT_failure = 1u << 15; 00121 static uint const SkewNormal_failure = 1u << 16; 00122 00123 00124 // ---------------------------------------------------------------------- 00125 // RandBinomial 00126 00127 uint testRandBinomial() 00128 { 00129 MTwistEngine r1( 97531L ); 00130 RandBinomial d1( r1 ); 00131 if( ! copy_constructor_is_okay(d1) ) return Binomial_failure; 00132 00133 DualRand r2( 13579L ); 00134 RandBinomial d2( r2 ); 00135 if( ! copy_assignment_is_okay(d1,d2) ) return Binomial_failure; 00136 00137 return 0u; 00138 } 00139 00140 00141 // ---------------------------------------------------------------------- 00142 // RandBreitWigner 00143 00144 uint testRandBreitWigner() 00145 { 00146 MTwistEngine r1( 97531L ); 00147 RandBreitWigner d1( r1 ); 00148 if( ! copy_constructor_is_okay(d1) ) return BreitWigner_failure; 00149 00150 DualRand r2( 13579L ); 00151 RandBreitWigner d2( r2 ); 00152 if( ! copy_assignment_is_okay(d1,d2) ) return BreitWigner_failure; 00153 00154 return 0u; 00155 } // testRandBreitWigner 00156 00157 00158 // ---------------------------------------------------------------------- 00159 // RandChiSquare 00160 00161 uint testRandChiSquare() 00162 { 00163 MTwistEngine r1( 97531L ); 00164 RandChiSquare d1( r1 ); 00165 if( ! copy_constructor_is_okay(d1) ) return ChiSquare_failure; 00166 00167 DualRand r2( 13579L ); 00168 RandChiSquare d2( r2 ); 00169 if( ! copy_assignment_is_okay(d1,d2) ) return ChiSquare_failure; 00170 00171 return 0u; 00172 } // testRandChiSquare 00173 00174 00175 // ---------------------------------------------------------------------- 00176 // RandExponential 00177 00178 uint testRandExponential() 00179 { 00180 MTwistEngine r1( 97531L ); 00181 RandExponential d1( r1 ); 00182 if( ! copy_constructor_is_okay(d1) ) return Exponential_failure; 00183 00184 DualRand r2( 13579L ); 00185 RandExponential d2( r2 ); 00186 if( ! copy_assignment_is_okay(d1,d2) ) return Exponential_failure; 00187 00188 return 0u; 00189 } // testRandExponential 00190 00191 00192 // ---------------------------------------------------------------------- 00193 // RandFlat 00194 00195 uint testRandFlat() 00196 { 00197 MTwistEngine r1( 97531L ); 00198 RandFlat d1( r1 ); 00199 if( ! copy_constructor_is_okay(d1) ) return Flat_failure; 00200 00201 DualRand r2( 13579L ); 00202 RandFlat d2( r2 ); 00203 if( ! copy_assignment_is_okay(d1,d2) ) return Flat_failure; 00204 00205 return 0u; 00206 } // testRandFlat 00207 00208 00209 // ---------------------------------------------------------------------- 00210 // RandGamma 00211 00212 uint testRandGamma() 00213 { 00214 MTwistEngine r1( 97531L ); 00215 RandGamma d1( r1 ); 00216 if( ! copy_constructor_is_okay(d1) ) return Gamma_failure; 00217 00218 DualRand r2( 13579L ); 00219 RandGamma d2( r2 ); 00220 if( ! copy_assignment_is_okay(d1,d2) ) return Gamma_failure; 00221 00222 return 0u; 00223 } // testRandGamma 00224 00225 00226 // ---------------------------------------------------------------------- 00227 // RandGauss 00228 00229 uint testRandGauss() 00230 { 00231 MTwistEngine r1( 97531L ); 00232 RandGauss d1( r1 ); 00233 if( ! copy_constructor_is_okay(d1) ) return Gauss_failure; 00234 00235 DualRand r2( 13579L ); 00236 RandGauss d2( r2 ); 00237 if( ! copy_assignment_is_okay(d1,d2) ) return Gauss_failure; 00238 00239 return 0u; 00240 } // testRandGauss 00241 00242 00243 // ---------------------------------------------------------------------- 00244 // RandGaussQ 00245 00246 uint testRandGaussQ() 00247 { 00248 MTwistEngine r1( 97531L ); 00249 RandGaussQ d1( r1 ); 00250 if( ! copy_constructor_is_okay(d1) ) return GaussQ_failure; 00251 00252 DualRand r2( 13579L ); 00253 RandGaussQ d2( r2 ); 00254 if( ! copy_assignment_is_okay(d1,d2) ) return GaussQ_failure; 00255 00256 return 0u; 00257 } // testRandGaussQ 00258 00259 00260 // ---------------------------------------------------------------------- 00261 // RandGaussT 00262 00263 uint testRandGaussT() 00264 { 00265 MTwistEngine r1( 97531L ); 00266 RandGaussT d1( r1 ); 00267 if( ! copy_constructor_is_okay(d1) ) return GaussT_failure; 00268 00269 DualRand r2( 13579L ); 00270 RandGaussT d2( r2 ); 00271 if( ! copy_assignment_is_okay(d1,d2) ) return GaussT_failure; 00272 00273 return 0u; 00274 } // testRandGaussT 00275 00276 00277 // ---------------------------------------------------------------------- 00278 // RandGeneral 00279 00280 uint testRandGeneral() 00281 { 00282 MTwistEngine r1( 97531L ); 00283 double pdf1[] = { 1.5, 2.5, 3.0, 4.25, 5.65 }; 00284 RandGeneral d1( r1, pdf1, sizeof(pdf1)/sizeof(pdf1[0]) ); 00285 if( ! copy_constructor_is_okay(d1) ) return General_failure; 00286 00287 DualRand r2( 13579L ); 00288 double pdf2[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 0.60 }; 00289 RandGeneral d2( r2, pdf2, sizeof(pdf2)/sizeof(pdf2[0]) ); 00290 if( ! copy_assignment_is_okay(d1,d2) ) return General_failure; 00291 00292 return 0u; 00293 } // testRandGeneral 00294 00295 00296 // ---------------------------------------------------------------------- 00297 // RandLandau 00298 00299 uint testRandLandau() 00300 { 00301 MTwistEngine r1( 97531L ); 00302 RandLandau d1( r1 ); 00303 if( ! copy_constructor_is_okay(d1) ) return Landau_failure; 00304 00305 DualRand r2( 13579L ); 00306 RandLandau d2( r2 ); 00307 if( ! copy_assignment_is_okay(d1,d2) ) return Landau_failure; 00308 00309 return 0u; 00310 } // testRandLandau 00311 00312 00313 // ---------------------------------------------------------------------- 00314 // RandPoisson 00315 00316 uint testRandPoisson() 00317 { 00318 MTwistEngine r1( 97531L ); 00319 RandPoisson d1( r1 ); 00320 if( ! copy_constructor_is_okay(d1) ) return Poisson_failure; 00321 00322 DualRand r2( 13579L ); 00323 RandPoisson d2( r2 ); 00324 if( ! copy_assignment_is_okay(d1,d2) ) return Poisson_failure; 00325 00326 return 0u; 00327 } // testRandPoisson 00328 00329 00330 // ---------------------------------------------------------------------- 00331 // RandPoissonQ 00332 00333 uint testRandPoissonQ() 00334 { 00335 MTwistEngine r1( 97531L ); 00336 RandPoissonQ d1( r1 ); 00337 if( ! copy_constructor_is_okay(d1) ) return PoissonQ_failure; 00338 00339 DualRand r2( 13579L ); 00340 RandPoissonQ d2( r2 ); 00341 if( ! copy_assignment_is_okay(d1,d2) ) return PoissonQ_failure; 00342 00343 return 0u; 00344 } // testRandPoissonQ 00345 00346 00347 // ---------------------------------------------------------------------- 00348 // RandPoissonT 00349 00350 uint testRandPoissonT() 00351 { 00352 MTwistEngine r1( 97531L ); 00353 RandPoissonT d1( r1 ); 00354 if( ! copy_constructor_is_okay(d1) ) return PoissonT_failure; 00355 00356 DualRand r2( 13579L ); 00357 RandPoissonT d2( r2 ); 00358 if( ! copy_assignment_is_okay(d1,d2) ) return PoissonT_failure; 00359 00360 return 0u; 00361 } // testRandPoissonT 00362 00363 00364 // ---------------------------------------------------------------------- 00365 // RandSkewNormal 00366 00367 uint testRandSkewNormal() 00368 { 00369 MTwistEngine r1( 97531L ); 00370 RandSkewNormal d1( r1 ); 00371 if( ! copy_constructor_is_okay(d1) ) return SkewNormal_failure; 00372 00373 DualRand r2( 13579L ); 00374 RandSkewNormal d2( r2 ); 00375 if( ! copy_assignment_is_okay(d1,d2) ) return SkewNormal_failure; 00376 00377 return 0u; 00378 } // testRandSkewNormal 00379 00380 00381 // ---------------------------------------------------------------------- 00382 // RandStudentT 00383 00384 uint testRandStudentT() 00385 { 00386 MTwistEngine r1( 97531L ); 00387 RandStudentT d1( r1 ); 00388 if( ! copy_constructor_is_okay(d1) ) return StudentT_failure; 00389 00390 DualRand r2( 13579L ); 00391 RandStudentT d2( r2 ); 00392 if( ! copy_assignment_is_okay(d1,d2) ) return StudentT_failure; 00393 00394 return 0u; 00395 } // testRandStudentT 00396 00397 00398 // ---------------------------------------------------------------------- 00399 // main 00400 00401 int main() 00402 { 00403 uint mask = 0u 00404 | testRandBinomial () 00405 | testRandBreitWigner() 00406 | testRandChiSquare () 00407 | testRandExponential() 00408 | testRandFlat () 00409 | testRandGamma () 00410 | testRandGauss () 00411 | testRandGaussQ () 00412 | testRandGaussT () 00413 | testRandGeneral () 00414 | testRandLandau () 00415 | testRandPoisson () 00416 | testRandPoissonQ () 00417 | testRandPoissonT () 00418 | testRandSkewNormal () 00419 | testRandStudentT () 00420 ; 00421 00422 return - int(mask); 00423 } 00424