{
"cells": [
{
"cell_type": "markdown",
"id": "4436251e",
"metadata": {},
"source": [
"# tree123_clonesarray\n",
"How to write a TClonesArray to a TTree\n",
"\n",
"The following tests can be run\n",
"Interactive tests\n",
"```\n",
"Root > .x tree123_clonesarray.C //no-split interpreted\n",
"Root > .x tree123_clonesarray.C(1) //split interpreted\n",
"Root > .x tree123_clonesarray.C++ //no-split compiled\n",
"Root > .x tree123_clonesarray.C++(1) //split compiled\n",
"```\n",
"Batch tests: same as above but with no graphics\n",
"```bash\n",
"root -b -q tree123_clonesarray.C\n",
"root -b -q tree123_clonesarray.C++\n",
"root -b -q \"tree123_clonesarray.C(1)\"\n",
"root -b -q \"tree123_clonesarray.C++(1)\"\n",
"```\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": "8a749c75",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "45a925a8",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:02.404429Z",
"iopub.status.busy": "2026-05-19T20:19:02.404303Z",
"iopub.status.idle": "2026-05-19T20:19:02.421424Z",
"shell.execute_reply": "2026-05-19T20:19:02.421017Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \"TFile.h\"\n",
"#include \"TClonesArray.h\"\n",
"#include \"TH2.h\"\n",
"#include \"TLine.h\"\n",
"#include \"TTree.h\"\n",
"#include \"TBenchmark.h\"\n",
"#include \"TRandom.h\"\n",
"\n",
"void write_clonesarray(Int_t split)\n",
"{\n",
" // Generate a Tree with a TClonesArray\n",
" // The array can be split or not\n",
" TFile f(\"clonesarray.root\", \"recreate\");\n",
" f.SetCompressionLevel(1); //try level 2 also\n",
" TTree T(\"T\", \"test clonesarray\");\n",
" TClonesArray *arr = new TClonesArray(\"TLine\");\n",
" TClonesArray &ar = *arr;\n",
" T.Branch(\"tcl\", &arr, 256000, split);\n",
" // By default a TClonesArray is created with its BypassStreamer bit set.\n",
" // However, because TLine has a custom Streamer, this bit was reset\n",
" // by TTree::Branch above. We set again this bit because the current\n",
" // version of TLine uses the automatic Streamer.\n",
" // BypassingStreamer saves space and time.\n",
" arr->BypassStreamer();\n",
" for (Int_t ev=0; ev<10000; ev++) {\n",
" ar.Clear();\n",
" Int_t nlines = Int_t(gRandom->Gaus(50,10));\n",
" if(nlines < 0)\n",
" nlines = 1;\n",
" for (Int_t i=0;iRndm();\n",
" Float_t y1 = gRandom->Rndm();\n",
" Float_t x2 = gRandom->Rndm();\n",
" Float_t y2 = gRandom->Rndm();\n",
" new(ar[i]) TLine(x1, y1, x2, y2);\n",
" }\n",
" T.Fill();\n",
" }\n",
" T.Print();\n",
" T.Write();\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "7f9bf1aa",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a6ab28d9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:02.422869Z",
"iopub.status.busy": "2026-05-19T20:19:02.422749Z",
"iopub.status.idle": "2026-05-19T20:19:02.433188Z",
"shell.execute_reply": "2026-05-19T20:19:02.432803Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void read_clonesarray()\n",
"{\n",
" // read file generated by write_clonesarray\n",
" // loop on all entries.\n",
" // histogram center of lines\n",
" auto f = new TFile(\"clonesarray.root\");\n",
" auto T = f->Get(\"T\");\n",
" auto h2 = new TH2F(\"h2\", \"center of lines\", 40, 0, 1, 40, 0, 1);\n",
"\n",
" auto arr = new TClonesArray(\"TLine\");\n",
" T->GetBranch(\"tcl\")->SetAutoDelete(kFALSE);\n",
" T->SetBranchAddress(\"tcl\", &arr);\n",
" Long64_t nentries = T->GetEntries();\n",
" for (Long64_t ev=0; evClear();\n",
" T->GetEntry(ev);\n",
" Int_t nlines = arr->GetEntriesFast();\n",
" for (Int_t i=0; iAt(i);\n",
" h2->Fill(0.5 * (line->GetX1() + line->GetX2()), 0.5 * (line->GetY1() + line->GetY2()));\n",
" }\n",
" }\n",
" h2->Draw(\"lego\");\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "e16a68d5",
"metadata": {},
"source": [
" Arguments are defined. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2db21027",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:02.434533Z",
"iopub.status.busy": "2026-05-19T20:19:02.434415Z",
"iopub.status.idle": "2026-05-19T20:19:02.755713Z",
"shell.execute_reply": "2026-05-19T20:19:02.755107Z"
}
},
"outputs": [],
"source": [
"Int_t split = 0;"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "46997f8b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:02.757269Z",
"iopub.status.busy": "2026-05-19T20:19:02.757132Z",
"iopub.status.idle": "2026-05-19T20:19:03.226160Z",
"shell.execute_reply": "2026-05-19T20:19:03.225709Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"******************************************************************************\n",
"*Tree :T : test clonesarray *\n",
"*Entries : 10000 : Total = 24271934 bytes File Size = 9063862 *\n",
"* : : Tree compression factor = 2.67 *\n",
"******************************************************************************\n",
"*Br 0 :tcl : TClonesArray *\n",
"*Entries : 10000 : Total Size= 24271605 bytes File Size = 9063862 *\n",
"*Baskets : 95 : Basket Size= 256000 bytes Compression= 2.67 *\n",
"*............................................................................*\n",
"clonesarray: Real Time = 0.25 seconds Cpu Time = 0.24 seconds\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : created default TCanvas with name c1\n"
]
}
],
"source": [
"gBenchmark->Start(\"clonesarray\");\n",
"write_clonesarray(split);\n",
"read_clonesarray();\n",
"gBenchmark->Show(\"clonesarray\");"
]
},
{
"cell_type": "markdown",
"id": "2801bccf",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "cf9a7552",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:03.227593Z",
"iopub.status.busy": "2026-05-19T20:19:03.227469Z",
"iopub.status.idle": "2026-05-19T20:19:03.432721Z",
"shell.execute_reply": "2026-05-19T20:19:03.432234Z"
}
},
"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
}