TGraph2D to TH2 segmentation violation

I have some root files with TGraph2D objects in it that I need to use for interpolation. I read in other thread that using Interpolation from TH2 is faster, so I tried to Get the objects as a TH2 but I get errors when using Interpolate():

root [11] TH2F *h2 = (TH2F*)_file0->Get("Efield_x")
(TH2F *) 0x1f18e90
root [12] Efield_x->Interpolate(0,0)
(Double_t) -177.869
root [13] h2->Interpolate(0,0)

 *** Break *** segmentation violation

If use h2->Draw("COLZ") it draws the expected map.
What is going on and how can I fix this?

What is Efield_x ? a TGraph2D ?
If that’s the case you cannot do:

TH2F *h2 = (TH2F*)_file0->Get("Efield_x")

You can try:

TH2F *h2 = Efield_x->GetHistogram();

This works and gives me a TH2F object that when I Draw it looks exactly like the TGraph2D. However when I use Interpolate() the result can be significantly different. For example:

h2->Interpolate(0.05,0)
(Double_t) -303134.
Efield_x_graph->Interpolate(0.05,0)
(Double_t) -459678.

I understand that the algorithm used for the interpolate function is different, but is this difference normal? Is there a way around this?

It might be different depending on your data. For instance if your TGraph2D has a small number of points with a high gradian between them … also the number of bins of the underlying histogram can have an effect … they to increase the number of bins. It is difficult to tell without seeing both …

The points in my TGraph2D are equidistant. Is it possible to have a bin per TGraph2D point?

You can use SetNpx and SetNpy to change le number of bins of the underlying histogram.