Maybe a mistake in example/basic/B1/RunAction.cc?

G4double rms = edep2 - edep*edep/nofEvents;

in this code, “rms” maybe a negative value when more than one step in a volume in an event,
so here “nofEvents” should modified to total steps in the application.

No, the equation is correct. ‘rms’ in this equation is really the estimate of the variance (squared root mean square) of the deposited energy over a run. The estimate of the variance of the deposited energy = mean square deposited energy minus the square of the mean deposited energy. The deposited energy is accumulated at each step, so the total number of steps is not needed in the calculation of ‘rms’.

For small amounts of energy deposited, the variance estimate may be less than zero due to statistical fluctuations. That is why, on the next code line which estimates the root mean square deposited energy, there is a test where a negative rms value is set to 0.


here is the equation of the variance, the difference between us is
what the “n” means?
first the “rms” must bigger and/or equal 0, can nto less than 0 in math.
secondly here the “n” is the number of deposited energy not the event number.

The quantity rms being calculated in the equation:

G4double rms = edep2 - edep*edep/nofEvents;

is not the root mean square deposited energy. It is an ESTIMATE of the square of the root mean square deposited energy and it can be less than zero (computational error, e.g. roundoff) if the deposited energy is near zero. That is exactly why they have the following line of code immediately after the above line:

if (rms > 0.) rms = std::sqrt(rms); else rms = 0.;

Also, the first equation is a calculation of the event by event variation over a run, so n is the number of events, not the number of steps.

As John said, edep is the total energy deposited per event, not per step.
The sommation is done in SteppingAction.cc, line 74.

yes, you are right,
i score energy deposit directly through SteppingAction not through EventAction,
so the “n” means the step number not the event number,
i try the two resolutions, the energy deposit and its relative error are almost the same.
by the way,


in the equation of the variance, “rms” must bigger and/or equal 0 in math.

That is true in math. It is not necessarily true in floating point computer arithmetic.