From 8107236d61427b018144c32b9d3288286ba37ea9 Mon Sep 17 00:00:00 2001 From: Jonas Rembser Date: Thu, 9 Nov 2023 12:06:15 +0100 Subject: [PATCH] [RF] Make `rootbench` also build in memory-safe interfaces mode In particular, this avoids some leaking RooWorkspaces. --- root/roofit/histfactory/TestWorkspaces.cxx | 4 ++-- root/roofit/histfactory/TestWorkspaces.h | 5 ++++- root/roofit/histfactory/benchHistFactory.cxx | 16 ++++++++-------- root/roofit/roofit/RooFitBinnedBenchmarks.cxx | 6 +++--- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/root/roofit/histfactory/TestWorkspaces.cxx b/root/roofit/histfactory/TestWorkspaces.cxx index cd65ed962..9c6576704 100644 --- a/root/roofit/histfactory/TestWorkspaces.cxx +++ b/root/roofit/histfactory/TestWorkspaces.cxx @@ -12,7 +12,7 @@ /// Returns the workspace that is also created in the hf001_example.C tutorial. /// Note that running this function creates a directory `hf001_data` in the /// current directory. -RooWorkspace * +std::unique_ptr TestWorkspaces::getWorkspace001(RooStats::HistFactory::HistoToWorkspaceFactoryFast::Configuration const &cfg) { using namespace RooStats::HistFactory; @@ -81,5 +81,5 @@ TestWorkspaces::getWorkspace001(RooStats::HistFactory::HistoToWorkspaceFactoryFa meas.SetExportOnly(true); // Now, do the measurement - return MakeModelAndMeasurementFast(meas, cfg); + return std::unique_ptr{MakeModelAndMeasurementFast(meas, cfg)}; } diff --git a/root/roofit/histfactory/TestWorkspaces.h b/root/roofit/histfactory/TestWorkspaces.h index f7258f2cb..4caebac84 100644 --- a/root/roofit/histfactory/TestWorkspaces.h +++ b/root/roofit/histfactory/TestWorkspaces.h @@ -3,11 +3,14 @@ #include +#include + class RooWorkspace; namespace TestWorkspaces { -RooWorkspace *getWorkspace001(RooStats::HistFactory::HistoToWorkspaceFactoryFast::Configuration const &cfg); +std::unique_ptr +getWorkspace001(RooStats::HistFactory::HistoToWorkspaceFactoryFast::Configuration const &cfg); } // namespace TestWorkspaces diff --git a/root/roofit/histfactory/benchHistFactory.cxx b/root/roofit/histfactory/benchHistFactory.cxx index 4a9b82e71..689819a70 100644 --- a/root/roofit/histfactory/benchHistFactory.cxx +++ b/root/roofit/histfactory/benchHistFactory.cxx @@ -23,18 +23,18 @@ class TestData { using namespace RooFit; RooStats::HistFactory::HistoToWorkspaceFactoryFast::Configuration hfCfg; - auto *w = TestWorkspaces::getWorkspace001(hfCfg); + std::unique_ptr ws = TestWorkspaces::getWorkspace001(hfCfg); - auto *mc = static_cast(w->obj("ModelConfig")); + auto *mc = static_cast(ws->obj("ModelConfig")); if (mc->GetParametersOfInterest()) constraintParams.add(*mc->GetParametersOfInterest()); if (mc->GetNuisanceParameters()) constraintParams.add(*mc->GetNuisanceParameters()); - auto *pdf = w->pdf("simPdf"); + auto *pdf = ws->pdf("simPdf"); - std::unique_ptr nll{pdf->createNLL(*w->data("obsData"), Constrain(constraintParams), + std::unique_ptr nll{pdf->createNLL(*ws->data("obsData"), Constrain(constraintParams), GlobalObservables(*mc->GetGlobalObservables()), EvalBackend::Legacy())}; RooMinimizer m(*nll); @@ -64,14 +64,14 @@ static void benchHistFactory001(benchmark::State &state) RooStats::HistFactory::HistoToWorkspaceFactoryFast::Configuration hfCfg; hfCfg.binnedFitOptimization = state.range(0); - auto *w = TestWorkspaces::getWorkspace001(hfCfg); + std::unique_ptr ws = TestWorkspaces::getWorkspace001(hfCfg); - auto *mc = static_cast(w->obj("ModelConfig")); + auto *mc = static_cast(ws->obj("ModelConfig")); - auto *pdf = w->pdf("simPdf"); + auto *pdf = ws->pdf("simPdf"); auto evalBackend = static_cast(state.range(1)); - std::unique_ptr nll{pdf->createNLL(*w->data("obsData"), Constrain(g_testData.constraintParams), + std::unique_ptr nll{pdf->createNLL(*ws->data("obsData"), Constrain(g_testData.constraintParams), GlobalObservables(*mc->GetGlobalObservables()), EvalBackend(evalBackend))}; diff --git a/root/roofit/roofit/RooFitBinnedBenchmarks.cxx b/root/roofit/roofit/RooFitBinnedBenchmarks.cxx index 8babe7735..809effbb3 100644 --- a/root/roofit/roofit/RooFitBinnedBenchmarks.cxx +++ b/root/roofit/roofit/RooFitBinnedBenchmarks.cxx @@ -115,11 +115,11 @@ void buildBinnedTest(int n_channels = 1, int nbins = 10, int nnps = 1, const cha meas.AddChannel(*chan); } HistoToWorkspaceFactoryFast hist2workspace(meas); - RooWorkspace *ws; + std::unique_ptr ws; if (n_channels < 2) { - ws = hist2workspace.MakeSingleChannelModel(meas, *chan); + ws = std::unique_ptr{hist2workspace.MakeSingleChannelModel(meas, *chan)}; } else { - ws = hist2workspace.MakeCombinedModel(meas); + ws = std::unique_ptr{hist2workspace.MakeCombinedModel(meas)}; } for (RooAbsArg * arg : ws->components()) { if (arg->IsA() == RooRealSumPdf::Class()) {