Countig particles at the boundary

I have to check how many electrons cross the boundary between two different material (same geometry of a calorimeter). At this point (see the code) i don’t know how to count the number of electrons with that conditions. I’m only a student and i’m trying to learn geant by myself so i miss some fundamentals and i’m sorry for this trivial question.
Thanks to all.

if (particleName==“electron” && pre_vol_name ==“Abso” && post_vol_name == “Gap”){

What you have will work.

You can test for postStepPoint->GetStepStatus() == fGeomBoundary, which means the track hit the boundary leaving the volume. That’s an integer compare, so faster than comparing strings.

You can also test the track->GetParticleDefinition() directly against G4Electron::Definition(), which is a pointer comparison instead of strings.

SteppingAction or a sensitive detector (SD) gets called on every step of every track, so if you can avoid string comparisons your job will run faster.

Thank you so much for the reply. But will fGeomBoundary consider the boundary between the volume and the world? What should i use to count the particles? I only need the number of electrons.