#!/usr/bin/env python3
"""podio-ttree-to-rntuple tool to create an rntuple file from a ttree file or vice-versa"""

import argparse
import podio.root_io

parser = argparse.ArgumentParser(
    description="podio-ttree-to-rntuple tool to create"
    "an rntuple file from a ttree file or vice-versa"
)
parser.add_argument("input_file", help="input file")
parser.add_argument("output_file", help="output file")
parser.add_argument(
    "-r", "--reverse", action="store_true", help="reverse the conversion (from RNTuple to TTree)"
)
args = parser.parse_args()

if not args.reverse:
    reader = podio.root_io.Reader(args.input_file)
    writer = podio.root_io.RNTupleWriter(args.output_file)
else:
    reader = podio.root_io.RNTupleReader(args.input_file)
    writer = podio.root_io.Writer(args.output_file)

for category in reader.categories:
    for frame in reader.get(category):
        writer.write_frame(frame, category)
