How to Write and Read a ROOT File

First, How to Create a ROOT File

In this section, I will be showing how to store data inside a root file. And then, I will show how to read data from a root file properly using minimum effort. If you follow my way, then you might bypass any conflict of data types between root defined types (i.e. Double_t, Float_t, etc) and c++ defined types (i.e. double, float, etc).

Storing data into a root file has several advantages over ASCII or BINARY files. Root file compresses data to occupy small disk space. But, that is not the best part of it. In a root file, you can store data in a specific data structure. Data in a root file is stored entry wise. In each entry, several types of data structures can be stored – especially Array of Data.

In each root file, there might be several (T)Tree. Inside a Tree, there might be many (T)Branch. Inside a Branch, there might be many (T)Leaf. Objects like Histograms, TGraphs, etc. can be stored directly within a Tree. But, the data can be stored within a Branch or Leaf. Each Branch can be filled with several multidimensional arrays at each entry. If the dimension of the array is 1 then it is a single valued.

Now, first we are going to store some data into a root file. Here, we are going to store the same data used in the advanced example. We will sore a data array. The first data array entry will be the voltage points. Then the rest of the entries will include the current reading from each set. Here is the code.

Code to Create ROOT File

Note. There is a famous runtime error in creating root files. The error looks like this.

Error in TBranch::TBranch: Illegal leaf:

Say, you want to store an array named read[pointIn] where the variable pointIn changes in each entry. To make this work, both pointIn and read[pointIn] must have two separate TBranch in the TTree. Please see the code carefully. Please check this part of the code carefully.

const int points = 11;
unsigned int pointIn;
float reading[points];

outTree->Branch("pointIn",&pointIn,"pointIn/I");
outTree->Branch("reading",reading,"reading[pointIn]/F");

Second, How to read a ROOT File

To read a root file, first, the structure of the root file has to be acquired. There are two ways to include the structure into the code. I always prefer to keep the structure as a include and header file. To get those files, you need a small help of root prompt or root interactive mode. Follow these steps.

  1. Go to the directory where the root file is situated. In this example it is test.root.
  2. Use the command bellow to load the file into root prompt.
    $ root -l test.root
  3. Use .ls command to see the list of TTree and other objects (histograms etc). There will be a line like this,
    KEY: TTree    testTree;1    testTree
    The name of the TTree is  testTree;1 excluded ;1, I mean testTree.
  4. Now use the next command to generate include and header file.
    $ testTree->MakeClass()
    You should now see two files with extensions .h and .C inside the current directory. The first one is the header file and the second one is the include file.

All the variables and arrays are defined in this header class. Just include them in the code. And call proper functions to read the data. The code for reading root file is at bellow.

Code to Read ROOT File

In the code, I have tried to explain each steps properly. The plotting part is exactly similar with the Advanced V-I example. So, the output has to be same.

 Tags: TRandom, TH1, TGraphErrors, TPostScript, TFile, TTree