root_numpy.array2hist

root_numpy.array2hist(array, hist, errors=None)

Convert a NumPy array into a ROOT histogram

Parameters:

array : numpy array

A 1, 2, or 3-d numpy array that will set the bin contents of the ROOT histogram.

hist : ROOT TH1, TH2, or TH3

A ROOT histogram.

errors : numpy array

A numpy array of errors with matching dimensionality as the bin contents array. If not given, no errors are set

Returns:

hist : ROOT TH1, TH2, or TH3

The ROOT histogram with bin contents set from the array.

Raises:

TypeError

If hist is not a ROOT histogram.

ValueError

If the array and histogram are not compatible in terms of dimensionality or number of bins along any axis.

See also

hist2array

Notes

The NumPy array is copied into the histogram’s internal array. If the input NumPy array is not of the same data type as the histogram bin contents (i.e. TH1D vs TH1F, etc.) and/or the input array does not contain overflow bins along any of the axes, an additional copy is made into a temporary array with all values converted into the matching data type and with overflow bins included. Avoid this second copy by ensuring that the NumPy array data type matches the histogram data type and that overflow bins are included.

Examples

>>> from root_numpy import array2hist, hist2array
>>> import numpy as np
>>> from rootpy.plotting import Hist2D
>>> hist = Hist2D(5, 0, 1, 3, 0, 1, type='F')
>>> array = np.random.randint(0, 10, size=(7, 5))
>>> array
array([[6, 7, 8, 3, 4],
       [8, 9, 7, 6, 2],
       [2, 3, 4, 5, 2],
       [7, 6, 5, 7, 3],
       [2, 0, 5, 6, 8],
       [0, 0, 6, 5, 2],
       [2, 2, 1, 5, 4]])
>>> _ = array2hist(array, hist)
>>> # dtype matches histogram type (D, F, I, S, C)
>>> hist2array(hist)
array([[ 9.,  7.,  6.],
       [ 3.,  4.,  5.],
       [ 6.,  5.,  7.],
       [ 0.,  5.,  6.],
       [ 0.,  6.,  5.]], dtype=float32)
>>> # overflow is excluded by default
>>> hist2array(hist, include_overflow=True)
array([[ 6.,  7.,  8.,  3.,  4.],
       [ 8.,  9.,  7.,  6.,  2.],
       [ 2.,  3.,  4.,  5.,  2.],
       [ 7.,  6.,  5.,  7.,  3.],
       [ 2.,  0.,  5.,  6.,  8.],
       [ 0.,  0.,  6.,  5.,  2.],
       [ 2.,  2.,  1.,  5.,  4.]], dtype=float32)
>>> array2 = hist2array(hist, include_overflow=True, copy=False)
>>> hist[2, 2] = -10
>>> # array2 views the same memory as hist because copy=False
>>> array2
array([[  6.,   7.,   8.,   3.,   4.],
       [  8.,   9.,   7.,   6.,   2.],
       [  2.,   3., -10.,   5.,   2.],
       [  7.,   6.,   5.,   7.,   3.],
       [  2.,   0.,   5.,   6.,   8.],
       [  0.,   0.,   6.,   5.,   2.],
       [  2.,   2.,   1.,   5.,   4.]], dtype=float32)
>>> # x, y, z axes correspond to axes 0, 1, 2 in numpy
>>> hist[2, 3] = -10
>>> array2
array([[  6.,   7.,   8.,   3.,   4.],
       [  8.,   9.,   7.,   6.,   2.],
       [  2.,   3., -10., -10.,   2.],
       [  7.,   6.,   5.,   7.,   3.],
       [  2.,   0.,   5.,   6.,   8.],
       [  0.,   0.,   6.,   5.,   2.],
       [  2.,   2.,   1.,   5.,   4.]], dtype=float32)