From $ROOTSYS/tutorials/tree/hsimpleReader.C

// A simple TTreeReader use: read data from hsimple.root (written by hsimple.C)
// Author: Anders Eie, 2013.

#include "TFile.h"
#include "TH1F.h"
#include "TTreeReader.h"
#include "TTreeReaderValue.h"

class TVirtualPad;

void hsimpleReader() {
   // Create a histogram for the values we read.
   TH1F *myHist = new TH1F("h1","ntuple",100,-4,4);

   // Open the file containing the tree.
   TFile *myFile = TFile::Open("$ROOTSYS/tutorials/hsimple.root");
   if (!myFile || myFile->IsZombie()) {
      gROOT->ProcessLine(".x $ROOTSYS/tutorials/hsimple.C");
      myFile = TFile::Open("$ROOTSYS/tutorials/hsimple.root");
      if (!myFile || myFile->IsZombie()) {
         return;
      }
   }
   // Create a TTreeReader for the tree, for instance by passing the
   // TTree's name and the TDirectory / TFile it is in.
   TTreeReader myReader("ntuple", myFile);

   // The branch "px" contains floats; access them as myPx.
   TTreeReaderValue<Float_t> myPx(myReader, "px");
   // The branch "py" contains floats, too; access those as myPy.
   TTreeReaderValue<Float_t> myPy(myReader, "py");

   // Loop over all entries of the TTree or TChain.
   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();
}
 hsimpleReader.C:1
 hsimpleReader.C:2
 hsimpleReader.C:3
 hsimpleReader.C:4
 hsimpleReader.C:5
 hsimpleReader.C:6
 hsimpleReader.C:7
 hsimpleReader.C:8
 hsimpleReader.C:9
 hsimpleReader.C:10
 hsimpleReader.C:11
 hsimpleReader.C:12
 hsimpleReader.C:13
 hsimpleReader.C:14
 hsimpleReader.C:15
 hsimpleReader.C:16
 hsimpleReader.C:17
 hsimpleReader.C:18
 hsimpleReader.C:19
 hsimpleReader.C:20
 hsimpleReader.C:21
 hsimpleReader.C:22
 hsimpleReader.C:23
 hsimpleReader.C:24
 hsimpleReader.C:25
 hsimpleReader.C:26
 hsimpleReader.C:27
 hsimpleReader.C:28
 hsimpleReader.C:29
 hsimpleReader.C:30
 hsimpleReader.C:31
 hsimpleReader.C:32
 hsimpleReader.C:33
 hsimpleReader.C:34
 hsimpleReader.C:35
 hsimpleReader.C:36
 hsimpleReader.C:37
 hsimpleReader.C:38
 hsimpleReader.C:39
 hsimpleReader.C:40
 hsimpleReader.C:41
 hsimpleReader.C:42