How to get the energy deposit in Xray simulation

Hi geant4 community,
I made a simple model to simulate xray production in xray tube. The xray tube would emit photons and I wanna use a detector to recond their enegy. My code is:

if (volume != fScoringVolume) 
  if (step->GetTrack()->GetDefinition() != G4Gamma::GammaDefinition()){
        return; //only collect energy deposition from gamma

      // collect energy deposited in this step
      G4double edepStep = step->GetTotalEnergyDeposit();
     fEventAction->AddEdep(edepStep);//sum energy of each events

The results as shown below it only get repeated deposited enegy instead of a enegy spectrum.

But if I get the deposited energy through codes below. It get different results as next picture

G4Track *track=step->GetTrack();
 G4double edepStep=step->GetPreStepPoint()->GetTotalEnergy();

My question is which method is correct and what are differences between them?Why the first method get repeated deposit energy?
Best regards

1 Like