Number of particles inside volume

how can i get the number of particles that have passed through any volume

As for me, the most obvious solution is to assign a sensitive detector to your volume and then to count unique track IDs in the ProcessHits() method. Some sort of std::map sounds pretty straightforward.
There’re also some default Geant4 solution called G4ScoringManager, which provides you a bunch of quantities it can track for you (e.g. nOfTrack - the number of tracks), but I personally prefer to control such things by myself, that’s up to you.

Cheers

Hi,
You can use steppingaction to do this. You can set boundary conditions and particles name to record the desired files. Illustration of this is presented below:

if((namePre==“DetectorPhys” ) && aStep->GetTrack()->GetDefinition()->GetParticleName() == “gamma” && (PostStep->GetStepStatus() == fGeomBoundary))
{
if(eKin > .0 ){
G4String CPName;
if(aStep->GetTrack()->GetCreatorProcess()!=0){
CPName = aStep->GetTrack()->GetCreatorProcess()->GetProcessName();
//theTrack->SetTrackStatus(fStopAndKill);
std::ofstream WriteDataIn(“DetectorGamma.out”, std::ios::app);
WriteDataIn << eKin << ‘\t’ << " "
<< PosX << ‘\t’ << " "
<< PosY << ‘\t’ << " "
<< PosZ << ‘\t’ << " "
<< MomX << ‘\t’ << " "
<< MomY << ‘\t’ << " "
<< MomZ << ‘\t’ << " "
<< globalTime << ‘\t’ << " "
//<< process << ‘\t’ << " "
//<< parentID << ‘\t’ << " "
//<< trackID << ‘\t’ << " "
//<< CPName << ‘\t’ << " "
<< G4endl;}

}
}

Regards,
Ananta