ROOT offers the possibility to write instances of classes into a ROOT file, this is, you can make the created objects “persistent”. When reading the ROOT file back, the object is reconstructed in the memory.
Working with ROOT files
A ROOT file, this is a TFile object, is like a UNIX file directory. It can contain directories and objects organized in unlimited number of levels. A ROOT file is stored in machine independent format (ASCII, IEEE floating point, Big Endian byte ordering).
Creating a ROOT file
- Use the TFile
constructor for creating a ROOT file. A ROOT file uses the
The following options are available:
CREATE: Creates a ROOT file.
NEW: Same as
RECREATE: Replaces the ROOT file.
UPDATE: Updates the ROOT file.
READ: Opens an existing ROOT file for reading.
Once a TFile
object has been created, it becomes the default file for all I/O. This default is held in the global variable
gFile (see → ROOT classes, data types and global variables), which can be updated at any time to change the default.
When you create a TFile
object, it becomes the current directory. Therefore, the last ROOT file to be opened is always the current directory.
Check the current directory as follows:
In this case the current directory is the ROOT session (
When you create a TFile object, the ROOT file becomes the current directory.
Checking whether a ROOT file is open
- Use TFile::IsOpen() to check whether the ROOT file was successfully opened.
You can also check whether the ROOT file is correctly opened by:
Writing ROOT files
To write objects to a ROOT file, they must be open.
- Use TFile::Write() to write objects to a ROOT file.
A copy of
MyObject is written to the current directory of the current ROOT file with the named key
MyObject does not inherit from TClass
, you can use
This example creates 15 histograms, fills each histogram with 1000 entries from a Gaussian distribution, and writes them to a ROOT file.
The ROOT file is saved by default in the current working directory.
Closing a ROOT file
- Use TFile::Close() to close a ROOT file:
ROOT will automatically close any ROOT files still open when the session ends.
deleteto delete the TFile object.
Retrieving objects from a ROOT file
- Use the
GetObject()method to retrieve the objects from a ROOT file.
From the ROOT file
hsimple.root (see → First steps with ROOT), the histogram
hpx;1 is retrieved.
In detail, the following happens when executing
The key with name
hpx;1is found in the list of keys.
A TBuffer object is created.
The buffer is read from the ROOT file.
An empty object is created by calling the default constructor for the class referenced in TKey .
The Streamer() method is called for this new object.
In case there is an object with multiple cycles, you can pick a particular cycle with a name like
hpx; (for example
You can also directly access the keys, for example when the names of the objects contained in the ROOT file are not known or when a long series of objects needs to be read sequentially.
This example illustrates how to loop over all keys of a ROOT file.
Crash while reading and writing the ROOT file
If ROOT is not properly terminated, the file directory may not be written at the end of the ROOT file. Next time this ROOT file is used, ROOT will automatically detect this abnormal termination and will recover the directory by scanning sequentially the list of keys in the ROOT file. If the ROOT file has been opened in UPDATE mode, the recovered directory will be automatically written to the ROOT file. This automatic recovery procedure is possible because of redundant information written to the ROOT file.
In case you write large TTree s (see also → Trees), you may have large buffers in memory. In case of a job crash, you may loose a lot of data. Therefore, it recommended to use the auto save method TTree::AutoSave.
Merging ROOT files
- Use the
$ROOTSYS/bin/hadd, to merge ROOT files:
File system operations
A TFile , this is ROOT file, behaves like UNIX file system. Therefore, you can perform the usual operations for a file system.
ROOT command line tools
With the ROOT command line tools you can quickly inspect and modify the contents of ROOT files. There are ROOT command line tools for:
- simple file operations
- automating common operations performed on ROOT classes
rootls: Lists the content of a ROOT file.
rootcp: Copies objects stored in a ROOT file to another ROOT file.
rootrm: Deletes objects contained in a ROOT file.
rootmv: Moves objects stored in a ROOT file to another ROOT file.
rootmkdir: Creates a “directory” inside a ROOT file.
On the system prompt you can use the ROOT command line tool
rootls to list the contents of a ROOT file.
Operations on ROOT classes
rootbrowse: Opens a TBrowser directly on the content of a ROOT file.
rooteventselector: Extracts a range of events of a tree contained in a ROOT file and put them as a new tree in another ROOT file.
rootprint: Plots objects in an image ROOT file.
rootslimtree: Copies trees with a subset of branches from source ROOT files.
genreflex: Generates dictionary sources and related ROOT pcm, starting from an header.
hadd: Adds histograms from a list of ROOT files and writes them to a target ROOT file.
-h option to get more information on the available options for the specific ROOT command line tool.
ROOT Object Browser
With a TBrowser , this is the ROOT Object Browser, you can browse all ROOT objects within a ROOT file.
- Create a TBrowser object:
The ROOT Object Browser is displayed.
Figure: ROOT Object Browser.
- Click the ROOT file and the content of the ROOT file.
Figure: ROOT Object Browser displaying the content of a ROOT file.
Graphical objects are displayed in the
Canvas_1 tab. Files that end with
.root are displayed in the
Editor 1 tab.
A TFolder is a collection of objects visible and expandable in the ROOT Object Browser. Folders have a name and a title. They are identified in the folder hierarchy by an “UNIX-like” naming convention. New folders can be added and removed to/from a folder.
The base of all folders is the
//root folder. It is visible at the top of the left panel in the ROOT Object Browser.
Figure: root folder in the ROOT Object Browser.
With folders you can reduce class dependencies and improve modularity. Each set of data has a producer class and one or many consumer classes. When using folders, the producer class places a pointer to the data into a folder, and the consumer class retrieves a reference to the folder. The consumer can access the objects in a folder by specifying the path name of the folder.
Creating a folder hierarchy
To create a folder hierarchy, you add a top folder of your hierarchy to //root. Then you add a folder to an existing
folder with the TFolder::AddFolder() method. The
AddFolder() method takes two parameters: the name and title of the folder to be added. It returns a pointer of the newly created folder.
The following example creates a folder hierarchy shown in the ROOT Object Browser.
Figure: Folder hierarchy in the ROOT Object Browser.
Reading data from a folder
- Use the TROOT::FindObjectAny() method to search for a folder or an object in a folder.
FindObjectAny() method analyzes the string passed as its argument and searches in the hierarchy until it finds an object or folder matching the name.
FindObjectAny() you can give the full path name, or the name of the folder. If only the name of the folder is given, it will return the first instance of that name.
A string-based search is time consuming. If the retrieved object is used frequently or inside a loop, save a pointer to the object as a class data member.
By default, a folder does not own the object it contains. You can overwrite that with TFolder::SetOwner(). Once the folder is the owner of its contents, the contents are deleted when the folder is deleted.
If a file
myFile.root is added to the list of files, you can retrieve a pointer to the corresponding TFile
object with the following statements:
Viewing the contents of a ROOT file
Physical layout of a ROOT file
- Call the TFile::Map() method to view the physical layout of a ROOT file.
The output prints the date/time, the start record address, the number of bytes in the record, the class name of the record and the compression factor.
Logical contents of a ROOT file
ROOT provides not only sequential access to the content of a ROOT file, but also random or direct access.
TKey class describes the record headers of objects in the ROOT file. With the
GetListOfKeys() method you get the list of keys.
Get the list of keys from the demo.root file and print them.
Finding TKey objects
TFile::Get() method, you can find TKey objects.
Get() method finds the
TKey object with name h9.
Iterating over objects
TKeys of the
demo.root (see example → Creating a ROOT file file are iterated.
The output is of the
iterate.C ROOT macro is:
Remotely accessing a ROOT file
You can remotely access ROOT files on the base of the protocol URL.
You can read and write a ROOT file over the net by using the TFile::Open() method.
ls () lists what is in the ROOT file.