# Hot to use TMinuit2

**URL:** <https://root-forum.cern.ch/t/hot-to-use-tminuit2/59670>\
**Category:** Newbie\
**Tags:** root\
**Created:** [June 18, 2024, 3:07am UTC](https://root-forum.cern.ch/t/hot-to-use-tminuit2/59670 "2024-06-18T03:07:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 18, 2024, 3:07am UTC](https://root-forum.cern.ch/t/hot-to-use-tminuit2/59670/1 "2024-06-18T03:07:07Z")

</div>

Dear experts

I want to use TMinuit2 to fit some straight tracks (I assume it’s better than TMinuit), but all legacy code I can get are using TMinuit instead of TMinuit2, so here I have two questions:

1. Is TMinuit2 better than TMinuit? (even only in some cases)
2. Is there any example for how to use TMinuit2 comparing with doing the same thing with TMinuit?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 18, 2024, 4:27am UTC](https://root-forum.cern.ch/t/hot-to-use-tminuit2/59670/2 "2024-06-18T04:27:24Z")

</div>

Hi,

Thanks for the post.  
Minuit served us very well for a long time. However, ROOT is transitioning to the Minuit2 minimizer, that builds on the experience of Minuit and, only because of that, aims to be better than that.  
I would perhps start by pointing you to [ROOT: Minuit2](https://root.cern.ch/doc/master/Minuit2Page.html)

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 19, 2024, 5:25am UTC](https://root-forum.cern.ch/t/hot-to-use-tminuit2/59670/3 "2024-06-19T05:25:00Z")

</div>

Thank you for pointing this page, I have read the page and there are two calls:

```auto
minimum->SetMaxFunctionCalls(1000000); //for Minuit/Minuit2
minimum->SetMaxIterations(10000); //for GSL

```

What are the difference of them? And Do I need to set the second one when using TMinuit2?

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 19, 2024, 6:02am UTC](https://root-forum.cern.ch/t/hot-to-use-tminuit2/59670/4 "2024-06-19T06:02:55Z")

</div>

And I have another question, in the Fitting function I need to use some extra information instead of fitting parameters, I made it a class and Fitting function is a member function so that it can access the member parameter in the class, but when write `ROOT::Math::Functor functionRT(&FittingFunctionRT, 4);` I got this:

```auto
error: must explicitly qualify name of member function when taking its address

```

then I added the namespace `ROOT::Math::Functor functionRT(&TrackFitMinimizer::FittingFunctionRT, 4);` I got this error:

```auto
Functor.h:121:14: error: called object type 'double (TrackFitMinimizer::*)(const double *)' is not a function or function pointer
      return fFunc(x);

```

What should I do?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 20, 2024, 6:29am UTC](https://root-forum.cern.ch/t/hot-to-use-tminuit2/59670/5 "2024-06-20T06:29:25Z")

</div>

Hi,

I think the error is not related to ROOT: perhaps one can have a look to pages like this one? [c++ - Function Pointer (Within Class) - Stack Overflow](https://stackoverflow.com/questions/30228452/function-pointer-within-class)

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 20, 2024, 12:03pm UTC](https://root-forum.cern.ch/t/hot-to-use-tminuit2/59670/6 "2024-06-20T12:03:26Z")

</div>

Thank you, I understood about the error, and can you please tell more details about the functions I mentioned before? What are the difference of them? And Do I need to set the second one when using TMinuit2?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [June 20, 2024, 3:38pm UTC](https://root-forum.cern.ch/t/hot-to-use-tminuit2/59670/7 "2024-06-20T15:38:29Z")

</div>

Hi,

For creating the Functor class from a non-static member function, you need to pass a pointer to the class itself, in order to get its data, and the member function pointer. Here is an example of using the Functor class from [Mathematical libraries - ROOT](https://root.cern/manual/math/)

```auto
#include "Math/Functor.h"

class MyFunction {
public:
    double operator()(const double *x) const
    {
        return x[0]+x[1];
    }
    double Eval(const double * x) const
    {
        return x[0]+x[1];
    }
};

double freeFunction(const double * x ) {
    return x[0]+x[1];
}

int main() {
    // Test directly calling the function object.
    MyFunction myf;

    // Test from a free function pointer.
    ROOT::Math::Functor f1(&freeFunction,2);

    // Test from function object.
    ROOT::Math::Functor f2(myf,2);

    // Test from a member function.
    ROOT::Math::Functor f3(&myf,&MyFunction::Eval,2);
    double x[] = {1,2};
    cout << f1(x) << endl;
    cout << f2(x) << endl;
    cout << f3(x) << endl;
    return 0;
}

```

Then for doing the minimization you should not use the `TMinuit2` class. This is a legacy and deprecated class, that has a signature similar to `TMinuit`. For minimising a function, you should use the minimiser interface, as described in this tutorial: [ROOT: tutorials/fit/NumericalMinimization.C File Reference](https://root.cern.ch/doc/master/NumericalMinimization_8C.html).

Concerning your previous question:

```auto
minimum->SetMaxFunctionCalls(1000000); //for Minuit/Minuit2
minimum->SetMaxIterations(10000); //for GSL

```

Minuit and Minuit2 do not have a limit on the maximum number of iterations but on the number of function calls. Normally, if not computing the Hessian but computing numerical derivatives you have something like:

```auto
number_of_function_calls = number_of_iterations * (number_of_parameters * 2 + 2)

```

Teh GSL minimiser instead can control the number of iterations. In general you can always use the default values and change them only if needed.

Lorenzo
