Skip to content

Commit 6ef1065

Browse files
committed
add classmethod and serialization to th2 and th3
1 parent 4b50481 commit 6ef1065

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_th2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ def _FillWithArrayTH2(self, *args):
8383
# Add UHI plotting features
8484
pythonization(klass)(_add_plotting_features)
8585

86+
# Add serialization features
87+
from ROOT._pythonization._uhi import _add_serialization_features
88+
89+
pythonization(klass)(_add_serialization_features)
90+
8691
# Support vectorized Fill
8792
@pythonization(klass)
8893
def _enable_numpy_fill(klass):

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_th3.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
# For the list of contributors see $ROOTSYS/README/CREDITS. #
99
################################################################################
1010

11-
from . import pythonization
1211
from ROOT._pythonization._memory_utils import inject_constructor_releasing_ownership
1312

13+
from . import pythonization
1414

1515
# The constructors need to be pythonized for each derived class separately:
1616
_th3_derived_classes_to_pythonize = [
@@ -31,3 +31,8 @@
3131

3232
# Add UHI plotting features
3333
pythonization(klass)(_add_plotting_features)
34+
35+
# Add serialization features
36+
from ROOT._pythonization._uhi import _add_serialization_features
37+
38+
pythonization(klass)(_add_serialization_features)

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,25 @@ def _TH1_Constructor(self, *args, **kwargs):
7777
else forward to the original
7878
"""
7979
if len(args) == 1 and isinstance(args[0], dict):
80-
from .serialization import _from_uhi_
80+
from .serialization import _get_ctor_args, _set_histogram_storage_from_dict
81+
82+
# get constructor arguments
83+
uhi_dict = args[0]
84+
ctor_args = _get_ctor_args(self, uhi_dict)
85+
# call original constructor
86+
self._original_init_(*ctor_args)
87+
# set storage
88+
_set_histogram_storage_from_dict(self, uhi_dict["storage"])
8189

82-
_from_uhi_(self, args[0])
8390
else:
8491
self._original_init_(*args, **kwargs)
8592

8693

8794
def _add_serialization_features(klass: Any) -> None:
88-
from .serialization import _to_uhi_
95+
from .serialization import _from_uhi_, _to_uhi_
8996

9097
klass._to_uhi_ = _to_uhi_
98+
klass._from_uhi_ = classmethod(_from_uhi_)
9199

92100
klass._original_init_ = klass.__init__
93101
klass.__init__ = _TH1_Constructor

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ def _storage_to_dict(hist: Any) -> dict[str, Any]:
9292
return storage_dict
9393

9494

95+
def _get_ctor_args(self, uhi_dict: dict[str, Any]) -> ROOT.TH1:
96+
# rebuild axes
97+
axes = uhi_dict["axes"]
98+
axes_specs = [_axis_from_dict(axis_dict) for axis_dict in axes]
99+
100+
# constructor arguments
101+
# TODO give a unique name or disable gloval directory?
102+
ctor_args = ["h_uhi", "h_uhi"]
103+
for axis_spec in axes_specs:
104+
ctor_args.extend(axis_spec)
105+
106+
return ctor_args
107+
108+
95109
def _set_histogram_storage_from_dict(hist: Any, storage_dict: dict[str, Any]) -> None:
96110
"""
97111
Set the histogram storage (values and statistics) from the given storage dictionary.
@@ -109,17 +123,17 @@ def _to_uhi_(self) -> dict[str, Any]:
109123
}
110124

111125

112-
def _from_uhi_(self, uhi_dict: dict[str, Any]) -> ROOT.TH1:
113-
# rebuild axes
114-
axes = uhi_dict["axes"]
115-
axes_specs = [_axis_from_dict(axis_dict) for axis_dict in axes]
126+
def _from_uhi_(cls, uhi_dict: dict[str, Any]) -> ROOT.TH1:
127+
# get constructor arguments
128+
ctor_args = _get_ctor_args(cls, uhi_dict)
116129

117-
# construct the histogram
118-
ctor_args = ["h_uhi", "h_uhi"]
119-
for axis_spec in axes_specs:
120-
ctor_args.extend(axis_spec)
130+
# allocate instance
131+
self = cls.__new__(cls)
121132

133+
# call original constructor
122134
self._original_init_(*ctor_args)
123135

124136
# set storage
125137
_set_histogram_storage_from_dict(self, uhi_dict["storage"])
138+
139+
return self

0 commit comments

Comments
 (0)