Skip to content

Commit c114459

Browse files
committed
[doc][Python] Add UHI cheatsheet and update section
1 parent 52d453d commit c114459

6 files changed

Lines changed: 55 additions & 32 deletions

File tree

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/index.md

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,33 @@
22
\ingroup Python
33
\brief Using ROOT histograms in Python
44

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
12+
[`TH3`](https://root.cern.ch/doc/master/classTH3.html).
13+
14+
## Cheat Sheet
15+
16+
A one-page quick reference covering the API.
17+
18+
\htmlonly
19+
<object data="uhi-cheatsheet.pdf"
20+
type="application/pdf"
21+
width="100%"
22+
height="520px"
23+
style="border:1px solid #ccc;border-radius:6px;">
24+
<p>PDF preview not available in your browser.</p>
25+
</object>
26+
<a href="uhi-cheatsheet.pdf"
27+
style="display:inline-block;margin-top:8px;padding:6px 14px;background:#1a73e8;
28+
color:#fff;border-radius:4px;text-decoration:none;font-size:13px;">
29+
⬇ Download cheat sheet (PDF)
30+
</a>
31+
\endhtmlonly
632

733

834
\anchor plotting
@@ -18,10 +44,12 @@ You can read more about the protocol on the [UHI plotting](https://uhi.readthedo
1844
import ROOT
1945
import matplotlib.pyplot as plt
2046
import mplhep as hep
47+
import numpy as np
2148

2249
# Create and fill a 1D histogram
2350
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)
2553

2654
# Load a style sheet and plot the histogram
2755
hep.style.use("LHCb2")
@@ -30,39 +58,16 @@ plt.title("MyHist")
3058
plt.show()
3159
```
3260

33-
\image html uhi_th1_plot.png width=600px
61+
For 2D histograms, use `hep.hist2dplot`:
3462

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):
4163
```python
42-
import ROOT
43-
import matplotlib.pyplot as plt
44-
import seaborn as sns
45-
import numpy as np
46-
4764
h2 = ROOT.TH2D("h2", "h2", 10, -1, 1, 10, -1, 1)
48-
h2[...] = np.random.rand(10, 10)
65+
h2.FillRandom("gaus", 10000)
4966

50-
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
51-
52-
# First subplot with imshow
53-
ax1.imshow(h2.values(), cmap='hot', interpolation='nearest')
54-
ax1.set_title("imshow")
55-
56-
# Second subplot with seaborn heatmap
57-
sns.heatmap(h2.values(), linewidth=0.5, ax=ax2)
58-
ax2.set_title("heatmap")
59-
60-
plt.tight_layout()
67+
hep.hist2dplot(h2)
6168
plt.show()
6269
```
6370

64-
\image html uhi_th2_plot.png width=800px
65-
6671
\anchor indexing
6772
# Indexing
6873

@@ -154,4 +159,21 @@ v = h[underflow, underflow]
154159
- ex. `h_projected = h[:, ::sum, ::sum]` --> `h_projected` is a 1D histogram representing the y and z projections along the x axis.
155160
- **Setting operations**
156161
- 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)
178+
h_hist = hist.Hist(ir)
179+
```

bindings/pyroot/pythonizations/python/ROOT/_pythonization/dataloader.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
\defgroup Py_ML Machine Learning Training
1+
\defgroup Py_ML RDataLoader
22
\ingroup Python
33
\brief Feed ROOT data directly into models for machine learning training.
44

5-
# RDataLoader
65

76
`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.
87

98
\note `RDataLoader` is part of `ROOT.Experimental.ML` and is currently experimental. The API may change between ROOT releases.
109

1110
## Cheat Sheet
1211

13-
A one-page quick reference covering the full API.
12+
A one-page quick reference covering the API.
1413

1514
\htmlonly
1615
<object data="rdataloader-cheatsheet.pdf"

documentation/doxygen/Doxyfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,7 @@ HTML_EXTRA_STYLESHEET = ROOT.css
14741474
HTML_EXTRA_FILES = ./rootlogo_s.gif \
14751475
./notebook.gif \
14761476
./cheatsheets/rdataloader-cheatsheet.pdf
1477+
./cheatsheets/uhi-cheatsheet.pdf
14771478

14781479
# The HTML_COLORSTYLE tag can be used to specify if the generated HTML output
14791480
# should be rendered with a dark or light theme.

documentation/doxygen/Doxyfile_preview

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,7 @@ HTML_EXTRA_STYLESHEET = ROOT.css
14881488
HTML_EXTRA_FILES = ./rootlogo_s.gif \
14891489
./notebook.gif \
14901490
./cheatsheets/rdataloader-cheatsheet.pdf
1491+
./cheatsheets/uhi-cheatsheet.pdf
14911492

14921493
# The HTML_COLORSTYLE tag can be used to specify if the generated HTML output
14931494
# should be rendered with a dark or light theme.
1.73 MB
Binary file not shown.
3.29 MB
Binary file not shown.

0 commit comments

Comments
 (0)