#include#include #include void getKeysInFile(TString fileName) { TFile* file = new TFile(fileName, "READ"); TIter next(file->GetListOfKeys()); TKey* key; while ((key = (TKey*)next())) { std::cout << key->GetName() << std::endl; } file->Close(); }
#includeIn this example, we first create a TFile object by opening the file "output.root" in write mode. We then create two histograms and write them to the file using the Write() function. Finally, we retrieve a list of all the keys within the file using GetListOfKeys() and print out their names using GetName(). Note that in order to retrieve the list of keys, we need to call the function after we have written the histograms to the file. Package/Library: ROOT#include #include #include #include void addHistograms(TString fileName) { TFile* file = new TFile(fileName, "RECREATE"); TH1F* hist1 = new TH1F("hist1", "Histogram 1", 100, 0, 1); TH1F* hist2 = new TH1F("hist2", "Histogram 2", 100, 0, 1); file->Write(); TIter next(file->GetListOfKeys()); TKey* key; while ((key = (TKey*)next())) { std::cout << key->GetName() << std::endl; } file->Close(); }