{ "cells": [ { "cell_type": "markdown", "id": "e2111bb1", "metadata": {}, "source": [ "# hist019_TH2_projection\n", "A TExec allows to redraw automatically the projections when a zoom is performed\n", "on the 2D histogram.\n", "\n", "\n", "\n", "\n", "**Author:** Olivier Couet \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:12 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "9c8616bb", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:21.860701Z", "iopub.status.busy": "2026-05-19T20:12:21.860567Z", "iopub.status.idle": "2026-05-19T20:12:22.201199Z", "shell.execute_reply": "2026-05-19T20:12:22.200329Z" } }, "outputs": [], "source": [ "TH2F *h2;\n", "TH1D *projh2X;\n", "TH1D *projh2Y;\n", "TPad *right_pad, *top_pad;" ] }, { "cell_type": "markdown", "id": "b2ab31f7", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "98bc493b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:22.203166Z", "iopub.status.busy": "2026-05-19T20:12:22.203040Z", "iopub.status.idle": "2026-05-19T20:12:22.211583Z", "shell.execute_reply": "2026-05-19T20:12:22.211076Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void ZoomExec()\n", "{\n", " int xfirst = h2->GetXaxis()->GetFirst();\n", " int xlast = h2->GetXaxis()->GetLast();\n", " double xmin = h2->GetXaxis()->GetBinLowEdge(xfirst);\n", " double xmax = h2->GetXaxis()->GetBinUpEdge(xlast);\n", " projh2X->GetXaxis()->SetRangeUser(xmin, xmax);\n", " top_pad->Modified();\n", "\n", " int yfirst = h2->GetYaxis()->GetFirst();\n", " int ylast = h2->GetYaxis()->GetLast();\n", " double ymin = h2->GetYaxis()->GetBinLowEdge(yfirst);\n", " double ymax = h2->GetYaxis()->GetBinUpEdge(ylast);\n", " projh2Y->GetXaxis()->SetRangeUser(ymin, ymax);\n", " right_pad->Modified();\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "id": "7bb9c181", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:22.213063Z", "iopub.status.busy": "2026-05-19T20:12:22.212944Z", "iopub.status.idle": "2026-05-19T20:12:22.432811Z", "shell.execute_reply": "2026-05-19T20:12:22.432225Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "auto c1 = new TCanvas(\"c1\", \"c1\", 900, 900);\n", "gStyle->SetOptStat(0);\n", "\n", "TPad *center_pad = new TPad(\"center_pad\", \"center_pad\", 0.0, 0.0, 0.6, 0.6);\n", "center_pad->Draw();\n", "\n", "right_pad = new TPad(\"right_pad\", \"right_pad\", 0.55, 0.0, 1.0, 0.6);\n", "right_pad->Draw();\n", "\n", "top_pad = new TPad(\"top_pad\", \"top_pad\", 0.0, 0.55, 0.6, 1.0);\n", "top_pad->Draw();\n", "\n", "h2 = new TH2F(\"h2\", \"\", 40, -4, 4, 40, -20, 20);\n", "Float_t px, py;\n", "for (Int_t i = 0; i < 25000; i++) {\n", " gRandom->Rannor(px, py);\n", " h2->Fill(px, 5 * py);\n", "}\n", "projh2X = h2->ProjectionX();\n", "projh2Y = h2->ProjectionY();\n", "\n", "center_pad->cd();\n", "gStyle->SetPalette(1);\n", "h2->Draw(\"COL\");\n", "\n", "top_pad->cd();\n", "projh2X->SetFillColor(kBlue + 1);\n", "projh2X->Draw(\"bar\");\n", "\n", "right_pad->cd();\n", "projh2Y->SetFillColor(kBlue - 2);\n", "projh2Y->Draw(\"hbar\");\n", "\n", "c1->cd();\n", "TLatex t;\n", "t.SetTextFont(42);\n", "t.SetTextSize(0.02);\n", "t.DrawLatex(0.6, 0.88, \"This example demonstrates how to display\");\n", "t.DrawLatex(0.6, 0.85, \"a histogram and its two projections.\");\n", "\n", "auto ex = new TExec(\"zoom\", \"ZoomExec()\");\n", "h2->GetListOfFunctions()->Add(ex);" ] }, { "cell_type": "markdown", "id": "4992b7e3", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 4, "id": "af508101", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:22.435794Z", "iopub.status.busy": "2026-05-19T20:12:22.435650Z", "iopub.status.idle": "2026-05-19T20:12:22.641501Z", "shell.execute_reply": "2026-05-19T20:12:22.640813Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%jsroot on\n", "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 }