Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree106_tree.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook
4/// This example is the same as tree105_tree.C, but uses a class instead of a C-struct.
5/// In this example, we are mapping a class to one of the Geant3
6/// common blocks /gctrak/. In the real life, this common will be filled
7/// by Geant3 at each step and only the Tree Fill function should be called.
8/// The example emulates the Geant3 step routines.
9///
10/// to run the example, do to execute with native compiler:
11/// ~~~
12/// .x tree106_tree.C+
13/// ~~~
14///
15/// Note that since IO is involved, ACLiC has to be invoked to create the dictionary of class Gctrak.
16/// \macro_code
17///
18/// \author Rene Brun
19
20#include "TROOT.h"
21#include "TFile.h"
22#include "TTree.h"
23#include "TBrowser.h"
24#include "TH2.h"
25#include "TMath.h"
26#include "TRandom.h"
27#include "TCanvas.h"
28
29const Int_t MAXMEC = 30;
30
31class Gctrak : public TObject {
32public:
33 Float_t vect[7];
36 Float_t vout[7]; //! not persistent
37 Int_t nmec;
38 Int_t *lmec; //[nmec]
39 Int_t *namec; //[nmec]
40 Int_t nstep; //! not persistent
41 Int_t pid;
43 Float_t destel; //! not persistent
44 Float_t safety; //! not persistent
45 Float_t sleng; //! not persistent
46 Float_t step; //! not persistent
47 Float_t snext; //! not persistent
48 Float_t sfield; //! not persistent
49 Float_t tofg; //! not persistent
50 Float_t gekrat; //! not persistent
51 Float_t upwght; //! not persistent
52
53 Gctrak() {
54 lmec = nullptr;
55 namec = nullptr;
56 }
57
59};
60
61
63{
64 // extrapolate track in constant field
65 Float_t field = 20; // magnetic field in kilogauss
66 enum Evect {kX, kY, kZ, kPX, kPY, kPZ, kPP};
67 vout[kPP] = vect[kPP];
68 Float_t h4 = field * 2.99792e-4;
69 Float_t rho = -h4/vect[kPP];
70 Float_t tet = rho * step;
71 Float_t tsint = tet * tet / 6;
72 Float_t sintt = 1 - tsint;
74 Float_t cos1t = tet / 2;
75 Float_t f1 = step * sintt;
76 Float_t f2 = step * cos1t;
77 Float_t f3 = step * tsint * vect[kPZ];
78 Float_t f4 = -tet * cos1t;
79 Float_t f5 = sint;
80 Float_t f6 = tet * cos1t * vect[kPZ];
81 vout[kX] = vect[kX] + (f1*vect[kPX] - f2*vect[kPY]);
82 vout[kY] = vect[kY] + (f1*vect[kPY] + f2*vect[kPX]);
83 vout[kZ] = vect[kZ] + (f1*vect[kPZ] + f3);
84 vout[kPX] = vect[kPX] + (f4*vect[kPX] - f5*vect[kPY]);
85 vout[kPY] = vect[kPY] + (f4*vect[kPY] + f5*vect[kPX]);
86 vout[kPZ] = vect[kPZ] + (f4*vect[kPZ] + f6);
87}
88
89void tree106_write()
90{
91 // create a Tree file tree106.root
92
93 // create the file, the Tree and a few branches with a subset of gctrak
94 TFile f("tree106.root", "recreate");
95 TTree t2("t2", "a Tree with data from a fake Geant3");
96 auto gstep = new Gctrak;
97 t2.Branch("track", &gstep, 8000, 1);
98
99 // initialize particle parameters at first point
100 Float_t px, py, pz, p, charge=0;
101 Float_t vout[7];
102 Float_t mass = 0.137;
104 gstep->lmec = new Int_t[MAXMEC];
105 gstep->namec = new Int_t[MAXMEC];
106 gstep->step = 0.1;
107 gstep->destep = 0;
108 gstep->nmec = 0;
109 gstep->pid = 0;
110
111 // transport particles
112 for (Int_t i=0;i <10000; i++) {
113 // generate a new particle if necessary
114 if (newParticle) {
115 px = gRandom->Gaus(0, .02);
116 py = gRandom->Gaus(0, .02);
117 pz = gRandom->Gaus(0, .02);
118 p = TMath::Sqrt(px * px + py * py + pz * pz);
119 charge = 1;
120 if (gRandom->Rndm() < 0.5)
121 charge = -1;
122 gstep->pid += 1;
123 gstep->vect[0] = 0;
124 gstep->vect[1] = 0;
125 gstep->vect[2] = 0;
126 gstep->vect[3] = px / p;
127 gstep->vect[4] = py / p;
128 gstep->vect[5] = pz / p;
129 gstep->vect[6] = p*charge;
130 gstep->getot = TMath::Sqrt(p * p + mass * mass);
131 gstep->gekin = gstep->getot - mass;
133 }
134
135 // fill the Tree with current step parameters
136 t2.Fill();
137
138 // transport particle in magnetic field
139 helixStep(gstep->step, gstep->vect, vout); // make one step
140
141 //apply energy loss
142 gstep->destep = gstep->step*gRandom->Gaus(0.0002, 0.00001);
143 gstep->gekin -= gstep->destep;
144 gstep->getot = gstep->gekin + mass;
145 gstep->vect[6] = charge * TMath::Sqrt(gstep->getot * gstep->getot - mass * mass);
146 gstep->vect[0] = vout[0];
147 gstep->vect[1] = vout[1];
148 gstep->vect[2] = vout[2];
149 gstep->vect[3] = vout[3];
150 gstep->vect[4] = vout[4];
151 gstep->vect[5] = vout[5];
152 gstep->nmec = (Int_t)(5 * gRandom->Rndm());
153 for (Int_t l=0; l<gstep->nmec; l++) {
154 gstep->lmec[l] = l;
155 gstep->namec[l] = l + 100;
156 }
157 if (gstep->gekin < 0.001)
159 if (TMath::Abs(gstep->vect[2]) > 30)
161 }
162
163 // save the Tree header. The file will be automatically closed
164 // when going out of the function scope
165 t2.Write();
166}
167
168void tree106_read()
169{
170 // read the Tree generated by tree2w and fill one histogram
171 // we are only interested by the destep branch.
172
173 // note that we create the TFile and TTree objects on the heap
174 // because we want to keep these objects alive when we leave
175 // this function.
176 auto f = TFile::Open("tree106.root");
177 auto t2 = f->Get<TTree>("t2");
178 Gctrak *gstep = nullptr;
179 t2->SetBranchAddress("track", &gstep);
180 auto b_destep = t2->GetBranch("destep");
181
182 // create one histogram
183 auto hdestep = new TH1F("hdestep", "destep in Mev", 100, 1e-5, 3e-5);
184
185 // read only the destep branch for all entries
186 Long64_t nentries = t2->GetEntries();
187 for (Long64_t i=0; i<nentries; i++) {
188 b_destep->GetEntry(i);
189 hdestep->Fill(gstep->destep);
190 }
191
192 // we do not close the file
193 // we want to keep the generated histograms
194 // we fill a 3-d scatter plot with the particle step coordinates
195 auto c1 = new TCanvas("c1", "c1", 600, 800);
196 c1->SetFillColor(42);
197 c1->Divide(1, 2);
198 c1->cd(1);
199 hdestep->SetFillColor(45);
200 hdestep->Fit("gaus");
201 c1->cd(2);
202 gPad->SetFillColor(37);
203 t2->SetMarkerColor(kRed);
204 t2->Draw("vect[0]:vect[1]:vect[2]");
205 if (gROOT->IsBatch())
206 return;
207
208 // invoke the x3d viewer
209 gPad->GetViewer3D("ogl");
210}
211
212void tree106_tree()
213{
215 tree106_read();
216}
#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
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
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
#define gROOT
Definition TROOT.h:414
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gPad
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
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
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