Scoring optical photon population of each event to get an energy spectrum

I modeled a simple scintillation problem with a photosensor attached to a NaI crystal surrounded by a PTFE layer and an aluminum casing. A 662 keV gamma source is firing at it. I am using primitive scorers and managed to count optical photons reaching the photosensor; however, the number of counts I get is the sum over all events. I need one number of counts per event, in order to generate an histogram i.e. an energy spectrum. What is the most computationally efficient way to do so? I suppose I should write the number of counts in the output csv after each event, but I don’t know how to do so.

Here is the relevant bit of my macro:

Here is the output file. It has a single line but what I need is one line per event.

image

Not sure if relevant but I am using Gears.
Thanks!

Histograms are fast, efficienct, and already integrate with multithreaded modules. The output can be csv, or root, or hdf5. Rdecay02 has some good examples here. Just count the number of photons per event and then add them to your histograms at the end of the event action.

Edit- They can also be done in the macro level