Adding up the Total Energy of each step

#include “detector.hh”
#include “G4RunManager.hh”
#include “G4HCofThisEvent.hh”
#include “G4OpticalPhoton.hh”

MySensitiveDetector::MySensitiveDetector(G4String name) :
G4VSensitiveDetector(name)
{}

MySensitiveDetector::~MySensitiveDetector()
{}

G4double final_energy = 0.0;

G4bool MySensitiveDetector::ProcessHits(G4Step aStep, G4TouchableHistory ROhist)
{
G4Track
aTrack = aStep->GetTrack();
G4ParticleDefinition
particleDef = aTrack->GetDefinition();
G4double energyDeposit = aStep->GetTotalEnergyDeposit();

// Check if the particle is an optical photon
if (particleDef == G4OpticalPhoton::OpticalPhotonDefinition()) {
    G4double photonEnergy = aTrack->GetTotalEnergy();
    final_energy += photonEnergy;
    G4cout << "Optical photon produced with energy: " << final_energy << " MeV" << G4endl;
    G4CsvAnalysisManager *man = G4CsvAnalysisManager::Instance();
    G4int evt = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
    man->FillNtupleIColumn(0, evt);
    man->FillNtupleDColumn(1, final_energy); 
    man->AddNtupleRow(0);     
}

return true;

}

Here the final_energy sums up every step one at a time and gives every single one of them as output
How can i make it only print the last step so that i can get the total energy of all optical photons

In your code, is the stepwise energy coming out to be greater than the incident energy ?