Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tv3.C
Go to the documentation of this file.
1class Vector3
2{
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
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}
ROOT::R::TRInterface & r
Definition Object.C:4
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:80
int nentries
A TTree is a list of TBranches.
Definition TBranch.h:89
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:1652
The Canvas class.
Definition TCanvas.h:23
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3351
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3074
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
A TTree represents a columnar dataset.
Definition TTree.h:79
Definition tv3.C:2
Double_t fZ
Definition tv3.C:5
Vector3()
Definition tv3.C:8
Double_t x()
Definition tv3.C:10
Double_t z()
Definition tv3.C:12
Double_t fX
Definition tv3.C:3
Double_t y()
Definition tv3.C:11
Double_t fY
Definition tv3.C:4
void SetXYZ(Double_t x, Double_t y, Double_t z)
Definition tv3.C:14
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5
void tv3Read2()
Definition tv3.C:51
void tv3Write()
Definition tv3.C:21
void tv3()
Definition tv3.C:67
void tv3Read1()
Definition tv3.C:36