Looping over dir inside a root file

Hello Rooters,

I have a root file with many directories with names DF_XXXXX, as shown in the attached image. They all have parts of the same tree. I am not sure why this tree was split like this, but now I have this file, and I need to loop over all the folders to extract information from the tree. Is there a way I can do that without painstakingly hardcoding the name of each folder?

Thank you for your time!

ROOT Version:6.24/02
Compiler: gcc 11.1.0


Hi @Aman_Upadhyay ,
given a TFile you can loop over its entries with

for (auto k : *file.GetListOfKeys())


1 Like

Thanks a lot!


I tried to and it gave me the address as TObjects. When using my code to Get TTrees

TFile T("AnalysisResults_trees.root");
	for (auto k : *T.GetListOfKeys()){
	TTree *t1 = (TTree*)k->Get("My_tree");

It returns an error:

/home/train/configure.C:85:25: error: no member named 'Get' in 'TObject'
        TTree *t1 = (TTree*)k->Get("My_tree");

Yes k in that snippet is a TObject*, because GetListOfKeys returns a TList pointer and iterating over a TList yields TObject*. You can use k->GetName() to get the key value (the names of the objects in the file).

If you just want to extract "My_tree" from the file:

TTree* tree = T.Get<TTree>("My_tree");
assert(tree != nullptr);