23.8. Creating an Energy Spectrum
Table of Contents
From Energy Deposition to an Energy Spectrum
In the gamma ray detector example the goal of creating an energy spectrum is to turn many individual energy depositions in the scintillator into a histogram that shows how many events fall into each energy interval. This is the same type of plot you see from a real gamma spectrometer, with photopeaks, Compton continua, and possibly escape peaks.
In Geant4 this means combining three pieces that you already prepared in earlier chapters of the example: the detector geometry, the sensitive detector that records energy deposition in the crystal, and the analysis system that can store data. Here we focus only on how to go from “energy deposited in one event” to “an energy spectrum histogram on disk.”
What the Energy Spectrum Represents
An energy spectrum is a distribution of event counts as a function of detected energy. Each event corresponds to one primary gamma that you shoot at the detector. For each event the detector records some total deposited energy in the scintillator, which may be less than the initial gamma energy if not all of the energy is absorbed.
To construct the spectrum you assign each event’s total detected energy to a bin in a 1D histogram. The binning determines your energy resolution in the plot, not the detector resolution itself. For a monoenergetic gamma source, an ideal detector without resolution effects would produce a very narrow peak at the gamma energy, plus features from partial energy deposition processes.
In an energy spectrum each event contributes once with its total deposited energy, not once per step. Summing per step without resetting per event will distort the spectrum.
Accumulating Energy per Event
Inside your sensitive detector you already use G4Step information to get the energy deposited in the scintillator at each step. For a spectrum you must accumulate this step energy over the whole event, then fill the histogram exactly once at the end of the event.
A common pattern is to keep a per event accumulator in the event action. The sensitive detector adds to this accumulator every time there is a nonzero step energy deposit. At the end of the event, the event action reads the accumulator, fills the histogram, and resets the accumulator to zero.
Conceptually, the flow for one event looks like this:
- At the beginning of the event, set
eventEdep = 0. - For each step in the scintillator, add
stepEdeptoeventEdep. - At the end of the event, fill the histogram with
eventEdepand then reset it.
You do not need to store every individual step for an energy spectrum, only the total energy deposit per event.
Always reset the per event energy accumulator at the beginning of each event. Forgetting this leads to cumulative energy across events and a completely wrong spectrum.
Choosing Histogram Binning
To create a realistic and useful spectrum you must choose the histogram range and number of bins. These choices depend on the gamma energy of your source and the level of detail you want to see.
Suppose your gamma source has energy $E_\gamma = 662\ \text{keV}$, like a typical Cs-137 source. A suitable histogram range might be from 0 to somewhat above the gamma energy, for example 0 to 800 keV. You then choose a number of bins $N_\text{bins}$ that gives an energy bin width $\Delta E$:
$$
\Delta E = \frac{E_\text{max} - E_\text{min}}{N_\text{bins}}.
$$
If you select 800 bins between 0 and 800 keV, you get $\Delta E = 1\ \text{keV}$ per bin. For a teaching example this is a reasonable choice.
A simple planning table for binning looks like this:
| Parameter | Example value |
|---|---|
| $E_\text{min}$ | 0 keV |
| $E_\text{max}$ | 800 keV |
| $N_\text{bins}$ | 800 |
| $\Delta E$ | 1 keV |
Pick $E_\text{max}$ so that all realistic deposited energies fall inside the histogram range. Events outside the range will not be counted and can hide important information.
Creating the Energy Histogram with G4AnalysisManager
To store the spectrum you use G4AnalysisManager and create a 1D histogram. You typically do this in your run action, so that the histogram exists for the whole run.
You specify the histogram id, name, number of bins, and low and high energy limits. Remember that you must express the energy limits in Geant4 units, for example using keV or MeV.
The histogram definition corresponds directly to your binning plan: the number of bins and the minimum and maximum energy are the same parameters you chose when designing the spectrum.
Filling the Spectrum at End of Event
Once the histogram exists, you must fill it once per event with the total energy deposit in the scintillator. This is done in the event action, not in the stepping action. The sequence is:
- At the beginning of each event, set
edepto zero. - Each time the sensitive detector records a hit with some
stepEdep, add it toedep. - At the end of the event, call the analysis manager’s
FillH1with the histogram id andedep.
The key point is that FillH1 is called once per event, with the cumulative energy for that event. Events where the gamma does not interact at all in the detector will have edep = 0 and will contribute to the first bin if you include zero in the histogram range.
Do not fill the energy spectrum histogram in the stepping action. Filling per step creates a distribution of step energies, not a detector energy spectrum per event.
Inspecting and Saving the Spectrum
At the end of the run, the analysis manager writes all histograms to an output file. This file contains the binned counts as a function of energy and can be read by ROOT or another supported backend.
After running the simulation with a monoenergetic gamma source and your scintillator detector, you should see:
- A main photopeak at the gamma energy, if full absorption is frequent.
- A Compton continuum below the peak, if partial energy depositions occur.
- Possibly a backscatter peak and escape features, depending on your geometry and physics.
The shape you see is an idealized spectrum. In a later chapter on detector resolution and smearing you will broaden this ideal spectrum with Gaussian smearing to represent the finite resolution of a real detector.
At this point, your gamma ray detector example has a working, ideal energy spectrum. You can already perform basic tasks such as checking whether the peak appears at the correct energy, comparing shapes for different geometries, and verifying that the total number of events and counts behave as expected.
Views: 9
KAHIBARO