Segmentation fault scoring particle in volume

Hello,

I’m trying to store the information of neutrons entering a certain volume of the lab. To be more specific: I’m firing muons, these muons produce neutrons that travel through the lab and reach a veto. So I’m trying to score their spectrum before entering this veto. I tried the methods described here and here So, this is what I tried:

  1. In the steppingAction:

    if(particle == “neutron”){
    G4VPhysicalVolume* volIn = step->GetPreStepPoint()->GetPhysicalVolume();
    G4VPhysicalVolume* volFin = step->GetPostStepPoint()->GetPhysicalVolume();
    G4bool condition = step->IsLastStepInVolume();

    if(volIn->GetName()==“Lab” && volFin->GetName()==“MuonVetoVolume”){
    if(condition) store info
    }

  2. In steppingAction, use:

    G4bool condition = step->IsLastStepInVolume();
    if(particle == “neutron” && aStep->GetTrack()->GetVolume()->GetName()==“Lab” && aStep->GetTrack()->GetNextVolume()->GetName()==“MuonVetoVolume”){

    if(condition) store info
    }

But I get the same segmentation fault:
#5 0x00007f88c8242f90 in std::__cxx11::basic_string<char, std::char_traits, std::allocator >::compare(char const*) const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007f88d1e2c783 in G4String::operator== (this=0x18, str=0x7f88d1e919e6 “MuonVetoVolume”) at /home/jose/Codes/geant4.10.06/install/include/Geant4/G4String.icc:122

I run it again in verbose mode and I the last thing it does is:


G4Track Information: Particle = neutron, Track ID = 42836, Parent ID = 27784


Step# X(mm) Y(mm) Z(mm) KinE(MeV) dE(MeV) StepLeng TrackLeng NextVolume ProcName
0 -4.98e+04 -1.28e+04 -2.61e+04 0.134 0 0 0 Lab initStep
1 -6e+04 -1.55e+04 -2.42e+04 0.134 0 1.07e+04 1.07e+04 OutOfWorld Transportation

*** Break *** segmentation violation

When fire only neutrons from the Lab volume to the MuonVetoVolume both methods work, I checked it in the verbose mode. The problem is when I fire muons: some neutrons are stored correctly, but then one neutron of the above appear and produces a segmentation fault.

Where is my error? How could I solve it?
Thank you
Jose

1 Like

Hello Jose,

I spotted your message trying to find a solution for a similar problem you’ve related.
In my case, the error came after using the GetPreStepPoint and GetPostStepPoint in the same condition. I didn’t discover why is that. Try to change the condition to get the same information and see if the error persists. That worked for me.

For example if you want detect particles entering a certain volume you may use:
G4VPhysicalVolume* volIn = step->GetPreStepPoint()->GetPhysicalVolume();
G4bool condition = step->IsFirstStepInVolume();`
if(volIn == ‘yourVolume’ && particle == ‘neutron’ && condition == true)
{
Get the information you want
}

Another possible issue in your code is that the condition is a boolean and may need a true or false for the statement.

Hope it helps.

Cheers

Jordi

Hello, Jordi.

Thanks for your answer. It worked :slight_smile: So yes, probably is a problem of putting Pre and Post step in the same condition. Another thing that I tried and works is to ask if the next volume exists asking the track object:

if(aStep->GetTrack()->GetNextVolume())

because it could be that the next volume is out of the world. To be honest, I have to understand this in more detail :sweat_smile:

Thank you very much again.

Cheers

Jose