How to check when particle enters the SD volume?

Hi everyone!

I want to check how many particles cross my calorimeter cell. And count number of particles with pz>0 and pz<=0

To do this, i check for any particles which entry my calorimeter cell volume with a following code:

G4bool MySensitiveDetector::ProcessHits(G4Step *step, G4TouchableHistory*){
    const G4VProcess* currentProcess=preStepPoint->GetProcessDefinedStep();
    if(currentProcess != 0){
        const G4String & stepProcessName = currentProcess->GetProcessName();
        G4String volumePos = step->GetTrack()->GetNextVolume()->GetName();
        if(stepProcessName == "Transportation" && volumePos == "Si"){
            G4double pz = (step->GetTrack()->GetMomentum()).getZ();
            if(pz > 0.) hit->AddDirParticle();
            else hit->AddBSParticle();

But in the result I have a lot of energy deposits in cells with
n_dir_particles =0 and n_bs_particles = 0.

Do some particles can enter the SD volume omitting Transportation process?
Or something wrong with my code for volumes boundary cross check?

I found more elegant way to do it, but still I found some cells with energy deposits and no particles are seen on boundaries
Now I do it this way:

if(preStepPoint->GetStepStatus() == fGeomBoundary)

thanks and have a nice day,

Your first code assumes that the particle will reach some exit boundary. But many of them -the low energy ones- will die inside the volume, hence not reaching a boundary. For the hit cells a bit far appart of the shower center, it is likely that these cells will see only a few low energy particles, with none of them exiting the volume. If such cases, your hit will remain empty.
(Please note BTW that your first code -with the GetNextVolume- is looking from a boundary at the end of the step -particle exiting the volume- while you second code snippet -preStepPoint->GetStepStatus()- is checking if the particle has crossed a boundary at the beginning of the step.)


[quote=“marcverderi, post:2, topic:867”]
Thanks for the answer!
But let me understand this:
If I put “Si” as my Sensitive detector, it will loop only through the steps inside this volume.

stepProcessName == "Transportation" && volumePos == "Si"
Is it possible at all?
That step has PostStepPoint inside sensitive detector and Process is “Transportation”

Shouldn’t it be only with PreStepPoint outside the SD but then it will not be looped over
Which makes above code is useless


preStepPoint->GetStepStatus() == fGeomBoundary
works fine if PreStepPoint was created after Boundary (particle entered SD)
But if particle leaves the SD: PreStepPoint created by Boundary crossing already in another volume and won’t be measured.
Am I correct?
So the GetStepStatus() look for iteration back PostStepPoint, lets say

Am I correct?