5Convert TTree to RNTuple using ROOT's RNTupleImporter.
7This script imports TTrees from ROOT files and converts them to RNTuple format,
8which provides better compression, faster I/O, and modern columnar storage.
11 python convertTreeToRNTuple.py -f input.root -t cbmsim -o output.root -n fairdata
12 python convertTreeToRNTuple.py -f input.root
15 RNTuple has some limitations compared to TTree:
16 - TObjArray
and other legacy ROOT containers cannot be stored natively
17 - Use std::vector instead of TObjArray
for RNTuple compatibility
18 - TClonesArray
is also
not supported; migrate to std::vector
27def convert_tree_to_rntuple(input_file, tree_name, output_file, ntuple_name) -> bool:
29 Convert a TTree to RNTuple format.
32 input_file: Path to input ROOT file containing TTree
33 tree_name: Name of the TTree to convert
34 output_file: Path to output ROOT file for RNTuple
35 ntuple_name: Name
for the output RNTuple
37 print("Converting TTree → RNTuple")
38 print(f
" Input: {input_file}:{tree_name}")
39 print(f
" Output: {output_file}:{ntuple_name}")
42 with ROOT.TFile.Open(input_file,
"READ")
as input_root_file:
43 tree = input_root_file.Get(tree_name)
45 print(f
"ERROR: Tree '{tree_name}' not found in {input_file}")
46 print(
"Available trees:")
47 for key
in input_root_file.GetListOfKeys():
49 if obj.InheritsFrom(
"TTree"):
50 print(f
" - {key.GetName()}")
53 num_entries = tree.GetEntries()
54 print(f
"Found TTree '{tree_name}' with {num_entries} entries")
59 importer = ROOT.ROOT.Experimental.RNTupleImporter.Create(input_file, tree_name, output_file)
62 print(
"ERROR: Failed to create RNTupleImporter")
66 importer.SetNTupleName(ntuple_name)
72 print(
"Starting conversion...")
75 print(
"✓ Conversion complete!")
79 with ROOT.TFile.Open(output_file,
"READ"):
80 reader = ROOT.ROOT.RNTupleReader.Open(ntuple_name, output_file)
81 num_rntuple_entries = reader.GetNEntries()
83 print(
"Verification:")
84 print(f
" TTree entries: {num_entries}")
85 print(f
" RNTuple entries: {num_rntuple_entries}")
87 if num_entries == num_rntuple_entries:
88 print(
" ✓ Entry count matches")
90 print(
" ✗ WARNING: Entry count mismatch!")
94 except Exception
as e:
95 print(f
"ERROR: Exception during conversion: {e}")
103 """Parse arguments and run the TTree to RNTuple conversion."""
104 parser = argparse.ArgumentParser(
105 description=
"Convert TTree to RNTuple using ROOT's RNTupleImporter",
106 formatter_class=argparse.RawDescriptionHelpFormatter,
110 %(prog)s -f ship.conical.Pythia8-TGeant4.root -t cbmsim -o output.root -n fairdata
113 %(prog)s -f ship.conical.Pythia8-TGeant4.root
116 %(prog)s -f mydata.root -t mytree -o converted.root -n myntuple
120 parser.add_argument("-f",
"--input-file", required=
True, help=
"Input ROOT file containing TTree")
125 help=
"Name of TTree to convert (default: cbmsim)",
131 help=
"Output ROOT file for RNTuple (default: <input>_rntuple.root)",
137 help=
"Name for output RNTuple (default: fairdata)",
140 args = parser.parse_args()
142 if args.output_file
is None:
143 if args.input_file.endswith(
".root"):
144 args.output_file = args.input_file.replace(
".root",
"_rntuple.root")
146 args.output_file = args.input_file +
"_rntuple.root"
152 print(
"Success! RNTuple file created:")
153 print(f
" {args.output_file}")
157 print(
"Conversion failed!")
161if __name__ ==
"__main__":
bool convert_tree_to_rntuple(input_file, tree_name, output_file, ntuple_name)