# Linear interpolation TH2

**URL:** <https://root-forum.cern.ch/t/linear-interpolation-th2/42519>\
**Category:** Newbie\
**Created:** [November 30, 2020, 11:32am UTC](https://root-forum.cern.ch/t/linear-interpolation-th2/42519 "2020-11-30T11:32:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [November 30, 2020, 11:32am UTC](https://root-forum.cern.ch/t/linear-interpolation-th2/42519/1 "2020-11-30T11:32:51Z")

</div>

Hi,

I have TH2 plot showing a clear pattern, however, fitting a function doesn’t help to model it. is there a way to fit a linear interpolation that connects the mean values in the x-axis bins? Thanks!

---

<div class="post-metadata">

**Author:** ![oshadura](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oshadura/32/14460_2.png) [@oshadura](https://root-forum.cern.ch/u/oshadura)\
**Post date:** [November 30, 2020, 12:03pm UTC](https://root-forum.cern.ch/t/linear-interpolation-th2/42519/2 "2020-11-30T12:03:13Z")

</div>

@moneta could you please take a look or maybe suggest who can help here? Thank you!

---

<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:** [December 1, 2020, 3:48pm UTC](https://root-forum.cern.ch/t/linear-interpolation-th2/42519/3 "2020-12-01T15:48:47Z")

</div>

Hi,

You can use `TH2::Interpolate(x,y)` , which performs linear interpolation between bin center.  
See [https://root.cern/doc/master/classTH2.html#a49ae133dbce4e3aab95eed13c81577ff](https://root.cern/doc/master/classTH2.html#a49ae133dbce4e3aab95eed13c81577ff)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [December 2, 2020, 12:48am UTC](https://root-forum.cern.ch/t/linear-interpolation-th2/42519/4 "2020-12-02T00:48:47Z")

</div>

Hi Moneta, thank you very much for your help.

I was probably not very clear, but what I’m looking for is the y-value for a given x-value based on the mean in the x-axis bin

For example, the TH2 shows a linear dependence, but since it’s a TH2, there is not only one y-bin per x-bin, but rather a distribution in the x-slice. When I fit the TH2, I get what I want, which is a fit through the mean values of these distribution in the x-slice. However a linear fit is not good enough, so I wanted a piecewise function that basically connects the means in the x-slices with a linear interpolation.

At the end, the goal would be to have this piecewise function, which I can call to find the y-value given a x-value.

---

<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:** [December 2, 2020, 9:01am UTC](https://root-forum.cern.ch/t/linear-interpolation-th2/42519/5 "2020-12-02T09:01:05Z")

</div>

Look at `TH2::FitSlicesX()`.

> **[ROOT: TH2 Class Reference](https://root.cern/doc/master/classTH2.html#a9bfd1ea19dee46c25861d58ee60eaa3d)**

An example is [https://root.cern/doc/master/fitslicesy\_8C.html](https://root.cern/doc/master/fitslicesy_8C.html)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [December 3, 2020, 12:16am UTC](https://root-forum.cern.ch/t/linear-interpolation-th2/42519/6 "2020-12-03T00:16:40Z")

</div>

Hi Lorenzo, this is a function to fit the slices right? I want to fit the TH2, not the individual slices. (my apologies if I wasn’t clear)

---

<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:** [December 3, 2020, 1:55pm UTC](https://root-forum.cern.ch/t/linear-interpolation-th2/42519/7 "2020-12-03T13:55:05Z")

</div>

I am not sure it is very clear to me what you want.

> I’m looking for is the y-value for a given x-value based on the mean in the x-axis bin

What does it mean ? in a 2d histogram you have for a given x a set of ny bins. It is not clear. Do you want the weighted mean (weighted by the bin content) in y of the x slice ? This is similar to FitSlices the difference is that you do not fit , but you just take the mean value of the obtained histogram. You can code this staring from the code implementing FitSlicesX.

> I wanted a piecewise function that basically connects the means in the x-slices with a linear interpolation

This is again similar to FitSlicesX (see returned histograms for the fit parameters).  
From the obtained x-slice histogram you can get the mean and then create another histograms. From there you can use interpolation or whatever you like to build that function.

If I have misunderstood you, please let me know,  
Best regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [December 5, 2020, 12:31am UTC](https://root-forum.cern.ch/t/linear-interpolation-th2/42519/8 "2020-12-05T00:31:30Z")

</div>

Hi Lorenzo,

My apologies for the vague description of the problem. I think I understand better now what you mean, this code is indeed a good starting point! Many thanks for your help!!
