Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README/ReleaseNotes/v644/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,51 @@ RenderCore, the WebGL engine of the REve client, is fetched at build time as the

## RooFit

### HistFactory models with fewer RooFit objects

HistFactory now builds each channel as a `RooHistFactoryPdf`, which corresponds
to the `histfactory_dist` of the HS3 standard. Each sample of a channel is a
single `RooHistFactorySample` object, which holds the nominal histogram and all
its modifiers (NormFactor, OverallSys, HistoSys, StatError, ShapeSys and
ShapeFactor). Before, HistFactory created several RooFit objects for each
histogram (`RooHistFunc`, `RooDataHist`, `PiecewiseInterpolation`,
`FlexibleInterpVar`, `ParamHistFunc` and `RooProduct`). For models with many
systematic variations, this reduces the size of the workspaces in memory and on
disk. The histogram data of a sample is shared between its copies, so cloning
the model, e.g. when creating the likelihood, doesn't copy the histograms.

The models have the same parameters, constraint terms, observables and datasets
as before, and they give the same values up to floating point rounding. The
binned likelihood optimization,
the code generation backend and the plotting of single samples with
`RooFit::Components("<sample>_<channel>_shapes")` work as before. Code that
navigates the internal structure of HistFactory models, e.g. to retrieve the
`RooHistFunc` of a sample, has to be adapted. The modifiers of a sample are
available via `RooHistFactorySample::data()`.

When a workspace is written to a ROOT file with `RooWorkspace::Write()` or
`RooWorkspace::writeToFile()`, the histogram data of all its samples goes to an
RNTuple next to the workspace, named after the workspace with the suffix
`_histfactory_arrays`. Like this, HistFactory workspaces are no longer limited
by the 1 GB size limit of single objects in ROOT files. `hist2workspace` writes
its workspaces like this. With `TDirectory::WriteObject()`, the histograms stay
in the workspace object.

The `Measurement` that HistFactory writes next to the workspaces holds all
histograms as well, so it is subject to the same limit. Since the workspaces
don't need it, writing it can be disabled with the new `writeMeasurement`
member of `RooStats::HistFactory::HistoToWorkspaceFactoryFast::Configuration`.

The HS3 importer also creates `RooHistFactoryPdf` objects for the
`histfactory_dist` distributions, and the exporter writes them with the names
of their modifiers.

To get the previous model structure, set the `useHistFactoryPdf` member of the
`RooStats::HistFactory::HistoToWorkspaceFactoryFast::Configuration` to `false`,
use the `-legacy_model_structure` command line argument of `hist2workspace`, or
set `RooJSONFactoryWSTool::config().useHistFactoryPdf = false` for the HS3
importer. Workspaces with the previous structure can still be read.

### Removal of the legacy evaluation backend

The `legacy` evaluation backend for likelihood and chi-square fits is removed.
Expand Down
3 changes: 3 additions & 0 deletions roofit/codegen/inc/RooFit/CodegenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class RooFunctorPdfBinding;
class RooGamma;
class RooGaussian;
class RooGenericPdf;
class RooHistFactorySample;
class RooHistFunc;
class RooHistPdf;
class RooLandau;
Expand Down Expand Up @@ -109,6 +110,7 @@ void codegenImpl(RooFunctorPdfBinding &arg, CodegenContext &ctx);
void codegenImpl(RooGamma &arg, CodegenContext &ctx);
void codegenImpl(RooGaussian &arg, CodegenContext &ctx);
void codegenImpl(RooGenericPdf &arg, CodegenContext &ctx);
void codegenImpl(RooHistFactorySample &arg, CodegenContext &ctx);
void codegenImpl(RooHistFunc &arg, CodegenContext &ctx);
void codegenImpl(RooHistPdf &arg, CodegenContext &ctx);
void codegenImpl(RooLandau &arg, CodegenContext &ctx);
Expand Down Expand Up @@ -159,6 +161,7 @@ std::string codegenIntegralImpl(RooEfficiency &arg, int code, const char *rangeN
std::string codegenIntegralImpl(RooExponential &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooGamma &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooGaussian &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooHistFactorySample &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooHistFunc &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooHistPdf &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooLandau &arg, int code, const char *rangeName, CodegenContext &ctx);
Expand Down
103 changes: 103 additions & 0 deletions roofit/codegen/src/CodegenImpl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include <RooStats/HistFactory/FlexibleInterpVar.h>
#include <RooStats/HistFactory/ParamHistFunc.h>
#include <RooStats/HistFactory/PiecewiseInterpolation.h>
#include <RooStats/HistFactory/RooHistFactorySample.h>
#include <RooUniform.h>
#include <RooWrapperPdf.h>

Expand Down Expand Up @@ -594,6 +595,108 @@ void codegenImpl(RooGenericPdf &arg, CodegenContext &ctx)
ctx.addResult(&arg, funcName + "(" + inputVar + ")");
}

