Problem into extracting information from ROOT files with pyROOT

Dear all ,

I’m a really beginner with ROOT and I recently started to use it.

I started to work with pyROOT, since I need to use some specific Python libraries for post processing the results in which I’m working on.

Unfortunately, I got some problems to extract the information from the root files. I would like to ask, please, some suggestions taking as the reference the .root files from the B4a example. I report the python script that I used to manage the file generated, B4.root (that I am not allowed to attach here due to its format):


from ROOT import *

fileName="./build/B4.root"
myFile = ROOT.TFile.Open(fileName, "READ")

tree=myFile.Get("B4")
leaf = tree.GetBranch("Eabs").GetLeaf("Eabs")
myFile.ls(), tree.Scan(),  leaf.GetValue(), tree.Print()

In Jupyter Notebook, I get printed this information:

(20, 22.873348309946593)

TFile**		B4aTestRoot/build/B4.root	
 TFile*		std_examples/B4aTestRoot/build/B4.root	
  KEY: TTree	B4;1	Edep and TrackL
  KEY: TH1D	Eabs;1	Edep in absorber
  KEY: TH1D	Egap;1	Edep in gap
  KEY: TH1D	Labs;1	trackL in absorber
  KEY: TH1D	Lgap;1	trackL in gap

************************************************************
*    Row   * Eabs.Eabs * Egap.Egap * Labs.Labs * Lgap.Lgap *
************************************************************
*        0 * 43.042040 *         0 * 29.575223 *         0 *
*        1 * 27.340958 *         0 * 19.127214 *         0 *
*        2 * 45.473479 *         0 * 30.956585 *         0 *
*        3 * 4.7084119 *         0 * 1.4942436 *         0 *
*        4 * 3.6049559 *         0 * 0.7217933 *         0 *
*        5 * 3.4328333 *         0 * 0.5616289 *         0 *
*        6 * 51.597768 * 0.2504165 * 39.337433 * 0.6461602 *
*        7 * 3.3216662 *         0 * 0.4482391 *         0 *
*        8 * 44.348139 * 1.4075870 * 33.634048 * 7.6039427 *
*        9 * 3.2214064 *         0 * 0.3639539 *         0 *
*       10 * 3.1431678 *         0 * 0.3159866 *         0 *
*       11 * 29.325894 *         0 * 20.652747 *         0 *
*       12 * 48.283826 *         0 * 36.003877 *         0 *
*       13 * 31.511904 * 1.4158138 * 23.978050 * 8.1664451 *
*       14 * 32.828006 *         0 * 23.612736 *         0 *
*       15 * 10.381831 *         0 * 6.1633032 *         0 *
*       16 * 3.2690086 *         0 * 0.3967726 *         0 *
*       17 * 3.1745242 *         0 * 0.3311820 *         0 *
*       18 * 47.452206 * 7.2540443 * 33.439702 * 39.732736 *
*       19 * 22.873348 *         0 * 15.977893 *         0 *
************************************************************

******************************************************************************
*Tree    :B4        : Edep and TrackL                                        *
*Entries :       20 : Total =            4621 bytes  File  Size =       2805 *
*        :          : Tree compression factor =   1.00                       *
******************************************************************************
*Br    0 :Eabs      : Double_t B4                                            *
*Entries :       20 : Total  Size=       1053 bytes  File Size  =        548 *
*Baskets :        4 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    1 :Egap      : Double_t B4                                            *
*Entries :       20 : Total  Size=       1053 bytes  File Size  =        548 *
*Baskets :        4 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    2 :Labs      : Double_t B4                                            *
*Entries :       20 : Total  Size=       1053 bytes  File Size  =        548 *
*Baskets :        4 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    3 :Lgap      : Double_t B4                                            *
*Entries :       20 : Total  Size=       1053 bytes  File Size  =        548 *
*Baskets :        4 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*

where I am not able to extract the values of each column but only the one at the last row (22.873348309946593).
I tried to get the single values in the following way:

leaf.GetValue(1), leaf.GetValue(2), leaf.GetValue(3), leaf.GetValue(20)

by obtaining:

(0.0, 5.73e-322, 2.4e-322, 5.667e-321)

I installed the last version of ROOT and I updated jupyter and Python in the Anaconda environment, even though I don’t exclude errors due to bugs.

Do you have any suggestion, please?

Thank you very much for your time.
Best regards,

Christian

Hi Christian,

it seems more of a pyROOT problem than a Geant4 problem :slight_smile:
Anyhow, I created a B4.root file from the B4a example (using Geant4.10.7.p03).
The B4.root file has a tree called B4, as we can see:

$ root -l B4.root 
root [0] 
Attaching file B4.root as _file0...
(TFile *) 0x133039bf0
root [1] B4->Scan()
************************************************************
*    Row   * Eabs.Eabs * Egap.Egap * Labs.Labs * Lgap.Lgap *
************************************************************
*        0 * 3.1430317 *         0 * 0.3156294 *         0 *
*        1 * 29.325894 *         0 * 20.652535 *         0 *
*        2 * 43.042040 *         0 * 29.574990 *         0 *
*        3 * 4.7064291 *         0 * 1.4924497 *         0 *
*        4 * 48.283826 *         0 * 36.003647 *         0 *
*        5 * 31.511932 * 1.4157858 * 23.977831 * 8.1663040 *
*        6 * 3.2211543 *         0 * 0.3635041 *         0 *
*        7 * 3.6043244 *         0 * 0.7210179 *         0 *
*        8 * 3.4323383 *         0 * 0.5609711 *         0 *
*        9 * 32.828006 *         0 * 23.612691 *         0 *
*       10 * 10.381598 *         0 * 6.1628690 *         0 *
*       11 * 27.340908 *         0 * 19.126947 *         0 *
*       12 * 3.2687263 *         0 * 0.3963018 *         0 *
*       13 * 3.1743573 *         0 * 0.3308129 *         0 *
*       14 * 51.597768 * 0.2504165 * 39.337183 * 0.6461602 *
*       15 * 3.3213269 *         0 * 0.4477258 *         0 *
*       16 * 45.473078 *         0 * 30.956096 *         0 *
*       17 * 47.450724 * 7.2555270 * 33.437378 * 39.741463 *
*       18 * 22.873231 *         0 * 15.977561 *         0 *
*       19 * 44.348139 * 1.4075873 * 33.633828 * 7.6039458 *
************************************************************
(long long) 20

This simple pyROOT script reads the file B4.root and dumps the value of Eabs for each event/entry.

import ROOT as root

file = root.TFile("B4.root")
myTree = file.Get("B4")
for entry in myTree:
     print(entry.Eabs)

Example:

$ python3 ReadB4.py 
3.143031740918895
29.32589465977339
43.04204057050752
4.706429105412588
48.28382616027215
31.511932050149184
3.221154317499973
3.604324480252908
3.432338392694154
32.82800645049663
10.381598555407171
27.34090799536754
3.2687263239040467
3.174357324374154
51.59776871745688
3.3213269303611423
45.47307832209695
47.45072396692001
22.873231903794025
44.34813956015004

Let me know if this fixes your problem. Lorenzo

1 Like

Dear Lorenzo,

thank you very much. It works!

Christian

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