Beispiel #1
0
def root2array(filenames,
               treename=None,
               branches=None,
               selection=None,
               start=None,
               stop=None,
               step=None,
               include_weight=False,
               weight_name='weight'):
    """Convert trees in ROOT files into a numpy structured array.

    Refer to the type conversion table :ref:`here <conversion_table>`.

    Parameters
    ----------
    filenames : str or list
        ROOT file name pattern or list of patterns. Wildcarding is supported by
        Python globbing.
    treename : str, optional (default=None)
        Name of the tree to convert (optional if each file contains exactly one
        tree).
    branches : list of str, optional (default=None)
        List of branch names to include as columns of the array. If None or
        empty then include all branches than can be converted in the first
        tree. If branches contains duplicate branches, only the first one is
        used.
    selection : str, optional (default=None)
        Only include entries fulfilling this condition.
    start, stop, step: int, optional (default=None)
        The meaning of the ``start``, ``stop`` and ``step`` parameters is the
        same as for Python slices. If a range is supplied (by setting some of
        the ``start``, ``stop`` or ``step`` parameters), only the entries in
        that range and fulfilling the ``selection`` condition (if defined) are
        used.
    include_weight : bool, optional (default=False)
        Include a column containing the tree weight.
    weight_name : str, optional (default='weight')
        The field name for the weight column if ``include_weight=True``.

    Examples
    --------

    Read all branches from the tree named ``mytree`` in ``a.root``
    Remember that ``mytree`` is optional if ``a.root`` has one tree::

        root2array('a.root', 'mytree')

    Read all branches starting from entry 5 and include 10 entries or up to the
    end of the file::

        root2array('a.root', 'mytree', start=5, stop=11)

    Read all branches in reverse order::

        root2array('a.root', 'mytree', step=-1)

    Read every second entry::

        root2array('a.root', 'mytree', step=2)

    Read all branches from the tree named ``mytree`` in ``a*.root``::

        root2array('a*.root', 'mytree')

    Read all branches from the tree named ``mytree`` in ``a*.root`` and
    ``b*.root``::

        root2array(['a*.root', 'b*.root'], 'mytree')

    Read branch ``x`` and ``y`` from the tree named ``mytree`` in ``a.root``::

        root2array('a.root', 'mytree', ['x', 'y'])

    Notes
    -----

    Due to the way TChain works, if the trees specified in the input files have
    different structures, only the branch in the first tree will be
    automatically extracted. You can work around this by either reordering the
    input file or specifying the branches manually.

    """
    filenames = _glob(filenames)

    if not filenames:
        raise ValueError("specify at least one filename")

    if treename is None:
        trees = list_trees(filenames[0])
        if len(trees) > 1:
            raise ValueError(
                "treename must be specified if the file "
                "contains more than one tree")
        elif not trees:
            raise IOError(
                "no trees present in {0}".format(filenames[0]))
        else:
            treename = trees[0]

    return _librootnumpy.root2array_fromFname(
        filenames, treename, branches,
        selection,
        start, stop, step,
        include_weight,
        weight_name)
Beispiel #2
0
def root2array(filenames,
               treename=None,
               branches=None,
               selection=None,
               start=None,
               stop=None,
               step=None,
               include_weight=False,
               weight_name='weight'):
    """Convert trees in ROOT files into a numpy structured array.

    Refer to the type conversion table :ref:`here <conversion_table>`.

    Parameters
    ----------
    filenames : str or list
        ROOT file name pattern or list of patterns. Wildcarding is
        supported by Python globbing.
    treename : str, optional (default=None)
        Name of the tree to convert (optional if each file contains exactly one
        tree).
    branches : list of str, optional (default=None)
        List of branch names to include as columns of the array.
        If None or empty then include all branches than can be converted in the
        first tree.
        If branches contains duplicate branches, only the first one is used.
    selection : str, optional (default=None)
        Only include entries fulfilling this condition.
    start, stop, step: int, optional (default=None)
        The meaning of the ``start``, ``stop`` and ``step``
        parameters is the same as for Python slices.
        If a range is supplied (by setting some of the
        ``start``, ``stop`` or ``step`` parameters), only the entries in that
        range and fulfilling the ``selection`` condition (if defined) are used.
    include_weight : bool, optional (default=False)
        Include a column containing the tree weight.
    weight_name : str, optional (default='weight')
        The field name for the weight column if ``include_weight=True``.

    Examples
    --------

    Read all branches from the tree named ``mytree`` in ``a.root``
    Remember that ``mytree`` is optional if ``a.root`` has one tree::

        root2array('a.root', 'mytree')

    Read all branches starting from entry 5 and include 10 entries or up to the
    end of the file::

        root2array('a.root', 'mytree', start=5, stop=11)

    Read all branches in reverse order::

        root2array('a.root', 'mytree', step=-1)

    Read every second entry::

        root2array('a.root', 'mytree', step=2)

    Read all branches from the tree named ``mytree`` in ``a*.root``::

        root2array('a*.root', 'mytree')

    Read all branches from the tree named ``mytree`` in ``a*.root`` and
    ``b*.root``::

        root2array(['a*.root', 'b*.root'], 'mytree')

    Read branch ``x`` and ``y`` from the tree named ``mytree`` in ``a.root``::

        root2array('a.root', 'mytree', ['x', 'y'])

    Notes
    -----

    Due to the way TChain works, if the trees specified in the input files have
    different structures, only the branch in the first tree will be
    automatically extracted. You can work around this by either reordering the
    input file or specifying the branches manually.

    """
    filenames = _glob(filenames)

    if not filenames:
        raise ValueError("specify at least one filename")

    if treename is None:
        trees = list_trees(filenames[0])
        if len(trees) > 1:
            raise ValueError(
                "treename must be specified if the file "
                "contains more than one tree")
        elif not trees:
            raise IOError(
                "no trees present in {0}".format(filenames[0]))
        else:
            treename = trees[0]

    return _librootnumpy.root2array_fromFname(
        filenames, treename, branches,
        selection,
        start, stop, step,
        include_weight,
        weight_name)
