{ "cells": [ { "cell_type": "markdown", "id": "3921781f", "metadata": {}, "source": [ "# StandardFrequentistDiscovery\n", "StandardFrequentistDiscovery\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", "\n", "\n", "\n", "**Author:** Sven Kreiss, 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": "e4e01b49", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TFile.h\"\n", "#include \"TROOT.h\"\n", "#include \"TH1F.h\"\n", "#include \"TF1.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TStopwatch.h\"\n", "\n", "#include \"RooWorkspace.h\"\n", "#include \"RooAbsData.h\"\n", "#include \"RooRandom.h\"\n", "#include \"RooRealSumPdf.h\"\n", "#include \"RooNumIntConfig.h\"\n", "\n", "#include \"RooStats/ModelConfig.h\"\n", "#include \"RooStats/ToyMCImportanceSampler.h\"\n", "#include \"RooStats/HypoTestResult.h\"\n", "#include \"RooStats/HypoTestPlot.h\"\n", "#include \"RooStats/SamplingDistribution.h\"\n", "#include \"RooStats/ProfileLikelihoodTestStat.h\"\n", "#include \"RooStats/SimpleLikelihoodRatioTestStat.h\"\n", "#include \"RooStats/ProfileLikelihoodCalculator.h\"\n", "#include \"RooStats/LikelihoodInterval.h\"\n", "#include \"RooStats/LikelihoodIntervalPlot.h\"\n", "\n", "#include \"RooStats/FrequentistCalculator.h\"\n", "#include \"TSystem.h\"\n", "\n", "#include \n", "\n", "using namespace RooFit;\n", "using namespace RooStats;" ] }, { "cell_type": "markdown", "id": "d05cc7ae", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": null, "id": "a3d7471a", "metadata": { "collapsed": false }, "outputs": [], "source": [ "const char *infile = \"\";\n", "const char *workspaceName = \"channel1\";\n", "const char *modelConfigNameSB = \"ModelConfig\";\n", "const char *dataName = \"obsData\";\n", "int toys = 1000;\n", "double poiValueForBackground = 0.0;\n", "double poiValueForSignal = 1.0;" ] }, { "cell_type": "markdown", "id": "954567cd", "metadata": {}, "source": [ "The workspace contains the model for s+b. The b model is \"autogenerated\"\n", "by copying s+b and setting the one parameter of interest to zero.\n", "To keep the script simple, multiple parameters of interest or different\n", "functional forms of the b model are not supported." ] }, { "cell_type": "markdown", "id": "93fc44b9", "metadata": {}, "source": [ "for now, assume there is only one parameter of interest, and these are\n", "its values:" ] }, { "cell_type": "markdown", "id": "66bef577", "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": "1cf2014b", "metadata": { "collapsed": false }, "outputs": [], "source": [ "const char *filename = \"\";\n", "if (!strcmp(infile, \"\")) {\n", " filename = \"results/example_channel1_GammaExample_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": "a86a73ea", "metadata": {}, "source": [ "Try to open the file" ] }, { "cell_type": "code", "execution_count": null, "id": "e58006fc", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TFile *file = TFile::Open(filename);" ] }, { "cell_type": "markdown", "id": "f80fd99e", "metadata": {}, "source": [ "if input file was specified but not found, quit" ] }, { "cell_type": "code", "execution_count": null, "id": "c421c8b4", "metadata": { "collapsed": false }, "outputs": [], "source": [ "if (!file) {\n", " cout << \"StandardRooStatsDemoMacro: Input file \" << filename << \" is not found\" << endl;\n", " return -1;\n", "}" ] }, { "cell_type": "markdown", "id": "cad99493", "metadata": {}, "source": [ "-------------------------------------------------------\n", "Tutorial starts here\n", "-------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": null, "id": "23415b1f", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TStopwatch *mn_t = new TStopwatch;\n", "mn_t->Start();" ] }, { "cell_type": "markdown", "id": "ffc18f02", "metadata": {}, "source": [ "get the workspace out of the file" ] }, { "cell_type": "code", "execution_count": null, "id": "2c21d3b8", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);\n", "if (!w) {\n", " cout << \"workspace not found\" << endl;\n", " return -1.0;\n", "}" ] }, { "cell_type": "markdown", "id": "facb9566", "metadata": {}, "source": [ "get the modelConfig out of the file" ] }, { "cell_type": "code", "execution_count": null, "id": "6db02973", "metadata": { "collapsed": false }, "outputs": [], "source": [ "ModelConfig *mc = (ModelConfig *)w->obj(modelConfigNameSB);" ] }, { "cell_type": "markdown", "id": "19b013ff", "metadata": {}, "source": [ "get the data out of the file" ] }, { "cell_type": "code", "execution_count": null, "id": "4c46ae0c", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RooAbsData *data = w->data(dataName);" ] }, { "cell_type": "markdown", "id": "392cd520", "metadata": {}, "source": [ "make sure ingredients are found" ] }, { "cell_type": "code", "execution_count": null, "id": "74bf98cc", "metadata": { "collapsed": false }, "outputs": [], "source": [ "if (!data || !mc) {\n", " w->Print();\n", " cout << \"data or ModelConfig was not found\" << endl;\n", " return -1.0;\n", "}\n", "\n", "RooRealVar *firstPOI = (RooRealVar *)mc->GetParametersOfInterest()->first();\n", "firstPOI->setVal(poiValueForSignal);\n", "mc->SetSnapshot(*mc->GetParametersOfInterest());" ] }, { "cell_type": "markdown", "id": "90beb0f1", "metadata": {}, "source": [ "create null model" ] }, { "cell_type": "code", "execution_count": null, "id": "2a012f8b", "metadata": { "collapsed": false }, "outputs": [], "source": [ "ModelConfig *mcNull = mc->Clone(\"ModelConfigNull\");\n", "firstPOI->setVal(poiValueForBackground);\n", "mcNull->SetSnapshot(*(RooArgSet *)mcNull->GetParametersOfInterest()->snapshot());" ] }, { "cell_type": "markdown", "id": "3f650ef9", "metadata": {}, "source": [ "----------------------------------------------------\n", "Configure a ProfileLikelihoodTestStat and a SimpleLikelihoodRatioTestStat\n", "to use simultaneously with ToyMCSampler" ] }, { "cell_type": "code", "execution_count": null, "id": "72f781cc", "metadata": { "collapsed": false }, "outputs": [], "source": [ "ProfileLikelihoodTestStat *plts = new ProfileLikelihoodTestStat(*mc->GetPdf());\n", "plts->SetOneSidedDiscovery(true);\n", "plts->SetVarName(\"q_{0}/2\");" ] }, { "cell_type": "markdown", "id": "21bcaa94", "metadata": {}, "source": [ "----------------------------------------------------\n", "configure the ToyMCImportanceSampler with two test statistics" ] }, { "cell_type": "code", "execution_count": null, "id": "3f43a020", "metadata": { "collapsed": false }, "outputs": [], "source": [ "ToyMCSampler toymcs(*plts, 50);" ] }, { "cell_type": "markdown", "id": "4695b4dc", "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": "889b055c", "metadata": { "collapsed": false }, "outputs": [], "source": [ "if (!mc->GetPdf()->canBeExtended()) {\n", " if (data->numEntries() == 1) {\n", " toymcs.SetNEventsPerToy(1);\n", " } else\n", " cout << \"Not sure what to do about this model\" << endl;\n", "}" ] }, { "cell_type": "markdown", "id": "d18ed4d5", "metadata": {}, "source": [ "instantiate the calculator" ] }, { "cell_type": "code", "execution_count": null, "id": "d48d3909", "metadata": { "collapsed": false }, "outputs": [], "source": [ "FrequentistCalculator freqCalc(*data, *mc, *mcNull, &toymcs);\n", "freqCalc.SetToys(toys, toys); // null toys, alt toys" ] }, { "cell_type": "markdown", "id": "6e30bd5c", "metadata": {}, "source": [ "Run the calculator and print result" ] }, { "cell_type": "code", "execution_count": null, "id": "1aeee46f", "metadata": { "collapsed": false }, "outputs": [], "source": [ "HypoTestResult *freqCalcResult = freqCalc.GetHypoTest();\n", "freqCalcResult->GetNullDistribution()->SetTitle(\"b only\");\n", "freqCalcResult->GetAltDistribution()->SetTitle(\"s+b\");\n", "freqCalcResult->Print();\n", "double pvalue = freqCalcResult->NullPValue();" ] }, { "cell_type": "markdown", "id": "108de81c", "metadata": {}, "source": [ "stop timing" ] }, { "cell_type": "code", "execution_count": null, "id": "e45ace55", "metadata": { "collapsed": false }, "outputs": [], "source": [ "mn_t->Stop();\n", "cout << \"total CPU time: \" << mn_t->CpuTime() << endl;\n", "cout << \"total real time: \" << mn_t->RealTime() << endl;" ] }, { "cell_type": "markdown", "id": "691a84b3", "metadata": {}, "source": [ "plot" ] }, { "cell_type": "code", "execution_count": null, "id": "9a34ea68", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas();\n", "HypoTestPlot *plot = new HypoTestPlot(*freqCalcResult, 100, -0.49, 9.51);\n", "plot->SetLogYaxis(true);" ] }, { "cell_type": "markdown", "id": "105127b8", "metadata": {}, "source": [ "add chi2 to plot" ] }, { "cell_type": "code", "execution_count": null, "id": "1d12d0fd", "metadata": { "collapsed": false }, "outputs": [], "source": [ "int nPOI = 1;\n", "TF1 *f = new TF1(\"f\", TString::Format(\"1*ROOT::Math::chisquared_pdf(2*x,%d,0)\", nPOI), 0, 20);\n", "f->SetLineColor(kBlack);\n", "f->SetLineStyle(7);\n", "plot->AddTF1(f, TString::Format(\"#chi^{2}(2x,%d)\", nPOI));\n", "\n", "plot->Draw();\n", "c1->SaveAs(\"standard_discovery_output.pdf\");\n", "\n", "return pvalue;" ] }, { "cell_type": "markdown", "id": "5af5ec9b", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "a1395656", "metadata": { "collapsed": false }, "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 }