ROOT
6.30.04
Reference Guide
All
Namespaces
Files
Pages
tree0.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_tree
3
/// \notebook -nodraw
4
/// Simple Event class example
5
///
6
/// execute as: .x tree0.C++
7
///
8
/// You have to copy it first to a directory where you have write access!
9
/// Note that .x tree0.C cannot work with this example
10
///
11
/// ### Effect of ClassDef() and ClassImp() macros
12
///
13
/// After running this macro create an instance of Det and Event
14
///
15
/// ~~~
16
/// Det d;
17
/// Event e;
18
/// ~~~
19
///
20
/// now you can see the effect of the ClassDef() and ClassImp() macros.
21
/// (for the Det class these commands are commented!)
22
/// For instance 'e' now knows who it is:
23
///
24
/// ~~~
25
/// cout<<e.Class_Name()<<endl;
26
/// ~~~
27
///
28
/// whereas d does not.
29
///
30
/// The methods that are added by the ClassDef()/Imp() marcro can be listed with
31
///
32
/// ~~~
33
/// .class
34
/// .class Event
35
/// .class Det
36
/// ~~~
37
///
38
/// \macro_code
39
///
40
/// \author Heiko.Scheit@mpi-hd.mpg.de
41
42
#include <
TRandom.h
>
43
#include <
TTree.h
>
44
#include <
TCanvas.h
>
45
#include <
TStyle.h
>
46
47
#include <
Riostream.h
>
48
49
//class Det : public TObject {
50
class
Det {
// each detector gives an energy and time signal
51
public
:
52
Double_t e;
//energy
53
Double_t t;
//time
54
55
// ClassDef(Det,1)
56
};
57
58
//ClassImp(Det)
59
60
//class Event { //TObject is not required by this example
61
class
Event :
public
TObject {
62
public
:
63
64
Det a;
// say there are two detectors (a and b) in the experiment
65
Det b;
66
ClassDef(Event,1)
67
};
68
69
ClassImp(Event)
70
71
void tree0() {
72
// create a TTree
73
TTree *tree =
new
TTree(
"tree"
,
"treelibrated tree"
);
74
Event *e =
new
Event;
75
76
// create a branch with energy
77
tree->Branch(
"event"
,&e);
78
79
// fill some events with random numbers
80
Int_t nevent=10000;
81
for
(Int_t iev=0;iev<nevent;iev++) {
82
if
(iev%1000==0) cout<<
"Processing event "
<<iev<<
"..."
<<endl;
83
84
Float_t ea,eb;
85
gRandom->Rannor(ea,eb);
// the two energies follow a gaus distribution
86
e->a.e=ea;
87
e->b.e=eb;
88
e->a.t=gRandom->Rndm();
// random
89
e->b.t=e->a.t + gRandom->Gaus(0.,.1);
// identical to a.t but a gaussian
90
// 'resolution' was added with sigma .1
91
92
tree->Fill();
// fill the tree with the current event
93
}
94
95
// start the viewer
96
// here you can investigate the structure of your Event class
97
tree->StartViewer();
98
99
//gROOT->SetStyle("Plain"); // uncomment to set a different style
100
101
// now draw some tree variables
102
TCanvas *c1 =
new
TCanvas();
103
c1->Divide(2,2);
104
c1->cd(1);
105
tree->Draw(
"a.e"
);
//energy of det a
106
tree->Draw(
"a.e"
,
"3*(-.2<b.e && b.e<.2)"
,
"same"
);
// same but with condition on energy b; scaled by 3
107
c1->cd(2);
108
tree->Draw(
"b.e:a.e"
,
""
,
"colz"
);
// one energy against the other
109
c1->cd(3);
110
tree->Draw(
"b.t"
,
""
,
"e"
);
// time of b with errorbars
111
tree->Draw(
"a.t"
,
""
,
"same"
);
// overlay time of detector a
112
c1->cd(4);
113
tree->Draw(
"b.t:a.t"
);
// plot time b again time a
114
115
cout<<endl;
116
cout<<
"You can now examine the structure of your tree in the TreeViewer"
<<endl;
117
cout<<endl;
118
}
119
TTree.h
TCanvas.h
Riostream.h
TStyle.h
TRandom.h
tutorials
tree
tree0.C
Generated on Tue May 5 2020 14:03:51 for ROOT by
1.8.5