Skip to content

Commit

Permalink
Test grogu clone
Browse files Browse the repository at this point in the history
Closes: #16
  • Loading branch information
APN-Pucky committed Oct 13, 2024
1 parent 485d384 commit 49863b7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 24 deletions.
14 changes: 1 addition & 13 deletions src/babyyoda/grogu/histo1d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def __post_init__(self):
# YODA compatibilty code
########################################################

# TODO drop either clone or copy
def clone(self):
return GROGU_HISTO1D_V2.Bin(
d_xmin=self.d_xmin,
Expand All @@ -38,17 +37,6 @@ def clone(self):
d_numentries=self.d_numentries,
)

def copy(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 fill(self, x: float, weight: float = 1.0, fraction: float = 1.0) -> bool:
# if (self.d_xmin is None or x > self.d_xmin) and (self.d_xmax is None or x < self.d_xmax):
sf = fraction * weight
Expand Down Expand Up @@ -213,7 +201,7 @@ def clone(self):
d_key=self.d_key,
d_path=self.d_path,
d_title=self.d_title,
d_bins=[b.copy() for b in self.d_bins],
d_bins=[b.clone() for b in self.d_bins],
d_underflow=self.d_underflow,
d_overflow=self.d_overflow,
)
Expand Down
14 changes: 3 additions & 11 deletions src/babyyoda/grogu/histo1d_v3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import re
from typing import List
from dataclasses import dataclass, field
Expand Down Expand Up @@ -28,15 +29,6 @@ def clone(self):
d_numentries=self.d_numentries,
)

def copy(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 fill(self, x: float, weight: float = 1.0, fraction: float = 1.0) -> bool:
sf = fraction * weight
self.d_sumw += sf
Expand Down Expand Up @@ -168,8 +160,8 @@ def clone(self):
d_key=self.d_key,
d_path=self.d_path,
d_title=self.d_title,
d_edges=self.d_edges.copy(),
d_bins=[b.copy() for b in self.d_bins],
d_edges=copy.deepcopy(self.d_edges),
d_bins=[b.clone() for b in self.d_bins],
)

def underflow(self):
Expand Down
26 changes: 26 additions & 0 deletions src/babyyoda/grogu/histo2d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ class Bin:
# YODA compatibilty code
########################################################

def clone(self):
return GROGU_HISTO2D_V2.Bin(
d_xmin=self.d_xmin,
d_xmax=self.d_xmax,
d_ymin=self.d_ymin,
d_ymax=self.d_ymax,
d_sumw=self.d_sumw,
d_sumw2=self.d_sumw2,
d_sumwx=self.d_sumwx,
d_sumwx2=self.d_sumwx2,
d_sumwy=self.d_sumwy,
d_sumwy2=self.d_sumwy2,
d_sumwxy=self.d_sumwxy,
d_numentries=self.d_numentries,
)

def fill(self, x: float, y: float, weight: float = 1.0, fraction=1.0):
sf = fraction * weight
self.d_sumw += sf
Expand Down Expand Up @@ -127,6 +143,16 @@ def __post_init__(self):
# YODA compatibilty code
#

def clone(self):
return GROGU_HISTO2D_V2(
d_key=self.d_key,
d_path=self.d_path,
d_title=self.d_title,
d_bins=[b.clone() for b in self.d_bins],
d_underflow=self.d_underflow,
d_overflow=self.d_overflow,
)

def fill(self, x, y, weight=1.0, fraction=1.0):
for b in self.d_bins:
if b.d_xmin <= x < b.d_xmax and b.d_ymin <= y < b.d_ymax:
Expand Down
22 changes: 22 additions & 0 deletions src/babyyoda/grogu/histo2d_v3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import re
from dataclasses import dataclass, field
import sys
Expand All @@ -21,6 +22,18 @@ class Bin:
d_sumwxy: float = 0.0
d_numentries: float = 0.0

def clone(self):
return GROGU_HISTO2D_V3.Bin(
d_sumw=self.d_sumw,
d_sumw2=self.d_sumw2,
d_sumwx=self.d_sumwx,
d_sumwx2=self.d_sumwx2,
d_sumwy=self.d_sumwy,
d_sumwy2=self.d_sumwy2,
d_sumwxy=self.d_sumwxy,
d_numentries=self.d_numentries,
)

def fill(self, x: float, y: float, weight: float = 1.0, fraction=1.0):
sf = fraction * weight
self.d_sumw += sf
Expand Down Expand Up @@ -118,6 +131,15 @@ def __post_init__(self):
# YODA compatibilty code
#

def clone(self):
return GROGU_HISTO2D_V3(
d_key=self.d_key,
d_path=self.d_path,
d_title=self.d_title,
d_bins=[b.clone() for b in self.d_bins],
d_edges=copy.deepcopy(self.d_edges),
)

def xEdges(self):
return self.d_edges[0]

Expand Down
30 changes: 30 additions & 0 deletions tests/grogu/test_gg_clone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import babyyoda.grogu as yoda
from babyyoda.grogu.analysis_object import GROGU_ANALYSIS_OBJECT


def test_gg_clone_hist1d_v2():
h1 = next(iter(yoda.read("tests/test_histo1d_v2.yoda").values()))
h2 = h1.clone()
assert GROGU_ANALYSIS_OBJECT.__eq__(h1, h2)
assert h1 == h2


def test_gg_clone_hist1d_v3():
h1 = next(iter(yoda.read("tests/test_histo1d_v3.yoda").values()))
h2 = h1.clone()
assert GROGU_ANALYSIS_OBJECT.__eq__(h1, h2)
assert h1 == h2


def test_gg_clone_hist2d_v2():
h1 = next(iter(yoda.read("tests/test_histo2d_v2.yoda").values()))
h2 = h1.clone()
assert GROGU_ANALYSIS_OBJECT.__eq__(h1, h2)
assert h1 == h2


def test_gg_clone_hist2d_v3():
h1 = next(iter(yoda.read("tests/test_histo2d_v3.yoda").values()))
h2 = h1.clone()
assert GROGU_ANALYSIS_OBJECT.__eq__(h1, h2)
assert h1 == h2

0 comments on commit 49863b7

Please sign in to comment.