Changing the tracking order


I would like to record x,y,z,pname for particles created during the simulation where pname is the name of the 2nd particle created in the event.
so what I do is store the particle name when the Track ID is 2 and when Track ID > 2 I record the whole data.
unfortunately during the run my Geant4 handles the created particles backwise so pname is allways empty.
How can I reverse the order / fix it?

You can use a StackingAction to change the order in which tracks are processed. See the documentation, or the examples (find examples -name '*StackingAction*.cc').

Here’s some more detailed information, which doesn’t seem to be described clearly in the documentation. You may also want to read the G4UserStackingAction.hh and G4StackManager.hh files directly; they have fairly detailed explanatory comments.

The tracking stack does not have an interface for you to “sort tracks” directly. Instead, the stacking manager calls your G4UserStackingAction::ClassifyNewTrack() each time a new track (a primary or a secondary) is created.

You can implement ClassifyNewTrack() with criteria to put tracks either on the “urgent” stack, where they will be tracked immediately, from the last one back to the first one; or on the “waiting” stack.

All the “urgent” tracks will be processed first, and when that stack is empty, the “waiting” stack will be moved to “urgent” and processed.