ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hsimpleReader.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 ///
4 /// TTreeReader simplest example.
5 ///
6 /// Read data from hsimple.root (written by hsimple.C)
7 /// \macro_code
8 /// \author Anders Eie, 2013
9 
10 #include "TFile.h"
11 #include "TH1F.h"
12 #include "TTreeReader.h"
13 #include "TTreeReaderValue.h"
14 
15 void hsimpleReader() {
16  // Create a histogram for the values we read.
17  auto myHist = new TH1F("h1","ntuple",100,-4,4);
18 
19  // Open the file containing the tree.
20  auto myFile = TFile::Open("hsimple.root");
21  if (!myFile || myFile->IsZombie()) {
22  return;
23  }
24  // Create a TTreeReader for the tree, for instance by passing the
25  // TTree's name and the TDirectory / TFile it is in.
26  TTreeReader myReader("ntuple", myFile);
27 
28  // The branch "px" contains floats; access them as myPx.
29  TTreeReaderValue<Float_t> myPx(myReader, "px");
30  // The branch "py" contains floats, too; access those as myPy.
31  TTreeReaderValue<Float_t> myPy(myReader, "py");
32 
33  // Loop over all entries of the TTree or TChain.
34  while (myReader.Next()) {
35  // Just access the data as if myPx and myPy were iterators (note the '*'
36  // in front of them):
37  myHist->Fill(*myPx + *myPy);
38  }
39 
40  myHist->Draw();
41 }
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:48
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3851