Standard demo of the Bayesian MCMC calculator
This is a standard demo that can be used with any ROOT file prepared in the standard way. You specify:
With default parameters the macro will attempt to run the standard hist2workspace example and read the ROOT file that it produces.
The actual heart of the demo is only about 10 lines long.
The MCMCCalculator is a Bayesian tool that uses the Metropolis-Hastings algorithm to efficiently integrate in many dimensions. It is not as accurate as the BayesianCalculator for simple problems, but it scales to much more complicated cases.
Author: Kyle Cranmer
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:18 PM.
%%cpp -d
#include "TFile.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TMath.h"
#include "TSystem.h"
#include "RooWorkspace.h"
#include "RooAbsData.h"
#include "RooStats/ModelConfig.h"
#include "RooStats/MCMCCalculator.h"
#include "RooStats/MCMCInterval.h"
#include "RooStats/MCMCIntervalPlot.h"
#include "RooStats/SequentialProposal.h"
#include "RooStats/ProposalHelper.h"
#include "RooStats/ProposalHelper.h"
#include "RooFitResult.h"
using namespace RooFit;
using namespace RooStats;
struct BayesianMCMCOptions {
double confLevel = 0.95;
int intervalType = 2; // type of interval (0 is shortest, 1 central, 2 upper limit)
double maxPOI = -999; // force different values of POI for doing the scan (default is given value)
double minPOI = -999;
int numIters = 100000; // number of iterations
int numBurnInSteps = 100; // number of burn in steps to be ignored
};
BayesianMCMCOptions optMCMC;
Arguments are defined.
const char *infile = "";
const char *workspaceName = "combined";
const char *modelConfigName = "ModelConfig";
const char *dataName = "obsData";
First part is just to access a user-defined file or create the standard example file if it doesn't exist
const char *filename = "";
if (!strcmp(infile, "")) {
filename = "results/example_combined_GaussExample_model.root";
bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
// if file does not exists generate with histfactory
if (!fileExist) {
// Normally this would be run on the command line
cout << "will run standard hist2workspace example" << endl;
gROOT->ProcessLine(".! prepareHistFactory .");
gROOT->ProcessLine(".! hist2workspace config/example.xml");
cout << "\n\n---------------------" << endl;
cout << "Done creating example input" << endl;
cout << "---------------------\n\n" << endl;
}
} else
filename = infile;
Try to open the file
TFile *file = TFile::Open(filename);
if input file was specified byt not found, quit
if (!file) {
cout << "StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
return;
}
get the workspace out of the file
RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
if (!w) {
cout << "workspace not found" << endl;
return;
}
get the modelConfig out of the file
ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
get the modelConfig out of the file
RooAbsData *data = w->data(dataName);
input_line_83:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration RooAbsData *data = w->data(dataName); ^
make sure ingredients are found
if (!data || !mc) {
w->Print();
cout << "data or ModelConfig was not found" << endl;
return;
}
input_line_84:2:7: error: reference to 'data' is ambiguous if (!data || !mc) { ^ input_line_83:2:14: note: candidate found by name lookup is 'data' RooAbsData *data = w->data(dataName); ^ /usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data' data(initializer_list<_Tp> __il) noexcept ^ /usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data' data(_Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data' data(const _Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data' data(_Tp (&__array)[_Nm]) noexcept ^
Want an efficient proposal function default is uniform.
/*
this one is based on the covariance matrix of fit
RooFitResult* fit = mc->GetPdf()->fitTo(*data,Save());
ProposalHelper ph;
ph.SetVariables((RooArgSet&)fit->floatParsFinal());
ph.SetCovMatrix(fit->covarianceMatrix());
ph.SetUpdateProposalParameters(kTRUE); // auto-create mean vars and add mappings
ph.SetCacheSize(100);
ProposalFunction* pf = ph.GetProposalFunction();
*/
this proposal function seems fairly robust
SequentialProposal sp(0.1);
create and use the MCMCCalculator to find and plot the 95% credible interval on the parameter of interest as specified in the model config
MCMCCalculator mcmc(*data, *mc);
mcmc.SetConfidenceLevel(optMCMC.confLevel); // 95% interval
input_line_87:2:23: error: reference to 'data' is ambiguous MCMCCalculator mcmc(*data, *mc); ^ input_line_83:2:14: note: candidate found by name lookup is 'data' RooAbsData *data = w->data(dataName); ^ /usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data' data(initializer_list<_Tp> __il) noexcept ^ /usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data' data(_Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data' data(const _Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data' data(_Tp (&__array)[_Nm]) noexcept ^
mcmc.SetProposalFunction(*pf);
mcmc.SetProposalFunction(sp);
mcmc.SetNumIters(optMCMC.numIters); // Metropolis-Hastings algorithm iterations
mcmc.SetNumBurnInSteps(optMCMC.numBurnInSteps); // first N steps to be ignored as burn-in
input_line_89:2:3: error: use of undeclared identifier 'mcmc' (mcmc.SetProposalFunction(((*(SequentialProposal*)0x7ff557395000)))) ^ Error in <HandleInterpreterException>: Error evaluating expression (mcmc.SetProposalFunction(((*(SequentialProposal*)0x7ff557395000)))) Execution of your code was aborted.
default is the shortest interval.
if (optMCMC.intervalType == 0)
mcmc.SetIntervalType(MCMCInterval::kShortest); // for shortest interval (not really needed)
if (optMCMC.intervalType == 1)
mcmc.SetLeftSideTailFraction(0.5); // for central interval
if (optMCMC.intervalType == 2)
mcmc.SetLeftSideTailFraction(0.); // for upper limit
RooRealVar *firstPOI = (RooRealVar *)mc->GetParametersOfInterest()->first();
if (optMCMC.minPOI != -999)
firstPOI->setMin(optMCMC.minPOI);
if (optMCMC.maxPOI != -999)
firstPOI->setMax(optMCMC.maxPOI);
MCMCInterval *interval = mcmc.GetInterval();
input_line_90:3:25: error: cannot take the address of an rvalue of type 'RooStats::MCMCInterval::IntervalType' mcmc.SetIntervalType(MCMCInterval::kShortest); // for shortest interval (not really needed) ^~~~~~~~~~~~~~~~~~~~~~~ Error while creating dynamic expression for: mcmc.SetIntervalType(MCMCInterval::kShortest)
make a plot TCanvas* c1 =
auto c1 = new TCanvas("IntervalPlot");
MCMCIntervalPlot plot(*interval);
plot.Draw();
TCanvas *c2 = new TCanvas("extraPlots");
const RooArgSet *list = mc->GetNuisanceParameters();
if (list->getSize() > 1) {
double n = list->getSize();
int ny = TMath::CeilNint(sqrt(n));
int nx = TMath::CeilNint(double(n) / ny);
c2->Divide(nx, ny);
}
input_line_91:7:1: warning: 'list' shadows a declaration with the same name in the 'std' namespace; use '::list' to reference this declaration const RooArgSet *list = mc->GetNuisanceParameters(); ^ [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv, _ZN5TMath8CeilNintEd, _ZNK8RooStats11ModelConfig21GetNuisanceParametersEv, _ZNKSt6vectorIP9RooAbsArgSaIS1_EE4sizeEv, __orc_init_func.cling-module-483, __cxx_global_var_initcling_module_483_.5, _ZN12__cling_N53224__dynamic__cling_Un1Qu30E, _ZN7TObjectnwEm, _ZNK16RooAbsCollection7getSizeEv, __cxx_global_var_initcling_module_483_.6, _ZN12__cling_N5324listE, _ZN13RooStringViewC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, $.cling-module-483.__inits.0, _ZN12__cling_N5322c2E, _ZN12__cling_N53216__cling_Un1Qu341EPv, __cxx_global_var_initcling_module_483_.7, _ZN13RooStringViewC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, _GLOBAL__sub_I_cling_module_483, _ZN12__cling_N5322c1E, __vd_init_order__cling_Un1Qu35, _Z30__fd_init_order__cling_Un1Qu34v, _ZN12__cling_N5324plotE, __cxx_global_var_initcling_module_483_.9, __cxx_global_var_initcling_module_483_, __cxx_global_var_initcling_module_483_.2, _ZN5TMath4NintIdEEiT_ }) } IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerC1EPNS1_15DynamicExprInfoEPN5clang11DeclContextEPKcPNS_11InterpreterE' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::LifetimeHandler(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*, char const*, cling::Interpreter*) Maybe you need to load the corresponding shared library? IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerD1Ev' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::~LifetimeHandler() Maybe you need to load the corresponding shared library?
draw a scatter plot of chain results for poi vs each nuisance parameters
TIter it = mc->GetNuisanceParameters()->createIterator();
RooRealVar *nuis = NULL;
int iPad = 1; // iPad, that's funny
while ((nuis = (RooRealVar *)it.Next())) {
c2->cd(iPad++);
plot.DrawChainScatter(*firstPOI, *nuis);
}
input_line_92:2:42: warning: 'createIterator' is deprecated: will be removed in ROOT v6.34: begin(), end() and range-based for loops. [-Wdeprecated-declarations] TIter it = mc->GetNuisanceParameters()->createIterator(); ^ /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RooAbsCollection.h:256:3: note: 'createIterator' has been explicitly marked deprecated here R__DEPRECATED(6,34, "begin(), end() and range-based for loops.") { ^ /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RConfig.hxx:522:3: note: expanded from macro 'R__DEPRECATED' _R__JOIN3_(_R__DEPRECATED_,MAJOR,MINOR)("will be removed in ROOT v" #MAJOR "." #MINOR ": " REASON) ^ /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RConfig.hxx:477:30: note: expanded from macro '_R__JOIN3_' # define _R__JOIN3_(F,X,Y) _NAME3_(F,X,Y) ^ /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RConfig.hxx:460:39: note: expanded from macro '_NAME3_' # define _NAME3_(name1,name2,name3) name1##name2##name3 ^ <scratch space>:80:1: note: expanded from here _R__DEPRECATED_634 ^ /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RConfig.hxx:506:37: note: expanded from macro '_R__DEPRECATED_634' # define _R__DEPRECATED_634(REASON) _R__DEPRECATED_LATER(REASON) ^ /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RConfig.hxx:488:56: note: expanded from macro '_R__DEPRECATED_LATER' # define _R__DEPRECATED_LATER(REASON) __attribute__((deprecated(REASON))) ^ cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5322c2E }) } [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN5TIterD2Ev, _ZSt12__get_helperILm1ESt14default_deleteI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS4_EEEEJEERT0_RSt11_Tuple_implIXT_EJS9_DpT1_EE, _ZN12__cling_N5334iPadE, _ZSt3getILm0EJP23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERSE_, _ZNSt10unique_ptrI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EED1Ev, _ZN9TIteratorD2Ev, _ZNKSt10unique_ptrI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EE3getEv, _ZN23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS2_EEED1Ev, _ZNSt12__shared_ptrI9TIteratorLN9__gnu_cxx12_Lock_policyE2EEC2I23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS8_EEESt14default_deleteISB_EvEEOSt10unique_ptrIT_T0_E, _ZNSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EE7_M_swapERS2_, _ZNSt12__shared_ptrI9TIteratorLN9__gnu_cxx12_Lock_policyE2EE31_M_enable_shared_from_this_withI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS8_EEESB_EENSt9enable_ifIXntsr15__has_esft_baseIT0_EE5valueEvE4typeEPT_, $.cling-module-484.__inits.0, _ZNSt12__shared_ptrI9TIteratorLN9__gnu_cxx12_Lock_policyE2EEC2EOS3_, _ZNSt10unique_ptrI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EE11get_deleterEv, _ZN17RooLinkedListIterC1ESt10shared_ptrI9TIteratorE, _ZN17RooLinkedListIterC2ESt10shared_ptrI9TIteratorE, _ZNSt15__uniq_ptr_implI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EE6_M_ptrEv, _ZNKSt14default_deleteI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEEEclEPS6_, _ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE15_M_add_ref_copyEv, _ZNK16RooAbsCollection14createIteratorEb, _ZNSt10shared_ptrI9TIteratorED1Ev, _GLOBAL__sub_I_cling_module_484, _ZN15GenericRooFIterD2Ev, __cxx_global_var_initcling_module_484_, _ZN9__gnu_cxxL19__atomic_add_singleEPii, _ZNSt10unique_ptrI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EED2Ev, _ZNSt10_Head_baseILm0EP23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEELb0EE7_M_headERS8_, _ZNSt10shared_ptrI9TIteratorEC2EOS1_, _ZN12__cling_N53316__cling_Un1Qu342EPv, _ZN23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS2_EEED2Ev, _ZNSt10shared_ptrI9TIteratorEC1I23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS6_EEESt14default_deleteIS9_EvEEOSt10unique_ptrIT_T0_E, _ZN12__cling_N5334nuisE, _ZSt3getILm1EJP23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERSE_, _ZN5TIterC1EP9TIterator, _ZNSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EEaSERKS2_, _ZN5TIterC2EP9TIterator, _ZNSt10shared_ptrI9TIteratorEC2I23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS6_EEESt14default_deleteIS9_EvEEOSt10unique_ptrIT_T0_E, _ZN9__gnu_cxxL12__atomic_addEPVii, _ZNSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EEC1I23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS7_EEESt14default_deleteISA_EEEOSt10unique_ptrIT_T0_E, _ZSt12__get_helperILm0EP23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEEJSt14default_deleteIS6_EEERKT0_RKSt11_Tuple_implIXT_EJSA_DpT1_EE, __orc_init_func.cling-module-484, _ZNSt10shared_ptrI9TIteratorEC1EOS1_, _ZN9TIteratorC2Ev, _ZNSt11_Tuple_implILm1EJSt14default_deleteI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS4_EEEEEE7_M_headERS9_, _ZN9__gnu_cxxL21__atomic_add_dispatchEPii, _ZNSt12__shared_ptrI9TIteratorLN9__gnu_cxx12_Lock_policyE2EED2Ev, _ZNSt15__uniq_ptr_implI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EE10_M_deleterEv, _ZSt12__get_helperILm0EP23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEEJSt14default_deleteIS6_EEERT0_RSt11_Tuple_implIXT_EJSA_DpT1_EE, _ZNKSt15__uniq_ptr_implI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EE6_M_ptrEv, _ZN12__cling_N5332itE, _ZNSt10shared_ptrI9TIteratorED2Ev, _ZN5TIterD1Ev, _ZNSt10_Head_baseILm1ESt14default_deleteI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS4_EEEELb1EE7_M_headERS9_, _ZNSt11_Tuple_implILm0EJP23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EEE7_M_headERSA_, _ZSt12__to_addressI23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEEEPT_S8_, _ZNSt10_Head_baseILm0EP23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEELb0EE7_M_headERKS8_, _ZSt3getILm0EJP23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EEERKNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERKSE_, _ZNSt11_Tuple_implILm0EJP23TIteratorToSTLInterfaceISt6vectorIP9RooAbsArgSaIS3_EEESt14default_deleteIS6_EEE7_M_headERKSA_ }) }
print out the interval on the first Parameter of Interest
cout << "\n>>>> RESULT : " << optMCMC.confLevel * 100 << "% interval on " << firstPOI->GetName() << " is : ["
<< interval->LowerLimit(*firstPOI) << ", " << interval->UpperLimit(*firstPOI) << "] " << endl;
gPad = c1;
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-483 }) } cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5322c1E }) }
Draw all canvases
%jsroot on
gROOT->GetListOfCanvases()->Draw()