{
"cells": [
{
"cell_type": "markdown",
"id": "563eab57",
"metadata": {},
"source": [
"# limit\n",
"This example shows random number generation for filling histograms. \n",
"It demonstrates the computation of 95 % confidence level (CL) limits.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Christophe Delaere \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": "a9321a4c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:42.762758Z",
"iopub.status.busy": "2026-05-19T20:25:42.762631Z",
"iopub.status.idle": "2026-05-19T20:25:42.774224Z",
"shell.execute_reply": "2026-05-19T20:25:42.773632Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \n",
"#include \"TH1.h\"\n",
"#include \"THStack.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TFrame.h\"\n",
"#include \"TRandom2.h\"\n",
"#include \"TSystem.h\"\n",
"#include \"TVector.h\"\n",
"#include \"TObjArray.h\"\n",
"#include \"TLimit.h\"\n",
"#include \"TLimitDataSource.h\"\n",
"#include \"TConfidenceLevel.h\"\n",
"\n",
"using std::cout;\n",
"using std::endl;"
]
},
{
"cell_type": "markdown",
"id": "6a59b993",
"metadata": {},
"source": [
"Create a new canvas."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "36df546f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:42.775549Z",
"iopub.status.busy": "2026-05-19T20:25:42.775435Z",
"iopub.status.idle": "2026-05-19T20:25:43.136436Z",
"shell.execute_reply": "2026-05-19T20:25:43.135763Z"
}
},
"outputs": [],
"source": [
"TCanvas *c1 = new TCanvas(\"c1\",\"Dynamic Filling Example\",200,10,700,500);\n",
"c1->SetFillColor(42);"
]
},
{
"cell_type": "markdown",
"id": "62cf55e3",
"metadata": {},
"source": [
"Create some histograms"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "aa64dc2b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:43.140170Z",
"iopub.status.busy": "2026-05-19T20:25:43.139992Z",
"iopub.status.idle": "2026-05-19T20:25:43.345051Z",
"shell.execute_reply": "2026-05-19T20:25:43.344243Z"
}
},
"outputs": [],
"source": [
"TH1D* backgroundHist = new TH1D(\"background\",\"The expected background\",30,-4,4);\n",
"TH1D* signalHist = new TH1D(\"signal\",\"the expected signal\",30,-4,4);\n",
"TH1D* dataHist = new TH1D(\"data\",\"some fake data points\",30,-4,4);\n",
"backgroundHist->SetFillColor(48);\n",
"signalHist->SetFillColor(41);\n",
"dataHist->SetMarkerStyle(21);\n",
"dataHist->SetMarkerColor(kBlue);\n",
"backgroundHist->Sumw2(); // needed for stat uncertainty\n",
"signalHist->Sumw2(); // needed for stat uncertainty"
]
},
{
"cell_type": "markdown",
"id": "1581a30a",
"metadata": {},
"source": [
"Fill histograms randomly"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "013a9b01",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:43.346994Z",
"iopub.status.busy": "2026-05-19T20:25:43.346875Z",
"iopub.status.idle": "2026-05-19T20:25:43.553594Z",
"shell.execute_reply": "2026-05-19T20:25:43.552995Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"TRandom2 r;\n",
"Float_t bg,sig,dt;\n",
"for (Int_t i = 0; i < 25000; i++) {\n",
" bg = r.Gaus(0,1);\n",
" sig = r.Gaus(1,.2);\n",
" backgroundHist->Fill(bg,0.02);\n",
" signalHist->Fill(sig,0.001);\n",
"}\n",
"for (Int_t i = 0; i < 500; i++) {\n",
" dt = r.Gaus(0,1);\n",
" dataHist->Fill(dt);\n",
"}\n",
"THStack *hs = new THStack(\"hs\",\"Signal and background compared to data...\");\n",
"hs->Add(backgroundHist);\n",
"hs->Add(signalHist);\n",
"hs->Draw(\"hist\");\n",
"dataHist->Draw(\"PE1,Same\");\n",
"c1->Modified();\n",
"c1->Update();\n",
"c1->GetFrame()->SetFillColor(21);\n",
"c1->GetFrame()->SetBorderSize(6);\n",
"c1->GetFrame()->SetBorderMode(-1);\n",
"c1->Modified();\n",
"c1->Update();\n",
"gSystem->ProcessEvents();"
]
},
{
"cell_type": "markdown",
"id": "cf48801c",
"metadata": {},
"source": [
"Compute the limits"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "179a6b8b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:43.555485Z",
"iopub.status.busy": "2026-05-19T20:25:43.555363Z",
"iopub.status.idle": "2026-05-19T20:25:43.887735Z",
"shell.execute_reply": "2026-05-19T20:25:43.887106Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Computing limits... \n",
"CLs : 0.0179535\n",
"CLsb : 0.00930313\n",
"CLb : 0.51818\n",
"< CLs > : 0.0165344\n",
"< CLsb > : 0.00826754\n",
"< CLb > : 0.50002\n"
]
}
],
"source": [
"cout << \"Computing limits... \" << endl;\n",
"TLimitDataSource* mydatasource = new TLimitDataSource(signalHist,backgroundHist,dataHist);\n",
"TConfidenceLevel *myconfidence = TLimit::ComputeLimit(mydatasource,50000);\n",
"cout << \"CLs : \" << myconfidence->CLs() << endl;\n",
"cout << \"CLsb : \" << myconfidence->CLsb() << endl;\n",
"cout << \"CLb : \" << myconfidence->CLb() << endl;\n",
"cout << \"< CLs > : \" << myconfidence->GetExpectedCLs_b() << endl;\n",
"cout << \"< CLsb > : \" << myconfidence->GetExpectedCLsb_b() << endl;\n",
"cout << \"< CLb > : \" << myconfidence->GetExpectedCLb_b() << endl;"
]
},