Skip to content

Commit

Permalink
[ntuple] Detect entry-model mismatch in LoadEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
enirolf committed Jan 30, 2025
1 parent 5a8a119 commit a20c8cd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
8 changes: 7 additions & 1 deletion tree/ntuple/v7/inc/ROOT/RNTupleReader.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ public:
LoadEntry(index, fModel->GetDefaultEntry());
}
/// Fills a user provided entry after checking that the entry has been instantiated from the ntuple model
void LoadEntry(NTupleSize_t index, REntry &entry) { entry.Read(index); }
void LoadEntry(NTupleSize_t index, REntry &entry)
{
if (R__unlikely(entry.GetModelId() != fModel->GetModelId()))
throw RException(R__FAIL("mismatch between entry and model"));

entry.Read(index);
}

/// Returns an iterator over the entry indices of the RNTuple.
///
Expand Down
40 changes: 28 additions & 12 deletions tree/ntuple/v7/test/ntuple_basics.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -547,27 +547,43 @@ TEST(RNTuple, ModelId)

TEST(RNTuple, Entry)
{
auto m1 = RNTupleModel::Create();
auto m = RNTupleModel::Create();
try {
m1->CreateEntry();
m->CreateEntry();
FAIL() << "creating entry of unfrozen model should throw";
} catch (const ROOT::RException &err) {
EXPECT_THAT(err.what(), testing::HasSubstr("invalid attempt to create entry"));
}
m1->Freeze();
auto e1 = m1->CreateEntry();
m->Freeze();
auto e = m->CreateEntry();

auto m2 = RNTupleModel::Create();
m2->Freeze();
auto e2 = m2->CreateEntry();
auto mWrite = m->Clone();
mWrite->Freeze();
auto eWrite = mWrite->CreateEntry();

FileRaii fileGuard("test_ntuple_entry.root");
auto ntuple = RNTupleWriter::Recreate(std::move(m1), "ntpl", fileGuard.GetPath());
ntuple->Fill();
ntuple->Fill(*e1);
{
auto ntuple = RNTupleWriter::Recreate(std::move(mWrite), "ntpl", fileGuard.GetPath());
ntuple->Fill();
ntuple->Fill(*eWrite);
try {
ntuple->Fill(*e);
FAIL() << "filling with wrong entry should throw";
} catch (const ROOT::RException &err) {
EXPECT_THAT(err.what(), testing::HasSubstr("mismatch between entry and model"));
}
}

auto mRead = m->Clone();
mRead->Freeze();
auto eRead = mRead->CreateEntry();

auto ntuple = RNTupleReader::Open(std::move(mRead), "ntpl", fileGuard.GetPath());
ntuple->LoadEntry(0);
ntuple->LoadEntry(0, *eRead);
try {
ntuple->Fill(*e2);
FAIL() << "filling with wrong entry should throw";
ntuple->LoadEntry(0, *e);
FAIL() << "loading the wrong entry should throw";
} catch (const ROOT::RException &err) {
EXPECT_THAT(err.what(), testing::HasSubstr("mismatch between entry and model"));
}
Expand Down

0 comments on commit a20c8cd

Please sign in to comment.