-
Notifications
You must be signed in to change notification settings - Fork 5
Add test for attributes #77
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Attributes | ||
|
|
||
| ## Fields | ||
|
|
||
| * `Int32` (in main RNTuple) | ||
| * `attr` (in attribute set called `Attributes`) | ||
|
|
||
| with each the type `std::int32_t`. | ||
|
|
||
| ## Entries | ||
|
|
||
| 1. Associated to the first entry of the main RNTuple | ||
| 2. Referring to the second and third entry of the main RNTuple | ||
|
|
||
| The values can be found in the reference file `structure.attributes.json`. |
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,43 @@ | ||
| #include <ROOT/RNTupleReader.hxx> | ||
| #include <ROOT/RVersion.hxx> | ||
|
|
||
| #include <iostream> | ||
| #include <filesystem> | ||
|
|
||
| void read(std::string_view input = "structure.attributes.root", | ||
| std::string_view output = "structure.attributes.json") { | ||
| #if ROOT_VERSION_CODE < ROOT_VERSION(6, 40, 0) | ||
| std::cout << "Skipped structure/attributes/read.C - ROOT version too old." | ||
| << std::endl; | ||
| #else | ||
| // skipping test if .root file file doesn't exist | ||
| if (!std::filesystem::exists(input)) { | ||
| std::cout << "Skipped structure/attributes/read.C - file ('" << input << "') not found" << std::endl; | ||
| return; | ||
| } | ||
| // similar logic as read_structure.hxx | ||
| std::unique_ptr<ROOT::RNTupleReader> reader = ROOT::RNTupleReader::Open("ntpl", input); | ||
|
|
||
| std::ofstream os(std::string{output}); | ||
| os << "[\n"; | ||
| bool first = true; | ||
|
|
||
| auto attrSet = reader->OpenAttributeSet("Attributes"); | ||
| auto attr = attrSet->GetModel().GetDefaultEntry().GetPtr<std::int32_t>("attr"); | ||
|
|
||
| for (auto attrIdx : attrSet->GetAttributes()) { | ||
| auto range = attrSet->LoadEntry(attrIdx); | ||
| if (first) { | ||
| first = false; | ||
| } else { | ||
| os << ",\n"; | ||
| } | ||
|
hahnjo marked this conversation as resolved.
|
||
| os << " {\n"; | ||
| os << " \"Value\": " << *attr << ",\n"; | ||
| os << " \"Range\": [" << *range.GetFirst() << ", " << *range.GetLast() << "]\n"; | ||
| os << " }"; | ||
| } | ||
|
|
||
| os << "\n]\n"; | ||
| #endif | ||
| } | ||
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,42 @@ | ||
| #include <ROOT/RNTupleModel.hxx> | ||
| #include <ROOT/RNTupleWriteOptions.hxx> | ||
| #include <ROOT/RNTupleWriter.hxx> | ||
| #include <ROOT/RVersion.hxx> | ||
|
|
||
| #include <TFile.h> | ||
| #include <cstdint> | ||
| #include <string_view> | ||
|
|
||
| void write(std::string_view filename = "structure.attributes.root") { | ||
| #if ROOT_VERSION_CODE < ROOT_VERSION(6, 40, 0) | ||
| std::cout << "Skipped structure/attributes/write.C - ROOT version too old." | ||
| << std::endl; | ||
| #else | ||
| auto model = ROOT::RNTupleModel::Create(); | ||
| auto Int32 = model->MakeField<std::int32_t>("Int32"); | ||
|
|
||
| auto file = std::unique_ptr<TFile>(TFile::Open(std::string(filename).c_str(), "RECREATE")); | ||
| auto writer = ROOT::RNTupleWriter::Append(std::move(model), "ntpl", *file); | ||
|
|
||
| // definition of attribute set with attribute fields | ||
| auto attrModel = ROOT::RNTupleModel::Create(); | ||
| auto attr = attrModel->MakeField<std::int32_t>("attr"); | ||
| auto attrSet = writer->CreateAttributeSet(std::move(attrModel), "Attributes"); | ||
|
|
||
| // first entry | ||
| auto attrRange = attrSet->BeginRange(); | ||
| *Int32 = 1; | ||
| writer->Fill(); | ||
| *attr = 1; | ||
| attrSet->CommitRange(std::move(attrRange)); | ||
|
|
||
| // second and third entry | ||
| attrRange = attrSet->BeginRange(); | ||
| *attr = 2; | ||
| *Int32 = 2; | ||
| writer->Fill(); | ||
| *Int32 = 3; | ||
| writer->Fill(); | ||
| attrSet->CommitRange(std::move(attrRange)); | ||
| #endif | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.