diff --git a/include/remollRun.hh b/include/remollRun.hh index 7141725a9..4c294d901 100644 --- a/include/remollRun.hh +++ b/include/remollRun.hh @@ -24,6 +24,8 @@ class remollRun: public G4Run { static remollRunData* fRunData; public: static remollRunData* GetRunData(); + + static void UpdateSeed(); }; #endif //__REMOLLRUN_HH diff --git a/include/remollRunData.hh b/include/remollRunData.hh index 6781a500f..3c87f01a0 100644 --- a/include/remollRunData.hh +++ b/include/remollRunData.hh @@ -32,7 +32,7 @@ class remollRunData : public TObject { const char *GetGenName(){ return fGenName; } void SetBeamE(double E){ fBeamE = E; } - void SetSeed(unsigned int seed){ fSeed = seed; } + void SetSeed(unsigned long int seed){ fSeed = seed; } void AddMagData(filedata_t d){fMagData.push_back(d);} void SetMacroFile(const char *fn){ fMacro = remollTextFile(fn); } @@ -48,7 +48,7 @@ class remollRunData : public TObject { TTimeStamp fRunTime; long int fNthrown; - unsigned int fSeed; + long int fSeed; double fBeamE; char fGenName[__RUNSTR_LEN]; diff --git a/macros/tests/test_seed.mac b/macros/tests/test_seed.mac new file mode 100644 index 000000000..e2f601d0f --- /dev/null +++ b/macros/tests/test_seed.mac @@ -0,0 +1,23 @@ +# This will produce four files test_seed_$i.root, $i = 0,...,3. + +# The file test_seed_0.root should have a random seed and have unique content. +# The file test_seed_1.root and test_seed_3.root should have identical content. +# The file test_seed_2.root should have different content from test_seed_1.root and test_seed_3.root. + +/run/initialize + +/remoll/filename test_seed_0.root +/run/beamOn 100 + +/remoll/filename test_seed_1.root +/remoll/seed 123456 +/run/beamOn 100 + +/remoll/filename test_seed_2.root +/remoll/seed 654321 +/run/beamOn 100 + +/remoll/filename test_seed_3.root +/remoll/seed 123456 +/run/beamOn 100 + diff --git a/remoll.cc b/remoll.cc index c4007e23a..ff2110cea 100644 --- a/remoll.cc +++ b/remoll.cc @@ -35,6 +35,7 @@ typedef G4RunManager RunManager; #endif #include +#include namespace { void PrintUsage() { @@ -54,17 +55,16 @@ int main(int argc, char** argv) { clock_t tStart = clock(); - // Initialize the CLHEP random engine - unsigned int seed = time(0) + (int) getpid(); - unsigned int devrandseed = 0; - // /dev/urandom doens't block - FILE *fdrand = fopen("/dev/urandom", "r"); - if (fdrand) { - if (fread(&devrandseed, sizeof(int), 1, fdrand)) { - seed += devrandseed; - } else G4cerr << "Can't read /dev/urandom." << G4endl; - fclose(fdrand); - } + // Initialize the random seed + G4long seed = time(0) + (int) getpid(); + // Open /dev/urandom + std::ifstream urandom("/dev/urandom", std::ios::in | std::ios::binary); + // If stream is open + if (urandom) { + urandom.read(reinterpret_cast(&seed), sizeof(seed)); + urandom.close(); + } else G4cerr << "Can't read /dev/urandom." << G4endl; + // Parse command line options G4String macro; @@ -98,9 +98,8 @@ int main(int argc, char** argv) { #endif // Choose the Random engine - G4Random::setTheEngine(new CLHEP::RanecuEngine()); G4Random::setTheSeed(seed); - remollRun::GetRunData()->SetSeed(seed); + remollRun::UpdateSeed(); // Messenger remollMessenger* messenger = remollMessenger::GetInstance(); diff --git a/src/remollMessenger.cc b/src/remollMessenger.cc index 4301f3230..7471f3cab 100644 --- a/src/remollMessenger.cc +++ b/src/remollMessenger.cc @@ -60,9 +60,9 @@ remollMessenger::~remollMessenger() { } void remollMessenger::SetNewValue(G4UIcommand* cmd, G4String newValue) { if( cmd == seedCmd ){ - G4int seed = seedCmd->GetNewIntValue(newValue); + G4long seed = seedCmd->GetNewIntValue(newValue); G4Random::setTheSeed(seed); - remollRun::GetRunData()->SetSeed(seed); + remollRun::UpdateSeed(); } if( cmd == opticalCmd ){ diff --git a/src/remollRun.cc b/src/remollRun.cc index dd6cc4eca..cec941c96 100644 --- a/src/remollRun.cc +++ b/src/remollRun.cc @@ -1,5 +1,6 @@ #include "remollRun.hh" +#include "Randomize.hh" #include "G4Event.hh" #include "G4HCofThisEvent.hh" @@ -16,6 +17,11 @@ remollRunData* remollRun::GetRunData() return fRunData; } +void remollRun::UpdateSeed() +{ + GetRunData()->SetSeed(G4Random::getTheSeed()); +} + remollRun::remollRun() { } remollRun::~remollRun() { }