Is it possible to "rerun" a simulation with same initial conditions?

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?

Thanks.

The seed is generated in restG4

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.

 98     std::string g4Version = TRestTools::Execute("geant4-config --version");
 99     //g4Version.erase(std::remove(g4Version.begin(), g4Version.end(), '\n'), g4Version.end());
100     restG4Metadata->SetGeant4Version(g4Version);

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.

Remember to increase the value of ClassDef if you add a fSeed member.

hi, jgalan

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:

image

Thank you.

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.

We can in any case do:

160     restG4Metadata->SetSeed(seed);

Thanks for the answer.

I will use your suggestion for the seeding.

I have created an issue with this topic here: Sign in Ā· GitLab

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.

1 Like

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?

I donā€™t have explicit need the seed depending on the run number, I just need to assure two jobs will get different seed values.

In the other hand, I like number 13.