root_numpy.array2root

root_numpy.array2root(arr, filename, treename='tree', mode='update')

Convert a numpy array into a ROOT TTree and save it in a ROOT TFile.

Fields of basic types, strings, and fixed-size subarrays of basic types are supported. np.object and np.float16 are currently not supported.

Parameters:

arr : array

A numpy structured array

filename : str

Name of the output ROOT TFile. A new file will be created if it doesn’t already exist.

treename : str (optional, default=’tree’)

Name of the ROOT TTree that will be created. If a TTree with the same name already exists in the TFile, it will be extended as documented in array2tree().

mode : str (optional, default=’update’)

Mode used to open the ROOT TFile (‘update’ or ‘recreate’).

Examples

>>> from root_numpy import array2root, root2array
>>> import numpy as np
>>>
>>> a = np.array([(1, 2.5, 3.4),
...               (4, 5, 6.8)],
...              dtype=[('a', np.int32),
...                     ('b', np.float32),
...                     ('c', np.float64)])
>>> array2root(a, 'test.root', mode='recreate')
>>> root2array('test.root')
array([(1, 2.5, 3.4), (4, 5.0, 6.8)],
      dtype=[('a', '<i4'), ('b', '<f4'), ('c', '<f8')])
>>>
>>> a = np.array(['', 'a', 'ab', 'abc', 'xyz', ''],
...              dtype=[('string', 'S3')])
>>> array2root(a, 'test.root', mode='recreate')
>>> root2array('test.root')
array([('',), ('a',), ('ab',), ('abc',), ('xyz',), ('',)],
      dtype=[('string', 'S3')])
>>>
>>> a = np.array([([1, 2, 3],),
...               ([4, 5, 6],)],
...              dtype=[('array', np.int32, (3,))])
>>> array2root(a, 'test.root', mode='recreate')
>>> root2array('test.root')
array([([1, 2, 3],), ([4, 5, 6],)],
      dtype=[('array', '<i4', (3,))])