Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
Python Interface

Python bindings and utilities for ROOT.

ROOT is a C++ framework used across HEP for data storage, analysis and visualisation. Its full API is available directly in Python through dynamic bindings powered by cppyy. Every ROOT class you see in the C++ documentation is accessible from Python under the ROOT module.

On top of that, a set of pythonizations adapt selected classes to feel more natively Pythonic: operator overloading, iterators, NumPy interoperability, and more.

Installation

conda install -c conda-forge root

See root.cern/install for all installation options.

Quickstart

The entry point to ROOT in Python is one import:

import ROOT

Every ROOT class and function is available under the ROOT module.

Now let's create a histogram, fill it from a NumPy array and write it to a file:

import numpy as np
# Create a 1D histogram
h = ROOT.TH1D("h", "Gaussian distribution;x;counts", 100, -5, 5)
# Fill it from a NumPy array
data = np.random.normal(0, 1, 10000)
h.Fill(data)
# Write it to a ROOT file
with ROOT.TFile.Open("output.root", "RECREATE") as f:
h.Write()

Now we create an RDataFrame from scratch, define a new column with a Python lambda and draw a histogram:

import numpy as np
# Create an RDataFrame with 10000 rows
rdf = ROOT.RDataFrame(10000)
# Define a column x
rdf = rdf.Define("x", lambda : np.random.normal(0, 1))
# Draw a histogram of x
rdf.Histo1D("x").Draw()
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
th1 Draw()

Topics

 Interoperability
 Using ROOT alongside other Python packages.
 Pythonizations
 Python-specific functionalities offered by ROOT.
 RDataLoader
 Feed ROOT data directly into models for machine learning training.
 Unified Histogram Interface (UHI)
 Using ROOT histograms in Python.