I would like to know if its possible to set some random seed or something similar in order to reproduce the results of a previous simulation, I would use it to verify that changes to the simulation code did not introduce unwanted effects in the simulation by comparing two simulation outputs.
If its not possible due to the nature of Monte Carlo simulations, is there a way to seed the particles generated by the generator? What would be the recommended way to achieve this, maybe using the FILE generator?
154 // choose the Random engine
155 CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
156 time_t systime = time(NULL);
157 long seed = (long)systime + restRun->GetRunNumber() * 13;
158 CLHEP::HepRandom::setTheSeed(seed);
We use the time of the system to generate an arbitrary seed. However, we could fix this number and the resulting simulation should be the same.
It would suffice adding a new member to TRestG4Metadata. For example a TString fSeed.
/// This uses whatever is implemented right now
<parameter name="seed" value="random">
/// This uses 18571 as seed number
<parameter name="seed" value="18571">
In any case, the value of the seed will be stored in fSeed.
Note that in order to reproduce the simulation we need to use the same physics list and the same geant4 version. The geant4 version is also recorded in restG4.
We bias the time using the runNumber because when launching jobs to a cluster it may happen that several jobs are sent in a bunch and all of them get the same starting time, and then we get the same results in several files.
Do you mean if I want the same simulation results, I need to define a member variable such as āfSeedā in class TRestG4Metadataļ¼ which let me set the seed value in .rml file, and also need to modify the restG4.cc like that:
Exactly, we might in fact define an Long_t fSeed inside TRestG4Metadata. And if fSeed is -1. Then we use systime. If we do not define anything inside the RML, then seed value will be always -1.
154 // choose the Random engine
155 CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
156 time_t systime = time(NULL);
157 long seed = restG4Metadata->GetSeed();
158 if (seed == -1) seed = (long)systime + restRun->GetRunNumber() * 13;
159 CLHEP::HepRandom::setTheSeed(seed);
However, if seed was -1 we need to update the member value inside TRestG4Metadata.
So that we store the value of seed we used.
I have verified by running two simulations (2.2.11) that setting a constant seed in restG4.cc works correctly as Javier described. I get the same number of events with same directions and origins.
What is the reason of this *13 multiplication? I would assume that just adding the run number to the seed would work correctly if we want the seed to also depend on the run number right?