Skip to content

Commit

Permalink
Add ScaledBy AO
Browse files Browse the repository at this point in the history
Closes: #28
  • Loading branch information
APN-Pucky committed Oct 13, 2024
1 parent 67f4912 commit 3d5858c
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 8 deletions.
89 changes: 87 additions & 2 deletions debug/.ipynb_checkpoints/create_yoda2_tests-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,91 @@
{
"cells": [],
"metadata": {},
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "afa242f1-a982-4ae5-9915-73151f0adf19",
"metadata": {},
"outputs": [],
"source": [
"import yoda"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2e702678-f4ca-4f6c-83b5-605075d6c2db",
"metadata": {},
"outputs": [],
"source": [
"h =yoda.Histo1D(10, 0, 10, title=\"test\")\n",
"for i in range(-2,12,1):\n",
" for _ in range(abs(i)):\n",
" h.fill(i)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "65f5d336-baec-44a8-a844-93d758f68568",
"metadata": {},
"outputs": [],
"source": [
"yoda.write([h],\"test_histo1d_v2.yoda\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "549d08c6-7f81-4994-8dc9-2459f744a14c",
"metadata": {},
"outputs": [],
"source": [
"h =yoda.Histo2D(10, 0, 10,10, 0, 10, title=\"test\")\n",
"w=0\n",
"for i in range(-2,12,1):\n",
" for j in range(-2,12,1):\n",
" w+=1\n",
" h.fill(i,j,w)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a1948b81-557b-4b0f-86b6-e1a9f2db00e0",
"metadata": {},
"outputs": [],
"source": [
"yoda.write([h],\"test_histo2d_v2.yoda\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03172e6a-b354-460b-9e77-a7b4da24ed2a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
6 changes: 5 additions & 1 deletion src/babyyoda/grogu/analysis_object.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from typing import Optional


@dataclass
Expand All @@ -8,7 +9,10 @@ class GROGU_ANALYSIS_OBJECT:
d_type: str = ""
d_title: str = ""
d_path: str = "/"
# d_scaled_by: float = 1.0 #TODO maybe we want to track ScaledBy in the future
d_scaled_by: Optional[float] = (
1.0 # TODO maybe we want to track ScaledBy in the future
)
# TODO how do I access anotation in YODA python interface for same scaledby treatment

############################################
# YODA compatibilty code
Expand Down
11 changes: 10 additions & 1 deletion src/babyyoda/grogu/histo1d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def clone(self):
return GROGU_HISTO1D_V2(
d_key=self.d_key,
d_path=self.d_path,
d_scaled_by=self.d_scaled_by,
d_title=self.d_title,
d_bins=[b.clone() for b in self.d_bins],
d_underflow=self.d_underflow,
Expand Down Expand Up @@ -271,9 +272,13 @@ def rebinXTo(self, edges: list[float]):

def to_string(histo) -> str:
"""Convert a YODA_HISTO1D_V2 object to a formatted string."""
scale = (
"" if histo.d_scaled_by == 1.0 else f"ScaledBy: {histo.d_scaled_by:.17e}\n"
)
header = (
f"BEGIN YODA_HISTO1D_V2 {histo.d_key}\n"
f"Path: {histo.d_path}\n"
f"{scale}"
f"Title: {histo.d_title}\n"
f"Type: Histo1D\n"
"---\n"
Expand All @@ -291,7 +296,7 @@ def to_string(histo) -> str:
# Add the bin data
bin_data = "\n".join(b.to_string() for b in histo.bins())

footer = "END YODA_HISTO1D_V2\n"
footer = "END YODA_HISTO1D_V2"

return f"{header}{stats}{xlegend}{total}\n{underflow}\n{overflow}\n{legend}{bin_data}\n{footer}"

Expand All @@ -305,11 +310,14 @@ def from_string(cls, file_content: str) -> "GROGU_HISTO1D_V2":
# Extract metadata (path, title)
path = ""
title = ""
scaled_by = 1.0
for line in lines:
if line.startswith("Path:"):
path = line.split(":")[1].strip()
elif line.startswith("Title:"):
title = line.split(":")[1].strip()
elif line.startswith("ScaledBy:"):
scaled_by = float(line.split(":")[1].strip())
elif line.startswith("---"):
break

Expand Down Expand Up @@ -347,6 +355,7 @@ def from_string(cls, file_content: str) -> "GROGU_HISTO1D_V2":
d_key=key,
d_path=path,
d_title=title,
d_scaled_by=scaled_by,
d_bins=bins,
d_underflow=underflow,
d_total=total,
Expand Down
13 changes: 11 additions & 2 deletions src/babyyoda/grogu/histo1d_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __add__(self, other):

def to_string(self) -> str:
"""Convert a Histo1DBin object to a formatted string."""
return f"{self.d_sumw:<13.6e}\t{self.d_sumw2:<13.6e}\t{self.d_sumwx:<13.6e}\t{self.d_sumwx2:<13.6e}\t{self.d_numentries:<13.6e}"
return f"{self.d_sumw:<13.6e}\t{self.d_sumw2:<13.6e}\t{self.d_sumwx:<13.6e}\t{self.d_sumwx2:<13.6e}\t{self.d_numentries:<13.6e}".strip()

@classmethod
def from_string(cls, string: str) -> "GROGU_HISTO1D_V3.Bin":
Expand All @@ -159,6 +159,7 @@ def clone(self):
return GROGU_HISTO1D_V3(
d_key=self.d_key,
d_path=self.d_path,
d_scaled_by=self.d_scaled_by,
d_title=self.d_title,
d_edges=copy.deepcopy(self.d_edges),
d_bins=[b.clone() for b in self.d_bins],
Expand Down Expand Up @@ -241,11 +242,14 @@ def from_string(cls, file_content: str) -> "GROGU_HISTO1D_V3":
# Extract metadata (path, title)
path = ""
title = ""
scaled_by = 1.0
for line in lines:
if line.startswith("Path:"):
path = line.split(":")[1].strip()
elif line.startswith("Title:"):
title = line.split(":")[1].strip()
elif line.startswith("ScaledBy:"):
scaled_by = float(line.split(":")[1].strip())
elif line.startswith("---"):
break

Expand Down Expand Up @@ -279,16 +283,21 @@ def from_string(cls, file_content: str) -> "GROGU_HISTO1D_V3":
return cls(
d_key=key,
d_path=path,
d_scaled_by=scaled_by,
d_title=title,
d_bins=bins,
d_edges=edges,
)

def to_string(self):
"""Convert a YODA_HISTO1D_V3 object to a formatted string."""
scale = (
"" if self.d_scaled_by == 1.0 else f"ScaledBy: {self.d_scaled_by:.17e}\n"
)
header = (
f"BEGIN YODA_HISTO1D_V3 {self.d_key}\n"
f"Path: {self.d_path}\n"
f"{scale}"
f"Title: {self.d_title}\n"
f"Type: Histo1D\n"
"---\n"
Expand All @@ -301,6 +310,6 @@ def to_string(self):
# Add the bin data
bin_data = "\n".join(GROGU_HISTO1D_V3.Bin.to_string(b) for b in self.bins(True))

footer = "END YODA_HISTO1D_V3\n"
footer = "END YODA_HISTO1D_V3"

return f"{header}{stats}{edges}# sumW \tsumW2 \tsumW(A1) \tsumW2(A1) \tnumEntries\n{bin_data}\n{footer}"
9 changes: 9 additions & 0 deletions src/babyyoda/grogu/histo2d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def clone(self):
return GROGU_HISTO2D_V2(
d_key=self.d_key,
d_path=self.d_path,
d_scaled_by=self.d_scaled_by,
d_title=self.d_title,
d_bins=[b.clone() for b in self.d_bins],
d_total=self.d_total.clone(),
Expand Down Expand Up @@ -213,9 +214,13 @@ def binAt(self, x, y):

def to_string(self) -> str:
"""Convert a YODA_HISTO2D_V2 object to a formatted string."""
scale = (
"" if self.d_scaled_by == 1.0 else f"ScaledBy: {self.d_scaled_by:.17e}\n"
)
header = (
f"BEGIN YODA_HISTO2D_V2 {self.d_key}\n"
f"Path: {self.d_path}\n"
f"{scale}"
f"Title: {self.d_title}\n"
f"Type: {self.d_type}\n"
f"---\n"
Expand Down Expand Up @@ -248,11 +253,14 @@ def from_string(cls, file_content: str) -> "GROGU_HISTO2D_V2":
# Extract metadata (path, title)
path = ""
title = ""
scaled_by = 1.0
for line in lines:
if line.startswith("Path:"):
path = line.split(":")[1].strip()
elif line.startswith("Title:"):
title = line.split(":")[1].strip()
elif line.startswith("ScaledBy:"):
scaled_by = float(line.split(":")[1].strip())
elif line.startswith("---"):
break

Expand Down Expand Up @@ -326,6 +334,7 @@ def from_string(cls, file_content: str) -> "GROGU_HISTO2D_V2":
return cls(
d_key=key,
d_path=path,
d_scaled_by=scaled_by,
d_title=title,
d_bins=bins,
d_total=total,
Expand Down
13 changes: 11 additions & 2 deletions src/babyyoda/grogu/histo2d_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def to_string(self) -> str:
return (
f"{self.d_sumw:<13.6e}\t{self.d_sumw2:<13.6e}\t{self.d_sumwx:<13.6e}\t{self.d_sumwx2:<13.6e}\t"
f"{self.d_sumwy:<13.6e}\t{self.d_sumwy2:<13.6e}\t{self.d_sumwxy:<13.6e}\t{self.d_numentries:<13.6e}"
)
).strip()

@classmethod
def from_string(cls, line: str) -> "GROGU_HISTO2D_V3.Bin":
Expand Down Expand Up @@ -135,6 +135,7 @@ def clone(self):
return GROGU_HISTO2D_V3(
d_key=self.d_key,
d_path=self.d_path,
d_scaled_by=self.d_scaled_by,
d_title=self.d_title,
d_bins=[b.clone() for b in self.d_bins],
d_edges=copy.deepcopy(self.d_edges),
Expand Down Expand Up @@ -191,9 +192,13 @@ def bins(self, includeOverflows=False):

def to_string(self) -> str:
"""Convert a YODA_HISTO2D_V3 object to a formatted string."""
scale = (
"" if self.d_scaled_by == 1.0 else f"ScaledBy: {self.d_scaled_by:.17e}\n"
)
header = (
f"BEGIN YODA_HISTO2D_V3 {self.d_key}\n"
f"Path: {self.d_path}\n"
f"{scale}"
f"Title: {self.d_title}\n"
f"Type: {self.d_type}\n"
f"---\n"
Expand All @@ -211,7 +216,7 @@ def to_string(self) -> str:

legend = "# sumW \tsumW2 \tsumW(A1) \tsumW2(A1) \tsumW(A2) \tsumW2(A2) \tsumW(A1,A2) \tnumEntries\n"
bin_data = "\n".join(b.to_string() for b in self.d_bins)
footer = "\nEND YODA_HISTO2D_V3\n"
footer = "\nEND YODA_HISTO2D_V3"

return f"{header}{stats}{edges}{legend}{bin_data}{footer}"

Expand All @@ -224,11 +229,14 @@ def from_string(cls, file_content: str) -> "GROGU_HISTO2D_V3":
# Extract metadata (path, title)
path = ""
title = ""
scaled_by = 1.0
for line in lines:
if line.startswith("Path:"):
path = line.split(":")[1].strip()
elif line.startswith("Title:"):
title = line.split(":")[1].strip()
elif line.startswith("ScaledBy:"):
scaled_by = float(line.split(":")[1].strip())
elif line.startswith("---"):
break

Expand Down Expand Up @@ -262,6 +270,7 @@ def from_string(cls, file_content: str) -> "GROGU_HISTO2D_V3":
return cls(
d_key=key,
d_path=path,
d_scaled_by=scaled_by,
d_title=title,
d_bins=bins,
d_edges=edges,
Expand Down
1 change: 1 addition & 0 deletions tests/test_histo2d_v2.yoda
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BEGIN YODA_HISTO2D_V2 /ALICE_2020_I1797621/GGPdfXQ
Path: /ALICE_2020_I1797621/GGPdfXQ
ScaledBy: 1.00000000000000002e-02
Title: ~
Type: Histo2D
---
Expand Down

0 comments on commit 3d5858c

Please sign in to comment.