Number Of Particle inside EM Shower

Hello, i’m new on Geant4. I have to carry out a university project.
My goal is to simulate an EM shower with a initial beam of 10 GeV electrons in two different materials. I have no problems with the geometry construction or the system, but i’m not still able to answer a question “try to reconstruct the em shower profile in terms of number of particles and energy”.
For the energy i have no problems, but what about the number of particles? Is there some command that store every particle which is created in the shower?
For example my idea is to act inside the SteppingAction.cc file and at each step count in someway the particles. Does anyone know the answer?
Thanks in advice.

You can certainly do the counting yourself in SteppingAction.cc or in a sensitive detector (SD) attached to the volume. We the macro-based scorers, I believe you can define a “flux scorer”, which counts particles passing through each voxel.

Hi @mkelsey thanks for the feedback! What do you mean with “counting yourself”? I report you the following which is my SteppingAction.cc file. I implemented the part to compute the energy deposited by secondary particles and then i would implement also a command which count at every step the amount of particles.
It’s my first time with Geant4 so it is not required to write a “beautiful code”, a rudimental successfull way is also fine. Thanks for your time.

//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at The Geant4 Software License | geant4.web.cern.ch . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
/// \file SteppingAction.cc
/// \brief Implementation of the SteppingAction class
//
//
// $Id: SteppingAction.cc 98241 2016-07-04 16:56:59Z gcosmo $
//
//
//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…
//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

#include “SteppingAction.hh”
#include “RunAction.hh”
#include “DetectorConstruction.hh”
#include “EventAction.hh”

#include “G4Step.hh”
#include “G4UnitsTable.hh”
#include <G4SystemOfUnits.hh>

G4double esecdep;

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

SteppingAction::SteppingAction(DetectorConstruction* det,
EventAction* evt)
: G4UserSteppingAction(),
fDetector(det), fEventAction(evt)
{ }

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

SteppingAction::~SteppingAction()
{ }

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

void SteppingAction::UserSteppingAction(const G4Step* aStep)
{
// get volume of the current step
G4VPhysicalVolume* volume
= aStep->GetPreStepPoint()->GetTouchableHandle()->GetVolume();

// collect energy and track length step by step
G4double edep = aStep->GetTotalEnergyDeposit();

G4double stepl = 0.;
if (aStep->GetTrack()->GetDefinition()->GetPDGCharge() != 0.)
stepl = aStep->GetStepLength();

if(aStep->GetTrack()->GetParentID()!=0) {
esecdep+=aStep->GetTotalEnergyDeposit();
G4cout<<"Total Energy of Secondary Particles: "<<G4BestUnit(esecdep,“Energy”)<<std::endl;
}

if (volume == fDetector->GetAbsorber()) fEventAction->AddAbs(edep,stepl);
if (volume == fDetector->GetGap()) fEventAction->AddGap(edep,stepl);

}

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

So that’s pretty easy. In addition to the energy sum you’re accumulating, add a data member “G4int nsec” to just count the secondaries, and increment it similarly. You will only want to increment nsec based on the number of secondaries produced at that step. See the G4Step.hh file to get that number.