ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tree2.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// This example illustrates how to make a Tree from variables or arrays
4 /// in a C struct. **Use of C structs is strongly discouraged and one should
5 /// use classes instead**. However support for C structs is important for
6 /// legacy applications written in C or Fortran.
7 /// See tree2a.C for the same example using a class instead of a C-struct.
8 ///
9 /// In this example, we are mapping a C struct to one of the Geant3
10 /// common blocks /gctrak/. In the real life, this common will be filled
11 /// by Geant3 at each step and only the Tree Fill function should be called.
12 /// The example emulates the Geant3 step routines.
13 ///
14 /// to run the example, do:
15 /// ~~~
16 /// .x tree2.C to execute with the Cling interpreter
17 /// .x tree2.C++ to execute with native compiler
18 /// ~~~
19 /// \macro_code
20 ///
21 /// \author Rene Brun
22 
23 #include "TFile.h"
24 #include "TTree.h"
25 #include "TBrowser.h"
26 #include "TH2.h"
27 #include "TRandom.h"
28 #include "TCanvas.h"
29 #include "TMath.h"
30 #include "TROOT.h"
31 
32 const Int_t MAXMEC = 30;
33 
34 typedef struct {
35  Float_t vect[7];
36  Float_t getot;
37  Float_t gekin;
38  Float_t vout[7];
39  Int_t nmec;
40  Int_t lmec[MAXMEC];
41  Int_t namec[MAXMEC];
42  Int_t nstep;
43  Int_t pid;
44  Float_t destep;
45  Float_t destel;
46  Float_t safety;
47  Float_t sleng;
48  Float_t step;
49  Float_t snext;
50  Float_t sfield;
51  Float_t tofg;
52  Float_t gekrat;
53  Float_t upwght;
54 } Gctrak_t;
55 
56 
57 void helixStep(Float_t step, Float_t *vect, Float_t *vout)
58 {
59  // extrapolate track in constant field
60  Float_t field = 20; //magnetic field in kilogauss
61  enum Evect {kX,kY,kZ,kPX,kPY,kPZ,kPP};
62  vout[kPP] = vect[kPP];
63  Float_t h4 = field*2.99792e-4;
64  Float_t rho = -h4/vect[kPP];
65  Float_t tet = rho*step;
66  Float_t tsint = tet*tet/6;
67  Float_t sintt = 1 - tsint;
68  Float_t sint = tet*sintt;
69  Float_t cos1t = tet/2;
70  Float_t f1 = step*sintt;
71  Float_t f2 = step*cos1t;
72  Float_t f3 = step*tsint*vect[kPZ];
73  Float_t f4 = -tet*cos1t;
74  Float_t f5 = sint;
75  Float_t f6 = tet*cos1t*vect[kPZ];
76  vout[kX] = vect[kX] + (f1*vect[kPX] - f2*vect[kPY]);
77  vout[kY] = vect[kY] + (f1*vect[kPY] + f2*vect[kPX]);
78  vout[kZ] = vect[kZ] + (f1*vect[kPZ] + f3);
79  vout[kPX] = vect[kPX] + (f4*vect[kPX] - f5*vect[kPY]);
80  vout[kPY] = vect[kPY] + (f4*vect[kPY] + f5*vect[kPX]);
81  vout[kPZ] = vect[kPZ] + (f4*vect[kPZ] + f6);
82 }
83 
84 void tree2w()
85 {
86  //create a Tree file tree2.root
87 
88  //create the file, the Tree and a few branches with
89  //a subset of gctrak
90  TFile f("tree2.root","recreate");
91  TTree t2("t2","a Tree with data from a fake Geant3");
92  Gctrak_t gstep;
93  t2.Branch("vect",gstep.vect,"vect[7]/F");
94  t2.Branch("getot",&gstep.getot,"getot/F");
95  t2.Branch("gekin",&gstep.gekin,"gekin/F");
96  t2.Branch("nmec",&gstep.nmec,"nmec/I");
97  t2.Branch("lmec",gstep.lmec,"lmec[nmec]/I");
98  t2.Branch("destep",&gstep.destep,"destep/F");
99  t2.Branch("pid",&gstep.pid,"pid/I");
100 
101  //Initialize particle parameters at first point
102  Float_t px,py,pz,p,charge=0;
103  Float_t vout[7];
104  Float_t mass = 0.137;
105  Bool_t newParticle = kTRUE;
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; if (gRandom->Rndm() < 0.5) charge = -1;
120  gstep.pid += 1;
121  gstep.vect[0] = 0;
122  gstep.vect[1] = 0;
123  gstep.vect[2] = 0;
124  gstep.vect[3] = px/p;
125  gstep.vect[4] = py/p;
126  gstep.vect[5] = pz/p;
127  gstep.vect[6] = p*charge;
128  gstep.getot = TMath::Sqrt(p*p + mass*mass);
129  gstep.gekin = gstep.getot - mass;
130  newParticle = kFALSE;
131  }
132 
133  // fill the Tree with current step parameters
134  t2.Fill();
135 
136  //transport particle in magnetic field
137  helixStep(gstep.step, gstep.vect, vout); //make one step
138 
139  //apply energy loss
140  gstep.destep = gstep.step*gRandom->Gaus(0.0002,0.00001);
141  gstep.gekin -= gstep.destep;
142  gstep.getot = gstep.gekin + mass;
143  gstep.vect[6] = charge*TMath::Sqrt(gstep.getot*gstep.getot - mass*mass);
144  gstep.vect[0] = vout[0];
145  gstep.vect[1] = vout[1];
146  gstep.vect[2] = vout[2];
147  gstep.vect[3] = vout[3];
148  gstep.vect[4] = vout[4];
149  gstep.vect[5] = vout[5];
150  gstep.nmec = (Int_t)(5*gRandom->Rndm());
151  for (Int_t l=0;l<gstep.nmec;l++) gstep.lmec[l] = l;
152  if (gstep.gekin < 0.001) newParticle = kTRUE;
153  if (TMath::Abs(gstep.vect[2]) > 30) newParticle = kTRUE;
154  }
155 
156  //save the Tree header. The file will be automatically closed
157  //when going out of the function scope
158  t2.Write();
159 }
160 
161 void tree2r()
162 {
163  //read the Tree generated by tree2w and fill one histogram
164  //we are only interested by the destep branch.
165 
166  //note that we use "new" to create the TFile and TTree objects !
167  //because we want to keep these objects alive when we leave
168  //this function.
169  TFile *f = new TFile("tree2.root");
170  TTree *t2 = (TTree*)f->Get("t2");
171  static Float_t destep;
172  TBranch *b_destep = t2->GetBranch("destep");
173  b_destep->SetAddress(&destep);
174 
175  //create one histogram
176  TH1F *hdestep = new TH1F("hdestep","destep in Mev",100,1e-5,3e-5);
177 
178  //read only the destep branch for all entries
179  Long64_t nentries = t2->GetEntries();
180  for (Long64_t i=0;i<nentries;i++) {
181  b_destep->GetEntry(i);
182  hdestep->Fill(destep);
183  }
184 
185  //we do not close the file.
186  //We want to keep the generated histograms
187  //We fill a 3-d scatter plot with the particle step coordinates
188  TCanvas *c1 = new TCanvas("c1","c1",600,800);
189  c1->SetFillColor(42);
190  c1->Divide(1,2);
191  c1->cd(1);
192  hdestep->SetFillColor(45);
193  hdestep->Fit("gaus");
194  c1->cd(2);
195  gPad->SetFillColor(37);
196  t2->SetMarkerColor(kRed);
197  t2->Draw("vect[0]:vect[1]:vect[2]");
198  if (gROOT->IsBatch()) return;
199 
200  // invoke the x3d viewer
201  gPad->GetViewer3D("x3d");
202 }
203 
204 void tree2() {
205  tree2w();
206  tree2r();
207 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
Float_t pz
Definition: hprod.C:33
virtual void SetAddress(void *add)
Set address of this branch.
Definition: TBranch.cxx:2049
#define snext(osub1, osub2)
Definition: triangle.c:1167
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
void f4()
Definition: na49.C:60
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
void f6()
Definition: na49.C:122
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
Definition: Rtypes.h:61
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4306
tuple f2
Definition: surfaces.py:24
void f3()
Definition: na49.C:50
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:344
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Float_t py
Definition: hprod.C:33
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
TFile * f
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
if(pyself &&pyself!=Py_None)
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4803
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:8780
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
void f5()
Definition: na49.C:91
TLine * l
Definition: textangle.C:4
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition: TBranch.cxx:1199
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
TText * t2
Definition: rootenv.C:28
The Canvas class.
Definition: TCanvas.h:48
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:360
int nentries
Definition: THbookFile.cxx:89
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
Float_t px
Definition: hprod.C:33
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
TF1 * f1
Definition: legend1.C:11
#define gPad
Definition: TVirtualPad.h:288
virtual Long64_t GetEntries() const
Definition: TTree.h:386
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
A TTree is a list of TBranches.
Definition: TBranch.h:58
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3607