ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tree1.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// This example is a variant of hsimple.C but using a TTree instead
4 /// of a TNtuple. It shows:
5 /// - how to fill a Tree with a few simple variables.
6 /// - how to read this Tree
7 /// - how to browse and analyze the Tree via the TBrowser and TTreeViewer
8 /// This example can be run in many different ways:
9 /// 1. Using the Cling interpreter
10 /// ~~~
11 /// .x tree1.C
12 /// ~~~
13 /// 2. Using the automatic compiler interface
14 /// ~~~
15 /// .x tree1.C++
16 /// ~~~
17 /// 3.
18 /// ~~~
19 /// .L tree1.C or .L tree1.C++
20 /// tree1()
21 /// ~~~
22 /// One can also run the write and read parts in two separate sessions.
23 /// For example following one of the sessions above, one can start the session:
24 /// ~~~
25 /// .L tree1.C
26 /// tree1r();
27 /// ~~~
28 /// \macro_code
29 ///
30 /// \author Rene Brun
31 
32 #include "TROOT.h"
33 #include "TFile.h"
34 #include "TTree.h"
35 #include "TBrowser.h"
36 #include "TH2.h"
37 #include "TRandom.h"
38 
39 void tree1w()
40 {
41  //create a Tree file tree1.root
42 
43  //create the file, the Tree and a few branches
44  TFile f("tree1.root","recreate");
45  TTree t1("t1","a simple Tree with simple variables");
46  Float_t px, py, pz;
48  Int_t ev;
49  t1.Branch("px",&px,"px/F");
50  t1.Branch("py",&py,"py/F");
51  t1.Branch("pz",&pz,"pz/F");
52  t1.Branch("random",&random,"random/D");
53  t1.Branch("ev",&ev,"ev/I");
54 
55  //fill the tree
56  for (Int_t i=0;i<10000;i++) {
57  gRandom->Rannor(px,py);
58  pz = px*px + py*py;
59  random = gRandom->Rndm();
60  ev = i;
61  t1.Fill();
62  }
63 
64  //save the Tree header. The file will be automatically closed
65  //when going out of the function scope
66  t1.Write();
67 }
68 
69 void tree1r()
70 {
71  //read the Tree generated by tree1w and fill two histograms
72 
73  //note that we use "new" to create the TFile and TTree objects !
74  //because we want to keep these objects alive when we leave this function.
75  TFile *f = new TFile("tree1.root");
76  TTree *t1 = (TTree*)f->Get("t1");
77  Float_t px, py, pz;
79  Int_t ev;
80  t1->SetBranchAddress("px",&px);
81  t1->SetBranchAddress("py",&py);
82  t1->SetBranchAddress("pz",&pz);
83  t1->SetBranchAddress("random",&random);
84  t1->SetBranchAddress("ev",&ev);
85 
86  //create two histograms
87  TH1F *hpx = new TH1F("hpx","px distribution",100,-3,3);
88  TH2F *hpxpy = new TH2F("hpxpy","py vs px",30,-3,3,30,-3,3);
89 
90  //read all entries and fill the histograms
91  Long64_t nentries = t1->GetEntries();
92  for (Long64_t i=0;i<nentries;i++) {
93  t1->GetEntry(i);
94  hpx->Fill(px);
95  hpxpy->Fill(px,py);
96  }
97 
98  //we do not close the file. We want to keep the generated histograms
99  //we open a browser and the TreeViewer
100  if (gROOT->IsBatch()) return;
101  new TBrowser();
102  t1->StartViewer();
103  // in the browser, click on "ROOT Files", then on "tree1.root".
104  // you can click on the histogram icons in the right panel to draw them.
105  // in the TreeViewer, follow the instructions in the Help button.
106 }
107 
108 void tree1() {
109  tree1w();
110  tree1r();
111 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
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:460
Float_t pz
Definition: hprod.C:33
long long Long64_t
Definition: RtypesCore.h:69
tuple random
Definition: hsimple.py:62
float Float_t
Definition: RtypesCore.h:53
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4306
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:344
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5144
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
Float_t py
Definition: hprod.C:33
TLatex * t1
Definition: textangle.C:20
TFile * f
virtual void StartViewer()
Start the TTreeViewer on this tree.
Definition: TTree.cxx:8473
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7510
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:8780
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
double Double_t
Definition: RtypesCore.h:55
int nentries
Definition: THbookFile.cxx:89
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1623
Float_t px
Definition: hprod.C:33
virtual Long64_t GetEntries() const
Definition: TTree.h:386
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:287