PreUserTrackingAction called multiple times per particle

I’m trying to record the initial angle of each particle I generate with respect to its (planar) source, its initial energy and the energy it deposits in my detector. I thought I could do this with the TrackingAction class, because track has the GetMomentumDirection function, so my looks like this:


void TrackingAction::PreUserTrackingAction(const G4Track* track) {
//record with particle info
G4ThreeVector direction = track->GetMomentumDirection();
G4double pi = 3.14159;
G4double theta = (180 / pi) * direction.theta();

G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
std::cout << “pre user tracking action called” << ‘\n’;

What happens is that this gets called multiple times per particle, so I wind up with incorrect data. Is there a proper place to record the initial momentum direction of the particle? I’m using a general particle source macro file.

1- why not to do that in PrimaryGeneratorAction ?
2- PreUserTrackingAction is called once per track, primaries as well secondaries. If you want to record here, you must check that it is a primary track : parentID = 0

I don’t really have a firm grasp on how to get particle properties yet, and I saw there was a way to get that info with track->GetMomentumDirection() so I kind of went with it.

What’s the best way to do that in PrimaryGeneratorAction? Does G4Event have a way to get particle information or is it better to use G4GeneralParticleSource()?

I think GeneralParticleSource() has, at least, the same functionalities than ParticleGun()
So, after GeneratePrimaryVertex() you can do

fGeneralParticleSource -> GetMomentunDirection(), GetEnergy(), GetPosition() …etc…