Hsimple Reader


TTreeReader simplest example.

Read data from hsimple.root (written by hsimple.C)

Author: Anders Eie, 2013
This notebook tutorial was automatically generated with ROOTBOOK-izer (Beta) from the macro found in the ROOT repository on Tuesday, January 17, 2017 at 02:43 PM.


In [1]:
%%cpp -d
#include "TFile.h"
#include "TH1F.h"
#include "TTreeReader.h"
#include "TTreeReaderValue.h"



Create a histogram for the values we read.


In [2]:
auto myHist = new TH1F("h1","ntuple",100,-4,4);

Open the file containing the tree.


In [3]:
auto myFile = TFile::Open("hsimple.root");
if (!myFile || myFile->IsZombie()) {
   return;
}


Error in <TFile::TFile>: file hsimple.root does not exist

Create a ttreereader for the tree, for instance by passing the TTree's name and the TDirectory / TFile it is in.


In [4]:
TTreeReader myReader("ntuple", myFile);

The branch "px" contains floats; access them as mypx.


In [5]:
TTreeReaderValue<Float_t> myPx(myReader, "px");

The branch "py" contains floats, too; access those as mypy.


In [6]:
TTreeReaderValue<Float_t> myPy(myReader, "py");

Loop over all entries of the ttree or tchain.


In [7]:
while (myReader.Next()) {
   // Just access the data as if myPx and myPy were iterators (note the '*'
   // in front of them):
   myHist->Fill(*myPx + *myPy);
}

myHist->Draw();


Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

Draw all canvases


In [8]:
gROOT->GetListOfCanvases()->Draw()