{
"cells": [
{
"cell_type": "markdown",
"id": "3546e0b5",
"metadata": {},
"source": [
"# rf705_linearmorph\n",
"Special pdf's: linear interpolation between pdf shapes using the 'Alex Read' algorithm\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, March 19, 2024 at 07:17 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ddfae3f4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:34.979611Z",
"iopub.status.busy": "2024-03-19T19:17:34.978707Z",
"iopub.status.idle": "2024-03-19T19:17:35.023280Z",
"shell.execute_reply": "2024-03-19T19:17:35.022343Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"RooPolynomial.h\"\n",
"#include \"RooIntegralMorph.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"#include \"TH1.h\"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "b037ad5d",
"metadata": {},
"source": [
"Create end point pdf shapes\n",
"------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "020c141a",
"metadata": {},
"source": [
"Observable"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0ccba08f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:35.028463Z",
"iopub.status.busy": "2024-03-19T19:17:35.027940Z",
"iopub.status.idle": "2024-03-19T19:17:35.869117Z",
"shell.execute_reply": "2024-03-19T19:17:35.867823Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", -20, 20);"
]
},
{
"cell_type": "markdown",
"id": "34a16f45",
"metadata": {},
"source": [
"Lower end point shape: a Gaussian"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a36e28ba",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:35.874805Z",
"iopub.status.busy": "2024-03-19T19:17:35.874469Z",
"iopub.status.idle": "2024-03-19T19:17:36.152895Z",
"shell.execute_reply": "2024-03-19T19:17:36.139058Z"
}
},
"outputs": [],
"source": [
"RooRealVar g1mean(\"g1mean\", \"g1mean\", -10);\n",
"RooGaussian g1(\"g1\", \"g1\", x, g1mean, 2.0);"
]
},
{
"cell_type": "markdown",
"id": "61b33ebd",
"metadata": {},
"source": [
"Upper end point shape: a Polynomial"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "06d3bf29",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:36.159107Z",
"iopub.status.busy": "2024-03-19T19:17:36.158718Z",
"iopub.status.idle": "2024-03-19T19:17:36.527256Z",
"shell.execute_reply": "2024-03-19T19:17:36.520178Z"
}
},
"outputs": [],
"source": [
"RooPolynomial g2(\"g2\", \"g2\", x, RooArgSet(-0.03, -0.001));"
]
},
{
"cell_type": "markdown",
"id": "061a8aa5",
"metadata": {},
"source": [
"Create interpolating pdf\n",
"-----------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "f5d292dd",
"metadata": {},
"source": [
"Create interpolation variable"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4eee6729",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:36.556115Z",
"iopub.status.busy": "2024-03-19T19:17:36.555505Z",
"iopub.status.idle": "2024-03-19T19:17:36.811657Z",
"shell.execute_reply": "2024-03-19T19:17:36.801264Z"
}
},
"outputs": [],
"source": [
"RooRealVar alpha(\"alpha\", \"alpha\", 0, 1.0);"
]
},
{
"cell_type": "markdown",
"id": "f884d448",
"metadata": {},
"source": [
"Specify sampling density on observable and interpolation variable"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9b916f24",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:36.818423Z",
"iopub.status.busy": "2024-03-19T19:17:36.817998Z",
"iopub.status.idle": "2024-03-19T19:17:37.060232Z",
"shell.execute_reply": "2024-03-19T19:17:37.059034Z"
}
},
"outputs": [],
"source": [
"x.setBins(1000, \"cache\");\n",
"alpha.setBins(50, \"cache\");"
]
},
{
"cell_type": "markdown",
"id": "fe10d542",
"metadata": {},
"source": [
"Construct interpolating pdf in (x,a) represent g1(x) at a=a_min\n",
"and g2(x) at a=a_max"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f45c9b10",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:37.080862Z",
"iopub.status.busy": "2024-03-19T19:17:37.080477Z",
"iopub.status.idle": "2024-03-19T19:17:37.451647Z",
"shell.execute_reply": "2024-03-19T19:17:37.450492Z"
}
},
"outputs": [],
"source": [
"RooIntegralMorph lmorph(\"lmorph\", \"lmorph\", g1, g2, x, alpha);"
]
},
{
"cell_type": "markdown",
"id": "1380cb9b",
"metadata": {},
"source": [
"Plot interpolating pdf at various alpha\n",
"-----------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "0804e76b",
"metadata": {},
"source": [
"Show end points as blue curves"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5951a17c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:37.488921Z",
"iopub.status.busy": "2024-03-19T19:17:37.488533Z",
"iopub.status.idle": "2024-03-19T19:17:37.708952Z",
"shell.execute_reply": "2024-03-19T19:17:37.707842Z"
}
},
"outputs": [],
"source": [
"RooPlot *frame1 = x.frame();\n",
"g1.plotOn(frame1);\n",
"g2.plotOn(frame1);"
]
},