diff --git a/debug/YODA2.ipynb b/debug/YODA2.ipynb index e1960f8..9fff582 100644 --- a/debug/YODA2.ipynb +++ b/debug/YODA2.ipynb @@ -150,10 +150,96 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "b416531d-ce1d-4ec1-b4c9-f6ccc47d0ad5", "metadata": {}, "outputs": [], + "source": [ + "h = yoda.Histo1D(10, 0, 10)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "fd05f306-67bd-462f-90f0-b3c759fb4a22", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "h.xEdges()" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "0e7c5762-e776-4ec2-89b0-b046468eb6a7", + "metadata": {}, + "outputs": [], + "source": [ + "h.rebinXBy(3, begin=2, end=7)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "49d76a40-8a00-427b-a1e9-21bb5711b9a1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0.0, 1.0, 4.0, 7.0, 8.0, 9.0, 10.0]" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "h.xEdges()" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "4ed3f7f5-6805-4d5b-8569-15a23c514a21", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([, ,\n", + " , ,\n", + " , ],\n", + " dtype=object)" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "h.bins()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "364ee9b2-0c6e-4bb6-a932-cc36f32bcf1f", + "metadata": {}, + "outputs": [], "source": [] } ], diff --git a/src/babyyoda/grogu/histo1d_v2.py b/src/babyyoda/grogu/histo1d_v2.py index 8385647..3ef2906 100644 --- a/src/babyyoda/grogu/histo1d_v2.py +++ b/src/babyyoda/grogu/histo1d_v2.py @@ -21,6 +21,18 @@ class Bin: # YODA compatibilty code ######################################################## + # TODO drop either clone or copy + def clone(self): + return GROGU_HISTO1D_V2.Bin( + d_xmin=self.d_xmin, + d_xmax=self.d_xmax, + d_sumw=self.d_sumw, + d_sumw2=self.d_sumw2, + d_sumwx=self.d_sumwx, + d_sumwx2=self.d_sumwx2, + d_numentries=self.d_numentries, + ) + def copy(self): return GROGU_HISTO1D_V2.Bin( d_xmin=self.d_xmin, @@ -237,24 +249,39 @@ def binAt(self, x): def binDim(self): return 1 - def rebinXBy(self, factor: int, start=None, stop=None): + def rebinXBy(self, factor: int, begin=1, end=None): + start = begin - 1 + stop = end # TODO what about not fitting start stop with factor?! - if start is None: - start = 0 if stop is None: stop = len(self.bins()) + else: + stop = stop - 1 + new_bins = [] + for i in range(0, start): + print("start") + new_bins.append(self.bins()[i].clone()) + last = None for i in range(start, stop, factor): - nb = GROGU_HISTO1D_V2.Bin( - d_xmin=self.bins()[i].xMin(), d_xmax=self.bins()[i].xMax() - ) - for j in range(0, factor): - nb += self.bins()[i + j] - nb.d_xmin = min(nb.d_xmin, self.bins()[i + j].xMin()) - nb.d_xmax = max(nb.d_xmax, self.bins()[i + j].xMax()) + print(f"i: {i}, factor: {factor}, len: {len(self.bins())}") + if i + factor <= len(self.bins()): + nb = GROGU_HISTO1D_V2.Bin( + d_xmin=self.bins()[i].xMin(), d_xmax=self.bins()[i].xMax() + ) + for j in range(0, factor): + last = i + j + nb += self.bins()[i + j] + nb.d_xmin = min(nb.d_xmin, self.bins()[i + j].xMin()) + nb.d_xmax = max(nb.d_xmax, self.bins()[i + j].xMax()) + new_bins.append(nb) + for j in range(last + 1, len(self.bins())): + print(f"stop {i}") + new_bins.append(self.bins()[j].clone()) - new_bins.append(nb) self.d_bins = new_bins + + assert len(self.d_bins) == len(self.xEdges()) - 1 # return self # not inplace # return GROGU_HISTO1D_V2( diff --git a/src/babyyoda/grogu/histo1d_v3.py b/src/babyyoda/grogu/histo1d_v3.py index 8b13a97..1329674 100644 --- a/src/babyyoda/grogu/histo1d_v3.py +++ b/src/babyyoda/grogu/histo1d_v3.py @@ -19,6 +19,15 @@ class Bin: # YODA compatibilty code ######################################################## + def clone(self): + return GROGU_HISTO1D_V3.Bin( + d_sumw=self.d_sumw, + d_sumw2=self.d_sumw2, + d_sumwx=self.d_sumwx, + d_sumwx2=self.d_sumwx2, + d_numentries=self.d_numentries, + ) + def copy(self): return GROGU_HISTO1D_V3.Bin( d_sumw=self.d_sumw, @@ -195,26 +204,41 @@ def binAt(self, x): def binDim(self): return 1 - def rebinXBy(self, factor: int, start=None, stop=None): + def rebinXBy(self, factor: int, begin=1, end=None): + start = begin - 1 + stop = end if start is None: start = 0 if stop is None: stop = len(self.bins()) + else: + stop = stop - 1 new_edges = [] new_bins = [] new_bins += [self.underflow()] + for i in range(0, start): + new_bins.append(self.bins()[i].clone()) + new_edges.append(self.xEdges()[i]) + new_edges.append(self.xEdges()[i + 1]) + last = None for i in range(start, stop, factor): - xmin = self.xEdges()[i] - xmax = self.xEdges()[i + 1] - nb = GROGU_HISTO1D_V3.Bin() - for j in range(0, factor): - nb += self.bins()[i + j] - xmin = min(xmin, self.xEdges()[i + j]) - xmax = max(xmax, self.xEdges()[i + j + 1]) - new_bins.append(nb) - # add both edges - new_edges.append(xmin) - new_edges.append(xmax) + if i + factor <= len(self.bins()): + xmin = self.xEdges()[i] + xmax = self.xEdges()[i + 1] + nb = GROGU_HISTO1D_V3.Bin() + for j in range(0, factor): + last = i + j + nb += self.bins()[i + j] + xmin = min(xmin, self.xEdges()[i + j]) + xmax = max(xmax, self.xEdges()[i + j + 1]) + new_bins.append(nb) + # add both edges + new_edges.append(xmin) + new_edges.append(xmax) + for j in range(last + 1, len(self.bins())): + new_bins.append(self.bins()[j].clone()) + new_edges.append(self.xEdges()[j]) + new_edges.append(self.xEdges()[j + 1]) new_bins += [self.overflow()] # drop duplicate edges self.d_edges = list(set(new_edges)) diff --git a/src/babyyoda/test.py b/src/babyyoda/test.py index 73ed470..1c5ca93 100644 --- a/src/babyyoda/test.py +++ b/src/babyyoda/test.py @@ -62,10 +62,10 @@ def assert_value1d(gb, yb): def assert_equal_histo1d(gh1, yh1): assert_ao(gh1, yh1) - assert len(gh1.bins()) == len(yh1.bins()), f"{gh1.bins()} != {yh1.bins()}" + assert len(gh1.bins()) == len(yh1.bins()), f"{len(gh1.bins())} != {len(yh1.bins())}" for ge, ye in zip(gh1.xEdges(), yh1.xEdges()): - assert ge == ye + assert ge == ye, f"{gh1.xEdges()} != {yh1.xEdges()}" for gb, yb in zip(gh1.bins(), yh1.bins()): assert_value1d(gb, yb) diff --git a/tests/babyyoda/test_histo1d.py b/tests/babyyoda/test_histo1d.py index 520bca5..1318980 100644 --- a/tests/babyyoda/test_histo1d.py +++ b/tests/babyyoda/test_histo1d.py @@ -54,11 +54,11 @@ def test_histos_equal(factory1, factory2): "factory2", [grogu.Histo1D, grogu.Histo1D_v2, grogu.Histo1D_v3, yoda.Histo1D] ) def test_histos_rebinby(factory1, factory2): - h1 = create_histo(factory1) - h2 = create_histo(factory2) + o1 = create_histo(factory1) + o2 = create_histo(factory2) - o1 = h1.clone() - o2 = h2.clone() + h1 = o1.clone() + h2 = o2.clone() h1.rebinBy(2) h2.rebinBy(2) @@ -70,3 +70,19 @@ def test_histos_rebinby(factory1, factory2): assert_equal_histo1d(o2, h2) assert_equal_histo1d(h1, h2) + + h1 = o1.clone() + h2 = o2.clone() + + h1.rebinBy(3, begin=2) + h2.rebinBy(3, begin=2) + + assert_equal_histo1d(h1, h2) + + h1 = o1.clone() + h2 = o2.clone() + + h1.rebinBy(3, begin=2, end=7) + h2.rebinBy(3, begin=2, end=7) + + assert_equal_histo1d(h1, h2)