Skip to content

Commit

Permalink
Speed up YODA
Browse files Browse the repository at this point in the history
  • Loading branch information
APN-Pucky committed Nov 16, 2024
1 parent 713ee0b commit c7a88a3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 22 deletions.
42 changes: 33 additions & 9 deletions examples/interface/uproot/histo1d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{
"data": {
"text/plain": [
"{'/': GROGU_HISTO1D_V3(d_annotations={'Path': '/', 'Title': 'test', 'Type': 'Histo1D'}, d_key='/', d_edges=[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0], d_bins=[GROGU_HISTO1D_V3.Bin(d_sumw=3.0, d_sumw2=3.0, d_sumwx=-5.0, d_sumwx2=9.0, d_numentries=3.0), GROGU_HISTO1D_V3.Bin(d_sumw=0.0, d_sumw2=0.0, d_sumwx=0.0, d_sumwx2=0.0, d_numentries=0.0), GROGU_HISTO1D_V3.Bin(d_sumw=1.0, d_sumw2=1.0, d_sumwx=1.0, d_sumwx2=1.0, d_numentries=1.0), GROGU_HISTO1D_V3.Bin(d_sumw=2.0, d_sumw2=2.0, d_sumwx=4.0, d_sumwx2=8.0, d_numentries=2.0), GROGU_HISTO1D_V3.Bin(d_sumw=3.0, d_sumw2=3.0, d_sumwx=9.0, d_sumwx2=27.0, d_numentries=3.0), GROGU_HISTO1D_V3.Bin(d_sumw=4.0, d_sumw2=4.0, d_sumwx=16.0, d_sumwx2=64.0, d_numentries=4.0), GROGU_HISTO1D_V3.Bin(d_sumw=5.0, d_sumw2=5.0, d_sumwx=25.0, d_sumwx2=125.0, d_numentries=5.0), GROGU_HISTO1D_V3.Bin(d_sumw=6.0, d_sumw2=6.0, d_sumwx=36.0, d_sumwx2=216.0, d_numentries=6.0), GROGU_HISTO1D_V3.Bin(d_sumw=7.0, d_sumw2=7.0, d_sumwx=49.0, d_sumwx2=343.0, d_numentries=7.0), GROGU_HISTO1D_V3.Bin(d_sumw=8.0, d_sumw2=8.0, d_sumwx=64.0, d_sumwx2=512.0, d_numentries=8.0), GROGU_HISTO1D_V3.Bin(d_sumw=9.0, d_sumw2=9.0, d_sumwx=81.0, d_sumwx2=729.0, d_numentries=9.0), GROGU_HISTO1D_V3.Bin(d_sumw=21.0, d_sumw2=21.0, d_sumwx=221.0, d_sumwx2=2331.0, d_numentries=21.0)])}"
"{'/': <babyyoda.yoda.histo1d.Histo1D at 0x7f7e77957bf0>}"
]
},
"execution_count": 2,
Expand All @@ -34,6 +34,30 @@
"h"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "ad961761-a98c-4388-a41c-03bd135e55aa",
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mh\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m/\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbin\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32minclude/generated/BinnedHisto1D.pyx:280\u001b[0m, in \u001b[0;36myoda.core.BinnedHisto1D.bin\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32minclude/generated/BinnedHisto1D.pyx:149\u001b[0m, in \u001b[0;36myoda.core.BinnedHisto1D.localToGlobalIndex\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"h[\"/\"].bin(0, 1)"
]
},
{
"cell_type": "code",
"execution_count": 4,
Expand All @@ -42,17 +66,17 @@
"outputs": [
{
"ename": "AttributeError",
"evalue": "'list' object has no attribute 'edges'",
"evalue": "'UHIAxis' object has no attribute 'edges'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m uproot\u001b[38;5;241m.\u001b[39mrecreate(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnew-file.root\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m file:\n\u001b[0;32m----> 2\u001b[0m \u001b[43mfile\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhist\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m h[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m'\u001b[39m]\n",
"File \u001b[0;32m/usr/lib/python3.13/site-packages/uproot/writing/writable.py:984\u001b[0m, in \u001b[0;36mWritableDirectory.__setitem__\u001b[0;34m(self, where, what)\u001b[0m\n\u001b[1;32m 982\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_file\u001b[38;5;241m.\u001b[39msink\u001b[38;5;241m.\u001b[39mclosed:\n\u001b[1;32m 983\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot write data to a closed file\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 984\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mupdate\u001b[49m\u001b[43m(\u001b[49m\u001b[43m{\u001b[49m\u001b[43mwhere\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhat\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m/usr/lib/python3.13/site-packages/uproot/writing/writable.py:1553\u001b[0m, in \u001b[0;36mWritableDirectory.update\u001b[0;34m(self, pairs, **more_pairs)\u001b[0m\n\u001b[1;32m 1550\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m item \u001b[38;5;129;01min\u001b[39;00m path:\n\u001b[1;32m 1551\u001b[0m directory \u001b[38;5;241m=\u001b[39m directory[item]\n\u001b[0;32m-> 1553\u001b[0m \u001b[43muproot\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwriting\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43midentify\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd_to_directory\u001b[49m\u001b[43m(\u001b[49m\u001b[43mv\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdirectory\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstreamers\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1555\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_file\u001b[38;5;241m.\u001b[39m_cascading\u001b[38;5;241m.\u001b[39mstreamers\u001b[38;5;241m.\u001b[39mupdate_streamers(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_file\u001b[38;5;241m.\u001b[39msink, streamers)\n",
"File \u001b[0;32m/usr/lib/python3.13/site-packages/uproot/writing/identify.py:155\u001b[0m, in \u001b[0;36madd_to_directory\u001b[0;34m(obj, name, directory, streamers)\u001b[0m\n\u001b[1;32m 152\u001b[0m tree\u001b[38;5;241m.\u001b[39mextend(data)\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 155\u001b[0m writable \u001b[38;5;241m=\u001b[39m \u001b[43mto_writable\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m rawstreamer \u001b[38;5;129;01min\u001b[39;00m writable\u001b[38;5;241m.\u001b[39mclass_rawstreamers:\n\u001b[1;32m 158\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(rawstreamer, \u001b[38;5;28mtuple\u001b[39m):\n",
"File \u001b[0;32m/usr/lib/python3.13/site-packages/uproot/writing/identify.py:336\u001b[0m, in \u001b[0;36mto_writable\u001b[0;34m(obj)\u001b[0m\n\u001b[1;32m 328\u001b[0m fEntries \u001b[38;5;241m=\u001b[39m data\u001b[38;5;241m.\u001b[39msum()\n\u001b[1;32m 330\u001b[0m \u001b[38;5;66;03m# convert all axes in one list comprehension\u001b[39;00m\n\u001b[1;32m 331\u001b[0m axes \u001b[38;5;241m=\u001b[39m [\n\u001b[1;32m 332\u001b[0m to_TAxis(\n\u001b[1;32m 333\u001b[0m fName\u001b[38;5;241m=\u001b[39mdefault_name,\n\u001b[1;32m 334\u001b[0m fTitle\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mgetattr\u001b[39m(axis, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlabel\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mgetattr\u001b[39m(obj, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m)),\n\u001b[1;32m 335\u001b[0m fNbins\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mlen\u001b[39m(axis),\n\u001b[0;32m--> 336\u001b[0m fXmin\u001b[38;5;241m=\u001b[39m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43medges\u001b[49m[\u001b[38;5;241m0\u001b[39m],\n\u001b[1;32m 337\u001b[0m fXmax\u001b[38;5;241m=\u001b[39maxis\u001b[38;5;241m.\u001b[39medges[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m],\n\u001b[1;32m 338\u001b[0m fXbins\u001b[38;5;241m=\u001b[39m_fXbins_maybe_regular(axis, boost_histogram),\n\u001b[1;32m 339\u001b[0m fLabels\u001b[38;5;241m=\u001b[39m_fLabels_maybe_categorical(axis, boost_histogram),\n\u001b[1;32m 340\u001b[0m )\n\u001b[1;32m 341\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m axis, default_name \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(obj\u001b[38;5;241m.\u001b[39maxes, [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mxaxis\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myaxis\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mzaxis\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m 342\u001b[0m ]\n\u001b[1;32m 344\u001b[0m \u001b[38;5;66;03m# make TH1, TH2, TH3 types independently\u001b[39;00m\n\u001b[1;32m 345\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(axes) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n",
"\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute 'edges'"
"Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m uproot\u001b[38;5;241m.\u001b[39mrecreate(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnew-file.root\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m file:\n\u001b[0;32m----> 2\u001b[0m \u001b[43mfile\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhist\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m h[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n",
"File \u001b[0;32m/usr/lib/python3.12/site-packages/uproot/writing/writable.py:984\u001b[0m, in \u001b[0;36mWritableDirectory.__setitem__\u001b[0;34m(self, where, what)\u001b[0m\n\u001b[1;32m 982\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_file\u001b[38;5;241m.\u001b[39msink\u001b[38;5;241m.\u001b[39mclosed:\n\u001b[1;32m 983\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot write data to a closed file\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 984\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mupdate\u001b[49m\u001b[43m(\u001b[49m\u001b[43m{\u001b[49m\u001b[43mwhere\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhat\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m/usr/lib/python3.12/site-packages/uproot/writing/writable.py:1553\u001b[0m, in \u001b[0;36mWritableDirectory.update\u001b[0;34m(self, pairs, **more_pairs)\u001b[0m\n\u001b[1;32m 1550\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m item \u001b[38;5;129;01min\u001b[39;00m path:\n\u001b[1;32m 1551\u001b[0m directory \u001b[38;5;241m=\u001b[39m directory[item]\n\u001b[0;32m-> 1553\u001b[0m \u001b[43muproot\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwriting\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43midentify\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd_to_directory\u001b[49m\u001b[43m(\u001b[49m\u001b[43mv\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdirectory\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstreamers\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1555\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_file\u001b[38;5;241m.\u001b[39m_cascading\u001b[38;5;241m.\u001b[39mstreamers\u001b[38;5;241m.\u001b[39mupdate_streamers(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_file\u001b[38;5;241m.\u001b[39msink, streamers)\n",
"File \u001b[0;32m/usr/lib/python3.12/site-packages/uproot/writing/identify.py:155\u001b[0m, in \u001b[0;36madd_to_directory\u001b[0;34m(obj, name, directory, streamers)\u001b[0m\n\u001b[1;32m 152\u001b[0m tree\u001b[38;5;241m.\u001b[39mextend(data)\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 155\u001b[0m writable \u001b[38;5;241m=\u001b[39m \u001b[43mto_writable\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m rawstreamer \u001b[38;5;129;01min\u001b[39;00m writable\u001b[38;5;241m.\u001b[39mclass_rawstreamers:\n\u001b[1;32m 158\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(rawstreamer, \u001b[38;5;28mtuple\u001b[39m):\n",
"File \u001b[0;32m/usr/lib/python3.12/site-packages/uproot/writing/identify.py:336\u001b[0m, in \u001b[0;36mto_writable\u001b[0;34m(obj)\u001b[0m\n\u001b[1;32m 328\u001b[0m fEntries \u001b[38;5;241m=\u001b[39m data\u001b[38;5;241m.\u001b[39msum()\n\u001b[1;32m 330\u001b[0m \u001b[38;5;66;03m# convert all axes in one list comprehension\u001b[39;00m\n\u001b[1;32m 331\u001b[0m axes \u001b[38;5;241m=\u001b[39m [\n\u001b[1;32m 332\u001b[0m to_TAxis(\n\u001b[1;32m 333\u001b[0m fName\u001b[38;5;241m=\u001b[39mdefault_name,\n\u001b[1;32m 334\u001b[0m fTitle\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mgetattr\u001b[39m(axis, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlabel\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mgetattr\u001b[39m(obj, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m)),\n\u001b[1;32m 335\u001b[0m fNbins\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mlen\u001b[39m(axis),\n\u001b[0;32m--> 336\u001b[0m fXmin\u001b[38;5;241m=\u001b[39m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43medges\u001b[49m[\u001b[38;5;241m0\u001b[39m],\n\u001b[1;32m 337\u001b[0m fXmax\u001b[38;5;241m=\u001b[39maxis\u001b[38;5;241m.\u001b[39medges[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m],\n\u001b[1;32m 338\u001b[0m fXbins\u001b[38;5;241m=\u001b[39m_fXbins_maybe_regular(axis, boost_histogram),\n\u001b[1;32m 339\u001b[0m fLabels\u001b[38;5;241m=\u001b[39m_fLabels_maybe_categorical(axis, boost_histogram),\n\u001b[1;32m 340\u001b[0m )\n\u001b[1;32m 341\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m axis, default_name \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(obj\u001b[38;5;241m.\u001b[39maxes, [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mxaxis\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myaxis\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mzaxis\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m 342\u001b[0m ]\n\u001b[1;32m 344\u001b[0m \u001b[38;5;66;03m# make TH1, TH2, TH3 types independently\u001b[39;00m\n\u001b[1;32m 345\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(axes) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n",
"\u001b[0;31mAttributeError\u001b[0m: 'UHIAxis' object has no attribute 'edges'"
]
}
],
Expand Down Expand Up @@ -86,7 +110,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.0"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
Binary file modified examples/interface/uproot/new-file.root
Binary file not shown.
3 changes: 0 additions & 3 deletions src/babyyoda/grogu/histo1d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ def bins(self, includeOverflows: bool = False) -> list[Bin]:
# TODO sorted needed here?
return sorted(self.d_bins, key=lambda b: b.d_xmin or -float("inf"))

def bin(self, *indices: int) -> list[Bin]:
return [self.bins()[i] for i in indices]

def binAt(self, x: float) -> Optional[Bin]:
for b in self.bins():
if b.contains(x):
Expand Down
3 changes: 0 additions & 3 deletions src/babyyoda/grogu/histo1d_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,6 @@ def xMin(self) -> float:
def bins(self, includeOverflows: bool = False) -> list[Bin]:
return self.d_bins[1:-1] if not includeOverflows else self.d_bins

def bin(self, *indices: int) -> list[Bin]:
return [self.bins()[i] for i in indices]

def binAt(self, x: float) -> Optional[Bin]:
# TODO add tests for binAt
for i, b in enumerate(self.bins()):
Expand Down
3 changes: 0 additions & 3 deletions src/babyyoda/grogu/histo2d_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,6 @@ def bins(self, includeOverflows: bool = False) -> list[Bin]:
# YODA-2
return sorted(self.d_bins, key=lambda b: (b.d_ymin, b.d_xmin))

def bin(self, index: int) -> Bin:
return self.bins()[index]

def binAt(self, x: float, y: float) -> Optional[Bin]:
for b in self.bins():
if (
Expand Down
11 changes: 7 additions & 4 deletions src/babyyoda/histo1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class UHIHisto1D(
def bins(self, includeOverflows: bool = False) -> list[Any]:
raise NotImplementedError

def bin(self, i: int) -> Any:
return self.bins()[i]

def xEdges(self) -> list[float]:
raise NotImplementedError

Expand Down Expand Up @@ -99,7 +102,7 @@ def to_boost_histogram(self) -> Any:
)
for i in range(len(self.xEdges()) - 1):
# we do not carry over numEntries nor sumWX...
h[i] = (self.bins()[i].sumW(), self.bins()[i].sumW2())
h[i] = (self.bin(i).sumW(), self.bin(i).sumW2())
return h

def to_hist(self) -> Any:
Expand All @@ -114,7 +117,7 @@ def to_hist(self) -> Any:
)
for i in range(len(self.xEdges()) - 1):
# we do not carry over numEntries nor sumWX...
h[i] = (self.bins()[i].sumW(), self.bins()[i].sumW2())
h[i] = (self.bin(i).sumW(), self.bin(i).sumW2())
return h

def to_grogu_v2(self) -> Any:
Expand Down Expand Up @@ -294,7 +297,7 @@ def __getitem__(
index = self.__get_index(slices)
# integer index
if isinstance(index, int): # loc and int
return self.bins()[index]
return self.bin(index)
if slices is underflow:
return self.underflow()
if slices is overflow:
Expand Down Expand Up @@ -383,7 +386,7 @@ def __set_by_index(
set_bin1d(self.overflow(), value)
return
if isinstance(index, int):
set_bin1d(self.bins()[index], value)
set_bin1d(self.bin(index), value)
return
err = "Invalid argument type"
raise TypeError(err)
Expand Down
3 changes: 3 additions & 0 deletions src/babyyoda/histo2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class UHIHisto2D(UHIAnalysisObject, PlottableHistogram):
def bins(self, includeOverflows: bool = False) -> list[Any]:
raise NotImplementedError

def bin(self, i: int) -> Any:
return self.bins()[i]

def xEdges(self) -> list[float]:
raise NotImplementedError

Expand Down

0 comments on commit c7a88a3

Please sign in to comment.