Counts for energy bins

hello everyone,
i want to get energy spectrum of gamma rays, the following code makes me to extract the energy of each particle.
my question is how to get number of counts for each energy bin.
if (volume != ScoringVolume ) return;
G4double Energy = step->GetPostStepPoint()->GetKineticEnergy();;
G4cout<<particleName<<" "<<Energy*1000<<G4endl;

Hi, I am interested to this topic, did you get a spectrum count/Energy for a gamma rays source? I would like to get the energy output spectrum for a gamma source Inside a spherical detector. Have you any simple example that worked for you?
Thanks in advance

Hi, I am also interested about this topic. Did any of you find the answer to your questions? I am trying also to get number of counts for each energy bin but I do not know how to implement it to my code.
Thank you very much!

Hi, In steppingAction.cc I am filling a file “.csv” with the cinetic energy of gamma crossing my detector

void SteppingAction::UserSteppingAction(const G4Step* step)
{
    if (volume_ == fScoringVolume)
    {
         const G4Track* track = step->GetTrack();
    	 auto ecin = track->GetKineticEnergy();
         if (PrimaryName == "gamma")
         {
             #ifdef G4MULTITHREADED
             static G4Mutex stuffMutex = G4MUTEX_INITIALIZER;
             G4AutoLock al(&stuffMutex);
             #endif
             static std::ofstream stuff_GAMMA("stuff_GAMMA.csv");
             stuff_GAMMA << ecin << std::endl;
        }
    }
}

Then on Matlab I use an histogram function to have count rate vs energy…

I am sur there are better ways but it works for me, experts do you have any faster idea?

You can also directly in G4 use an hystogram but with that method you can change as you whant your energy discretisation using matlab