Skip to content

Conversation

guitargeek
Copy link
Contributor

@guitargeek guitargeek commented Oct 3, 2025

This makes is possible to use Minuit2 directly from Python, as we can benefit from the std::function pythonization.

It also makes the Minuit2 code more consistent, because std::function was already used for the G2 (second derivatives) and the Hessian.

Here is an example of how the base Minuit 2 can be used from Python now:

import ROOT

# Define function to minimize
def fcn(params):
    x, y = params[0], params[1]
    return (x - 2.0)**2 + (y + 1.0)**2 + 1.0

# Wrap into Minuit2 FCNBase
func = ROOT.Minuit2.FCNAdapter(fcn)

# Initial values and step sizes as std::vector<double>
start_vals = ROOT.std.vector['double']([0.0, 0.0])
step_sizes = ROOT.std.vector['double']([0.1, 0.1])

# Create the minimizer
minimizer = ROOT.Minuit2.VariableMetricMinimizer()


state = ROOT.Minuit2.MnUserParameterState(start_vals, step_sizes)

# Run minimization directly
result = minimizer.Minimize(func, state)

# Inspect results
print("Minimization valid?", result.IsValid())
print("Minimum f =", result.Fval())
print("Best-fit parameters:")
for i in range(result.UserParameters().Params().size()):
    val = result.UserParameters().Params()[i]
    err = result.UserParameters().Errors()[i]
    print(f"  param[{i}] = {val} ± {err}")

This makes the base Minuit 2 code easily usable from Python, as promised in the ROOT plan of work. With "base Minuit 2 code", I mean the same classes that would also be built with Minuit 2 standalone, not using any other ROOT abstraction layers like the ROOT::Math::Minimizer interface, the ROOT::Math::Fitter or RooFit.

Copy link
Member

@hageboeck hageboeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but note that an entry in the release notes might be in order:

  • FCNGradAdapter.h is gone, so code might fail to compile
  • FCNAdapter<xxx> has to be converted to FCNAdapter

Copy link

github-actions bot commented Oct 3, 2025

Test Results

    22 files      22 suites   3d 20h 39m 24s ⏱️
 3 686 tests  3 686 ✅ 0 💤 0 ❌
79 196 runs  79 196 ✅ 0 💤 0 ❌

Results for commit 22b91bb.

♻️ This comment has been updated with latest results.

@guitargeek guitargeek force-pushed the minuit2_function branch 2 times, most recently from ab9c3d9 to 8a03ed8 Compare October 3, 2025 17:55
@guitargeek guitargeek requested a review from dpiparo as a code owner October 3, 2025 18:50
We should avoid leaking the ROOT Math classes into Minuit 2.
This commit proposes to use Minuit 2 directly via the `Minuit2Minimizer`
instead of the `ROOT::Math::Minimizer` abstraction.

The benefit is that we will be able to remove some interfaces in the
`ROOT::Math` classes that were only motivated by the control that RooFit
multiprocessing needs over Minuit 2, leaking implementation details.
The option to return external gradients in Minuit 2 internal space is
only used for Minuit 2, and these flags should not be present in math
function base classes. The same is true for the interface to calculate
the gradient considering a previous result.
The base classes `FCNBase` and `FCNGradientBase` were already unified in
71806b3, and the Minuit code can be further simplified by merging
also the derived adapter class for `std::function` objects.

This will also make it easier for the users, because then don't have to
use different classes depending on whether they pass external gradients
or not.

We also make the new `FCNAdapter` purely `std::function` based, instead
of using a template parameter. This makes is possible to use Minuit2
directly from Python, as we can benefit from the `std::function`
pythonization.

It also makes the Minuit2 code more consistent, because `std::function`
was already used for the G2 (second derivatives) and the Hessian.
@guitargeek guitargeek merged commit ee67000 into root-project:master Oct 4, 2025
27 checks passed
@guitargeek guitargeek deleted the minuit2_function branch October 4, 2025 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants