Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Tree tutorials

Example code which illustrates how to use ROOT trees and ntuples.

TTree represents a columnar dataset. Used for example by all LHC (Large Hadron Collider) experiments. Trees are optimized for reduced disk space and selecting, high-throughput columnar access with reduced memory usage.

...

In a nutshell:

auto file = TFile::Open("myfile.root", "recreate");
auto tree = new TTree("mytree", "A tree with my data");
...
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4130
A TTree represents a columnar dataset.
Definition TTree.h:79

Explore the examples below or go to TTree class reference.

Tutorials sorted after groups

List of all tutorials

Basic usage

These examples show how data can be stored to and read back from TTree.

Tutorial Description
tree101_basic.C Basic TTree usage.
tree102_basic.C A variant of tree101_basic.C
tree103_tree.C Simple Event class example
tree104_tree.C A variant of hsimple.C but using a TTree instead of a TNtuple
tree105_tree.C Illustrates how to make a tree from variables or arrays in a C struct - without a dictionary, by creating the branches for builtin types (int, float, double) and arrays explicitly
tree106_tree.C The same as tree105_tree.C, but uses a class instead of a C-struct
tree107_tree.C Example of a tree where branches are variable length arrays
tree108_tree.C This example writes a tree with objects of the class Event
tree109_friend.C Illustrates how to use tree friends
tree113_getval.C Illustrates how to retrieve tree variables in arrays
tree114_circular.C Example of a circular Tree. Circular Trees are interesting in online real time environments to store the results of the last maxEntries events

Copying Trees

These examples shows how to copy the content of trees.

Tutorial Description
tree110_copy.C Copy a subset of a tree to a new tree
tree111_copy.C Copy a subset of a tree to a new tree, one branch in a separate file
tree112_copy.C Copy a subset of a tree to a new tree, selecting entries

Collection Classes

These examples show how to write and read several collection classes (std::vector, TClonesArray,...) to trees.

Tutorial Description
tree120_ntuple.C Read a TNtuple from a file and performs a simple analysis
tree121_hvector.C Write and read STL vectors in a tree
tree122_vector3.C Write and read a Vector3 class in a tree
tree123_clonesarray.C Write and read a TClonesArray to a tree

More advanced examples

These examples shows a couple of more advanced examples using complex data.

Tutorial Description
tree130_jets.C Usage of a tree using the JetEvent class. The JetEvent class has several collections (TClonesArray) and other collections (TRefArray) referencing objects in the TClonesArrays
tree131_clones_event.C Example to write & read a Tree built with a complex class inheritance tree. It demonstrates usage of inheritance and TClonesArrays

Graphics

These tutorials show how to generate complex graphics from TNtuple and/or TTree.

Tutorial Description
tree140_spider.C Example of a TSpider plot generated from a TNtuple (from hsimple.root)
tree141_parallelcoord.C Illustrate the use of the TParallelCoord class (from a TNtuple)
tree142_parallelcoordtrans.C It displays TParallelCoord from the same dataset twice. The first time without transparency and the second time with transparency
tree143_drawsparse.C Convert a THnSparse to a TTree using efficient iteration through the THnSparse and draw it using TParallelCoord

More tutorials

A collection of other examples which showcases other usecases

Tutorial Description
tree200_temperature.C Illustrates how to use the highlight mode with trees, using a temperature dataset from Prague between 1775 and 2004
tree201_histograms.C Save histograms in tree branches
tree202_benchmarks.C Benchmark comparing row-wise and column-wise storage performance
tree500_cernbuild.C Read data (CERN staff) from an ascii file and create a root file with a tree
tree501_cernstaff.C Playing with a tree containing variables of type character
tree502_staff.C Create a plot of the data in cernstaff.root

Files

file  csv2tntuple.py
  View in nbviewer Open in SWAN
Build ROOT Ntuple from other source.
 
file  csv2tree_ReadStream.py
 This function provides an example of how one might massage a csv data file to read into a ROOT TTree via TTree::ReadStream.
 
file  hsimpleProxy.C
  View in nbviewer Open in SWAN
Used by hsimpleProxyDriver.C.
 
file  hsimpleProxyDriver.C
  View in nbviewer Open in SWAN
This is the driver of the hsimpleProxy example.
 
file  hsimpleReader.C
  View in nbviewer Open in SWAN
TTreeReader simplest example.
 
file  imt_parBranchProcessing.C
  View in nbviewer Open in SWAN
Demonstrate how to activate and use the implicit parallelisation of TTree::GetEntry.
 
file  imt_parTreeProcessing.C
  View in nbviewer Open in SWAN
Illustrate the usage of the TTreeProcessorMT::Process method.
 
file  mp_fillNtuples.C
  View in nbviewer Open in SWAN
Fill n-tuples in distinct workers.
 
file  mp_readNtuplesFillHistosAndFit.C
  View in nbviewer Open in SWAN
Read n-tuples in distinct workers, fill histograms, merge them and fit.
 
