Logo ROOT  
Reference Guide
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 
TRandom::Gaus
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
e
#define e(i)
Definition: RSha256.hxx:103
tree
Definition: tree.py:1
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
TRandom.h
Float_t
float Float_t
Definition: RtypesCore.h:57
TStyle.h
Int_t
int Int_t
Definition: RtypesCore.h:45
TCanvas.h
TTree.h
b
#define b(i)
Definition: RSha256.hxx:100
TRandom::Rannor
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
a
auto * a
Definition: textangle.C:12
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
TRandom::Rndm
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:541
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
Riostream.h
c1
return c1
Definition: legend1.C:41