{ "cells": [ { "cell_type": "markdown", "id": "f8f5175c", "metadata": {}, "source": [ "# vectorizedFit\n", "Tutorial for creating a Vectorized TF1 function using a formula expression and\n", "use it for fitting an histogram\n", "\n", "To create a vectorized function (if ROOT has been compiled with support for vectorization)\n", "is very easy. One needs to create the TF1 object with the option \"VEC\" or call the method\n", "TF1::SetVectorized\n", "\n", "\n", "\n", "\n", "**Author:** Lorenzo Moneta \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:25 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "c49a6590", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:23.810348Z", "iopub.status.busy": "2026-05-19T20:25:23.810217Z", "iopub.status.idle": "2026-05-19T20:25:24.768820Z", "shell.execute_reply": "2026-05-19T20:25:24.759439Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Doing Serial Gaussian Fit \n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 40217.9\n", "NDf = 39409\n", "Edm = 3.38998e-08\n", "NCalls = 75\n", "Constant = 120.018 +/- 0.105817 \n", "Mean = 0.00114402 +/- 0.000709328 \n", "Sigma = 0.979817 +/- 0.000519995 \t (limited)\n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "MinFCN = 20355.3\n", "Chi2 = 40710.5\n", "NDf = 39997\n", "Edm = 8.96571e-10\n", "NCalls = 67\n", "Constant = 120.024 +/- 0.105551 \n", "Mean = 0.000138332 +/- 0.000716607 \n", "Sigma = 0.99985 +/- 0.000537073 \t (limited)\n", "Real time 0:00:00, CP time 0.190\n", "Doing Vectorized Gaussian Fit \n" ] } ], "source": [ "gStyle->SetOptFit(111111);\n", "\n", "\n", "ROOT::Math::MinimizerOptions::SetDefaultMinimizer(\"Minuit2\");\n", "\n", "int nbins = 40000;\n", "auto h1 = new TH1D(\"h1\",\"h1\",nbins,-3,3);\n", "h1->FillRandom(\"gaus\",nbins*50);\n", "auto c1 = new TCanvas(\"Fit\",\"Fit\",800,1000);\n", "c1->Divide(1,2);\n", "c1->cd(1);\n", "TStopwatch w;\n", "\n", "std::cout << \"Doing Serial Gaussian Fit \" << std::endl;\n", "auto f1 = new TF1(\"f1\",\"gaus\");\n", "f1->SetNpx(nbins*10);\n", "w.Start();\n", "h1->Fit(f1);\n", "h1->Fit(f1,\"L+\");\n", "w.Print();\n", "\n", "std::cout << \"Doing Vectorized Gaussian Fit \" << std::endl;\n", "auto f2 = new TF1(\"f2\",\"gaus\",-3,3,\"VEC\");" ] }, { "cell_type": "markdown", "id": "03e38487", "metadata": {}, "source": [ "alternatively you can also use the TF1::SetVectorized function\n", "f2->SetVectorized(true);" ] }, { "cell_type": "code", "execution_count": 2, "id": "37ca218a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:24.773963Z", "iopub.status.busy": "2026-05-19T20:25:24.773825Z", "iopub.status.idle": "2026-05-19T20:25:25.125415Z", "shell.execute_reply": "2026-05-19T20:25:25.124938Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 40217.9\n", "NDf = 39409\n", "Edm = 3.38998e-08\n", "NCalls = 75\n", "Constant = 120.018 +/- 0.105817 \n", "Mean = 0.00114402 +/- 0.000709328 \n", "Sigma = 0.979817 +/- 0.000519995 \t (limited)\n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "MinFCN = 20355.3\n", "Chi2 = 40710.5\n", "NDf = 39997\n", "Edm = 8.96571e-10\n", "NCalls = 67\n", "Constant = 120.024 +/- 0.105551 \n", "Mean = 0.000138332 +/- 0.000716607 \n", "Sigma = 0.99985 +/- 0.000537073 \t (limited)\n", "Real time 0:00:00, CP time 0.140\n" ] } ], "source": [ "w.Start();\n", "h1->Fit(f2);\n", "h1->Fit(f2,\"L+\");\n", "w.Print();" ] }, { "cell_type": "markdown", "id": "69a39704", "metadata": {}, "source": [ "rebin histograms and scale it back to the function" ] }, { "cell_type": "code", "execution_count": 3, "id": "39e154ca", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:25.127057Z", "iopub.status.busy": "2026-05-19T20:25:25.126943Z", "iopub.status.idle": "2026-05-19T20:25:25.331454Z", "shell.execute_reply": "2026-05-19T20:25:25.330822Z" } }, "outputs": [], "source": [ "h1->Rebin(nbins/100);\n", "h1->Scale(100./nbins);\n", "((TF1 *)h1->GetListOfFunctions()->At(0))->SetTitle(\"Chi2 Fit\");\n", "((TF1 *)h1->GetListOfFunctions()->At(1))->SetTitle(\"Likelihood Fit\");\n", "((TF1 *)h1->GetListOfFunctions()->At(1))->SetLineColor(kBlue);" ] }, { "cell_type": "markdown", "id": "42fc2643", "metadata": {}, "source": [ "c1->cd(1)->BuildLegend();" ] }, { "cell_type": "markdown", "id": "206f7efd", "metadata": {}, "source": [ "Do a polynomial fit now" ] }, { "cell_type": "code", "execution_count": 4, "id": "e3c316ca", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:25.334194Z", "iopub.status.busy": "2026-05-19T20:25:25.334077Z", "iopub.status.idle": "2026-05-19T20:25:25.540944Z", "shell.execute_reply": "2026-05-19T20:25:25.540576Z" } }, "outputs": [], "source": [ "c1->cd(2);\n", "auto f3 = new TF1(\"f3\",\"[A]*x^2+[B]*x+[C]\",0,10);\n", "f3->SetParameters(0.5,3,2);\n", "f3->SetNpx(nbins*10);" ] }, { "cell_type": "markdown", "id": "3fcadb9d", "metadata": {}, "source": [ "generate the events" ] }, { "cell_type": "code", "execution_count": 5, "id": "12762562", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:25.542890Z", "iopub.status.busy": "2026-05-19T20:25:25.542776Z", "iopub.status.idle": "2026-05-19T20:25:26.015873Z", "shell.execute_reply": "2026-05-19T20:25:26.015453Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Doing Serial Polynomial Fit \n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 37690\n", "NDf = 38075\n", "Edm = 1.01067e-14\n", "NCalls = 72\n", "A = 0.202001 +/- 0.00176461 \n", "B = 0.268032 +/- 0.0153893 \n", "C = 1.05504 +/- 0.0248331 \n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "MinFCN = 20527.5\n", "Chi2 = 41055\n", "NDf = 39997\n", "Edm = 8.16246e-08\n", "NCalls = 90\n", "A = 0.149763 +/- 0.00165111 \n", "B = 0.880262 +/- 0.0135143 \n", "C = 0.6066 +/- 0.0181308 \n", "Real time 0:00:00, CP time 0.130\n", "Doing Vectorized Polynomial Fit \n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 37690\n", "NDf = 38075\n", "Edm = 1.32608e-15\n", "NCalls = 68\n", "A = 0.202001 +/- 0.00176461 \n", "B = 0.268032 +/- 0.0153893 \n", "C = 1.05504 +/- 0.0248331 \n", "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "MinFCN = 20527.5\n", "Chi2 = 41055\n", "NDf = 39997\n", "Edm = 8.1626e-08\n", "NCalls = 90\n", "A = 0.149763 +/- 0.00165111 \n", "B = 0.880262 +/- 0.0135143 \n", "C = 0.6066 +/- 0.0181308 \n", "Real time 0:00:00, CP time 0.120\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning in : Cannot set vectorized -- try building with C++20 on Linux\n" ] } ], "source": [ "auto h2 = new TH1D(\"h2\",\"h2\",nbins,0,10);\n", "h2->FillRandom(\"f3\",10*nbins);\n", "std::cout << \"Doing Serial Polynomial Fit \" << std::endl;\n", "f3->SetParameters(2,2,2);\n", "w.Start();\n", "h2->Fit(f3);\n", "h2->Fit(f3,\"L+\");\n", "w.Print();\n", "\n", "std::cout << \"Doing Vectorized Polynomial Fit \" << std::endl;\n", "auto f4 = new TF1(\"f4\",\"[A]*x*x+[B]*x+[C]\",0,10);\n", "f4->SetVectorized(true);\n", "f4->SetParameters(2,2,2);\n", "w.Start();\n", "h2->Fit(f4);\n", "h2->Fit(f4,\"L+\");\n", "w.Print();" ] }, { "cell_type": "markdown", "id": "77d901ce", "metadata": {}, "source": [ "rebin histograms and scale it back to the function" ] }, { "cell_type": "code", "execution_count": 6, "id": "b825a3f8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:26.017173Z", "iopub.status.busy": "2026-05-19T20:25:26.017059Z", "iopub.status.idle": "2026-05-19T20:25:26.221379Z", "shell.execute_reply": "2026-05-19T20:25:26.220889Z" } }, "outputs": [], "source": [ "h2->Rebin(nbins/100);\n", "h2->Scale(100./nbins);\n", "((TF1 *)h2->GetListOfFunctions()->At(0))->SetTitle(\"Chi2 Fit\");\n", "((TF1 *)h2->GetListOfFunctions()->At(1))->SetTitle(\"Likelihood Fit\");\n", "((TF1 *)h2->GetListOfFunctions()->At(1))->SetLineColor(kBlue);" ] }, { "cell_type": "markdown", "id": "7842d224", "metadata": {}, "source": [ "c1->cd(2)->BuildLegend();" ] }, { "cell_type": "markdown", "id": "0d20fd64", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 7, "id": "803ea6c5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:26.222948Z", "iopub.status.busy": "2026-05-19T20:25:26.222832Z", "iopub.status.idle": "2026-05-19T20:25:26.536739Z", "shell.execute_reply": "2026-05-19T20:25:26.536258Z" } }, "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 }