# 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:** 6\
**Page:** 1

<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 1, 2016, 7:12am UTC](https://root-forum.cern.ch/t/syntax-of-a-free-function-or-c-functor-for-tgraph-fitting/22292/1 "2016-12-01T07:12:09Z")

</div>

Hi there,

I’m currently trying to integrate the enclosed area of a TGraph and the x-axis in a certain x-range (I’ve attached a pdf showing it).  
As I’ve figured out now, TGraph.Integral() is not the correct method for that.

In another thread, Lorenzo has suggested to use a free function or a C++ functor to fit the TGraph (and then just integrate the fit function). [Locate like command](https://root-forum.cern.ch/t/locate-like-command/65/1)  
However, I’m clueless about how I can implement the free function, such that it works from pyroot.  
The C++ syntax by Lorenzo is

`struct MyFunction {
   MyFunction(TGraph & g) : fGraph(&g) {}
   double operator() (double *x, double *) { return fGraph->Eval(x[0]); }
   TGraph * fGraph;
};`  
and in principal, I’d like to call it like this:

```auto
Graph = ROOT.TGraph(n, xarr, yarr)

# Declaration of MyFunction here

f1 = ROOT.TF1("f1", )
Graph.Fit("f1")
Integral = f1.Integral(xlow, xhigh)
```

The troubling point where I’m stuck now, is that it’s not a function but a general function object.

Does anybody have an idea? Thanks a lot!  
I’m using root version 5.34/32.  
[TGraphAlphaToProb.pdf](https://root-forum.cern.ch/uploads/default/original/2X/e/eac85c0dc499e2a64df326201865c6de531723f4.pdf) (16.2 KB)

---

<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:** [December 1, 2016, 9:20pm UTC](https://root-forum.cern.ch/t/syntax-of-a-free-function-or-c-functor-for-tgraph-fitting/22292/2 "2016-12-01T21:20:43Z")

</div>

Hi,

you need to provide the dictionary for MyFunction, selecting it with a linkdef file, creating a dictionary and compiling the library.  
Alternatively, you can move to ROOT6 and JIT the function and access it directly from within Python, like this:

```auto
import ROOT
ROOT.gInterpreter.ProcessLine('''
struct MyFunction {
   MyFunction(TGraph & g) : fGraph(&g) {}
   double operator() (double *x, double *) { return fGraph->Eval(x[0]); }
   TGraph * fGraph;
};
''')
# suppose your graph is called "mygraph"
myf = ROOT.MyFunction(mygraph)
f1 = ROOT.TF1("f1", myf)
[...]
```

Cheers,  
D

---

<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).

---

<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:** [December 3, 2016, 7:16am UTC](https://root-forum.cern.ch/t/syntax-of-a-free-function-or-c-functor-for-tgraph-fitting/22292/4 "2016-12-03T07:16:00Z")

</div>

Hi,

thanks for moving to 6.08: this is a modern release, I am sure you’ll find the enormous set of new features very useful to boost your research.  
I corrected a typo and remanipulated the script: this works.

```auto
import ROOT
graph_1 = ROOT.TGraph(2)
graph_1.SetPoint(0,0,0)
graph_1.SetPoint(0,1,1)

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,0,10,1) # <- note the 1
Integral = ffree.Integral(0, 1)
print Integral
```

Cheers,  
D

---

<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 6, 2016, 5:24am UTC](https://root-forum.cern.ch/t/syntax-of-a-free-function-or-c-functor-for-tgraph-fitting/22292/5 "2016-12-06T05:24:46Z")

</div>

Thanks a lot for developing ROOT and also for providing excellent support!

The arguments in the TF1 fixed it, now it works like a charm 😃

---

<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:** [December 6, 2016, 7:02am UTC](https://root-forum.cern.ch/t/syntax-of-a-free-function-or-c-functor-for-tgraph-fitting/22292/6 "2016-12-06T07:02:02Z")

</div>

Hi,

great that you could move forward!  
We’ll try to make the operation you carried out even easier from the next release.

Cheers,  
Danilo
