def hist2array(hist, include_overflow=False, copy=True, return_edges=False):
import ROOT<EOL>simple_hist = True<EOL>if isinstance(hist, ROOT.TH3):<EOL><INDENT>shape = (hist.GetNbinsZ() + <NUM_LIT:2>,<EOL>hist.GetNbinsY() + <NUM_LIT:2>,<EOL>hist.GetNbinsX() + <NUM_LIT:2>)<EOL><DEDENT>elif isinstance(hist, ROOT.TH2):<EOL><INDENT>shape = (hist.GetNbinsY() + <NUM_LIT:2>, hist.GetNbinsX() + <NUM_LIT:2>)<EOL><DEDENT>elif isinstance(hist, ROOT.TH1):<EOL><INDENT>shape = (hist.GetNbinsX() + <NUM_LIT:2>,)<EOL><DEDENT>elif isinstance(hist, ROOT.THnBase):<EOL><INDENT>shape = tuple([hist.GetAxis(i).GetNbins() + <NUM_LIT:2><EOL>for i in range(hist.GetNdimensions())])<EOL>simple_hist = False<EOL><DEDENT>else:<EOL><INDENT>raise TypeError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if simple_hist:<EOL><INDENT>for hist_type in '<STR_LIT>':<EOL><INDENT>if isinstance(hist, getattr(ROOT, '<STR_LIT>'.format(hist_type))):<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise AssertionError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>else:  <EOL><INDENT>if isinstance(hist, ROOT.THnSparse):<EOL><INDENT>cls_string = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>cls_string = '<STR_LIT>'<EOL><DEDENT>for hist_type in '<STR_LIT>':<EOL><INDENT>if isinstance(hist, getattr(ROOT, cls_string.format(hist_type))):<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise AssertionError(<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>if simple_hist:<EOL><INDENT>if hist_type == '<STR_LIT:C>':<EOL><INDENT>array_func = getattr(_librootnumpy,<EOL>'<STR_LIT>'.format(len(shape)))<EOL>array = array_func(ROOT.AsCObject(hist))<EOL>array.shape = shape<EOL><DEDENT>else:<EOL><INDENT>dtype = np.dtype(DTYPE_ROOT2NUMPY[hist_type])<EOL>array = np.ndarray(shape=shape, dtype=dtype,<EOL>buffer=hist.GetArray())<EOL><DEDENT><DEDENT>else:  <EOL><INDENT>dtype = np.dtype(DTYPE_ROOT2NUMPY[hist_type])<EOL>if isinstance(hist, ROOT.THnSparse):<EOL><INDENT>array = _librootnumpy.thnsparse2array(ROOT.AsCObject(hist),<EOL>shape, dtype)<EOL><DEDENT>else:<EOL><INDENT>array = _librootnumpy.thn2array(ROOT.AsCObject(hist),<EOL>shape, dtype)<EOL><DEDENT><DEDENT>if return_edges:<EOL><INDENT>if simple_hist:<EOL><INDENT>ndims = hist.GetDimension()<EOL>axis_getters = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>'][:ndims]<EOL><DEDENT>else:<EOL><INDENT>ndims = hist.GetNdimensions()<EOL>axis_getters = ['<STR_LIT>'] * ndims<EOL><DEDENT>edges = []<EOL>for idim, axis_getter in zip(range(ndims), axis_getters):<EOL><INDENT>ax = getattr(hist, axis_getter)(*(() if simple_hist else (idim,)))<EOL>edges.append(np.empty(ax.GetNbins() + <NUM_LIT:1>, dtype=np.double))<EOL>ax.GetLowEdge(edges[-<NUM_LIT:1>])<EOL>edges[-<NUM_LIT:1>][-<NUM_LIT:1>] = ax.GetBinUpEdge(ax.GetNbins())<EOL><DEDENT><DEDENT>if not include_overflow:<EOL><INDENT>array = array[tuple([slice(<NUM_LIT:1>, -<NUM_LIT:1>) for idim in range(array.ndim)])]<EOL><DEDENT>if simple_hist:<EOL><INDENT>array = np.transpose(array)<EOL>if copy:<EOL><INDENT>array = np.copy(array)<EOL><DEDENT><DEDENT>if return_edges:<EOL><INDENT>return array, edges<EOL><DEDENT>return array<EOL>
Convert a ROOT histogram into a NumPy array

    Parameters
    ----------
    hist : ROOT TH1, TH2, TH3, THn, or THnSparse
        The ROOT histogram to convert into an array
    include_overflow : bool, optional (default=False)
        If True, the over- and underflow bins will be included in the
        output numpy array. These bins are excluded by default.
    copy : bool, optional (default=True)
        If True (the default) then copy the underlying array, otherwise the
        NumPy array will view (and not own) the same memory as the ROOT
        histogram's array.
    return_edges : bool, optional (default=False)
        If True, also return the bin edges along each axis.

    Returns
    -------
    array : numpy array
        A NumPy array containing the histogram bin values
    edges : list of numpy arrays
        A list of numpy arrays where each array contains the bin edges along
        the corresponding axis of ``hist``. Overflow and underflow bins are not
        included.

    Raises
    ------
    TypeError
        If hist is not a ROOT histogram.

    See Also
    --------
    array2hist