You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/index.md
+51-29Lines changed: 51 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,33 @@
2
2
\ingroup Python
3
3
\brief Using ROOT histograms in Python
4
4
5
-
ROOT histograms implement the [Unified Histogram Interface (UHI)](https://uhi.readthedocs.io/en/latest/index.html), enhancing interoperability with other UHI-compatible libraries. This compliance standardizes histogram operations, making tasks like plotting, indexing, and slicing more intuitive and consistent.
5
+
# ROOT Histograms & UHI
6
+
7
+
ROOT histograms implement the [Unified Histogram Interface (UHI)](https://uhi.readthedocs.io/en/latest/index.html), a standard protocol that makes ROOT histograms interoperable with the broader Python
8
+
scientific ecosystem. This compliance standardizes histogram operations, making tasks like plotting, indexing, and slicing more intuitive and consistent.
9
+
10
+
\note UHI support is available for all [`TH1`](https://root.cern.ch/doc/master/classTH1.html)-derived
11
+
classes, including [`TH2`](https://root.cern.ch/doc/master/classTH2.html) and
@@ -18,10 +44,12 @@ You can read more about the protocol on the [UHI plotting](https://uhi.readthedo
18
44
importROOT
19
45
import matplotlib.pyplot as plt
20
46
import mplhep as hep
47
+
import numpy as np
21
48
22
49
# Create and fill a 1D histogram
23
50
h1 =ROOT.TH1D("h1", "MyHist", 10, -1, 1)
24
-
h1.FillRandom("gaus", 1000)
51
+
arr = np.random.normal(0, 1, 1000)
52
+
h1.Fill(arr)
25
53
26
54
# Load a style sheet and plot the histogram
27
55
hep.style.use("LHCb2")
@@ -30,39 +58,16 @@ plt.title("MyHist")
30
58
plt.show()
31
59
```
32
60
33
-
\image html uhi_th1_plot.png width=600px
61
+
For 2D histograms, use `hep.hist2dplot`:
34
62
35
-
\anchor additional-notes-1
36
-
## Additional Notes
37
-
38
-
- UHI plotting related pythonizations are added to all [`TH1`](https://root.cern.ch/doc/master/classTH1.html)-derived classes (that includes [`TH2`](https://root.cern.ch/doc/master/classTH2.html) and [`TH3`](https://root.cern.ch/doc/master/classTH3.html)).
39
-
- While some libraries such as [mplhep](https://github.com/scikit-hep/mplhep) may not yet support multidimensional `PlottableHistogram` objects, you can call `.values()` on your histogram to retrieve a [`numpy.ndarray`](https://numpy.org/doc/2.2/reference/generated/numpy.ndarray.html) and pass it to appropriate plotting functions.
40
-
- Example plotting a 2D ROOT histogram with [`matplotlib.pyplot.imshow`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html#matplotlib-pyplot-imshow) and [`seaborn.heatmap`](https://seaborn.pydata.org/generated/seaborn.heatmap.html#seaborn-heatmap):
- ex. `h_projected = h[:, ::sum, ::sum]` --> `h_projected` is a 1D histogram representing the y and z projections along the x axis.
155
160
-**Setting operations**
156
161
- Setting with a scalar does not set the flow bins.
157
-
- Setting with an array checks whether the array matches the shape of the histogram with flow bins or the size without flow bins.
162
+
- Setting with an array checks whether the array matches the shape of the histogram with flow bins or the size without flow bins.
163
+
164
+
165
+
# Serialization
166
+
167
+
ROOT histograms can be serialized to a [shared UHI format](https://uhi.readthedocs.io/en/latest/serialization.html)
168
+
and deserialized into any UHI-compatible library, enabling histogram exchange between ROOT, boost-histogram, hist and others without manual conversion.
169
+
170
+
```python
171
+
import json, uhi.io.json, hist
172
+
173
+
# ROOT histogram → JSON
174
+
ob = json.dumps(h_root, default=uhi.io.json.default)
175
+
176
+
# JSON → any UHI-compatible library
177
+
ir = json.loads(ob, object_hook=uhi.io.json.object_hook)
Copy file name to clipboardExpand all lines: bindings/pyroot/pythonizations/python/ROOT/_pythonization/dataloader.md
+2-3Lines changed: 2 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,15 @@
1
-
\defgroup Py_ML Machine Learning Training
1
+
\defgroup Py_ML RDataLoader
2
2
\ingroup Python
3
3
\brief Feed ROOT data directly into models for machine learning training.
4
4
5
-
# RDataLoader
6
5
7
6
`RDataLoader` streams ROOT data into machine learning frameworks as batches ready for training. It takes any [RDataFrame](@ref Py_RDataFrame) as input, giving you access to the full ROOT ecosystem for filtering, defining new variables and applying selections; it delivers batches of your dataset for [NumPy](https://numpy.org/devdocs/reference/generated/numpy.ndarray.html), [TensorFlow](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) and [PyTorch](https://docs.pytorch.org/docs/main/tensors.html) through a simple iteration interface.
8
7
9
8
\note `RDataLoader` is part of `ROOT.Experimental.ML` and is currently experimental. The API may change between ROOT releases.
0 commit comments