Skip to content
Open
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
16 changes: 10 additions & 6 deletions math/mathcore/src/RootFinder.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ RootFinder::RootFinder(RootFinder::EType type) :
fSolver(nullptr)
{
// constructor passing type (default is kBRENT)
SetMethod(type);
const bool success = SetMethod(type);
if (!success)
throw std::runtime_error("ROOT::Math::RootFinder: ROOT was built without the requested algorithm");
}

bool RootFinder::SetMethod(RootFinder::EType type)
{
fSolver = nullptr;

// set method - Use the plug-in manager if method is implemented in MathMore
if ( type == RootFinder::kBRENT )
{
Expand Down Expand Up @@ -125,16 +129,16 @@ bool RootFinder::SetMethod(RootFinder::EType type)
return false;
}

fSolver = reinterpret_cast<ROOT::Math::IRootFinderMethod *>( h->ExecPlugin(0) );
assert(fSolver != nullptr);
fSolver = reinterpret_cast<ROOT::Math::IRootFinderMethod *>(h->ExecPlugin(0));
}
else {

#endif

if (!fSolver) {
MATH_ERROR_MSG("RootFinder::SetMethod","Error loading RootFinderMethod");
return false;
}

#endif

return true;
}

Expand Down
11 changes: 10 additions & 1 deletion roofit/roofitcore/src/RooGlobalFunc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,16 @@ RooCmdArg EventRange(Int_t nStart, Int_t nStop)

// RooAbsPdf::fitTo arguments

