Create loop with runManager

Dear all,
I want to simulate a batch of air showers and merge in one output file, when i simulate one shower it’s ok but when i try to use loop my program crush with exception.
I have the following code: (Detector construction and physics list is const and declare higher)

for (int j = 0; j < n; ++j) {
bl.read_event_comp(fileName.c_str(), j/*num_of_event*/); // read shower from corsika dat file
runManager->SetUserInitialization(new ActionInitialization(&bl));
numberOfEvent =; // get number of events
std::cout << "Done!" << '\n';
delete runManager;

Is it possible to reinitialize runManager like this?

No. Why do you want to do this? Each shower should be one event in your job. You set up the run manager, and either via macro command call /run/beamOn <N> (to get N showers), or in your executable call runManager->RunBeamOn(N);.

I want to do this because I don’t want to spend time on detector’s geometry and physics list initialization every time. In my program one shower is an one Run and one event is an one secondary particle in the shower. My secondary particles parameters (x,y,E,id) came from Corsika and Geant processes it (N in BeamOn is a number of particles in one shower). So there is no alternative way to do what i want?

No. Why don’t you call Corsika in your PrimaryGeneratorAction, and configure the generator with the size (number of desired particles) of the shower? Then you populate each event with N primaries, Geant does what it does best, and you get a new shower with each event in your run.

Because Corsika simulation is done and there is a large amount of data that needed to process. So i understood, thanks for answer.

So the Corsika output is available in a file? I guess that right now you’re reading that file one line at a time, and using the Geant4 event loop to step through the file? It would take a bit of work on your part, but you could set up your PrimaryGeneratorAction to handle that; open the file, read N lines per event, and close the file at end-of-run.

Yes, but i wrote special class that read the definite shower in Corsika dat file, store it to vector and pass to ActionInitializition and than to PrimaryGeneratorAction. There is no problems to simulate separate shower.

But it sounded like you’re not passing the shower, you’re passing one particle at a time, right? Well, if that’s how you want to do it, you can, but you cannot keep calling the runManager initialization every time. If you don’t want to make use of the Geant4 event loop, you’ll need to come up with something else to do.

One particle is an one G4Event in PrimaryGeneratorAction.

n_particle = 1;
fParticleGun  = new G4ParticleGun(n_particle)

Loop through all Events process the whole shower. You mean that i can change fParticleGun and in one Event pass the whole shower and N in RunBeamOn(N) will be the total number of showers?

Write your own PrimaryGeneratorAction. You can use G4ParticleGun if you want, to add particles one at a time to the event, then when all the particles you want are added you return from the generator and Geant4 takes over tracking them all. You can also instantiate the G4PrimaryVertex and G4PrimaryParticle(s) with your own code.