{ "cells": [ { "cell_type": "markdown", "id": "18418e3d", "metadata": {}, "source": [ "# StandardFeldmanCousinsDemo\n", "Standard demo of the Feldman-Cousins calculator\n", "StandardFeldmanCousinsDemo\n", "\n", "This is a standard demo that can be used with any ROOT file\n", "prepared in the standard way. You specify:\n", " - name for input ROOT file\n", " - name of workspace inside ROOT file that holds model and data\n", " - name of ModelConfig that specifies details for calculator tools\n", " - name of dataset\n", "\n", "With default parameters the macro will attempt to run the\n", "standard hist2workspace example and read the ROOT file\n", "that it produces.\n", "\n", "The actual heart of the demo is only about 10 lines long.\n", "\n", "The FeldmanCousins tools is a classical frequentist calculation\n", "based on the Neyman Construction. The test statistic can be\n", "generalized for nuisance parameters by using the profile likelihood ratio.\n", "But unlike the ProfileLikelihoodCalculator, this tool explicitly\n", "builds the sampling distribution of the test statistic via toy Monte Carlo.\n", "\n", "\n", "\n", "\n", "**Author:** Kyle Cranmer \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:36 PM." ] }, { "cell_type": "code", "execution_count": null, "id": "a729ba97", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TFile.h\"\n", "#include \"TROOT.h\"\n", "#include \"TH1F.h\"\n", "#include \"TSystem.h\"\n", "\n", "#include \"RooWorkspace.h\"\n", "#include \"RooAbsData.h\"\n", "\n", "#include \"RooStats/ModelConfig.h\"\n", "#include \"RooStats/FeldmanCousins.h\"\n", "#include \"RooStats/ToyMCSampler.h\"\n", "#include \"RooStats/PointSetInterval.h\"\n", "#include \"RooStats/ConfidenceBelt.h\"\n", "\n", "using namespace RooFit;\n", "using namespace RooStats;" ] }, { "cell_type": "markdown", "id": "b85fdc35", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": null, "id": "95d04870", "metadata": { "collapsed": false }, "outputs": [], "source": [ "const char *infile = \"\";\n", "const char *workspaceName = \"combined\";\n", "const char *modelConfigName = \"ModelConfig\";\n", "const char *dataName = \"obsData\";" ] }, { "cell_type": "markdown", "id": "28b386cb", "metadata": {}, "source": [ "-------------------------------------------------------\n", "First part is just to access a user-defined file\n", "or create the standard example file if it doesn't exist" ] }, { "cell_type": "code", "execution_count": null, "id": "f1c98386", "metadata": { "collapsed": false }, "outputs": [], "source": [ "const char *filename = \"\";\n", "if (!strcmp(infile, \"\")) {\n", " filename = \"results/example_combined_GaussExample_model.root\";\n", " bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code\n", " // if file does not exists generate with histfactory\n", " if (!fileExist) {\n", " // Normally this would be run on the command line\n", " cout << \"will run standard hist2workspace example\" << endl;\n", " gROOT->ProcessLine(\".! prepareHistFactory .\");\n", " gROOT->ProcessLine(\".! hist2workspace config/example.xml\");\n", " cout << \"\\n\\n---------------------\" << endl;\n", " cout << \"Done creating example input\" << endl;\n", " cout << \"---------------------\\n\\n\" << endl;\n", " }\n", "\n", "} else\n", " filename = infile;" ] }, { "cell_type": "markdown", "id": "6b6f73d4", "metadata": {}, "source": [ "Try to open the file" ] }, { "cell_type": "code", "execution_count": null, "id": "450112d8", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TFile *file = TFile::Open(filename);" ] }, { "cell_type": "markdown", "id": "3653ae44", "metadata": {}, "source": [ "if input file was specified but not found, quit" ] }, { "cell_type": "code", "execution_count": null, "id": "9841715a", "metadata": { "collapsed": false }, "outputs": [], "source": [ "if (!file) {\n", " cout << \"StandardRooStatsDemoMacro: Input file \" << filename << \" is not found\" << endl;\n", " return;\n", "}" ] }, { "cell_type": "markdown", "id": "2eaa1d4b", "metadata": {}, "source": [ "-------------------------------------------------------\n", "Tutorial starts here\n", "-------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "8f9c31af", "metadata": {}, "source": [ "get the workspace out of the file" ] }, { "cell_type": "code", "execution_count": null, "id": "c9dd9434", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);\n", "if (!w) {\n", " cout << \"workspace not found\" << endl;\n", " return;\n", "}" ] }, { "cell_type": "markdown", "id": "6a685e55", "metadata": {}, "source": [ "get the modelConfig out of the file" ] }, { "cell_type": "code", "execution_count": null, "id": "0387d77f", "metadata": { "collapsed": false }, "outputs": [], "source": [ "ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);" ] }, { "cell_type": "markdown", "id": "8c589345", "metadata": {}, "source": [ "get the modelConfig out of the file" ] }, { "cell_type": "code", "execution_count": null, "id": "1396cbca", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooAbsData *data = w->data(dataName);" ] }, { "cell_type": "markdown", "id": "e0eadccf", "metadata": {}, "source": [ "make sure ingredients are found" ] }, { "cell_type": "code", "execution_count": null, "id": "89a79ba0", "metadata": { "collapsed": false }, "outputs": [], "source": [ "if (!data || !mc) {\n", " w->Print();\n", " cout << \"data or ModelConfig was not found\" << endl;\n", " return;\n", "}" ] }, { "cell_type": "markdown", "id": "85c0843e", "metadata": {}, "source": [ "-------------------------------------------------------\n", "create and use the FeldmanCousins tool\n", "to find and plot the 95% confidence interval\n", "on the parameter of interest as specified\n", "in the model config" ] }, { "cell_type": "code", "execution_count": null, "id": "d2197af3", "metadata": { "collapsed": false }, "outputs": [], "source": [ "FeldmanCousins fc(*data, *mc);\n", "fc.SetConfidenceLevel(0.95); // 95% interval" ] }, { "cell_type": "markdown", "id": "96065129", "metadata": {}, "source": [ "fc.AdditionalNToysFactor(0.1); // to speed up the result" ] }, { "cell_type": "code", "execution_count": null, "id": "e9cb2329", "metadata": { "collapsed": false }, "outputs": [], "source": [ "fc.UseAdaptiveSampling(true); // speed it up a bit\n", "fc.SetNBins(10); // set how many points per parameter of interest to scan\n", "fc.CreateConfBelt(true); // save the information in the belt for plotting" ] }, { "cell_type": "markdown", "id": "fd3608c9", "metadata": {}, "source": [ "Since this tool needs to throw toy MC the PDF needs to be\n", "extended or the tool needs to know how many entries in a dataset\n", "per pseudo experiment.\n", "In the 'number counting form' where the entries in the dataset\n", "are counts, and not values of discriminating variables, the\n", "datasets typically only have one entry and the PDF is not\n", "extended." ] }, { "cell_type": "code", "execution_count": null, "id": "5e6e2151", "metadata": { "collapsed": false }, "outputs": [], "source": [ "if (!mc->GetPdf()->canBeExtended()) {\n", " if (data->numEntries() == 1)\n", " fc.FluctuateNumDataEntries(false);\n", " else\n", " cout << \"Not sure what to do about this model\" << endl;\n", "}" ] }, { "cell_type": "markdown", "id": "6d2ba367", "metadata": {}, "source": [ "Now get the interval" ] }, { "cell_type": "code", "execution_count": null, "id": "164100d3", "metadata": { "collapsed": false }, "outputs": [], "source": [ "PointSetInterval *interval = fc.GetInterval();\n", "ConfidenceBelt *belt = fc.GetConfidenceBelt();" ] }, { "cell_type": "markdown", "id": "b35f8f56", "metadata": {}, "source": [ "print out the interval on the first Parameter of Interest" ] }, { "cell_type": "code", "execution_count": null, "id": "7236db2f", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooRealVar *firstPOI = (RooRealVar *)mc->GetParametersOfInterest()->first();\n", "cout << \"\\n95% interval on \" << firstPOI->GetName() << \" is : [\" << interval->LowerLimit(*firstPOI) << \", \"\n", " << interval->UpperLimit(*firstPOI) << \"] \" << endl;" ] }, { "cell_type": "markdown", "id": "adc1d08d", "metadata": {}, "source": [ "---------------------------------------------\n", "No nice plots yet, so plot the belt by hand" ] }, { "cell_type": "markdown", "id": "fe0d27e8", "metadata": {}, "source": [ "Ask the calculator which points were scanned" ] }, { "cell_type": "code", "execution_count": null, "id": "b1d04f32", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooDataSet *parameterScan = (RooDataSet *)fc.GetPointsToScan();\n", "RooArgSet *tmpPoint;" ] }, { "cell_type": "markdown", "id": "0ad246fe", "metadata": {}, "source": [ "make a histogram of parameter vs. threshold" ] }, { "cell_type": "code", "execution_count": null, "id": "9f53bf4e", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TH1F *histOfThresholds =\n", " new TH1F(\"histOfThresholds\", \"\", parameterScan->numEntries(), firstPOI->getMin(), firstPOI->getMax());" ] }, { "cell_type": "markdown", "id": "2c6ab160", "metadata": {}, "source": [ "loop through the points that were tested and ask confidence belt\n", "what the upper/lower thresholds were.\n", "For FeldmanCousins, the lower cut off is always 0" ] }, { "cell_type": "code", "execution_count": null, "id": "63d81895", "metadata": { "collapsed": false }, "outputs": [], "source": [ "for (Int_t i = 0; i < parameterScan->numEntries(); ++i) {\n", " tmpPoint = (RooArgSet *)parameterScan->get(i)->clone(\"temp\");\n", " double arMax = belt->GetAcceptanceRegionMax(*tmpPoint);\n", " double arMin = belt->GetAcceptanceRegionMax(*tmpPoint);\n", " double poiVal = tmpPoint->getRealValue(firstPOI->GetName());\n", " histOfThresholds->Fill(poiVal, arMax);\n", "}\n", "histOfThresholds->SetMinimum(0);\n", "histOfThresholds->Draw();" ] }, { "cell_type": "markdown", "id": "bc022020", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "0e586695", "metadata": { "collapsed": false }, "outputs": [], "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 }