FairShip
Loading...
Searching...
No Matches
inspect_tree_branches.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# SPDX-License-Identifier: LGPL-3.0-or-later
3# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
4"""
5Inspect TTree branches to find which ones use TObjArray or other unsupported types.
6
7This helps identify branches that would prevent RNTuple conversion.
8"""
9
10import argparse
11import sys
12
13import ROOT
14
15
16def inspect_tree_branches(input_file, tree_name) -> bool:
17 """
18 Inspect all branches in a TTree and report their types.
19
20 Args:
21 input_file: Path to input ROOT file
22 tree_name: Name of the TTree to inspect
23 """
24 print(f"Inspecting TTree: {input_file}:{tree_name}")
25 print("=" * 80)
26 print()
27
28 with ROOT.TFile.Open(input_file, "READ") as f:
29 tree = f.Get(tree_name)
30 if not tree:
31 print(f"ERROR: Tree '{tree_name}' not found")
32 return False
33
34 branches = tree.GetListOfBranches()
35 print(f"Found {branches.GetEntries()} branches\n")
36
37 problematic_branches = []
38
39 for i in range(branches.GetEntries()):
40 branch = branches.At(i)
41 branch_name = branch.GetName()
42 class_name = branch.GetClassName()
43
44 is_problematic = False
45 reason = ""
46
47 if "TObjArray" in class_name:
48 is_problematic = True
49 reason = "TObjArray not supported in RNTuple"
50 elif "TClonesArray" in class_name:
51 is_problematic = True
52 reason = "TClonesArray not supported in RNTuple"
53 elif class_name and "TObject" in class_name and "std::" not in class_name:
54 # Other legacy ROOT types might be problematic
55 is_problematic = True
56 reason = "Legacy ROOT container"
57
58 status = "⚠️ PROBLEM" if is_problematic else "✓ OK"
59 print(f"{status:12} {branch_name:40} {class_name}")
60
61 if is_problematic:
62 problematic_branches.append((branch_name, class_name, reason))
63
64 print()
65 print("=" * 80)
66
67 if problematic_branches:
68 print(f"\n❌ Found {len(problematic_branches)} problematic branch(es):\n")
69 for name, cls, reason in problematic_branches:
70 print(f" • {name}")
71 print(f" Type: {cls}")
72 print(f" Issue: {reason}")
73 print()
74
75 print("These branches prevent RNTuple conversion.")
76 print("Consider migrating to std::vector or other supported types.")
77 return False
78 else:
79 print("\n✓ All branches appear to be RNTuple-compatible!")
80 return True
81
82
83def main() -> int:
84 """Parse arguments and run the TTree branch inspection."""
85 parser = argparse.ArgumentParser(description="Inspect TTree branches for RNTuple compatibility")
86 parser.add_argument("-f", "--input-file", required=True, help="Input ROOT file")
87 parser.add_argument(
88 "-t",
89 "--tree-name",
90 default="cbmsim",
91 help="Name of TTree to inspect (default: cbmsim)",
92 )
93
94 args = parser.parse_args()
95
96 success = inspect_tree_branches(args.input_file, args.tree_name)
97 return 0 if success else 1
98
99
100if __name__ == "__main__":
101 sys.exit(main())