How to distinguish 0 from empty histogram bins

I want to plot an efficiency (in 2 variables) and it is important that I can distinguish between 0 efficiency and an empty bin. What is the recommended way to do this? All I can think of is to do the division (and fill the values) manually, assigning a very low, non-zero value to 0 bins and leaving empty ones as 0.

@couet can you take a look here please?


Seems to me there is no way to distinguish empty bin from bin with content 0. @moneta can confirm and may be provide some solution.

IMO the only save way is to create a e.g. h2i = new TH2I with same axes as
the real TH2F and for each Fill increment the TH2I

Int_t bin = h2->Fill(x,y);
if (bin >= 0) h2i->AddBinContent(bin);

but even then watch out for int overflows:
you might need to do

Int_t bin = h2->Fill(x,y);
if (bin >= 0) h2i->SetBinContent(bin, 1);

In this case loose the number how often a bin was filled.
To save space you can use a TH2C.


Thank you all for the input. Given that my primary goal is visualization of a TH2 with the 'COLZ' option, it sounds like my initial suggestion is my best bet. I simply do:

    effhist = (TH2F*)foundhist->Clone("effhist");
    for(int k=0;k<effhist->GetNbinsX(); k++){
      for(int l=0;l<effhist->GetNbinsY(); l++){
        binnum = effhist->GetBin(k+1, l+1);
        nchit = foundhist->GetBinContent(binnum);
        nc = genhist->GetBinContent(binnum);
        if(nchit == 0 && nc != 0) heffprojStripY[j]->SetBinContent(binnum, 1e-6);  // small number instead of 0

In my case, this small number works just fine and shows up nicely in near-zero colors in COLZ:

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