Generating histograms for a run

Dear all,

I’m writing a code to get quantities such as energy deposition and particle fluxes in a sensitive detector by using G4MultiFunctionalDetector and G4VPrimitiveScorer, together with needed filters. The final results all work well after Merge, and match those from UI-command base scoring. What I’m trying to achieve now is to generate histograms of those quantities for a run, e.g., electron fluxes on the sensitive detector as function of energy. My first approach was to create primitive scorers in a loop and adding the G4SDParticleWithEnergyFilter for each energy bins in the histogram that I want to generate. Here’s a snippet of my code for electrons flux:

    auto detector0 = new G4MultiFunctionalDetector("Detector");

    G4double minEnergy = 0.0*MeV;
    G4double maxEnergy = 1000.0*MeV;
    G4double energyStp = 200.0*MeV;
    G4int noBins = (maxEnergy-minEnergy)/energyStp;

    for(G4int i=0; i<noBins; i++){
        G4String eMinusScorer = "electronFlx" + to_string(i+1);
        G4PSFlatSurfaceFlux *eMinusFlx = new G4PSFlatSurfaceFlux(eMinusScorer, fFlux_In);
        G4SDParticleWithEnergyFilter *eMinusFilter = new G4SDParticleWithEnergyFilter("eMinusFilter");
    SetSensitiveDetector("SensorLV1", detector0);

I can theoretically run it if the primary energy is relatively low, or if I drastically increase the energy step (so less histogram bins). But I see that the approach uses an enormous amount of memory, and I get a “Segmentation fault (core dumped)” in most cases.
I’m sure that there’s a smarter way of getting those histograms, but I’m a little lost on where to start. Would anyone be kind to give me a direction?

Many thanks in advance.