
{
"cell_type": "markdown",
"id": "fabb816c",
"metadata": {},
"source": [
"Show interpolated shapes in red"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b6b87ffa",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:37.722055Z",
"iopub.status.busy": "2024-03-19T19:17:37.721668Z",
"iopub.status.idle": "2024-03-19T19:17:38.203725Z",
"shell.execute_reply": "2024-03-19T19:17:38.202749Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d59a1d30 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0\n",
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d59a1d30 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0\n",
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d59a1d30 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0\n",
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d59a1d30 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0\n",
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d59a1d30 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0\n",
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d59a1d30 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0\n",
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d59a1d30 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0\n",
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d59a1d30 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0\n"
]
}
],
"source": [
"alpha.setVal(0.125);\n",
"lmorph.plotOn(frame1, LineColor(kRed));\n",
"alpha.setVal(0.25);\n",
"lmorph.plotOn(frame1, LineColor(kRed));\n",
"alpha.setVal(0.375);\n",
"lmorph.plotOn(frame1, LineColor(kRed));\n",
"alpha.setVal(0.50);\n",
"lmorph.plotOn(frame1, LineColor(kRed));\n",
"alpha.setVal(0.625);\n",
"lmorph.plotOn(frame1, LineColor(kRed));\n",
"alpha.setVal(0.75);\n",
"lmorph.plotOn(frame1, LineColor(kRed));\n",
"alpha.setVal(0.875);\n",
"lmorph.plotOn(frame1, LineColor(kRed));\n",
"alpha.setVal(0.95);\n",
"lmorph.plotOn(frame1, LineColor(kRed));"
]
},
{
"cell_type": "markdown",
"id": "fda3c4df",
"metadata": {},
"source": [
"Show 2D distribution of pdf(x,alpha)\n",
"-----------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "4e9ada4b",
"metadata": {},
"source": [
"Create 2D histogram"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "32242b5b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:38.210189Z",
"iopub.status.busy": "2024-03-19T19:17:38.209713Z",
"iopub.status.idle": "2024-03-19T19:17:39.089178Z",
"shell.execute_reply": "2024-03-19T19:17:39.088218Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d5991e30 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x_alpha for nset (x,alpha) with code 0 from preexisting content.\n"
]
}
],
"source": [
"TH1 *hh = lmorph.createHistogram(\"hh\", x, Binning(40), YVar(alpha, Binning(40)));\n",
"hh->SetLineColor(kBlue);"
]
},
{
"cell_type": "markdown",
"id": "7509da04",
"metadata": {},
"source": [
"Fit pdf to dataset with alpha=0.8\n",
"-----------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "593325de",
"metadata": {},
"source": [
"Generate a toy dataset at alpha = 0.8"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "24548e82",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:39.095565Z",
"iopub.status.busy": "2024-03-19T19:17:39.094690Z",
"iopub.status.idle": "2024-03-19T19:17:39.422654Z",
"shell.execute_reply": "2024-03-19T19:17:39.413380Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d5a20380 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0\n",
"[#1] INFO:Caching -- RooAbsCachedPdf::getCache(lmorph) creating new cache 0x7f78d5a82700 with pdf g1_MORPH_g2_CACHE_Obs[x]_NORM_x for nset (x) with code 0 from preexisting content.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_57:3:1: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n",
"std::unique_ptr data{lmorph.generate(x, 1000)};\n",
"^\n"
]
}
],
"source": [
"alpha = 0.8;\n",
"std::unique_ptr data{lmorph.generate(x, 1000)};"
]
},
{
"cell_type": "markdown",
"id": "cf851140",
"metadata": {},
"source": [
"Fit pdf to toy data"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4a44b500",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:39.428624Z",
"iopub.status.busy": "2024-03-19T19:17:39.428244Z",
"iopub.status.idle": "2024-03-19T19:17:39.659472Z",
"shell.execute_reply": "2024-03-19T19:17:39.657451Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_58:3:15: error: reference to 'data' is ambiguous\n",
"lmorph.fitTo(*data, Verbose(true), PrintLevel(-1));\n",
" ^\n",
"input_line_57:3:29: note: candidate found by name lookup is 'data'\n",
"std::unique_ptr data{lmorph.generate(x, 1000)};\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'\n",
" data(initializer_list<_Tp> __il) noexcept\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'\n",
" data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Tp (&__array)[_Nm]) noexcept\n",
" ^\n"
]
}
],
"source": [
"lmorph.setCacheAlpha(true);\n",
"lmorph.fitTo(*data, Verbose(true), PrintLevel(-1));"
]
},