How to get the process name of Step?

Hi all,
I get the process name of the step through codes:

G4StepPoint* postPoint = step->GetPostStepPoint();
G4StepPoint* prePoint=step->GetPreStepPoint();
G4String processName_post = postPoint->GetProcessDefinedStep()->GetProcessName();
G4String processName_pre = prePoint->GetProcessDefinedStep()->GetProcessName();

The programs can be compiled and excuted, but after runing some events the program encounters
segmentation error: ###CAUGHT SIGNAL: 11 ### address: 0x128, signal = SIGSEGV, value = 11, description = segmentation violation. Address not mapped to object.
I guss it’s due to the program didn’t get pointer correctly in these four lines for some particular events.
How can I fix this problem?

The first step of any track will have its pre-step point “automatically defined.” There’s no process that “starts” a step, just processes that stop them. You should check the pointer returned by GetProcessDefinedStep() before dereferencing it.

Hi @mkelsey,
Thanks for your help. I changed the codes following your instruction, then the issuse is solved.

G4StepPoint* postPoint = step->GetPostStepPoint();
G4StepPoint* prePoint=step->GetPreStepPoint();
const G4VProcess *post_step=postPoint->GetProcessDefinedStep();
const G4VProcess *pre_step=prePoint->GetProcessDefinedStep();
if( post_step!=nullptr && pre_step!=nullptr){
    const G4String & processName_post = post_step->GetProcessName();
    const G4String & processName_pre = pre_step->GetProcessName();
}

That almost works, the strings won’t be visible outside of the if-block.

You might want to do it for each endpoint separately:

const G4String& processName_pre = pre_step ? pre_step->GetProcessName() : "";
const G4String& processName_post = post_step ? post_step->GetProcessName() : "";