00001 #include "BDSRandom.hh"
00002 #include "BDSExecOptions.hh"
00003 #include "BDSGlobalConstants.hh"
00004 #include "BDSDebug.hh"
00005
00006 #include "globals.hh"
00007
00008 #include "CLHEP/Random/Random.h"
00009 #include "CLHEP/Random/JamesRandom.h"
00010
00011 #include <ctime>
00012
00013 void BDSRandom::CreateRandomNumberGenerator()
00014 {
00015
00016 #ifdef BDSDEBUG
00017 G4cout << __METHOD_NAME__ << "> Initialising random number generator." << G4endl;
00018 #endif
00019 CLHEP::HepRandom::setTheEngine(new CLHEP::HepJamesRandom);
00020 }
00021
00022 void BDSRandom::SetSeed()
00023 {
00024 #ifdef BDSDEBUG
00025 G4cout << __METHOD_NAME__ << " set the seed" << G4endl;
00026 G4cout << __METHOD_NAME__ << " seed from BDSGlobalConstants = "
00027 << BDSGlobalConstants::Instance()->GetRandomSeed() << G4endl
00028 << __METHOD_NAME__ << " seed from BDSExecOptions = "
00029 << BDSExecOptions::Instance()->GetSeed() << G4endl
00030 << __METHOD_NAME__ << " seed set in exec options : "
00031 << BDSExecOptions::Instance()->SetSeed() << G4endl;
00032 #endif
00033
00034
00035
00036
00037 long seed = 0;
00038 if(BDSGlobalConstants::Instance()->GetRandomSeed()<=0)
00039 seed = time(NULL);
00040 else
00041 seed = BDSGlobalConstants::Instance()->GetRandomSeed();
00042
00043
00044 if(BDSExecOptions::Instance()->SetSeed())
00045 {seed = BDSExecOptions::Instance()->GetSeed();}
00046
00047 #ifdef BDSDEBUG
00048 G4cout << __METHOD_NAME__ << "> selected seed = " << seed << G4endl;
00049 #endif
00050
00051 CLHEP::HepRandom::setTheSeed(seed);
00052
00053
00054 G4cout << __METHOD_NAME__ << "> Random number generator's seed = "
00055 << CLHEP::HepRandom::getTheSeed() << G4endl;
00056 #ifdef BDSDEBUG
00057 BDSRandom::PrintFullSeedState();
00058 #endif
00059 }
00060
00061 void BDSRandom::PrintFullSeedState()
00062 {
00063 G4cout << __METHOD_NAME__ << "> Random number generator's state: " << G4endl << G4endl;
00064 CLHEP::HepRandom::saveFullState(G4cout);
00065 G4cout << G4endl;
00066 }
00067
00068 void BDSRandom::WriteSeedState()
00069 {
00070
00071
00072
00073 G4String seedstatefilename = BDSExecOptions::Instance()->GetOutputFilename() + ".seedstate.txt";
00074 std::ofstream ofseedstate (seedstatefilename.c_str());
00075 if (ofseedstate.is_open())
00076 {CLHEP::HepRandom::saveFullState(ofseedstate);}
00077 else
00078 {
00079 G4cout << __METHOD_NAME__ << "> cannot open file : " << seedstatefilename << G4endl;
00080 exit(1);
00081 }
00082 }
00083
00084 void BDSRandom::LoadSeedState(G4String inSeedFilename)
00085 {
00086 #ifdef BDSDEBUG
00087 G4cout << __METHOD_NAME__ << "> loading file: " << inSeedFilename << G4endl;
00088 #endif
00089 std::ifstream ifseedstate;
00090 ifseedstate.open(inSeedFilename);
00091 if (ifseedstate.is_open())
00092 {CLHEP::HepRandom::restoreFullState(ifseedstate);}
00093 else
00094 {
00095 G4cout << __METHOD_NAME__ << "> cannot open file : " << inSeedFilename << G4endl;
00096 exit(1);
00097 }
00098 ifseedstate.close();
00099 #ifdef BDSDEBUG
00100 BDSRandom::PrintFullSeedState();
00101 #endif
00102 }
00103
00104