Logo ROOT  
Reference Guide
tree4.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook -nodraw
4/// This example writes a tree with objects of the class Event.
5/// It is a simplified version of $ROOTSYS/test/MainEvent.cxx to
6/// write the tree, and $ROOTSYS/test/eventb.C
7/// It shows:
8/// - how to fill a Tree with an event class containing these data members:
9/// ~~~
10/// char fType[20];
11/// Int_t fNtrack;
12/// Int_t fNseg;
13/// Int_t fNvertex;
14/// UInt_t fFlag;
15/// Float_t fTemperature;
16/// EventHeader fEvtHdr;
17/// TClonesArray *fTracks; //->
18/// TH1F *fH; //->
19/// Int_t fMeasures[10];
20/// Float_t fMatrix[4][4];
21/// Float_t *fClosestDistance; //[fNvertex]
22/// ~~~
23/// - the difference in splitting or not splitting a branch
24/// - how to read selected branches of the tree, and print the first entry with less than 587 tracks.
25/// - how to browse and analyze the Tree via the TBrowser and TTreeViewer
26///
27/// This example can be run in many different ways:
28/// - way1 using the Cling interpreter:
29/// ~~~
30/// .x tree4.C
31/// ~~~
32/// - way2 using the Cling interpreter:
33/// ~~~
34/// .L tree4.C
35/// tree4()
36/// ~~~
37/// - way3 using ACLIC:
38/// ~~~
39/// .L ../test/libEvent.so
40/// .x tree4.C++
41/// ~~~
42/// One can also run the write and read parts in two separate sessions.
43/// For example following one of the sessions above, one can start the session:
44/// ~~~
45/// .L tree4.C
46/// tree4r();
47/// ~~~
48/// \macro_code
49///
50/// \author Rene Brun
51
52R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so)
53
54#include "TFile.h"
55#include "TTree.h"
56#include "TBrowser.h"
57#include "TH2.h"
58#include "TRandom.h"
59#include "TClassTable.h"
60#include "TSystem.h"
61#include "TROOT.h"
62#include "../test/Event.h"
63
64void tree4w()
65{
66
67 //create a Tree file tree4.root
68 TFile f("tree4.root","RECREATE");
69
70 // Create a ROOT Tree
71 TTree t4("t4","A Tree with Events");
72
73 // Create a pointer to an Event object
74 Event *event = new Event();
75
76 // Create two branches, split one.
77 t4.Branch("event_split", &event,16000,99);
78 t4.Branch("event_not_split", &event,16000,0);
79
80 // a local variable for the event type
81 char etype[20];
82
83 // Fill the tree
84 for (Int_t ev = 0; ev <100; ev++) {
85 Float_t sigmat, sigmas;
86 gRandom->Rannor(sigmat,sigmas);
87 Int_t ntrack = Int_t(600 + 600 *sigmat/120.);
88 Float_t random = gRandom->Rndm(1);
89 sprintf(etype,"type%d",ev%5);
90 event->SetType(etype);
91 event->SetHeader(ev, 200, 960312, random);
92 event->SetNseg(Int_t(10*ntrack+20*sigmas));
93 event->SetNvertex(Int_t(1+20*gRandom->Rndm()));
94 event->SetFlag(UInt_t(random+0.5));
95 event->SetTemperature(random+20.);
96
97 for(UChar_t m = 0; m < 10; m++) {
98 event->SetMeasure(m, Int_t(gRandom->Gaus(m,m+1)));
99 }
100
101 // fill the matrix
102 for(UChar_t i0 = 0; i0 < 4; i0++) {
103 for(UChar_t i1 = 0; i1 < 4; i1++) {
104 event->SetMatrix(i0,i1,gRandom->Gaus(i0*i1,1));
105 }
106 }
107
108 // Create and fill the Track objects
109 for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random);
110
111 // Fill the tree
112 t4.Fill();
113
114 // Clear the event before reloading it
115 event->Clear();
116 }
117
118 // Write the file header
119 f.Write();
120
121 // Print the tree contents
122 t4.Print();
123}
124
125
126void tree4r()
127{
128 // read the tree generated with tree4w
129
130 //note that we use "new" to create the TFile and TTree objects !
131 //because we want to keep these objects alive when we leave this function.
132 TFile *f = new TFile("tree4.root");
133 TTree *t4 = (TTree*)f->Get("t4");
134
135 // create a pointer to an event object. This will be used
136 // to read the branch values.
137 Event *event = new Event();
138
139 // get two branches and set the branch address
140 TBranch *bntrack = t4->GetBranch("fNtrack");
141 TBranch *branch = t4->GetBranch("event_split");
142 branch->SetAddress(&event);
143
144 Long64_t nevent = t4->GetEntries();
145 Int_t nselected = 0;
146 Int_t nb = 0;
147 for (Long64_t i=0;i<nevent;i++) {
148 //read branch "fNtrack"only
149 bntrack->GetEntry(i);
150
151 //reject events with more than 587 tracks
152 if (event->GetNtrack() > 587)continue;
153
154 //read complete accepted event in memory
155 nb += t4->GetEntry(i);
156 nselected++;
157
158 //print the first accepted event
159 if (nselected == 1) t4->Show();
160
161 //clear tracks array
162 event->Clear();
163 }
164
165 if (gROOT->IsBatch()) return;
166 new TBrowser();
167 t4->StartViewer();
168}
169
170void tree4() {
171 Event::Reset(); // Allow for re-run this script by cleaning static variables.
172 tree4w();
173 Event::Reset(); // Allow for re-run this script by cleaning static variables.
174 tree4r();
175}
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
unsigned char UChar_t
Definition: RtypesCore.h:34
unsigned int UInt_t
Definition: RtypesCore.h:42
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
#define R__LOAD_LIBRARY(LIBRARY)
Definition: Rtypes.h:473
#define gROOT
Definition: TROOT.h:415
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
A TTree is a list of TBranches.
Definition: TBranch.h:91
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition: TBranch.cxx:1579
virtual void SetAddress(void *add)
Set address of this branch.
Definition: TBranch.cxx:2519
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
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:263
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
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:5170
virtual void Show(Long64_t entry=-1, Int_t lenmax=20)
Print values of all active leaves for entry.
Definition: TTree.cxx:9096
virtual Long64_t GetEntries() const
Definition: TTree.h:450
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:5497
virtual void StartViewer()
Start the TTreeViewer on this tree.
Definition: TTree.cxx:9170
Definition: test.py:1
auto * m
Definition: textangle.C:8