Get parent particle name for secondary gammaI

_Geant4 Version:_11.3.0
_Operating System:_ubuntu
Compiler/Version:
CMake Version:


Based on the suggestion under the relative question:How to get the name of parent particle that created the current track:

The purpose of getting the parent particle name for my code is to know the parent particle of produced gamma. so that I can draw different spectrum from different source respectively.
I develop the userTrackingInfo and stepping action corresponding function. Then I do something different from the Quote suggested-----I want to use UserTrackingaction to get parent particle name though track->GetUserInformation().

Then I meet some problems. I use TrackingAction

        const G4ParticleDefinition* particle = track->GetParticleDefinition();
        
        if (particle->GetParticleName() != "gamma") return;

        G4VUserTrackInformation* rawInfo = track->GetUserInformation();
        if (!rawInfo) {
            G4cout << "[ERROR] No UserInformation for TrackID=" << track->GetTrackID() << G4endl;
            
        }

            G4String parentName = "unknown";
            G4int parentID = -1;
  if (musrTrackingInfo* trackInfo =static_cast<musrTrackingInfo*>(track->GetUserInformation())) {
                parentName = trackInfo->GetParentName();
                parentID = trackInfo->GetParentID();
            }else{
                if (track->GetParentID() == 0) {
                    parentName = "primary";                    
                }else{
                    parentName="NoTrackingInfo";
                }
            }
            musrTrackingInfo* trackInfo =static_cast<musrTrackingInfo*>(track->GetUserInformation());
            G4cout << "[Gamma Tracking] TrackID: " << track->GetTrackID()
            << " | Parent: " << parentName 
            << " (ID:" << parentID << ")"
            << G4endl;

I can make it successfully. But when run the .mac file, it will print out

[Gamma Tracking] TrackID: 3 | Parent: NoTrackingInfo (ID:-1)
[Gamma Tracking] TrackID: 2 | Parent: NoTrackingInfo (ID:-1)
Unknown particle: NoTrackingInfo
Unknown particle: NoTrackingInfo

Showing that the gamma userInformation is not stored successfully.

As a reference for you, the following is the function I added in SteppingAction:

void musrSteppingAction::UserSteppingAction(const G4Step* aStep)  {

  G4Track* aTrack = aStep->GetTrack();
  G4Track* currentTrack = aStep->GetTrack();

  const std::vector<const G4Track*>* secondary = aStep->GetSecondaryInCurrentStep();

  for (const G4Track* secondaryTrack : *secondary){
    if (secondaryTrack->GetUserInformation()) continue;
      // const G4int parentID = currentTrack->GetTrackID();
      // const G4String& parentName = currentTrack->GetParticleDefinition()->GetParticleName();
      // const G4double parentEnergy = currentTrack->GetKineticEnergy();
      // const G4ThreeVector& parentMomentum = currentTrack->GetMomentum();
      musrTrackingInfo* info = new musrTrackingInfo(
        currentTrack->GetTrackID(),
        currentTrack->GetParticleDefinition()->GetParticleName(),
        currentTrack->GetKineticEnergy(),
        currentTrack->GetMomentum()
      );

    secondaryTrack->SetUserInformation(info);

    
  }

I have stuck in this question for a long time,:frowning:
Additionally,I also try other way,but it still does not work well.

Thanks!

Best,
Beining