-
Notifications
You must be signed in to change notification settings - Fork 62
Make the python writers have the same behavior as the C++ writers #897
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
Merged
+168
−3
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #include "datamodel/ExampleHitCollection.h" | ||
|
|
||
| #include "podio/Frame.h" | ||
| #include "podio/ROOTReader.h" | ||
| #include "podio/ROOTWriter.h" | ||
|
|
||
| #include <iostream> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace { | ||
|
|
||
| int checkEmptyCollectionsFrame(const podio::Frame& frame) { | ||
| const auto colls = frame.getAvailableCollections(); | ||
| if (!colls.empty()) { | ||
| std::cerr << "expected no collections, got " << colls.size() << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| if (frame.get("hits") != nullptr) { | ||
| std::cerr << "collection 'hits' should not be persisted" << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| const auto anInt = frame.getParameter<int>("an_int"); | ||
| if (!anInt.has_value() || anInt.value() != 42) { | ||
| std::cerr << "parameter an_int not stored correctly" << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| const auto greetings = frame.getParameter<std::vector<std::string>>("greetings"); | ||
| const std::vector<std::string> expectedGreetings{"from", "python"}; | ||
| if (!greetings.has_value() || greetings.value() != expectedGreetings) { | ||
| std::cerr << "parameter greetings not stored correctly" << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| int main(int, char**) { | ||
| const auto filename = std::string{"empty_colls_frame_cpp.root"}; | ||
|
|
||
| podio::Frame frame; | ||
| auto hits = ExampleHitCollection(); | ||
| hits.create(0xBADull, 0.0f, 0.0f, 0.0f, 23.0f); | ||
| frame.put(std::move(hits), "hits"); | ||
|
|
||
| frame.putParameter("an_int", 42); | ||
| frame.putParameter("greetings", std::vector<std::string>{"from", "python"}); | ||
|
|
||
| auto writer = podio::ROOTWriter(filename); | ||
| const std::vector<std::string> noCollections{}; | ||
| writer.writeFrame(frame, "events", noCollections); | ||
| writer.finish(); | ||
|
|
||
| auto reader = podio::ROOTReader(); | ||
| reader.openFile(filename); | ||
|
|
||
| if (reader.getEntries("events") != 1) { | ||
| std::cerr << "expected exactly one entry" << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| auto data = reader.readEntry("events", 0); | ||
| if (!data) { | ||
| std::cerr << "could not read entry 0" << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| return checkEmptyCollectionsFrame(podio::Frame(std::move(data))); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #!/usr/bin/env python3 | ||
| """Write a frame while explicitly passing an empty collections list. | ||
|
|
||
| This is a regression test helper for the python writer bindings to ensure that | ||
| passing an empty list of collections behaves like the C++ writers: | ||
| - collections=None -> write all collections | ||
| - collections=[] -> write no collections (parameters only) | ||
| """ | ||
|
|
||
| import ROOT # type: ignore | ||
|
|
||
| # ROOT is a dynamic module; silence static type checkers. | ||
| if ROOT.gSystem.Load("libTestDataModelDict") < 0: # type: ignore[attr-defined] | ||
| raise RuntimeError("Could not load TestDataModel dictionary") | ||
|
|
||
| from ROOT import ExampleHitCollection # pylint: disable=wrong-import-position | ||
|
|
||
| from podio import Frame, reading, root_io # pylint: disable=wrong-import-position | ||
|
|
||
|
|
||
| def create_frame(): | ||
| """Create a frame with one collection and some parameters""" | ||
| frame = Frame() | ||
|
|
||
| hits = ExampleHitCollection() | ||
| hits.create(0xBAD, 0.0, 0.0, 0.0, 23.0) | ||
| frame.put(hits, "hits") | ||
|
|
||
| frame.put_parameter("an_int", 42) | ||
| frame.put_parameter("greetings", ["from", "python"]) | ||
|
|
||
| return frame | ||
|
|
||
|
|
||
| def assert_empty_collections(frame): | ||
| """Assert that the given frame has no persisted collections""" | ||
| if frame.getAvailableCollections(): | ||
| raise RuntimeError("Expected no persisted collections") | ||
|
|
||
| try: | ||
| frame.get("hits") | ||
| except KeyError: | ||
| pass | ||
| else: | ||
| raise RuntimeError("Collection 'hits' should not be persisted") | ||
|
|
||
| if frame.get_parameter("an_int") != 42: | ||
| raise RuntimeError("Parameter 'an_int' not stored correctly") | ||
| if frame.get_parameter("greetings") != ["from", "python"]: | ||
| raise RuntimeError("Parameter 'greetings' not stored correctly") | ||
|
|
||
|
|
||
| def write_file(filename): | ||
| """Write a ROOT file passing an empty collections list""" | ||
| if not filename.endswith(".root"): | ||
| raise ValueError("This test helper expects a .root output file") | ||
|
|
||
| writer = root_io.Writer(filename) | ||
| frame = create_frame() | ||
|
|
||
| # The important part: explicitly pass an empty list | ||
| writer.write_frame(frame, "events", []) | ||
| writer._writer.finish() # pylint: disable=protected-access | ||
|
|
||
| # Use the standard (TTree) reader inference and validate contents. | ||
| reader = reading.get_reader(filename) | ||
| if not isinstance(reader, root_io.Reader): | ||
| raise RuntimeError("Expected the regular ROOT TTree reader") | ||
|
|
||
| events = reader.get("events") | ||
| read_frame = next(iter(events)) | ||
| assert_empty_collections(read_frame) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("outputfile", help="Output file name") | ||
|
|
||
| args = parser.parse_args() | ||
| write_file(args.outputfile) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be excluded from the sanitizer runs as they do not usually work with anything that invokes the python interpreter.