Beispiel #3
0
def root2array(filenames,
               treename=None,
               branches=None,
               entries=None,
               offset=0,
               selection=None):
    """
    Convert trees in ROOT files into a numpy structured array.
    Refer to the type conversion table :ref:`here <conversion_table>`.

    Parameters
    ----------
    filenames : str or list
        ROOT file name pattern or list of patterns. Wildcarding is
        supported by Python globbing.
    treename : str, optional (default=None)
        Name of the tree to convert (optional if each file contains exactly one
        tree).
    branches : list of str, optional (default=None)
        List of branch names to include as columns of the array.
        If None or empty then include all branches than can be converted in the
        first tree.
        If branches contains duplicate branches, only the first one is used.
    entries : int, optional (default=None)
        Maximum number of entries that will be converted from the chained
        trees. If None then convert all entries. If a selection is applied then
        fewer entries may be converted.
    offset : int, optional (default=0):
        Offset from the beginning of the chained trees where conversion will
        begin.
    selection : str, optional (default=None)
        Only include entries passing a cut expression.

    Examples
    --------

    Read all branches from the tree named ``mytree`` in ``a.root``
    Remember that ``mytree`` is optional if ``a.root`` has one tree::

        root2array('a.root', 'mytree')

    Read all branches starting from entry 5 and include 10 entries or up to the
    end of the file::

        root2array('a.root', 'mytree', entries=10, offset=5)

    Read all branches from the tree named ``mytree`` in ``a*.root``::

        root2array('a*.root', 'mytree')

    Read all branches from the tree named ``mytree`` in ``a*.root`` and
    ``b*.root``::

        root2array(['a*.root', 'b*.root'], 'mytree')

    Read branch ``x`` and ``y`` from the tree named ``mytree`` in ``a.root``::

        root2array('a.root', 'mytree', ['x', 'y'])

    Notes
    -----

    Due to the way TChain works, if the trees specified in the input files have
    different structures, only the branch in the first tree will be
    automatically extracted. You can work around this by either reordering the
    input file or specifying the branches manually.

    """
    matched_filenames = []
    if isinstance(filenames, basestring):
        matched_filenames = glob(filenames)
    else:
        for fn in filenames:
            tmp = glob(fn)
            if len(tmp) == 0:
                raise IOError('%s does not match any readable file.' % fn)
            matched_filenames.extend(tmp)

    if len(matched_filenames) == 0:
        raise IOError('pattern given does not match any file %s' % filenames)

    if treename is None:
        trees = list_trees(matched_filenames[0])
        if len(trees) != 1:
            raise ValueError('treename needs to be specified if the file '
                             'contains more than one tree. Your choices are:'
                             + str(trees))
        else:
            treename = trees[0]

    return _librootnumpy.root2array_fromFname(
        matched_filenames, treename, branches, entries, offset, selection)
