Pbm with histo values reading

Hi, since now I use the following commands for some analysis :

            f_root = ROOT.TFile(pathDATA + item)
            h_rel = gr.getHisto(f_root, tp_1)
            for i in range(0, N_histos): 
                histo_rel = h_rel.Get(branches[i])
                print('[{:03d}] : {:s}'.format(i, branches[i]))
                if (histo_rel):
                s_new = []
                    for entry in histo:
                        s_new.append(entry)
                s_new = np.asarray(s_new)
                s_new = s_new[1:-1]

but since the 6.36.00 version of ROOT, I have the following error :

>  File "/usr/lib64/python3.9/site-packages/ROOT/_pythonization/_uhi.py", line 367, in _getitem
>     uhi_index = _compute_common_index(self, index)
>   File "/usr/lib64/python3.9/site-packages/ROOT/_pythonization/_uhi.py", line 223, in _compute_common_index
>     return [_compute_uhi_index(self, idx, axis, include_flow_bins) for axis, idx in enumerate(index)]
>   File "/usr/lib64/python3.9/site-packages/ROOT/_pythonization/_uhi.py", line 223, in <listcomp>
>     return [_compute_uhi_index(self, idx, axis, include_flow_bins) for axis, idx in enumerate(index)]
>   File "/usr/lib64/python3.9/site-packages/ROOT/_pythonization/_uhi.py", line 192, in _compute_uhi_index
>     return _process_index_for_axis(self, index, axis)
>   File "/usr/lib64/python3.9/site-packages/ROOT/_pythonization/_uhi.py", line 180, in _process_index_for_axis
>     raise IndexError(f"Histogram index {index - 1} out of range for axis {axis}. Valid range: (0,{nbins})")
> IndexError: Histogram index 11 out of range for axis 0. Valid range: (0,11)

In fact, I saw that I do not need the last line of my version ( s_new = s_new[1:-1] ) but it seems that if the loop begins with 1, it ends with 11 (in my example) instead of 10 (nbins+1 instead of nbins).

Is there a correct way to correct this ?
Thanks in adcance.
Arnaud


Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.36.00
Platform: lxplus
Compiler: python3


Hi @archiron,

Thank you for reporting this issue and sharing the code snippet!
In fact, a related issue was recently addressed in ROOT and the fix has been merged to master (see this pull request).
However, this fix is only available in the latest ROOT master, so it won’t be part of your current ROOT 6.36.00 installation. If you’re hitting indexing problems when accessing histogram bins, it’s likely related to the transition to UHI (Unified Histogram Interface), which introduced some indexing conventions.

ROOT histograms use a bin indexing system that ranges from 0 to nbins+1 where 0 is the underflow bin and nbins+1 is the overflow (see conventions for numbering bins). In contrast, UHI inherits 0-based indexing from numpy array conventions where 0 is the first valid element and n-1 is the last valid element, here is the concrete breakdown:

  • h[underflow] returns the underflow bin (equivalent to h.GetBinContent(0)).
  • h[0] returns the first valid bin (equivalent to h.GetBinContent(1)).
  • h[-1] returns the last valid bin (equivalent to h.GetBinContent(nbins)).
  • h[overflow] returns the overflow bin (equivalent to h.GetBinContent(nbins+1)).

You can read more about it here.

In your specific case, if you’re just trying to convert the histogram into an array, there’s no need to manually loop and slice the entries. You can now directly do:

s_new = histo_rel.values()

This returns a NumPy array with the histogram bin contents (excluding underflow and overflow bins).

Let us know if you need help adapting the rest of your code. And thanks again for bringing this up!

You save me !!
I applied your tip (s_new = histo_rel.values()) and more than the solution to my bin problem, I also had a solution to another problem.

Many thanks !