Skip to main content

ROOT I/O in pure Python and Numpy.

Project description

uproot
======

.. image:: https://travis-ci.org/scikit-hep/uproot.svg?branch=master
:target: https://travis-ci.org/scikit-hep/uproot

.. inclusion-marker-1-do-not-remove

ROOT I/O in pure Python and Numpy.

uproot (originally μproot, for "micro-Python ROOT") is a reader and (someday) a writer of the `ROOT file format <https://root.cern/>`_ using only Python and Numpy. Unlike the standard C++ ROOT implementation, uproot is only an I/O library, primarily intended to stream data into machine learning libraries in Python. Unlike PyROOT and root_numpy, uproot does not depend on C++ ROOT. Instead, it uses Numpy calls to rapidly cast data blocks in the ROOT file as Numpy arrays.

It is important to note that uproot is *not* maintained by the ROOT project team, so post bug reports as `uproot GitHub issues <https://github.com/scikit-hep/uproot/issues>`_, not on any ROOT forum.

.. inclusion-marker-2-do-not-remove

Installation
------------

Install OAMap like any other Python package:

.. code-block:: bash

pip install uproot --user

or similar (use ``sudo``, ``virtualenv``, or ``conda`` if you wish).

Strict dependencies:
""""""""""""""""""""

- `Python <http://docs.python-guide.org/en/latest/starting/installation/>`_ (2.6+, 3.4+)
- `Numpy <https://scipy.org/install.html>`_

Recommended dependencies:
"""""""""""""""""""""""""

- `lz4 <https://anaconda.org/anaconda/lz4>`_ compression used by some ROOT files
- `lzma <https://anaconda.org/conda-forge/backports.lzma>`_ compression used by some ROOT files; this is part of the Python 3 standard library, so only install for Python 2

