Stop Run After Set Number of Events Recorded

I’m working on a simulation of a germanium detector that uses sensitive detectors and hits to write energy deposition of each event to a root analysis file. A large number of particles have 0 energy deposition and I do not write these to the root analysis file. I am comparing the simulation data with experimental calibration data, and I need to have the same or a comparable number of data points in my simulation data. Is there away I can run my simulation until a specific number of data points have been written to the analysis file, as opposed to just using /run/beamOn N and trying to guess what N will give me enough data points?

Thanks in advance

Not sure how you’d do that, but do you really want to. After all, the number of events you get, either in simulation or experiment, for a given number of interactions, is subject to statistical fluctuation sqrt(number of selected events). To match numbers exactly would be artificial and may even introduce a bias. Better to fix the number of events generated and relate that to the number of experimental interactions. If that relationship is unknown, i.e., the ratio, then that is estimated from the ratio of selected events, subject to sqrt(N) in each.
Hope that makes sense.

Well yes I do want to do that. I need to compare the results of my simulation with experimental results so the number of recorded events has to be similar for any comparison to even make any sense.

There is a call somewhere in G4RunManager (I think) to softly abort the run but I can’t remember off the top of my head. But there is always the option of just putting a condition variable around the data structure you are updating and setting it to false when you reached the hit the point where you don’t want to collect any more data. Or make all the particles in the PGA after that geantinos. Or kill all the tracks in BeginTrackingAction when you reach it.

1 Like

Mm yes some good ideas. Thanks for the help!

Did you solve it already? I wanted a similar thing and the solution was to make a custom RunManager, derived from G4RunManager and override the virtual RunTermination() method. At the end of the termination you can call another beamOn if needed.

So what you could do is set a constant N_target for the amount of non-zero data-points you want and then in your sensitive detectors count every non-zero energy deposition, N. Then in your RunTermination() method in your custom RunManager at the end you call
if (N<N_target){ //Run another beamOn}

Then in your application you will only have to /run/beamOn 1 and it will go on until it has reached the desired amount of data points.

I didn’t have time to work on it unfortunately. This simulation is for my undergraduate thesis which I have a deadline for so I had to move on as trying to figure out how to implement it was taking too much time. I may try your solution when I have more time though, thank you for the reply