Skip to content

Commit

Permalink
[RF] Disable RooFit::Evaluator in RooFuncWrapper NLL tests
Browse files Browse the repository at this point in the history
Disable the `RooFit::Evaluator` in the RooFuncWrapper NLL tests, such
that the generated code is used for the nominal likelihood too, and not
just to generate the gradient.

Like this, we also validate the nomial likelihood value from the
generated code.
  • Loading branch information
guitargeek committed Jun 27, 2024
1 parent a2ac7ec commit 3e06a9c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 4 additions & 1 deletion roofit/roofitcore/inc/RooFuncWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Experimental {
class RooFuncWrapper final : public RooAbsReal {
public:
RooFuncWrapper(const char *name, const char *title, RooAbsReal &obj, const RooAbsData *data = nullptr,
RooSimultaneous const *simPdf = nullptr, bool useEvaluator=false);
RooSimultaneous const *simPdf = nullptr, bool useEvaluator = false);

RooFuncWrapper(const RooFuncWrapper &other, const char *name = nullptr);

Expand All @@ -59,6 +59,8 @@ class RooFuncWrapper final : public RooAbsReal {

void createGradient();

void disableEvaluator() { _useEvaluator = false; }

protected:
double evaluate() const override;

Expand Down Expand Up @@ -89,6 +91,7 @@ class RooFuncWrapper final : public RooAbsReal {
Func _func;
Grad _grad;
bool _hasGradient = false;
bool _useEvaluator = false;
mutable std::vector<double> _gradientVarBuffer;
std::vector<double> _observables;
std::map<RooFit::Detail::DataKey, ObsInfo> _obsInfos;
Expand Down
6 changes: 3 additions & 3 deletions roofit/roofitcore/src/RooFuncWrapper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ namespace Experimental {

RooFuncWrapper::RooFuncWrapper(const char *name, const char *title, RooAbsReal &obj, const RooAbsData *data,
RooSimultaneous const *simPdf, bool useEvaluator)
: RooAbsReal{name, title}, _params{"!params", "List of parameters", this}
: RooAbsReal{name, title}, _params{"!params", "List of parameters", this}, _useEvaluator{useEvaluator}
{
if (useEvaluator) {
if (_useEvaluator) {
_absReal = std::make_unique<RooEvaluatorWrapper>(obj, const_cast<RooAbsData *>(data), false, "", simPdf, false);
}

Expand Down Expand Up @@ -194,7 +194,7 @@ void RooFuncWrapper::updateGradientVarBuffer() const

double RooFuncWrapper::evaluate() const
{
if (_absReal)
if (_useEvaluator)
return _absReal->getVal();
updateGradientVarBuffer();

Expand Down
7 changes: 5 additions & 2 deletions roofit/roofitcore/test/testRooFuncWrapper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ TEST_P(FactoryTest, NLLFit)
std::unique_ptr<RooAbsReal> nllRef = _params._createNLL(model, *data, ws, RooFit::EvalBackend::Cpu());
std::unique_ptr<RooAbsReal> nllFunc = _params._createNLL(model, *data, ws, RooFit::EvalBackend::Codegen());

// We don't use the RooFit::Evaluator for the nominal likelihood. Like this,
// we make sure to validate also the NLL values of the generated code.
static_cast<RooFit::Experimental::RooFuncWrapper &>(*nllFunc).disableEvaluator();

double tol = _params._fitResultTolerance;

EXPECT_NEAR(nllRef->getVal(observables), nllFunc->getVal(), tol);
Expand Down Expand Up @@ -585,8 +589,7 @@ FactoryTestParams param13{"RooFormulaVar",
/*randomizeParameters=*/false};

// Test for the uniform pdf. Since it doesn't depend on any parameters, we need
// to add it with some other model like a Gaussian to get a meaningful model to
// fit.
// to add it to some other model like a Gaussian to get a meaningful fit model.
FactoryTestParams param14{"Uniform",
[](RooWorkspace &ws) {
ws.factory("Gaussian::sig(x[0, 10], mean[5, -10, 10], sigma1[0.50, .01, 10])");
Expand Down

0 comments on commit 3e06a9c

Please sign in to comment.