Making a Linear Interpolation for TH2


I have a Profile histogram (attached) which I would like to make a linear interpolation for.

Is there anyway to make a linear interpolation for a profile histogram? If I could see any type of example that would be very helpful!



ROOT Version: 6.30/02
Platform: Ubuntu 22.04.3 LTS
Compiler: Not Provided

Your profile histogram looks great! I think that, for linear interpolation in ROOT, you could use the TGraphErrors class to create a graph from your TProfile. Then, TGraphErrors::Interpolate can be used for linear interpolation at any point.

Here’s a quick example:

// Assuming 'h_profPt' is your TProfile
TGraphErrors graph(h_profPt->GetNbinsX());
for (int i = 0; i < h_profPt->GetNbinsX(); ++i) {
    graph.SetPoint(i, h_profPt->GetBinCenter(i+1), h_profPt->GetBinContent(i+1));
    graph.SetPointError(i, 0, h_profPt->GetBinError(i+1));

// Now you can use graph.Interpolate(x) for any x-value

This should give you a linearly interpolated value at any x. Hope this helps!

I think this example can help you.

Dear @EmberEnigma ,

When I try to use “graph.Interpolate(x)” I get the error:

error: no member named ‘Interpolate’ in ‘TGraphErrors’
cout << "20 interpolate = " << graph.Interpolate(20.6) << endl;

Thank you for your suggestions,

Why not using the Fit method given in the example I posted before ?
Note: Interpolate is not a TGraph method.



1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.