Optional dependencies:
""""""""""""""""""""""

- `XRootD <https://anaconda.org/nlesc/xrootd>`_ to access remote files
- `futures <https://pypi-hypernode.com/pypi/futures>`_ for parallel processing; this is part of the Python 3 standard library, so only install for Python 2

*Reminder: you do not need C++ ROOT to run uproot.*

.. inclusion-marker-3-do-not-remove

Getting started
---------------

Download a Z → μμ `flat ntuple <http://scikit-hep.org/uproot/examples/Zmumu.root>`_ and a H → ZZ → eeμμ `structured TTree <http://scikit-hep.org/uproot/examples/HZZ.root>`_.

.. code-block:: bash

wget http://scikit-hep.org/uproot/examples/Zmumu.root
wget http://scikit-hep.org/uproot/examples/HZZ.root

Open each of the files; uproot presents them as ``dict``-like objects with ROOT names and objects as keys and values. (The "cycle number" after the semicolon can usually be ignored.)

.. code-block:: python

>>> import uproot
>>> uproot.open("Zmumu.root").keys()
[b'events;1']
>>> uproot.open("HZZ.root").keys()
[b'events;1']

Since the file acts as a ``dict``, access the TTrees with square brackets. TTrees are also ``dict``-like objects, with branch names and branches as keys and values. (Hint: ``allkeys()`` lists branches recursively, if they're nested.)

.. code-block:: python

>>> zmumu = uproot.open("Zmumu.root")["events"]
>>> hzz = uproot.open("HZZ.root")["events"]
>>> zmumu.keys()
[b'Type', b'Run', b'Event', b'E1', b'px1', b'py1', b'pz1', b'pt1', b'eta1', b'phi1',
b'Q1', b'E2', b'px2', b'py2', b'pz2', b'pt2', b'eta2', b'phi2', b'Q2', b'M']
>>> hzz.keys()
[b'NJet', b'Jet_Px', b'Jet_Py', b'Jet_Pz', b'Jet_E', b'Jet_btag', b'Jet_ID', b'NMuon',
b'Muon_Px', b'Muon_Py', b'Muon_Pz', b'Muon_E', b'Muon_Charge', b'Muon_Iso', b'NElectron',
b'Electron_Px', b'Electron_Py', b'Electron_Pz', b'Electron_E', b'Electron_Charge',
...

You can turn a chosen set of branches into Numpy arrays with the ``arrays`` method. Each array represents the values of a single attribute for all events, just as they're stored in a split ROOT file.

.. code-block:: python

>>> zmumu.arrays(["px1", "py1", "pz1"])
{b'px1': array([-41.19528764, 35.11804977, ..., 32.37749196, 32.48539387]),
b'py1': array([ 17.4332439 , -16.57036233, ..., 1.19940578, 1.2013503 ]),
b'pz1': array([-68.96496181, -48.77524654, ..., -74.53243061, -74.80837247])}

If the number of items per entry is not constant, such as the number of jets in an event, they can't be expressed as flat Numpy arrays. Instead, uproot loads them into `jagged arrays <https://en.wikipedia.org/wiki/Jagged_array>`_.

.. code-block:: python

>>> hzz.array("Jet_E")
jaggedarray([[],
[44.137363],
[],
...,
[55.95058],
[229.57799 33.92035],
[]])

A jagged array behaves like an array of unequal-length arrays,

.. code-block:: python

>>> for jetenergies in hzz.array("Jet_E"):
... print("event")
... for jetenergy in jetenergies:
... print(jetenergy)
...
event
event
44.137363
event
event
230.34601
101.35884
60.08414

But it's built out of regular Numpy arrays, for use in libraries that accept Numpy.

.. code-block:: python

>>> jaggedarray.content
array([ 44.137363, 230.34601 , 101.35884 , ..., 55.95058 , 229.57799 ,
33.92035 ], dtype=float32)
>>> jaggedarray.starts
array([ 0, 0, 1, ..., 2770, 2771, 2773])
>>> jaggedarray.stops
array([ 0, 1, 1, ..., 2771, 2773, 2773])

.. inclusion-marker-4-do-not-remove

Reference documentation
-----------------------

The complete reference documentation is available on `uproot.readthedocs.io <http://uproot.readthedocs.io/en/latest/>`_. These are exhaustive descriptions of each function and its parameters, also available as Python help strings.

- `Opening files <http://uproot.readthedocs.io/en/latest/opening-files.html>`_
- `ROOT I/O <http://uproot.readthedocs.io/en/latest/root-io.html>`_
- `TTree methods <http://uproot.readthedocs.io/en/latest/ttree-handling.html#uproot-tree-ttreemethods>`_
- `TBranch methods <http://uproot.readthedocs.io/en/latest/ttree-handling.html#uproot-tree-tbranchmethods>`_

Introductory tutorials
----------------------

Reference documentation is not the place to start learning about a topic. Introductory tutorials are included on this page.

- `Exploring a file`_
- `Array-reading parameters`_
- `Remote files through XRootD`_
- `Reading only part of a TBranch`_
- `Iterating over files (like TChain)`_
- `Non-flat TTrees: jagged arrays and more`_
- `Non-TTrees: histograms and more`_
- `Caching data`_
- `Parallel processing`_
- `Connectors to other packages`_

Introductory tutorials
----------------------

Introductory tutorials can be found on the `GitHub README <https://github.com/scikit-hep/uproot#introductory-tutorials>`_.

Release history Release notifications | RSS feed

This version

2.6.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uproot-2.6.3.tar.gz (2.8 MB view details)

Uploaded Source

File details

Details for the file uproot-2.6.3.tar.gz.

File metadata

  • Download URL: uproot-2.6.3.tar.gz
  • Upload date:
  • Size: 2.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for uproot-2.6.3.tar.gz
Algorithm Hash digest
SHA256 e5139c7b4a5a9feb7abb7956ff43db5eabfd45dc49b5c48ce7b033764bc5f4d9
MD5 e51a5db2bbb220e9a1e073f7dda25d13
BLAKE2b-256 8d1e039a97ca74e24dcb478225fe8ee088f624256854a2808abff23bf65589f3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page