I can’t fit with TF2

I’m analyzing data representing the coordinates of a star over the year.
I write the code creating a graph with TGraphError. Then I want to fit my data with the generic conic equation but when I define the function for fitting data with TF2, then root say “unknown function”. How can I define it correctly?___
Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


In order to help you it would be noce to post a small piece of code reproducing your problem.

At the end i undertood that I had to fit with TF1 using a branch of an ellipse, but it didn’t fit yet.

Double_t f(Double_t *x, Double_t*par)
{
Double_t val =par[0]+par[1]*sqrt(par[2]-((1/par[3]*par[3])*pow(x[0]-par[4],2))) ;
return val;}

void myfunc(){


Double_t x[12]={128,129,130,133,134,135,134,135,133,131,129,123};
Double_t y[12]={111,110,111,112,114,119,122,123,126,128,129,122};
Double_t errx[12]={0,0,0,0,0,0,0,0,0,0,0,0};
Double_t erry[12]={0,0,0,0,0,0,0,0,0,0,0,0};

//create graph
TGraphErrors *graf = new TGraphErrors(12,x,y,errx,erry);
graf->SetTitle("Traiettoria S1");
graf->SetMarkerStyle(6);
graf->SetMarkerColor(kBlack);
graf->SetLineColor(kBlack);

//DEFINE USER FIT

TF1*f2=new TF1("f2","f",128,134,5);


f2->SetParameters(2,3,4,5,16,8);
f2->SetParName(0,"a");
f2->SetParName(1,"b");
f2->SetParName(2,"c");
f2->SetParName(3,"d");
f2->SetParName(4,"e");
f2->SetParName(5,"f");


f2->GetChisquare(); // Chiquadro
f2->GetNDF(); //Gradi di libertà
f2->GetParameter(0); //valore del parametro i-esimo
f2->GetParameter(1);
f2->GetParameter(2);
f2->GetParameter(3);
f2->GetParameter(4);
//f2->GetParameter(5);  

TCanvas *c1=new TCanvas ("c1","fk",200,10,400,800);
c1->SetLogy();
c1->SetGrid();
graf->GetXaxis()->SetTitle(" x (pixel)");
graf->GetYaxis()->SetTitle("y (pixel)");

graf->Fit("f2","R");
f2->SetLineColor(kRed); f2->SetLineStyle(2);
gStyle->SetOptFit(11111);
graf->Draw("APE");

TLegend *leg =new TLegend (.1,.7,.3,.9,"");
leg->SetFillColor(0);
leg->AddEntry(graf,"Punti");
leg->AddEntry(f2,"FIT");
leg->Draw("SAME");
}

Your “graf” (“y” versus “x”) cannot be represented by a “mathematical function”.

You can try:

{
  Double_t x[10] = {129,130,133,134,135,134,135,133,131,129};
  Double_t y[10] = {110,111,112,114,119,122,123,126,128,129};
  TGraph *g = new TGraph(10, y, x); // "x" versus "y"
  g->Sort(); // just a precaution
#if 0 /* 0 or 1 */
  g->Fit("pol4", "");
#else /* 0 or 1 */
  TF1 *f =
    new TF1("f",
            "[3]*sqrt((abs(x-[0])<[2])*(1.-pow((x-[0])/[2],2.)))+[1]",
            100., 140.);
  f->SetParNames("x0", "y0", "a", "b"); // ((x-x0)/a)^2 + ((y-y0)/b)^2 = 1
  // f->Print();
  f->SetParameters(120., 125., 10., 8.);
  g->Fit(f, "");
#endif /* 0 or 1 */
  g->Draw("A*");
}

It did’t fit yet

Run my “example macro” without any modifications.

If you want to use another fit function, make sure that it is really able to describe your data and make sure that you set “reasonable” initial values for all its parameters (before trying to fit).

See also:

Create a simple “g.cxx” file:

{ // g.cxx ...
  Double_t x[12] = {128,129,130,133,134,135,134,135,133,131,129,123};
  Double_t y[12] = {111,110,111,112,114,119,122,123,126,128,129,122};
  TGraph *g = new TGraph(12, x, y);
} // ... g.cxx

Then try:

[...]$ root
root [0] .x g.cxx
root [1] .x ${ROOTSYS}/tutorials/fit/fitEllipseTGraphRMM.cxx(g)
Minuit2Minimizer: Minimize with max-calls 1000000 convergence for edm < 0.001 strategy 1
Minuit2Minimizer : Valid minimum - status = 0
FVAL  = 0.0273867721707554204
Edm   = 4.24745121337942057e-08
Nfcn  = 118
x0	  = 128.941	 +/-  3.1625	(limited)
y0	  = 119.739	 +/-  4.51188	(limited)
a	  = 9.29333	 +/-  4.87759	(limited)
b	  = 6.01632	 +/-  3.71139	(limited)
theta	  = 96.1823	 +/-  70.454	(limited)
root [2] .x ${ROOTSYS}/tutorials/fit/fitEllipseTGraphDLSF.cxx(g)

x0 = 128.993
y0 = 119.701
a = 9.23337
b = 5.99821
theta = 95.5219

root [3] .q

You can also easily fix, e.g., the “theta” (the ellipse’s axes rotation angle) at, e.g., 90 degrees.
Edit the “fitEllipseTGraphRMM.cxx” file and replace:
m->SetVariable(4, "theta", theta, 1);
with:
m->SetFixedVariable(4, "theta", 90.);

Then try:

[...]$ root
root [0] .x g.cxx
root [1] .x fitEllipseTGraphRMM.cxx(g)
Minuit2Minimizer: Minimize with max-calls 1000000 convergence for edm < 0.001 strategy 1
Minuit2Minimizer : Valid minimum - status = 0
FVAL  = 0.0338409401330741905
Edm   = 7.33420454037152048e-07
Nfcn  = 76
x0	  = 128.876	 +/-  3.21928	(limited)
y0	  = 119.519	 +/-  3.62183	(limited)
a	  = 9.16578	 +/-  4.18785	(limited)
b	  = 6.12493	 +/-  3.68467	(limited)
theta	  = 90	 (fixed)
root [2] .q