# How to read file .root in Geant4

**URL:** <https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895>\
**Category:** Ideas and Requirements\
**Created:** [September 12, 2019, 2:40am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895 "2019-09-12T02:40:42Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![PedroValenciaMex](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/pedrovalenciamex/32/361_2.png) [@PedroValenciaMex](https://geant4-forum.web.cern.ch/u/PedroValenciaMex)\
**Post date:** [September 12, 2019, 2:40am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/1 "2019-09-12T02:40:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ilyhamamonov](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/ilyhamamonov/32/370_2.png) [@ilyhamamonov](https://geant4-forum.web.cern.ch/u/ilyhamamonov)\
**Post date:** [September 12, 2019, 10:06am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/2 "2019-09-12T10:06:46Z")

</div>

I think you must use external ROOT libraries

---

<div class="post-metadata">

**Author:** ![PedroValenciaMex](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/pedrovalenciamex/32/361_2.png) [@PedroValenciaMex](https://geant4-forum.web.cern.ch/u/PedroValenciaMex)\
**Post date:** [September 13, 2019, 6:22am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/3 "2019-09-13T06:22:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![emma](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@emma](https://geant4-forum.web.cern.ch/u/emma)\
**Post date:** [September 13, 2019, 1:48pm UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/4 "2019-09-13T13:48:57Z")

</div>

The g4root.hh header file is used to fill [ROOT](https://root.cern.ch/) 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:

```
h1->Draw("HIST")

```

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.

---

<div class="post-metadata">

**Author:** ![mkelsey](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/mkelsey/32/49_2.png) [@mkelsey](https://geant4-forum.web.cern.ch/u/mkelsey)\
**Post date:** [September 15, 2019, 7:46pm UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/5 "2019-09-15T19:46:38Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![PedroValenciaMex](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/pedrovalenciamex/32/361_2.png) [@PedroValenciaMex](https://geant4-forum.web.cern.ch/u/PedroValenciaMex)\
**Post date:** [September 19, 2019, 11:57pm UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/6 "2019-09-19T23:57:03Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![emma](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@emma](https://geant4-forum.web.cern.ch/u/emma)\
**Post date:** [September 20, 2019, 7:36am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/7 "2019-09-20T07:36:01Z")

</div>

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](http://people.physics.tamu.edu/fenkerbb/convertTH1.cpp) to convert a 1D histogram to text in the past.

---

<div class="post-metadata">

**Author:** ![bmorgan](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/bmorgan/32/7_2.png) [@bmorgan](https://geant4-forum.web.cern.ch/u/bmorgan)\
**Post date:** [September 20, 2019, 10:36am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/8 "2019-09-20T10:36:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PedroValenciaMex](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/pedrovalenciamex/32/361_2.png) [@PedroValenciaMex](https://geant4-forum.web.cern.ch/u/PedroValenciaMex)\
**Post date:** [September 27, 2019, 1:31am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/9 "2019-09-27T01:31:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aharel](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/c2a13f/32.png) [@aharel](https://geant4-forum.web.cern.ch/u/aharel)\
**Post date:** [March 15, 2020, 8:08pm UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/10 "2020-03-15T20:08:18Z")

</div>

Posting here, as this came up when I was looking for answers on how to use ROOT libraries from my GEANT code.

Caveat emptor: I am not an expert on any of this stuff, and I expect there’s a nice simple way to get this to work. Hopefully someone will post it, or maybe even document it.

In principle it’s easy, that’s what make variables (a.k.a. macros) are for, specifically the LDLIBS variable. And how do you know what to put there? Also easy, since that’s what root-config is for. So ideally, you’d put into your makefile, just before the binmake.gmk line something like

`LDLIBS += `root-config --libs`

or if you prefer

`LDLIBS += $(shell root-config --libs)`

and it should work.  
If you really want to be organized, you can also put

`LDFLAGS = `root-config --ldflags`

or so.

But no.  
I have no idea why the LDFLAGS line (in my case the only flag is -m64) confused GEANT’s make, but everything works without it, so I let that go.  
A bigger problem is that when binmake.gmk sees that the user set up the LDLIBS, it omits some of the libraries that is normally adds to LDLIBS. And those are needed, so the link will fail.  
Things almost work if you put the libraries from root-config into LDFLAGS, except that now your library will appear **after** the ROOT libraries, which means that the linker will **not** use the ROOT libraries for your library, So you can use ROOT in your main file, but not elsewhere.

Eventually, I just hacked binmake.gmk to accept a variable to be placed **after** its LDLIBS.  
So in lines 418 and 423 of binmake.gmk I now have:  
` $(LDLIBS) $(AMNONS_2020_HACK)`  
and in my makefile  
` AMNONS_2020_HACK = $(shell root-config --libs)`

Yet another caveat is that some ROOT headers clash with CLHEP headers. Specifically, TString (which is used everywhere in ROOT) uses “s”, which CLHEP uses for seconds. So I try to (a) avoid ROOT headers in my GEANT headers, (b) in the .cc files, load the ROOT files like this:  
#pragma GCC diagnostic push  
#pragma GCC diagnostic ignored “-Wshadow”  
#include “TMath.h”  
#include “TFile.h”  
// and any other ROOT headers you need  
#pragma GCC diagnostic pop

cheers,  
Amnon

---

<div class="post-metadata">

**Author:** ![ahmer](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@ahmer](https://geant4-forum.web.cern.ch/u/ahmer)\
**Post date:** [June 30, 2020, 4:47am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/11 "2020-06-30T04:47:01Z")

</div>

![Screenshot from 2020-06-30 09-43-11](https://geant4-forum.web.cern.ch/uploads/default/original/2X/c/c4d802c4128ea3421d0abe98a17b9159fddfd643.png) .  
this is the error which came when i used these commands for reading the root file in which my graphs are stored. can you please explain what’s the issue and how can i resolve it?

---

<div class="post-metadata">

**Author:** ![anna](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anna](https://geant4-forum.web.cern.ch/u/anna)\
**Post date:** [June 30, 2020, 8:07am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/12 "2020-06-30T08:07:33Z")

</div>

You should enter into your terminal

```auto
root acosta.root

```

instead of

```auto
root

```

This will execute ROOT and open the file `acosta.root`.

Or, instead, after executing ROOT with

```auto
root

```

you may open the file typing into ROOT prompt:

```auto
TFile file("acosta.root")

```

Please consult [ROOT documentation](https://root.cern.ch/input-and-output#Accessing-a-ROOT-File).

---

<div class="post-metadata">

**Author:** ![ahmer](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@ahmer](https://geant4-forum.web.cern.ch/u/ahmer)\
**Post date:** [July 7, 2020, 2:11am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/13 "2020-07-07T02:11:07Z")

</div>

very helpful thanks dear for your time.

---

<div class="post-metadata">

**Author:** ![ahmer](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@ahmer](https://geant4-forum.web.cern.ch/u/ahmer)\
**Post date:** [July 7, 2020, 2:13am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/14 "2020-07-07T02:13:34Z")

</div>

can you help me more sorry i’m not that fluent in this work you can say new. 🙂  
i wanna ask now how to normalize the two graphs or histograms which i already have with in the acosta.root file?

---

<div class="post-metadata">

**Author:** ![bmorgan](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/bmorgan/32/7_2.png) [@bmorgan](https://geant4-forum.web.cern.ch/u/bmorgan)\
**Post date:** [July 9, 2020, 10:57am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/15 "2020-07-09T10:57:43Z")

</div>

It’s best to consult the [ROOT Documentation](https://root.cern/manual/), or ask a question on [their forum](https://root-forum.cern.ch) if you can’t find the answer there.

---

<div class="post-metadata">

**Author:** ![ahmer](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@ahmer](https://geant4-forum.web.cern.ch/u/ahmer)\
**Post date:** [July 12, 2020, 4:47am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/16 "2020-07-12T04:47:26Z")

</div>

i have the documentation dear but issue is. i didn’t understand how to apply to my histograms to normalize them.

---

<div class="post-metadata">

**Author:** ![mkelsey](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/mkelsey/32/49_2.png) [@mkelsey](https://geant4-forum.web.cern.ch/u/mkelsey)\
**Post date:** [July 12, 2020, 5:06am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/17 "2020-07-12T05:06:22Z")

</div>

As @bmorgan said, you’re asking questions about how to use ROOT. If you can’t find “histogram normalization” in the documentation, you’re much more likely to get useful answers in the [ROOT forums](https://root-forum.cern.ch/).

---

<div class="post-metadata">

**Author:** ![Moxuan](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/moxuan/32/3475_2.png) [@Moxuan](https://geant4-forum.web.cern.ch/u/Moxuan)\
**Post date:** [October 13, 2021, 7:34am UTC](https://geant4-forum.web.cern.ch/t/how-to-read-file-root-in-geant4/895/18 "2021-10-13T07:34:01Z")

</div>

Hi, I am trying to use CORSIKA to get the cosmic ray muon flux. Do you have any tutorial of CORSIKA? Thank you very much!
