Skip to content

Commit 0e85c10

Browse files
maszymanjblomer
authored andcommitted
[ntuple] Allow inline definition of RNTuple model in RNTupleWriter::Recreate
Fixes root-project#8711
1 parent 96be3ea commit 0e85c10

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

tree/ntuple/v7/inc/ROOT/RNTuple.hxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@ public:
473473
std::string_view ntupleName,
474474
std::string_view storage,
475475
const RNTupleWriteOptions &options = RNTupleWriteOptions());
476+
static std::unique_ptr<RNTupleWriter> Recreate(std::initializer_list<std::pair<std::string_view, std::string_view>> fields,
477+
std::string_view ntupleName,
478+
std::string_view storage,
479+
const RNTupleWriteOptions &options = RNTupleWriteOptions());
476480
/// Throws an exception if the model is null.
477481
static std::unique_ptr<RNTupleWriter> Append(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
478482
TFile &file,

tree/ntuple/v7/src/RNTuple.cxx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,21 @@ ROOT::Experimental::RNTupleWriter::Recreate(std::unique_ptr<RNTupleModel> model,
365365
return Create(std::move(model), std::move(sink), options);
366366
}
367367

368+
std::unique_ptr<ROOT::Experimental::RNTupleWriter>
369+
ROOT::Experimental::RNTupleWriter::Recreate(std::initializer_list<std::pair<std::string_view, std::string_view>> fields, std::string_view ntupleName,
370+
std::string_view storage, const RNTupleWriteOptions &options)
371+
{
372+
auto sink = Detail::RPagePersistentSink::Create(ntupleName, storage, options);
373+
auto model = RNTupleModel::Create();
374+
for ( const auto& fieldDesc : fields ) {
375+
std::string typeName(fieldDesc.first);
376+
std::string fieldName(fieldDesc.second);
377+
auto field = RFieldBase::Create(fieldName, typeName);
378+
model->AddField(field.Unwrap());
379+
}
380+
return Create(std::move(model), std::move(sink), options);
381+
}
382+
368383
std::unique_ptr<ROOT::Experimental::RNTupleWriter>
369384
ROOT::Experimental::RNTupleWriter::Append(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName, TFile &file,
370385
const RNTupleWriteOptions &options)

tree/ntuple/v7/test/ntuple_basics.cxx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,47 @@ TEST(RNTuple, WriteRead)
147147
EXPECT_STREQ("abc", rdKlass->s.c_str());
148148
}
149149

150+
TEST(RNTuple, WriteReadInlinedModel)
151+
{
152+
FileRaii fileGuard("test_ntuple_writeread_inlinedmodel.root");
153+
154+
{
155+
auto writer = RNTupleWriter::Recreate({
156+
{"std::uint32_t", "id"},
157+
{"std::vector<float>", "vpx"},
158+
{"std::vector<float>", "vpy"},
159+
{"std::vector<float>", "vpz"},
160+
}, "NTuple", fileGuard.GetPath());
161+
162+
auto entry = writer->CreateEntry();
163+
*entry->GetPtr<std::uint32_t>("id") = 1;
164+
*entry->GetPtr<std::vector<float>>("vpx") = {1.0, 1.1, 1.2};
165+
*entry->GetPtr<std::vector<float>>("vpy") = {2.0, 2.1, 2.2};
166+
*entry->GetPtr<std::vector<float>>("vpz") = {3.0, 3.1, 3.2};
167+
168+
writer->Fill(*entry);
169+
}
170+
171+
auto reader = RNTupleReader::Open("NTuple", fileGuard.GetPath());
172+
EXPECT_EQ(1U, reader->GetNEntries());
173+
reader->LoadEntry(0);
174+
auto readid = reader->GetModel().GetDefaultEntry().GetPtr<std::uint32_t>("id");
175+
EXPECT_EQ(1, *readid);
176+
auto readvpx = reader->GetModel().GetDefaultEntry().GetPtr<std::vector<float>>("vpx");
177+
EXPECT_FLOAT_EQ(1.0, (*readvpx)[0]);
178+
EXPECT_FLOAT_EQ(1.1, (*readvpx)[1]);
179+
EXPECT_FLOAT_EQ(1.2, (*readvpx)[2]);
180+
auto readvpy = reader->GetModel().GetDefaultEntry().GetPtr<std::vector<float>>("vpy");
181+
EXPECT_FLOAT_EQ(2.0, (*readvpy)[0]);
182+
EXPECT_FLOAT_EQ(2.1, (*readvpy)[1]);
183+
EXPECT_FLOAT_EQ(2.2, (*readvpy)[2]);
184+
auto readvpz = reader->GetModel().GetDefaultEntry().GetPtr<std::vector<float>>("vpz");
185+
EXPECT_FLOAT_EQ(3.0, (*readvpz)[0]);
186+
EXPECT_FLOAT_EQ(3.1, (*readvpz)[1]);
187+
EXPECT_FLOAT_EQ(3.2, (*readvpz)[2]);
188+
189+
}
190+
150191
TEST(RNTuple, FileAnchor)
151192
{
152193
FileRaii fileGuard("test_ntuple_file_anchor.root");

0 commit comments

Comments
 (0)