Problem in fitting (in function!)

{
  TGraphErrors *g = new TGraphErrors("data.txt", "%lg %lg %lg");
  // g->Sort(); // just a precaution
  Int_t n = g->GetN();
  Double_t *x = new Double_t[(n + 1)];
  x[0] = (3.0 * g->GetX()[0] - g->GetX()[1]) / 2.0;
  x[n] = (3.0 * g->GetX()[(n - 1)] - g->GetX()[(n - 2)]) / 2.0;
  for (Int_t i = 1; i < n; i++)
    { x[i] = (g->GetX()[(i - 1)] + g->GetX()[i]) / 2.0; }
  TH1D *h = new TH1D("h", "spectrum;x-value;#counts", n, x);
  for (Int_t i = 0; i < n; i++) {
    h->SetBinContent(i + 1, g->GetY()[i]);
    h->SetBinError(i + 1, g->GetEY()[i]);
  }
  h->ResetStats(); // reset the statistics including the number of entries
  delete [] x; // no longer needed
  delete g; // no longer needed
  h->Draw();
}

Note that the histogram’s “bins’ centers” usually do not coincide with graph’s points “x coordinates”. One can easily see it if one uses:

  // delete g; // no longer needed
  h->Draw();
  g->SetMarkerStyle(20); g->Draw("P");
1 Like