{
"cells": [
{
"cell_type": "markdown",
"id": "79d992b4",
"metadata": {},
"source": [
"# tree201_histograms\n",
"Save histograms in Tree branches\n",
"\n",
"To run this example, do\n",
"```cpp\n",
"root > .L tree201_histograms.C\n",
"root > htw()\n",
"root > htr1()\n",
"root > htr2()\n",
"root > htr3()\n",
"```\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:19 PM."
]
},
{
"cell_type": "markdown",
"id": "11523412",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6f75f24e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:43.755364Z",
"iopub.status.busy": "2026-05-19T20:19:43.755252Z",
"iopub.status.idle": "2026-05-19T20:19:43.775994Z",
"shell.execute_reply": "2026-05-19T20:19:43.775370Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"void write_histograms()\n",
"{\n",
" // Create a Tree with a few branches of type histogram\n",
" // 25000 entries are filled in the Tree\n",
" // For each entry, the copy of 3 histograms is written\n",
" // The data base will contain 75000 histograms.\n",
" gBenchmark->Start(\"write_histograms\");\n",
" TFile f(\"ht.root\", \"recreate\");\n",
" auto T = new TTree(\"T\", \"test\");\n",
" auto hpx = new TH1F(\"hpx\", \"This is the px distribution\", 100, -4, 4);\n",
" auto hpxpy = new TH2F(\"hpxpy\", \"py vs px\", 40, -4, 4, 40, -4, 4);\n",
" auto hprof = new TProfile(\"hprof\", \"Profile of pz versus px\", 100, -4, 4, 0, 20);\n",
" T->Branch(\"hpx\", \"TH1F\", &hpx, 32000, 0);\n",
" T->Branch(\"hpxpy\", \"TH2F\", &hpxpy, 32000, 0);\n",
" T->Branch(\"hprof\", \"TProfile\", &hprof, 32000, 0);\n",
" Float_t px, py, pz;\n",
" for (Int_t i = 0; i < 25000; i++) {\n",
" if (i % 1000 == 0)\n",
" printf(\"at entry: %d\\n\", i);\n",
" gRandom->Rannor(px, py);\n",
" pz = px * px + py * py;\n",
" hpx->Fill(px);\n",
" hpxpy->Fill(px ,py);\n",
" hprof->Fill(px, pz);\n",
" T->Fill();\n",
" }\n",
" T->Print();\n",
" f.Write();\n",
" gBenchmark->Show(\"write_histograms\");\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "14f6a8e1",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "00575917",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:43.777271Z",
"iopub.status.busy": "2026-05-19T20:19:43.777152Z",
"iopub.status.idle": "2026-05-19T20:19:43.787465Z",
"shell.execute_reply": "2026-05-19T20:19:43.786889Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void read_histogram1()\n",
"{\n",
" // Connect Tree generated by htw and show histograms for entry 12345\n",
" auto f = TFile::Open(\"ht.root\");\n",
" auto T = f->Get(\"T\");\n",
" TH1F *hpx = nullptr;\n",
" TH2F *hpxpy = nullptr;\n",
" TProfile *hprof = nullptr;\n",
" T->SetBranchAddress(\"hpx\", &hpx);\n",
" T->SetBranchAddress(\"hpxpy\", &hpxpy);\n",
" T->SetBranchAddress(\"hprof\", &hprof);\n",
" T->GetEntry(12345);\n",
" auto c1 = new TCanvas(\"c1\", \"test\", 10, 10, 600, 1000);\n",
" c1->Divide(1, 3);\n",
" c1->cd(1);\n",
" hpx->Draw();\n",
" c1->cd(2);\n",
" hpxpy->Draw();\n",
" c1->cd(3);\n",
" hprof->Draw();\n",
" c1->Print(\"htr1.png\");\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "80f70daa",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0d4d78d3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:43.788717Z",
"iopub.status.busy": "2026-05-19T20:19:43.788578Z",
"iopub.status.idle": "2026-05-19T20:19:43.792347Z",
"shell.execute_reply": "2026-05-19T20:19:43.791843Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void read_histogram2()\n",
"{\n",
" // Connect Tree generated by htw and show histograms for entry 12345\n",
" // a variant of read_histogram1\n",
" auto f = TFile::Open(\"ht.root\");\n",
" auto T = f->Get(\"T\");\n",
" auto c1 = new TCanvas(\"c1\", \"test\", 10, 10, 600, 1000);\n",
" c1->Divide(1, 3);\n",
" c1->cd(1);\n",
" T->Draw(\"hpx.Draw()\", \"\", \"goff\", 1, 12345);\n",
" c1->cd(2);\n",
" T->Draw(\"hpxpy.Draw()\", \"\", \"goff\", 1, 12345);\n",
" c1->cd(3);\n",
" T->Draw(\"hprof.Draw()\", \"\", \"goff\", 1, 12345);\n",
" c1->Print(\"htr2.png\");\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "2791f138",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "51b936ab",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:43.793569Z",
"iopub.status.busy": "2026-05-19T20:19:43.793455Z",
"iopub.status.idle": "2026-05-19T20:19:43.796862Z",
"shell.execute_reply": "2026-05-19T20:19:43.796328Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void read_histogram3()\n",
"{\n",
" // Connect Tree generated by htw\n",
" // read all histograms and plot the RMS of hpx versus the Mean of hprof\n",
" // for each of the 25000 entries\n",
" auto f = TFile::Open(\"ht.root\");\n",
" auto T = f->Get(\"T\");\n",
" auto c1 = new TCanvas(\"c1\", \"test\", 10, 10, 600, 400);\n",
" T->Draw(\"hpx.GetRMS():hprof.GetMean()\");\n",
" c1->Print(\"htr3.png\");\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ba70710b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:43.798012Z",
"iopub.status.busy": "2026-05-19T20:19:43.797900Z",
"iopub.status.idle": "2026-05-19T20:19:45.523070Z",
"shell.execute_reply": "2026-05-19T20:19:45.522337Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"at entry: 0\n",
"at entry: 1000\n",
"at entry: 2000\n",
"at entry: 3000\n",
"at entry: 4000\n",
"at entry: 5000\n",
"at entry: 6000\n",
"at entry: 7000\n",
"at entry: 8000\n",
"at entry: 9000\n",
"at entry: 10000\n",
"at entry: 11000\n",
"at entry: 12000\n",
"at entry: 13000\n",
"at entry: 14000\n",
"at entry: 15000\n",
"at entry: 16000\n",
"at entry: 17000\n",
"at entry: 18000\n",
"at entry: 19000\n",
"at entry: 20000\n",
"at entry: 21000\n",
"at entry: 22000\n",
"at entry: 23000\n",
"at entry: 24000\n",
"******************************************************************************\n",
"*Tree :T : test *\n",
"*Entries : 25000 : Total = 292585154 bytes File Size = 26323564 *\n",
"* : : Tree compression factor = 11.11 *\n",
"******************************************************************************\n",
"*Br 0 :hpx : TH1F *\n",
"*Entries : 25000 : Total Size= 24075398 bytes File Size = 1520577 *\n",
"*Baskets : 757 : Basket Size= 32000 bytes Compression= 15.81 *\n",
"*............................................................................*\n",
"*Br 1 :hpxpy : TH2F *\n",
"*Entries : 25000 : Total Size= 191656554 bytes File Size = 17485787 *\n",
"*Baskets : 6250 : Basket Size= 32000 bytes Compression= 10.95 *\n",
"*............................................................................*\n",
"*Br 2 :hprof : TProfile *\n",
"*Entries : 25000 : Total Size= 76852808 bytes File Size = 7317200 *\n",
"*Baskets : 2500 : Basket Size= 32000 bytes Compression= 10.49 *\n",
"*............................................................................*\n",
"write_histograms: Real Time = 0.90 seconds Cpu Time = 0.90 seconds\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : png file htr1.png has been created\n",
"Warning in : Deleting canvas with same name: c1\n",
"Info in : png file htr2.png has been created\n",
"Warning in : Deleting canvas with same name: c1\n",
"Info in : png file htr3.png has been created\n"
]
}
],
"source": [
"write_histograms();\n",
"read_histogram1();\n",
"read_histogram2();\n",
"read_histogram3();"
]
},
{
"cell_type": "markdown",
"id": "72b39dc1",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "36b65e80",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:19:45.524876Z",
"iopub.status.busy": "2026-05-19T20:19:45.524745Z",
"iopub.status.idle": "2026-05-19T20:19:45.730378Z",
"shell.execute_reply": "2026-05-19T20:19:45.729685Z"
}
},
"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
}