Kill all secondary particles except in detector using StackingAction

Hello Geant4 users,

I try to kill all secondary particles induced by gamma and neutron interactions but only when they are generated out of my detector (I want to keep them inside the detector, especially e- and e+).

I tried this in StackingAction :

G4ClassificationOfNewTrack
StackingAction::ClassifyNewTrack(const G4Track* track)
{
   if (track->GetDefinition() == G4Gamma::Gamma()) return fUrgent;
   if (track->GetDefinition() == G4Neutron::Neutron()) return fUrgent;

   G4String volume_name_ = track->GetVolume()->GetName();

   if  ( volume_name_ != "detector" )
   {
   	if (track->GetDefinition() != G4Gamma::Gamma() && track->GetDefinition() != G4Neutron::Neutron()  )  return fKill; 
   }

}

But I get an error (e1xgps is my application, exgps_batch_14.in is my input file and QGSP_BIC_HP_PEN is my physics list) :

G4WT1 > ### Run 0 starts on worker thread 1.
G4WT2 > ### Run 0 starts on worker thread 2.
G4WT3 > ### Run 0 starts on worker thread 3.
G4WT0 > ### Run 0 starts on worker thread 0.
G4WT1 > → Event 0 starts with initial seeds (31164119,71878599).
*** Error in `./e1xgps’: munmap_chunk(): invalid pointer: 0x00007fffe7c702e0 ***
./exe_sonde_neutron_1.sh: line 45: 31575 Segmentation fault (core dumped) ./e1xgps -m exgps_batch_14.in -p QGSP_BIC_HP_PEN

Is there somone who already managed to do something similar ?

Thank you

the return value for volume_name == "detector" (and others, e.g. electrons) is not defined.

also, after the first two lines, track->GetDefinition() will never be gamma or neutron anymore. no need to check again :slight_smile:

Hello Weller,

Thank you very much for your answer.
According to your recommendations and including the proper .hh files, I tried :

G4ClassificationOfNewTrack
StackingAction::ClassifyNewTrack(const G4Track* track)
{
   if (track->GetDefinition() == G4Gamma::Gamma()) return fUrgent;
   if (track->GetDefinition() == G4Neutron::Neutron()) return fUrgent;

   if (   track->GetVolume()->GetName() != "detector" &&  track->GetDefinition() == G4Electron::Electron()  )  return fKill; 
   if (   track->GetVolume()->GetName() != "detector" &&  track->GetDefinition() == G4Positron::Positron()  )  return fKill; 

}

But

track->GetVolume()->GetName() == "detector"

appears to be always true and finally I never get any e- or e+ in my detector. Any idea to solve this problem ? I do not understand why the logical test doesn’t work… It kills every e- and e+ even if they are in the detector.

I tried also this :

   if (track->GetVolume()->GetName() == "detector") 
   {
   	G4cout << "  inside detector  " <<G4endl;
   }

But it is always true and I get the diplay whatever the volume is.

Thank you !
Geoffrey

do you have other volumes besides “detector”?
and how do you check if you get any electrons?

i still recommend to return fUrgent at the end of that method, in case all if-cases miss

I check if I get electrons through a .csv fille I fill in SteppingAction.cc according if the interaction occurs in "detector”.

Finally I managed with your explanations to get all interactions and particles in my detector and to kill all others outside excepted for gamma and neutron.

My code bellow :

G4ClassificationOfNewTrack
StackingAction::ClassifyNewTrack(const G4Track* track)
{
   if (track->GetDefinition() == G4Gamma::Gamma()) return fUrgent;
   if (track->GetDefinition() == G4Neutron::Neutron()) return fUrgent;

   if (track->GetVolume()->GetName() == "detector1") 
   {
   	//G4cout <<   track->GetVolume()->GetName()   <<G4endl;
	//if (track->GetDefinition() == G4Electron::Electron()) return fUrgent;
	//if (track->GetDefinition() == G4Positron::Positron()) return fUrgent;
        return fUrgent;
   }

   if (track->GetVolume()->GetName() == "detector2") 
   {
   	//G4cout <<   track->GetVolume()->GetName()   <<G4endl;
	//if (track->GetDefinition() == G4Electron::Electron()) return fUrgent;
	//if (track->GetDefinition() == G4Positron::Positron()) return fUrgent;
        return fUrgent;
   }

   if (track->GetDefinition() != G4Gamma::Gamma() && track->GetDefinition() != G4Neutron::Neutron()  )  return fKill;
   return fUrgent;
}

Thank you very much for your help !

Geoffrey

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