{
"cells": [
{
"cell_type": "markdown",
"id": "043b7ed8",
"metadata": {},
"source": [
"# rf706_histpdf\n",
"Special pdf's: histogram-based pdfs and functions\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": "98fb4996",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:42.831654Z",
"iopub.status.busy": "2026-05-19T20:34:42.831532Z",
"iopub.status.idle": "2026-05-19T20:34:42.846840Z",
"shell.execute_reply": "2026-05-19T20:34:42.846360Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"RooPolynomial.h\"\n",
"#include \"RooHistPdf.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "702683db",
"metadata": {},
"source": [
"Create pdf for sampling\n",
"---------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e172d025",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:42.848127Z",
"iopub.status.busy": "2026-05-19T20:34:42.848015Z",
"iopub.status.idle": "2026-05-19T20:34:43.252480Z",
"shell.execute_reply": "2026-05-19T20:34:43.251868Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", 0, 20);\n",
"RooPolynomial p(\"p\", \"p\", x, RooArgList(0.01, -0.01, 0.0004));"
]
},
{
"cell_type": "markdown",
"id": "6f1e3359",
"metadata": {},
"source": [
"Create low stats histogram\n",
"---------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "94c68741",
"metadata": {},
"source": [
"Sample 500 events from p"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a7e57794",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:43.254157Z",
"iopub.status.busy": "2026-05-19T20:34:43.254037Z",
"iopub.status.idle": "2026-05-19T20:34:43.469918Z",
"shell.execute_reply": "2026-05-19T20:34:43.467052Z"
}
},
"outputs": [],
"source": [
"x.setBins(20);\n",
"std::unique_ptr data1{p.generate(x, 500)};"
]
},
{
"cell_type": "markdown",
"id": "f6d2ec16",
"metadata": {},
"source": [
"Create a binned dataset with 20 bins and 500 events"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "41f2350f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:43.471424Z",
"iopub.status.busy": "2026-05-19T20:34:43.471310Z",
"iopub.status.idle": "2026-05-19T20:34:43.679987Z",
"shell.execute_reply": "2026-05-19T20:34:43.679412Z"
}
},
"outputs": [],
"source": [
"std::unique_ptr hist1{data1->binnedClone()};"
]
},
{
"cell_type": "markdown",
"id": "36c57c57",
"metadata": {},
"source": [
"Represent data in dh as pdf in x"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4c93e9b5",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:43.682062Z",
"iopub.status.busy": "2026-05-19T20:34:43.681949Z",
"iopub.status.idle": "2026-05-19T20:34:43.890599Z",
"shell.execute_reply": "2026-05-19T20:34:43.889953Z"
}
},
"outputs": [],
"source": [
"RooHistPdf histpdf1(\"histpdf1\", \"histpdf1\", x, *hist1, 0);"
]
},
{
"cell_type": "markdown",
"id": "e542fae4",
"metadata": {},
"source": [
"Plot unbinned data and histogram pdf overlaid"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "48b76846",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:43.892595Z",
"iopub.status.busy": "2026-05-19T20:34:43.892480Z",
"iopub.status.idle": "2026-05-19T20:34:44.101169Z",
"shell.execute_reply": "2026-05-19T20:34:44.100492Z"
}
},
"outputs": [],
"source": [
"RooPlot *frame1 = x.frame(Title(\"Low statistics histogram pdf\"), Bins(100));\n",
"data1->plotOn(frame1);\n",
"histpdf1.plotOn(frame1);"
]
},
{
"cell_type": "markdown",
"id": "513b034e",
"metadata": {},
"source": [
"Create high stats histogram\n",
"-----------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "e7d7bd35",
"metadata": {},
"source": [
"Sample 100000 events from p"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7638ae32",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:44.103004Z",
"iopub.status.busy": "2026-05-19T20:34:44.102891Z",
"iopub.status.idle": "2026-05-19T20:34:44.311700Z",
"shell.execute_reply": "2026-05-19T20:34:44.310961Z"
}
},
"outputs": [],
"source": [
"x.setBins(10);\n",
"std::unique_ptr data2{p.generate(x, 100000)};"
]
},
{
"cell_type": "markdown",
"id": "a76785dc",
"metadata": {},
"source": [
"Create a binned dataset with 10 bins and 100K events"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "bf070a66",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:44.334434Z",
"iopub.status.busy": "2026-05-19T20:34:44.334294Z",
"iopub.status.idle": "2026-05-19T20:34:44.543236Z",
"shell.execute_reply": "2026-05-19T20:34:44.542467Z"
}
},
"outputs": [],
"source": [
"std::unique_ptr hist2{data2->binnedClone()};"
]
},
{
"cell_type": "markdown",
"id": "514cf97c",
"metadata": {},
"source": [
"Represent data in dh as pdf in x, apply 2nd order interpolation"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "9af9832f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:44.544840Z",
"iopub.status.busy": "2026-05-19T20:34:44.544723Z",
"iopub.status.idle": "2026-05-19T20:34:44.753563Z",
"shell.execute_reply": "2026-05-19T20:34:44.752803Z"
}
},
"outputs": [],
"source": [
"RooHistPdf histpdf2(\"histpdf2\", \"histpdf2\", x, *hist2, 2);"
]
},
{
"cell_type": "markdown",
"id": "4040ceda",
"metadata": {},
"source": [
"Plot unbinned data and histogram pdf overlaid"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c3d611bb",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:44.755173Z",
"iopub.status.busy": "2026-05-19T20:34:44.755055Z",
"iopub.status.idle": "2026-05-19T20:34:44.963785Z",
"shell.execute_reply": "2026-05-19T20:34:44.963101Z"
}
},
"outputs": [],
"source": [
"RooPlot *frame2 = x.frame(Title(\"High stats histogram pdf with interpolation\"), Bins(100));\n",
"data2->plotOn(frame2);\n",
"histpdf2.plotOn(frame2);\n",
"\n",
"TCanvas *c = new TCanvas(\"rf706_histpdf\", \"rf706_histpdf\", 800, 400);\n",
"c->Divide(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.8);\n",
"frame2->Draw();"
]
},
{
"cell_type": "markdown",
"id": "e6244dcb",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "a16490f5",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:44.965916Z",
"iopub.status.busy": "2026-05-19T20:34:44.965791Z",
"iopub.status.idle": "2026-05-19T20:34:45.196099Z",
"shell.execute_reply": "2026-05-19T20:34:45.195427Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%jsroot on\n",
"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
}