ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tree4.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// This example writes a tree with objects of the class Event.
4 /// It is a simplified version of $ROOTSYS/test/MainEvent.cxx to
5 /// write the tree, and $ROOTSYS/test/eventb.C
6 /// It shows:
7 /// - how to fill a Tree with an event class containing these data members:
8 /// ~~~
9 /// char fType[20];
10 /// Int_t fNtrack;
11 /// Int_t fNseg;
12 /// Int_t fNvertex;
13 /// UInt_t fFlag;
14 /// Float_t fTemperature;
15 /// EventHeader fEvtHdr;
16 /// TClonesArray *fTracks; //->
17 /// TH1F *fH; //->
18 /// Int_t fMeasures[10];
19 /// Float_t fMatrix[4][4];
20 /// Float_t *fClosestDistance; //[fNvertex]
21 /// ~~~
22 /// - the difference in splitting or not splitting a branch
23 /// - how to read selected branches of the tree, and print the first entry with less than 587 tracks.
24 /// - how to browse and analyze the Tree via the TBrowser and TTreeViewer
25 ///
26 /// This example can be run in many different ways:
27 /// - way1 using the Cling interpreter:
28 /// ~~~
29 /// .x tree4.C
30 /// ~~~
31 /// - way2 using the Cling interpreter:
32 /// ~~~
33 /// .L tree4.C
34 /// tree4()
35 /// ~~~
36 /// - way3 using ACLIC:
37 /// ~~~
38 /// .L ../test/libEvent.so
39 /// .x tree4.C++
40 /// ~~~
41 /// One can also run the write and read parts in two separate sessions.
42 /// For example following one of the sessions above, one can start the session:
43 /// ~~~
44 /// .L tree4.C
45 /// tree4r();
46 /// ~~~
47 /// \macro_code
48 ///
49 /// \author Rene Brun
50 #include "TFile.h"
51 #include "TTree.h"
52 #include "TBrowser.h"
53 #include "TH2.h"
54 #include "TRandom.h"
55 #include "TClassTable.h"
56 #include "TSystem.h"
57 #include "TROOT.h"
58 #include "../test/Event.h"
59 
60 void tree4w()
61 {
62 
63  //create a Tree file tree4.root
64  TFile f("tree4.root","RECREATE");
65 
66  // Create a ROOT Tree
67  TTree t4("t4","A Tree with Events");
68 
69  // Create a pointer to an Event object
70  Event *event = new Event();
71 
72  // Create two branches, split one.
73  t4.Branch("event_split", &event,16000,99);
74  t4.Branch("event_not_split", &event,16000,0);
75 
76  // a local variable for the event type
77  char etype[20];
78 
79  // Fill the tree
80  for (Int_t ev = 0; ev <100; ev++) {
81  Float_t sigmat, sigmas;
82  gRandom->Rannor(sigmat,sigmas);
83  Int_t ntrack = Int_t(600 + 600 *sigmat/120.);
84  Float_t random = gRandom->Rndm(1);
85  sprintf(etype,"type%d",ev%5);
86  event->SetType(etype);
87  event->SetHeader(ev, 200, 960312, random);
88  event->SetNseg(Int_t(10*ntrack+20*sigmas));
89  event->SetNvertex(Int_t(1+20*gRandom->Rndm()));
90  event->SetFlag(UInt_t(random+0.5));
91  event->SetTemperature(random+20.);
92 
93  for(UChar_t m = 0; m < 10; m++) {
94  event->SetMeasure(m, Int_t(gRandom->Gaus(m,m+1)));
95  }
96 
97  // fill the matrix
98  for(UChar_t i0 = 0; i0 < 4; i0++) {
99  for(UChar_t i1 = 0; i1 < 4; i1++) {
100  event->SetMatrix(i0,i1,gRandom->Gaus(i0*i1,1));
101  }
102  }
103 
104  // Create and fill the Track objects
105  for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random);
106 
107  // Fill the tree
108  t4.Fill();
109 
110  // Clear the event before reloading it
111  event->Clear();
112  }
113 
114  // Write the file header
115  f.Write();
116 
117  // Print the tree contents
118  t4.Print();
119 }
120 
121 
122 void tree4r()
123 {
124  // check to see if the event class is in the dictionary
125  // if it is not load the definition in libEvent.so
126  if (!TClassTable::GetDict("Event")) {
127  gSystem->Load("$ROOTSYS/test/libEvent");
128  }
129 
130  // read the tree generated with tree4w
131 
132  //note that we use "new" to create the TFile and TTree objects !
133  //because we want to keep these objects alive when we leave this function.
134  TFile *f = new TFile("tree4.root");
135  TTree *t4 = (TTree*)f->Get("t4");
136 
137  // create a pointer to an event object. This will be used
138  // to read the branch values.
139  Event *event = new Event();
140 
141  // get two branches and set the branch address
142  TBranch *bntrack = t4->GetBranch("fNtrack");
143  TBranch *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)continue;
155 
156  //read complete accepted event in memory
157  nb += t4->GetEntry(i);
158  nselected++;
159 
160  //print the first accepted event
161  if (nselected == 1) t4->Show();
162 
163  //clear tracks array
164  event->Clear();
165  }
166 
167  if (gROOT->IsBatch()) return;
168  new TBrowser();
169  t4->StartViewer();
170 }
171 
172 void tree4() {
173  Event::Reset(); // Allow for re-run this script by cleaning static variables.
174  tree4w();
175  Event::Reset(); // Allow for re-run this script by cleaning static variables.
176  tree4r();
177 }
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:2049
long long Long64_t
Definition: RtypesCore.h:69
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 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
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
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1766
int Int_t
Definition: RtypesCore.h:41
TFile * f
virtual void StartViewer()
Start the TTreeViewer on this tree.
Definition: TTree.cxx:8473
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6495
virtual void Show(Long64_t entry=-1, Int_t lenmax=20)
Print values of all active leaves for entry.
Definition: TTree.cxx:8399
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4803
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
TThread * t[5]
Definition: threadsh1.C:13
TText * t4
Definition: rootenv.C:45
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2248
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:1199
void Reset(Detail::TBranchProxy *x)
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
static DictFuncPtr_t GetDict(const char *cname)
Given the class name returns the Dictionary() function of a class (uses hash of name).
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
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
unsigned char UChar_t
Definition: RtypesCore.h:34
A TTree is a list of TBranches.
Definition: TBranch.h:58