Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree105_tree.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook
4/// This example illustrates how to make a Tree from variables or arrays
5/// in a C struct - without a dictionary, by creating the branches for
6/// builtin types (int, float, double) and arrays explicitly.
7/// See tree106_tree.C for the same example using a class with dictionary
8/// instead of a C-struct.
9///
10/// In this example, we are mapping a C struct to one of the Geant3
11/// common blocks /gctrak/. In the real life, this common will be filled
12/// by Geant3 at each step and only the Tree Fill function should be called.
13/// The example emulates the Geant3 step routines.
14///
15/// to run the example, do:
16/// ~~~
17/// .x tree105_tree.C to execute with the Cling interpreter
18/// .x tree105_tree.C++ to execute with native compiler
19/// ~~~
20/// \macro_code
21///
22/// \author Rene Brun
23
24#include "TFile.h"
25#include "TTree.h"
26#include "TH2.h"
27#include "TRandom.h"
28#include "TCanvas.h"
29#include "TMath.h"
30
31const Int_t MAXMEC = 30;
32
33typedef struct {
34 Float_t vect[7];
37 Float_t vout[7];
38 Int_t nmec;
42 Int_t pid;
47 Float_t step;
53} Gctrak_t;
54
55
57{
58 // extrapolate track in constant field
59 Float_t field = 20; // magnetic field in kilogauss
60 enum Evect {kX, kY, kZ, kPX, kPY, kPZ, kPP};
61 vout[kPP] = vect[kPP];
62 Float_t h4 = field*2.99792e-4;
63 Float_t rho = -h4/vect[kPP];
64 Float_t tet = rho*step;
65 Float_t tsint = tet*tet/6;
66 Float_t sintt = 1 - tsint;
68 Float_t cos1t = tet/2;
69 Float_t f1 = step*sintt;
70 Float_t f2 = step*cos1t;
71 Float_t f3 = step*tsint*vect[kPZ];
72 Float_t f4 = -tet*cos1t;
73 Float_t f5 = sint;
74 Float_t f6 = tet*cos1t*vect[kPZ];
75 vout[kX] = vect[kX] + (f1*vect[kPX] - f2*vect[kPY]);
76 vout[kY] = vect[kY] + (f1*vect[kPY] + f2*vect[kPX]);
77 vout[kZ] = vect[kZ] + (f1*vect[kPZ] + f3);
78 vout[kPX] = vect[kPX] + (f4*vect[kPX] - f5*vect[kPY]);
79 vout[kPY] = vect[kPY] + (f4*vect[kPY] + f5*vect[kPX]);
80 vout[kPZ] = vect[kPZ] + (f4*vect[kPZ] + f6);
81}
82
83void tree105_write()
84{
85 // create a Tree file tree105.root
86
87 // create the file, the Tree and a few branches with
88 // a subset of gctrak
89 TFile f("tree105.root", "recreate");
90 TTree t2("t2", "a Tree with data from a fake Geant3");
92 t2.Branch("vect", gstep.vect, "vect[7]/F");
93 t2.Branch("getot", &gstep.getot);
94 t2.Branch("gekin", &gstep.gekin);
95 t2.Branch("nmec", &gstep.nmec);
96 t2.Branch("lmec", gstep.lmec, "lmec[nmec]/I");
97 t2.Branch("destep", &gstep.destep);
98 t2.Branch("pid", &gstep.pid);
99
100 // Initialize particle parameters at first point
101 Float_t px, py, pz, p, charge = 0;
102 Float_t vout[7];
103 Float_t mass = 0.137;
105 gstep.step = 0.1;
106 gstep.destep = 0;
107 gstep.nmec = 0;
108 gstep.pid = 0;
109
110 // transport particles
111 for (Int_t i=0; i<10000; i++) {
112 // generate a new particle if necessary
113 if (newParticle) {
114 px = gRandom->Gaus(0, .02);
115 py = gRandom->Gaus(0, .02);
116 pz = gRandom->Gaus(0, .02);
117 p = TMath::Sqrt(px * px + py *py + pz * pz);
118 charge = 1;
119 if (gRandom->Rndm() < 0.5)
120 charge = -1;
121 gstep.pid += 1;
122 gstep.vect[0] = 0;
123 gstep.vect[1] = 0;
124 gstep.vect[2] = 0;
125 gstep.vect[3] = px / p;
126 gstep.vect[4] = py / p;
127 gstep.vect[5] = pz / p;
128 gstep.vect[6] = p * charge;
129 gstep.getot = TMath::Sqrt(p * p + mass * mass);
130 gstep.gekin = gstep.getot - mass;
132 }
133
134 // fill the Tree with current step parameters
135 t2.Fill();
136
137 // transport particle in magnetic field
138 helixStep(gstep.step, gstep.vect, vout); // make one step
139
140 // apply energy loss
141 gstep.destep = gstep.step*gRandom->Gaus(0.0002, 0.00001);
142 gstep.gekin -= gstep.destep;
143 gstep.getot = gstep.gekin + mass;
144 gstep.vect[6] = charge*TMath::Sqrt(gstep.getot * gstep.getot - mass * mass);
145 gstep.vect[0] = vout[0];
146 gstep.vect[1] = vout[1];
147 gstep.vect[2] = vout[2];
148 gstep.vect[3] = vout[3];
149 gstep.vect[4] = vout[4];
150 gstep.vect[5] = vout[5];
151 gstep.nmec = (Int_t)(5 * gRandom->Rndm());
152 for (Int_t l=0; l<gstep.nmec; l++)
153 gstep.lmec[l] = l;
154 if (gstep.gekin < 0.001)
156 if (TMath::Abs(gstep.vect[2]) > 30)
158 }
159 // save the Tree header. The file will be automatically closed
160 // when going out of the function scope
161 t2.Write();
162}
163
164void tree105_read()
165{
166 // read the Tree generated by tree105_write and fill one histogram
167 // we are only interested by the destep branch.
168
169 // note that we create the TFile and TTree objects on the heap
170 // because we want to keep these objects alive when we leave
171 // this function.
172 auto f = TFile::Open("tree105.root");
173 auto t2 = f->Get<TTree>("t2");
174 static Float_t destep;
175 TBranch *b_destep = t2->GetBranch("destep");
176 b_destep->SetAddress(&destep);
177
178 // create one histogram
179 auto hdestep = new TH1F("hdestep", "destep in Mev", 100, 1e-5, 3e-5);
180
181 // read only the destep branch for all entries
182 Long64_t nentries = t2->GetEntries();
183 for (Long64_t i=0; i<nentries; i++) {
184 b_destep->GetEntry(i);
185 hdestep->Fill(destep);
186 }
187
188 // we do not close the file
189 // we want to keep the generated histograms
190 // we fill a 3-d scatter plot with the particle step coordinates
191 auto c1 = new TCanvas("c1", "c1", 600, 800);
192 c1->SetFillColor(42);
193 c1->Divide(1, 2);
194 c1->cd(1);
195 hdestep->SetFillColor(45);
196 hdestep->Fit("gaus");
197 c1->cd(2);
198 gPad->SetFillColor(37);
199 t2->SetMarkerColor(kRed);
200 t2->Draw("vect[0]:vect[1]:vect[2]");
201
202 // Allow to use the TTree after the end of the function.
203 t2->ResetBranchAddresses();
204}
205
206void tree105_tree()
207{
209 tree105_read();
210}
#define f(i)
Definition RSha256.hxx:104
#define e(i)
Definition RSha256.hxx:103
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
long long Long64_t
Definition RtypesCore.h:69
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
@ kRed
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
int nentries
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gPad
A TTree is a list of TBranches.
Definition TBranch.h:93
The Canvas class.
Definition TCanvas.h:23
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4131
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:877
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
A TTree represents a columnar dataset.
Definition TTree.h:84
return c1
Definition legend1.C:41
TF1 * f1
Definition legend1.C:11
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
TLine l
Definition textangle.C:4