Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,16 @@ def _FillWithArrayTH1(self, *args):
Raises:
- ValueError: If weights length doesn't match data length
"""
import collections.abc

# If the first argument has no len() method, we don't even need to consider
# the array code path.
if not isinstance(args[0], collections.abc.Sized):
return self._Fill(*args)

import numpy as np

data = np.asanyarray(args[0], dtype=np.float64)
n = len(data)
# Try to convert the first argument to a numpy array
# if it fails, we call the original Fill
try:
data = np.asanyarray(args[0], dtype=np.float64)
n = len(data)
except Exception:
# Not convertible
return self._Fill(*args)

if len(args) >= 2 and args[1] is not None:
weights = np.asanyarray(args[1], dtype=np.float64)
Expand Down
24 changes: 11 additions & 13 deletions bindings/pyroot/pythonizations/python/ROOT/_pythonization/_th2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,23 @@ def _FillWithArrayTH2(self, *args):
Raises:
- ValueError: If x, y, and/or weights do not have matching lengths
"""
import collections.abc

# If the first argument has no len() method, we don't even need to consider
# the array code path.
if not isinstance(args[0], collections.abc.Sized):
return self._Fill(*args)

# If there are less than 2 arguments, cannot do vectorized Fill
if len(args) < 2:
raise ValueError("TH2.Fill requires at least x and y array-like arguments")
return self._Fill(*args)

import numpy as np

x = np.asanyarray(args[0], dtype=np.float64)
y = np.asanyarray(args[1], dtype=np.float64)
try:
x = np.asanyarray(args[0], dtype=np.float64)
y = np.asanyarray(args[1], dtype=np.float64)

if len(x) != len(y):
raise ValueError(f"Length mismatch: x length ({len(x)}) != y length ({len(y)})")
if len(x) != len(y):
raise ValueError(f"Length mismatch: x length ({len(x)}) != y length ({len(y)})")

n = len(x)
n = len(x)
except Exception:
# Not convertible
return self._Fill(*args)

if len(args) >= 3 and args[2] is not None:
weights = np.asanyarray(args[2], dtype=np.float64)
Expand Down
8 changes: 8 additions & 0 deletions bindings/pyroot/pythonizations/test/th1_fillN.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
with self.subTest(input_type=type(input_data).__name__):
self._run_fill_test(input_data)

# Test filling with a string argument
def test_fill_string_argument(self):
h = ROOT.TH1D("h", "h", 5, 0, 5)
try:
h.Fill("a", 1)
except Exception as e:

Check failure on line 64 in bindings/pyroot/pythonizations/test/th1_fillN.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F841)

bindings/pyroot/pythonizations/test/th1_fillN.py:64:29: F841 Local variable `e` is assigned to but never used
self.fail()


if __name__ == "__main__":
unittest.main()
Loading