Skip to content

Latest commit

 

History

History
119 lines (96 loc) · 3.4 KB

File metadata and controls

119 lines (96 loc) · 3.4 KB

\defgroup Python Python Interface \ingroup Python \brief 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 @ref Pythonizations adapt selected classes to feel more natively Pythonic: operator overloading, iterators, NumPy interoperability, and more.

Installation

\htmlonly

conda pip
conda install -c conda-forge root
pip install root

⚠ Alpha - Linux only.

<style> .install-tabs { border: 1px solid var(--page-foreground-color, #ccc); border-radius: 6px; overflow: hidden; max-width: 420px; font-family: monospace; } .tab-buttons { display: flex; background: var(--code-background, #f0f0f0); border-bottom: 1px solid var(--page-foreground-color, #ccc); } .tab-btn { padding: 6px 18px; border: none; background: none; cursor: pointer; font-size: 13px; color: var(--page-foreground-color, #333); border-bottom: 2px solid transparent; } .tab-btn.active { border-bottom: 2px solid #1a73e8; font-weight: 600; color: #1a73e8; } .tab-panel pre { margin: 0; padding: 12px 16px; background: var(--code-background, #fff); color: var(--page-foreground-color, #333); } </style> <script> function switchTab(btn, id) { document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active')); document.querySelectorAll('.tab-panel').forEach(p => p.style.display = 'none'); btn.classList.add('active'); document.getElementById(id).style.display = 'block'; } </script>

\endhtmlonly

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:
    f.WriteObject(h, "my_histogram")

Now we create an @ref dataframe - ROOT's high-level interface for columnar data analysis - from scratch, define a new column and draw a histogram:

import numpy as np

# Create an RDataFrame with 10000 rows
rdf = ROOT.RDataFrame(10000)

# Define a column x representing a normal distribution
rdf = rdf.Define("x", "gRandom->Gaus(0, 1)")

# Draw a histogram of x
rdf.Histo1D("x").Draw()