class TTreeReader: public TObject

TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple. It uses TTreeReaderValue<T> and TTreeReaderArray<T> to access the data. Example code can be found in tutorials/tree/hsimpleReader.C and tutorials/trees/h1analysisTreeReader.h and tutorials/trees/h1analysisTreeReader.C for a TSelector. Roottest contains an example showing the full power. A simpler analysis example - the one from the tutorials - can be found below: it histograms a function of the px and py branches.

// A simple TTreeReader use: read data from hsimple.root (written by hsimple.C)
#include "TFile.h"
#include "TH1F.h"
#include "TTreeReader.h"
#include "TTreeReaderValue.h"
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");
   // 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);

A more complete example including error handling and a few combinations of TTreeReaderValue and TTreeReaderArray would look like this:

#include <TFile.h>
#include <TH1.h>
#include <TTreeReader.h>
#include <TTreeReaderValue.h>
#include <TTreeReaderArray.h>
#include "TriggerInfo.h"
#include "Muon.h"
#include "Tau.h"
#include <vector>
#include <iostream>
bool CheckValue(ROOT::TTreeReaderValueBase* value) {
   if (value->GetSetupStatus() < 0) {
      std::cerr << "Error " << value->GetSetupStatus()
                << "setting up reader for " << value->GetBranchName() << '\n';
      return false;
   return true;
// Analyze the tree "MyTree" in the file passed into the function.
// Returns false in case of errors.
bool analyze(TFile* file) {
   // Create a TTreeReader named "MyTree" from the given TDirectory.
   // The TTreeReader gives access to the TTree to the TTreeReaderValue and
   // TTreeReaderArray objects. It knows the current entry number and knows
   // how to iterate through the TTree.
   TTreeReader reader("MyTree", file);
   // Read a single float value in each tree entries:
   TTreeReaderValue<float> weight(reader, "event.weight");
   if (!CheckValue(weight)) return false;
   // Read a TriggerInfo object from the tree entries:
   TTreeReaderValue<TriggerInfo> triggerInfo(reader, "triggerInfo");
   if (!CheckValue(triggerInfo)) return false;
   // Read a vector of Muon objects from the tree entries:
   TTreeReaderValue<std::vector<Muon>> muons(reader, "muons");
   if (!CheckValue(muons)) return false;
   // Read the pT for all jets in the tree entry:
   TTreeReaderArray<double> jetPt(reader, "jets.pT");
   if (!CheckValue(jetPt)) return false;
   // Read the taus in the tree entry:
   TTreeReaderArray<Tau> taus(reader, "taus");
   if (!CheckValue(taus)) return false;
   // Now iterate through the TTree entries and fill a histogram.
   TH1* hist = new TH1F("hist", "TTreeReader example histogram", 10, 0., 100.);
   while (reader.Next()) {
      if (reader.GetEntryStatus() == kEntryValid) {
         std::cout << "Loaded entry " << reader.GetCurrentEntry() << '\n';
      } else {
         switch (reader.GetEntryStatus()) {
            // Handled above.
            std::cerr << "Error: TTreeReader has not loaded any data yet!\n";
            std::cerr << "Error: TTreeReader cannot find a tree names \"MyTree\"!\n";
            // Can't really happen as TTreeReader::Next() knows when to stop.
            std::cerr << "Error: The entry number doe not exist\n";
            std::cerr << "Error: TTreeReader cannot access a chain element, e.g. file without the tree\n";
            std::cerr << "Error: TTreeReader cannot open a chain element, e.g. missing file\n";
            std::cerr << "Error: TTreeReader cannot find the dictionary for some data\n";
         return false;
      // Access the TriggerInfo object as if it's a pointer.
      if (!triggerInfo->hasMuonL1())
      // Ditto for the vector<Muon>.
      if (!muons->size())
      // Access the jetPt as an array, whether the TTree stores this as
      // a std::vector, std::list, TClonesArray or Jet* C-style array, with
      // fixed or variable array size.
      if (jetPt.GetSize() < 2 || jetPt[0] < 100)
      // Access the array of taus.
      if (!taus.IsEmpty()) {
         float currentWeight = *weight;
         for (int iTau = 0, nTau = taus.GetSize(); iTau < nTau; ++iTau) {
            // Access a float value - need to dereference as TTreeReaderValue
            // behaves like an iterator
            hist->Fill(taus[iTau].eta(), currentWeight);
   } // TTree entry / event loop

Function Members (Methods)

voidTObject::AbstractMethod(const char* method) const
virtual voidTObject::AppendPad(Option_t* option = "")
virtual voidTObject::Browse(TBrowser* b)
static TClass*Class()
virtual const char*TObject::ClassName() const
virtual voidTObject::Clear(Option_t* = "")
virtual TObject*TObject::Clone(const char* newname = "") const
virtual Int_tTObject::Compare(const TObject* obj) const
virtual voidTObject::Copy(TObject& object) const
virtual voidTObject::Delete(Option_t* option = "")MENU
virtual Int_tTObject::DistancetoPrimitive(Int_t px, Int_t py)
virtual voidTObject::Draw(Option_t* option = "")
virtual voidTObject::DrawClass() constMENU
virtual TObject*TObject::DrawClone(Option_t* option = "") constMENU
virtual voidTObject::Dump() constMENU
TTreeReader::Iterator_tend() const
virtual voidTObject::Error(const char* method, const char* msgfmt) const
virtual voidTObject::Execute(const char* method, const char* params, Int_t* error = 0)
virtual voidTObject::Execute(TMethod* method, TObjArray* params, Int_t* error = 0)
virtual voidTObject::ExecuteEvent(Int_t event, Int_t px, Int_t py)
virtual voidTObject::Fatal(const char* method, const char* msgfmt) const
virtual TObject*TObject::FindObject(const char* name) const
virtual TObject*TObject::FindObject(const TObject* obj) const
Long64_tGetCurrentEntry() const
virtual Option_t*TObject::GetDrawOption() const
static Long_tTObject::GetDtorOnly()
Long64_tGetEntries(Bool_t force) const
TTreeReader::EEntryStatusGetEntryStatus() const
virtual const char*TObject::GetIconName() const
virtual const char*TObject::GetName() const
virtual char*TObject::GetObjectInfo(Int_t px, Int_t py) const
static Bool_tTObject::GetObjectStat()
virtual Option_t*TObject::GetOption() const
virtual const char*TObject::GetTitle() const
TTree*GetTree() const
virtual UInt_tTObject::GetUniqueID() const
virtual Bool_tTObject::HandleTimer(TTimer* timer)
virtual ULong_tTObject::Hash() const
virtual voidTObject::Info(const char* method, const char* msgfmt) const
virtual Bool_tTObject::InheritsFrom(const char* classname) const
virtual Bool_tTObject::InheritsFrom(const TClass* cl) const
virtual voidTObject::Inspect() constMENU
voidTObject::InvertBit(UInt_t f)
virtual TClass*IsA() const
Bool_tIsChain() const
virtual Bool_tTObject::IsEqual(const TObject* obj) const
virtual Bool_tTObject::IsFolder() const
Bool_tTObject::IsOnHeap() const
virtual Bool_tTObject::IsSortable() const
Bool_tTObject::IsZombie() const
virtual voidTObject::ls(Option_t* option = "") const
voidTObject::MayNotUse(const char* method) const
virtual Bool_tTObject::Notify()
voidTObject::Obsolete(const char* method, const char* asOfVers, const char* removedFromVers) const
voidTObject::operator delete(void* ptr)
voidTObject::operator delete(void* ptr, void* vp)
voidTObject::operator delete[](void* ptr)
voidTObject::operator delete[](void* ptr, void* vp)
void*TObject::operator new(size_t sz)
void*TObject::operator new(size_t sz, void* vp)
void*TObject::operator new[](size_t sz)
void*TObject::operator new[](size_t sz, void* vp)
TTreeReader&operator=(const TTreeReader&)
virtual voidTObject::Paint(Option_t* option = "")
virtual voidTObject::Pop()
virtual voidTObject::Print(Option_t* option = "") const
virtual Int_tTObject::Read(const char* name)
virtual voidTObject::RecursiveRemove(TObject* obj)
voidTObject::ResetBit(UInt_t f)
virtual voidTObject::SaveAs(const char* filename = "", Option_t* option = "") constMENU
virtual voidTObject::SavePrimitive(ostream& out, Option_t* option = "")
voidTObject::SetBit(UInt_t f)
voidTObject::SetBit(UInt_t f, Bool_t set)
voidSetChain(const char*, TFileCollection*)
virtual voidTObject::SetDrawOption(Option_t* option = "")MENU
static voidTObject::SetDtorOnly(void* obj)
TTreeReader::EEntryStatusSetEntry(Long64_t entry)
TTreeReader::EEntryStatusSetLocalEntry(Long64_t entry)
static voidTObject::SetObjectStat(Bool_t stat)
voidSetTree(TTree* tree)
voidSetTree(const char*, TDirectory*)
virtual voidTObject::SetUniqueID(UInt_t uid)
virtual voidShowMembers(TMemberInspector& insp) const
virtual voidStreamer(TBuffer&)
voidStreamerNVirtual(TBuffer& ClassDef_StreamerNVirtual_b)
virtual voidTObject::SysError(const char* method, const char* msgfmt) const
Bool_tTObject::TestBit(UInt_t f) const
Int_tTObject::TestBits(UInt_t f) const
TTreeReader(TTree* tree)
TTreeReader(const TTreeReader&)
TTreeReader(const char* keyname, TDirectory* dir = __null)
TTreeReader(const char*, TFileCollection*)
virtual voidTObject::UseCurrentStyle()
virtual voidTObject::Warning(const char* method, const char* msgfmt) const
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0)
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) const
voidDeregisterValueReader(ROOT::TTreeReaderValueBase* reader)
virtual voidTObject::DoError(int level, const char* location, const char* fmt, va_list va) const
ROOT::TNamedBranchProxy*FindProxy(const char* branchname) const
voidRegisterValueReader(ROOT::TTreeReaderValueBase* reader)
TTreeReader::EEntryStatusSetEntryBase(Long64_t entry, Bool_t local)

Data Members

static TObject::(anonymous)TObject::kBitMask
static TObject::EStatusBitsTObject::kCanDelete
static TObject::EStatusBitsTObject::kCannotPick
static TTreeReader::EEntryStatuskEntryChainFileError
static TTreeReader::EEntryStatuskEntryChainSetupError
static TTreeReader::EEntryStatuskEntryDictionaryError
static TTreeReader::EEntryStatuskEntryNoTree
static TTreeReader::EEntryStatuskEntryNotFound
static TTreeReader::EEntryStatuskEntryNotLoaded
static TTreeReader::EEntryStatuskEntryValid
static TObject::EStatusBitsTObject::kHasUUID
static TObject::EStatusBitsTObject::kInvalidObject
static TObject::(anonymous)TObject::kIsOnHeap
static TObject::EStatusBitsTObject::kIsReferenced
static TObject::EStatusBitsTObject::kMustCleanup
static TObject::EStatusBitsTObject::kNoContextMenu
static TObject::(anonymous)TObject::kNotDeleted
static TObject::EStatusBitsTObject::kObjInCanvas
static TObject::(anonymous)TObject::kOverwrite
static TObject::(anonymous)TObject::kSingleKey
static TObject::(anonymous)TObject::kWriteDelete
static TObject::(anonymous)TObject::kZombie
ROOT::TBranchProxyDirector*fDirectorproxying director, owned
TDirectory*fDirectorydirectory (or current file for chains)
TTreeReader::EEntryStatusfEntryStatusstatus of most recent read request
THashTablefProxiesattached ROOT::TNamedBranchProxies; owned
TTree*fTreetree that's read
deque<ROOT::TTreeReaderValueBase*>fValuesreaders that use our director
static TTreeReader::EPropertyBitskBitIsChain

Class Charts

Inheritance Chart:

Function documentation

TTreeReader(TTree* tree)
 Access data from tree.
TTreeReader(const char* keyname, TDirectory* dir = __null)
 Access data from the tree called keyname in the directory (e.g. TFile)
 dir, or the current directory if dir is NULL. If keyname cannot be
 found, or if it is not a TTree, IsZombie() will return true.
 Tell all value readers that the tree reader does not exist anymore.
void Initialize()
 Initialization of the director.
Long64_t GetCurrentEntry() const
Returns the index of the current entry being read
void SetTree(TTree* tree)
 Set (or update) the which tree to reader from. tree can be
 a TTree or a TChain.
void RegisterValueReader(ROOT::TTreeReaderValueBase* reader)
 Add a value reader for this tree.
void DeregisterValueReader(ROOT::TTreeReaderValueBase* reader)
 Remove a value reader for this tree.
TTreeReader(TTree* tree)
TTreeReader(const char* keyname, TDirectory* dir = __null)
void SetTree(TTree* tree)
void SetChain(const char* , TFileCollection* )
{ Error("SetChain()", "Not Implemented!");}
Bool_t IsChain() const
{ return TestBit(kBitIsChain); }
Bool_t Next()
{ return SetEntry(GetCurrentEntry() + 1) == kEntryValid; }
EEntryStatus SetEntry(Long64_t entry)
{ return SetEntryBase(entry, kFALSE); }
EEntryStatus SetLocalEntry(Long64_t entry)
{ return SetEntryBase(entry, kTRUE); }
EEntryStatus GetEntryStatus() const
{ return fEntryStatus; }
TTree* GetTree() const
{ return fTree; }
Long64_t GetEntries(Bool_t force) const
{ return fTree ? (force ? fTree->GetEntries() : fTree->GetEntriesFast() ) : -1; }
Iterator_t begin()
 Return an iterator to the 0th TTree entry.
Iterator_t end() const
{ return Iterator_t(); }
ROOT::TNamedBranchProxy* FindProxy(const char* branchname) const
TCollection* GetProxies()
{ return &fProxies; }
EEntryStatus SetEntryBase(Long64_t entry, Bool_t local)