{ "cells": [ { "cell_type": "markdown", "id": "e7d21dc5", "metadata": {}, "source": [ "# hist105_TExec_dynamic_slice\n", "\n", "\n", "\n", "\n", "**Author:** Rene Brun, Sergey Linev \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:13 PM." ] }, { "cell_type": "markdown", "id": "6d6546ef", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 1, "id": "9f07fc06", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.328702Z", "iopub.status.busy": "2026-05-19T20:13:18.328569Z", "iopub.status.idle": "2026-05-19T20:13:18.346796Z", "shell.execute_reply": "2026-05-19T20:13:18.346300Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void DynamicExec()\n", "{\n", " // Example of function called when a mouse event occurs in a pad.\n", " // When moving the mouse in the canvas, a second canvas shows the\n", " // projection along X of the bin corresponding to the Y position\n", " // of the mouse. The resulting histogram is fitted with a gaussian.\n", " // A \"dynamic\" line shows the current bin position in Y.\n", " // This more elaborated example can be used as a starting point\n", " // to develop more powerful interactive applications exploiting Cling\n", " // as a development engine.\n", "\n", " static int pyold = 0;\n", "\n", " float uxmin = gPad->GetUxmin();\n", " float uxmax = gPad->GetUxmax();\n", " int pxmin = gPad->XtoAbsPixel(uxmin);\n", " int pxmax = gPad->XtoAbsPixel(uxmax);\n", " int px = gPad->GetEventX();\n", " int py = gPad->GetEventY();\n", " TObject *select = gPad->GetSelected();\n", "\n", " gPad->GetCanvas()->FeedbackMode(kTRUE);\n", " if (pyold) {\n", " // erase line at old position\n", " gVirtualX->DrawLine(pxmin, pyold, pxmax, pyold);\n", " pyold = 0;\n", " }\n", "\n", " if (!select || !select->InheritsFrom(TH2::Class()))\n", " return;\n", "\n", " TH2 *h = (TH2 *)select;\n", "\n", " // draw a line at current position\n", " gVirtualX->DrawLine(pxmin, py, pxmax, py);\n", " pyold = py;\n", "\n", " Float_t upy = gPad->AbsPixeltoY(py);\n", " Float_t y = gPad->PadtoY(upy);\n", "\n", " // create or set the new canvas c2\n", " TVirtualPad *padsav = gPad;\n", " TCanvas *c2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject(\"c2\");\n", " if (c2)\n", " delete c2->GetPrimitive(\"Projection\");\n", " else\n", " c2 = new TCanvas(\"c2\", \"Projection Canvas\", 710, 10, 700, 500);\n", " c2->SetGrid();\n", " c2->cd();\n", "\n", " // draw slice corresponding to mouse position\n", " Int_t biny = h->GetYaxis()->FindBin(y);\n", " TH1D *hp = h->ProjectionX(\"\", biny, biny);\n", " hp->SetFillColor(38);\n", " hp->SetName(\"Projection\");\n", " hp->SetTitle(TString::Format(\"Projection of biny=%d\", biny));\n", " hp->Fit(\"gaus\", \"ql\");\n", " hp->GetFunction(\"gaus\")->SetLineColor(kRed);\n", " hp->GetFunction(\"gaus\")->SetLineWidth(6);\n", " c2->Update();\n", " padsav->cd();\n", "}" ] }, { "cell_type": "markdown", "id": "c8115081", "metadata": {}, "source": [ "Create a new canvas." ] }, { "cell_type": "code", "execution_count": 2, "id": "8a03cf18", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.348106Z", "iopub.status.busy": "2026-05-19T20:13:18.347988Z", "iopub.status.idle": "2026-05-19T20:13:18.662451Z", "shell.execute_reply": "2026-05-19T20:13:18.661867Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"c1\", \"Dynamic Slice Example\", 10, 10, 700, 500);" ] }, { "cell_type": "markdown", "id": "26e144c4", "metadata": {}, "source": [ "create a 2-d histogram, fill and draw it" ] }, { "cell_type": "code", "execution_count": 3, "id": "a60fdb02", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.664214Z", "iopub.status.busy": "2026-05-19T20:13:18.664098Z", "iopub.status.idle": "2026-05-19T20:13:18.867370Z", "shell.execute_reply": "2026-05-19T20:13:18.866891Z" } }, "outputs": [], "source": [ "TH2F *hpxpy = new TH2F(\"hpxpy\", \"py vs px\", 40, -4, 4, 40, -4, 4);\n", "hpxpy->SetStats(0);\n", "Double_t px, py;\n", "for (Int_t i = 0; i < 50000; i++) {\n", " gRandom->Rannor(px, py);\n", " hpxpy->Fill(px, py);\n", "}\n", "hpxpy->Draw(\"col\");" ] }, { "cell_type": "markdown", "id": "ef7c5eda", "metadata": {}, "source": [ "Add a TExec object to the canvas" ] }, { "cell_type": "code", "execution_count": 4, "id": "b893177f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.869367Z", "iopub.status.busy": "2026-05-19T20:13:18.869253Z", "iopub.status.idle": "2026-05-19T20:13:19.072450Z", "shell.execute_reply": "2026-05-19T20:13:19.071953Z" } }, "outputs": [], "source": [ "c1->AddExec(\"dynamic\", \"DynamicExec()\");" ] }, { "cell_type": "markdown", "id": "1f61717c", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "ec98c218", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:19.074229Z", "iopub.status.busy": "2026-05-19T20:13:19.074117Z", "iopub.status.idle": "2026-05-19T20:13:19.294536Z", "shell.execute_reply": "2026-05-19T20:13:19.294009Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "