ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tree2a.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// This example is the same as tree2.C, but uses a class instead of a C-struct.
4 /// In this example, we are mapping a class to one of the Geant3
5 /// common blocks /gctrak/. In the real life, this common will be filled
6 /// by Geant3 at each step and only the Tree Fill function should be called.
7 /// The example emulates the Geant3 step routines.
8 ///
9 /// to run the example, do to execute with native compiler:
10 /// ~~~
11 /// .x tree2a.C+
12 /// ~~~
13 ///
14 /// Note that since IO is involved, ACLiC has to be invoked to create the dictionary of class Gctrak.
15 /// \macro_code
16 ///
17 /// \author Rene Brun
18 
19 #include "TROOT.h"
20 #include "TFile.h"
21 #include "TTree.h"
22 #include "TBrowser.h"
23 #include "TH2.h"
24 #include "TMath.h"
25 #include "TRandom.h"
26 #include "TCanvas.h"
27 
28 const Int_t MAXMEC = 30;
29 
30 class Gctrak : public TObject {
31 public:
32  Float_t vect[7];
33  Float_t getot;
34  Float_t gekin;
35  Float_t vout[7]; //! not persistent
36  Int_t nmec;
37  Int_t *lmec; //[nmec]
38  Int_t *namec; //[nmec]
39  Int_t nstep; //! not persistent
40  Int_t pid;
41  Float_t destep;
42  Float_t destel; //! not persistent
43  Float_t safety; //! not persistent
44  Float_t sleng; //! not persistent
45  Float_t step; //! not persistent
46  Float_t snext; //! not persistent
47  Float_t sfield; //! not persistent
48  Float_t tofg; //! not persistent
49  Float_t gekrat; //! not persistent
50  Float_t upwght; //! not persistent
51 
52  Gctrak() {lmec=0; namec=0;}
53 
54  ClassDef(Gctrak,1)
55 };
56 
57 
58 void helixStep(Float_t step, Float_t *vect, Float_t *vout)
59 {
60  // extrapolate track in constant field
61  Float_t field = 20; //magnetic field in kilogauss
62  enum Evect {kX,kY,kZ,kPX,kPY,kPZ,kPP};
63  vout[kPP] = vect[kPP];
64  Float_t h4 = field*2.99792e-4;
65  Float_t rho = -h4/vect[kPP];
66  Float_t tet = rho*step;
67  Float_t tsint = tet*tet/6;
68  Float_t sintt = 1 - tsint;
69  Float_t sint = tet*sintt;
70  Float_t cos1t = tet/2;
71  Float_t f1 = step*sintt;
72  Float_t f2 = step*cos1t;
73  Float_t f3 = step*tsint*vect[kPZ];
74  Float_t f4 = -tet*cos1t;
75  Float_t f5 = sint;
76  Float_t f6 = tet*cos1t*vect[kPZ];
77  vout[kX] = vect[kX] + (f1*vect[kPX] - f2*vect[kPY]);
78  vout[kY] = vect[kY] + (f1*vect[kPY] + f2*vect[kPX]);
79  vout[kZ] = vect[kZ] + (f1*vect[kPZ] + f3);
80  vout[kPX] = vect[kPX] + (f4*vect[kPX] - f5*vect[kPY]);
81  vout[kPY] = vect[kPY] + (f4*vect[kPY] + f5*vect[kPX]);
82  vout[kPZ] = vect[kPZ] + (f4*vect[kPZ] + f6);
83 }
84 
85 void tree2aw()
86 {
87  //create a Tree file tree2.root
88 
89  //create the file, the Tree and a few branches with
90  //a subset of gctrak
91  TFile f("tree2.root","recreate");
92  TTree t2("t2","a Tree with data from a fake Geant3");
93  Gctrak *gstep = new Gctrak;
94  t2.Branch("track",&gstep,8000,1);
95 
96  //Initialize particle parameters at first point
97  Float_t px,py,pz,p,charge=0;
98  Float_t vout[7];
99  Float_t mass = 0.137;
100  Bool_t newParticle = kTRUE;
101  gstep->lmec = new Int_t[MAXMEC];
102  gstep->namec = new Int_t[MAXMEC];
103  gstep->step = 0.1;
104  gstep->destep = 0;
105  gstep->nmec = 0;
106  gstep->pid = 0;
107 
108  //transport particles
109  for (Int_t i=0;i<10000;i++) {
110  //generate a new particle if necessary
111  if (newParticle) {
112  px = gRandom->Gaus(0,.02);
113  py = gRandom->Gaus(0,.02);
114  pz = gRandom->Gaus(0,.02);
115  p = TMath::Sqrt(px*px+py*py+pz*pz);
116  charge = 1; if (gRandom->Rndm() < 0.5) charge = -1;
117  gstep->pid += 1;
118  gstep->vect[0] = 0;
119  gstep->vect[1] = 0;
120  gstep->vect[2] = 0;
121  gstep->vect[3] = px/p;
122  gstep->vect[4] = py/p;
123  gstep->vect[5] = pz/p;
124  gstep->vect[6] = p*charge;
125  gstep->getot = TMath::Sqrt(p*p + mass*mass);
126  gstep->gekin = gstep->getot - mass;
127  newParticle = kFALSE;
128  }
129 
130  // fill the Tree with current step parameters
131  t2.Fill();
132 
133  //transport particle in magnetic field
134  helixStep(gstep->step, gstep->vect, vout); //make one step
135 
136  //apply energy loss
137  gstep->destep = gstep->step*gRandom->Gaus(0.0002,0.00001);
138  gstep->gekin -= gstep->destep;
139  gstep->getot = gstep->gekin + mass;
140  gstep->vect[6] = charge*TMath::Sqrt(gstep->getot*gstep->getot - mass*mass);
141  gstep->vect[0] = vout[0];
142  gstep->vect[1] = vout[1];
143  gstep->vect[2] = vout[2];
144  gstep->vect[3] = vout[3];
145  gstep->vect[4] = vout[4];
146  gstep->vect[5] = vout[5];
147  gstep->nmec = (Int_t)(5*gRandom->Rndm());
148  for (Int_t l=0;l<gstep->nmec;l++) {
149  gstep->lmec[l] = l;
150  gstep->namec[l] = l+100;
151  }
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 tree2ar()
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  Gctrak *gstep = 0;
172  t2->SetBranchAddress("track",&gstep);
173  TBranch *b_destep = t2->GetBranch("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(gstep->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 tree2a() {
205  tree2aw();
206  tree2ar();
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
#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
#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:7510
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
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
Mother of all ROOT objects.
Definition: TObject.h:58
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