Hiya,
I’m working on a Geant4 simulation for generating multiple image samples (with randomised/varying detector phantom geometries) which will then be used for training a deep learning model.
Because of this, my Geant4 app needs to be able to cleanly reset itself after each run, as I would need to delete my detector geometry, and then construct it again (with a new randomised configuration) before each run.
Currently, my logic for executing multiple runs is done in my small custom RunManager class (which just inherits from G4MTRunManager), and the function to do multiple beamOn’s is shown here:
void RunManager::ExecuteSimulations(G4int nRuns, G4int nEventsPerRun) {
for (G4int i = 0; i < nRuns; i++) {
SetRandomSeed(fSeedInstance + fRunsThisInstance);
ReinitializeGeometry(true); // Force geometry to be reinitialized
Initialize();
InitializeGeometry();
GeometryHasBeenModified(); // Let the kernel know that the geometry has been modified
G4cout << "Starting run " << fRunsThisInstance << " with random seed " << GetRandomSeed() << G4endl;
// Call base class BeamOn
G4MTRunManager::BeamOn(nEventsPerRun);
fRunsThisInstance++;
}
}
Trouble is, there seem to be a few tell-tale signs that something’s not going right - as although the geometry does indeed change each run (as I use the G4Navigator to generate a 2D slice of our phantom before each run begins, and this does verify that it changes per run), our visualiser does not update each run (showing the geometry which existed before the first run, and not changing after subsequent runs)
Now, that is likely just an issue with the visualiser not refreshing the geometries that it’s drawing, but there are also a few other indicators that I’m not resetting the geometry right, as I get a warning exception whenever I try to begin my >2nd run -
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : DET1010
issued by : G4SDStructure::AddNewDetector()
mfd had already been stored in /. Object pointer is overwritten.
It's users' responsibility to delete the old sensitive detector object.
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------
Which seems to me that our SD’s are not being properly deleted after the previous run. I assume I can call sdManager->Clear()
at some point before my next run, but I’m just wondering about the other things that I would have missed out on properly clearing.
Lastly, the Accumulables that I collect for each run do not seem to be resetting properly either. I do call G4AccumulableManager::Instance()->Reset()
at the start of my RunAction class (in BeginOfRunAction
), however I suspect that it’s the G4THitsMap
which is not being properly reset before a new run (if so, how can I reset that?).
I’d greatly appreciate some guidance on the “correct” or proper way to clean up after each run so that we can run multiple beamOn’s without previous runs interfering. Thank you!
Geant4 Version: 11.2.2
Operating System: Ubuntu 24.04.1 LTS
Compiler/Version: gcc 13.3.0
CMake Version: 3.28.3