Retrieving and saving particle/track information

Please fill out the following information to help in answering your question, and also see tips for posting code snippets. If you don’t provide this information it will take more time to help with your problem!

_Geant4 Version: 11.2.0
_Operating System: Almalinux 9.4
_Compiler/Version: g++ (GCC) 13.1.0
_CMake Version: cmake version 3.26.2

Dear experts I’m new in Geant4 world, and I’m developing a geometry with various crystals starting from exampleB4d.

I’m using gps for creating a beam source, and I’m interested to get particle position and energy when generated from the beam and at the beginning of each volume, but it’s not clear to me how and where retrieving this information and how storing in a TTree.

Just for completeness these are the classes I’m using (as already existing in the example)

ActionInitialization.hh
DetectorConstruction.hh
RunAction.hh
ConfigFile.hh
EventAction.hh
Materials.hh (I created this class to have materials properly ordered)
PrimaryGeneratorAction.hh

Thanks for the attention.

Best regards,
Francesco


In the step action, check if the pre-step logical volume is outside and the step is inside. Then, do whatever recording you want, like angle, energy, position.

Thanks for the reply @phirippu

So should I need to add SteppingAction class? At the moment it’s not part of the code, since I’m using global scorers for some variables that I’m saving (e.g. Energy deposit).

Best regards,
Francesco

Yes, you can check examples of how to do it. Practically, you need to add another user action in the main and add a step action class from one of the examples where it is used. I am sorry not to remember where it was used the best way.