-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[ntuple] Enable subfield access in REntry
#16693
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -307,6 +307,22 @@ void ROOT::Experimental::RNTupleModel::AddField(std::unique_ptr<RFieldBase> fiel | |
fFieldZero->Attach(std::move(field)); | ||
} | ||
|
||
void ROOT::Experimental::RNTupleModel::AddSubField(std::unique_ptr<RFieldBase> field, RFieldBase &parent) | ||
{ | ||
EnsureNotFrozen(); | ||
if (!field) | ||
throw RException(R__FAIL("null field")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make this error a bit more explicative |
||
EnsureValidFieldName(field->GetFieldName()); | ||
|
||
field->SetParent(&parent); | ||
|
||
if (fDefaultEntry) | ||
fDefaultEntry->AddValue(field->CreateValue()); | ||
fFieldNames.insert(field->GetFieldName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this collide between a top-level field |
||
|
||
parent.SetSubField(std::move(field)); | ||
} | ||
|
||
ROOT::Experimental::RResult<void> | ||
ROOT::Experimental::RNTupleModel::AddProjectedField(std::unique_ptr<RFieldBase> field, FieldMappingFunc_t mapping) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -808,6 +808,58 @@ TEST(REntry, Basics) | |
EXPECT_NE(&pt, e->GetPtr<void>("pt").get()); | ||
} | ||
|
||
TEST(REntry, SubFields) | ||
{ | ||
FileRaii fileGuard("test_rentry_subfields"); | ||
{ | ||
auto model = RNTupleModel::Create(); | ||
model->MakeField<CustomStruct>("struct", CustomStruct{1.f, {2.f, 3.f}, {{4.f}, {5.f, 6.f, 7.f}}, "foo"}); | ||
|
||
auto ntuple = RNTupleWriter::Recreate(std::move(model), "ntuple", fileGuard.GetPath()); | ||
ntuple->Fill(); | ||
} | ||
|
||
auto model = RNTupleModel::Create(); | ||
auto fldStruct = RFieldBase::Create("struct", "CustomStruct").Unwrap(); | ||
auto fldA = RFieldBase::Create("a", "float").Unwrap(); | ||
|
||
model->AddSubField(std::move(fldA), *fldStruct); | ||
|
||
try { | ||
auto tmpModel = model->Clone(); | ||
auto fldInvalidSubField = RFieldBase::Create("doesnotexist", "float").Unwrap(); | ||
tmpModel->AddSubField(std::move(fldInvalidSubField), *fldStruct); | ||
FAIL() << "should not be able to add a subfield not present in the provided parent"; | ||
} catch (const RException &err) { | ||
EXPECT_THAT(err.what(), testing::HasSubstr("subfield \"doesnotexist\" not found")); | ||
} | ||
|
||
model->AddField(std::move(fldStruct)); | ||
|
||
auto &entry = model->GetDefaultEntry(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the test should also exercise a non-default entry (doesn't |
||
auto ntuple = RNTupleReader::Open(std::move(model), "ntuple", fileGuard.GetPath()); | ||
ntuple->LoadEntry(0); | ||
|
||
CustomStruct expectedStruct{1.f, {2.f, 3.f}, {{4.f}, {5.f, 6.f, 7.f}}, "foo"}; | ||
|
||
EXPECT_EQ(expectedStruct, *entry.GetPtr<CustomStruct>("struct")); | ||
EXPECT_FLOAT_EQ(1.f, *entry.GetPtr<float>("struct.a")); | ||
|
||
auto tokenStruct = entry.GetToken("struct"); | ||
EXPECT_EQ(expectedStruct, *entry.GetPtr<CustomStruct>(tokenStruct)); | ||
|
||
auto tokenStructA = entry.GetToken("struct.a"); | ||
EXPECT_FLOAT_EQ(1.f, *entry.GetPtr<float>(tokenStructA)); | ||
|
||
// sanity check | ||
try { | ||
*entry.GetPtr<std::vector<float>>("v1"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe also test |
||
FAIL() << "subfields not explicitly added to the model shouldn't be present in the model"; | ||
} catch (const RException &err) { | ||
EXPECT_THAT(err.what(), testing::HasSubstr("invalid field name: v1")); | ||
} | ||
} | ||
|
||
TEST(RFieldBase, CreateObject) | ||
{ | ||
auto ptrInt = RField<int>("name").CreateObject<int>(); | ||
|
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.
Does this need to be public? Considering it doesn't do any sanity check I'm wondering if it's not something we want to prevent users to call directly..unless I'm missing something. (same consideration for SetSubField)