ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tree0.C
Go to the documentation of this file.
1 // Author: Heiko.Scheit@mpi-hd.mpg.de
2 //
3 // simple Event class example
4 //
5 // execute as: .x tree0.C++
6 //
7 // You have to copy it first to a directory where you have write access!
8 // Note that .x tree0.C cannot work with this example
9 //
10 //
11 ///////////////////////////////
12 // Effect of ClassDef() and ClassImp() macros
13 //===============================================
14 //
15 // After running this macro create an instance of Det and Event
16 //
17 // Det d;
18 // Event e;
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 // cout<<e.Class_Name()<<endl;
25 //
26 // whereas d does not.
27 //
28 // The methods that are added by the ClassDef()/Imp() marcro can be listed with
29 // .class
30 // .class Event
31 // .class Det
32 ///////////////////
33 
34 #include <TRandom.h>
35 #include <TTree.h>
36 #include <TCanvas.h>
37 #include <TStyle.h>
38 
39 #include <Riostream.h>
40 
41 //class Det : public TObject {
42 class Det { // each detector gives an energy and time signal
43 public:
44  Double_t e; //energy
45  Double_t t; //time
46 
47 // ClassDef(Det,1)
48 };
49 
50 //ClassImp(Det)
51 
52 //class Event { //TObject is not required by this example
53 class Event : public TObject {
54 public:
55 
56  Det a; // say there are two detectors (a and b) in the experiment
57  Det b;
58  ClassDef(Event,1)
59 };
60 
61 ClassImp(Event)
62 
63 void tree0() {
64  // create a TTree
65  TTree *tree = new TTree("tree","treelibrated tree");
66  Event *e = new Event;
67 
68  // create a branch with energy
69  tree->Branch("event",&e);
70 
71  // fill some events with random numbers
72  Int_t nevent=10000;
73  for (Int_t iev=0;iev<nevent;iev++) {
74  if (iev%1000==0) cout<<"Processing event "<<iev<<"..."<<endl;
75 
76  Float_t ea,eb;
77  gRandom->Rannor(ea,eb); // the two energies follow a gaus distribution
78  e->a.e=ea;
79  e->b.e=eb;
80  e->a.t=gRandom->Rndm(); // random
81  e->b.t=e->a.t + gRandom->Gaus(0.,.1); // identical to a.t but a gaussian
82  // 'resolution' was added with sigma .1
83 
84  tree->Fill(); // fill the tree with the current event
85  }
86 
87  // start the viewer
88  // here you can investigate the structure of your Event class
89  tree->StartViewer();
90 
91  //gROOT->SetStyle("Plain"); // uncomment to set a different style
92  gStyle->SetPalette(1); // use precomputed color palette 1
93 
94  // now draw some tree variables
95  TCanvas *c1 = new TCanvas();
96  c1->Divide(2,2);
97  c1->cd(1);
98  tree->Draw("a.e"); //energy of det a
99  tree->Draw("a.e","3*(-.2<b.e && b.e<.2)","same"); // same but with condition on energy b; scaled by 3
100  c1->cd(2);
101  tree->Draw("b.e:a.e","","colz"); // one energy against the other
102  c1->cd(3);
103  tree->Draw("b.t","","e"); // time of b with errorbars
104  tree->Draw("a.t","","same"); // overlay time of detector a
105  c1->cd(4);
106  tree->Draw("b.t:a.t"); // plot time b again time a
107 
108  cout<<endl;
109  cout<<"You can now examine the structure of your tree in the TreeViewer"<<endl;
110  cout<<endl;
111 }
112 
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
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
TCanvas * c1
Definition: legend1.C:2
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4306
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
int Int_t
Definition: RtypesCore.h:41
TArc * a
Definition: textangle.C:12
ClassImp(Event)
Definition: tree0.C:61
virtual void StartViewer()
Start the TTreeViewer on this tree.
Definition: TTree.cxx:8473
#define ClassDef(name, id)
Definition: Rtypes.h:254
TThread * t[5]
Definition: threadsh1.C:13
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
The Canvas class.
Definition: TCanvas.h:48
tuple tree
Definition: tree.py:24
double Double_t
Definition: RtypesCore.h:55
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:360
Mother of all ROOT objects.
Definition: TObject.h:58
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
typedef void((*Func_t)())
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:1073
A TTree object has a header with a name and a title.
Definition: TTree.h:98
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1445