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

[ntuple] Add RNTupleModel::GetMutableField #16713

Merged
merged 2 commits into from
Oct 22, 2024
Merged
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
3 changes: 2 additions & 1 deletion tree/ntuple/v7/inc/ROOT/RNTupleModel.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ public:
/// Mutable access to the root field is used to make adjustments to the fields.
RFieldZero &GetMutableFieldZero();
const RFieldZero &GetConstFieldZero() const { return *fFieldZero; }
const RFieldBase &GetField(std::string_view fieldName) const;
RFieldBase &GetMutableField(std::string_view fieldName);
const RFieldBase &GetConstField(std::string_view fieldName) const;

const std::string &GetDescription() const { return fDescription; }
void SetDescription(std::string_view description);
Expand Down
15 changes: 13 additions & 2 deletions tree/ntuple/v7/src/RNTupleModel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ ROOT::Experimental::Internal::RProjectedFields::Clone(const RNTupleModel &newMod
for (const auto &[k, v] : fFieldMap) {
for (const auto &f : clone->GetFieldZero()) {
if (f.GetQualifiedFieldName() == k->GetQualifiedFieldName()) {
clone->fFieldMap[&f] = &newModel.GetField(v->GetQualifiedFieldName());
clone->fFieldMap[&f] = &newModel.GetConstField(v->GetQualifiedFieldName());
break;
}
}
Expand Down Expand Up @@ -354,7 +354,18 @@ ROOT::Experimental::RFieldZero &ROOT::Experimental::RNTupleModel::GetMutableFiel
return *fFieldZero;
}

const ROOT::Experimental::RFieldBase &ROOT::Experimental::RNTupleModel::GetField(std::string_view fieldName) const
ROOT::Experimental::RFieldBase &ROOT::Experimental::RNTupleModel::GetMutableField(std::string_view fieldName)
{
if (IsFrozen())
throw RException(R__FAIL("invalid attempt to get mutable field of frozen model"));
auto f = FindField(fieldName);
if (!f)
throw RException(R__FAIL("invalid field: " + std::string(fieldName)));

return *f;
}

const ROOT::Experimental::RFieldBase &ROOT::Experimental::RNTupleModel::GetConstField(std::string_view fieldName) const
{
auto f = FindField(fieldName);
if (!f)
Expand Down
5 changes: 3 additions & 2 deletions tree/ntuple/v7/src/RPageSinkBuf.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ void ROOT::Experimental::Internal::RPageSinkBuf::UpdateSchema(const RNTupleModel
auto p = &(*cloned);
auto &projectedFields = Internal::GetProjectedFieldsOfModel(changeset.fModel);
Internal::RProjectedFields::FieldMap_t fieldMap;
fieldMap[p] = &fInnerModel->GetField(projectedFields.GetSourceField(field)->GetQualifiedFieldName());
fieldMap[p] = &fInnerModel->GetConstField(projectedFields.GetSourceField(field)->GetQualifiedFieldName());
auto targetIt = cloned->begin();
for (auto &f : *field)
fieldMap[&(*targetIt++)] = &fInnerModel->GetField(projectedFields.GetSourceField(&f)->GetQualifiedFieldName());
fieldMap[&(*targetIt++)] =
&fInnerModel->GetConstField(projectedFields.GetSourceField(&f)->GetQualifiedFieldName());
Internal::GetProjectedFieldsOfModel(*fInnerModel).Add(std::move(cloned), fieldMap);
return p;
};
Expand Down
14 changes: 7 additions & 7 deletions tree/ntuple/v7/test/ntuple_compat.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ TEST(RNTupleCompat, FutureColumnType)
auto model = desc.CreateModel(modelOpts);

// The future column should not show up in the model
EXPECT_THROW(model->GetField("futureColumn"), RException);
EXPECT_THROW(model->GetConstField("futureColumn"), RException);

const auto &floatFld = model->GetField("float");
const auto &floatFld = model->GetConstField("float");
EXPECT_EQ(floatFld.GetTypeName(), "float");

reader.reset();
Expand Down Expand Up @@ -336,9 +336,9 @@ TEST(RNTupleCompat, FutureColumnType_Nested)
auto model = desc.CreateModel(modelOpts);

// The future column should not show up in the model
EXPECT_THROW(model->GetField("future"), RException);
EXPECT_THROW(model->GetConstField("future"), RException);

const auto &floatFld = model->GetField("float");
const auto &floatFld = model->GetConstField("float");
EXPECT_EQ(floatFld.GetTypeName(), "float");

reader.reset();
Expand Down Expand Up @@ -395,7 +395,7 @@ TEST(RNTupleCompat, FutureFieldStructuralRole)
modelOpts.fForwardCompatible = true;
auto model = desc.CreateModel(modelOpts);
try {
model->GetField("future");
model->GetConstField("future");
FAIL() << "trying to get a field with unknown role should fail";
} catch (const RException &err) {
EXPECT_THAT(err.what(), testing::HasSubstr("invalid field"));
Expand Down Expand Up @@ -431,10 +431,10 @@ TEST(RNTupleCompat, FutureFieldStructuralRole_Nested)
auto modelOpts = RNTupleDescriptor::RCreateModelOptions();
modelOpts.fForwardCompatible = true;
auto model = desc.CreateModel(modelOpts);
const auto &floatFld = model->GetField("float");
const auto &floatFld = model->GetConstField("float");
EXPECT_EQ(floatFld.GetTypeName(), "float");
try {
model->GetField("record");
model->GetConstField("record");
FAIL() << "trying to get a field with unknown role should fail";
} catch (const RException &err) {
EXPECT_THAT(err.what(), testing::HasSubstr("invalid field"));
Expand Down
30 changes: 19 additions & 11 deletions tree/ntuple/v7/test/ntuple_model.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,21 @@ TEST(RNTupleModel, GetField)
auto m = RNTupleModel::Create();
m->MakeField<int>("x");
m->MakeField<CustomStruct>("cs");
m->GetMutableField("cs.v1._0").SetColumnRepresentatives({{EColumnType::kReal32}});
m->Freeze();
EXPECT_EQ(m->GetField("x").GetFieldName(), "x");
EXPECT_EQ(m->GetField("x").GetTypeName(), "std::int32_t");
EXPECT_EQ(m->GetField("cs.v1").GetFieldName(), "v1");
EXPECT_EQ(m->GetField("cs.v1").GetTypeName(), "std::vector<float>");
EXPECT_EQ(m->GetConstField("x").GetFieldName(), "x");
EXPECT_EQ(m->GetConstField("x").GetTypeName(), "std::int32_t");
EXPECT_EQ(m->GetConstField("cs.v1").GetFieldName(), "v1");
EXPECT_EQ(m->GetConstField("cs.v1").GetTypeName(), "std::vector<float>");
EXPECT_EQ(m->GetConstField("cs.v1._0").GetColumnRepresentatives()[0][0], EColumnType::kReal32);
try {
m->GetField("nonexistent");
m->GetConstField("nonexistent");
FAIL() << "invalid field name should throw";
} catch (const RException &err) {
EXPECT_THAT(err.what(), testing::HasSubstr("invalid field"));
}
try {
m->GetField("");
m->GetConstField("");
FAIL() << "empty field name should throw";
} catch (const RException &err) {
EXPECT_THAT(err.what(), testing::HasSubstr("invalid field"));
Expand All @@ -90,10 +92,16 @@ TEST(RNTupleModel, GetField)
} catch (const RException &err) {
EXPECT_THAT(err.what(), testing::HasSubstr("frozen model"));
}
try {
m->GetMutableField("x");
FAIL() << "GetMutableField should throw";
} catch (const RException &err) {
EXPECT_THAT(err.what(), testing::HasSubstr("frozen model"));
}
EXPECT_EQ("", m->GetConstFieldZero().GetQualifiedFieldName());
EXPECT_EQ("x", m->GetField("x").GetQualifiedFieldName());
EXPECT_EQ("cs", m->GetField("cs").GetQualifiedFieldName());
EXPECT_EQ("cs.v1", m->GetField("cs.v1").GetQualifiedFieldName());
EXPECT_EQ("x", m->GetConstField("x").GetQualifiedFieldName());
EXPECT_EQ("cs", m->GetConstField("cs").GetQualifiedFieldName());
EXPECT_EQ("cs.v1", m->GetConstField("cs.v1").GetQualifiedFieldName());
}

TEST(RNTupleModel, EstimateWriteMemoryUsage)
Expand Down Expand Up @@ -160,6 +168,6 @@ TEST(RNTupleModel, Clone)
EXPECT_EQ(EColumnType::kUInt32, f.GetColumnRepresentatives()[0][0]);
}
}
EXPECT_TRUE(clone->GetField("struct").GetTraits() & RFieldBase::kTraitTypeChecksum);
EXPECT_TRUE(clone->GetField("obj").GetTraits() & RFieldBase::kTraitTypeChecksum);
EXPECT_TRUE(clone->GetConstField("struct").GetTraits() & RFieldBase::kTraitTypeChecksum);
EXPECT_TRUE(clone->GetConstField("obj").GetTraits() & RFieldBase::kTraitTypeChecksum);
}
14 changes: 7 additions & 7 deletions tree/ntuple/v7/test/ntuple_modelext.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ TEST(RNTuple, ModelExtensionSimple)
auto ntuple = RNTupleReader::Open("myNTuple", fileGuard.GetPath());
EXPECT_EQ(4U, ntuple->GetNEntries());
EXPECT_EQ(4U, ntuple->GetDescriptor().GetNFields());
EXPECT_EQ(0U, GetFirstEntry(ntuple->GetModel().GetField("pt")));
EXPECT_EQ(2U, GetFirstEntry(ntuple->GetModel().GetField("array")));
EXPECT_EQ(0U, GetFirstEntry(ntuple->GetModel().GetConstField("pt")));
EXPECT_EQ(2U, GetFirstEntry(ntuple->GetModel().GetConstField("array")));

