Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a tool to merge several podio files into a single one #681

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ install(PROGRAMS ${CMAKE_CURRENT_LIST_DIR}/podio-vis DESTINATION ${CMAKE_INSTALL
if(ENABLE_RNTUPLE)
install(PROGRAMS ${CMAKE_CURRENT_LIST_DIR}/podio-ttree-to-rntuple DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
install(PROGRAMS ${CMAKE_CURRENT_LIST_DIR}/podio-merge-files DESTINATION ${CMAKE_INSTALL_BINDIR})

# Add a very basic test of podio-vis
if(BUILD_TESTING)
Expand Down
53 changes: 53 additions & 0 deletions tools/podio-merge-files
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
"""podio-merge-files tool to merge any number of podio files into one"""

import argparse
import podio.root_io
from podio import reading

parser = argparse.ArgumentParser(
description="Merge any number of podio files into one, can merge TTree and RNTuple files"
)

parser.add_argument("--output-file", help="name of the output file", required=True)
parser.add_argument("files", nargs="+", help="which files to merge")
parser.add_argument(
"--metadata",
choices=["none", "all", "first"],
default="first",
help="metadata to include in the output file, default: "
"only the one from the first file, other options: all files, none",
)
parser.add_argument(
"--metadata-category-name",
default="metadata",
help="name of the metadata category in the output file, default: metadata",
)
args = parser.parse_args()

all_files = set()
for f in args.files:
if f in all_files:
raise ValueError(f"File {f} is present more than once in the input list")
all_files.add(f)

root_format = reading._determine_root_format(args.files[0])
if root_format == reading.RootFileFormat.TTREE:
reader = podio.root_io.Reader(args.files)
writer = podio.root_io.Writer(args.output_file)
elif root_format == reading.RootFileFormat.RNTUPLE:
reader = podio.root_io.RNTupleReader(args.files)
writer = podio.root_io.RNTupleWriter(args.output_file)
else:
raise ValueError(f"Input file {args.files[0]} is not a TTree or RNTuple file")


for category in reader.categories:
if category == args.metadata_category_name and args.metadata == "none":
continue
if category == args.metadata_category_name and args.metadata == "first":
all_frames = [reader.get(category)[0]]
else:
all_frames = reader.get(category)
for frame in all_frames:
writer.write_frame(frame, category)
Loading