CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: 00002 // -*- C++ -*- 00003 // 00004 // ----------------------------------------------------------------------- 00005 // HEP Random 00006 // --- StaticRandomStates --- 00007 // class implementation file 00008 // ----------------------------------------------------------------------- 00009 // 00010 // ======================================================================= 00011 // Mark Fischler - Created: Dec. 21, 2004 00012 // Mark Fischler - Modified restore() to utilize anonymous engine input 00013 // to create anonymous restore of the static distributions 00014 // 00015 // ======================================================================= 00016 00017 #include "CLHEP/Random/StaticRandomStates.h" 00018 #include "CLHEP/Random/RandGauss.h" 00019 #include "CLHEP/Random/RandFlat.h" 00020 #include <string> 00021 #include <sstream> 00022 00023 //======================// 00024 // // 00025 // Maintenance warning: // 00026 // // 00027 //======================// 00028 // 00029 // Currently, only two distributions (RandFlat and RandGauss) have cached 00030 // distribution state. All such distributions must be saved below, so if 00031 // another such distribution is added, this implementation file must be 00032 // modified to reflect that. 00033 00034 namespace CLHEP { 00035 00036 00037 std::ostream & StaticRandomStates::save(std::ostream & os){ 00038 RandGauss::saveFullState(os); 00039 RandFlat::saveDistState(os); 00040 return os; 00041 } 00042 00043 #ifdef NOTYET 00044 std::istream & StaticRandomStates::restore(std::istream & is) { 00045 RandGauss::restoreFullState(is); 00046 RandFlat::restoreDistState(is); 00047 return is; 00048 } 00049 #endif 00050 00051 std::istream & StaticRandomStates::restore(std::istream & is) { 00052 HepRandomEngine * e = HepRandom::getTheEngine(); 00053 HepRandomEngine *ne = HepRandomEngine::newEngine(is); 00054 if ( !is ) return is; 00055 if ( !ne ) return is; 00056 if (ne->name() == e->name()) { 00057 // Because e has const data members, cannot simply do *e = *ne 00058 std::ostringstream os; 00059 os << *ne; 00060 std::istringstream istst(os.str()); 00061 istst >> *e; 00062 if (!istst) { 00063 std::cerr << "???? Unexpected behavior in StaticRandomStates::restore:\n" 00064 << "The new engine, which had been input successfully from istream\n" 00065 << "has encountered a problem when used to set state of theEngine\n"; 00066 is.clear(std::ios::badbit | is.rdstate()); 00067 return is; 00068 } 00069 } else { 00070 HepRandom::setTheEngine(ne); 00071 } 00072 RandGauss::restoreDistState(is); 00073 RandFlat::restoreDistState(is); 00074 return is; 00075 } 00076 00077 } // namespace CLHEP