Trees
Introduction
A TTree is a specialised ROOT data structure designed for the
efficient storage and processing of large datasets.
Unlike conventional tables, ROOT trees organise data into independent branches, allowing individual variables to be accessed without reading the entire dataset into memory.
This approach is particularly useful in experimental particle physics, where millions of events may contain numerous measured or reconstructed physical quantities.
Structure of a TTree
A ROOT tree consists of three fundamental components:
- Branches (
TBranch) — represent individual variables, collections or objects stored in the tree. - Leaves (
TLeaf) — describe the data elements stored within a branch. - Entries — represent individual observations, typically corresponding to experimental or simulated events.
graph TD
T[TTree] --> B1[Branch: Energy]
T --> B2[Branch: Momentum]
T --> B3[Branch: Charge]
B1 --> L1[Leaf: E]
B2 --> L2[Leaf: p]
B3 --> L3[Leaf: q]
Logical representation
Consider a dataset containing measurements of individual particles.
| Entry | Energy [GeV] | Momentum [GeV/c] | Charge |
|---|---|---|---|
| 0 | 12.5 | 12.3 | -1 |
| 1 | 8.2 | 8.1 | +1 |
| 2 | 15.7 | 15.5 | -1 |
Each row represents an entry, while the columns correspond to branches in a simple tree structure.
Column-oriented storage
ROOT stores branch data independently in compressed buffers called baskets. This allows selected branches to be read without loading unrelated variables.
The main advantages of this storage approach are:
- Efficient access to selected variables.
- Reduced disk input/output operations.
- Effective data compression.
- Processing datasets larger than the available memory.
Creating a TTree
A tree can be created using the TTree class.
#include <TFile.h>
#include <TTree.h>
void create_tree()
{
TFile file("particles.root", "RECREATE");
TTree tree("particles", "Particle measurements");
double energy;
double momentum;
int charge;
tree.Branch("energy", &energy);
tree.Branch("momentum", &momentum);
tree.Branch("charge", &charge);
energy = 12.5;
momentum = 12.3;
charge = -1;
tree.Fill();
tree.Write();
}
The Branch() method associates a branch with a variable
in memory.
The Fill() method stores the current values of all
associated variables as a new entry.
The Write() method writes the tree to the ROOT file.
Reading a TTree
An existing tree can be accessed through a ROOT file.
TFile file("particles.root", "READ");
TTree* tree = file.Get<TTree>("particles");
tree->Print();
Inspecting the tree
ROOT provides several methods for inspecting tree contents.
| Method | Description |
|---|---|
Print() |
Displays the structure of the tree. |
Show(i) |
Displays the contents of entry i. |
Scan() |
Displays selected values in tabular form. |
GetEntries() |
Returns the number of entries. |
GetEntry(i) |
Loads the data of entry i. |
Draw() |
Visualises expressions involving branch values. |
Iterating over entries
Individual entries can be processed using a loop.
double energy;
tree->SetBranchAddress("energy", &energy);
for (Long64_t i = 0; i < tree->GetEntries(); ++i)
{
tree->GetEntry(i);
std::cout << energy << std::endl;
}
The SetBranchAddress() method associates an existing
branch with a variable in memory.
Each call to GetEntry() loads the corresponding entry
and updates the associated variables.
Visualising tree data
The Draw() method provides a convenient way to create
histograms directly from tree data.
tree->Draw("energy");
Selection criteria can also be specified.
tree->Draw("energy", "charge == -1");
Relationships between two variables can be investigated using two-dimensional visualisations.
tree->Draw("energy:momentum");
Modern interfaces
ROOT also provides higher-level interfaces for processing tree data:
TTreeReader— type-safe sequential access to tree entries.RDataFrame— declarative data analysis, filtering and transformation, with support for parallel execution.
These interfaces simplify the implementation of more complex scientific data analysis workflows.