How to check whether directory in TFile exists

Hi ROOT experts,

I am looking for a way to check whether a directory in a TFile exists.

I see that interactively I can do:

z=_file0->cd(“analyzeHiMassTau”);

And z will become a boolean indicating whether the cd was successful or not. This doesn’t work within my script and nonetheless, I am sure that there is a better way to do check whether a directory exists.

Any suggestions would be greatly appreciated. Thanks!
Will

Hi Will,

You can use TDirectory::GetDirectory for this purpose.

Cheers,
Philippe.

Hi Philippe,

Thanks for the reply. I don’t see exactly how to use TDirectory::GetDirectory to get a boolean for whether or not the directory exists. Interactively I see that root returns the place in memory if the directory does exist:

root [2] _file0->GetDirectory(“analyzeHiMassTau”)
(class TDirectory*)0x17e616d0

And a zero pointer if it does not:

root [2] _file0->GetDirectory(“analyzeHiMassTau”)
(class TDirectory*)0x0

I can’t tell if the printError option might be useful:
root.cern.ch/root/html/TDirector … tDirectory

Thanks,
Will

Hi Will,

The following should work:if (_file0->GetDirectory("analyzeHiMassTau")) { // or if (_file0->GetDirectory("analyzeHiMassTau") != 0) z=_file0->cd("analyzeHiMassTau"); }or even more directly:TDirectory *dir = _file0->GetDirectory("analyzeHiMassTau"); if (dir) { dir->cd(); } else { fprintf(stderr,"Missing directory analyzeHiMassTau\n"); }

Cheers,
Philippe.

[quote]I can’t tell if the printError option might be useful:[/quote]Why not?

Philippe,

That works perfectly. Thanks so much for the help!

Will