Is there a way to extract the best fit line in fitting a 2D histogram

I have plotted a 2D histogram and fitted it with a best-fit line, now I want to extract the best-fit lines and plot them separately.

Below is the example of the plot I have
brk1-18

Try:

TF1 *f = your_histogram->GetFunction("name_of_your_fit_function");
if (f) f->Draw(); else std::cout << "Function not found." << std::endl;

I have tried adding those two lines in the and duplicated them to produce for other 12 histograms but nothing seem to come up instead it deletes all the canvas i created, i dont know how to fix the problem. This is the marco i used:
basichist.c (9.4 KB)

It seems to me that you Fit("pol1") and you do not Fit("best-fit for Vi brick ...").

So i have to replace (“best-fit for Vo brick 3”… with “pol1” ?

I tried replacing the Fit("best-fit for Vo brick 3) with Fit(“pol1”) but I seem to be getting an error whereby the canvas is being deleted (see attached picture).
error

You always use: new TCanvas("c1", ...)

Yes I noticed that I did not change the numbering of the canvas, the numbering was the same

now the best-fit lines come out but they do not show the parameters, I tried using the
gStyle->SetOptFit(1011); but nothing is coming out

I do not have you data so there is 0 points found but your macro was not right. I tried to improve it. We would need the input data to investigate closer.
basichist.C (7.0 KB)

@couet Do you expect that the “statistics box” with the “fit parameters” is drawn when you simply draw the function alone?

Nope… I cleaned up the macro (it was far too complex) but I cannot see the result as the data are missing.Actually each plotting paragraph should be:

   TCanvas *c1 = new TCanvas("c1", "c1",600,400);
   h2->Fit("pol1"); // Draw the fit parameters
//   h2->Draw(); //<-- not needed
   c1->Update();
   TF1 *f2 = h2->GetFunction("best-fit for Vi for brick 1");
//   if (f2) f2->Draw(); else std::cout << "Function not found." << std::endl; // This line will overwrite the plot. remove it or use option "same"

Yes i would prefer to have a statistic box with fit parameters

kindly find the data attached 19-03-13.zip (1.3 MB)

It seems you sent the output file (root file)
According to your macro the input file is a txt file

   in.open(Form("%s19-03-20-2.txt",dir.Data()));;

from that marco you can just change that text file name to the one I sent you

in.open(Form("%s19-03-13.txt",dir.Data()));;

and it should be fine i think

I guess not … you send me a root fille. It cannot be open with in.open

here is a root file
19-03-20-2.root (1.1 MB)

Your macro needs the file 19-03-13.txt as input data and generates the root file as output. I am missing the file 19-03-13.txt … not the root file

19-03-13.zip (1.3 MB)
please uncompress it to get the actual text file 19-03-13.txt

basichist.C (6.4 KB)