# Error calling Migrad in python

**URL:** <https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822>\
**Category:** Newbie\
**Tags:** python\
**Created:** [March 4, 2021, 8:02pm UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822 "2021-03-04T20:02:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![roodman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/9fc29f/32.png) [@roodman](https://root-forum.cern.ch/u/roodman)\
**Post date:** [March 4, 2021, 8:02pm UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822/1 "2021-03-04T20:02:57Z")

</div>

I am getting the following error:

```auto
TypeError: int TMinuit::Migrad() =>
    TypeError: callable was deleted

```

when calling:

```auto
import ROOT
gMinuit = ROOT.TMinuit(10)
# setup the function, etc...
gMinuit.Migrad()

```

This is with Root 6.22, and worked fine with Root 6.18 and previously for many years.

\_ROOT Version: 6.22  
\_Platform: Mac (MacPorts)  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [March 5, 2021, 9:04am UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822/2 "2021-03-05T09:04:57Z")

</div>

Hi @roodman,

Could you please send us a minimal code excerpt that we can use to reproduce the problem? Other than that, I am sure @moneta can help with this.

Cheers.

---

<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:** [March 5, 2021, 9:15am UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822/3 "2021-03-05T09:15:35Z")

</div>

Hello,  
I would recommend not to use TMinuit, but the `ROOT::Math::Minimizer` class. It should work fine in Python. A C++ example is here: [ROOT: tutorials/fit/NumericalMinimization.C File Reference](https://root.cern.ch/doc/master/NumericalMinimization_8C.html)

Lorenzo

---

<div class="post-metadata">

**Author:** ![roodman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/9fc29f/32.png) [@roodman](https://root-forum.cern.ch/u/roodman)\
**Post date:** [March 6, 2021, 7:38am UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822/4 "2021-03-06T07:38:46Z")

</div>

Thanks for the responses - with further searching around this forum, I found the issue. As of Root 6.22 the types of the arguments in a TMinuit FCN function changed from TROOT types to python ctypes.

I had:

> ```
> def chisq(self,npar, gin, f, par, iflag ):
> ... calculate stuff
> f[0] = chisquared
> 
> ```

but this needs to change to:

> ```
> f.value = chisquared
> 
> ```

Aaron

---

<div class="post-metadata">

**Author:** ![roodman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/9fc29f/32.png) [@roodman](https://root-forum.cern.ch/u/roodman)\
**Post date:** [May 17, 2021, 7:40pm UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822/5 "2021-05-17T19:40:32Z")

</div>

Hi again,

I am still having problems with this issue - calling a TMinuit function from within Python.

I can get this to work in a simple example, when the FCN is just defined as a standalone function in Python. But my actual code sets up the fit and the fit FCN inside a Python Class, and in that case I am still getting the

> TypeError: int TMinuit::Migrad() =\>TypeError: callable was deleted

error. I am hoping to find a solution that does not require me to rewrite a lot of my code, or change to do a different interface.

Attached I have two test scripts - testMigrad.py works fine, while testMigradC.py with the FCN in a class gives the error. I am running root 6.24.00 and python 3.8

Thanks very much for your help!

Aaron

[testMigrad.py](https://root-forum.cern.ch/uploads/short-url/wVq6zQF60OSWCSq6mJffZ6t5cKD.py) (785 Bytes)  
[testMigradC.py](https://root-forum.cern.ch/uploads/short-url/iEOwLGSQl0y2EaFh3vC9oc1CCnu.py) (1.2 KB)

---

<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:** [May 26, 2021, 4:07pm UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822/6 "2021-05-26T16:07:47Z")

</div>

Hi,  
Also in C++ you cannot pass class member functions to TMinuit. You would need to use th MInimizer class where you can pass a functor object as function to be minimized.

Lorenzo

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [May 27, 2021, 10:52am UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822/7 "2021-05-27T10:52:25Z")

</div>

Closed on May 27, 2021, 10:52am UTC



---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [May 27, 2021, 10:52am UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822/8 "2021-05-27T10:52:30Z")

</div>

Opened on May 27, 2021, 10:52am UTC



---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [June 10, 2021, 8:00am UTC](https://root-forum.cern.ch/t/error-calling-migrad-in-python/43822/9 "2021-06-10T08:00:42Z")

</div>

Closed on June 10, 2021, 8:00am UTC

This topic was automatically closed after 13 days. New replies are no longer allowed.
