{ "cells": [ { "cell_type": "markdown", "id": "87212da1", "metadata": {}, "source": [ "# hist002_TH1_fillrandom_userfunc\n", "Fill a 1D histogram from a user-defined parametric function.\n", "\n", "\n", "\n", "\n", "**Author:** Rene Brun, Giacomo Parolini \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:11 PM." ] }, { "cell_type": "markdown", "id": "86130fba", "metadata": {}, "source": [ "Create a user-defined formula.\n", "A function (any dimension) or a formula may reference an already defined formula" ] }, { "cell_type": "code", "execution_count": 1, "id": "1666018a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:43.598798Z", "iopub.status.busy": "2026-05-19T20:11:43.598674Z", "iopub.status.idle": "2026-05-19T20:11:43.909853Z", "shell.execute_reply": "2026-05-19T20:11:43.909237Z" } }, "outputs": [], "source": [ "TFormula form1(\"form1\", \"abs(sin(x)/x)\");" ] }, { "cell_type": "markdown", "id": "a2eee917", "metadata": {}, "source": [ "Create a 1D function using the formula defined above and the predefined \"gaus\" formula." ] }, { "cell_type": "code", "execution_count": 2, "id": "40d35619", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:43.911681Z", "iopub.status.busy": "2026-05-19T20:11:43.911520Z", "iopub.status.idle": "2026-05-19T20:11:44.133416Z", "shell.execute_reply": "2026-05-19T20:11:44.128872Z" } }, "outputs": [], "source": [ "double rangeMin = 0.0;\n", "double rangeMax = 10.0;\n", "TF1 sqroot(\"sqroot\", \"x*gaus(0) + [3]*form1\", rangeMin, rangeMax);\n", "sqroot.SetLineColor(4);\n", "sqroot.SetLineWidth(6);" ] }, { "cell_type": "markdown", "id": "9cb8cc22", "metadata": {}, "source": [ "Set parameters to the functions \"gaus\" and \"form1\"." ] }, { "cell_type": "code", "execution_count": 3, "id": "e3a3b7f5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:44.142167Z", "iopub.status.busy": "2026-05-19T20:11:44.141990Z", "iopub.status.idle": "2026-05-19T20:11:44.355861Z", "shell.execute_reply": "2026-05-19T20:11:44.355405Z" } }, "outputs": [], "source": [ "double gausScale = 10.0; // [0]\n", "double gausMean = 4.0; // [1]\n", "double gausVar = 1.0; // [2]\n", "double form1Scale = 20.0; // [3]\n", "sqroot.SetParameters(gausScale, gausMean, gausVar, form1Scale);" ] }, { "cell_type": "markdown", "id": "4ec18389", "metadata": {}, "source": [ "Create a one dimensional histogram and fill it following the distribution in function sqroot." ] }, { "cell_type": "code", "execution_count": 4, "id": "b34d8fb7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:44.361440Z", "iopub.status.busy": "2026-05-19T20:11:44.361313Z", "iopub.status.idle": "2026-05-19T20:11:44.564753Z", "shell.execute_reply": "2026-05-19T20:11:44.564382Z" } }, "outputs": [], "source": [ "int nBins = 200;\n", "TH1D h1d(\"h1d\", \"Test random numbers\", nBins, rangeMin, rangeMax);" ] }, { "cell_type": "markdown", "id": "1be6b39d", "metadata": {}, "source": [ "Use our user-defined function to fill the histogram with random values sampled from it." ] }, { "cell_type": "code", "execution_count": 5, "id": "92332ef7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:44.572210Z", "iopub.status.busy": "2026-05-19T20:11:44.572084Z", "iopub.status.idle": "2026-05-19T20:11:44.784401Z", "shell.execute_reply": "2026-05-19T20:11:44.783592Z" } }, "outputs": [], "source": [ "h1d.FillRandom(\"sqroot\", 10000);" ] }, { "cell_type": "markdown", "id": "e357597e", "metadata": {}, "source": [ "Open a ROOT file and save the formula, function and histogram" ] }, { "cell_type": "code", "execution_count": 6, "id": "408f7e7b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:44.786516Z", "iopub.status.busy": "2026-05-19T20:11:44.786379Z", "iopub.status.idle": "2026-05-19T20:11:45.120355Z", "shell.execute_reply": "2026-05-19T20:11:45.119703Z" } }, "outputs": [], "source": [ "auto myFile = std::unique_ptr(TFile::Open(\"fillrandom_userfunc.root\", \"RECREATE\"));\n", "myFile->WriteObject(&form1, form1.GetName());\n", "myFile->WriteObject(&sqroot, sqroot.GetName());\n", "myFile->WriteObject(&h1d, h1d.GetName());" ] }, { "cell_type": "markdown", "id": "04d4d7ff", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 7, "id": "17a3106e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:45.159034Z", "iopub.status.busy": "2026-05-19T20:11:45.158867Z", "iopub.status.idle": "2026-05-19T20:11:45.367479Z", "shell.execute_reply": "2026-05-19T20:11:45.366782Z" } }, "outputs": [], "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 }