pythonuproot

How do I read a TMatrixT with uproot in Python?


I have a *.root file I'm trying to read into Python with uproot (uproot4). It looks like this:

>>> data = up.open('file.root')
>>> data.keys()
['ring_sums;1', 'tpc_multiplicity;1', 'impact_parameter;1']
>>> data['ring_sums']
<TMatrixT<double> (version 4) at 0x0205b219d748>

How can I read a TMatrixT type? Using .keys(), .values(), and .items() doesn't work.

>>> data['ring_sums'].values()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Model_TMatrixT_3c_double_3e__v4' object has no attribute 'values'

What's the proper way to read out the data into an awkward array, pandas dataframe, or numpy array?


Solution

  • TMatrixT is a class that Uproot doesn't have any specialized code for (yet?), but it can be read anyway because its "streamers" (instructions for deserialization) are included in the ROOT file. It's therefore presented in a rather generic way.

    You can find all of an object's member data in all_members (dict) or extract only one using the member("memberName") method. So, for instance, try

    data['ring_sums'].member("fElements")
    

    because TMatrixT::fElements seems to be relevant. These will probably come out as an unshaped NumPy array (or uproot.models.TArray, which is a subclass of NumPy arrays). You may need to reshape it into the right form, and the proper shape might be found in some other member (maybe one belonging to a superclass).

    If you find a good way of using TMatrixT objects and would like it to become automated, perhaps in a values method, add it as a pull request or describe it as a feature request for Uproot (a new "behavior").