Hello,
I am wondering if it is possible to create one single legend for a graph with a fit function, where the legend contains the markers and the fit function line (with their respective descriptions) PLUS the fit parameters of the function.
I’ve tried creating a TLegend for the graph and the fit function and also setting gStyle->SetOptFit(1111) separately, but getting the two boxes to align nicely is a lot of inconvenient fiddling around, especially if the fonts should be the same size, the alignment should be nice etc. So I am wondering, is there a (more or less) straightforward way of including the fit parameters in the TLegend (or an alternative to TLegend that can accomplish this)?
This is my code at the moment:
{
TCanvas *myCanvas = new TCanvas("myCanvas","Window1", 1080,720);
myCanvas -> SetGrid();
TGraph *gr1 = new TGraph("data.txt");
gr1 -> SetTitle("Autocorrelation fit function for 138#pm 3 kcts/s;#Delta t [ns];Counts");
gr1 -> SetMarkerStyle(22);
gr1 -> SetMarkerSize(1);
gr1 -> SetMarkerColor(kBlue);
gr1 -> GetXaxis() -> SetLimits(0,2400);
TF1 *fitfunc = new TF1("fitfunc","[0]*(1-([1]+1)*exp(-[2]*abs(x-[3]))+[1]*exp(-[4]*abs(x-[3])))+[5]",0,2400);
fitfunc -> SetParameters(738,0.49,0.079,2269,0.0086,100);
fitfunc -> SetParNames("C_{1}","k","k_{1}","x_{0}","k_{2}","C_{0}");
gr1 -> Fit(fitfunc,"R0");
fitfunc -> SetLineColor(kBlue+2);
fitfunc -> SetLineWidth(4);
TLegend *leg = new TLegend(0.11,0.75,0.5,0.89);
leg -> AddEntry(gr1, "Measured counts", "P");
leg -> AddEntry(fitfunc, "Fit function", "L");
leg -> SetBorderSize(1);
// What follows is the option that I am currently using. It sort of gets everything to align on my screen, but seems rather inconvenient:
gStyle -> SetOptFit(1111);
gStyle -> SetStatBorderSize(1);
gStyle -> SetStatX(0.5);
gStyle -> SetStatY(0.75);
gStyle -> SetStatW(0.2165);
gr1 -> Draw("AP");
fitfunc -> Draw("same");
leg -> Draw("same");
}
My Idea was to include the fit parameters in the TLegend in some way like the following (replacing the entire gStyle section above):
for(int par=0;par<=5;par++){
leg -> AddEntry(fitfunc->GetParName(par), Form("%g",fitfunc->GetParameter(par))," ");
}
This displays the parameter values nicely; however it does NOT display the parameter names and I also don’t have any ideas on how I would add the errors.
Thanks in advance!
Joscha
ROOT Version: v6.20.04
Platform: Ubuntu 20.04
Compiler: Cling