How to collect a number of particles using sensitive detector

Hello! Thanks to everyone i can reach this point but i need again your help. In calorimeterSD, in processhit I wrote this:

  G4StepPoint* posStep = step->GetPostStepPoint();
 G4int counter = 0;
 G4TrackVector* secTracks = G4TrackingManager->GimmeSecondaries();
if (posStep->GetStepStatus()==fGeomBoundary){
if(secTracks) {
size_t nmbSecTracks = (*secTracks).size();
for(size_t i = 0; i < nmbSecTracks; i++) {
  if((*secTracks)[i] ->GetDefinition() = G4Electron::Definition())
  G4cout<<"particles passing through"<<counter<<G4endl;

I have to count the number of electron produced by a muon in a layer of copper and passing through the boundary between the absorber and the gap. I tried to introduce an HitCollection but i don’t know how to do it properly so i made a loop over all the secondaries but i would like a more efficient way.
P.s. i started from the B4c example.
P.P.s. it gives me an error GimmeSecondaries() because of the brackets an again I don’t know why.


there are many Geant4 examples demonstrating how to score different values.

A complex analysis is shown in $G4INSTALL/Examples/extended/electromagnetic/TestEm5 - this example is working in multi-threaded mode and may be used without any modifications.

A single threaded analysis is shown in $G4INSTALL/examples/extended/hadronic/Hadr01

You may also consult other examples.


Thank you so much, i found what i needed in the hadr01 example. Thanks again, i think the topic can be closed.