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

**URL:** <https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605>\
**Category:** Newbie\
**Created:** [April 15, 2019, 8:17am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605 "2019-04-15T08:17:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 15, 2019, 8:17am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/1 "2019-04-15T08:17:22Z")

</div>

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](https://root-forum.cern.ch/uploads/default/original/3X/1/9/19d96ebf01638d0849fcf826850b43ea10e8bb23.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 15, 2019, 8:24am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/2 "2019-04-15T08:24:39Z")

</div>

Try:

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

```

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 15, 2019, 1:48pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/3 "2019-04-15T13:48:14Z")

</div>

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](https://root-forum.cern.ch/uploads/default/original/3X/5/3/53a0ee4aef1315473c62a5190706bcc7b7ae39ab.c) (9.4 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 15, 2019, 2:33pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/4 "2019-04-15T14:33:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 15, 2019, 3:02pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/5 "2019-04-15T15:02:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 15, 2019, 3:08pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/6 "2019-04-15T15:08:06Z")

</div>

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](https://root-forum.cern.ch/uploads/default/original/3X/3/2/32016175d25299fcad145f3ab23a7a95f50a411b.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 15, 2019, 3:18pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/7 "2019-04-15T15:18:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 15, 2019, 3:24pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/8 "2019-04-15T15:24:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 16, 2019, 7:18am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/9 "2019-04-16T07:18:31Z")

</div>

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](https://root-forum.cern.ch/uploads/default/original/3X/a/3/a3c36368ddd27f21958c50683a128bb9a520b917.C) (7.0 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 16, 2019, 7:52am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/10 "2019-04-16T07:52:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 16, 2019, 8:00am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/11 "2019-04-16T08:00:43Z")

</div>

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:

```auto
   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"

```

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 16, 2019, 8:39am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/13 "2019-04-16T08:39:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 16, 2019, 9:21am UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/14 "2019-04-16T09:21:11Z")

</div>

kindly find the data attached [19-03-13.zip](https://root-forum.cern.ch/uploads/default/original/3X/e/4/e46e19fb8d8d1ed6bf9d3e4fec71ab5ef3b504db.zip) (1.3 MB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 16, 2019, 12:09pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/15 "2019-04-16T12:09:33Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 16, 2019, 12:16pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/16 "2019-04-16T12:16:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 16, 2019, 12:32pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/17 "2019-04-16T12:32:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 16, 2019, 12:56pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/18 "2019-04-16T12:56:47Z")

</div>

here is a root file  
[19-03-20-2.root](https://root-forum.cern.ch/uploads/default/original/3X/d/c/dc3a43b36a266da592be78f40bb610653783afb5.root) (1.1 MB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 16, 2019, 12:58pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/19 "2019-04-16T12:58:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 16, 2019, 1:06pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/20 "2019-04-16T13:06:25Z")

</div>

[19-03-13.zip](https://root-forum.cern.ch/uploads/default/original/3X/e/4/e46e19fb8d8d1ed6bf9d3e4fec71ab5ef3b504db.zip) (1.3 MB)  
please uncompress it to get the actual text file 19-03-13.txt

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 16, 2019, 1:31pm UTC](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605/21 "2019-04-16T13:31:13Z")

</div>

[basichist.C](https://root-forum.cern.ch/uploads/default/original/3X/7/4/74070821dbfe9aef224ba0f1deaea52a126a821b.C) (6.4 KB)

[Next page](https://root-forum.cern.ch/t/is-there-a-way-to-extract-the-best-fit-line-in-fitting-a-2d-histogram/33605.md?page=2)
