{
"cells": [
{
"cell_type": "markdown",
"id": "7a47447c",
"metadata": {},
"source": [
"# hsimpleReader\n",
"TTreeReader simplest example.\n",
"\n",
"Read data from hsimple.root (written by hsimple.C)\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Anders Eie, 2013 \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:16 PM."
]
},
{
"cell_type": "markdown",
"id": "0cd55f44",
"metadata": {},
"source": [
"Create a histogram for the values we read."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "874baf78",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:16:58.255149Z",
"iopub.status.busy": "2026-05-19T20:16:58.255021Z",
"iopub.status.idle": "2026-05-19T20:16:58.566307Z",
"shell.execute_reply": "2026-05-19T20:16:58.565720Z"
}
},
"outputs": [],
"source": [
"auto myHist = new TH1F(\"h1\",\"ntuple\",100,-4,4);"
]
},
{
"cell_type": "markdown",
"id": "66ce95dd",
"metadata": {},
"source": [
"Open the file containing the tree."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2b5a7cee",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:16:58.568092Z",
"iopub.status.busy": "2026-05-19T20:16:58.567966Z",
"iopub.status.idle": "2026-05-19T20:16:58.770795Z",
"shell.execute_reply": "2026-05-19T20:16:58.769694Z"
}
},
"outputs": [],
"source": [
"auto myFile = TFile::Open(\"hsimple.root\");\n",
"if (!myFile || myFile->IsZombie()) {\n",
" return;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "92284250",
"metadata": {},
"source": [
"Create a TTreeReader for the tree, for instance by passing the\n",
"TTree's name and the TDirectory / TFile it is in."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3149bc51",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:16:58.772227Z",
"iopub.status.busy": "2026-05-19T20:16:58.772106Z",
"iopub.status.idle": "2026-05-19T20:16:58.977205Z",
"shell.execute_reply": "2026-05-19T20:16:58.976583Z"
}
},
"outputs": [],
"source": [
"TTreeReader myReader(\"ntuple\", myFile);"
]
},
{
"cell_type": "markdown",
"id": "60e6e626",
"metadata": {},
"source": [
"The branch \"px\" contains floats; access them as myPx."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1e68df5b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:16:58.979067Z",
"iopub.status.busy": "2026-05-19T20:16:58.978944Z",
"iopub.status.idle": "2026-05-19T20:16:59.183858Z",
"shell.execute_reply": "2026-05-19T20:16:59.182746Z"
}
},
"outputs": [],
"source": [
"TTreeReaderValue myPx(myReader, \"px\");"
]
},
{
"cell_type": "markdown",
"id": "f094a29e",
"metadata": {},
"source": [
"The branch \"py\" contains floats, too; access those as myPy."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "248505d4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:16:59.185280Z",
"iopub.status.busy": "2026-05-19T20:16:59.185155Z",
"iopub.status.idle": "2026-05-19T20:16:59.389069Z",
"shell.execute_reply": "2026-05-19T20:16:59.388423Z"
}
},
"outputs": [],
"source": [
"TTreeReaderValue myPy(myReader, \"py\");"
]
},
{
"cell_type": "markdown",
"id": "7c3f57e4",
"metadata": {},
"source": [
"Loop over all entries of the TTree or TChain."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f23e32d8",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:16:59.390951Z",
"iopub.status.busy": "2026-05-19T20:16:59.390827Z",
"iopub.status.idle": "2026-05-19T20:16:59.596871Z",
"shell.execute_reply": "2026-05-19T20:16:59.596287Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : created default TCanvas with name c1\n"
]
}
],
"source": [
"while (myReader.Next()) {\n",
" // Just access the data as if myPx and myPy were iterators (note the '*'\n",
" // in front of them):\n",
" myHist->Fill(*myPx + *myPy);\n",
"}\n",
"\n",
"myHist->Draw();"
]
},
{
"cell_type": "markdown",
"id": "38a449b2",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8a52422f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:16:59.598329Z",
"iopub.status.busy": "2026-05-19T20:16:59.598207Z",
"iopub.status.idle": "2026-05-19T20:16:59.803088Z",
"shell.execute_reply": "2026-05-19T20:16:59.802476Z"
}
},
"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
}