# Fitting problem with TGraph and TF1

**URL:** <https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444>\
**Category:** Newbie\
**Created:** [May 24, 2024, 11:29am UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444 "2024-05-24T11:29:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [May 24, 2024, 11:29am UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/1 "2024-05-24T11:29:20Z")

</div>

Hi all,

I couldn’t see the mistake . Please help me , why the code cannot do the fitting.

Cheers.

codes:

[Si\_estar.txt](https://root-forum.cern.ch/uploads/short-url/7Rj7q6CVsxPxjLcWbMDi8Clv0AS.txt) (6.4 KB)

[fittingcode3.cpp](https://root-forum.cern.ch/uploads/short-url/gO8FgXjBe8XEKx9Qoz9NCFYuLTl.cpp) (4.8 KB)

[source.h](https://root-forum.cern.ch/uploads/short-url/zWrB8JJ200xRVyFpwifrQZ9QUpp.h) (1.6 KB)

---

<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:** [May 24, 2024, 11:44am UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/2 "2024-05-24T11:44:58Z")

</div>

What you sent gives:

```auto
root [0] 
Processing fittingcode3.cpp...
In file included from input_line_8:1:
/Users/couet/Downloads/fittingcode3.cpp:30:10: fatal error: '.../Header.h' file not found
#include ".../Header.h"
         ^ ~~~~~~~~~~~~~
root [1] 

```

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [May 24, 2024, 12:46pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/3 "2024-05-24T12:46:54Z")

</div>

I changed the original one, let me recheck.  
Did you put your path for the header file!

#include “…/source.h”. inside the second file.

---

<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:** [May 24, 2024, 12:48pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/4 "2024-05-24T12:48:33Z")

</div>

I ran your example without any changes . Can you provide an example ready to run ?

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [May 24, 2024, 12:58pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/5 "2024-05-24T12:58:47Z")

</div>

[fittingcode3.cpp](https://root-forum.cern.ch/uploads/short-url/gO8FgXjBe8XEKx9Qoz9NCFYuLTl.cpp) (4.8 KB)

[source.h](https://root-forum.cern.ch/uploads/short-url/zWrB8JJ200xRVyFpwifrQZ9QUpp.h) (1.6 KB)

In fittingcode3.cpp file, you have to put your path. Updated file above!

---

<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:** [May 24, 2024, 1:00pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/6 "2024-05-24T13:00:56Z")

</div>

Still an other file is missing:

```auto
root [0] 
Processing fittingcode3.cpp...
Error in <TGraph::TGraph>: Cannot open file: /Users/spyhunter0066/Library/CloudStorage/OneDrive-harran.edu.tr/MacbookCplusplus/RangeCalculators/Fitting/Si_estar.txt, TGraph is Zombie

```

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [May 24, 2024, 1:02pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/7 "2024-05-24T13:02:52Z")

</div>

> [@couet](#):
>
> `: /Users/spyhun`

You are right, so sorry, the input file. I forgot.

[Si\_estar.txt](https://root-forum.cern.ch/uploads/short-url/7Rj7q6CVsxPxjLcWbMDi8Clv0AS.txt) (6.4 KB)

Result!

 ![Screenshot 2024-05-24 at 16.01.24](https://root-forum.cern.ch/uploads/default/original/3X/e/7/e757215c7e2dff27beb153d78e14675ebcd4bfd9.png)

---

<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:** [May 24, 2024, 1:31pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/8 "2024-05-24T13:31:23Z")

</div>

I tried to play a bit with your macro by changing the initial value of the parameter and also adding one parameter (an offset, as it seems your fit is to low on Y) but that didi not help. May be you function cannot fit these data ? lets ping @moneta . He may have more idea.

I simplified you macro to:

```auto
const double PI = 3.141592653589793;
const double Avogadro = 6.022E23; //atoms/mole
const double c_square = 931.5 ; //MeV/u
const double FourPiRsquare= 4.0* PI * (2.818*1.E-15) * (2.818*1.E-15); //m^2
const double m_electron = 0.00054858 ; //in akb, u; 9.11E-31; //kg

double A_Si = 28.0 ;
double rho_Si = 2.33*1.E3 ;
double N_Si = (rho_Si*Avogadro)/ (A_Si*1.0E-3);
double Z_Si = 14.0 ;
double MEP_Si = 172.*1.0E-6 ;

TF1* f_T_particle;
TF1* f_LorentzFactor_e ;
TF1* f_Beta_e;
TF1* f_STP_e;
TF1* f_STP_e_over_rho;

double MEP_medium = MEP_Si; // MEP_Calculator(Z_Si); //mean potential energy
double RME_calculator(double m_particle);
double T_particle(double *x,double *par);
double LorentzFactor_e(double *x,double *par);
double Beta_e(double *x, double *par);
double STP_e(double *x, double *par);
double STP_e_over_rho(double *x, double *par);
double RME_e = RME_calculator(m_electron); // rest mass energy

void fittingcode3() {
   auto c1 = new TCanvas("c1", "c1",294,78,1343,770);
   auto gr1 = new TGraph("Si_estar.txt"); //total STP (MeV. cm^2 /g)
   gr1->SetMarkerSize(0.6);
   gr1->SetMarkerStyle(20);
   gr1->SetMarkerColor(kRed);
   gr1->Draw("AP");
   gr1->GetXaxis()->SetRangeUser(1.e-2, 20.);
   gr1->GetYaxis()->SetRangeUser(1.e-2, 50.);

   double min_Limit= 1.e-2;
   double max_Limit= 20. ;
   f_T_particle = new TF1("f_T_particle", T_particle, min_Limit,max_Limit,0);
   f_LorentzFactor_e = new TF1("f_LorentzFactor_e", LorentzFactor_e, min_Limit,max_Limit,0 ); //between 1 and 10
   f_Beta_e = new TF1("f_Beta_e",Beta_e, min_Limit,max_Limit,0); //between 0 and 1
   f_STP_e = new TF1("f_STP_e", STP_e, min_Limit, max_Limit,0);
   f_STP_e_over_rho= new TF1("f_STP_e_over_rho", STP_e_over_rho, min_Limit, max_Limit,1);
   f_STP_e_over_rho->SetParameter(0, 0.);
// f_STP_e_over_rho->SetParameter(1, 0.);

   gr1->Fit(f_STP_e_over_rho, "MR");
}

double T_particle(double *x,double *par){
    return x[0];
}

double RME_calculator(double m_particle){
    double RME;
    RME= m_particle * c_square; //u* (MeV/u) = MeV
    return RME; //MeV
}

double LorentzFactor_e(double *x,double *par){
    double result1= ((x[0] / RME_e) +1.0);
    return result1;
}

double Beta_e(double *x, double *par){
    double result2= sqrt( (pow(pow(x[0]/RME_e,2.) +1. ,2.) -1. ) /(pow(pow(x[0]/RME_e,2.) +1. ,2.)) ) ;
    return result2;
}

double STP_e(double *x,double *par){ //Unit is MeV/m
    double result3 = FourPiRsquare* RME_e* N_Si * Z_Si*pow(1./f_Beta_e->Eval(x[0]),2)* (log(f_Beta_e->Eval(x[0])*f_LorentzFactor_e->Eval(x[0])*sqrt(f_LorentzFactor_e->Eval(x[0])-1.) /MEP_medium) +((1.0/(2.0*pow(f_LorentzFactor_e->Eval(x[0]),2.)))*((pow(f_LorentzFactor_e->Eval(x[0])-1., 2.)/8.0)+1.0-(log(2.)*(pow(f_LorentzFactor_e->Eval(x[0]),2.)+(2.*f_LorentzFactor_e->Eval(x[0]))-1.)))));
    return result3 ;
}

double STP_e_over_rho(double *x,double *par){ // (MeV .m^2/kg) * 10 = (MeV.cm^2 /g)
    double arg = 10. * (f_STP_e->Eval(x[0]) / rho_Si);
    printf("par[0] =%f\n",par[0]);
    return par[0] * arg ; //MeV * cm^2 /g
}

```

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [May 24, 2024, 1:36pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/9 "2024-05-24T13:36:16Z")

</div>

@moneta  
I suspected the initial parameter value as well. However, I couldn’t succeed. Function shape looks right. Database values on TGraph seems high. Functions are from the book.

I kept the parameter changing a bit. Interestingly, the fit didn’t move an inch! Why!

```
f_STP_e_over_rho->SetParameter(0, 30.);

```

 ![Screenshot 2024-05-24 at 21.56.44](https://root-forum.cern.ch/uploads/default/original/3X/9/4/94195a184df5b6b40151d461741148a66b6efddf.png)

 ![Screenshot 2024-05-24 at 21.58.29](https://root-forum.cern.ch/uploads/default/original/3X/5/1/51bcd34866047d16a7f637cc80216ce4a096caa8.png)

 ![Screenshot 2024-05-24 at 22.03.11](https://root-forum.cern.ch/uploads/default/original/3X/7/c/7c3e4fa7d477131fb2155012628388ccd27c4ee6.png)

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [May 25, 2024, 9:19pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/10 "2024-05-25T21:19:08Z")

</div>

So I spent a bit of time to understand where the error was.  
The error was in the function were you define the Beta of the electron from its kinetic energy  
What you have is

```auto
double result2= sqrt( (pow(pow(x[0]/RME_e,2.) +1. ,2.) -1. ) /(pow(pow(x[0]/RME_e,2.) +1. ,2.)) ) ;

```

what you want instead is

```auto
double result2= sqrt( (pow(x[0]/RME_e+1. ,2.) -1. ) /(pow(x[0]/RME_e +1. ,2.)) ) ;

```

Moreover in your final formula inside the logarithm you missed the electron rest mass, the change in the fit parameter is quite relevant it pass from 0.878 to 0.9987.

Below the result after of the fit after solving the issue with the beta factor

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/c/c/cc17987144309916269d50d0514cd59f7b96ee78.png)

PS:

The MEP\_medium of silicon that I found in the PDG is 173 eV and not 172 ,the effect of this parameter on the fit is very small.

I suggest you to rewrite the function for the stopping power in this way, you have less function and the readability of your code, according to me, is quite better.

```auto
double STP_e(double *x,double *par){ //Unit is MeV/m
    double T=x[0];
    double gamma=(T/RME_e) +1.;
    double beta = sqrt(1.-1./gamma/gamma);
      
    double result3 = FourPiRsquare* RME_e* N_Si * Z_Si * 1. /pow(beta,2) * ( log( beta*gamma*sqrt(gamma-1.)*RME_e/MEP_medium) + 1./2./pow(gamma,2) * ( pow(gamma-1,2.)/8. +1. -log(2.)*(pow(gamma,2) +2. *gamma -1.)));
    
    return result3 ;
}

```

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [May 26, 2024, 3:29pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/11 "2024-05-26T15:29:12Z")

</div>

Thanks. Since the formula is long, apparently I missed some of it.

@Dilicus : When I zoomed in, I noticed the function didn’t start from 0.01 MeV in my code. Why is that? Yours looks fine.

 ![Screenshot 2024-05-26 at 18.27.16](https://root-forum.cern.ch/uploads/default/original/3X/f/b/fb41313b41d49ec71a77845ed7cf715859e19630.png)  
[STP\_Fit.cpp](https://root-forum.cern.ch/uploads/short-url/kxpejdiKkiKHqFLGUzG5E1ifet9.cpp) (5.3 KB)

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [May 27, 2024, 6:33am UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/12 "2024-05-27T06:33:02Z")

</div>

I changed a bit the code, I set the number of points of the function to be 500 and I set the x minimum to 5e-3.

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [May 27, 2024, 5:44pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/13 "2024-05-27T17:44:58Z")

</div>

For instance, why it does not start from the the number I set up ? I wanted graph, and all functions to have the same range on X axis; however, it doesn’t seem like they do by looking at the line. We shouldn’t need to set it up like you did. They should all start from 1.e-2 as I told it so.

Another issue is when I try to zoom in the plot in TBrowser. It does not allow me to zoom in , for instance, between 0 and 2 on X axis. Why? I does not even let me do right click on my mouse to set the X range.

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [May 27, 2024, 8:46pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/14 "2024-05-27T20:46:41Z")

</div>

These are the code, I used I changed a bit few parameters like the MEP of silicon, the electron mass multiplied by c square is already in MeV.

I restored the limit of your function.

I do not know why you cannot zoom in TBrowser.

[fittingcode3.cpp](https://root-forum.cern.ch/uploads/short-url/hynhLzXzY8oT9MnXUimoNqfZen8.cpp) (4.6 KB)  
[source.h](https://root-forum.cern.ch/uploads/short-url/53B8cOTc4fYj4NQCr1t9TW5WB03.h) (1.6 KB)

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [May 28, 2024, 5:42pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/15 "2024-05-28T17:42:54Z")

</div>

Thanks all.

I realized " f\_STP\_e\_over\_rho-\>SetNpx(50000.); " is an option to widen the points in the fit for a function. Otherwise, it does not do it automatically.

I still have no idea why I cannot focus on TBrowser with my mouse click on X axis. Maybe a glitch.

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 4, 2024, 9:31am UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/17 "2024-06-04T09:31:11Z")

</div>

@Dilicus  
Is there any automated way to get the X axis range where the fit is perfect instead of me trying to change and find the range THE FIT is possible? Most of the time, I waste time when finding the initial values of Xrange where the fit works. It also misleads me most of the time by letting me think the code is wrong instead of the range I set initially is not suitable.

For instance, it cannot do the fit if I lower the X min limit close to zero, so I have to start somewhere close to 1.

 ![Screenshot 2024-06-04 at 12.33.09](https://root-forum.cern.ch/uploads/default/original/3X/d/5/d572742af49ed40bf1b81f11bee1ae8c93e941a0.png)

Cheers.

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [June 4, 2024, 9:50am UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/18 "2024-06-04T09:50:04Z")

</div>

I didn’t had the time to look in your code carefully yesteraday. But I think you were using a Bethe-Bloch. The range of application of the Bethe Bloch goes from \beta\gamma\sim0.05 to \beta\gamma\sim80. Given that your X axis is on kinetic energy, you should set the fit range according to the particle mass.

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 4, 2024, 12:49pm UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/19 "2024-06-04T12:49:16Z")

</div>

Not the original Bethe Bloch, but something similar.

I think I should check where the function is defined and restricted by the logarithmic term.  
dE/dx term should be positive at all times.

Formula References:

1. Evans, R. D., The Atomic Nucleus, McGraw-Hill, New York, 1955.
2. Roy, R. R. and Reed, R. D., Interactions of Photons and Leptons with Matter, Academic Press, New York, 1968.
3. Segré, E., Nuclei and Particles. W. A. Benjamin, New York, 1968.

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 5, 2024, 9:07am UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/20 "2024-06-05T09:07:30Z")

</div>

@Dilicus

double T\_particle(double \*x,double \*par){  
return x[0];  
}

double min\_Limit= 0. ;  
double max\_Limit= 1. ;

TF1\* f\_T\_particle = new TF1(“f\_T\_particle”, T\_particle, min\_Limit,max\_Limit,0);  
f\_T\_particle-\>SetNpx(1000.);  
int Numberx = f\_T\_particle-\>GetNpx();

Is there any way to create a loop to print out all the energies incremented 0.001 precision?  
double T[Numberx];  
for(int i=0; i \< Numberx; i++){  
T[i] = f\_T\_particle-\>Eval(i);  
}

Normally, the f\_T\_particle function should have infinite amount of real numbers on X axis, but we can set it up by using “SetNpx()” function. When I try to see what numbers on my X range, it gets blurry. I can only get integer numbers due to the limitation in for loop incrementation.

Can I see all the numbers in 0.001 increment between 0 and 1 for instance?

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [June 5, 2024, 9:27am UTC](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444/21 "2024-06-05T09:27:58Z")

</div>

Change just the for loop to

```auto
for(double i=0; i < 1; i+=0.001){
T[i] = f_T_particle->Eval(i);
}

```

or

```auto
for(int i=0; i < Numberx; i++){
T[i] = f_T_particle->Eval(double(i)/dobule(Numberx));
}

```

[Next page](https://root-forum.cern.ch/t/fitting-problem-with-tgraph-and-tf1/59444.md?page=2)
