Accessing TTree in ROOT folder

Hey

I tried to use the pythonic approach to accessing the content of root file if I have a root tree inside the file by using:

file = TFile.Open("file.root")
myTree=file.myTree # this is the name of the tree in the file
for event in myTree:
   print(event.x, event.y) # x and y are names of branches of the tree

and this works fine. Now if I put a tree into the folder inside a root file I have trouble using the above syntax. I know one could use Get or GetObject and then loop event by event. But I wonder if one can do something like:

file_with_folder = TFile.Open("file.root")
file_with_folder.cd("myFolder")
... (some more code) ...
myTree=file_with_folder.myTree # this is the name of the tree in the file
for event in myTree:
   print(event.x, event.y) # x and y are names of branches of the tree

I know that in ROOT 6.14 one could use the RDataFrame to loop over all entries but unfortunately, I’m forced to use older versions of ROOT.

I figured that the difference is that if one has no folder the TTree key is available. At least that’s what file.ls() says:

TFile**		file.root	
 TFile*		file.root	
  KEY: TTree	myTree;1	this is a test tree

Then if one adds folder the file structure file_with_folder.ls() gives:

TFile**		file.root	
 TFile*		file.root	
  KEY: TDirectoryFile	data;1	data

which makes sense and if one does gDirectory.cd("data") the file_with_dir.ls() returns:

TFile**		file.root	
 TFile*		file.root	
  TDirectoryFile*		data	data
   KEY: TTree	myTree;1	this is a test tree
  KEY: TDirectoryFile	data;1	data

Key to the tree is now accessible but how can one use it? I tried the above example but I get:

    for event in file.myTree:
AttributeError: TFile object has no attribute 'myTree'

which indicates that “myTree key is not visible” by file variable.

For clarity, I added two scripts that should do the same thing. Generate a tree, store it in a root file and read it back and do some example processing. Their only difference is that one of the produced root files has a directory in it and the other one doesn’t.

Any ideas :slight_smile:

root_file_with_dir.py (1.0 KB)
root_file.py (728 Bytes)

Hi @brencic,

I believe file.myTree will only work if that tree is not in a folder, even if you move into that folder first.

Is it a problem if you just do:

t = file.Get("data/myTree")
for event in t:

Cheers,
Enric

Hi @etejedor

Yes, this works indeed. Haven’t thought about it. Thanks for the idea.

I have another question. I was trying to find the source code for the pyroot objects when looking into the above issue. I know pyroot objects are wrappers around C++ objects but to have the functionality like: file.myTree.track ... one needs to add some extra code to the pyroot module. I assume at least some setattr(name, object). I was wondering if there are any docs/source code of pyroot or how pyroot is generated. I find the C++ code often a good reference. Would be nice to know how pyroot works too :slight_smile:

Thanks for the help.

Hi @brencic,

As you deduced, syntax like file.myTree is possible thanks to what we call a pythonization, which is code that makes it possible to use ROOT C++ classes in a more pythonic way.

We are currently working on a new PyROOT that will support more modern C++, and we are migrating the pythonizations to it. Here is where we keep track of them:
https://sft.its.cern.ch/jira/browse/ROOT-9510

Cheers,
Enric

Great. Thanks for the info.

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