Search code examples
pythonarraysnumpyroot-framework

Python: saving numpy array to ROOT file


So my research mates and I are trying to save a pretty big (47104,5) array into a TTree in a ROOT file. The array on the Python side works fine. We can access everything and run normal commands, but when we run the root_numpy.array2root() command, we get a weird error.

Object of type 'NoneType' has no len()

The code we are running for this portion is as follows:

import root_numpy as rnp
import numpy as np
import scipy
import logging

def save_array(outputArray, outputName):
    outputString =str(outputName)
    logging.info("Creating .Root file")
    rnp.array2root(outputArray,outputString,treename="Training_Variables",mode="recreate")

We placed the outputString variable as a way to make sure we were putting the filename in as a string. ( In our python terminal, we add .root at the end of outputName to save it as a .root file.).

Here is a picture of the terminal. Showing exact error location in root_numpy

Pretty much, we are confused about why array2root() is calling for the len() of an object, which we dont think should have a len? It should just have a shape. Any insight would be greatly appreciated.


Solution

  • The conversion routines from NumPy arrays to ROOT datatypes work with structured arrays. See the two following links. (Not tested, but this is very likely the problem as the routines use the arr.dtypes.names and arr.dtypes.fields attributes).

    http://rootpy.github.io/root_numpy/reference/generated/root_numpy.array2tree.html#root_numpy.array2tree

    http://rootpy.github.io/root_numpy/reference/generated/root_numpy.array2root.html#root_numpy.array2root