Geant4 Cross Reference |
>> 1 // $Id:$ 1 // -*- C++ -*- 2 // -*- C++ -*- 2 // 3 // 3 // ------------------------------------------- 4 // ----------------------------------------------------------------------- 4 // HEP Random 5 // HEP Random 5 // --- StaticRandomStates 6 // --- StaticRandomStates --- 6 // class implementation f 7 // class implementation file 7 // ------------------------------------------- 8 // ----------------------------------------------------------------------- 8 // 9 // 9 // =========================================== 10 // ======================================================================= 10 // Mark Fischler - Created: Dec. 21, 2004 11 // Mark Fischler - Created: Dec. 21, 2004 11 // Mark Fischler - Modified restore() to util 12 // Mark Fischler - Modified restore() to utilize anonymous engine input 12 // to create anonymous restor 13 // to create anonymous restore of the static distributions 13 // 14 // 14 // =========================================== 15 // ======================================================================= 15 16 16 #include "CLHEP/Random/StaticRandomStates.h" 17 #include "CLHEP/Random/StaticRandomStates.h" 17 #include "CLHEP/Random/RandGauss.h" 18 #include "CLHEP/Random/RandGauss.h" 18 #include "CLHEP/Random/RandFlat.h" 19 #include "CLHEP/Random/RandFlat.h" 19 #include <iostream> << 20 #include <string> 20 #include <string> 21 #include <sstream> 21 #include <sstream> 22 22 23 //======================// 23 //======================// 24 // // 24 // // 25 // Maintenance warning: // 25 // Maintenance warning: // 26 // // 26 // // 27 //======================// 27 //======================// 28 // 28 // 29 // Currently, only two distributions (RandFlat 29 // Currently, only two distributions (RandFlat and RandGauss) have cached 30 // distribution state. All such distributions 30 // distribution state. All such distributions must be saved below, so if 31 // another such distribution is added, this im 31 // another such distribution is added, this implementation file must be 32 // modified to reflect that. 32 // modified to reflect that. 33 33 34 namespace CLHEP { 34 namespace CLHEP { 35 35 36 36 37 std::ostream & StaticRandomStates::save(std::o 37 std::ostream & StaticRandomStates::save(std::ostream & os){ 38 RandGauss::saveFullState(os); 38 RandGauss::saveFullState(os); 39 RandFlat::saveDistState(os); 39 RandFlat::saveDistState(os); 40 return os; 40 return os; 41 } 41 } 42 42 43 #ifdef NOTYET 43 #ifdef NOTYET 44 std::istream & StaticRandomStates::restore(std 44 std::istream & StaticRandomStates::restore(std::istream & is) { 45 RandGauss::restoreFullState(is); 45 RandGauss::restoreFullState(is); 46 RandFlat::restoreDistState(is); 46 RandFlat::restoreDistState(is); 47 return is; 47 return is; 48 } 48 } 49 #endif 49 #endif 50 50 51 std::istream & StaticRandomStates::restore(std 51 std::istream & StaticRandomStates::restore(std::istream & is) { 52 HepRandomEngine * e = HepRandom::getTheEngin 52 HepRandomEngine * e = HepRandom::getTheEngine(); 53 HepRandomEngine *ne = HepRandomEngine::newEn 53 HepRandomEngine *ne = HepRandomEngine::newEngine(is); 54 if ( !is ) return is; 54 if ( !is ) return is; 55 if ( !ne ) return is; 55 if ( !ne ) return is; 56 if (ne->name() == e->name()) { 56 if (ne->name() == e->name()) { 57 // Because e has const data members, canno 57 // Because e has const data members, cannot simply do *e = *ne 58 std::ostringstream os; 58 std::ostringstream os; 59 os << *ne; 59 os << *ne; 60 std::istringstream istst(os.str()); 60 std::istringstream istst(os.str()); 61 istst >> *e; 61 istst >> *e; 62 if (!istst) { 62 if (!istst) { 63 std::cerr << "???? Unexpected behavior i 63 std::cerr << "???? Unexpected behavior in StaticRandomStates::restore:\n" 64 << "The new engine, which had been inp 64 << "The new engine, which had been input successfully from istream\n" 65 << "has encountered a problem when used to s 65 << "has encountered a problem when used to set state of theEngine\n"; 66 is.clear(std::ios::badbit | is.rdstate() 66 is.clear(std::ios::badbit | is.rdstate()); 67 return is; 67 return is; 68 } 68 } 69 } else { 69 } else { 70 HepRandom::setTheEngine(ne); 70 HepRandom::setTheEngine(ne); 71 } 71 } 72 RandGauss::restoreDistState(is); 72 RandGauss::restoreDistState(is); 73 RandFlat::restoreDistState(is); 73 RandFlat::restoreDistState(is); 74 return is; 74 return is; 75 } 75 } 76 76 77 } // namespace CLHEP 77 } // namespace CLHEP 78 78