Linear interpolation TH2

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!

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

Hi,

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

Lorenzo

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.

Look at TH2::FitSlicesX().

An example is https://root.cern/doc/master/fitslicesy_8C.html

Lorenzo

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)

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

1 Like

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!!