SteppingAction: if clause with time condition not working when asking for target particle

Hello everyone,
I found a code piece in the forum (Can we know which atom the deposited energy is deposited on - #3 by mkelsey) on how to get the target particle interacted with by a primary. Now I want to set a time condition of 250ns global time for the process:

if (step->GetPreStepPoint()->GetGlobalTime() < 250*CLHEP::ns) {
    
    G4StepPoint* post = step->GetPostStepPoint();
    const G4VProcess* proc = (post ? post->GetProcessDefinedStep() : 0);
    const G4HadronicProcess* had = dynamic_cast<const G4HadronicProcess*>(proc);
    G4HadronicProcess* had2 = const_cast <G4HadronicProcess*> (had);
    
    const G4Isotope* target = (had2 ? had2->GetTargetIsotope() : 0);
    
    if (target) {
        E_DeltaStep = step->GetDeltaEnergy();
        const G4String targetname = target->GetName();
        
        int result_C12 = std::strcmp(targetname, "C12" );
        
        
        if (result_C12 == 0) {
            fEventAction->AddEC12(E_DeltaStep);
        }
    }
}

However, this does not work. Also events that take longer than 250ns for example neutron capture can be seen in the spectrum. Which is not the case if I only write down the energy spectrum after the time condition (without the target loop). Has anyone an idea why the time condition does not work in this case?
Also I wonder: If I use GetTotalEnergyDeposit instead of GetDeltaEnergy I always get E=0 values for those steps entering the target loop. That’s why I use Delta energy instead… Has anyone an idea why this happens?
I think there might be an Important part of the simulation procedure which I don’t fully understand, so I am very happy for all input.

Thank you for taking the time reading this I greatly appreciate any comments!!
Lennard

not sure about the step->GetPreStepPoint()->GetGlobalTime(), maybe you need

step->GetPostStepPoint()->GetGlobalTime()

instead? Your way filters for stuff that starts before 250ns global time, but if the process/step itself extends across the 250ns boundary, it will still be valid. Or is that exactly what you intend?

Hi thanks for your reply!! Unfortunately this changes nothing and the odd thing is that if I ask for the energy outside the target loop I get the correct energy spectrum with the time cut applied:

if (step->GetPreStepPoint()->GetGlobalTime() < 250*CLHEP::ns) {
    
    G4StepPoint* post = step->GetPostStepPoint();
    const G4VProcess* proc = (post ? post->GetProcessDefinedStep() : 0);
    const G4HadronicProcess* had = dynamic_cast<const G4HadronicProcess*>(proc);
    G4HadronicProcess* had2 = const_cast <G4HadronicProcess*> (had);
    
    const G4Isotope* target = (had2 ? had2->GetTargetIsotope() : 0);
    
 fEventAction->AddEnergy(E_DeltaStep); // this Energy has time cut included
 
    if (target) {
        E_DeltaStep = step->GetDeltaEnergy();
        const G4String targetname = target->GetName();
        
        int result_C12 = std::strcmp(targetname, "C12" );
        
        
        if (result_C12 == 0) {
            fEventAction->AddEC12(E_DeltaStep); // this spectrum also shows steps that take longer than 250ns 
        }
    }
}

E_DeltaStep is not defined at that point, yet?!

yes sorry in the original code I defined it at the beginning of the stepping actions and I just inserted it somewhere here to keep the code short. And I must correct my previous statement. Both spectra AddEnergy and AddEC12 have the weird neutron capture energies in it. When I said that outside the target loop I get the correct spectrum it was not correct both spectra are weirdly not affected by the time cut. Is it maybe a problem using GetDeltaEnergy? I would rather use TotalEnergyDeposit but it gives 0 every time if I use it in AddEC12 instead of DeltaEnergy … But Why?

can you maybe test what the value of step->GetPostStepPoint()->GetGlobalTime() is for the events that are recorded in that code snippet?

it looks just the same :confused:

just to get it right: the time value is indeed smaller than 250ns, but the energy delta is a value that you would only expect for times > 250ns?

Yes. That is my problem :confused: