Logo ROOT   6.14/05
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 
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:481
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:256
return c1
Definition: legend1.C:41
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
int Int_t
Definition: RtypesCore.h:41
virtual void StartViewer()
Start the TTreeViewer on this tree.
Definition: TTree.cxx:8983
#define ClassDef(name, id)
Definition: Rtypes.h:320
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:533
auto * a
Definition: textangle.C:12
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
The Canvas class.
Definition: TCanvas.h:31
#define ClassImp(name)
Definition: Rtypes.h:359
double Double_t
Definition: RtypesCore.h:55
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:357
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
Mother of all ROOT objects.
Definition: TObject.h:37
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:1711
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1162
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:70