TFile* file = new TFile("data.root", "READ"); TDirectory* directory = file->GetDirectory("folder");
TFile* file = new TFile("data.root", "READ"); TIter next(file->GetListOfKeys()); TKey* key; while ((key = (TKey*)next())) { if (strcmp(key->GetClassName(), "TDirectory") == 0) { TDirectory* directory = (TDirectory*)key->ReadObj(); // Do something with the directory object directory->ls(); } }In this example, a TFile object is created that opens a ROOT file "data.root" in read mode. The GetListOfKeys method is called to retrieve a list of all objects stored in the ROOT file. The TIter object is used to loop over all objects in the list, and for each object, its class name is checked to see if it is a TDirectory. If it is, the ReadObj method is called to retrieve a pointer to the TDirectory object, which can then be used to perform operations such as listing its contents using the ls method. Package library: ROOT.