My code is based on the Geant4 c++ physics toolkit but I believe my problem may be more related to basic programming knowledge. Essentially a particle moves through the simulation in a loop of steps, and I am trying to get the x-position where it leaves a particular volume and enters another. To do this, I have added the last 'if' block to the "SteppingAction.cc" source file which, upon the particle crossing the relevant boundary, gets the 3-D track position, prints the x value, and then writes the x value as a double to a csv file. This successfully prints the correct position but after the simulation finishes iterating the rest of the particles and before the event is visualized, I get a segmentation fault and crash. This happens even if I delete either the print line or the csv write line, but if I remove both leaving only the line which gets the track position, I no longer receive the fault.
G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
LXeSteppingAction::LXeSteppingAction(LXeEventAction* ea)
: fOneStepPrimaries(false)
, fEventAction(ea)
{
fSteppingMessenger = new LXeSteppingMessenger(this);
fExpectedNextStatus = Undefined;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
LXeSteppingAction::~LXeSteppingAction() { delete fSteppingMessenger; }
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void LXeSteppingAction::UserSteppingAction(const G4Step* theStep)
{
G4Track* theTrack = theStep->GetTrack();
const G4ParticleDefinition* part = theTrack->GetDefinition();
G4int pdg = part->GetPDGEncoding();
if (theTrack->GetCurrentStepNumber() == 1)
fExpectedNextStatus = Undefined;
LXeUserTrackInformation* trackInformation =
static_cast<LXeUserTrackInformation*>(theTrack->GetUserInformation());
G4StepPoint* thePrePoint = theStep->GetPreStepPoint();
G4VPhysicalVolume* thePrePV = thePrePoint->GetPhysicalVolume();
G4StepPoint* thePostPoint = theStep->GetPostStepPoint();
G4VPhysicalVolume* thePostPV = thePostPoint->GetPhysicalVolume();
G4OpBoundaryProcessStatus boundaryStatus = Undefined;
if (theTrack->GetParentID() == 0)
{
if (thePostPV->GetName() == "scintillator1")
{
if (thePrePV->GetName() != "scintillator1")
{
CLHEP::Hep3Vector Det1 = theTrack->GetPosition();
std::cout << Det1[0] << std::endl;
analysisManager->FillNtupleDColumn(0, 14, Det1[0]);
}
}
}
}
I have also tried writing a separate function to write to the csv file, and calling from within the if statement, with the same result. I think I may be overlooking something basic, any ideas? Thank you.