# How to distinguish 0 from empty histogram bins

**URL:** <https://root-forum.cern.ch/t/how-to-distinguish-0-from-empty-histogram-bins/33516>\
**Category:** ROOT\
**Created:** [April 9, 2019, 5:00pm UTC](https://root-forum.cern.ch/t/how-to-distinguish-0-from-empty-histogram-bins/33516 "2019-04-09T17:00:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [April 9, 2019, 5:00pm UTC](https://root-forum.cern.ch/t/how-to-distinguish-0-from-empty-histogram-bins/33516/1 "2019-04-09T17:00:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![oshadura](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oshadura/32/14460_2.png) [@oshadura](https://root-forum.cern.ch/u/oshadura)\
**Post date:** [April 10, 2019, 7:10am UTC](https://root-forum.cern.ch/t/how-to-distinguish-0-from-empty-histogram-bins/33516/2 "2019-04-10T07:10:19Z")

</div>

@couet can you take a look here please?

Oksana.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 10, 2019, 7:28am UTC](https://root-forum.cern.ch/t/how-to-distinguish-0-from-empty-histogram-bins/33516/3 "2019-04-10T07:28:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 10, 2019, 7:30am UTC](https://root-forum.cern.ch/t/how-to-distinguish-0-from-empty-histogram-bins/33516/4 "2019-04-10T07:30:12Z")

</div>

> [@Set palette of histogram](https://root-forum.cern.ch/t/set-palette-of-histogram/32152/5):
>
> Actually, you could have a signature for “non-empty” bins with “zero contents” … histo1D-\>Fill(x, 1.e-6); histo1D-\>Fill(x, -1.e-6); Note: [Empty bins are these that have the content equal to zero AND error equal to zero.](https://root.cern.ch/doc/master/classTH1.html#a7e7d34c91d5ebab4fc9bba3ca47dabdd)

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [April 10, 2019, 8:09am UTC](https://root-forum.cern.ch/t/how-to-distinguish-0-from-empty-histogram-bins/33516/5 "2019-04-10T08:09:57Z")

</div>

Hi,  
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

```auto
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

```auto
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.

Cheers  
Otto

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [April 11, 2019, 9:36pm UTC](https://root-forum.cern.ch/t/how-to-distinguish-0-from-empty-histogram-bins/33516/6 "2019-04-11T21:36:55Z")

</div>

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:

```auto
    effhist = (TH2F*)foundhist->Clone("effhist");
    effhist->Divide(genhist);
    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:

 ![plot](https://root-forum.cern.ch/uploads/default/original/3X/c/5/c5fe1554bd0e46cab6390e0ead4d49df724ef098.png)

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [April 25, 2019, 9:37pm UTC](https://root-forum.cern.ch/t/how-to-distinguish-0-from-empty-histogram-bins/33516/7 "2019-04-25T21:37:05Z")

</div>

Closed on April 25, 2019, 9:37pm UTC

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