#include "TCanvas.h"
#include "TROOT.h"
#include "TGraphErrors.h"
#include "TF1.h"
#include "TFitResultPtr.h"
#include "TFitResult.h"
void plot()
{
gStyle->SetOptTitle(1);
gStyle->SetOptStat(111111);
gStyle->SetOptFit(1111);
gStyle->SetStatBorderSize(0);
gStyle->SetStatX(.89);
gStyle->SetStatY(.89);
auto c = new TCanvas();
TGraphErrors graph("./ed.txt","%lg %lg");
graph.SetTitle("Experimental Data;Energy(keV);Resolution");
graph.SetMarkerStyle(5);
graph.SetMarkerColor(kBlue);
graph.DrawClone("AP");
TF1 f("Power law","[0]*x**(-0.5)",0.,400.);
f.SetLineColor(kRed);
f.SetLineStyle(1);
f.SetLineWidth(2);
graph.Fit(&f);
f.DrawClone("Same");
TFitResult(&f);
TFitResult::Print();
c->Print("graph_with_law.pdf");
}
int main(){
plot();
}
The argument of Fit is the name of the function, i.e. “Power law” in your case.
graph.Fit(&f); graph.Draw("AP");
Thank you very much! The problem has been solved. But I do not understand it. Did you mean the function “Draw” can print the fit result? And what’s meaning of the parameter “Prob”?
Yes, see the plot you obtain.