EvalBackend::EvalBackend(EvalBackend::Value value) : RooCmdArg{"EvalBackend", static_cast<int>(value)} {}
EvalBackend::EvalBackend(EvalBackend::Value value) : RooCmdArg{"EvalBackend", static_cast<int>(value)}
{
#ifndef ROOFIT_CLAD
if (value == Value::Codegen || value == Value::CodegenNoGrad) {
oocoutE(nullptr, InputArguments)
<< "RooFit was built without clad. Codegen backends are unavailable. Falling back to default.\n";
setInt(0, static_cast<int>(defaultValue()));
}
#endif
}
EvalBackend::EvalBackend(std::string const &name) : EvalBackend{toValue(name)} {}
EvalBackend::Value EvalBackend::toValue(std::string const &name)
{
Expand Down
14 changes: 8 additions & 6 deletions roofit/roofitcore/test/testRooMulti.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ TEST(RooMultiPdfTest, FitConvergesAndReturnsReasonableResult)

{
using namespace RooFit;
const EvalBackend backend{EvalBackend::defaultValue()};

RooRealVar x("x", "x", -10, 10);

Expand All @@ -78,7 +79,7 @@ TEST(RooMultiPdfTest, FitConvergesAndReturnsReasonableResult)

// Fit 1 - RooMultiPdf fit

std::unique_ptr<RooAbsReal> nll{multipdf.createNLL(*data, EvalBackend("codegen"))};
std::unique_ptr<RooAbsReal> nll{multipdf.createNLL(*data, backend)};

RooMinimizer minim{*nll};
minim.setStrategy(0);
Expand All @@ -92,7 +93,7 @@ TEST(RooMultiPdfTest, FitConvergesAndReturnsReasonableResult)
m1.setError(0.0);
s1.setError(0.0);
// Fit 2 - Reference fit
std::unique_ptr<RooAbsReal> nll1{gaus1.createNLL(*data, EvalBackend("codegen"))};
std::unique_ptr<RooAbsReal> nll1{gaus1.createNLL(*data, backend)};

RooMinimizer minim1{*nll1};
minim1.setStrategy(0);
Expand Down Expand Up @@ -122,10 +123,10 @@ TEST(RooMultiPdfTest, FitConvergesAndReturnsReasonableResult)

indx.setIndex(i);

std::unique_ptr<RooAbsReal> nll_multi{multipdf.createNLL(*data, EvalBackend("codegen"))};
std::unique_ptr<RooAbsReal> nll_multi{multipdf.createNLL(*data, backend)};

RooAbsPdf *selectedPdf = multipdf.getPdf(i);
std::unique_ptr<RooAbsReal> nll_direct{selectedPdf->createNLL(*data, EvalBackend("codegen"))};
std::unique_ptr<RooAbsReal> nll_direct{selectedPdf->createNLL(*data, backend)};

int n_param = countFloatingParametersIncludingObservable(*selectedPdf);

Expand All @@ -142,6 +143,7 @@ TEST(RooMultiPdfTest, FitConvergesAndReturnsReasonableResult)
TEST(RooMultiPdfTest, PenaltyTermIsAppliedCorrectly)
{
using namespace RooFit;
const EvalBackend backend = EvalBackend::defaultValue();

RooRealVar x("x", "x", -10, 10);

Expand All @@ -162,9 +164,9 @@ TEST(RooMultiPdfTest, PenaltyTermIsAppliedCorrectly)

std::unique_ptr<RooDataSet> data{gauss1.generate(x, 100)};

std::unique_ptr<RooAbsReal> nll_gauss1{gauss1.createNLL(*data, EvalBackend("codegen"))};
std::unique_ptr<RooAbsReal> nll_gauss1{gauss1.createNLL(*data, backend)};

std::unique_ptr<RooAbsReal> nll_multi{multiPdf.createNLL(*data, EvalBackend("codegen"))};
std::unique_ptr<RooAbsReal> nll_multi{multiPdf.createNLL(*data, backend)};

double val_gauss1 = nll_gauss1->getVal();
double val_multi = nll_multi->getVal();
Expand Down
40 changes: 18 additions & 22 deletions tutorials/math/exampleFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
##
## \author Lorenzo Moneta

import ROOT
import array

import ROOT

try:
import numpy as np
except:
print("Failed to import numpy.")
except Exception as e:
print("Failed to import numpy:", e)
exit()

## example 1D function
Expand Down Expand Up @@ -69,25 +71,19 @@ def RosenbrockFunction(xx):

def g(x): return 2 * x

gradFunc = ROOT.Math.GradFunctor1D(f, g)

#check if ROOT has mathmore
prevLevel = ROOT.gErrorIgnoreLevel
ROOT.gErrorIgnoreLevel=ROOT.kFatal
ret = ROOT.gSystem.Load("libMathMore")
ROOT.gErrorIgnoreLevel=prevLevel
if (ret < 0) :
print("ROOT has not Mathmore")
print("derivative value at x = 1", gradFunc.Derivative(1) )

else :
rf = ROOT.Math.RootFinder(ROOT.Math.RootFinder.kGSL_NEWTON)
rf.SetFunction(gradFunc, 3)
rf.Solve()
value = rf.Root()
print("Found root value x0 : f(x0) = 0 : ", value)
if (value != 1):
print("Error finding a ROOT of function f(x)=x^2-1")
# GSL_Newton is part of ROOT's MathMore library, which is not always active.
# Let's therefore run this in a try block:
try:
gradFunc = ROOT.Math.GradFunctor1D(f, g)
rf = ROOT.Math.RootFinder(ROOT.Math.RootFinder.kGSL_NEWTON)
rf.SetFunction(gradFunc, 3)
rf.Solve()
value = rf.Root()
print("Found root value x0 : f(x0) = 0 : ", value)
if value != 1:
print("Error finding a ROOT of function f(x)=x^2-1")
except Exception as e:
print(e)


print("\n\nUse GradFunctor for making a function object implementing f(x,y) and df(x,y)/dx and df(x,y)/dy")
Expand Down
6 changes: 3 additions & 3 deletions tutorials/roofit/roofit/rf619_discrete_profiling.C
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void rf619_discrete_profiling()

// Category 1 Pdf-s: Gaussian + Landau.
RooRealVar mean("mean", "shared mean", 25, 0, 50);
RooRealVar sigmaG("sigmaG", "Gaussian width", 2.0, 0.0, 5.0);
RooRealVar sigmaG("sigmaG", "Gaussian width", 2.0, 0.01, 5.0);
RooRealVar sigmaL("sigmaL", "Landau width", 3.0, 1.0, 8.0);

RooGaussian gauss1("gauss1", "Gaussian", x, mean, sigmaG);
Expand Down Expand Up @@ -118,8 +118,8 @@ void rf619_discrete_profiling()
data->add(vars);
}

// Create NLL with codegen and minimize it via the discrete profiling method.
std::unique_ptr<RooAbsReal> nll1(simPdf.createNLL(*data, EvalBackend("codegen")));
// Create an NLL and minimize it via the discrete profiling method.
std::unique_ptr<RooAbsReal> nll1(simPdf.createNLL(*data));
RooMinimizer minim(*nll1);

minim.setStrategy(1);
Expand Down
Loading