What is correct way to get position (x,y,z) of particle


I want to collect information on particles generated in radioactive decay. Currently, I am referring one code where it is counting number of particles generated in my radioactive decay as follows (it’s written in trackingAction.cc:

void TrackingAction::PreUserTrackingAction(const G4Track* track)
  Run* run 
   = static_cast<Run*>(G4RunManager::GetRunManager()->GetNonConstCurrentRun());
  G4ParticleDefinition* particle = track->GetDefinition();
  G4String name   = particle->GetParticleName();
  fCharge = particle->GetPDGCharge();
  fMass   = particle->GetPDGMass();  
  G4double Ekin = track->GetKineticEnergy();
  G4int ID      = track->GetTrackID();
  G4bool condition = false;
  // check LifeTime
  G4double meanLife = particle->GetPDGLifeTime();
  //count particles
  run->ParticleCount(name, Ekin, meanLife);
 ... and more code

void TrackingAction::PostUserTrackingAction(const G4Track* track)
  //keep only ions
  if (fCharge < 3. ) return;
  Run* run 
   = static_cast<Run*>(G4RunManager::GetRunManager()->GetNonConstCurrentRun());
  G4AnalysisManager* analysis = G4AnalysisManager::Instance();
  //get time
  G4double time = track->GetGlobalTime();
  G4int ID = track->GetTrackID();
  if (ID == 1) run->PrimaryTiming(time);        //time of life of primary ion
  fTime_end = time;
  //energy and momentum balance (from secondaries)
  const std::vector<const G4Track*>* secondaries 
                              = track->GetStep()->GetSecondaryInCurrentStep();
  size_t nbtrk = (*secondaries).size();
  if (nbtrk) {
    //there are secondaries --> it is a decay
    G4double EkinTot = 0., EkinVis = 0.;
    G4ThreeVector Pbalance = - track->GetMomentum();
    for (size_t itr=0; itr<nbtrk; itr++) {
       const G4Track* trk = (*secondaries)[itr];
       G4ParticleDefinition* particle = trk->GetDefinition();
       G4double Ekin = trk->GetKineticEnergy();
       EkinTot += Ekin;
       G4bool visible = !((particle == G4NeutrinoE::NeutrinoE())||
                          (particle == G4AntiNeutrinoE::AntiNeutrinoE()));
      ..and more code

Notice that, to count particles, the author has not written a stepping action. Before this, I thought that you must get it all in a stepping action file to get particle data. Also, take a look at how secondaries are extracted.

My doubt is if I want to know about my particle count, can I write code in steppingAction file instead of this?

Also, if I want to get position of particle should I write it in TrackingAction file like this:

G4double x = track->GetStep()->GetPostStepPoint()->GetPosition().x()

Or should I do it SteppingAction file:

G4double x =step->GetPostStepPoint()->GetPosition().x()

If these two works the same, can I write all code of steppingAction file into TrackingAction file by using the track object and secondaries pointer author used here.(instead of writing code in steppingAction)

1 Like

You can get position directly from G4Track::GetPosition(). The track’s parameters are always equal to the corresponding parameters of the current G4Step’s post-step point.

Thanks. Could you address this?

SteppingAction gives you direct access to the current G4Step at each step of the process. I believe that TrackingAction only gives you access to the track when it starts and when it ends.

Maybe this help you to achieve your goal,

G4ThreeVector trkPos = step->GetTrack()->GetPosition();

now you can call methods defined for x,y,z.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.