How can I get kinetic energy and energy deposit as root file

Hi I’m new with Geant4
and I want to get kinetic energy and energy deposit from my detector as root file as application for example B5

I checked my detector get works through it makes steps inside through ProcessHit() method.
More specifically, I used G4cout in ProcessHit() method for checking and I found that “step”, “kinetic energy” and “energy deposit” have some values in them.
So I saved it to the method SetEnergy() I made in Hit.hh before, which can take the value from GetEnergy() method (this two methods are connected through some variable)

But when I checked that values in EventAction(), they all have 0.
In here, I made pointer of Hit in eventaction and called the method GetEnergy() and tried to get some value.

auto something = new Hit();
auto energy = something → GetEnergy();

G4cout<< energy << G4endl; // I got 0

I have no idea any further.
How can I get the value as root file?

Followings are my Hit.hh

In the event action, the constructed hit is not the same as that (or rather, those, because there’s one hit per step created there) created in the ProcessHit method. As a default constructed SphereHit has zero kinetic/deposited energy, that’s why zero is seen in EndOfEventAction.

Instead, as the hits created by ProcessHits look like they’re being added to the hits collection for that SD (the line fHC->insert(hit)), retrieve that collection in EndOfEventAction. That will give the set of hits created, from which the kinetic/deposited energy can be extracted.

There’s a bit of coding to do to get that, the relevent section of the Application Developer Guide covers this in depth. The basic examples B2a/B2b show the minimal setup to add/retrieve hits, whilst basic example B4c showing a more complete example with persistency. B4d also demos the use of built in scorers which may prove useful for your use case of recording kinetic/deposited energy.

I solved it !
Thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.