ROOT logo
#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"
#include "TBrowser.h"
#include "TH2.h"
#include "TRandom.h"

// This example is a variant of hsimple.C but using a TTree instead
// of a TNtuple. It shows :
//   -how to fill a Tree with a few simple variables.
//   -how to read this Tree
//   -how to browse and analyze the Tree via the TBrowser and TTreeViewer
// This example can be run in many different ways:
//  way1:  .x tree1.C    using the CINT interpreter
//  way2:  .x tree1.C++  using the automatic compiler interface
//  way3:  .L tree1.C  or .L tree1.C++
//          tree1()
// One can also run the write and read parts in two separate sessions.
// For example following one of the sessions above, one can start the session:
//   .L tree1.C
//   tree1r();
//
//  Author: Rene Brun

void tree1w()
{
   //create a Tree file tree1.root
   
   //create the file, the Tree and a few branches
   TFile f("tree1.root","recreate");
   TTree t1("t1","a simple Tree with simple variables");
   Float_t px, py, pz;
   Double_t random;
   Int_t ev;
   t1.Branch("px",&px,"px/F");
   t1.Branch("py",&py,"py/F");
   t1.Branch("pz",&pz,"pz/F");
   t1.Branch("random",&random,"random/D");
   t1.Branch("ev",&ev,"ev/I");
   
   //fill the tree
   for (Int_t i=0;i<10000;i++) {
     gRandom->Rannor(px,py);
     pz = px*px + py*py;
     random = gRandom->Rndm();
     ev = i;
     t1.Fill();
  }
  
  //save the Tree header. The file will be automatically closed
  //when going out of the function scope
  t1.Write();
}

void tree1r()
{
   //read the Tree generated by tree1w and fill two histograms
   
   //note that we use "new" to create the TFile and TTree objects !
   //because we want to keep these objects alive when we leave this function.
   TFile *f = new TFile("tree1.root");
   TTree *t1 = (TTree*)f->Get("t1");
   Float_t px, py, pz;
   Double_t random;
   Int_t ev;
   t1->SetBranchAddress("px",&px);
   t1->SetBranchAddress("py",&py);
   t1->SetBranchAddress("pz",&pz);
   t1->SetBranchAddress("random",&random);
   t1->SetBranchAddress("ev",&ev);
   
   //create two histograms
   TH1F *hpx   = new TH1F("hpx","px distribution",100,-3,3);
   TH2F *hpxpy = new TH2F("hpxpy","py vs px",30,-3,3,30,-3,3);
   
   //read all entries and fill the histograms
   Int_t nentries = (Int_t)t1->GetEntries();
   for (Int_t i=0;i<nentries;i++) {
     t1->GetEntry(i);
     hpx->Fill(px);
     hpxpy->Fill(px,py);
  }
  
  //we do not close the file. We want to keep the generated histograms
  //we open a browser and the TreeViewer
  if (gROOT->IsBatch()) return;
  new TBrowser();
  t1->StartViewer();
  // in the browser, click on "ROOT Files", then on "tree1.root".
  //     you can click on the histogram icons in the right panel to draw them.
  // in the TreeViewer, follow the instructions in the Help button.
}   

void tree1() {
   tree1w();
   tree1r();
}
 tree1.C:1
 tree1.C:2
 tree1.C:3
 tree1.C:4
 tree1.C:5
 tree1.C:6
 tree1.C:7
 tree1.C:8
 tree1.C:9
 tree1.C:10
 tree1.C:11
 tree1.C:12
 tree1.C:13
 tree1.C:14
 tree1.C:15
 tree1.C:16
 tree1.C:17
 tree1.C:18
 tree1.C:19
 tree1.C:20
 tree1.C:21
 tree1.C:22
 tree1.C:23
 tree1.C:24
 tree1.C:25
 tree1.C:26
 tree1.C:27
 tree1.C:28
 tree1.C:29
 tree1.C:30
 tree1.C:31
 tree1.C:32
 tree1.C:33
 tree1.C:34
 tree1.C:35
 tree1.C:36
 tree1.C:37
 tree1.C:38
 tree1.C:39
 tree1.C:40
 tree1.C:41
 tree1.C:42
 tree1.C:43
 tree1.C:44
 tree1.C:45
 tree1.C:46
 tree1.C:47
 tree1.C:48
 tree1.C:49
 tree1.C:50
 tree1.C:51
 tree1.C:52
 tree1.C:53
 tree1.C:54
 tree1.C:55
 tree1.C:56
 tree1.C:57
 tree1.C:58
 tree1.C:59
 tree1.C:60
 tree1.C:61
 tree1.C:62
 tree1.C:63
 tree1.C:64
 tree1.C:65
 tree1.C:66
 tree1.C:67
 tree1.C:68
 tree1.C:69
 tree1.C:70
 tree1.C:71
 tree1.C:72
 tree1.C:73
 tree1.C:74
 tree1.C:75
 tree1.C:76
 tree1.C:77
 tree1.C:78
 tree1.C:79
 tree1.C:80
 tree1.C:81
 tree1.C:82
 tree1.C:83
 tree1.C:84
 tree1.C:85
 tree1.C:86
 tree1.C:87
 tree1.C:88
 tree1.C:89
 tree1.C:90
 tree1.C:91
 tree1.C:92
 tree1.C:93
 tree1.C:94
 tree1.C:95
 tree1.C:96
 tree1.C:97
 tree1.C:98