Problem into extracting information from ROOT files with pyROOT

Dear all ,

I’m a 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 of Geant4. 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

Your "Eabs" leaf seems to be an ordinary “Double_t” value (i.e., not an array), so it only makes sense to use: leaf.GetValue(0)

Note: you need to retrieve tree entries yourself, e.g.:
nb=tree.GetEntry(0); leaf.GetValue(); nb=tree.GetEntry(1); leaf.GetValue()

1 Like

Dear @Wile_E_Coyote,

Thank you very much. Here below, the script that I generated that works:

import ROOT

fileName="B4root"
myFile = ROOT.TFile.Open(fileName, "READ")
tree = myFile.Get("B4")

# to get Eabs

counter=0
for i in range(tree.GetEntries()):
    tree.GetEntry(i)
    Eabs = getattr(tree, 'Eabs')
    print (Eabs)

Best regards,

Christian

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