Logo ROOT  
Reference Guide
tree1.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook -nodraw
4/// This example is a variant of hsimple.C but using a TTree instead
5/// of a TNtuple. It shows:
6/// - how to fill a Tree with a few simple variables.
7/// - how to read this Tree
8/// - how to browse and analyze the Tree via the TBrowser and TTreeViewer
9/// This example can be run in many different ways:
10/// 1. Using the Cling interpreter
11/// ~~~
12/// .x tree1.C
13/// ~~~
14/// 2. Using the automatic compiler interface
15/// ~~~
16/// .x tree1.C++
17/// ~~~
18/// 3.
19/// ~~~
20/// .L tree1.C or .L tree1.C++
21/// tree1()
22/// ~~~
23/// One can also run the write and read parts in two separate sessions.
24/// For example following one of the sessions above, one can start the session:
25/// ~~~
26/// .L tree1.C
27/// tree1r();
28/// ~~~
29/// \macro_code
30///
31/// \author Rene Brun
32
33#include "TROOT.h"
34#include "TFile.h"
35#include "TTree.h"
36#include "TBrowser.h"
37#include "TH2.h"
38#include "TRandom.h"
39
40void tree1w()
41{
42 //create a Tree file tree1.root
43
44 //create the file, the Tree and a few branches
45 TFile f("tree1.root","recreate");
46 TTree t1("t1","a simple Tree with simple variables");
47 Float_t px, py, pz;
48 Double_t random;
49 Int_t ev;
50 t1.Branch("px",&px,"px/F");
51 t1.Branch("py",&py,"py/F");
52 t1.Branch("pz",&pz,"pz/F");
53 t1.Branch("random",&random,"random/D");
54 t1.Branch("ev",&ev,"ev/I");
55
56 //fill the tree
57 for (Int_t i=0;i<10000;i++) {
58 gRandom->Rannor(px,py);
59 pz = px*px + py*py;
60 random = gRandom->Rndm();
61 ev = i;
62 t1.Fill();
63 }
64
65 //save the Tree header. The file will be automatically closed
66 //when going out of the function scope
67 t1.Write();
68}
69
70void tree1r()
71{
72 //read the Tree generated by tree1w and fill two histograms
73
74 //note that we use "new" to create the TFile and TTree objects !
75 //because we want to keep these objects alive when we leave this function.
76 TFile *f = new TFile("tree1.root");
77 TTree *t1 = (TTree*)f->Get("t1");
78 Float_t px, py, pz;
79 Double_t random;
80 Int_t ev;
81 t1->SetBranchAddress("px",&px);
82 t1->SetBranchAddress("py",&py);
83 t1->SetBranchAddress("pz",&pz);
84 t1->SetBranchAddress("random",&random);
85 t1->SetBranchAddress("ev",&ev);
86
87 //create two histograms
88 TH1F *hpx = new TH1F("hpx","px distribution",100,-3,3);
89 TH2F *hpxpy = new TH2F("hpxpy","py vs px",30,-3,3,30,-3,3);
90
91 //read all entries and fill the histograms
92 Long64_t nentries = t1->GetEntries();
93 for (Long64_t i=0;i<nentries;i++) {
94 t1->GetEntry(i);
95 hpx->Fill(px);
96 hpxpy->Fill(px,py);
97 }
98
99 //we do not close the file. We want to keep the generated histograms
100 //we open a browser and the TreeViewer
101 if (gROOT->IsBatch()) return;
102 new TBrowser();
103 t1->StartViewer();
104 // in the browser, click on "ROOT Files", then on "tree1.root".
105 // you can click on the histogram icons in the right panel to draw them.
106 // in the TreeViewer, follow the instructions in the Help button.
107
108 // Allow to use the TTree after the end of the function.
109 t1->ResetBranchAddresses();
110}
111
112void tree1() {
113 tree1w();
114 tree1r();
115}
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
double Double_t
Definition: RtypesCore.h:55
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
int nentries
Definition: THbookFile.cxx:89
#define gROOT
Definition: TROOT.h:415
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3275
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition: TRandom.cxx:489
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:541
A TTree represents a columnar dataset.
Definition: TTree.h:72
auto * t1
Definition: textangle.C:20