Kill particles born in the wrong volume

Please fill out the following information to help in answering your question, and also see tips for posting code snippets. If you don’t provide this information it will take more time to help with your problem!

Geant4 Version: 11.1.1.
_Operating System:_Windows
_Compiler/Version:_Visual Studio 2022
_CMake Version:_3.23

Hello everyone,

is there a way to kill particle if they are launched (i.e. primary particles) inside a volume with a given copy number?

I tried with the function:

GetOriginTouchable() to obtain the G4Vtouchable of the volume where the particle is originated, and then kill the particle track inside the stepping class, as shown in the following lines:

const G4VTouchable *touchable_origin = aStep->GetTrack()->GetOriginTouchable();
G4int copyNo_origin = touchable_origin → GetCopyNumber();
if( particle_name == “e-” && particle_ID ==1 && copyNo_origin == 1 )
{
track->SetTrackStatus(fStopAndKill);
}

However, in such way I get that the particle is killed AFTER the first step it does.
Is it possible to kill it before the particle does the first step?

Thanks for the help,

Tommaso.

It must be done in TrackingAction::PreUserTrackingAction()

Thank you for the help!