Drawing a certain number of trajectories

Dear Geant4 users, I am trying to draw only the first fifty particles that are generated in an application. My aim is to run use /run/beamOn and generate 100000 particles but obviously the graphics interface is not capable of withstanding so many.

I am modifying the example B1 and tried to insert this bit of code to do that.

if (i < 50) {
std::cout << i << “\n”;
if (i == nofEvents){

The seccond if statement is to reset the counter variable i when the run is over but I cannot seem to define the number of events correctly. I have tried inserting the code in the B1EventAction.cc and in the B1RunAction.cc but it is not working. Does anyone have some idea as to why??

What I am basically looking for is a way of telling when the run is over so that I can reset the counter. I am not being able to find it.
Or is there a command that allows me to set the maximum number of trajectories that I want it to show without stopping the simulation, meaning that it would carry on but just not display them?
Any help is much apretiated.

Mmm. Not sure there’s an easy way of doing what you want.

If you disable vis


and include

if (i < 50) {

then at end of run


you will be able to see the first 50 events…but not until end of run.

Is it not good enough simply to

/run/beamOn 50

have a look and see if all’s OK, then

/run/beamOn 100000


If you want to see events during a run without slowing down the workers you could

/vis/multithreading/actionOnEventQueueFull discard

but you would still get lots of events drawn, which might be a problem for the vis system. (Perhaps not if

/vis/scene/endOfEventAction refresh


I wonder if you could use trajectory filtering. I’ve never tried this, but you could filter on event ID. If this appeals to you I’ll look into it further.


Thanks for the reply John. Yes, you are right, it is enough to run a test with 50 first and then run the 100000 with the interface disabled. At least for the level at which I currently am with geant4.

Hi @allison, I have another question related to this. Is there a way of generating the 100000 photons but only keeping those that hit the scoring volume?

Thank you for your time.

Not sure what you mean by “keeping”. But if you mean keeping for visualisation, then if you can figure out how to flag such an event in, say, your stepping action, you can /vis/disable and

if (flag) {

and then /vis/enable and view them at end of run. But you already know about that.

Another way is to use trajectory filtering. Try


That sounds to be exactly what you want.

In general, it pays to cast around in the command hierarchy using ls or help or, with Qt, browse the help tab.

Thank you very much for the help John.

/vis/filtering/trajectories/create/encounteredVolumeFilter did indeed do what I was referring to.