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,
109 # Convert cbmsim tree to fairdata RNTuple
110 %(prog)s -f ship.conical.Pythia8-TGeant4.root -t cbmsim -o output.root -n fairdata
112 # Use defaults (cbmsim → fairdata)
113 %(prog)s -f ship.conical.Pythia8-TGeant4.root
115 # Convert custom tree
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!")