@@ -39,43 +39,35 @@ def run_query(input_filenames):
39
39
import awkward as ak
40
40
import json
41
41
42
- def is_tree (obj ):
43
- """Helper to check if a root file item is TTree."""
44
- if hasattr (obj , "classname" ):
45
- cls_attr = obj .classname
46
- cls_value = cls_attr () if callable (cls_attr ) else cls_attr
47
- return "TTree" in cls_value
48
- elif hasattr (obj , "classnames" ):
49
- cls_attr = obj .classnames
50
- cls_values = cls_attr () if callable (cls_attr ) else cls_attr
51
- return any ("TTree" in cls for cls in cls_values )
52
- return False
53
-
54
42
"""
55
- Opens a ROOT file and returns a JSON-formatted string describing the structure,
56
- encoded inside an ak.Array for ServiceX.
43
+ Opens a ROOT file and returns a JSON-formatted string describing each tree's layout form and type.
44
+ This function is ran by trasnformers in the ServiceX backend .
57
45
"""
46
+
58
47
tree_dict = {}
59
48
60
49
with uproot .open (input_filenames ) as file :
61
50
for tree_name in file .keys ():
62
51
tree_name_clean = tree_name .rstrip (";1" )
63
52
tree = file [tree_name ]
64
53
65
- if not is_tree (tree ):
54
+ # Skip non-TTree objects
55
+ if not hasattr (tree , "arrays" ):
66
56
continue
67
57
68
- branch_dict = {}
69
- for branch_name , branch in tree .items ():
70
- branch_type = str (branch .interpretation )
71
- branch_dict [branch_name ] = branch_type
58
+ # Load a minimal awkward array (only 1 event)
59
+ try :
60
+ array = tree .arrays (entry_stop = 1 , library = "ak" )
61
+ except Exception :
62
+ continue # In case array reading fails, skip tree
72
63
73
- tree_dict [tree_name_clean ] = branch_dict
64
+ tree_dict [tree_name_clean ] = {
65
+ "form" : array .layout .form .to_dict (), # full layout form
66
+ "type" : str (array .type ), # human-readable type
67
+ }
74
68
75
- # Serialize tree_dict to JSON string
69
+ # Serialize as JSON and wrap in ak.Array
76
70
json_str = json .dumps (tree_dict )
77
-
78
- # Return JSON string wrapped in an awkward array
79
71
return ak .Array ([json_str ])
80
72
81
73
0 commit comments