file  mt_fillNtupleFromMultipleThreads.C
  View in nbviewer Open in SWAN
Fill the same TNtuple from different threads.
 
file  mt_fillNtuples.C
  View in nbviewer Open in SWAN
Fill n-tuples in distinct workers.
 
file  mt_readNtuplesFillHistosAndFit.C
  View in nbviewer Open in SWAN
Read n-tuples in distinct workers, fill histograms, merge them and fit.
 
file  mtbb_fillNtuples.C
  View in nbviewer Open in SWAN
Fill n-tuples in distinct workers.
 
file  ntuple1.py
  View in nbviewer Open in SWAN
Ntuple drawing example.
 
file  printSizes.C
  View in nbviewer Open in SWAN
This macro can be used to get aggregate information on the size take on disk or in memory by the various branches in a TTree.
 
file  staff.py
  View in nbviewer Open in SWAN
example of macro to read data from an ascii file and create a root file with a Tree.
 
file  tree.C
 Display the Tree data structures.
 
file  tree101_basic.C
  View in nbviewer Open in SWAN
Read data from an ascii file and create a root file with an histogram and an ntuple.
 
file  tree102_basic.C
  View in nbviewer Open in SWAN
Create can ntuple reading data from an ascii file.
 
file  tree103_tree.C
  View in nbviewer Open in SWAN
Simple Event class example
 
file  tree104_tree.C
  View in nbviewer Open in SWAN
This example is a variant of hsimple.C but using a TTree instead of a TNtuple.
 
file  tree105_tree.C
  View in nbviewer Open in SWAN
This example illustrates how to make a Tree from variables or arrays in a C struct - without a dictionary, by creating the branches for builtin types (int, float, double) and arrays explicitly.
 
file  tree106_tree.C
  View in nbviewer Open in SWAN
This example is the same as tree105_tree.C, but uses a class instead of a C-struct.
 
file  tree107_tree.C
  View in nbviewer Open in SWAN
Example of a Tree where branches are variable length arrays A second Tree is created and filled in parallel.
 
file  tree108_tree.C
  View in nbviewer Open in SWAN
This example writes a tree with objects of the class Event.
 
file  tree109_friend.C
  View in nbviewer Open in SWAN
Illustrates how to use Tree friends:
 
file  tree110_copy.C
  View in nbviewer Open in SWAN
Copy a subset of a Tree to a new Tree
 
file  tree111_copy.C
  View in nbviewer Open in SWAN
Copy a subset of a Tree to a new Tree, one branch in a separate file.
 
file  tree112_copy.C
  View in nbviewer Open in SWAN
Example of Root macro to copy a subset of a Tree to a new Tree, selecting entries.
 
file  tree113_getval.C
  View in nbviewer Open in SWAN
Illustrates how to retrieve TTree variables in arrays.
 
file  tree114_circular.C
  View in nbviewer Open in SWAN
Example of a circular Tree
 
file  tree120_ntuple.C
  View in nbviewer Open in SWAN
Simple tree analysis.
 
file  tree121_hvector.C
  View in nbviewer Open in SWAN
Write and read STL vectors in a tree.
 
file  tree122_vector3.C
  View in nbviewer Open in SWAN
Write and read a Vector3 class in a tree.
 
file  tree123_clonesarray.C
  View in nbviewer Open in SWAN
How to write a TClonesArray to a TTree
 
file  tree130_jets.C
 Usage of a Tree using the JetEvent class.
 
file  tree131_clones_event.C
 Example to write & read a Tree built with a complex class inheritance tree.
 
file  tree140_spider.C
  View in nbviewer Open in SWAN
TSpider example.
 
file  tree141_parallelcoord.C
  View in nbviewer Open in SWAN
Script illustrating the use of the TParallelCoord class
 
file  tree142_parallelcoordtrans.C
  View in nbviewer Open in SWAN
Use of transparency with ||-Coord.
 
file  tree143_drawsparse.C
  View in nbviewer Open in SWAN
Convert a THnSparse to a TTree using efficient iteration through the THnSparse and draw a THnSparse using TParallelCoord.
 
file  tree200_temperature.C
 This tutorial illustrates how to use the highlight mode with trees.
 
file  tree201_histograms.C
  View in nbviewer Open in SWAN
Save histograms in Tree branches
 
file  tree202_benchmarks.C
  View in nbviewer Open in SWAN
Benchmark comparing row-wise and column-wise storage performance
 
file  tree500_cernbuild.C
  View in nbviewer Open in SWAN
Read data (CERN staff) from an ascii file and create a root file with a Tree.
 
file  tree501_cernstaff.C
  View in nbviewer Open in SWAN
Playing with a Tree containing variables of type character
 
file  tree502_staff.C
  View in nbviewer Open in SWAN
Create a plot of the data in cernstaff.root To create cernstaff.root, execute tutorial $ROOTSYS/tutorials/io/tree/tree500_cernbuild.C