Personal fit using ROOT

Hi, I’ve a couple of data file (x,y)

I wrote a macro…but I get error

In attachment data file (datafit.txt) and the macro (grafico.c)datifit.txt (358 Bytes)
grafico.c (2.0 KB)

_ROOT Version:5.34/36
_Platform:Windows 10

Before posting a question here, please always make sure that you have a “valid” C++ code using ACLiC:

root [0] .x grafico.cxx++

grafico.cxx (2.0 KB)

Hi thank you!
If I write the code that you wrote for ACLIC

.x grafico.cxx++

I get this error

But if I just run the macro

.x grafico.cxx++

it works!
Thank you!

Hi Wile… Due to the error that I get by ACLIC, isn’t the fit result correnct?

Hi. I’m sorry…I’ve just checked an error in fit function.

I got the fit result function doing:

root [0] .x grafico.cxx

Minimizer is Minuit / Migrad
Chi2                      =  0.000138667
NDf                       =           12
Edm                       =  3.74766e-06
NCalls                    =           51
a                         =     0.201923   +/-   0.0136011   
b                         =     0.270924   +/-   0.0124885   
c                         =       -1.664   +/-   0.0454012   
root [1] auto g = (TGraphErrors*)gPad->GetListOfPrimitives()->FindObject("Graph")
(TGraphErrors *) @0x7ffeef1424b8
root [2] g->GetListOfFunctions()->ls()
OBJ: TList	TList	Doubly linked list : 0
 OBJ: TF1	myfit	myfit : 0 at: 0x7fa82ac8fad0
 OBJ: TPaveStats	stats  	X1= 0.244500 Y1=0.000000 X2=0.347100 Y2=0.000000
root [3] auto f = (TF1*)g->GetListOfFunctions()->FindObject("myfit");
root [4] f->Draw()

I get the following plot . As you see it does not match your graph points. The fit did not converge.

Yes…they are values that I get…

In ROOT, before you try to fit your graph or histogram, you MUST set “reasonable” initial values for ALL parameters of your function (except for some “built-in” formulas, like “gaus”, for which the standard fit procedure can automatically “guess” them), otherwise the fitting procedure may easily misbehave.

If this doesn’t help, it may be that your function is not able to describe your data at all.

BTW. Because the “y” values of your graph are very small (of the order of 1e-10), the chi^2 that you get is extremely small (of the order of 1e-20) and this may additionally fool ROOT (I guess lowering the fitter’s “tolerance” / “precision” could help and maybe @moneta could comment on it).

1 Like

by the way I do not understand why you need a multigraph. I simplified you macro to this:

void grafico()
   char datagamma[200]= "datifit.txt";
   TCanvas *c36 = new TCanvas("c36","multigraph",1280,1024);

   float offx=1.1;
   float offy=1.5;
   float margr=0.08;
   float margl=0.12;

   gStyle->SetOptStat("nemr"); // "nemr" or "neMR"

   TGraphErrors *gamma = new TGraphErrors(datagamma,"%lg %lg");
   gamma->GetXaxis()->SetTitle("x (m)");
   gamma->GetYaxis()->SetTitle("y (m)");

   TF1 *myfit = new TF1("myfit", FunFit, 0., 1., 3);
   myfit->SetParNames("a", "b", "c");
   myfit->SetParameters(1., 1., 1.);

   //gamma->Fit(myfit); // Show nothing
   gamma->Fit("pol2");  // a simple "Pol2"works better

   TLegend* leg10 = new TLegend(0.7, 0.83, .6, .89);
   leg10->AddEntry(gamma, "Dati", "lp");

thank you to the both…I will ask to professor about the fit function …

ps. I used an old macro and there was the multigraph… this is the reason because I’ve the multigraph!

Ok, better cleaning it in that case it makes the macro much simple.

Try (note the fit “STATUS=NOT POSDEF” or “STATUS=FAILURE” message):
grafico.cxx (1.8 KB)
datifit.txt (461 Bytes)

Thank you to the both!

couet in your code I get error

Download both files from my previous post and put them in the same subdirectory in which you start your root executable.

Good morning…maybe I solved

  1. I forgot to transform one value in cm to m…then it changed the fit function
  2. I had to use y^2 values, not y value
  3. Transforming um to m, i did by excel this operation 2 times

Now it works! I got the fit!
Thank you for your help!

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