Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree122_vector3.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook -nodraw
4/// Write and read a Vector3 class in a tree.
5///
6/// \macro_code
7///
8/// \author The ROOT Team
9
10class Vector3
11{
12 Double_t fX;
13 Double_t fY;
14 Double_t fZ;
15
16public:
17 Vector3() : fX(0), fY(0), fZ(0) {}
18
19 Double_t x() { return fX; }
20 Double_t y() { return fY; }
21 Double_t z() { return fZ; }
22
23 void SetXYZ(Double_t x, Double_t y, Double_t z) {
24 fX = x;
25 fY = y;
26 fZ = z;
27 }
28};
29
30void write_vector3()
31{
32 //creates the Tree
33 auto v = new Vector3();
34 auto f = TFile::Open("vector3.root", "recreate");
35 auto T = new TTree("T", "vector3 Tree");
36 T->Branch("v3", &v, 32000, 1);
37 TRandom r;
38 for (Int_t i=0; i<10000; i++) {
39 v->SetXYZ(r.Gaus(0, 1), r.Landau(0, 1), r.Gaus(100, 10));
40 T->Fill();
41 }
42 T->Write();
43 T->Print();
44 delete f;
45}
46
48{
49 //first read example showing how to read all branches
50 Vector3 *v = 0;
51 auto f = TFile::Open("vector3.root");
52 auto T = f->Get<TTree>("T");
53 T->SetBranchAddress("v3", &v);
54 auto h1 = new TH1F("x", "x component of Vector3", 100, -3, 3);
55 Long64_t nentries = T->GetEntries();
56 for (Long64_t i=0; i<nentries; i++) {
57 T->GetEntry(i);
58 h1->Fill(v->x());
59 }
60 h1->Draw();
61}
62
64 {
65 //second read example illustrating how to read one branch only
66 Vector3 *v = 0;
67 auto f = TFile::Open("vector3.root");
68 auto T = f->Get<TTree>("T");
69 T->SetBranchAddress("v3", &v);
70 auto by = T->GetBranch("fY");
71 auto TH1F("y", "y component of Vector3", 100, -5, 20);
72 Long64_t nentries = T->GetEntries();
73 for (Long64_t i=0; i<nentries; i++) {
74 by->GetEntry(i);
75 v->y());
76 }
77 h2->Draw();
78}
79
80void tree122_vector3()
81{
82 auto c1 = new TCanvas("c1", "demo of Trees", 10, 10, 600, 800);
83 c1->Divide(1, 2);
85 c1->cd(1);
87 c1->cd(2);
89}
#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:69
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
int nentries
The Canvas class.
Definition TCanvas.h:23
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4131
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:877
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3316
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3038
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
A TTree represents a columnar dataset.
Definition TTree.h:84
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8486
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
TH1F * h1
Definition legend1.C:5