Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree108_tree.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 tree108_tree.C
31/// ~~~
32/// - way2 using the Cling interpreter:
33/// ~~~
34/// .L tree108_tree.C
35/// tree108_tree()
36/// ~~~
37/// - way3 using ACLIC:
38/// ~~~
39/// .L ../test/libEvent.so
40/// .x tree108_tree.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 tree108_tree.C
46/// tree108_read();
47/// ~~~
48/// \macro_code
49///
50/// \author Rene Brun
51
52#ifdef R__WIN32
53R__LOAD_LIBRARY($ROOTSYS/test/libEvent.dll)
54#else
55R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so)
56#endif
57
58#include "TFile.h"
59#include "TTree.h"
60#include "TBrowser.h"
61#include "TH2.h"
62#include "TRandom.h"
63#include "TClassTable.h"
64#include "TSystem.h"
65#include "TROOT.h"
66#include "../test/Event.h"
67
68void tree108_write()
69{
70 // create a Tree file tree108.root
71 TFile f("tree108.root","RECREATE");
72
73 // create a ROOT Tree
74 TTree t4("t4","A Tree with Events");
75
76 // create a pointer to an Event object
77 Event *event = new Event();
78
79 // create two branches, split one.
80 t4.Branch("event_split", &event,16000,99);
81 t4.Branch("event_not_split", &event,16000,0);
82
83 // a local variable for the event type
84 char etype[20];
85
86 // fill the tree
87 for (Int_t ev = 0; ev < 100; ev++) {
88 Float_t sigmat, sigmas;
89 gRandom->Rannor(sigmat, sigmas);
90 Int_t ntrack = Int_t(600 + 600 * sigmat / 120.);
91 Float_t random = gRandom->Rndm(1);
92 sprintf(etype, "type%d", ev%5);
93 event->SetType(etype);
94 event->SetHeader(ev, 200, 960312, random);
95 event->SetNseg(Int_t(10 * ntrack + 20 * sigmas));
96 event->SetNvertex(Int_t(1 + 20 * gRandom->Rndm()));
97 event->SetFlag(UInt_t(random + 0.5));
98 event->SetTemperature(random + 20.);
99
100 for (UChar_t m = 0; m < 10; m++) {
101 event->SetMeasure(m, Int_t(gRandom->Gaus(m, m + 1)));
102 }
103
104 // fill the matrix
105 for (UChar_t i0 = 0; i0 < 4; i0++) {
106 for(UChar_t i1 = 0; i1 < 4; i1++) {
107 event->SetMatrix(i0, i1, gRandom->Gaus(i0 * i1, 1));
108 }
109 }
110
111 // Create and fill the Track objects
112 for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random);
113
114 // Fill the tree
115 t4.Fill();
116
117 // Clear the event before reloading it
118 event->Clear();
119 }
120 // Write the file header
121 f.Write();
122
123 // Print the tree contents
124 t4.Print();
125}
126
127
128void tree108_read()
129{
130 // read the tree generated with tree108_write
131
132 // note that we create the TFile and TTree objects on the heap !
133 // because we want to keep these objects alive when we leave this function.
134 auto f = TFile::Open("tree108.root");
135 auto t4 = f->Get<TTree>("t4");
136
137 // create a pointer to an event object. This will be used
138 // to read the branch values.
139 auto event = new Event();
140
141 // get two branches and set the branch address
142 auto bntrack = t4->GetBranch("fNtrack");
143 auto branch = t4->GetBranch("event_split");
144 branch->SetAddress(&event);
145
146 Long64_t nevent = t4->GetEntries();
147 Int_t nselected = 0;
148 Int_t nb = 0;
149 for (Long64_t i=0; i<nevent; i++) {
150 // read branch "fNtrack"only
151 bntrack->GetEntry(i);
152
153 // reject events with more than 587 tracks
154 if (event->GetNtrack() > 587)
155 continue;
156
157 // read complete accepted event in memory
158 nb += t4->GetEntry(i);
159 nselected++;
160
161 // print the first accepted event
162 if (nselected == 1)
163 t4->Show();
164
165 // clear tracks array
166 event->Clear();
167 }
168
169 if (gROOT->IsBatch())
170 return;
171 new TBrowser();
172 t4->StartViewer();
173}
174
175void tree108_tree()
176{
177 Event::Reset(); // Allow for re-run this script by cleaning static variables.
178 tree108_write();
179 Event::Reset(); // Allow for re-run this script by cleaning static variables.
180 tree108_read();
181}
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
unsigned char UChar_t
Definition RtypesCore.h:38
unsigned int UInt_t
Definition RtypesCore.h:46
float Float_t
Definition RtypesCore.h:57
long long Long64_t
Definition RtypesCore.h:69
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:496
#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
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:275
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
TMarker m
Definition textangle.C:8