1

Hi I have a ROOT TTree with a bit of a complicated structure. When I use uproot to create an array:

analysis = uproot.open("/b/LJ_data/02Oct2019/FRVZ/FRVZprompt2zd_mH125_mzd01.root")["analysis"]
el_Eratio = analysis.arrays(["el_Eratio"], cache=mycache);
print(el_Eratio)

I get a Jagged Array:

{b'el_Eratio': <JaggedArray [[0.9679527 0.8814101 0.88584787] [0.34557977 0.22699767 0.9040524 0.0] [] ... [0.94681776] [0.91043043 0.621741 0.85297334 0.9364375] [0.83885396]] at 0x7f39cf32dfd0>}

I am trying to create a simple histogram of this data:

n, bins, patches = plt.hist(el_Eratio, 100, density = True)

But I am getting the error:

Traceback (most recent call last):
  File "macro.py", line 45, in <module>
    n, bins, patches = plt.hist(el_Eratio, 100, density = True)
  File "/opt/ohpc/pub/packages/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2636, in hist
    **({"data": data} if data is not None else {}), **kwargs)
  File "/opt/ohpc/pub/packages/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py", line 1589, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/opt/ohpc/pub/packages/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 6721, in hist
    xmin = min(xmin, np.nanmin(xi))
TypeError: '<' not supported between instances of 'dict' and 'float'

Do I need to reformat the Jagged Array to a list or normal array? If so how do I do this?

Or am I just calling the array wrong? I also tried:

n, bins, patches = plt.hist(el_Eratio.values(), 100, density = True)

But I get a similar error:

Traceback (most recent call last):
  File "macro.py", line 45, in <module>
    n, bins, patches = plt.hist(el_Eratio.values(), 100, density = True)
  File "/opt/ohpc/pub/packages/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2636, in hist
    **({"data": data} if data is not None else {}), **kwargs)
  File "/opt/ohpc/pub/packages/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py", line 1589, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/opt/ohpc/pub/packages/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 6721, in hist
    xmin = min(xmin, np.nanmin(xi))
  File "/opt/ohpc/pub/packages/anaconda3/lib/python3.7/site-packages/numpy/lib/nanfunctions.py", line 298, in nanmin
    res = np.amin(a, axis=axis, out=out, **kwargs)
  File "/opt/ohpc/pub/packages/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 2618, in amin
    initial=initial)
  File "/opt/ohpc/pub/packages/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 86, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: operands could not be broadcast together with shapes (3,) (2,) 

Disclamer: Although I have experience with root and C++, I am new to python.

Thanks, Sarah

1 Answers1

0

Because you said analysis.arrays (plural), you got back a Python dict. The only array it contains (because you asked for only one, ["el_Eratio"]) has one key: b"el_Eratio". Note that this is a bytestring (starts with b). If you know the encoding, such as "utf-8", you can pass namedecode="utf-8" to the arrays method to get plain strings.

After extracting the JaggedArray, you'll still need to turn it into a flat array for the histogramming function to know what to do with it:

plt.hist(el_Eratio[b'el_Eratio'].flatten())

Specifically, you're saying that you want a histogram of the nested contents of the jagged array, not something else, like

plt.hist(el_Eratio[b'el_Eratio'].counts)

the number of values in each inner array. This kind of dataset has more structure, so you need to decide what to do with that structure before you have a one-dimensional bag of numbers to plot.

Jim Pivarski
  • 5,568
  • 2
  • 35
  • 47