|
1 | | -"""timeseries_diff.py |
| 1 | +""" AliasDataframe.py |
2 | 2 | import sys,os; sys.path.insert(1, os.environ[f"O2DPG"]+"/UTILS/dfextensions"); |
3 | 3 | from AliasDataFrame import * |
4 | 4 | Utility helpers extension of the pandas DataFrame to support on-demand computed columns (aliases) |
|
7 | 7 | import pandas as pd |
8 | 8 | import numpy as np |
9 | 9 | import json |
10 | | -import os |
11 | 10 | import uproot |
12 | | - |
| 11 | +import ROOT # type: ignore |
13 | 12 | class AliasDataFrame: |
14 | 13 | """ |
15 | 14 | A wrapper for pandas DataFrame that supports on-demand computed columns (aliases) |
16 | 15 | with dependency tracking and persistence. |
17 | 16 | Example usage: |
18 | | - >>> import pandas as pd |
19 | 17 | >>> df = pd.DataFrame({"x": [1, 2, 3], "y": [10, 20, 30]}) |
20 | 18 | >>> adf = AliasDataFrame(df) |
21 | 19 | >>> adf.add_alias("z", "x + y") |
@@ -174,16 +172,16 @@ def export_tree(self, filename, treename="tree", dropAliasColumns=True): |
174 | 172 |
|
175 | 173 | with uproot.recreate(filename) as f: |
176 | 174 | f[treename] = export_df |
177 | | - |
178 | | - import ROOT |
| 175 | + # Update the ROOT file with aliases |
179 | 176 | f = ROOT.TFile.Open(filename, "UPDATE") |
180 | 177 | tree = f.Get(treename) |
181 | 178 | for alias, expr in self.aliases.items(): |
182 | 179 | tree.SetAlias(alias, expr) |
183 | 180 | tree.Write("", ROOT.TObject.kOverwrite) |
184 | 181 | f.Close() |
185 | 182 |
|
186 | | - def read_tree(self, filename, treename="tree"): |
| 183 | + @staticmethod |
| 184 | + def read_tree(filename, treename="tree"): |
187 | 185 | with uproot.open(filename) as f: |
188 | 186 | df = f[treename].arrays(library="pd") |
189 | 187 | adf = AliasDataFrame(df) |
|
0 commit comments