# SetFcn

**URL:** <https://root-forum.cern.ch/t/setfcn/12998>\
**Category:** ROOT\
**Created:** [August 29, 2011, 9:10am UTC](https://root-forum.cern.ch/t/setfcn/12998 "2011-08-29T09:10:18Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [August 29, 2011, 9:10am UTC](https://root-forum.cern.ch/t/setfcn/12998/1 "2011-08-29T09:10:18Z")

</div>

Is it possible to call SetFCN in PyROOT with python function as a parameter? In my code I have

```auto
TVirtualFitter.Fitter(g).SetFCN(twoPartParabFCN)
```

where g is a TGraph and twoPartParabFCN starts as:

```auto
def twoPartParabFCN(nPar, grad, fval, par, iflag):
```

but I receive the following error:

> [@](#):
>
> Traceback (most recent call last):  
> File “./frame\_offset”, line 73, in   
> TVirtualFitter.Fitter(g).SetFCN(twoPartParabFCN)  
> TypeError: none of the 2 overloaded methods succeeded. Full details:  
> void TFitter::SetFCN(void\* fcn) =\>  
> could not convert argument 1  
> void TFitter::SetFCN(void (_)(Int\_t&, Double\_t_, Double\_t&f, Double\_t\*, Int\_t) fcn) =\>  
> could not convert argument 1 (void/unknown arguments can’t be set)

Any help appreciated 🙂

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 29, 2011, 6:23pm UTC](https://root-forum.cern.ch/t/setfcn/12998/2 "2011-08-29T18:23:50Z")

</div>

Hi,

in order to pass python functions, a C++ callback needs to exist. Since C++ functions can not be created on the fly, these xyzFCN() functions that take them need to be individually “pythonized.” This is done for TMinuit and Fit::TFitter, but not for ::TFitter (::TVirtualFitter). So unless the underlying fitter is actually TMinuit (and available through gMinuit), this call won’t work at the moment.

Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [August 29, 2011, 6:28pm UTC](https://root-forum.cern.ch/t/setfcn/12998/3 "2011-08-29T18:28:04Z")

</div>

The underlying fitter is in this case TMinuit. However, gMinuit.SetFCN does not seem to take any effect - the FCN function is not called (does not print). Tomorrow I’ll send the code to which I do not have access at the moment of writing this…

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [August 30, 2011, 9:20am UTC](https://root-forum.cern.ch/t/setfcn/12998/4 "2011-08-30T09:20:11Z")

</div>

OK, I am not sure, how to gMinuit.SetFCN() for use with TGraph::Fit.

When I call

```auto
TVirtualFitter.SetDefaultFitter("Minuit")
print gMinuit
```

No proper object is in gMinuit in this case and gMinuit.SetFCN() cannot be called.

In case of:

```auto
TVirtualFitter.SetDefaultFitter("Minuit")
TVirtualFitter.Fitter(g)
```

There is an initialized object in gMinuit and gMinuit.SetFCN() can be called, but it does not affect g.Fit(“U”) (where g is a TGraph). So how to do this properly?

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 30, 2011, 5:18pm UTC](https://root-forum.cern.ch/t/setfcn/12998/5 "2011-08-30T17:18:47Z")

</div>

Hi,

can you send a runnable script? Thanks!

Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [August 31, 2011, 9:27am UTC](https://root-forum.cern.ch/t/setfcn/12998/6 "2011-08-31T09:27:47Z")

</div>

Here it goes. If you uncomment line with

```auto
#f = TVirtualFitter.Fitter(g, 5)
```

it does not display error, but does not use FCN. If the line is commented, SetFCN on gMinuit does not work.  
[test\_gminuit.py](https://root-forum.cern.ch/uploads/default/original/2X/f/f1d0967e04fd15b619b70dfe78fdbd34dd08b899.py) (800 Bytes)

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 31, 2011, 10:53pm UTC](https://root-forum.cern.ch/t/setfcn/12998/7 "2011-08-31T22:53:06Z")

</div>

Hi,

AFAICS the “g.Fit(a, “U”)” method at least works in that it calls twoPartParab.

For the method through gMinuit, the code underneath is actually using TMinuitFitter and not TMinuit. This uses gMinuit2, not gMinuit. Since the pythonized method is associated with the class, gMinuit2 won’t have it.

So, I’m out of options as to how to fix this. Another complication is that the TMinuitFitter is not made available in a dictionary, it’s base class TFitter is. Thus, I can not distinguish between a TFitter and a TMinuitFitter when pythonizing the class. I do not have enough knowledge of the TFumiliFitter to know whether it and the TMinuitFitter are interchangeable.

Anyway, I added a SetFCN to TFitter, which then works for TMinuitFitter, but you’ll need trunk for that. Best I can think of if g.Fit() does not suffice …

Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![alessio](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@alessio](https://root-forum.cern.ch/u/alessio)\
**Post date:** [September 1, 2011, 3:51pm UTC](https://root-forum.cern.ch/t/setfcn/12998/8 "2011-09-01T15:51:36Z")

</div>

I’ve a question about SetFCN and passing pointers (a.k.a.: difference between C++ and python).

If I want set a function for minimizing, I use

```auto
TheMinuit = TMinuit(2)
TheMinuit.SetFCN(myClass::myFunc)
```

In C++ the myFunc should be done in such way:

```auto
void myClass::myFunc(double& value, double* par) {
   double chi2 = 0.
   for (int i = 0; i < myClass::Xvalue.size(); i++) {
      fx = myClass::Xvalue[i]*par[1] + par[0]
      y = myClass::Yvalue[i]
      ey = myClass::YvalueError[i]
      chi2 += pow((y-fx)/ey, 2)
   }
   if (chi2 <1e30) value = chi2
   else value = 1e30
}
```

In python how work the passaging of the ‘value’ variable? I mean, ‘SetFCN’ WANT a void function, so I cannot write in python ‘return value’, is it? And ‘par’ should be a numpy.array(par), is it not?

Thanks for the help and clarifications…

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [September 1, 2011, 3:59pm UTC](https://root-forum.cern.ch/t/setfcn/12998/9 "2011-09-01T15:59:56Z")

</div>

Hi,

the signature you have in that C++ function does not appear to be correct? It should conform to this:

virtual void SetFCN(void (\*fcn)(Int\_t &, Double\_t \*, Double\_t &f, Double\_t \*, Int\_t));

Anyway, the python function equivalent takes buffers, so index it with 0 and assign to it. For example (from roottest):[code]def fcn( npar, gin, f, par, iflag ):  
global ncount  
nbins = 5

# calculate chisquare

chisq, delta = 0., 0.  
for i in range(nbins):  
delta = (z[i]-func(x[i],y[i],par))/errorz[i]  
chisq += delta\*delta

f[0] = chisq  
ncount += 1[/code]  
Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![alessio](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@alessio](https://root-forum.cern.ch/u/alessio)\
**Post date:** [September 2, 2011, 12:27pm UTC](https://root-forum.cern.ch/t/setfcn/12998/10 "2011-09-02T12:27:23Z")

</div>

Ok, Thank you!!!

P.S.: not necessary… there are 2 possible prototype:

```auto
virtual void SetFCN(void* fcn);
virtual void SetFCN(void (*fcn)(Int_t &, Double_t *, Double_t &f, Double_t *, Int_t));
```

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [September 2, 2011, 5:06pm UTC](https://root-forum.cern.ch/t/setfcn/12998/11 "2011-09-02T17:06:50Z")

</div>

Hi,

the first prototype is for CINT and the functions passed through should still conform to the same interface. This is the body of that SetFCN(void\*) function:` const char *funcname = gCint->Getp2f2funcname(fcn);
   if (funcname) {
      fMethodCall = new TMethodCall();
      fMethodCall->InitWithPrototype(funcname,"Int_t&,Double_t*,Double_t&,Double_t*,Int_t");
   }`  
and as you can see, it sets up the same parameters for the call, so yes, it too needs to conform.

Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [September 8, 2011, 4:03pm UTC](https://root-forum.cern.ch/t/setfcn/12998/12 "2011-09-08T16:03:29Z")

</div>

Well, SetFCN works, but… I cannot get any proper results. It seems like fval from the FCN is not properly passed to the fitter and is always 0 (according to the verbose output). I attach the script…  
[test\_gminuit.py](https://root-forum.cern.ch/uploads/default/original/2X/a/af7bacbd74678ef6ca15461efb6802d27fc9fce5.py) (956 Bytes)

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [September 8, 2011, 6:51pm UTC](https://root-forum.cern.ch/t/setfcn/12998/13 "2011-09-08T18:51:04Z")

</div>

Hi,

yes, of course if you code it like that: standard python “problem” that assignment to a variable merely moves the reference; it does not overwrite the value of the original object pointed to by the variable. Therefore, fval is actually made to be a buffer, so do:`fval[0] = chi2`and things should work in that the C++ side will receive the value.

Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [September 9, 2011, 7:47am UTC](https://root-forum.cern.ch/t/setfcn/12998/14 "2011-09-09T07:47:14Z")

</div>

Thanks, my adventure with python begun quite a short time ago… 🙂
