Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree104_tree.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 tree104_tree.C
13/// ~~~
14/// 2. Using the automatic compiler interface
15/// ~~~
16/// .x tree104_tree.C++
17/// ~~~
18/// 3.
19/// ~~~
20/// .L tree104_tree.C or .L tree104_tree.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 tree104_tree.C
27/// read_tree();
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 tree104_write()
41{
42 // create a Tree file tree104.root
43
44 // create the file, the Tree and a few branches
45 TFile f("tree104.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 // save the Tree header. The file will be automatically closed
65 // when going out of the function scope
66 t1.Write();
67}
68
69void tree104_read()
70{
71 // read the Tree generated by tree1w and fill two histograms
72
73 Float_t px, py, pz;
74 Double_t random;
75 Int_t ev;
76 // note that we create the TFile and TTree objects on the heap!
77 // because we want to keep these objects alive when we leave this function.
78 auto f = TFile::Open("tree104.root");
79 auto t1 = f->Get<TTree>("t1");
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 auto hpx = new TH1F("hpx", "px distribution", 100, -3, 3);
88 auto 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())
101 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 tree104_tree()
113{
114 tree104_write();
115 tree104_read();
116}
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:69
int nentries
#define gROOT
Definition TROOT.h:406
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 an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4086
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:634
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:308
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
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:507
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8375
auto * t1
Definition textangle.C:20