Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree103_tree.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 tree103_tree.C++
7///
8/// You have to copy it first to a directory where you have write access!
9/// Note that .x tree103_tree.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() macro 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 {
50class Det { // each detector gives an energy and time signal
51public:
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
61class Event : public TObject {
62public:
63 Det a; // say there are two detectors (a and b) in the experiment
64 Det b;
65
67};
68
70
71void tree103_tree()
72{
73 // create a TTree
74 auto tree = new TTree("tree", "treelibrated tree");
75 auto e = new Event;
76
77 // create a branch with energy
78 tree->Branch("event", &e);
79
80 // fill some events with random numbers
81 Int_t nevent = 10000;
82 for (Int_t iev=0; iev<nevent; iev++) {
83 if (iev % 1000 == 0)
84 std::cout << "Processing event " << iev << "..." << std::endl;
85
86 Float_t ea, eb;
87 gRandom->Rannor(ea, eb); // the two energies follow a gaus distribution
88 e->a.e = ea;
89 e->b.e = eb;
90 e->a.t = gRandom->Rndm(); // random
91 e->b.t = e->a.t + gRandom->Gaus(0., .1); // identical to a.t but a gaussian
92 // 'resolution' was added with sigma .1
93 tree->Fill(); // fill the tree with the current event
94 }
95
96 // start the viewer
97 // here you can investigate the structure of your Event class
98 tree->StartViewer();
99
100 //gROOT->SetStyle("Plain"); // uncomment to set a different style
101
102 // now draw some tree variables
103 auto c1 = new TCanvas();
104 c1->Divide(2, 2);
105 c1->cd(1);
106 tree->Draw("a.e"); //energy of det a
107 tree->Draw("a.e", "3*(-.2<b.e && b.e<.2)", "same"); // same but with condition on energy b; scaled by 3
108 c1->cd(2);
109 tree->Draw("b.e:a.e", "", "colz"); // one energy against the other
110 c1->cd(3);
111 tree->Draw("b.t", "", "e"); // time of b with errorbars
112 tree->Draw("a.t", "", "same"); // overlay time of detector a
113 c1->cd(4);
114 tree->Draw("b.t:a.t"); // plot time b again time a
115
116 std::cout << std::endl;
117 std::cout << "You can now examine the structure of your tree in the TreeViewer" << std::endl;
118 std::cout << std::endl;
119}
120
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
#define ClassImp(name)
Definition Rtypes.h:382
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
The Canvas class.
Definition TCanvas.h:23
Mother of all ROOT objects.
Definition TObject.h:41
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:275
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
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:507
A TTree represents a columnar dataset.
Definition TTree.h:79
return c1
Definition legend1.C:41