Adding Hist2D empty_clone causes ROOT warning (different bin limits)

33 views
Skip to first unread message

Luke Kreczko

unread,
Jun 2, 2015, 11:45:19 AM6/2/15
to rootpy...@googlegroups.com
Dear all,

I see strange behaviour when adding the original histogram to an empty clone:
```python
from rootpy.plotting import Hist2D
h1 = Hist2D( 2, 0, 1, 2, 0, 1 )
h2 = h1.empty_clone()
h2.Add( h1 )
```
```
WARNING:ROOT.TH2F.Add] Attempt to add histograms with different bin limits
```
But the final histogram looks fine. Do you know what is going on?

Cheers,
Luke

Noel Dawe

unread,
Jun 2, 2015, 9:16:43 PM6/2/15
to rootpy...@googlegroups.com
Hi Luke,

h2 ends up being initialized like this inside empty_clone (with explicit bin edges):

>>> h2 = Hist2D([0.0, 0.5, 1.0], [0.0, 0.5, 1.0])

And then ROOT stores the edges in the internal TArrayD.

h1 was initialized with fixed-width bins and ROOT doesn't bother keep the array of bin edges.

Then in TH1::CheckConsistency / TH1::CheckBinLimits the DifferentBinLimits exception is thrown when the lengths of the TArrays don't match:

>>> h1.xaxis.GetXbins().fN
0
>>> h2.xaxis.GetXbins().fN
3

So like you saw:

>>> h2.Add(h1)
WARNING:ROOT.TH2F.Add] Attempt to add histograms with different bin limits
True

Interestingly this is not symmetric....

>>> h1.Add(h2)
True

No warning there.

Overall, I'd say that this is a ROOT issue. Their consistency check should be able to handle this properly.

We can avoid this in rootpy by either suppressing that warning, or detecting the fixed-width bins and initializing the histogram the simple way. I'd rather that the ROOT guys fix it on their end though.

Noel

--
You received this message because you are subscribed to the Google Groups "rootpy users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rootpy-users...@googlegroups.com.
To post to this group, send email to rootpy...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rootpy-users/CANW5eBBzVvT5LAr9jCuSLe-aFS7B0qpWKuLHa-RbEZc3fR83ZA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Noel Dawe

unread,
Jun 2, 2015, 9:25:04 PM6/2/15
to rootpy...@googlegroups.com
You can also avoid the warning by initializing h1 by specifying the bin edges:

>>> from rootpy.plotting import Hist
>>> from rootpy.plotting import Hist2D
>>> h1 = Hist2D([0, .5, 1], [0, .5, 1])
>>> h2 = h1.empty_clone()
>>> h1.Add(h2)
True
>>> h2.Add(h1)
True

Luke Kreczko

unread,
Jun 3, 2015, 4:26:48 AM6/3/15
to rootpy...@googlegroups.com
Hi Noel,

Thanks a lot for your replies. I think I understand now.

In our case we read h1 from a file, so the initialisation is not in our hand. I will see what I can do.
But as you mention, it is just a warning and the bins are all right.

Thanks for helping me understand.

Cheers,
Luke

Noel Dawe

unread,
Jun 4, 2015, 12:49:56 AM6/4/15
to rootpy...@googlegroups.com
Hi Luke,

No problem.

If you know what you are doing is fine, and you just want to ignore these warnings, you can use rootpy's logger to suppress them:

from rootpy.plotting import Hist2D
from rootpy import log
import logging
log['/ROOT.TH2F.Add'].setLevel(logging.FATAL)
h1 = Hist2D(2, 0, 1, 2, 0, 1)
h2 = h1.empty_clone()
h2.Add(h1)

Noel

Reply all
Reply to author
Forward
0 new messages