Search code examples
uproot

cannot write Awkward Array type to ROOT file


I'm new to uproot lib and just a user.
I want to write the string type into root file, but the error happens as below:

Error: TypeError: cannot write Awkward Array type to ROOT file: 46 * string

Aim: I want to extract some information from 10 root files via a algorithm and merge them into one root file. I stored these information into a list.

The root file and the code can be found here[GoogleDrive](https://drive.google.com/file/d/1U6-Ug-uq3sHMwsY9ISJj8K2Dw9ssuLlF/view?usp=sharing, https://drive.google.com/file/d/1a32KQw30iLBv_H9qWcNUEsBAfEDOSTVM/view?usp=sharing)

Does someone know how to solve this problem? I would be very grateful if someone could solve this difficult problem.

Best,
Jing ([email protected])


Solution

  • The ability to write strings was added in PR #940 (Oct 2023), which is in release 5.0.13, 5.1.0, and following. Here's a demo:

    >>> import awkward as ak
    >>> import uproot
    >>> strings = ak.Array([f"str{i}" for i in range(46)])
    >>> print(strings.type)
    46 * string
    >>> with uproot.recreate("output.root") as file:
    ...     file["tree"] = {"branch": strings}
    ... 
    

    Then, restart Python (to prove that it's really in the file) and read it back:

    >>> import uproot
    >>> with uproot.open("output.root") as file:
    ...     strings = file["tree"]["branch"].array()
    ... 
    >>> print(strings.type)
    46 * string
    >>> strings
    <Array ['str0', 'str1', 'str2', ..., 'str44', 'str45'] type='46 * string'>
    

    So you should be able to just upgrade Uproot and it will work.