{ "cells": [ { "cell_type": "markdown", "id": "56cce46b", "metadata": {}, "source": [ "# rf704_amplitudefit\n", "Special pdf's: using a pdf defined by a sum of real-valued amplitude components\n", "\n", "\n", "\n", "\n", "**Author:** Wouter Verkerke \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:34 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "d7beb98c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:34.666976Z", "iopub.status.busy": "2026-05-19T20:34:34.666867Z", "iopub.status.idle": "2026-05-19T20:34:34.682352Z", "shell.execute_reply": "2026-05-19T20:34:34.681794Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooFormulaVar.h\"\n", "#include \"RooRealSumPdf.h\"\n", "#include \"RooPolyVar.h\"\n", "#include \"RooProduct.h\"\n", "#include \"TH1.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"RooPlot.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "06b72811", "metadata": {}, "source": [ "Setup 2D amplitude functions\n", "-------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "9427d165", "metadata": {}, "source": [ "Observables" ] }, { "cell_type": "code", "execution_count": 2, "id": "89f50d23", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:34.683991Z", "iopub.status.busy": "2026-05-19T20:34:34.683869Z", "iopub.status.idle": "2026-05-19T20:34:35.043521Z", "shell.execute_reply": "2026-05-19T20:34:35.042821Z" } }, "outputs": [], "source": [ "RooRealVar t(\"t\", \"time\", -1., 15.);\n", "RooRealVar cosa(\"cosa\", \"cos(alpha)\", -1., 1.);\n", "\n", "RooRealVar tau(\"tau\", \"#tau\", 1.5);\n", "RooRealVar deltaGamma(\"deltaGamma\", \"deltaGamma\", 0.3);\n", "RooFormulaVar coshG(\"coshGBasis\", \"exp(-@0/ @1)*cosh(@0*@2/2)\", {t, tau, deltaGamma});\n", "RooFormulaVar sinhG(\"sinhGBasis\", \"exp(-@0/ @1)*sinh(@0*@2/2)\", {t, tau, deltaGamma});" ] }, { "cell_type": "markdown", "id": "ed6f3c39", "metadata": {}, "source": [ "Construct polynomial amplitudes in cos(a)" ] }, { "cell_type": "code", "execution_count": 3, "id": "1f12d106", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:35.045533Z", "iopub.status.busy": "2026-05-19T20:34:35.045384Z", "iopub.status.idle": "2026-05-19T20:34:35.255425Z", "shell.execute_reply": "2026-05-19T20:34:35.254734Z" } }, "outputs": [], "source": [ "RooPolyVar poly1(\"poly1\", \"poly1\", cosa, RooArgList{0.5, 0.2, 0.2}, 0);\n", "RooPolyVar poly2(\"poly2\", \"poly2\", cosa, RooArgList{1.0, -0.2, 3.0}, 0);" ] }, { "cell_type": "markdown", "id": "20a1539b", "metadata": {}, "source": [ "Construct 2D amplitude as uncorrelated product of amp(t)*amp(cosa)" ] }, { "cell_type": "code", "execution_count": 4, "id": "ac60c856", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:35.257572Z", "iopub.status.busy": "2026-05-19T20:34:35.257450Z", "iopub.status.idle": "2026-05-19T20:34:35.466316Z", "shell.execute_reply": "2026-05-19T20:34:35.465644Z" } }, "outputs": [], "source": [ "RooProduct ampl1(\"ampl1\", \"amplitude 1\", {poly1, coshG});\n", "RooProduct ampl2(\"ampl2\", \"amplitude 2\", {poly2, sinhG});" ] }, { "cell_type": "markdown", "id": "291901b8", "metadata": {}, "source": [ "Construct amplitude sum pdf\n", "-----------------------------------------------------" ] }, { "cell_type": "markdown", "id": "cee6a81b", "metadata": {}, "source": [ "Amplitude strengths" ] }, { "cell_type": "code", "execution_count": 5, "id": "78490af0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:35.468409Z", "iopub.status.busy": "2026-05-19T20:34:35.468289Z", "iopub.status.idle": "2026-05-19T20:34:35.677316Z", "shell.execute_reply": "2026-05-19T20:34:35.676513Z" } }, "outputs": [], "source": [ "RooRealVar f1(\"f1\", \"f1\", 1, 0, 2);\n", "RooRealVar f2(\"f2\", \"f2\", 0.5, 0, 2);" ] }, { "cell_type": "markdown", "id": "895c02fa", "metadata": {}, "source": [ "Construct pdf" ] }, { "cell_type": "code", "execution_count": 6, "id": "04864d73", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:35.679669Z", "iopub.status.busy": "2026-05-19T20:34:35.679537Z", "iopub.status.idle": "2026-05-19T20:34:35.888471Z", "shell.execute_reply": "2026-05-19T20:34:35.887764Z" } }, "outputs": [], "source": [ "RooRealSumPdf pdf(\"pdf\", \"pdf\", RooArgList(ampl1, ampl2), RooArgList(f1, f2));" ] }, { "cell_type": "markdown", "id": "20da4d3a", "metadata": {}, "source": [ "Generate some toy data from pdf" ] }, { "cell_type": "code", "execution_count": 7, "id": "7179cb4e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:35.890533Z", "iopub.status.busy": "2026-05-19T20:34:35.890412Z", "iopub.status.idle": "2026-05-19T20:34:36.101251Z", "shell.execute_reply": "2026-05-19T20:34:36.100522Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:NumericIntegration -- RooRealIntegral::init(coshGBasis_Int[t]) using numeric integrator RooIntegrator1D to calculate Int(t)\n", "[#1] INFO:NumericIntegration -- RooRealIntegral::init(sinhGBasis_Int[t]) using numeric integrator RooIntegrator1D to calculate Int(t)\n", "[#1] INFO:NumericIntegration -- RooRealIntegral::init(coshGBasis_Int[t]) using numeric integrator RooIntegrator1D to calculate Int(t)\n", "[#1] INFO:NumericIntegration -- RooRealIntegral::init(sinhGBasis_Int[t]) using numeric integrator RooIntegrator1D to calculate Int(t)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "input_line_66:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n", " std::unique_ptr data{pdf.generate({t, cosa}, 10000)};\n", " ^\n" ] } ], "source": [ "std::unique_ptr data{pdf.generate({t, cosa}, 10000)};" ] }, { "cell_type": "markdown", "id": "96ae5239", "metadata": {}, "source": [ "Fit pdf to toy data with only amplitude strength floating" ] }, { "cell_type": "code", "execution_count": 8, "id": "59ba276b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:36.102932Z", "iopub.status.busy": "2026-05-19T20:34:36.102812Z", "iopub.status.idle": "2026-05-19T20:34:36.318989Z", "shell.execute_reply": "2026-05-19T20:34:36.318426Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_67:2:13: error: reference to 'data' is ambiguous\n", " pdf.fitTo(*data, PrintLevel(-1));\n", " ^\n", "input_line_66:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{pdf.generate({t, cosa}, 10000)};\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n", " data(initializer_list<_Tp> __il) noexcept\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n", " data(_Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n", " data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n", " data(_Tp (&__array)[_Nm]) noexcept\n", " ^\n" ] } ], "source": [ "pdf.fitTo(*data, PrintLevel(-1));" ] }, { "cell_type": "markdown", "id": "bc7adf9e", "metadata": {}, "source": [ "Plot amplitude sum pdf\n", "-------------------------------------------" ] }, { "cell_type": "markdown", "id": "87109609", "metadata": {}, "source": [ "Make 2D plots of amplitudes" ] }, { "cell_type": "code", "execution_count": 9, "id": "f734f1a3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:36.320838Z", "iopub.status.busy": "2026-05-19T20:34:36.320719Z", "iopub.status.idle": "2026-05-19T20:34:36.529289Z", "shell.execute_reply": "2026-05-19T20:34:36.528752Z" } }, "outputs": [], "source": [ "TH1 *hh_cos = ampl1.createHistogram(\"hh_cos\", t, Binning(50), YVar(cosa, Binning(50)));\n", "TH1 *hh_sin = ampl2.createHistogram(\"hh_sin\", t, Binning(50), YVar(cosa, Binning(50)));\n", "hh_cos->SetLineColor(kBlue);\n", "hh_sin->SetLineColor(kRed);" ] }, { "cell_type": "markdown", "id": "00b4eec8", "metadata": {}, "source": [ "Make projection on t, plot data, pdf and its components\n", "Note component projections may be larger than sum because amplitudes can be negative" ] }, { "cell_type": "code", "execution_count": 10, "id": "f4c3fe8a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:36.531432Z", "iopub.status.busy": "2026-05-19T20:34:36.531316Z", "iopub.status.idle": "2026-05-19T20:34:36.740764Z", "shell.execute_reply": "2026-05-19T20:34:36.740203Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_69:3:1: error: reference to 'data' is ambiguous\n", "data->plotOn(frame1);\n", "^\n", "input_line_66:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{pdf.generate({t, cosa}, 10000)};\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n", " data(initializer_list<_Tp> __il) noexcept\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n", " data(_Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n", " data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n", " data(_Tp (&__array)[_Nm]) noexcept\n", " ^\n" ] } ], "source": [ "RooPlot *frame1 = t.frame();\n", "data->plotOn(frame1);\n", "pdf.plotOn(frame1);\n", "pdf.plotOn(frame1, Components(ampl1), LineStyle(kDashed));\n", "pdf.plotOn(frame1, Components(ampl2), LineStyle(kDashed), LineColor(kRed));" ] }, { "cell_type": "markdown", "id": "ceb1085c", "metadata": {}, "source": [ "Make projection on cosa, plot data, pdf and its components\n", "Note that components projection may be larger than sum because amplitudes can be negative" ] }, { "cell_type": "code", "execution_count": 11, "id": "859c5347", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:36.742486Z", "iopub.status.busy": "2026-05-19T20:34:36.742373Z", "iopub.status.idle": "2026-05-19T20:34:36.948901Z", "shell.execute_reply": "2026-05-19T20:34:36.948558Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_70:3:1: error: reference to 'data' is ambiguous\n", "data->plotOn(frame2);\n", "^\n", "input_line_66:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{pdf.generate({t, cosa}, 10000)};\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n", " data(initializer_list<_Tp> __il) noexcept\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n", " data(_Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n", " data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n", " data(_Tp (&__array)[_Nm]) noexcept\n", " ^\n", "input_line_70:12:1: error: use of undeclared identifier 'frame1'\n", "frame1->GetYaxis()->SetTitleOffset(1.4);\n", "^\n", "input_line_70:13:1: error: use of undeclared identifier 'frame1'\n", "frame1->Draw();\n", "^\n" ] } ], "source": [ "RooPlot *frame2 = cosa.frame();\n", "data->plotOn(frame2);\n", "pdf.plotOn(frame2);\n", "pdf.plotOn(frame2, Components(ampl1), LineStyle(kDashed));\n", "pdf.plotOn(frame2, Components(ampl2), LineStyle(kDashed), LineColor(kRed));\n", "\n", "TCanvas *c = new TCanvas(\"rf704_amplitudefit\", \"rf704_amplitudefit\", 800, 800);\n", "c->Divide(2, 2);\n", "c->cd(1);\n", "gPad->SetLeftMargin(0.15);\n", "frame1->GetYaxis()->SetTitleOffset(1.4);\n", "frame1->Draw();\n", "c->cd(2);\n", "gPad->SetLeftMargin(0.15);\n", "frame2->GetYaxis()->SetTitleOffset(1.4);\n", "frame2->Draw();\n", "c->cd(3);\n", "gPad->SetLeftMargin(0.20);\n", "hh_cos->GetZaxis()->SetTitleOffset(2.3);\n", "hh_cos->Draw(\"surf\");\n", "c->cd(4);\n", "gPad->SetLeftMargin(0.20);\n", "hh_sin->GetZaxis()->SetTitleOffset(2.3);\n", "hh_sin->Draw(\"surf\");" ] }, { "cell_type": "markdown", "id": "f31c1d0b", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 12, "id": "1793f02e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:34:36.950903Z", "iopub.status.busy": "2026-05-19T20:34:36.950787Z", "iopub.status.idle": "2026-05-19T20:34:37.159574Z", "shell.execute_reply": "2026-05-19T20:34:37.159079Z" } }, "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 }