Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b7054b7

Browse files
committedMar 10, 2025
[ntuple] Add tests for TMemFile-based reading
1 parent 0683fb2 commit b7054b7

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
 

‎tree/ntuple/v7/test/ntuple_processor.cxx

+31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <ROOT/RNTupleProcessor.hxx>
44

5+
#include <TMemFile.h>
6+
57
TEST(RNTupleProcessor, EmptyNTuple)
68
{
79
FileRaii fileGuard("test_ntuple_processor_empty.root");
@@ -22,6 +24,35 @@ TEST(RNTupleProcessor, EmptyNTuple)
2224
EXPECT_EQ(nEntries, proc->GetNEntriesProcessed());
2325
}
2426

27+
TEST(RNTupleProcessor, TMemFile)
28+
{
29+
{
30+
auto memFile = std::make_unique<TMemFile>("ntuple", "RECREATE");
31+
auto model = RNTupleModel::Create();
32+
auto fldX = model->MakeField<float>("x");
33+
auto ntuple = RNTupleWriter::Append(std::move(model), "ntuple", *memFile);
34+
35+
for (unsigned i = 0; i < 5; ++i) {
36+
*fldX = static_cast<float>(i);
37+
ntuple->Fill();
38+
}
39+
}
40+
41+
auto memFile = std::make_unique<TMemFile>("ntuple");
42+
auto proc = RNTupleProcessor::Create({"ntuple", memFile.get()});
43+
auto x = proc->GetEntry().GetPtr<float>("x");
44+
45+
int nEntries = 0;
46+
for ([[maybe_unused]] const auto &entry : *proc) {
47+
EXPECT_EQ(++nEntries, proc->GetNEntriesProcessed());
48+
EXPECT_EQ(nEntries - 1, proc->GetCurrentEntryNumber());
49+
50+
EXPECT_FLOAT_EQ(static_cast<float>(nEntries - 1), *x);
51+
}
52+
EXPECT_EQ(nEntries, 5);
53+
EXPECT_EQ(nEntries, proc->GetNEntriesProcessed());
54+
}
55+
2556
class RNTupleProcessorTest : public testing::Test {
2657
protected:
2758
const std::array<std::string, 3> fFileNames{"test_ntuple_processor1.root", "test_ntuple_processor2.root",

‎tree/ntuple/v7/test/ntuple_processor_chain.cxx

+43
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <ROOT/RNTupleProcessor.hxx>
44

5+
#include <TMemFile.h>
6+
57
class RNTupleChainProcessorTest : public testing::Test {
68
protected:
79
const std::array<std::string, 4> fFileNames{"test_ntuple_chain_processor1.root", "test_ntuple_chain_processor2.root",
@@ -258,3 +260,44 @@ TEST_F(RNTupleChainProcessorTest, LoadRandomEntry)
258260

259261
EXPECT_EQ(ROOT::kInvalidNTupleIndex, RNTupleProcessorEntryLoader::LoadEntry(*proc, 8));
260262
}
263+
264+
TEST(RNTupleChainProcessor, TMemFile)
265+
{
266+
FileRaii fileGuard("test_ntuple_processor_join_tmemfile.root");
267+
{
268+
auto model = RNTupleModel::Create();
269+
auto fldX = model->MakeField<float>("x");
270+
auto ntuple = RNTupleWriter::Recreate(std::move(model), "ntuple", fileGuard.GetPath());
271+
272+
for (unsigned i = 0; i < 5; i++) {
273+
*fldX = static_cast<float>(i);
274+
ntuple->Fill();
275+
}
276+
}
277+
{
278+
auto memFile = std::make_unique<TMemFile>("ntuple", "RECREATE");
279+
auto model = RNTupleModel::Create();
280+
auto fldX = model->MakeField<float>("x");
281+
auto ntuple = RNTupleWriter::Append(std::move(model), "ntuple", *memFile);
282+
283+
for (unsigned i = 5; i < 10; ++i) {
284+
*fldX = static_cast<float>(i);
285+
ntuple->Fill();
286+
}
287+
}
288+
289+
auto memFile = std::make_unique<TMemFile>("ntuple");
290+
291+
auto proc = RNTupleProcessor::CreateChain({{"ntuple", fileGuard.GetPath()}, {"ntuple", memFile.get()}});
292+
293+
std::uint64_t nEntries = 0;
294+
auto x = proc->GetEntry().GetPtr<float>("x");
295+
for ([[maybe_unused]] const auto &entry : *proc) {
296+
EXPECT_EQ(++nEntries, proc->GetNEntriesProcessed());
297+
EXPECT_EQ(nEntries - 1, proc->GetCurrentEntryNumber());
298+
299+
EXPECT_EQ(static_cast<float>(nEntries - 1), *x);
300+
}
301+
EXPECT_EQ(nEntries, 10);
302+
EXPECT_EQ(nEntries, proc->GetNEntriesProcessed());
303+
}

‎tree/ntuple/v7/test/ntuple_processor_join.cxx

+52
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <ROOT/RNTupleProcessor.hxx>
44

5+
#include <TMemFile.h>
6+
57
class RNTupleJoinProcessorTest : public testing::Test {
68
protected:
79
const std::array<std::string, 4> fFileNames{"test_ntuple_join_processor1.root", "test_ntuple_join_processor2.root",
@@ -287,3 +289,53 @@ TEST_F(RNTupleJoinProcessorTest, WithModel)
287289

288290
EXPECT_EQ(5, proc->GetNEntriesProcessed());
289291
}
292+
293+
TEST(RNTupleJoinProcessor, TMemFile)
294+
{
295+
FileRaii fileGuard("test_ntuple_processor_join_tmemfile.root");
296+
{
297+
auto model = RNTupleModel::Create();
298+
auto fldI = model->MakeField<int>("i");
299+
auto fldX = model->MakeField<float>("x");
300+
auto ntuple = RNTupleWriter::Recreate(std::move(model), "ntuple", fileGuard.GetPath());
301+
302+
for (unsigned i = 0; i < 5; ++i) {
303+
*fldI = i * 2;
304+
*fldX = *fldI * 0.5f;
305+
ntuple->Fill();
306+
}
307+
}
308+
{
309+
auto memFile = std::make_unique<TMemFile>("ntuple_aux", "RECREATE");
310+
auto model = RNTupleModel::Create();
311+
auto fldI = model->MakeField<int>("i");
312+
auto fldY = model->MakeField<std::vector<float>>("y");
313+
auto ntuple = RNTupleWriter::Append(std::move(model), "ntuple_aux", *memFile);
314+
315+
for (unsigned i = 0; i < 10; ++i) {
316+
*fldI = i;
317+
*fldY = {static_cast<float>(*fldI * 0.2), 3.14, static_cast<float>(*fldI * 1.3)};
318+
ntuple->Fill();
319+
}
320+
}
321+
322+
auto memFile = std::make_unique<TMemFile>("ntuple_aux");
323+
auto proc = RNTupleProcessor::CreateJoin({"ntuple", fileGuard.GetPath()}, {{"ntuple_aux", memFile.get()}}, {"i"});
324+
325+
int nEntries = 0;
326+
auto i = proc->GetEntry().GetPtr<int>("i");
327+
auto x = proc->GetEntry().GetPtr<float>("x");
328+
auto y = proc->GetEntry().GetPtr<std::vector<float>>("ntuple_aux.y");
329+
std::vector<float> yExpected;
330+
for ([[maybe_unused]] auto &entry : *proc) {
331+
EXPECT_EQ(proc->GetCurrentEntryNumber(), nEntries++);
332+
333+
EXPECT_FLOAT_EQ(proc->GetCurrentEntryNumber() * 2, *i);
334+
EXPECT_FLOAT_EQ(*i * 0.5f, *x);
335+
336+
yExpected = {static_cast<float>(*i * 0.2), 3.14, static_cast<float>(*i * 1.3)};
337+
EXPECT_EQ(yExpected, *y);
338+
}
339+
340+
EXPECT_EQ(5, proc->GetNEntriesProcessed());
341+
}

0 commit comments

Comments
 (0)
Please sign in to comment.