Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
tree122_vector3.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Write and read a Vector3 class in a tree.

class Vector3
{
public:
Vector3() : fX(0), fY(0), fZ(0) {}
Double_t x() { return fX; }
Double_t y() { return fY; }
Double_t z() { return fZ; }
void SetXYZ(Double_t x, Double_t y, Double_t z) {
fX = x;
fY = y;
fZ = z;
}
};
{
//creates the Tree
auto v = new Vector3();
auto f = TFile::Open("vector3.root", "recreate");
auto T = new TTree("T", "vector3 Tree");
T->Branch("v3", &v, 32000, 1);
for (Int_t i=0; i<10000; i++) {
v->SetXYZ(r.Gaus(0, 1), r.Landau(0, 1), r.Gaus(100, 10));
T->Fill();
}
T->Write();
T->Print();
delete f;
}
{
//first read example showing how to read all branches
Vector3 *v = 0;
auto f = TFile::Open("vector3.root");
auto T = f->Get<TTree>("T");
T->SetBranchAddress("v3", &v);
auto h1 = new TH1F("x", "x component of Vector3", 100, -3, 3);
Long64_t nentries = T->GetEntries();
for (Long64_t i=0; i<nentries; i++) {
T->GetEntry(i);
h1->Fill(v->x());
}
h1->Draw();
}
{
//second read example illustrating how to read one branch only
Vector3 *v = 0;
auto f = TFile::Open("vector3.root");
auto T = f->Get<TTree>("T");
T->SetBranchAddress("v3", &v);
auto by = T->GetBranch("fY");
auto TH1F("y", "y component of Vector3", 100, -5, 20);
Long64_t nentries = T->GetEntries();
for (Long64_t i=0; i<nentries; i++) {
by->GetEntry(i);
v->y());
}
h2->Draw();
}
{
auto c1 = new TCanvas("c1", "demo of Trees", 10, 10, 600, 800);
c1->Divide(1, 2);
c1->cd(1);
c1->cd(2);
}
#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:4130
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:647
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:79
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8477
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
Author
The ROOT Team

Definition in file tree122_vector3.C.