# Define FCN for Minuit that require parameters from main

**URL:** <https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955>\
**Category:** Newbie\
**Created:** [February 13, 2020, 9:26am UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955 "2020-02-13T09:26:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlehuraux](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@mlehuraux](https://root-forum.cern.ch/u/mlehuraux)\
**Post date:** [February 13, 2020, 9:26am UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/1 "2020-02-13T09:26:02Z")

</div>

Hello everyone,

My question is maybe more C++/code structure oriented but I wonder if some of you encountered the same difficulty and might be able to help me here !

I would like to define a fcn for Minuit to minimize, I understood this has to be a void.  
However, to compute the quantity I want to minimize, I need data that I read from ROOT files within my main function. How could I define my fcn then ?

That must be an easy one but if you have any advice that would be highly appreciated 😊

Cheers !

---

<div class="post-metadata">

**Author:** ![oshadura](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oshadura/32/14460_2.png) [@oshadura](https://root-forum.cern.ch/u/oshadura)\
**Post date:** [February 13, 2020, 11:26am UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/2 "2020-02-13T11:26:54Z")

</div>

@moneta can you help here please?

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [February 13, 2020, 12:10pm UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/3 "2020-02-13T12:10:06Z")

</div>

Hi  
the simplest way is to make the params global.  
For example look at:  
$ROOTSYS/ tutorials/fit/fit2dHist.C

Otto

---

<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:** [February 13, 2020, 1:39pm UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/4 "2020-02-13T13:39:04Z")

</div>

Hi,

You can use the Minimizer interface of Minuit , `ROOT::Math::Minimizer` class. In that case you need to pass to Minuit a functor (a C++ class implementing the operator() ) and you can then use all the parameters you want as data member of the class.

See the tutorial fit/NumericalMinimization.C  
[https://root.cern.ch/doc/master/NumericalMinimization\_8C.html](https://root.cern.ch/doc/master/NumericalMinimization_8C.html)

Lorenzo

---

<div class="post-metadata">

**Author:** ![mlehuraux](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@mlehuraux](https://root-forum.cern.ch/u/mlehuraux)\
**Post date:** [February 14, 2020, 8:19am UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/5 "2020-02-14T08:19:40Z")

</div>

Great, thanks a lot for your help @moneta I’ll try that !

---

<div class="post-metadata">

**Author:** ![mlehuraux](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@mlehuraux](https://root-forum.cern.ch/u/mlehuraux)\
**Post date:** [February 17, 2020, 8:40am UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/6 "2020-02-17T08:40:11Z")

</div>

Hi @moneta,

I’ve tried to implement things following your example. However I’m still getting an error 😬

```auto
 error: cannot convert ‘Double_t {aka double}’ to ‘Double_t* {aka double*}’ for argument ‘1’ to ‘Double_t chi2_1D(Double_t*, Double_t, TH1D*, TF1*)’
                     ROOT::Math::Functor f(&chi2_1D(Apeak[i], slice[i], PadResponseFunction), 1);
                                                                                           ^

```

Compared to your example, my functor is also f and your RosenBrock is my chi2\_1D.  
The only difference I can see is that, to compute my chi2\_1D, I need values from my ROOT file so when I compute it I passed TH1\* and TF1\* etc as arguments _(see below)_.

```auto
Double_t chi2_1D(Double_t *ytrack, Double_t A_peak, TH1D* slice, TF1* PadResponseFunction)
{
    Double_t chi2 = 0;
    for (int j = 0; j < geom::nPady; j++){
        chi2 += pow(1/slice->GetBinContent(j) - (A_peak*PadResponseFunction->Eval(j+0.5 - ytrack[0]))/pow(slice->GetBinContent(j), 2), 2);
    }
    return(chi2);
}

```

Here is the part of my code in the main where I define and use the Minimizer.

```auto

                    // Minimize chi2 of each line to extract ytrack (https://root.cern.ch/doc/master/NumericalMinimization_8C.html)
                    ROOT::Math::Minimizer* minimum = ROOT::Math::Factory::CreateMinimizer("Minuit2", "Migrad");
                    minimum->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2
                    minimum->SetMaxIterations(10000); // for GSL
                    minimum->SetTolerance(0.001);
                    minimum->SetPrintLevel(1);

                    // create function wrapper for minimizer
                    // a IMultiGenFunction type
                    ROOT::Math::Functor f(&chi2_1D(Apeak[i], slice[i], PadResponseFunction), 1);
                    Double_t step = 0.01;
                    // starting point
                    Double_t variable = slice[i]->GetMean();
                    minimum->SetFunction(f);
                    // Set the free variables to be minimized !
                    minimum->SetVariable(0,"ytrack",variable, step);
                    // do the minimization
                    minimum->Minimize();
                    ytrack[i] = minimum->X()[0];
                    myChi2->AddBinContent(it, minimum->MinValue());

```

Do you have any indication on how to fix the error ? Or a workaround ?  
Thanks a lot for your help,

Cheers

Marion

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [February 17, 2020, 9:23am UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/7 "2020-02-17T09:23:04Z")

</div>

Hi Marion,  
the 1. arg of your function is a pointer to double:

```auto
Double_t chi2_1D(Double_t *ytrack

```

you pass a double: (I guess APeak[i] is a double?)

```auto
ROOT::Math::Functor f(&chi2_1D(Apeak[i],

Otto

```

---

<div class="post-metadata">

**Author:** ![mlehuraux](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@mlehuraux](https://root-forum.cern.ch/u/mlehuraux)\
**Post date:** [February 17, 2020, 10:09am UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/8 "2020-02-17T10:09:58Z")

</div>

Hi Otto,

Thanks for the quick answer.  
This raises for me another question then !

In the example I followed, the first argument is a pointer to a const double :

```auto
double RosenBrock(const double *xx )
{
  const Double_t x = xx[0];
  const Double_t y = xx[1];
  const Double_t tmp1 = y-x*x;
  const Double_t tmp2 = 1-x;
  return 100*tmp1*tmp1+tmp2*tmp2;
}

```

But when calling the function, no argument is specified

```auto
ROOT::Math::Functor f(&RosenBrock,2);

```

which I thought is because it’s the variable. That’s why I didn’t put the ytrack argument, because in my case it’s the variable.

So, can you clarify in which case the argument shouldn’t be specified ann in which case it’s required ?

Sorry if that’s a stupid question…

Best wishes,

Marion

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [February 17, 2020, 12:45pm UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/9 "2020-02-17T12:45:56Z")

</div>

Hi Marion,  
sorry I didnt look carefully at your code snippet only at the  
compiler error.  
Your use of

```auto
ROOT::Math::Functor f(&chi2_1D(Apeak[i], slice[i], PadResponseFunction), 1);

```

is not correct.  
The ctor of Functor takes only 2 args:  
The address of the function and the dimension of the parameter array xx

```auto
double RosenBrock(const double *xx )

```

If you need extra data you should look at:  
$ROOTSYS/tutorials/fit/line3Dfit.C  
which use a function object, that is a class overwriting the () operator  
or  
$ROOTSYS/tutorials/fit/fitCircle.C  
which uses a Lambda function.

Lorenzo @moneta please correct me if I am wrong

Otto

---

<div class="post-metadata">

**Author:** ![mlehuraux](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@mlehuraux](https://root-forum.cern.ch/u/mlehuraux)\
**Post date:** [February 17, 2020, 1:51pm UTC](https://root-forum.cern.ch/t/define-fcn-for-minuit-that-require-parameters-from-main/37955/10 "2020-02-17T13:51:03Z")

</div>

Hi Otto,

No problem, OK thanks a lot for the explanation I will have a look at those tutorials/examples and try again !

Thanks for the help 😁
