Storing particle trajectory vertices

I need to store the initial particle’s trajectory vertices into a root file. How do I do it in the simplest way?


If it’s only the original origin point of the primary, you can get this in your event action class. You should create a class that inherits G4UserEventAction and implement the EndOfEvent action method

virtual void EndOfEventAction(const G4Event* anEvent);

In this, you can do anEvent->GetPrimaryVertex().

If you wish to store all of the 3D points along the trajectory for the primary, you should create a class that inherits G4UserTrackingAction and implement the method

virtual void PreUserTrackingAction(const G4Track* aTrack)

Something like:

void MyUserTrackingAction::PreUserTrackingAction(const G4Track* aTrack)
  if (aTrack->GetParentID() == 0) // a primary

You should only do this only for the tracks you want and not all tracks as this will use a lot of memory!

You can collect the information in your EndOfEventAction with G4TrajectoryContainer* trajCont = event->GetTrajectoryContainer();.

See: Tracking — Book For Application Developers 11.0 documentation

You may also consider using SetStoreTrajectory(3) which will use G4RichTrajectory with G4RichTrajectoryPoints that has a lot of extra information.