Beispiel #4
0
def root2array(fnames, treename=None, branches=None, N=None, offset=0):
    """
    convert tree *treename* in root files specified in *fnames* to
    numpy structured array. Type conversion table
    is given :ref:`here <conversion_table>`

    Arguments:

        *fnames*: Root file name pattern. Wildcard is also supported by
        Python glob (not ROOT semi-broken wildcard)
        fnames can be string or list of string.

        *treename*: name of tree to convert to numpy array.
        This is optional if the file contains exactly 1 tree.

        *branches(optional)*: list of string for branch name to be
        extracted from tree.

        * If branches is not specified or is None or is empty,
          all from the first treebranches are extracted
        * If branches contains duplicate branches, only the first one is used.

        *N(optional)*: maximum number of data that it should load
        useful for testing out stuff

        *offset(optional)*: start index (first one is 0)

    **Example**

    ::

        # read all branches from tree named mytree from a.root
        # remember that 'mytree' is optional if a.root has 1 tree
        root2array('a.root', 'mytree')

    ::

        #read all branches starting from record 5 for 10 records
        #or the end of file.
        root2array('a.root', 'mytree',offset=5,N=10)

    ::

        # read all branches from tree named mytree from a*.root
        root2array('a*.root', 'mytree')

    ::

        # read all branches from tree named mytree from a*.root and b*.root
        root2array(['a*.root', 'b*.root'], 'mytree')

    ::

        #read branch x and y from tree named mytree from a.root
        root2array('a.root', 'mytree', ['x', 'y'])


    .. note::

        Due to
        the way TChain works, if the trees specified
        in the input files have different structures, only the
        branch in the first tree will be automatically extracted.
        You can work around this by either reordering the input
        file or specifying the branches manually.

    """
    filenames = []
    if isinstance(fnames, basestring):
        filenames = glob(fnames)
    else:
        for fn in fnames:
            tmp = glob(fn)
            if len(tmp) == 0:
                raise IOError('%s does not match any readable file.' % tmp)
            filenames.extend(tmp)

    if len(filenames)==0:
        raise IOError('pattern given does not match any file')

    if treename is None:
        trees = list_trees(filenames[0])
        if len(trees) != 1:
            raise ValueError('treename needs to be specified if the file '
                             'contains more than one tree. Your choices are:'
                             + str(trees))
        else:
            treename = trees[0]

    return _librootnumpy.root2array_fromFname(
        filenames, treename, branches, N, offset)
Beispiel #5
0
def root2array(fnames, treename=None, branches=None, N=None, offset=0):
    """
    convert tree *treename* in root files specified in *fnames* to
    numpy structured array. Type conversion table
    is given :ref:`here <conversion_table>`

    Arguments:

        *fnames*: Root file name pattern. Wildcard is also supported by
        Python glob (not ROOT semi-broken wildcard)
        fnames can be string or list of string.

        *treename*: name of tree to convert to numpy array.
        This is optional if the file contains exactly 1 tree.

        *branches(optional)*: list of string for branch name to be
        extracted from tree.

        * If branches is not specified or is None or is empty,
          all from the first treebranches are extracted
        * If branches contains duplicate branches, only the first one is used.

        *N(optional)*: maximum number of data that it should load
        useful for testing out stuff

        *offset(optional)*: start index (first one is 0)

    **Example**

    ::

        # read all branches from tree named mytree from a.root
        # remember that 'mytree' is optional if a.root has 1 tree
        root2array('a.root', 'mytree')

    ::

        #read all branches starting from record 5 for 10 records
        #or the end of file.
        root2array('a.root', 'mytree',offset=5,N=10)

    ::

        # read all branches from tree named mytree from a*.root
        root2array('a*.root', 'mytree')

    ::

        # read all branches from tree named mytree from a*.root and b*.root
        root2array(['a*.root', 'b*.root'], 'mytree')

    ::

        #read branch x and y from tree named mytree from a.root
        root2array('a.root', 'mytree', ['x', 'y'])


    .. note::

        Due to
        the way TChain works, if the trees specified
        in the input files have different structures, only the
        branch in the first tree will be automatically extracted.
        You can work around this by either reordering the input
        file or specifying the branches manually.

    """
    filenames = []
    if isinstance(fnames, basestring):
        filenames = glob(fnames)
    else:
        for fn in fnames:
            tmp = glob(fn)
            if len(tmp) == 0:
                raise IOError('%s does not match any readable file.' % tmp)
            filenames.extend(tmp)

    if len(filenames) == 0:
        raise IOError('pattern given does not match any file')

    if treename is None:
        trees = list_trees(filenames[0])
        if len(trees) != 1:
            raise ValueError('treename needs to be specified if the file '
                             'contains more than one tree. Your choices are:' +
                             str(trees))
        else:
            treename = trees[0]

    return _librootnumpy.root2array_fromFname(filenames, treename, branches, N,
                                              offset)