Skip to content

Commit

Permalink
Merge pull request #56 from JeffersonLab/hotfix-random-seed
Browse files Browse the repository at this point in the history
Hotfix random seed
  • Loading branch information
wdconinc authored Dec 15, 2017
2 parents 3223485 + f5f7165 commit c45cfb3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
2 changes: 2 additions & 0 deletions include/remollRun.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class remollRun: public G4Run {
static remollRunData* fRunData;
public:
static remollRunData* GetRunData();

static void UpdateSeed();
};

#endif //__REMOLLRUN_HH
4 changes: 2 additions & 2 deletions include/remollRunData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand All @@ -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];

Expand Down
23 changes: 23 additions & 0 deletions macros/tests/test_seed.mac
Original file line number Diff line number Diff line change
@@ -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

25 changes: 12 additions & 13 deletions remoll.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef G4RunManager RunManager;
#endif

#include <ctime>
#include <fstream>

namespace {
void PrintUsage() {
Expand All @@ -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<char*>(&seed), sizeof(seed));
urandom.close();
} else G4cerr << "Can't read /dev/urandom." << G4endl;


// Parse command line options
G4String macro;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/remollMessenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ){
Expand Down
6 changes: 6 additions & 0 deletions src/remollRun.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "remollRun.hh"

#include "Randomize.hh"
#include "G4Event.hh"
#include "G4HCofThisEvent.hh"

Expand All @@ -16,6 +17,11 @@ remollRunData* remollRun::GetRunData()
return fRunData;
}

void remollRun::UpdateSeed()
{
GetRunData()->SetSeed(G4Random::getTheSeed());
}

remollRun::remollRun() { }

remollRun::~remollRun() { }

0 comments on commit c45cfb3

Please sign in to comment.