{
"cells": [
{
"cell_type": "markdown",
"id": "0b6b8e8c",
"metadata": {},
"source": [
"# tree104_tree\n",
"This example is a variant of hsimple.C but using a TTree instead\n",
"of a TNtuple. It shows:\n",
" - how to fill a Tree with a few simple variables.\n",
" - how to read this Tree\n",
" - how to browse and analyze the Tree via the TBrowser and TTreeViewer\n",
"This example can be run in many different ways:\n",
" 1. Using the Cling interpreter\n",
"```\n",
".x tree104_tree.C\n",
"```\n",
" 2. Using the automatic compiler interface\n",
"```\n",
".x tree104_tree.C++\n",
"```\n",
" 3. \n",
"```\n",
".L tree104_tree.C or .L tree104_tree.C++\n",
"tree1()\n",
"```\n",
"One can also run the write and read parts in two separate sessions.\n",
"For example following one of the sessions above, one can start the session:\n",
"```\n",
" .L tree104_tree.C\n",
" read_tree();\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:17 PM."
]
},
{
"cell_type": "markdown",
"id": "d60efec3",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d5b1b216",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:17:55.171748Z",
"iopub.status.busy": "2026-05-19T20:17:55.171638Z",
"iopub.status.idle": "2026-05-19T20:17:55.185197Z",
"shell.execute_reply": "2026-05-19T20:17:55.184636Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \"TROOT.h\"\n",
"#include \"TFile.h\"\n",
"#include \"TTree.h\"\n",
"#include \"TBrowser.h\"\n",
"#include \"TH2.h\"\n",
"#include \"TRandom.h\"\n",
"\n",
"void tree104_write()\n",
"{\n",
" // create a Tree file tree104.root\n",
"\n",
" // create the file, the Tree and a few branches\n",
" TFile f(\"tree104.root\", \"recreate\");\n",
" TTree t1(\"t1\", \"a simple Tree with simple variables\");\n",
" Float_t px, py, pz;\n",
" Double_t random;\n",
" Int_t ev;\n",
" t1.Branch(\"px\", &px, \"px/F\");\n",
" t1.Branch(\"py\", &py, \"py/F\");\n",
" t1.Branch(\"pz\", &pz, \"pz/F\");\n",
" t1.Branch(\"random\", &random, \"random/D\");\n",
" t1.Branch(\"ev\", &ev, \"ev/I\");\n",
"\n",
" // fill the tree\n",
" for (Int_t i=0; i<10000; i++) {\n",
" gRandom->Rannor(px, py);\n",
" pz = px * px + py * py;\n",
" random = gRandom->Rndm();\n",
" ev = i;\n",
" t1.Fill();\n",
" }\n",
" // save the Tree header. The file will be automatically closed\n",
" // when going out of the function scope\n",
" t1.Write();\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "7323d385",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1744aef9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:17:55.186404Z",
"iopub.status.busy": "2026-05-19T20:17:55.186289Z",
"iopub.status.idle": "2026-05-19T20:17:55.198237Z",
"shell.execute_reply": "2026-05-19T20:17:55.197635Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void tree104_read()\n",
"{\n",
" // read the Tree generated by tree1w and fill two histograms\n",
"\n",
" Float_t px, py, pz;\n",
" Double_t random;\n",
" Int_t ev;\n",
" // note that we create the TFile and TTree objects on the heap!\n",
" // because we want to keep these objects alive when we leave this function.\n",
" auto f = TFile::Open(\"tree104.root\");\n",
" auto t1 = f->Get(\"t1\");\n",
" t1->SetBranchAddress(\"px\", &px);\n",
" t1->SetBranchAddress(\"py\", &py);\n",
" t1->SetBranchAddress(\"pz\", &pz);\n",
" t1->SetBranchAddress(\"random\", &random);\n",
" t1->SetBranchAddress(\"ev\", &ev);\n",
"\n",
" // create two histograms\n",
" auto hpx = new TH1F(\"hpx\", \"px distribution\", 100, -3, 3);\n",
" auto hpxpy = new TH2F(\"hpxpy\", \"py vs px\", 30, -3, 3, 30, -3, 3);\n",
"\n",
" // read all entries and fill the histograms\n",
" Long64_t nentries = t1->GetEntries();\n",
" for (Long64_t i=0; iGetEntry(i);\n",
" hpx->Fill(px);\n",
" hpxpy->Fill(px, py);\n",
" }\n",
"\n",
" // we do not close the file. We want to keep the generated histograms\n",
" // we open a browser and the TreeViewer\n",
" if (gROOT->IsBatch())\n",
" return;\n",
" new TBrowser();\n",
" t1->StartViewer();\n",
" // in the browser, click on \"ROOT Files\", then on \"tree1.root\".\n",
" // you can click on the histogram icons in the right panel to draw them.\n",
" // in the TreeViewer, follow the instructions in the Help button.\n",
"\n",
" // Allow to use the TTree after the end of the function.\n",
" t1->ResetBranchAddresses();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5dc81482",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:17:55.199352Z",
"iopub.status.busy": "2026-05-19T20:17:55.199237Z",
"iopub.status.idle": "2026-05-19T20:17:55.649284Z",
"shell.execute_reply": "2026-05-19T20:17:55.648598Z"
}
},
"outputs": [],
"source": [
"tree104_write();\n",
"tree104_read();"
]
}
],
"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
}