You are here

ROOT files

ROOT provides system-independent binary files in which the user can store objects of any class having a dictionary. Because TFile iherits from TDirectory, information stored into a ROOT file can be organized in several subfolders, and the user can navigate it as he were browsing the file system of his operative system. User data are usually compressed when written into a TFile. Actually, it is also possible to save data into databases like SQL, or even into XML files. However, this type of I/O is really an extremely rare need: practically all users save their data into ROOT files.

ROOT files can be opened for reading and/or writing, and the most common way to open a file is via the TFile constructor:

root [] TFile f0("file0.root"); // read-only
root [] TFile f1("file1.root", "NEW"); // to write; file must not already exist
root [] TFile f2("file2.root", "UPDATE"); // to write into an existing file
root [] TFile f3("file3.root", "RECREATE"); // to (over-)write

Another option is to use the Open(...) method. The file name is a string that may start with the access protocol: if the file starts with "root:", "roots:" or "rootk:" a TNetFile object will be returned; with "http:" a TWebFile; with "file:" a local TFile.

Immediately after opening a file, it becomes the current directory. If more than one file are open, one can change the active file by means of the TDirectory::cd(...) method. A common error is to forget what is the active file and save objects in the wrong one.

Event data are usually stored into a TTree that is associated to one or more ROOT files. Trees are optimized for very high volumes and they are split in branches by default when saved into ROOT files. Other objects are saved as instances of TKey and are not split by default (apart from TClonesArray; also STL vectors may be split).