Fill empty bins in TH2 built from TGraph2D::GetHistogram

Dear experts:

I found myself in the situation that, due to a priori cuts in my calculation let say z<z0, my data has forbidden zones in the X,Y plane, so if I do something like this:

TGraph2D *gr2D = new TGraph2D(x,y,z);
TH2 *h2D = new TH2D();
h2D = gr2D->GetHistogram();

the extracted h2D contains several empty bins, in the X, Y forbidden areas, which I’d like to fill z0. Is there a way to accomplish that?

TIA,

arian

Try TH2::Fill or TH1::SetBinContent.

Dear willie:

thanks for your prompt reply. Yet my question still open, how do I differentiate a z=0 generated by my calculation from the one generated by the method used to obtain the 2D diagram (TGraphDelaunay::Interpolate)?

for (Int_t ibinx=0; ibinxGetXaxis()->GetNbins(); ibinx++) {
for (Int_t ibiny=0; ibinyGetYaxis()->GetNbins(); ibiny++) {
printf("%d %d %4.2f\n",ibinx,ibiny, h2D->GetBinContent(ibinx,ibiny));
}
}

Zeroes in “z” due to the lack information in the graph are not a real zero in my calculation; z=0 may be a valid value for any of my z values. Forbidden areas, not known a priori, are not restricted to a given position in XY plane either.

cheers,

arian

Try: for (Int_t ibinx = 1; ibinx <= h2D->GetNbinsX(); ibinx++) { for (Int_t ibiny = 1; ibiny <= h2D->GetNbinsY(); ibiny++) { if (h2D->GetBinContent(ibinx, ibiny) <= 0.0) { // you can add here an additional check that it is a "forbidden area" ... // ... and if it is one, ... call "SetBinContent" ... h2D->SetBinContent(ibinx, ibiny, z0); // set "z0" } } }
A better idea would probably be the following. When you create your graph and you meet a “forbidden area”, instead of skipping the point, add a a point with the “z0” value. Then your graph will have no “holes” in it.

[quote=“Wile E. Coyote”]
A better idea would probably be the following. When you create your graph and you meet a “forbidden area”, instead of skipping the point, add a a point with the “z0” value. Then your graph will have no “holes” in it.[/quote]

Yes, that’s a possible solution.

My calculation is meant to explore for “allowed” parameters in a function (6 parameters to be exact). Those parameters are randomly generated and my scape-point-rate is rather large under certain retrictions. For a plot with few zeroed area your solution will work but if the allowed parameter choice is small I’ll end up with a rather large files filled with z0 and a huge amount of calculation and disk space spent. So, ok … looks like I’ll have to refine my accuracy on the parameter selection.

thnks for your ideas,

arian