namespace {

/// The array arguments of the kernels of a RooHistFactorySample.
std::string intArrayArg(CodegenContext &ctx, std::vector<int> const &arr)
{
return arr.empty() ? "nullptr" : ctx.buildArg(std::span<const int>{arr});
}

std::string doubleArrayArg(CodegenContext &ctx, std::vector<double> const &arr)
{
return ctx.buildArg(std::span<const double>{arr});
}

/// The arguments of RooFit::Detail::MathFuncs::histFactoryBin() and histFactoryIntegral() after the bin.
std::string histFactoryBinArgs(RooHistFactorySample const &arg, CodegenContext &ctx)
{
auto const &data = arg.data();
return std::to_string(data.numBins()) + ", " + doubleArrayArg(ctx, data.contents) + ", " +
std::to_string(data.histoSysParams.size()) + ", " + intArrayArg(ctx, data.histoSysParams) + ", " +
intArrayArg(ctx, data.histoSysCodes) + ", " + doubleArrayArg(ctx, data.histoSysLo) + ", " +
doubleArrayArg(ctx, data.histoSysHi) + ", " + std::to_string(data.binFactorNames.size()) + ", " +
intArrayArg(ctx, data.binFactorParams);
}

/// The scale factor of a RooHistFactorySample, which only depends on the parameters and is therefore computed outside
/// of the loop over the data.
std::string histFactoryScale(RooHistFactorySample const &arg, std::string const &params, CodegenContext &ctx)
{
auto const &data = arg.data();
std::string scaleName = ctx.getTmpVarName();
std::string call = "RooFit::Detail::MathFuncs::histFactoryScale(" + params + ", " +
std::to_string(data.normSysParams.size()) + ", " + intArrayArg(ctx, data.normSysParams) + ", " +
intArrayArg(ctx, data.normSysCodes) + ", " + doubleArrayArg(ctx, data.normSysLo) + ", " +
doubleArrayArg(ctx, data.normSysHi) + ", " + std::to_string(data.normFactorParams.size()) + ", " +
intArrayArg(ctx, data.normFactorParams) + ")";
ctx.addToCodeBody("double " + scaleName + " = " + call + ";\n", /*isScopeIndep=*/true);
return scaleName;
}

} // namespace

void codegenImpl(RooHistFactorySample &arg, CodegenContext &ctx)
{
auto const &data = arg.data();
std::string const &params = ctx.buildArg(arg.parameters());
const std::string scale = arg.parameters().empty() ? "1.0" : histFactoryScale(arg, params, ctx);

// The index of the bin with the current values of the observables. The last
// observable varies fastest.
std::string idx;
unsigned int stride = 1;
for (int iDim = static_cast<int>(data.edges.size()) - 1; iDim >= 0; --iDim) {
std::vector<double> const &edges = data.edges[iDim];
const unsigned int nBinsDim = edges.size() - 1;
RooAbsArg const &obs = arg.observables()[iDim];
// The bin numbers are computed with a coefficient of one and only then
// multiplied with the stride, because uniformBinNumber() would
// multiply with the coefficient before rounding down.
std::string term =
data.uniform[iDim]
? ctx.buildCall(mathFunc("uniformBinNumber"), edges.front(), edges.back(), obs, nBinsDim, 1.0)
: ctx.buildCall(mathFunc("binNumber"), obs, 1.0, std::span<const double>{edges}, edges.size(), nBinsDim - 1,
0);
if (stride > 1) {
term = std::to_string(stride) + " * " + term;
}
idx = idx.empty() ? term : term + " + " + idx;
stride *= nBinsDim;
}
std::string idxName = ctx.getTmpVarName();
ctx.addToCodeBody(&arg, "unsigned int " + idxName + " = " + idx + ";\n");

std::string value = mathFunc("histFactoryBin") + "(" + (arg.parameters().empty() ? "nullptr" : params) + ", " +
idxName + ", " + histFactoryBinArgs(arg, ctx) + ")";
if (!arg.yieldsMode()) {
std::vector<double> invVolumes(data.numBins());
for (std::size_t i = 0; i < invVolumes.size(); ++i) {
invVolumes[i] = 1. / arg.binVolume(i);
}
value = "(" + value + " * (" + doubleArrayArg(ctx, invVolumes) + ")[" + idxName + "])";
}
ctx.addResult(&arg, value + " * " + scale);
}

