# Syntax of a free function (or C++ functor) for TGraph fitting

**URL:** <https://root-forum.cern.ch/t/syntax-of-a-free-function-or-c-functor-for-tgraph-fitting/22292>\
**Category:** ROOT\
**Created:** [December 1, 2016, 7:12am UTC](https://root-forum.cern.ch/t/syntax-of-a-free-function-or-c-functor-for-tgraph-fitting/22292 "2016-12-01T07:12:09Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Dusj](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e8c25b/32.png) [@Dusj](https://root-forum.cern.ch/u/Dusj)\
**Post date:** [December 2, 2016, 7:06am UTC](https://root-forum.cern.ch/t/syntax-of-a-free-function-or-c-functor-for-tgraph-fitting/22292/3 "2016-12-02T07:06:34Z")

</div>

Thanks a lot for the fast answer!  
I’ve switched to Root 6 (6.08.00), but with the following example

```auto
import ROOT
graph_1 = ROOT.TGraph(n, x_arr, y_arr) #arr created with the array module 

ROOT.gInterpreter.ProcessLine('''
struct MyFunction {
   MyFunction(TGraph & g) : fGraph(&g) {}
   double operator() (double *x, double *) { return fGraph->Eval(x[0]); }
   TGraph * fGraph;
};''')
myf = ROOT.MyFunction(graph_1)
ffree = ROOT.TF1("ffree", myf)
Integral = f1.Integral(0, 0.003)
```

I get a crash when calling f1.Integral (or also by saving the canvas with the drawn f1).  
Because of my limited C++ knowledge, I’ve tried to replace the x (and g) in the struct with x\_arr (graph\_1), but it also crashed. I think that something with MyFunction is wrong (i.e. it doesn’t return Eval to the TF1), but I don’t know how to fix it.  
I’ve attached the log of the crash.  
[LogCrash.txt](https://root-forum.cern.ch/uploads/default/original/2X/6/6d605c2bb7c091261cf35a4133d594aa590a6867.txt) (6.31 KB)

Alternatively, with Root6, one could use the lambda operator ([Integral of TGraph](https://root-forum.cern.ch/t/integral-of-tgraph/4745/1))

`TF1 f1("f",[&](double *x, double *){ return g.Eval(x[0]); },1,10,0);
double integral = f1.Integral(1,10);`  
, but I’m also clueless about how to call like this from python (I guess giving the python lambda doesn’t work).

---

_[View the full topic](https://root-forum.cern.ch/t/syntax-of-a-free-function-or-c-functor-for-tgraph-fitting/22292)._
