Logo ROOT   6.07/09
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 
52 #include "TFile.h"
53 #include "TTree.h"
54 #include "TBrowser.h"
55 #include "TH2.h"
56 #include "TRandom.h"
57 #include "TClassTable.h"
58 #include "TSystem.h"
59 #include "TROOT.h"
60 #include "../test/Event.h"
61 
62 void tree4w()
63 {
64 
65  //create a Tree file tree4.root
66  TFile f("tree4.root","RECREATE");
67 
68  // Create a ROOT Tree
69  TTree t4("t4","A Tree with Events");
70 
71  // Create a pointer to an Event object
72  Event *event = new Event();
73 
74  // Create two branches, split one.
75  t4.Branch("event_split", &event,16000,99);
76  t4.Branch("event_not_split", &event,16000,0);
77 
78  // a local variable for the event type
79  char etype[20];
80 
81  // Fill the tree
82  for (Int_t ev = 0; ev <100; ev++) {
83  Float_t sigmat, sigmas;
84  gRandom->Rannor(sigmat,sigmas);
85  Int_t ntrack = Int_t(600 + 600 *sigmat/120.);
86  Float_t random = gRandom->Rndm(1);
87  sprintf(etype,"type%d",ev%5);
88  event->SetType(etype);
89  event->SetHeader(ev, 200, 960312, random);
90  event->SetNseg(Int_t(10*ntrack+20*sigmas));
91  event->SetNvertex(Int_t(1+20*gRandom->Rndm()));
92  event->SetFlag(UInt_t(random+0.5));
93  event->SetTemperature(random+20.);
94 
95  for(UChar_t m = 0; m < 10; m++) {
96  event->SetMeasure(m, Int_t(gRandom->Gaus(m,m+1)));
97  }
98 
99  // fill the matrix
100  for(UChar_t i0 = 0; i0 < 4; i0++) {
101  for(UChar_t i1 = 0; i1 < 4; i1++) {
102  event->SetMatrix(i0,i1,gRandom->Gaus(i0*i1,1));
103  }
104  }
105 
106  // Create and fill the Track objects
107  for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random);
108 
109  // Fill the tree
110  t4.Fill();
111 
112  // Clear the event before reloading it
113  event->Clear();
114  }
115 
116  // Write the file header
117  f.Write();
118 
119  // Print the tree contents
120  t4.Print();
121 }
122 
123 
124 void tree4r()
125 {
126  // check to see if the event class is in the dictionary
127  // if it is not load the definition in libEvent.so
128  if (!TClassTable::GetDict("Event")) {
129  gSystem->Load("$ROOTSYS/test/libEvent");
130  }
131 
132  // read the tree generated with tree4w
133 
134  //note that we use "new" to create the TFile and TTree objects !
135  //because we want to keep these objects alive when we leave this function.
136  TFile *f = new TFile("tree4.root");
137  TTree *t4 = (TTree*)f->Get("t4");
138 
139  // create a pointer to an event object. This will be used
140  // to read the branch values.
141  Event *event = new Event();
142 
143  // get two branches and set the branch address
144  TBranch *bntrack = t4->GetBranch("fNtrack");
145  TBranch *branch = t4->GetBranch("event_split");
146  branch->SetAddress(&event);
147 
148  Long64_t nevent = t4->GetEntries();
149  Int_t nselected = 0;
150  Int_t nb = 0;
151  for (Long64_t i=0;i<nevent;i++) {
152  //read branch "fNtrack"only
153  bntrack->GetEntry(i);
154 
155  //reject events with more than 587 tracks
156  if (event->GetNtrack() > 587)continue;
157 
158  //read complete accepted event in memory
159  nb += t4->GetEntry(i);
160  nselected++;
161 
162  //print the first accepted event
163  if (nselected == 1) t4->Show();
164 
165  //clear tracks array
166  event->Clear();
167  }
168 
169  if (gROOT->IsBatch()) return;
170  new TBrowser();
171  t4->StartViewer();
172 }
173 
174 void tree4() {
175  Event::Reset(); // Allow for re-run this script by cleaning static variables.
176  tree4w();
177  Event::Reset(); // Allow for re-run this script by cleaning static variables.
178  tree4r();
179 }
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
virtual void SetAddress(void *add)
Set address of this branch.
Definition: TBranch.cxx:2068
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
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:235
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:364
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:5210
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1818
int Int_t
Definition: RtypesCore.h:41
virtual void StartViewer()
Start the TTreeViewer on this tree.
Definition: TTree.cxx:8694
virtual void Show(Long64_t entry=-1, Int_t lenmax=20)
Print values of all active leaves for entry.
Definition: TTree.cxx:8620
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4868
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
unsigned int UInt_t
Definition: RtypesCore.h:42
TMarker * m
Definition: textangle.C:8
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:1217
void Reset(Detail::TBranchProxy *x)
R__EXTERN TRandom * gRandom
Definition: TRandom.h:66
double f(double x)
static DictFuncPtr_t GetDict(const char *cname)
Given the class name returns the Dictionary() function of a class (uses hash of name).
virtual Long64_t GetEntries() const
Definition: TTree.h:392
A TTree object has a header with a name and a title.
Definition: TTree.h:98
unsigned char UChar_t
Definition: RtypesCore.h:34
A TTree is a list of TBranches.
Definition: TBranch.h:58