Problem with fit


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


Hello,

I took some points generated by a signal generator (the voltage versus time has a sinusoidal trend) and I have to do the fit with root. When I try to run the script it doesn’t work. Can someone help me?
grafico.C (260 Bytes)

The fit should be performed on the graph using the TF1 as fitting function.

void grafico(){
   TGraphErrors *gl=new TGraphErrors("output.dat","%lg %lg");
   TF1 *retta0=new TF1("retta0","[0]*sin([2]*x+[1])+[3]",1.,1024.);
   retta0->SetParameters(0.24,1.,1.,1.);
   gl->Fit("retta0");
}

it doesn’t work, the fit gives me a flat line

Ok. I do not have the data file to test.

output.txt (14.4 KB)

i changed the format from data to txt.

I guess the initial values for the parameters are too far away from the final value. May be @moneta can help with that.

I made a mistake with the format of output.txt (14.4 KB)
data. Now the format is correct. In the first column there are the time measurement, in the second there are the voltage measurement.

I guess the initial parameters’ values are not correct.
@moneta can help.

{
  TGraph *gl = new TGraph("output.txt");
  // Double_t xmin = gl->GetX()[0], xmax = gl->GetX()[(gl->GetN() - 1)];
  Double_t xmin = gl->GetXaxis()->GetXmin(), xmax = gl->GetXaxis()->GetXmax();
  Double_t ymin = gl->GetYaxis()->GetXmin(), ymax = gl->GetYaxis()->GetXmax();
  TF1 *retta0 = new TF1("retta0", "[0]*sin([2]*x+[1])+[3]", xmin, xmax);
  retta0->SetParameters((ymax - ymin) / 2.0, // peak-to-peak amplitude * 0.5
                        TMath::Pi(), // initial phase (could also be just 0.)
                        2.0 * TMath::TwoPi() / (xmax - xmin), // approximately two full cycles
                        // 2.0 * TMath::TwoPi() / (retta0->GetXmax() - retta0->GetXmin()), // approximately two full cycles
                        (ymax + ymin) / 2.0); // mean
  gl->Fit("retta0");
  gl->Draw("AP");
}
1 Like

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