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() macro
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() macro.
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() 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//class Event { //TObject is not required by this example
59class Event : public TObject {
60public:
61 Det a; // say there are two detectors (a and b) in the experiment
62 Det b;
63
65};
66
67void tree103_tree()
68{
69 // create a TTree
71 auto e = new Event;
72
73 // create a branch with energy
74 tree->Branch("event", &e);
75
76 // fill some events with random numbers
77 Int_t nevent = 10000;
78 for (Int_t iev=0; iev<nevent; iev++) {
79 if (iev % 1000 == 0)
80 std::cout << "Processing event " << iev << "..." << std::endl;
81
82 Float_t ea, eb;
83 gRandom->Rannor(ea, eb); // the two energies follow a gaus distribution
84 e->a.e = ea;
85 e->b.e = eb;
86 e->a.t = gRandom->Rndm(); // random
87 e->b.t = e->a.t + gRandom->Gaus(0., .1); // identical to a.t but a gaussian
88 // 'resolution' was added with sigma .1
89 tree->Fill(); // fill the tree with the current event
90 }
91
92 // start the viewer
93 // here you can investigate the structure of your Event class
94 tree->StartViewer();
95
96 //gROOT->SetStyle("Plain"); // uncomment to set a different style
97
98 // now draw some tree variables
99 auto c1 = new TCanvas();
100 c1->Divide(2, 2);
101 c1->cd(1);
102 tree->Draw("a.e"); //energy of det a
103 tree->Draw("a.e", "3*(-.2<b.e && b.e<.2)", "same"); // same but with condition on energy b; scaled by 3
104 c1->cd(2);
105 tree->Draw("b.e:a.e", "", "colz"); // one energy against the other
106 c1->cd(3);
107 tree->Draw("b.t", "", "e"); // time of b with errorbars
108 tree->Draw("a.t", "", "same"); // overlay time of detector a
109 c1->cd(4);
110 tree->Draw("b.t:a.t"); // plot time b again time a
111
112 std::cout << std::endl;
113 std::cout << "You can now examine the structure of your tree in the TreeViewer" << std::endl;
114 std::cout << std::endl;
115}
116
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:274
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:558
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:506
A TTree represents a columnar dataset.
Definition TTree.h:89
return c1
Definition legend1.C:41