Killing Secondaries inside Specific Logical Volumes

Hello,
I am trying to use the StackingAction::ClassifyNewTrack() method to identify secondary particles and kill them if they are generated inside a specific logical volume. below is the code

#include "G4VPhysicalVolume.hh"
#include "G4LogicalVolume.hh"
#include "G4LogicalVolumeStore.hh"
#include "G4LogicalCrystalVolume.hh"

G4ClassificationOfNewTrack
StackingAction::ClassifyNewTrack(const G4Track* aTrack){
    G4ClassificationOfNewTrack status = fUrgent;

    G4cout << "point1" << G4endl;
    G4VPhysicalVolume* aPV = aTrack->GetVolume();
    G4cout << "point2" << G4endl;
    G4LogicalVolume* aLV = aPV->GetLogicalVolume();
    G4cout << "point3" << G4endl;

    if(fKillSecondary) {
        // kill secondaries except primary
        if (aTrack->GetTrackID()>1 && G4LogicalCrystalVolume::IsLattice(aLV)) status = fKill}
    return status;
};

For some reason, I get segmentation error after “point2” is printed out. Any ideas why there would be a problem grabbing the LogicalVolume?

Thank you,
Alex

You’re getting the segmentation fault because you dereference aPV without testing whether it is null or not.

I have noticed that the StackingAction may be called for tracks before they have been processed by G4Navigator to assign a volume. In my code, I have written it like this:

G4VPhysicalVolume* trkvol = track.GetVolume();
if (!trkvol) trkvol = GetVolumeAtPoint(track.GetPosition());

with

G4VPhysicalVolume* GetVolumeAtPoint(const G4ThreeVector& pos) {
  static G4ThreadLocal G4Navigator* theNavigator = 0;
  if (!theNavigator) theNavigator = new G4Navigator;

  // Make sure current world volume is the one in use  
  G4VPhysicalVolume* theWorld =
    G4TransportationManager::GetTransportationManager()->
      GetNavigatorForTracking()->GetWorldVolume();

  if (theNavigator->GetWorldVolume() != theWorld)
    theNavigator->SetWorldVolume(theWorld);

  return theNavigator->LocateGlobalPointAndSetup(pos,0,false);
}

@mkelsey , thank you very much. This must be the reason! Thanks for the solution too!
I was thinking of killing the secondaries within the SteppingAction (as it is more usual to my little knowledge) but I am not sure if that would be equivalent to killing them with a StackingAction level.