# About TF2 Integral

**URL:** <https://root-forum.cern.ch/t/about-tf2-integral/45423>\
**Category:** ROOT\
**Created:** [June 16, 2021, 8:55am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423 "2021-06-16T08:55:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![hurricane1127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@hurricane1127](https://root-forum.cern.ch/u/hurricane1127)\
**Post date:** [June 16, 2021, 8:55am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/1 "2021-06-16T08:55:44Z")

</div>

Hi, rooters,

I want to fit a 1d histogram by using TF1 g(x).  
But the g(x) is obtained by integral variable y for f(x,y) based on some physical process.

And The integration process is numerical integration. (The analytic integral is too complex to work out by Mathematica software)

I want to how to define my fit function, could you please provide an example for me?

Best regards,  
hurricane.

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 16, 2021, 9:03am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/2 "2021-06-16T09:03:10Z")

</div>

> [@hurricane1127](#):
>
> I want to how to define my fit function

There is a [section in the Manual](https://root.cern/manual/fitting/#fitting-1-d-histograms-with-user-defined-functions) explaining how to proceed.

---

<div class="post-metadata">

**Author:** ![hurricane1127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@hurricane1127](https://root-forum.cern.ch/u/hurricane1127)\
**Post date:** [June 16, 2021, 9:07am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/3 "2021-06-16T09:07:11Z")

</div>

Thanks for your reply. But i am still confused.  
What I want to know is how to obtain my fit function TF1 by integral TF2.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 16, 2021, 9:17am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/4 "2021-06-16T09:17:33Z")

</div>

In the fit function you define, you can use any complex C++ programming or access the data you need. Have you checked the ROOT Math libraries to see if some tools there answer your question ? And, at the end, if no predefined tools are satisfactory for you, you can program yourself the algorithm you need. May be @moneta has some ideas.

---

<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:** [June 16, 2021, 9:50am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/5 "2021-06-16T09:50:06Z")

</div>

Hi,  
There exists many example for this. A recent and simple one is this:

> [@Help with TF2 integral](https://root-forum.cern.ch/t/help-with-tf2-integral/45326/4):
>
> So, I’ve tried some functions. For instance, first I tried to compute Int\_0^2 dx Int\_0^2 dy x\*y using this code: double func2(const double \*x){ return x[0]\*x[1]; } void integral(){ ROOT::Math::Functor f2(& func2, 2); double a[2] = {0,2}; double b[2] = {1,2}; ROOT::Math::IntegratorMultiDim ig1(ROOT::Math::IntegrationMultiDim::kADAPTIVE); ig1.SetFunction(f2); double val = ig1.Integral(a,b); cout\<\<val\<\<endl; } It worked. Them, I tried to change the y integration limits to Blockquote…

---

<div class="post-metadata">

**Author:** ![hurricane1127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@hurricane1127](https://root-forum.cern.ch/u/hurricane1127)\
**Post date:** [June 17, 2021, 6:42am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/6 "2021-06-17T06:42:50Z")

</div>

Thank you very much .  
It is very helpful.

Best regards,  
hurricane

---

<div class="post-metadata">

**Author:** ![hurricane1127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@hurricane1127](https://root-forum.cern.ch/u/hurricane1127)\
**Post date:** [June 17, 2021, 6:44am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/7 "2021-06-17T06:44:07Z")

</div>

Thank you very much!  
The function Integral() may be helpful I guess.

Best regards,  
hurricane

---

<div class="post-metadata">

**Author:** ![hurricane1127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@hurricane1127](https://root-forum.cern.ch/u/hurricane1127)\
**Post date:** [June 17, 2021, 8:06am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/8 "2021-06-17T08:06:44Z")

</div>

Please help me to check my example:  
My fit function is g(x), which obtained by integral f(x,y) from y = -sqrt(1-x^2) to y = sqrt(1-x^2).  
And the f(x,y) = 3_x^2+2_y.

```auto
#include "TF2.h"
#include "TH2.h"
#include "TCutG.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TStyle.h"

Double_t f2(Double_t *x, Double_t *par) {
   return par[0]*(3*x[0]*x[0]+2*x[1]);
}

Double_t gx(Double_t *x, Double_t *par) {
   Double_t yrange[2] = {-TMath::Sqrt(1-x[0]*x[0]),TMath::Sqrt(1-x[0]*x[0])};
   return f2->Integral(yrange[0],yrange[1]);
}

TCanvas *fit2a() {
   TCanvas *c = new TCanvas();
   gStyle->SetOptStat(kTRUE);
   gStyle->SetPalette(57);
   const Int_t npar = 1;
   Double_t f2params[npar] = {2};
   auto f1 = new TF1("f1",gx,-1,1, npar);
   f1->SetParameters(f2params);

   //Create an histogram and fill it randomly with f2
   auto h1 = new TH1F("h1","From f1",40,-1,1);
   Int_t nentries = 100000;
   h1->FillRandom("f1",nentries);
   f1->SetParameters(f2params);
   h1->Fit("f1","N");
   h1->Draw();
   f1->Draw("same");
   return c;
}

```

But the result is:

 ![QQ图片20210617160521](https://root-forum.cern.ch/uploads/default/original/3X/1/9/1916c01a26b8ba62c84dd75160ecc093e31c1644.png)

Best regards,  
hurricane

---

<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:** [June 17, 2021, 8:31am UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/9 "2021-06-17T08:31:46Z")

</div>

Hi,  
You did not follow closely the example above. You need to create an internal TF1 object to define the f2(y) function. You can create for every call of gx(x) as in the example or , in case it is too time consuming, create as a global object or use a functor class and create in its constructor (cleaner and more efficient solution).  
I attach below the case where the TF1 object is created inside g(x) as in the cited example.

```auto
#include "TF2.h"
#include "TH2.h"
#include "TCutG.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TStyle.h"

Double_t f2(Double_t *x, Double_t *par) {
   return par[0]*(3*x[0]*x[0]+2*x[1]);
}

Double_t gx(Double_t *x, Double_t *par) {
   Double_t yrange[2] = {-TMath::Sqrt(1-x[0]*x[0]),TMath::Sqrt(1-x[0]*x[0])};
   TF1 func2("f2",f2,TMath::Infinity(),-TMath::Infinity(), 1);
   func2.SetParameter(0, par[0]);
   return func2.Integral(yrange[0],yrange[1]);
}

TCanvas *fit2a() {
   TCanvas *c = new TCanvas();
   gStyle->SetOptStat(kTRUE);
   gStyle->SetPalette(57);
   const Int_t npar = 1;
   Double_t f2params[npar] = {2};
   auto f1 = new TF1("f1",gx,-1,1, npar);
   f1->SetParameters(f2params);

   //Create an histogram and fill it randomly with f2
   auto h1 = new TH1F("h1","From f1",40,-1,1);
   Int_t nentries = 100000;
   h1->FillRandom("f1",nentries);
   f1->SetParameters(f2params);
   h1->Fit("f1","N");
   h1->Draw();
   f1->Draw("same");
   return c;
}

```

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![hurricane1127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@hurricane1127](https://root-forum.cern.ch/u/hurricane1127)\
**Post date:** [June 17, 2021, 1:26pm UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/11 "2021-06-17T13:26:22Z")

</div>

The result of this code is:

 ![1](https://root-forum.cern.ch/uploads/default/original/3X/d/7/d7a49aaa5f6f6e527f9fbc14ede529ecd5456571.gif)

But the g(x) = 6x.^2_sqrt(1-x_x), the right plot is:

![2](https://root-forum.cern.ch/uploads/default/original/3X/9/2/92bc738f38c52ec343d6a2110ecd1984eb1122fc.png)

So the integral process in ROOT may be wrong, but I can’t find the bug…

Best regards,  
hurricane

---

<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:** [June 17, 2021, 1:43pm UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/12 "2021-06-17T13:43:47Z")

</div>

Hi,  
you are right, the code above is wrong, since you want to compute an integral of f(x,y) in dy. You need in that case to define x as a parameter for ‘f2’. .Here is the corrected code:

```auto
#include "TF2.h"
#include "TH2.h"
#include "TCutG.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TStyle.h"

Double_t f2(Double_t *x, Double_t *par) {
   return par[0]*(3*par[1]*par[1]+2*x[0]);
}

Double_t gx(Double_t *x, Double_t *par) {
   Double_t yrange[2] = {-TMath::Sqrt(1-x[0]*x[0]),TMath::Sqrt(1-x[0]*x[0])};
   TF1 func2("f2",f2,TMath::Infinity(),-TMath::Infinity(), 2);
   func2.SetParameter(0, par[0]);
   func2.SetParameter(1, x[0]);
   return func2.Integral(yrange[0],yrange[1]);
}

TCanvas *fit2a() {
   TCanvas *c = new TCanvas();
   gStyle->SetOptStat(kTRUE);
   gStyle->SetPalette(57);
   const Int_t npar = 1;
   Double_t f2params[npar] = {2.};
   auto f1 = new TF1("f1",gx,-1,1, npar);
   f1->SetParameters(f2params);

   //Create an histogram and fill it randomly with f2
   auto h1 = new TH1F("h1","From f1",40,-1,1);
   Int_t nentries = 100000;
   h1->FillRandom("f1",nentries);
   f1->SetParameters(f2params);
   ROOT::Math::IntegratorOneDimOptions::SetDefaultIntegrator("Gauss");
   h1->Fit("f1","N");
   h1->Draw();
   f1->Draw("same");
   return c;
}

```

---

<div class="post-metadata">

**Author:** ![hurricane1127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@hurricane1127](https://root-forum.cern.ch/u/hurricane1127)\
**Post date:** [June 17, 2021, 2:19pm UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/13 "2021-06-17T14:19:41Z")

</div>

Thank you very much .  
The code works well and solved my problem.

Best regards,  
hurricane

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 1, 2021, 2:20pm UTC](https://root-forum.cern.ch/t/about-tf2-integral/45423/14 "2021-07-01T14:20:16Z")

</div>

Closed on July 1, 2021, 2:20pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
