TH2D and TGraph2D

Hi, I have drawn a 2D histogram which contains some empty bins.
I would like to find a way to assign them a value interpolating between near bin values.
I saw that TGraph2D has the Interpolate method. How can I use it? I don’t know how to place in the graph the information of the 2D histogram to interpolate the empty bins.
I red the class description and a tutorial macro, but I did not find the answer…
Could anybody of you help me?
thank you very much…

TGraph2D *g2 = new TGraph2D(h2); // h2 is pointer to TH2 objectJan

so in this way then I can use all the TH2 methods?
Thank you very much!

I mean, writing
TGraph2D *g = new TGraph2D(h);
I can also write
h-> “TGraph2D methods”?

thank you

Using the TGraph2D constructor from a TH2 will create a TGraph2D but the empty bins will generated points as any other bins. Have a look at the code of the constructor here: … tml#O.ipvC

To solve your problem I suggest you do the same kind of loop done in that constructor but you do the SetPoint(…) in TGraph2D only if z is not 0. Once the TGraph2D will be filled, just do a GetHistogram() on this TGraph2D and you will get the smoothed 2 histogram you are looking for.

hi, thank you very much for your help. I really needed it!
Now it seems to me TGraph2D has “eliminated” the empty bins of my histogram. What I meant to do actually was the empty bins to remain empty, and to find a method that could allow me to fill them. The project is to interpolate between near bins: e.g. if I find an empty bin, I have to assign it a value that depends on the value which the 4 near bins have…
I thought that TGraph2D::Interpolate method could help me, but with this code I lost the information on the empty bins. What was wrong with it?

void segmenta(int n, double rmax, int npix, double threshold)
TH2D *h = new TH2D(“stat.eventi”,“immagine con thres”,npix,-rmax-5,rmax+5,npix,-rmax-5,rmax+5);
double asc,ord;
int m;
TRandom t;
asc = rmax * t.Rndm()- rmax/2.0;
ord = rmax * t.Rndm()- rmax/2.0;


int i,j;



TGraph2D *g= new TGraph2D(h);


TAxis *xaxis = h->GetXaxis();
TAxis *yaxis = h->GetYaxis();
Int_t xfirst = xaxis->GetFirst();
Int_t xlast = xaxis->GetLast();
Int_t yfirst = yaxis->GetFirst();
Int_t ylast = yaxis->GetLast();

Double_t hmin = h->GetMinimum();
Double_t hmax = h->GetMaximum();

Double_t x, y, z;
Int_t k=0;

for (Int_t i=xfirst; i<= xlast; i++) {
for (Int_t j=yfirst; j<= ylast; j++) {
x = xaxis->GetBinCenter(i);
y = yaxis->GetBinCenter(j);
z = h->GetBinContent(i,j);
if (z>hmin && z<hmax) {
g-> SetPoint(k, x, y, z);

TCanvas *c = new TCanvas ( “c” , “imm_thres” , 800 , 800 ) ;



TGraph2D, thanks to a Delaunay triangulation, can fill an histogram from the TGraph2D data set (via GetHistogram). This data set can come from an histogram, but usually it is not binned data. May be you do not need to use TGraph2D if you want to fill empty bins with the average of the neighbors, this you can do with a simple loop over all bins.