{
"cells": [
{
"cell_type": "markdown",
"id": "36945dfa",
"metadata": {},
"source": [
"# tree107_tree\n",
"Example of a Tree where branches are variable length arrays\n",
"A second Tree is created and filled in parallel.\n",
"Run this script with\n",
"```\n",
" .x tree107_tree.C\n",
"```\n",
"In the function treer, the first Tree is open.\n",
"The second Tree is declared friend of the first tree.\n",
"TTree::Draw is called with variables from both Trees.\n",
"\n",
"\n",
"\n",
"**Author:** Rene Brun \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": "96e4ef2b",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ccdbfbc1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:05.923301Z",
"iopub.status.busy": "2026-05-19T20:18:05.923185Z",
"iopub.status.idle": "2026-05-19T20:18:05.939242Z",
"shell.execute_reply": "2026-05-19T20:18:05.938636Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \"TFile.h\"\n",
"#include \"TTree.h\"\n",
"#include \"TRandom.h\"\n",
"#include \"TCanvas.h\"\n",
"\n",
"void tree107_write()\n",
"{\n",
" const Int_t kMaxTrack = 500;\n",
" Int_t ntrack;\n",
" Int_t stat[kMaxTrack];\n",
" Int_t sign[kMaxTrack];\n",
" Float_t px[kMaxTrack];\n",
" Float_t py[kMaxTrack];\n",
" Float_t pz[kMaxTrack];\n",
" Float_t pt[kMaxTrack];\n",
" Float_t zv[kMaxTrack];\n",
" Float_t chi2[kMaxTrack];\n",
" Double_t sumstat;\n",
"\n",
" TFile f(\"tree108.root\", \"recreate\");\n",
" auto t3 = new TTree(\"t3\", \"Reconst ntuple\");\n",
" t3->Branch(\"ntrack\", &ntrack, \"ntrack/I\");\n",
" t3->Branch(\"stat\", stat, \"stat[ntrack]/I\");\n",
" t3->Branch(\"sign\", sign, \"sign[ntrack]/I\");\n",
" t3->Branch(\"px\", px, \"px[ntrack]/F\");\n",
" t3->Branch(\"py\", py, \"py[ntrack]/F\");\n",
" t3->Branch(\"pz\", pz, \"pz[ntrack]/F\");\n",
" t3->Branch(\"zv\", zv, \"zv[ntrack]/F\");\n",
" t3->Branch(\"chi2\", chi2, \"chi2[ntrack]/F\");\n",
"\n",
" TFile fr(\"tree108f.root\", \"recreate\");\n",
" auto t3f = new TTree(\"t3f\", \"a friend Tree\");\n",
" t3f->Branch(\"ntrack\", &ntrack, \"ntrack/I\");\n",
" t3f->Branch(\"sumstat\", &sumstat, \"sumstat/D\");\n",
" t3f->Branch(\"pt\", pt, \"pt[ntrack]/F\");\n",
"\n",
" for (Int_t i=0; i<1000; i++) {\n",
" Int_t nt = gRandom->Rndm() * (kMaxTrack - 1);\n",
" ntrack = nt;\n",
" sumstat = 0;\n",
" for (Int_t n=0; nGaus(0, 1);\n",
" py[n] = gRandom->Gaus(0, 2);\n",
" pz[n] = gRandom->Gaus(10, 5);\n",
" zv[n] = gRandom->Gaus(100, 2);\n",
" chi2[n] = gRandom->Gaus(0, .01);\n",
" sumstat += chi2[n];\n",
" pt[n] = TMath::Sqrt(px[n] * px[n] + py[n] * py[n]);\n",
" }\n",
" t3->Fill();\n",
" t3f->Fill();\n",
" }\n",
" t3->Print();\n",
" f.cd();\n",
" t3->Write();\n",
" fr.cd();\n",
" t3f->Write();\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "1aad0e20",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1480329e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:05.941083Z",
"iopub.status.busy": "2026-05-19T20:18:05.940943Z",
"iopub.status.idle": "2026-05-19T20:18:05.945630Z",
"shell.execute_reply": "2026-05-19T20:18:05.945047Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void tree107_read()\n",
"{\n",
" auto f = TFile::Open(\"tree108.root\");\n",
" auto t3 = f->Get(\"t3\");\n",
" t3->AddFriend(\"t3f\", \"tree108f.root\");\n",
" t3->Draw(\"pz\", \"pt>3\");\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "3afc3c81",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "20686c8e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:05.947063Z",
"iopub.status.busy": "2026-05-19T20:18:05.946950Z",
"iopub.status.idle": "2026-05-19T20:18:05.954173Z",
"shell.execute_reply": "2026-05-19T20:18:05.953591Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void tree107_read2()\n",
"{\n",
" auto p = new TPad(\"p\", \"p\", 0.6, 0.4, 0.98, 0.8);\n",
" p->Draw();\n",
" p->cd();\n",
" auto f1 = TFile::Open(\"tree108.root\");\n",
" auto f2 = TFile::Open(\"tree108f.root\");\n",
" auto t3 = f1->Get(\"t3\");\n",
" t3->AddFriend(\"t3f\", f2);\n",
" t3->Draw(\"pz\", \"pt>3\");\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f947da44",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:05.955566Z",
"iopub.status.busy": "2026-05-19T20:18:05.955452Z",
"iopub.status.idle": "2026-05-19T20:18:06.556424Z",
"shell.execute_reply": "2026-05-19T20:18:06.555741Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"******************************************************************************\n",
"*Tree :t3 : Reconst ntuple *\n",
"*Entries : 1000 : Total = 7018766 bytes File Size = 4381392 *\n",
"* : : Tree compression factor = 1.56 *\n",
"******************************************************************************\n",
"*Br 0 :ntrack : ntrack/I *\n",
"*Entries : 1000 : Total Size= 4647 bytes One basket in memory *\n",
"*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n",
"*............................................................................*\n",
"*Br 1 :stat : stat[ntrack]/I *\n",
"*Entries : 1000 : Total Size= 1002090 bytes File Size = 15607 *\n",
"*Baskets : 31 : Basket Size= 32000 bytes Compression= 62.37 *\n",
"*............................................................................*\n",
"*Br 2 :sign : sign[ntrack]/I *\n",
"*Entries : 1000 : Total Size= 1002090 bytes File Size = 13476 *\n",
"*Baskets : 31 : Basket Size= 32000 bytes Compression= 72.23 *\n",
"*............................................................................*\n",
"*Br 3 :px : px[ntrack]/F *\n",
"*Entries : 1000 : Total Size= 1002023 bytes File Size = 901417 *\n",
"*Baskets : 31 : Basket Size= 32000 bytes Compression= 1.08 *\n",
"*............................................................................*\n",
"*Br 4 :py : py[ntrack]/F *\n",
"*Entries : 1000 : Total Size= 1002023 bytes File Size = 904712 *\n",
"*Baskets : 31 : Basket Size= 32000 bytes Compression= 1.08 *\n",
"*............................................................................*\n",
"*Br 5 :pz : pz[ntrack]/F *\n",
"*Entries : 1000 : Total Size= 1002023 bytes File Size = 865995 *\n",
"*Baskets : 31 : Basket Size= 32000 bytes Compression= 1.12 *\n",
"*............................................................................*\n",
"*Br 6 :zv : zv[ntrack]/F *\n",
"*Entries : 1000 : Total Size= 1002023 bytes File Size = 777135 *\n",
"*Baskets : 31 : Basket Size= 32000 bytes Compression= 1.25 *\n",
"*............................................................................*\n",
"*Br 7 :chi2 : chi2[ntrack]/F *\n",
"*Entries : 1000 : Total Size= 1002097 bytes File Size = 903050 *\n",
"*Baskets : 31 : Basket Size= 32000 bytes Compression= 1.08 *\n",
"*............................................................................*\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : created default TCanvas with name c1\n"
]
}
],
"source": [
"tree107_write();\n",
"tree107_read();\n",
"tree107_read2();"
]
},
{
"cell_type": "markdown",
"id": "55b777dc",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b4d0b387",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:06.558210Z",
"iopub.status.busy": "2026-05-19T20:18:06.558093Z",
"iopub.status.idle": "2026-05-19T20:18:06.763469Z",
"shell.execute_reply": "2026-05-19T20:18:06.762851Z"
}
},
"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
}