Access tree name automatically

Hello Experts,

To access a tree I can wirte code like

         TFile *inputFile1 = new TFile(filename1.c_str());
         TTree *jetTree1 = (TTree *) inputFile1->Get("demo/tree");

How can I make it automatic such that It will automatically check the name of tree and read it through Get()?

Thank you for your help.

The interface does not allow the retrieval of an object w/o specifying its name.

Try (note: you will need to “loop” over all found keys, check the “class” for each of them, if it’s a directory you would again need to get the list of its keys, and so on …):
inputFile1->GetListOfKeys()->Print();

If you decide to proceed this way, 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].

Note: if you know (in advance) all names of all trees that you are interested in, then it’s easy:
TTree *t1;
inputFile1->GetObject(“FirstTree”, t1); // try to read the "FirstTree"
if (t1) { … first tree found … }
TTree *t2;
inputFile1->GetObject(“SecondTree”, t2); // try to read the "SecondTree"
if (t2) { … second tree found … }