def table_from_root(source, treename=None, columns=None, **kwargs):
    """Read a Table from a ROOT tree
    """
    import root_numpy

    # parse column filters into tree2array ``selection`` keyword
    # NOTE: not all filters can be passed directly to root_numpy, so we store
    #       those separately and apply them after-the-fact before returning
    try:
        selection = kwargs.pop('selection')
    except KeyError:  # no filters
        filters = None
    else:
        rootfilters = []
        filters = []
        for col, op_, value in parse_column_filters(selection):
            try:
                opstr = [key for key in OPERATORS if OPERATORS[key] is op_][0]
            except (IndexError, KeyError):  # cannot filter with root_numpy
                filters.append((col, op_, value))
            else:  # can filter with root_numpy
                rootfilters.append('{0} {1} {2!r}'.format(col, opstr, value))
        kwargs['selection'] = ' && '.join(rootfilters)

    # pass file name (not path)
    if not isinstance(source, string_types):
        source = source.name

    # find single tree (if only one tree present)
    if treename is None:
        trees = root_numpy.list_trees(source)
        if len(trees) == 1:
            treename = trees[0]
        elif not trees:
            raise ValueError("No trees found in %s" % source)
        else:
            raise ValueError("Multiple trees found in %s, please select on "
                             "via the `treename` keyword argument, e.g. "
                             "`treename='events'`. Available trees are: %s."
                             % (source, ', '.join(map(repr, trees))))

    # read, filter, and return
    t = Table(root_numpy.root2array(
        source,
        treename,
        branches=columns,
        **kwargs
    ))
    if filters:
        return filter_table(t, *filters)
    return t
def table_from_root ( source , treename = None , columns = None , ** kwargs ) : import root_numpy try : selection = kwargs . pop ( 'selection' ) except KeyError : filters = None else : rootfilters = [ ] filters = [ ] for col , op_ , value in parse_column_filters ( selection ) : try : opstr = [ key for key in OPERATORS if OPERATORS [ key ] is op_ ] [ 0 ] except ( IndexError , KeyError ) : filters . append ( ( col , op_ , value ) ) else : rootfilters . append ( '{0} {1} {2!r}' . format ( col , opstr , value ) ) kwargs [ 'selection' ] = ' && ' . join ( rootfilters ) if not isinstance ( source , string_types ) : source = source . name if treename is None : trees = root_numpy . list_trees ( source ) if len ( trees ) == 1 : treename = trees [ 0 ] elif not trees : raise ValueError ( "No trees found in %s" % source ) else : raise ValueError ( "Multiple trees found in %s, please select on " "via the `treename` keyword argument, e.g. " "`treename='events'`. Available trees are: %s." % ( source , ', ' . join ( map ( repr , trees ) ) ) ) t = Table ( root_numpy . root2array ( source , treename , branches = columns , ** kwargs ) ) if filters : return filter_table ( t , * filters ) return t
Read a Table from a ROOT tree