Logo ROOT   6.16/01
Reference Guide
tv3.C
Go to the documentation of this file.
1class Vector3
2{
3 Double_t fX;
4 Double_t fY;
5 Double_t fZ;
6
7public:
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
21void 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}
36void 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);
43 Long64_t nentries = T->GetEntries();
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);
59 Long64_t nentries = T->GetEntries();
60 for (Long64_t i=0;i<nentries;i++) {
61 by->GetEntry(i);
62 h2->Fill(v->y());
63 }
64 h2->Draw();
65}
66
67void 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}
SVector< double, 2 > v
Definition: Dict.h:5
ROOT::R::TRInterface & r
Definition: Object.C:4
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
double Double_t
Definition: RtypesCore.h:55
long long Long64_t
Definition: RtypesCore.h:69
int nentries
Definition: THbookFile.cxx:89
A TTree is a list of TBranches.
Definition: TBranch.h:64
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:1317
The Canvas class.
Definition: TCanvas.h:31
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3251
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
A TTree object has a header with a name and a title.
Definition: TTree.h:71
return c1
Definition: legend1.C:41
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
TH1F * h1
Definition: legend1.C:5
double T(double x)
Definition: ChebyshevPol.h:34
void tv3Read2()
Definition: tv3.C:51
void tv3Write()
Definition: tv3.C:21
void tv3()
Definition: tv3.C:67
void tv3Read1()
Definition: tv3.C:36