auto pt = ntuple->GetView<float>("pt");
auto array = ntuple->GetView<std::array<double, 2>>("array");
Expand Down Expand Up @@ -155,11 +155,11 @@ TEST(RNTuple, ModelExtensionMultiple)
auto ntuple = RNTupleReader::Open("myNTuple", fileGuard.GetPath());
EXPECT_EQ(5U, ntuple->GetNEntries());
EXPECT_EQ(7U, ntuple->GetDescriptor().GetNFields());
EXPECT_EQ(0U, GetFirstEntry(ntuple->GetModel().GetField("pt")));
EXPECT_EQ(3U, GetFirstEntry(ntuple->GetModel().GetField("vec")));
EXPECT_EQ(3U, GetFirstEntry(ntuple->GetModel().GetField("f")));
EXPECT_EQ(3U, GetFirstEntry(ntuple->GetModel().GetField("u8")));
EXPECT_EQ(3U, GetFirstEntry(ntuple->GetModel().GetField("str")));
EXPECT_EQ(0U, GetFirstEntry(ntuple->GetModel().GetConstField("pt")));
EXPECT_EQ(3U, GetFirstEntry(ntuple->GetModel().GetConstField("vec")));
EXPECT_EQ(3U, GetFirstEntry(ntuple->GetModel().GetConstField("f")));
EXPECT_EQ(3U, GetFirstEntry(ntuple->GetModel().GetConstField("u8")));
EXPECT_EQ(3U, GetFirstEntry(ntuple->GetModel().GetConstField("str")));

