Logo ROOT   6.07/09
Reference Guide
tree2a.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook
4 /// This example is the same as tree2.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 tree2a.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 
29 const Int_t MAXMEC = 30;
30 
31 class Gctrak : public TObject {
32 public:
33  Float_t vect[7];
34  Float_t getot;
35  Float_t gekin;
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;
42  Float_t destep;
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() {lmec=0; namec=0;}
54 
55  ClassDef(Gctrak,1)
56 };
57 
58 
59 void helixStep(Float_t step, Float_t *vect, Float_t *vout)
60 {
61  // extrapolate track in constant field
62  Float_t field = 20; //magnetic field in kilogauss
63  enum Evect {kX,kY,kZ,kPX,kPY,kPZ,kPP};
64  vout[kPP] = vect[kPP];
65  Float_t h4 = field*2.99792e-4;
66  Float_t rho = -h4/vect[kPP];
67  Float_t tet = rho*step;
68  Float_t tsint = tet*tet/6;
69  Float_t sintt = 1 - tsint;
70  Float_t sint = tet*sintt;
71  Float_t cos1t = tet/2;
72  Float_t f1 = step*sintt;
73  Float_t f2 = step*cos1t;
74  Float_t f3 = step*tsint*vect[kPZ];
75  Float_t f4 = -tet*cos1t;
76  Float_t f5 = sint;
77  Float_t f6 = tet*cos1t*vect[kPZ];
78  vout[kX] = vect[kX] + (f1*vect[kPX] - f2*vect[kPY]);
79  vout[kY] = vect[kY] + (f1*vect[kPY] + f2*vect[kPX]);
80  vout[kZ] = vect[kZ] + (f1*vect[kPZ] + f3);
81  vout[kPX] = vect[kPX] + (f4*vect[kPX] - f5*vect[kPY]);
82  vout[kPY] = vect[kPY] + (f4*vect[kPY] + f5*vect[kPX]);
83  vout[kPZ] = vect[kPZ] + (f4*vect[kPZ] + f6);
84 }
85 
86 void tree2aw()
87 {
88  //create a Tree file tree2.root
89 
90  //create the file, the Tree and a few branches with
91  //a subset of gctrak
92  TFile f("tree2.root","recreate");
93  TTree t2("t2","a Tree with data from a fake Geant3");
94  Gctrak *gstep = new Gctrak;
95  t2.Branch("track",&gstep,8000,1);
96 
97  //Initialize particle parameters at first point
98  Float_t px,py,pz,p,charge=0;
99  Float_t vout[7];
100  Float_t mass = 0.137;
101  Bool_t newParticle = kTRUE;
102  gstep->lmec = new Int_t[MAXMEC];
103  gstep->namec = new Int_t[MAXMEC];
104  gstep->step = 0.1;
105  gstep->destep = 0;
106  gstep->nmec = 0;
107  gstep->pid = 0;
108 
109  //transport particles
110  for (Int_t i=0;i<10000;i++) {
111  //generate a new particle if necessary
112  if (newParticle) {
113  px = gRandom->Gaus(0,.02);
114  py = gRandom->Gaus(0,.02);
115  pz = gRandom->Gaus(0,.02);
116  p = TMath::Sqrt(px*px+py*py+pz*pz);
117  charge = 1; if (gRandom->Rndm() < 0.5) charge = -1;
118  gstep->pid += 1;
119  gstep->vect[0] = 0;
120  gstep->vect[1] = 0;
121  gstep->vect[2] = 0;
122  gstep->vect[3] = px/p;
123  gstep->vect[4] = py/p;
124  gstep->vect[5] = pz/p;
125  gstep->vect[6] = p*charge;
126  gstep->getot = TMath::Sqrt(p*p + mass*mass);
127  gstep->gekin = gstep->getot - mass;
128  newParticle = kFALSE;
129  }
130 
131  // fill the Tree with current step parameters
132  t2.Fill();
133 
134  //transport particle in magnetic field
135  helixStep(gstep->step, gstep->vect, vout); //make one step
136 
137  //apply energy loss
138  gstep->destep = gstep->step*gRandom->Gaus(0.0002,0.00001);
139  gstep->gekin -= gstep->destep;
140  gstep->getot = gstep->gekin + mass;
141  gstep->vect[6] = charge*TMath::Sqrt(gstep->getot*gstep->getot - mass*mass);
142  gstep->vect[0] = vout[0];
143  gstep->vect[1] = vout[1];
144  gstep->vect[2] = vout[2];
145  gstep->vect[3] = vout[3];
146  gstep->vect[4] = vout[4];
147  gstep->vect[5] = vout[5];
148  gstep->nmec = (Int_t)(5*gRandom->Rndm());
149  for (Int_t l=0;l<gstep->nmec;l++) {
150  gstep->lmec[l] = l;
151  gstep->namec[l] = l+100;
152  }
153  if (gstep->gekin < 0.001) newParticle = kTRUE;
154  if (TMath::Abs(gstep->vect[2]) > 30) newParticle = kTRUE;
155  }
156 
157  //save the Tree header. The file will be automatically closed
158  //when going out of the function scope
159  t2.Write();
160 }
161 
162 void tree2ar()
163 {
164  //read the Tree generated by tree2w and fill one histogram
165  //we are only interested by the destep branch.
166 
167  //note that we use "new" to create the TFile and TTree objects !
168  //because we want to keep these objects alive when we leave
169  //this function.
170  TFile *f = new TFile("tree2.root");
171  TTree *t2 = (TTree*)f->Get("t2");
172  Gctrak *gstep = 0;
173  t2->SetBranchAddress("track",&gstep);
174  TBranch *b_destep = t2->GetBranch("destep");
175 
176  //create one histogram
177  TH1F *hdestep = new TH1F("hdestep","destep in Mev",100,1e-5,3e-5);
178 
179  //read only the destep branch for all entries
180  Long64_t nentries = t2->GetEntries();
181  for (Long64_t i=0;i<nentries;i++) {
182  b_destep->GetEntry(i);
183  hdestep->Fill(gstep->destep);
184  }
185 
186  //we do not close the file.
187  //We want to keep the generated histograms
188  //We fill a 3-d scatter plot with the particle step coordinates
189  TCanvas *c1 = new TCanvas("c1","c1",600,800);
190  c1->SetFillColor(42);
191  c1->Divide(1,2);
192  c1->cd(1);
193  hdestep->SetFillColor(45);
194  hdestep->Fit("gaus");
195  c1->cd(2);
196  gPad->SetFillColor(37);
197  t2->SetMarkerColor(kRed);
198  t2->Draw("vect[0]:vect[1]:vect[2]");
199  if (gROOT->IsBatch()) return;
200 
201  // invoke the x3d viewer
202  gPad->GetViewer3D("x3d");
203 }
204 
205 void tree2a() {
206  tree2aw();
207  tree2ar();
208 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3127
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
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
return c1
Definition: legend1.C:41
Definition: Rtypes.h:61
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
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:364
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7719
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:43
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4868
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
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:1217
R__EXTERN TRandom * gRandom
Definition: TRandom.h:66
The Canvas class.
Definition: TCanvas.h:41
double f(double x)
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:366
int nentries
Definition: THbookFile.cxx:89
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
#define snext(osub1, osub2)
Definition: triangle.c:1167
Mother of all ROOT objects.
Definition: TObject.h:44
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:1089
double f2(const double *x)
TF1 * f1
Definition: legend1.C:11
#define gPad
Definition: TVirtualPad.h:289
virtual Long64_t GetEntries() const
Definition: TTree.h:392
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:3565