How to read file .root in Geant4

I am trying to read a corsika root file in Geant4, but I have not been successful yet. This is possible?

I think you must use external ROOT libraries

Hi, thanks for your response. What you are trying to tell me, is that I should use the g4root.hh library? Or another one? I imagine that for this I must have rooted root, right?

The g4root.hh header file is used to fill ROOT histograms in a Geant4 simulation, which can then be viewed and analysed outside Geant4, using ROOT. This can be done in a terminal:

> root -l filename.root

See what histograms are available with .ls and view histograms with:


where h1 is the name of a 1D histogram.

Having used Geant4 on and off for a few years, nothing I have seen indicates that viewing ROOT files from within Geant4 is possible, although I would be happy to be corrected if someone knows otherwise.

@emma is correct: the Geant4 toolkit (libraires) does not itself link against ROOT. The g4root utilities in g4analysis provide way to write out a binary-compatible file which can be read by ROOT, without making use of any ROOT library classes.

If you would like to write your application to read in a ROOT file, you will need to write that code yourself, and modify your application-level Makefile (or CMakeLists.txt) to link against the ROOT installation on your system.

Hi @emma and @mkelsey, from what I understand it is very complicated to read a .root file in Geant4, right? But is it possible to read text files?
I ask this, since they made the comment that the .root files can be converted to text files.

Thank you very much for your answers!

Hi @PedroValenciaMex. What exactly is it you are trying to do with this ROOT or text file?

Text files (actually *.mac files) can be read into Geant4 simulations to input initial radiation energies (and other information); I’m not aware of any other situations in which plain text files are used in Geant4. Again, I’m happy to be corrected if I’m wrong.

ROOT files can indeed be converted to text files; I’ve used this to convert a 1D histogram to text in the past.

It’s not complicated to read ROOT files in Geant4, but does require use of the appropriate ROOT types in the application code, together with linking the ROOT libraries for those types to the application.

As noted by @Emma, if you can tell us where in Geant4 you need to read the file from (I’m guessing in one of the User Action classes?), and what sort of ROOT objects it provides, we can provide clearer advice.

Hi, @emma, @bmorgan, an apology for my late reply, I appreciate the inconvenience you have taken to respond to this post.
What I want to do and what I have been breaking my head in recent weeks is: that I can live a simulation of an atmospheric shower made in CORSIKA together with a detector that I am doing in GEANT4.
That is, that the initial particles are obtained from a CORSIKA .root file, and that my GEANT detector reads that file.
I thank you again.