std::string codegenIntegralImpl(RooHistFactorySample &arg, int code, const char *rangeName, CodegenContext &ctx)
{
// Only integrals over all observables are supported, because otherwise
// the integral depends on the bins of the other observables.
if (code != (1 << arg.observables().size())) {
std::stringstream errorMsg;
errorMsg << "codegen: integrals of RooHistFactorySample \"" << arg.GetName()
<< "\" over only some of its observables are not supported";
oocoutE(&arg, InputArguments) << errorMsg.str() << std::endl;
throw std::runtime_error(errorMsg.str());
}
std::string const &params = ctx.buildArg(arg.parameters());
const std::string scale = arg.parameters().empty() ? "1.0" : histFactoryScale(arg, params, ctx);
return "(" + mathFunc("histFactoryIntegral") + "(" + (arg.parameters().empty() ? "nullptr" : params) + ", " +
doubleArrayArg(ctx, arg.integralWeights(code, rangeName)) + ", " + histFactoryBinArgs(arg, ctx) + ") * " +
scale + ")";
}

void codegenImpl(RooHistFunc &arg, CodegenContext &ctx)
{
rooHistTranslateImpl(arg, ctx, arg.getInterpolationOrder(), arg.dataHist(), arg.variables(), false,
Expand Down
5 changes: 5 additions & 0 deletions roofit/histfactory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ set(HISTFACTORY_HEADERS
RooStats/HistFactory/ParamHistFunc.h
RooStats/HistFactory/PiecewiseInterpolation.h
RooStats/HistFactory/RooBarlowBeestonLL.h
RooStats/HistFactory/RooHistFactoryPdf.h
RooStats/HistFactory/RooHistFactorySample.h
)

ROOT_STANDARD_LIBRARY_PACKAGE(HistFactory
Expand All @@ -53,6 +55,8 @@ ROOT_STANDARD_LIBRARY_PACKAGE(HistFactory
src/ParamHistFunc.cxx
src/PiecewiseInterpolation.cxx
src/RooBarlowBeestonLL.cxx
src/RooHistFactoryPdf.cxx
src/RooHistFactorySample.cxx
${HISTFACTORY_XML_SOURCES}
DICTIONARY_OPTIONS
"-writeEmptyRootPCM"
Expand All @@ -61,6 +65,7 @@ ROOT_STANDARD_LIBRARY_PACKAGE(HistFactory
DEPENDENCIES
RooFit
RooFitCore
ROOTNTuple
Tree
RIO
Hist
Expand Down
3 changes: 3 additions & 0 deletions roofit/histfactory/inc/LinkDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#pragma link C++ class PiecewiseInterpolation- ;
#pragma link C++ class ParamHistFunc+ ;
#pragma link C++ class RooHistFactoryPdf+ ;
#pragma link C++ class RooHistFactorySample- ;
#pragma link C++ class RooHistFactorySample::Data+ ;
#pragma link C++ class RooStats::HistFactory::LinInterpVar+ ;
#pragma link C++ class RooStats::HistFactory::FlexibleInterpVar+ ;
#pragma link C++ class RooStats::HistFactory::HistoToWorkspaceFactoryFast+ ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class HistoToWorkspaceFactoryFast : public TObject {
bool createPerRegionWorkspaces = true;
/// \brief Control whether errors on the data histograms are stored in the workspace (default `false`).
bool storeDataError = false;
/// \brief Build each channel as a RooHistFactoryPdf of RooHistFactorySample objects (default `true`). If
/// `false`, the channels are built as before ROOT 6.44, with several RooFit objects for each histogram.
bool useHistFactoryPdf = true;
/// \brief Control whether the Measurement is written to the ROOT files next to the workspaces (default `true`),
/// together with copies of its histograms. The workspaces don't need it. Like any object in a ROOT file, the
/// Measurement, which holds all histograms, is limited to 1 GB.
bool writeMeasurement = true;
};

HistoToWorkspaceFactoryFast() {}
Expand Down
39 changes: 39 additions & 0 deletions roofit/histfactory/inc/RooStats/HistFactory/RooHistFactoryPdf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Project: RooFit
* Authors:
* Jonas Rembser, CERN 2026
*
* Copyright (c) 2026, CERN
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted according to the terms
* listed in LICENSE (http://roofit.sourceforge.net/license.txt)
*/

#ifndef RooStats_HistFactory_RooHistFactoryPdf_h
#define RooStats_HistFactory_RooHistFactoryPdf_h

#include <RooRealSumPdf.h>

#include <string>

class RooHistFactorySample;

class RooHistFactoryPdf : public RooRealSumPdf {
public:
RooHistFactoryPdf() = default;
RooHistFactoryPdf(const char *name, const char *title, RooArgList const &samples);
RooHistFactoryPdf(const RooHistFactoryPdf &other, const char *name = nullptr);
TObject *clone(const char *newname = nullptr) const override { return new RooHistFactoryPdf(*this, newname); }

std::size_t numSamples() const { return _funcList.size(); }
RooHistFactorySample const &sample(std::size_t i) const;
RooHistFactorySample const *sample(std::string const &sampleName) const;
RooArgList const &observables() const;

void printMetaArgs(std::ostream &os) const override;

ClassDefOverride(RooHistFactoryPdf, 1);
};

#endif
Loading
Loading