{ "cells": [ { "cell_type": "markdown", "id": "695b49e3", "metadata": {}, "source": [ "# tree122_vector3\n", "Write and read a Vector3 class in a tree.\n", "\n", "\n", "\n", "\n", "**Author:** The ROOT Team \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:18 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "de1b1990", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:54.396477Z", "iopub.status.busy": "2026-05-19T20:18:54.396366Z", "iopub.status.idle": "2026-05-19T20:18:54.399285Z", "shell.execute_reply": "2026-05-19T20:18:54.398853Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "\n", "class Vector3\n", "{\n", " Double_t fX;\n", " Double_t fY;\n", " Double_t fZ;\n", "\n", "public:\n", " Vector3() : fX(0), fY(0), fZ(0) {}\n", "\n", " Double_t x() { return fX; }\n", " Double_t y() { return fY; }\n", " Double_t z() { return fZ; }\n", "\n", " void SetXYZ(Double_t x, Double_t y, Double_t z) {\n", " fX = x;\n", " fY = y;\n", " fZ = z;\n", " }\n", "};" ] }, { "cell_type": "markdown", "id": "9254939c", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "67700ead", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:54.400433Z", "iopub.status.busy": "2026-05-19T20:18:54.400322Z", "iopub.status.idle": "2026-05-19T20:18:54.414117Z", "shell.execute_reply": "2026-05-19T20:18:54.413687Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "\n", "void write_vector3()\n", "{\n", " //creates the Tree\n", " auto v = new Vector3();\n", " auto f = TFile::Open(\"vector3.root\", \"recreate\");\n", " auto T = new TTree(\"T\", \"vector3 Tree\");\n", " T->Branch(\"v3\", &v, 32000, 1);\n", " TRandom r;\n", " for (Int_t i=0; i<10000; i++) {\n", " v->SetXYZ(r.Gaus(0, 1), r.Landau(0, 1), r.Gaus(100, 10));\n", " T->Fill();\n", " }\n", " T->Write();\n", " T->Print();\n", " delete f;\n", "}" ] }, { "cell_type": "markdown", "id": "a86b8e77", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 3, "id": "238504cb", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:54.415507Z", "iopub.status.busy": "2026-05-19T20:18:54.415391Z", "iopub.status.idle": "2026-05-19T20:18:54.422980Z", "shell.execute_reply": "2026-05-19T20:18:54.422545Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void read_all_vector3()\n", "{\n", " //first read example showing how to read all branches\n", " Vector3 *v = 0;\n", " auto f = TFile::Open(\"vector3.root\");\n", " auto T = f->Get(\"T\");\n", " T->SetBranchAddress(\"v3\", &v);\n", " auto h1 = new TH1F(\"x\", \"x component of Vector3\", 100, -3, 3);\n", " Long64_t nentries = T->GetEntries();\n", " for (Long64_t i=0; iGetEntry(i);\n", " h1->Fill(v->x());\n", " }\n", " h1->Draw();\n", "}" ] }, { "cell_type": "markdown", "id": "d513a918", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 4, "id": "6380f9cd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:54.424154Z", "iopub.status.busy": "2026-05-19T20:18:54.424041Z", "iopub.status.idle": "2026-05-19T20:18:54.429267Z", "shell.execute_reply": "2026-05-19T20:18:54.428860Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void read_branch_vector3()\n", " {\n", " //second read example illustrating how to read one branch only\n", " Vector3 *v = 0;\n", " auto f = TFile::Open(\"vector3.root\");\n", " auto T = f->Get(\"T\");\n", " T->SetBranchAddress(\"v3\", &v);\n", " auto by = T->GetBranch(\"fY\");\n", " auto h2 = new TH1F(\"y\", \"y component of Vector3\", 100, -5, 20);\n", " Long64_t nentries = T->GetEntries();\n", " for (Long64_t i=0; iGetEntry(i);\n", " h2->Fill(v->y());\n", " }\n", " h2->Draw();\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "id": "e13f13f1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:54.430446Z", "iopub.status.busy": "2026-05-19T20:18:54.430332Z", "iopub.status.idle": "2026-05-19T20:18:54.925291Z", "shell.execute_reply": "2026-05-19T20:18:54.924831Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "******************************************************************************\n", "*Tree :T : vector3 Tree *\n", "*Entries : 10000 : Total = 243106 bytes File Size = 222153 *\n", "* : : Tree compression factor = 1.09 *\n", "******************************************************************************\n", "*Branch :v3 *\n", "*Entries : 10000 : BranchElement (see below) *\n", "*............................................................................*\n", "*Br 0 :fX : Double_t *\n", "*Entries : 10000 : Total Size= 80691 bytes File Size = 73088 *\n", "*Baskets : 3 : Basket Size= 32000 bytes Compression= 1.10 *\n", "*............................................................................*\n", "*Br 1 :fY : Double_t *\n", "*Entries : 10000 : Total Size= 80691 bytes File Size = 77487 *\n", "*Baskets : 3 : Basket Size= 32000 bytes Compression= 1.03 *\n", "*............................................................................*\n", "*Br 2 :fZ : Double_t *\n", "*Entries : 10000 : Total Size= 80691 bytes File Size = 70918 *\n", "*Baskets : 3 : Basket Size= 32000 bytes Compression= 1.13 *\n", "*............................................................................*\n" ] } ], "source": [ "auto c1 = new TCanvas(\"c1\", \"demo of Trees\", 10, 10, 600, 800);\n", "c1->Divide(1, 2);\n", "write_vector3();\n", "c1->cd(1);\n", "read_all_vector3();\n", "c1->cd(2);\n", "read_branch_vector3();" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }