# Remove gaps in a 2D histogram

Hi all,

I am trying to remove the white gaps in the 2D histogram below

Assuming we have a list of bin numbers corresponding to the white spaces, is there an efficient way to ‘modify’ the histogram by chopping those Y projections of the in numbers?

Or do I need to reconstruct the whole histogram by filling in the Y projections of the bins corresponding to non-zero entries? I am not sure, but I think this will be a more resource-intensive job.

Thanks

As you seem to know which values should have these white bins, simply fill them with (using `SetBinContent(...)`) which the appropriate value.

I am trying to delete the projection of these white and replace it with the closest filled projection.
I am not sure how to do this shiting of bins here.

Either bin moving the bins in place (proper loop should be able to do it). Or by creating a new histogram.

Hi I tried a new histogram, but I am seeing an empty histogram with non-zero entries in that statistics bar. Can you please suggest what is missing here? Thanks a lot

``````
void RemoveGap()
{
TCanvas *c4=new TCanvas("c4","Original 2D Hist",0,550,500,250);
TCanvas *c5=new TCanvas("c5","Processed 2D Hist",550,550,500,250);

c4->cd();

//Create a new histo
TH2* His_gm = new TH2D("his_gm",  "", His->GetNbinsX(), 0., 3000., His->GetNbinsY(), 0., 4000.);

for(int X=14; X<400; X++) {
int x = 0;
int entries = His->Integral(X,X,0,-1);
if (entries < 1000) continue;
for (int Y = 0; Y < His->GetNbinsY(); Y++) {
His_gm->SetBinContent(x, Y, His->GetBinContent(X, Y) );
}
x=x+1; //increment x bin of the newhistogram by 1

}
c5->cd();
His_gm->Draw();

}

``````

``````int x = 0;  // <<<<<< bin numbers start at 1 should be x=1;
``````
``````if (entries < 1000) continue; /// <<<< are you sure entries is sometimes > 1000 ?
``````
``````for (int Y = 0; Y < His->GetNbinsY(); Y++) { /// <<<<< bins strt at 1, it should e Y = 1
``````
1 Like

Hi thanks, it seems to work.

As a test case, I tried to copy the whole histogram with the white gaps by modifying the above macro (below).
However, there seems to be a mismatch between the entries(statistics) from the original histogram and the one created here. The mean, standard deviation and integral happen to be the same.

Does this have to do with the `SetBinContent()`and `GetBinContent` or with some issues in the macro (shared below)?

``````void RemoveGap()
{
TCanvas *c4=new TCanvas("c4","Original 2D Hist",0,550,500,250);
TCanvas *c5=new TCanvas("c5","Processed 2D Hist",550,550,500,250);

c4->cd();
TH1* His_copy = (TH1*)His->Clone();
His->Draw("colz");

//Create a new histo
TH2* His_gm = new TH2D("his_gm",  "", His->GetNbinsX(), 0., 3000., His->GetNbinsY(), 0., 4000.);
//int x = 0;
for(int X=1; X< His->GetNbinsX(); X++) {
int entries = His->Integral(X,X,0,-1);
//if (entries < 1000) continue;
for (int Y = 1; Y < His->GetNbinsY(); Y++) {
His_gm->SetBinContent(X, Y, His->GetBinContent(X, Y) );
}
//x=x+1; //increment x bin of the gm histogram by 1
}
c5->cd();
His_gm->Draw("colz");
}
``````

Thank You.

I guess @moneta might know why.

Hi,

I am not sure what the problem is. If you use SetBinContent, the number of entries is lost, because this is related to the number of time you call the `TH1::Fill` function. You can get the effective entry number by calling `TH1::ResetStats()`

Lorenzo

Very briefly, I have a 2d histogram and try to reproduce it by filling an empty histogram by looping across the x-axis bins followed by a nested loop in y-axis bins. However, the rebuilt histogram and the original histogram have a mismatch of entries. The mean, SD, and integral are the same.

Thanks for the explanation.

So effectively, to ensure that the entries of the original and copied histogram match, I need to reset the statistics for the copied histogram after using the `SetBInContent(),` right?

Thank You

Yes, you can call `ResetStats()` or save the previous entries and reset afterwards after manipulating the histograms. This is because we cannot compete them automatically if you manipulate the histogram

Lorenzo

Thanks for the help!

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