How to get number of detector hits per event

Hi,

If I want to get the number of photon hits from my detector per event (so that I can create a histogram of the number of hits per event in root), what can I add to what’s currently in my code?

I’ve tried to implement a count that updates every time I get a new position vector, but I’ve struggle to actually do that since I’m unfamiliar with C++ and Geant4. Does anyone have a way of implementing a count? Many thanks in advance

I have done this in what I believe to be the incorrect way (without a sensitive detector or root) but it can work (no promise on computing efficency)

You can have a variable in run manager that you increment from your sensitive detector (I have done it in stepping manager, i.e. if photon is in detector body, runmanager->increment(), then kill the photon)

Then in event manager you can run a function runmanager->clearcounts() that sets your count to 0.

Youll have to at the end of the event action do something with this count, send it to a histogram or have a vector you can add your count to. Often times I’ll have a vector with my counts per event, save it to a txt file at the end of my simulation, then use Python to plot, analyze the data.

Thanks for your reply! How exactly would I specify that I want the variable to increment when “the photon is in the detector body”? I’m pretty new to C++ so I’m not 100% sure what to write to make that happen.

The way I have done it is have a function in stepping action that checks if the postStep physical body is equal to the detector
if (postStep->GetPhysicalVolume()->GetName() == detPV)
{
runmanager()->increment();
}

you may also be able to do this in the ProcessHits function of your sensitive detector, if it works as it sounds.
Simply call
runmanager()->increment(); in the ProcessHits function.

Youll have to make sure that your increment function is included in your runmanager header file, if you are unfamiliar with how c++ manages multiple files

https://www.learncpp.com/cpp-tutorial/header-files/

Many thanks to @loydms for the suggestion, although I haven’t been able to successfully use it (:confused:), I was wondering if anyone else had any ideas about how to get total number of hits per event, preferably with a counting variable that I can add to every time I get a new position vector.

I’ve tried looking at some examples (TestEm5 , Hadr01) but I haven’t actually been able to work out how those examples implemented a counter (my C++ illiteracy is likely to blame). If it helps to get a better idea of what I’m actually working with, these are the files I’ve created for my simulation thus far: