How to read TDirectoryFile in a .root file?

KEY: TDirectoryFile service record;1 service record
KEY: PixLib::RootDBInquire rootRecord;1
KEY: TDirectoryFile read-back GR;1 read-back GR

Do you just want to manipulate a TDirectoryFile?

A TDirectoryFile is a derived class of TDirectory. So you could read it by the following:

// get pointer to TDirectoryFile, then print the contents
TDirectory* dir = gFile->GetDirectory(name_of_dirfile);
dir->ls();
1 Like

[quote=“jrtomps”]Do you just want to manipulate a TDirectoryFile?

A TDirectoryFile is a derived class of TDirectory. So you could read it by the following:

// get pointer to TDirectoryFile, then print the contents TDirectory* dir = gFile->GetDirectory(name_of_dirfile); dir->ls(); [/quote]

I want to read the file, then change its format and save as other data format such as txt…

I am not sure that there is anything that is perfectly suited to this. You can certainly access the contents of the file and manipulate them. Changing its format into a text file is a little trickier. I am unaware of any way to change the format of a root file. On the hand, you can write whatever content you want to a different file using C++ fstreams. What is in this file? Are there TGraphs or TH1s or other things? There may be a simple way to do this if you don’t care much about formatting and it is an object with a Print() method. If it has some sort of print method you could redirect its output to a file. There is a simple command to redirect the output to a file that is a analogous to the bash “>” command. See below for an example if it were a TGraph.

TDirectory* dir = gFile->GetDirectory(name_of_dir);

TGraph* gr;
dir->GetObject(name_of_graph, gr);

// Print contents and redirect to a file name output_file
// note that there is a semicolon before the >
gr->Print(); > output_file