Skip to content

Commit 11adee3

Browse files
committed
Robustness agains ;1 metadata tree names
1 parent 020be35 commit 11adee3

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

servicex_analysis_utils/file_peeking.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,23 @@ def is_tree(obj):
6262

6363
with uproot.open(input_filenames) as file:
6464

65-
meta = file["MetaData"]
66-
fm_branches = [b for b in meta.keys() if b.startswith("FileMetaDataAuxDyn.")]
67-
# remove the prefix in keys
68-
meta_dict = {p[19:]: str(meta[p].array(library="ak")[0]) for p in fm_branches}
69-
tree_dict["FileMetaData"] = meta_dict
70-
7165
for tree_name in file.keys():
7266
tree_name_clean = tree_name.rstrip(";1")
7367
tree = file[tree_name]
7468

7569
if not is_tree(tree):
7670
continue
7771

72+
if tree_name_clean == "Metadata":
73+
fm_branches = [
74+
b for b in tree.keys() if b.startswith("FileMetaDataAuxDyn.")
75+
]
76+
# remove the prefix in keys
77+
meta_dict = {
78+
p[19:]: str(tree[p].array(library="ak")[0]) for p in fm_branches
79+
}
80+
tree_dict["FileMetaData"] = meta_dict
81+
7882
branch_dict = {}
7983
for branch_name, branch in tree.items():
8084
branch_type = str(branch.interpretation)
@@ -199,8 +203,11 @@ def print_structure_from_str(
199203

200204
# Get the metadata first
201205
output_lines.append(f"\nFile Metadata \u2139\ufe0f :\n")
202-
for key, value in structure_dict.get("FileMetaData", {}).items():
203-
output_lines.append(f"── {key}: {value}")
206+
if "FileMetaData" not in structure_dict:
207+
output_lines.append("No FileMetaData found in dataset.")
208+
else:
209+
for key, value in structure_dict.get("FileMetaData", {}).items():
210+
output_lines.append(f"── {key}: {value}")
204211
output_lines.append("\n---------------------------")
205212

206213
# drop the File metadata from the trees
@@ -278,6 +285,8 @@ def str_to_array(encoded_json_str):
278285
"""
279286
reconstructed_data = {}
280287
structure_dict = json.loads(encoded_json_str)
288+
# drop the File metadata from the trees
289+
structure_dict.pop("FileMetaData", {})
281290

282291
for treename, branch_dict in structure_dict.items():
283292
branches = {}

0 commit comments

Comments
 (0)