Finding List of TTrees in TFile

Hello,

I’m looking to get a list of TTrees contained in a TFile which has a mixture of TTrees and histograms (TH1D). If I do a TFile::ls() on a file of interest it returns something that looks like:

KEY: TTree res_soft_up;1 4-vectors + variables required for scaling factors KEY: TH1D res_soft_up_allEventsHFOR_genWeights;1 allEventsHFOR, generator weights KEY: TH1D res_soft_up_allEventsHFOR_noWeights;1 allEventsHFOR, no weights KEY: TH1D res_soft_up_fullCutFlow_genWeights;1 fullCutFlow_genWeights

My goal is to be able to isolate the TTree elements from the TH1D elements. So far my code looks like:

TFile *file = new TFile(filename.c_str()); file->ls(); TList *list = file->GetListOfKeys(); TIterator *iter = list->MakeIterator(); TObject* obj = iter(); while(true){ string temp = obj->GetName(); cout<<"name of Key is: "<<temp<<endl; if(obj=list->Last()) break; obj = iter->Next(); }

This works well in that I get a list of all of the Keys contained within the TFile, however I can not distinguish between the TTrees and the TH1D’s. The output looks like:

name is: res_soft_up name is: res_soft_up_allEventsHFOR_genWeights name is: res_soft_up_allEventsHFOR_noWeights name is: res_soft_up_fullCutFlow_genWeights name is: res_soft_up_fullCutFlow_genXpileupWeights

I assume that there must be a way to distinguish between different types of Keys within the TFile, I just haven’t been able to figure out how. Any help would be greatly appreciated.

Thanks!

-Jeff

Hi Jeff,

this might get you started (I slightly modified your code):

TFile *file = new TFile("myfile.root");
file->ls();
TList *list = file->GetListOfKeys();
TIter iter(list->MakeIterator());
while(TObject* obj = iter()){
  TKey* theKey = (TKey*)obj;
  cout<< "name/type of Key is: "<<theKey->GetName() << " / " << theKey->GetClassName() << endl;
  theKey->Class()->Dump();
}

Cheers,
Danilo

See also [url=https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345/2 old thread about “AUX;1 AUX;2 AUX3;”[/url].