Look for a TTree in a file

i’ve created a ROOT file with some TTree file in it, and i need to retrieve the same TTree later, using this command

Sometime, however, the TTree is not created due to some conditions present in the code. In this case, when the last command is executed, the program exits with a segmentation fault.
Is there a way to ask the file if a given TTree is present inside the file?

Thanks in advance,

The problem is the assignment.
If the “t” is not in the file, the pointer returned is null

root [12] TObject *ob=f->Get("t");
root [13] ob
(class TObject*)0x0
root [14] TTree *tr=(Tree *)f->Get("t");
Error: class,struct,union or type Tree not defined  (tmpfile):1:
Error: tr already declared as different type. Switch to new type
*** Interpreter error recovered ***

Try checking for null first, then do the assignment.

      TTree *tr=(Tree *)f->Get("t");



The best is to use the type checked retrieval method:

TTree *t; f->GetObject("t',t); if (t==0) printf("error either the TTree does not exist or t is NOT a TTree on the file\n");


Thanks Philippe and dlayton,
your replies were both useful to me, and i’ve succeeded in running the code.