_Geant4 Version:_v11.3.0
_Operating System:_macOS Sequoia 15.2
_Compiler/Version:_v16.0.0
_CMake Version:_v3.31.2
Hello! I’m using Geant4 to model the particle production from a 120 GeV beam of protons incident on a beam dump made of iron. I’ve recently made some changes to my code, trying to record the particle name, kinetic energy, and momentum right as it hits the detector volume using ntuples to write to a .csv file, instead of recording the data each step through the detector. The code works for just one event, however, when I try to generate more than one event using “/run/beamOn #” or to generate a second event after the first using the both the green play button on the visualizer and “/run/beamOn 1”, my program crashes with a segmentation fault.
The strange thing is that when the segmentation fault occurs, it seems to occur after the beginning of the event generation as the initial proton data will be saved to the .csv file but never any other generated particles.
Here’s an example of the output with just one event, where the segmentation fault does not occur:
Particle Name, 4-mom.: proton, (119998.0548431, -1.5579852408857, 3448.6744494557, 120883.50359411)
Particle Name, 4-mom.: neutron, (0.0030342622349053, -1.3995103565934, 1.9054887945835, -0.33506387510176)
And here’s the output when I try to generate 2 events:
Particle Name, 4-mom.: proton, (119998.0548431, -1.5579852408857, 3448.6744494557, 120883.50359411)
Particle Name, 4-mom.: neutron, (0.0030342622349053, -1.3995103565934, 1.9054887945835, -0.33506387510176)
Particle Name, 4-mom.: proton, (119998.33490934, -1.1788201029167, 3379.7007084187, 120885.73183486)
zsh: segmentation fault ./simDarkQuest
I’ve attached my new code, where I save the particle data, below. Any advice or suggestions anyone has on how to fix this issue would be greatly appreciated!
#include "detector.hh"
#include "G4ParticleDefinition.hh"
#include "G4AnalysisManager.hh"
MySensitiveDetector::MySensitiveDetector(G4String name) : G4VSensitiveDetector(name)
{}
MySensitiveDetector::~MySensitiveDetector()
{}
G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROHist)
{
G4Track *track = aStep->GetTrack();
G4LogicalVolume* post_volume = aStep->GetPostStepPoint()->GetTouchableHandle()->GetVolume()->GetLogicalVolume();
G4String vol_name = post_volume->GetName();
G4bool boundary = aStep->GetPreStepPoint()->GetStepStatus()==fGeomBoundary;
if ((vol_name == "Detector") && (boundary)){
//get kinetic energy, momenta, particle name
G4double KE = aStep->GetPostStepPoint()->GetKineticEnergy();
G4double p_x = aStep->GetPostStepPoint()->GetMomentum().x();
G4double p_y = aStep->GetPostStepPoint()->GetMomentum().y();
G4double p_z = aStep->GetPostStepPoint()->GetMomentum().z();
G4String particleName = aStep->GetTrack()->GetDynamicParticle()->GetDefinition()->GetParticleName();
//print out the 4-mom.
G4cout << "Particle Name, 4-mom.: " << particleName << ", " << "(" << KE << ", " << p_x << ", " << p_y << ", " << p_z << ")" << G4endl;
//record the 4-mom.
G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
analysisManager->FillNtupleSColumn(0, 0, particleName);
analysisManager->FillNtupleDColumn(0, 1, KE);
analysisManager->FillNtupleDColumn(0, 2, p_x);
analysisManager->FillNtupleDColumn(0, 3, p_y);
analysisManager->FillNtupleDColumn(0, 4, p_z);
analysisManager->AddNtupleRow(0);
}
return 1;
}