# Problem fitting a linear TF1 in a TH2

**URL:** <https://root-forum.cern.ch/t/problem-fitting-a-linear-tf1-in-a-th2/41153>\
**Category:** Newbie\
**Created:** [August 25, 2020, 10:35pm UTC](https://root-forum.cern.ch/t/problem-fitting-a-linear-tf1-in-a-th2/41153 "2020-08-25T22:35:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliangb](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/juliangb/32/21448_2.png) [@juliangb](https://root-forum.cern.ch/u/juliangb)\
**Post date:** [August 25, 2020, 10:35pm UTC](https://root-forum.cern.ch/t/problem-fitting-a-linear-tf1-in-a-th2/41153/1 "2020-08-25T22:35:22Z")

</div>

Hello,

I’m trying to fit multiple TH2 with a straight line, using the command TH2.Fit(“pol1”). While this works, I’m not sure that this is the right way to do it, since it fails when the tracks are completely vertical, as I show in the figure.

![VerticalTrackError](https://root-forum.cern.ch/uploads/default/original/3X/9/6/969b0fc6473c31a64c1b428ecc2dd4d3974e73f8.png)

The histogram is filled from a tree in an external file, I’m uploading a shorter version of this file along with a short version of my code that can be ran and that contains the fitting part.

Code: [DrawHistFit.cc](https://root-forum.cern.ch/uploads/short-url/gr3kYIAbXPh1o3bzPGzjdqGtNsh.cc) (6.2 KB)  
Makefile:[Makefile.c](https://root-forum.cern.ch/uploads/short-url/O2Xb3z19LLloAqkWtGEHpvUmOd.c) (709 Bytes)  
(NOTE: it wouldn’t let me upload the makefile without an extension, so I added the .c, but the original file has no extension)  
External file with tree: [TenTracks-ohdu2-run300-Zoomed.root](https://root-forum.cern.ch/uploads/short-url/mZzwvAj7zmSw3X1JNOkELp3nocR.root) (34.3 KB)

The fitting of the histogram starts at line 152.

I couldn’t find anything yet that migh solve this problem with vertical tracks, I’d appreciate if you could help me with this issue.

Thanks in advance,  
Julian.

* * *

ROOT Version: 6.14/04  
Platform: Ubuntu

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 26, 2020, 7:21am UTC](https://root-forum.cern.ch/t/problem-fitting-a-linear-tf1-in-a-th2/41153/2 "2020-08-26T07:21:55Z")

</div>

Hi,  
Unfortunately it will not be possible to fit vertical tracks since you cannot defined as a function `y = f(x)`, but it is instead a function `x=constant`. The easier solution is then rotate the histogram.  
What we could do is to add an option to `TH2::Fit` to fit the data as a function `x = f(y)` instead of `y=f(x)`

Lorenzo

---

<div class="post-metadata">

**Author:** ![juliangb](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/juliangb/32/21448_2.png) [@juliangb](https://root-forum.cern.ch/u/juliangb)\
**Post date:** [August 27, 2020, 8:47pm UTC](https://root-forum.cern.ch/t/problem-fitting-a-linear-tf1-in-a-th2/41153/3 "2020-08-27T20:47:50Z")

</div>

So it isn’t possible (yet) to fit with something like an implicit function (ax+by+c=0)? That’s too bad, because I also need to plot some other histograms like that one, but some of them are horizontal.

> [@moneta](#):
>
> What we could do is to add an option to `TH2::Fit` to fit the data as a function `x = f(y)` instead of `y=f(x)`

This would indeed help a lot in doing this

> [@moneta](#):
>
> The easier solution is then rotate the histogram.

How could I do that, in a way that I can later fit the ‘inverted’ histogram (x-\>y’, y-\>x’) with y’=f(x’)?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 31, 2020, 9:40am UTC](https://root-forum.cern.ch/t/problem-fitting-a-linear-tf1-in-a-th2/41153/4 "2020-08-31T09:40:06Z")

</div>

Hi,

if you are fitting an implicit function as `f(x,y)=0` to some data points (x,y), it is better you do something like in this tutorial, where you create yourself a least-square function to minimise,  
see [https://root.cern.ch/doc/master/fitCircle\_8C.html](https://root.cern.ch/doc/master/fitCircle_8C.html)

The fit of a TH2 histogram to a functions makes a least square function using the difference between the y bin centre and the function value `y = f(x)` , weighted by the y bin width divided by the bin error. This might also be not really appropriate in some cases.

I have created a JIRA item for adding anyway the option to fix a `x = f(y)` function, see  
[https://sft.its.cern.ch/jira/browse/ROOT-11024](https://sft.its.cern.ch/jira/browse/ROOT-11024)

Attached you can find some code example to rotate a 2-dimensional histogram (i.e. to swap the x-y axes)

Best regards

Lorenzo  
[RotateHistogram.C](https://root-forum.cern.ch/uploads/short-url/3ljEFha0vvTJe9ug8jWIotmrsGU.C) (810 Bytes)

---

<div class="post-metadata">

**Author:** ![juliangb](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/juliangb/32/21448_2.png) [@juliangb](https://root-forum.cern.ch/u/juliangb)\
**Post date:** [September 1, 2020, 11:30pm UTC](https://root-forum.cern.ch/t/problem-fitting-a-linear-tf1-in-a-th2/41153/5 "2020-09-01T23:30:19Z")

</div>

I’ll check both out, thanks for the help!

---

<div class="post-metadata">

**Author:** ![Raluca00123](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/raluca00123/32/29382_2.png) [@Raluca00123](https://root-forum.cern.ch/u/Raluca00123)\
**Post date:** [April 11, 2022, 12:21pm UTC](https://root-forum.cern.ch/t/problem-fitting-a-linear-tf1-in-a-th2/41153/6 "2022-04-11T12:21:10Z")

</div>

Hello!  
How did you manage to solve this? I have a similar problem with a fit for a graph.

Raluca
