{
"cells": [
{
"cell_type": "markdown",
"id": "f1a2465c",
"metadata": {},
"source": [
"# tree121_hvector\n",
"Write and read STL vectors 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": "markdown",
"id": "1da2bd0e",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "28e43a1e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:45.417225Z",
"iopub.status.busy": "2026-05-19T20:18:45.417113Z",
"iopub.status.idle": "2026-05-19T20:18:45.447632Z",
"shell.execute_reply": "2026-05-19T20:18:45.447117Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \n",
"\n",
"#include \"TFile.h\"\n",
"#include \"TTree.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TFrame.h\"\n",
"#include \"TH1F.h\"\n",
"#include \"TBenchmark.h\"\n",
"#include \"TRandom.h\"\n",
"#include \"TSystem.h\"\n",
"\n",
"void write_vector()\n",
"{\n",
" auto f = TFile::Open(\"hvector.root\",\"RECREATE\");\n",
"\n",
" if (!f)\n",
" return;\n",
"\n",
" // Create one histograms\n",
" auto hpx = new TH1F(\"hpx\",\"This is the px distribution\", 100, -4, 4);\n",
" hpx->SetFillColor(48);\n",
"\n",
" std::vector vpx;\n",
" std::vector vpy;\n",
" std::vector vpz;\n",
" std::vector vrand;\n",
"\n",
" // Create a TTree\n",
" TTree *t = new TTree(\"tvec\", \"Tree with vectors\");\n",
" t->Branch(\"vpx\", &vpx);\n",
" t->Branch(\"vpy\", &vpy);\n",
" t->Branch(\"vpz\", &vpz);\n",
" t->Branch(\"vrand\", &vrand);\n",
"\n",
" // Create a new canvas.\n",
" auto c1 = new TCanvas(\"c1\", \"Dynamic Filling Example\", 200, 10, 700, 500);\n",
"\n",
" gRandom->SetSeed();\n",
" const Int_t kUPDATE = 1000;\n",
" for (Int_t i = 0; i < 25000; i++) {\n",
" Int_t npx = (Int_t)(gRandom->Rndm(1) * 15);\n",
"\n",
" vpx.clear();\n",
" vpy.clear();\n",
" vpz.clear();\n",
" vrand.clear();\n",
"\n",
" for (Int_t j = 0; j < npx; ++j) {\n",
"\n",
" Float_t px,py,pz;\n",
" gRandom->Rannor(px, py);\n",
" pz = px * px + py * py;\n",
" Float_t random = gRandom->Rndm(1);\n",
"\n",
" hpx->Fill(px);\n",
"\n",
" vpx.emplace_back(px);\n",
" vpy.emplace_back(py);\n",
" vpz.emplace_back(pz);\n",
" vrand.emplace_back(random);\n",
"\n",
" }\n",
" if (i && (i%kUPDATE) == 0) {\n",
" if (i == kUPDATE)\n",
" hpx->Draw();\n",
" c1->Modified();\n",
" c1->Update();\n",
" if (gSystem->ProcessEvents())\n",
" break;\n",
" }\n",
" t->Fill();\n",
" }\n",
" f->Write();\n",
"\n",
" delete f;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "faed2b46",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "17151234",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:45.449231Z",
"iopub.status.busy": "2026-05-19T20:18:45.449112Z",
"iopub.status.idle": "2026-05-19T20:18:45.457601Z",
"shell.execute_reply": "2026-05-19T20:18:45.457033Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void read_vector()\n",
"{\n",
" auto f = TFile::Open(\"hvector.root\", \"READ\");\n",
"\n",
" if (!f)\n",
" return;\n",
"\n",
" auto t = f->Get(\"tvec\");\n",
"\n",
" std::vector *vpx = nullptr;\n",
"\n",
" // Create a new canvas.\n",
" auto c1 = new TCanvas(\"c1\", \"Dynamic Filling Example\", 200, 10, 700, 500);\n",
"\n",
" const Int_t kUPDATE = 1000;\n",
"\n",
" TBranch *bvpx = nullptr;\n",
" t->SetBranchAddress(\"vpx\", &vpx, &bvpx);\n",
"\n",
"\n",
" // Create one histograms\n",
" auto h = new TH1F(\"h\", \"This is the px distribution\", 100, -4, 4);\n",
" h->SetFillColor(48);\n",
"\n",
" for (Int_t i = 0; i < 25000; i++) {\n",
"\n",
" Long64_t tentry = t->LoadTree(i);\n",
" bvpx->GetEntry(tentry);\n",
"\n",
" for (UInt_t j = 0; j < vpx->size(); ++j) {\n",
"\n",
" h->Fill(vpx->at(j));\n",
"\n",
" }\n",
" if (i && (i%kUPDATE) == 0) {\n",
" if (i == kUPDATE)\n",
" h->Draw();\n",
" c1->Modified();\n",
" c1->Update();\n",
" if (gSystem->ProcessEvents())\n",
" break;\n",
" }\n",
" }\n",
"\n",
" // Since we passed the address of a local variable we need\n",
" // to remove it.\n",
" t->ResetBranchAddresses();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4aa11aee",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:45.458796Z",
"iopub.status.busy": "2026-05-19T20:18:45.458678Z",
"iopub.status.idle": "2026-05-19T20:18:46.253046Z",
"shell.execute_reply": "2026-05-19T20:18:46.252524Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"hvector : Real Time = 0.26 seconds Cpu Time = 0.26 seconds\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Warning in : Deleting canvas with same name: c1\n"
]
}
],
"source": [
"gBenchmark->Start(\"hvector\");\n",
"write_vector();\n",
"read_vector();\n",
"gBenchmark->Show(\"hvector\");"
]
},
{
"cell_type": "markdown",
"id": "4c66aadc",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "068dc7f5",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:46.254810Z",
"iopub.status.busy": "2026-05-19T20:18:46.254691Z",
"iopub.status.idle": "2026-05-19T20:18:46.459971Z",
"shell.execute_reply": "2026-05-19T20:18:46.459322Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"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
}