
{
"cell_type": "markdown",
"id": "ed0d320a",
"metadata": {},
"source": [
"Plot fitted pdf and data overlaid"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "ccc4a1e0",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:39.678045Z",
"iopub.status.busy": "2024-03-19T19:17:39.677509Z",
"iopub.status.idle": "2024-03-19T19:17:39.911675Z",
"shell.execute_reply": "2024-03-19T19:17:39.900404Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_59:3:1: error: reference to 'data' is ambiguous\n",
"data->plotOn(frame2);\n",
"^\n",
"input_line_57:3:29: note: candidate found by name lookup is 'data'\n",
"std::unique_ptr data{lmorph.generate(x, 1000)};\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'\n",
" data(initializer_list<_Tp> __il) noexcept\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'\n",
" data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Tp (&__array)[_Nm]) noexcept\n",
" ^\n"
]
}
],
"source": [
"RooPlot *frame2 = x.frame(Bins(100));\n",
"data->plotOn(frame2);\n",
"lmorph.plotOn(frame2);"
]
},
{
"cell_type": "markdown",
"id": "860a0200",
"metadata": {},
"source": [
"Scan -log(L) vs alpha\n",
"-----------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "28dcb854",
"metadata": {},
"source": [
"Show scan -log(L) of dataset w.r.t alpha"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "dc99b44b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:39.935847Z",
"iopub.status.busy": "2024-03-19T19:17:39.935447Z",
"iopub.status.idle": "2024-03-19T19:17:40.159264Z",
"shell.execute_reply": "2024-03-19T19:17:40.151755Z"
}
},
"outputs": [],
"source": [
"RooPlot *frame3 = alpha.frame(Bins(100), Range(0.1, 0.9));"
]
},
{
"cell_type": "markdown",
"id": "7e6a9765",
"metadata": {},
"source": [
"Make 2D pdf of histogram"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "adee8e36",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:40.171142Z",
"iopub.status.busy": "2024-03-19T19:17:40.170747Z",
"iopub.status.idle": "2024-03-19T19:17:40.423315Z",
"shell.execute_reply": "2024-03-19T19:17:40.398754Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_61:2:52: error: reference to 'data' is ambiguous\n",
" std::unique_ptr nll{lmorph.createNLL(*data)};\n",
" ^\n",
"input_line_57:3:29: note: candidate found by name lookup is 'data'\n",
"std::unique_ptr data{lmorph.generate(x, 1000)};\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'\n",
" data(initializer_list<_Tp> __il) noexcept\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'\n",
" data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Tp (&__array)[_Nm]) noexcept\n",
" ^\n",
"input_line_61:23:1: error: use of undeclared identifier 'frame2'\n",
"frame2->GetYaxis()->SetTitleOffset(1.4);\n",
"^\n",
"input_line_61:24:1: error: use of undeclared identifier 'frame2'\n",
"frame2->Draw();\n",
"^\n"
]
}
],
"source": [
"std::unique_ptr nll{lmorph.createNLL(*data)};\n",
"nll->plotOn(frame3, ShiftToZero());\n",
"\n",
"lmorph.setCacheAlpha(false);\n",
"\n",
"TCanvas *c = new TCanvas(\"rf705_linearmorph\", \"rf705_linearmorph\", 800, 800);\n",
"c->Divide(2, 2);\n",
"c->cd(1);\n",
"gPad->SetLeftMargin(0.15);\n",
"frame1->GetYaxis()->SetTitleOffset(1.6);\n",
"frame1->Draw();\n",
"c->cd(2);\n",
"gPad->SetLeftMargin(0.20);\n",
"hh->GetZaxis()->SetTitleOffset(2.5);\n",
"hh->Draw(\"surf\");\n",
"c->cd(3);\n",
"gPad->SetLeftMargin(0.15);\n",
"frame3->GetYaxis()->SetTitleOffset(1.4);\n",
"frame3->Draw();\n",
"c->cd(4);\n",
"gPad->SetLeftMargin(0.15);\n",
"frame2->GetYaxis()->SetTitleOffset(1.4);\n",
"frame2->Draw();\n",
"\n",
"return;"
]
},
{
"cell_type": "markdown",
"id": "84237b0a",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "9e2c426a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:17:40.432682Z",
"iopub.status.busy": "2024-03-19T19:17:40.432282Z",
"iopub.status.idle": "2024-03-19T19:17:40.649197Z",
"shell.execute_reply": "2024-03-19T19:17:40.648001Z"
}
},
"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
}