
{
"cell_type": "markdown",
"id": "031cc58c",
"metadata": {},
"source": [
"Add stat uncertainty"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "75ed5ae8",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:43.889196Z",
"iopub.status.busy": "2026-05-19T20:25:43.889078Z",
"iopub.status.idle": "2026-05-19T20:25:44.401150Z",
"shell.execute_reply": "2026-05-19T20:25:44.400616Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Computing limits with stat systematics... \n",
"CLs : 0.0189723\n",
"CLsb : 0.00989482\n",
"CLb : 0.52154\n",
"< CLs > : 0.0172591\n",
"< CLsb > : 0.00862989\n",
"< CLb > : 0.50002\n"
]
}
],
"source": [
"cout << endl << \"Computing limits with stat systematics... \" << endl;\n",
"TConfidenceLevel *mystatconfidence = TLimit::ComputeLimit(mydatasource,50000,true);\n",
"cout << \"CLs : \" << mystatconfidence->CLs() << endl;\n",
"cout << \"CLsb : \" << mystatconfidence->CLsb() << endl;\n",
"cout << \"CLb : \" << mystatconfidence->CLb() << endl;\n",
"cout << \"< CLs > : \" << mystatconfidence->GetExpectedCLs_b() << endl;\n",
"cout << \"< CLsb > : \" << mystatconfidence->GetExpectedCLsb_b() << endl;\n",
"cout << \"< CLb > : \" << mystatconfidence->GetExpectedCLb_b() << endl;"
]
},
{
"cell_type": "markdown",
"id": "bc4d8a48",
"metadata": {},
"source": [
"Add some systematics"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "610b82ae",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:44.402556Z",
"iopub.status.busy": "2026-05-19T20:25:44.402439Z",
"iopub.status.idle": "2026-05-19T20:25:44.999236Z",
"shell.execute_reply": "2026-05-19T20:25:44.998702Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Computing limits with systematics... \n",
"CLs : 0.0352037\n",
"CLsb : 0.0184327\n",
"CLb : 0.5236\n",
"< CLs > : 0.0328702\n",
"< CLsb > : 0.0164358\n",
"< CLb > : 0.50002\n"
]
}
],
"source": [
"cout << endl << \"Computing limits with systematics... \" << endl;\n",
"TVectorD errorb(2);\n",
"TVectorD errors(2);\n",
"TObjArray* names = new TObjArray();\n",
"TObjString name1(\"bg uncertainty\");\n",
"TObjString name2(\"sig uncertainty\");\n",
"names->AddLast(&name1);\n",
"names->AddLast(&name2);\n",
"errorb[0]=0.05; // error source 1: 5%\n",
"errorb[1]=0; // error source 2: 0%\n",
"errors[0]=0; // error source 1: 0%\n",
"errors[1]=0.01; // error source 2: 1%\n",
"TLimitDataSource* mynewdatasource = new TLimitDataSource();\n",
"mynewdatasource->AddChannel(signalHist,backgroundHist,dataHist,&errors,&errorb,names);\n",
"TConfidenceLevel *mynewconfidence = TLimit::ComputeLimit(mynewdatasource,50000,true);\n",
"cout << \"CLs : \" << mynewconfidence->CLs() << endl;\n",
"cout << \"CLsb : \" << mynewconfidence->CLsb() << endl;\n",
"cout << \"CLb : \" << mynewconfidence->CLb() << endl;\n",
"cout << \"< CLs > : \" << mynewconfidence->GetExpectedCLs_b() << endl;\n",
"cout << \"< CLsb > : \" << mynewconfidence->GetExpectedCLsb_b() << endl;\n",
"cout << \"< CLb > : \" << mynewconfidence->GetExpectedCLb_b() << endl;"
]
},
{
"cell_type": "markdown",
"id": "2635bd25",
"metadata": {},
"source": [
"show canonical -2lnQ plots in a new canvas\n",
"- The histogram of -2lnQ for background hypothesis (full)\n",
"- The histogram of -2lnQ for signal and background hypothesis (dashed)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "31a8ab3b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:45.000626Z",
"iopub.status.busy": "2026-05-19T20:25:45.000496Z",
"iopub.status.idle": "2026-05-19T20:25:45.211458Z",
"shell.execute_reply": "2026-05-19T20:25:45.210817Z"
}
},
"outputs": [],
"source": [
"TCanvas *c2 = new TCanvas(\"c2\");\n",
"myconfidence->Draw();"
]
},
{
"cell_type": "markdown",
"id": "66e9339f",
"metadata": {},
"source": [
"clean up (except histograms and canvas)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a8e0be2f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:45.213227Z",
"iopub.status.busy": "2026-05-19T20:25:45.213091Z",
"iopub.status.idle": "2026-05-19T20:25:45.419894Z",
"shell.execute_reply": "2026-05-19T20:25:45.419215Z"
}
},
"outputs": [],
"source": [
"delete myconfidence;\n",
"delete mydatasource;\n",
"delete mystatconfidence;\n",
"delete mynewconfidence;\n",
"delete mynewdatasource;"
]
},
{
"cell_type": "markdown",
"id": "659ce6dd",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c3334a10",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:45.421721Z",
"iopub.status.busy": "2026-05-19T20:25:45.421569Z",
"iopub.status.idle": "2026-05-19T20:25:45.628455Z",
"shell.execute_reply": "2026-05-19T20:25:45.627822Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"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
}