Logo ROOT   6.10/09
Reference Guide
tv3.C
Go to the documentation of this file.
1 class Vector3
2 {
3  Double_t fX;
4  Double_t fY;
5  Double_t fZ;
6 
7 public:
8  Vector3() : fX(0),fY(0),fZ(0) {}
9 
10  Double_t x() { return fX; }
11  Double_t y() { return fY; }
12  Double_t z() { return fZ; }
13 
14  void SetXYZ(Double_t x, Double_t y, Double_t z) {
15  fX = x;
16  fY = y;
17  fZ = z;
18  }
19 };
20 
21 void tv3Write() {
22  //creates the Tree
23  Vector3 *v = new Vector3();
24  TFile *f = new TFile("v3.root","recreate");
25  TTree *T = new TTree("T","v3 Tree");
26  T->Branch("v3",&v,32000,1);
27  TRandom r;
28  for (Int_t i=0;i<10000;i++) {
29  v->SetXYZ(r.Gaus(0,1),r.Landau(0,1),r.Gaus(100,10));
30  T->Fill();
31  }
32  T->Write();
33  T->Print();
34  delete f;
35 }
36 void tv3Read1() {
37  //first read example showing how to read all branches
38  Vector3 *v = 0;
39  TFile *f = new TFile("v3.root");
40  TTree *T = (TTree*)f->Get("T");
41  T->SetBranchAddress("v3",&v);
42  TH1F *h1 = new TH1F("x","x component of Vector3",100,-3,3);
44  for (Long64_t i=0;i<nentries;i++) {
45  T->GetEntry(i);
46  h1->Fill(v->x());
47  }
48  h1->Draw();
49 }
50 
51  void tv3Read2() {
52  //second read example illustrating how to read one branch only
53  Vector3 *v = 0;
54  TFile *f = new TFile("v3.root");
55  TTree *T = (TTree*)f->Get("T");
56  T->SetBranchAddress("v3",&v);
57  TBranch *by = T->GetBranch("fY");
58  TH1F *h2 = new TH1F("y","y component of Vector3",100,-5,20);
60  for (Long64_t i=0;i<nentries;i++) {
61  by->GetEntry(i);
62  h2->Fill(v->y());
63  }
64  h2->Draw();
65 }
66 
67 void tv3() {
68  TCanvas *c1 = new TCanvas("c1","demo of Trees",10,10,600,800);
69  c1->Divide(1,2);
70  tv3Write();
71  c1->cd(1);
72  tv3Read1();
73  c1->cd(2);
74  tv3Read2();
75 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
long long Long64_t
Definition: RtypesCore.h:69
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
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6841
double T(double x)
Definition: ChebyshevPol.h:34
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4383
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5321
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
int Int_t
Definition: RtypesCore.h:41
Double_t x[n]
Definition: legend1.C:17
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7873
void tv3()
Definition: tv3.C:67
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
TH1F * h1
Definition: legend1.C:5
void tv3Read1()
Definition: tv3.C:36
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4979
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:9163
TRandom2 r(17)
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
SVector< double, 2 > v
Definition: Dict.h:5
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:1291
void tv3Read2()
Definition: tv3.C:51
The Canvas class.
Definition: TCanvas.h:31
double f(double x)
double Double_t
Definition: RtypesCore.h:55
int nentries
Definition: THbookFile.cxx:89
Double_t y[n]
Definition: legend1.C:17
virtual Long64_t GetEntries() const
Definition: TTree.h:381
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
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:1660
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:1135
A TTree object has a header with a name and a title.
Definition: TTree.h:78
A TTree is a list of TBranches.
Definition: TBranch.h:57
virtual Double_t Landau(Double_t mean=0, Double_t sigma=1)
Generate a random number following a Landau distribution with location parameter mu and scale paramet...
Definition: TRandom.cxx:340
void tv3Write()
Definition: tv3.C:21