Energy spectrum using G4SDParticleWithEnergyFilter

Hi everyone,
I am trying to record the energy spectrum of any radiation using G4SDParticleWithEnergyFilter. I write the following code in the detectorConstruction::constructSDandFields() metod:

G4PSPassageCellFlux3D * scorer0 =
new G4PSPassageCellFlux3D(psName=“flux”, fNx,fNy,fNz);

for(i=1;i<numberOfBins;i++) {
G4SDParticleWithEnergyFilter* pEFilter=
new G4SDParticleWithEnergyFilter(“pEFilter”);
pEFilter->add(“proton”);
Emin=i1MeV;
Emax=(i+1)1MeV;
pEFilter->SetKineticEnergy(Emin,Emax);
scorer0->SetFilter(pEFilter);
}

However, I have no idea how to extract the data recorded by this set of energy filters in the Run and RunAction classes.
I would appreciate any suggestion on how to get the counts as a function of energy, and if the method I have chosen for recording energy spectrum is correct?
Thanks

I worked more with it and figured out that I needed to do the following in RunAction:

void RE02RunAction::EndOfRunAction(const G4Run* aRun)
{
if(!IsMaster()) return;

//- RE02Run object.
RE02Run* theRun = (RE02Run*)aRun;

G4int NofHitsMap= theRun->GetNumberOfHitsMap();

std::ofstream file("energy spectrum.txt");

for (G4int im=0;im<NofHitsMap;im++){
G4THitsMap* RunMap= theRun->GetHitsMap(im);
if (RunMap) {
G4double* count= (*RunMap)[0];
if (!count) count=new G4double(0.0);
file<< im << “\t” << *count << G4endl;
}
}
file.close();
}

Thanks

Hello…
I ama trying to collect x-ray spectrum from geant4 code.
I don’t understand which method can I use.
Thanks