Skip to content

CppTTree

Maurik Holtrop edited this page Jul 5, 2021 · 7 revisions

Using C++ and the TTree->Draw() method.

This is a relatively easy way to explore what is in a flat ntuple style ROOT file. It works very well and it is very fast for simple histograms of items that are directly available in the ntuple. When you get to more complicated histograms that require calculations to produce the numbers that you want to fill the histogram with, I highly recommend that you try to write a loop (Python Loops) or better and faster, learn about RDataFrame.

For more general details on how to use ROOT with Python, see the documentation for PyROOT:

A quick example.

auto root_file = new TFile("pi0_455GeV_01_recon.root"); 
root_file->ls();    // See what is in the root file.
auto t = root_file->Get<TTree>("MiniDST");  // Get the TTree from the file. Note you must specify type.
t->Print();                     // Print a long list of all the available data in the TTree. Output not shown.
auto leaves = t->GetListOfLeaves();            // A Python way to get the list of leaves (data) in the tree.
for(auto l: *leaves) cout << l->GetName() << endl;  // Print the list of leaves. Similar: for(auto l: *leaves) l->Print();               
t->Draw("ecal_cluster_energy"); // Create a histogram of the ECal cluster energy and draw it.

Output:

TFile**		pi0_455GeV_01_recon.root	
 TFile*		pi0_455GeV_01_recon.root	
  KEY: TTree	MiniDST;10	HPS mini-DST [current cycle]
  KEY: TTree	MiniDST;9	HPS mini-DST [backup cycle]

Ecal Energy example histogram

If you want to just "click around" a bit and see what values various items in the tree take, you can simply accomplish this with:

auto root_file = new TFile("pi0_455GeV_01_recon.root"); 
new TTreeViewer("MiniDST")

TTreeViewer example

Basically, everything you can do with TTree in C++ you can also do in Python. See more in Python TTree.

More details of using Draw are found in ROOT TTree Manual and the tutorials for trees: Tree Tutorials

Clone this wiki locally