auto pt = ntuple->GetView<float>("pt");
auto vec = ntuple->GetView<std::vector<std::uint32_t>>("vec");
Expand Down
38 changes: 19 additions & 19 deletions tree/ntuple/v7/test/ntuple_multi_column.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ TEST(RNTuple, MultiColumnRepresentationSimple)
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("px")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("px")), 1);
*ptrPx = 2.0;
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("px")), 0);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("px")), 0);
*ptrPx = 3.0;
writer->Fill();
}

auto reader = RNTupleReader::Open("ntpl", fileGuard.GetPath());
EXPECT_EQ(3u, reader->GetModel().GetField("px").GetNElements());
EXPECT_EQ(3u, reader->GetModel().GetConstField("px").GetNElements());

const auto &desc = reader->GetDescriptor();
EXPECT_EQ(3u, desc.GetNClusters());
Expand Down Expand Up @@ -157,16 +157,16 @@ TEST(RNTuple, MultiColumnRepresentationString)
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("str")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("str")), 1);
ptrStr->clear();
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("str")), 0);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("str")), 0);
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("str")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("str")), 1);
*ptrStr = "x";
writer->Fill();
}
Expand Down Expand Up @@ -199,17 +199,17 @@ TEST(RNTuple, MultiColumnRepresentationVector)
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("vec")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("vec")), 1);
ptrVec->push_back(1.0);
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("vec")), 0);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("vec")), 0);
ptrVec->clear();
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("vec")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("vec")), 1);
ptrVec->push_back(2.0);
ptrVec->push_back(3.0);
writer->Fill();
Expand Down Expand Up @@ -250,17 +250,17 @@ TEST(RNTuple, MultiColumnRepresentationMany)
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("vec")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("vec")), 1);
(*ptrVec)[0] = 2.0;
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("vec")), 2);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("vec")), 2);
(*ptrVec)[0] = 3.0;
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("vec")), 3);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("vec")), 3);
(*ptrVec)[0] = 4.0;
writer->Fill();
}
Expand Down Expand Up @@ -302,18 +302,18 @@ TEST(RNTuple, MultiColumnRepresentationNullable)
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("scalar")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("scalar")), 1);
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("vector._0")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("vector._0")), 1);
ptrScalar->reset();
ptrVector->clear();
ptrVector->push_back(std::optional<float>());
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("scalar")), 0);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("scalar")), 0);
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("vector._0")), 0);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("vector._0")), 0);
*ptrScalar = 3.0;
ptrVector->clear();
ptrVector->push_back(15.0);
Expand Down Expand Up @@ -362,7 +362,7 @@ TEST(RNTuple, MultiColumnRepresentationBulk)
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("px")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("px")), 1);
*ptrPx = 2.0;
writer->Fill();
}
Expand Down Expand Up @@ -402,7 +402,7 @@ TEST(RNTuple, MultiColumnRepresentationFriends)
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("pt")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("pt")), 1);
*ptrPt = 2.0;
writer->Fill();
}
Expand All @@ -412,7 +412,7 @@ TEST(RNTuple, MultiColumnRepresentationFriends)
writer->Fill();
writer->CommitCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(writer->GetModel().GetField("eta")), 1);
const_cast<RFieldBase &>(writer->GetModel().GetConstField("eta")), 1);
*ptrEta = 4.0;
writer->Fill();
}
Expand Down
6 changes: 3 additions & 3 deletions tree/ntuple/v7/test/ntuple_parallel_writer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ TEST(RNTupleParallelWriter, StagedMultiColumn)
c->Fill(*e);
c->FlushCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(c->GetModel().GetField("px")), 1);
const_cast<RFieldBase &>(c->GetModel().GetConstField("px")), 1);
*px = 2.0;
c->Fill(*e);
c->FlushCluster();
ROOT::Experimental::Internal::RFieldRepresentationModifier::SetPrimaryColumnRepresentation(
const_cast<RFieldBase &>(c->GetModel().GetField("px")), 0);
const_cast<RFieldBase &>(c->GetModel().GetConstField("px")), 0);
*px = 3.0;
c->Fill(*e);
c->FlushCluster();
Expand All @@ -193,7 +193,7 @@ TEST(RNTupleParallelWriter, StagedMultiColumn)
}

auto reader = RNTupleReader::Open("ntpl", fileGuard.GetPath());
EXPECT_EQ(3u, reader->GetModel().GetField("px").GetNElements());
EXPECT_EQ(3u, reader->GetModel().GetConstField("px").GetNElements());

const auto &desc = reader->GetDescriptor();
EXPECT_EQ(3u, desc.GetNClusters());
Expand Down
4 changes: 2 additions & 2 deletions tree/ntuple/v7/test/ntuple_storage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,8 @@ TEST(RPageStorageFile, MultiKeyBlob_Header)
EXPECT_GE(ntupleUcmp->GetDescriptor().GetOnDiskHeaderSize(), kMaxKeySize);

for (int i = 0; i < kNFields; ++i) {
ntupleComp->GetModel().GetField(std::to_string(i));
ntupleUcmp->GetModel().GetField(std::to_string(i));
ntupleComp->GetModel().GetConstField(std::to_string(i));
ntupleUcmp->GetModel().GetConstField(std::to_string(i));
}
}
}
Expand Down
Loading
Loading