Hi, I have four data points and I would like to fit them but it does not work. Can you please have a look at it?
The code:
int Calibration()
{
const int points=4;
double channel[points]={610, 790, 1204, 1254};
double energy[points]={0.511, 0.6617, 1.274, 1.2491};
double energy_err[points]={0.0,0.0,0.0,0.0};
double channel_err[points]={188, 171, 222, 225};
gROOT->Reset();
TCanvas* c=new TCanvas("Calibration","Calibration",1700,1000);
gROOT->SetStyle("Plain");
gStyle->SetOptStat(0);
gStyle->SetOptFit(11);
gStyle->SetPadGridX(kTRUE);
gStyle->SetPadGridY(kTRUE);
c->Divide(1,1);
c->cd(1);
graph=new TGraphErrors(points, channel, energy, channel_err, energy_err);
graph->GetXaxis()->SetTitle("Channels");
graph->GetYaxis()->SetTitle("E / MeV");
graph->SetMarkerSize(2);
graph->SetMarkerColor(kBlue);
graph->SetMarkerStyle(20);
graph->SetTitle("Calibration");
//graph->GetXaxis()->SetRangeUser(391.5, 395);
//graph->GetYaxis()->SetRangeUser(140, 170);
//graph->GetXaxis()->SetNdivisions(5000,kTRUE);
TF1 *fit = new TF1("fit","([0]*x + [1])", 0, 1500);
fit->SetLineColor(kRed);
fit->SetLineStyle(2);
graph->Fit(fit);
graph->Draw("A*");
fit->Draw("SAME");
return 0;
}
Thank you in advance!