From e96b9a043c44434fcb8ae9a0ec8b32f34b13192b Mon Sep 17 00:00:00 2001 From: Vincenzo Eduardo Padulano Date: Thu, 12 Nov 2026 18:29:22 +0100 Subject: [PATCH 1/2] [df] Avoid processing a first empty range from data source Add an early quit condition in RunDataSource to avoid processing a data source that has zero entries to begin with when running with a single thread. (cherry picked from commit 62a641fa2f9c12299ac80ec133c2c56768ac688e) --- tree/dataframe/src/RLoopManager.cxx | 2 ++ tree/dataframe/test/datasource_ntuple.cxx | 37 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/tree/dataframe/src/RLoopManager.cxx b/tree/dataframe/src/RLoopManager.cxx index d750b4dfa8ddf..d8dc764612871 100644 --- a/tree/dataframe/src/RLoopManager.cxx +++ b/tree/dataframe/src/RLoopManager.cxx @@ -614,6 +614,8 @@ void RLoopManager::RunDataSource() ROOT::Internal::RDF::RDSRangeRAII __{*this, 0u, 0ull}; ranges = fDataSource->GetEntryRanges(); + if (ranges.empty()) + break; fSampleInfos[0] = ROOT::Internal::RDF::CreateSampleInfo(*fDataSource, /*slot*/ 0, fSampleMap); diff --git a/tree/dataframe/test/datasource_ntuple.cxx b/tree/dataframe/test/datasource_ntuple.cxx index 7645f7026b85c..a30c5245785bc 100644 --- a/tree/dataframe/test/datasource_ntuple.cxx +++ b/tree/dataframe/test/datasource_ntuple.cxx @@ -915,3 +915,40 @@ TEST_F(RNTupleDSTest, GetTopLevelFieldNames) std::vector{"VecElectron", "VecElectron.pt", "electron", "electron.pt", "energy", "jets", "nElectron", "nnlo", "pt", "rvec", "tag"}); } + +void ReadSingleEmptyRNTuple(const std::string &dataset_name, const std::string &file_name) +{ + FileRAII fileGuard(file_name); + { + ROOT::RDataFrame df{1}; + // Filter away everything creating an output with zero entries + auto flt = df.Define("x", []() -> int { return 42; }).Filter([](int x) { return x > 42; }, {"x"}); + + ROOT::RDF::RSnapshotOptions opts; + opts.fOutputFormat = ROOT::RDF::ESnapshotOutputFormat::kRNTuple; + + flt.Snapshot(dataset_name, file_name, {"x"}, opts); + } + { + ROOT::RDataFrame df{dataset_name, fileGuard.GetPath()}; + EXPECT_EQ(df.Count().GetValue(), 0); + } +} + +TEST(EmptyRNTupleTest, Seq) +{ + std::string dataset_name{"ReadSingleEmptyRNTuple"}; + std::string file_name{"ReadSingleEmptyRNTuple.root"}; + ReadSingleEmptyRNTuple(dataset_name, file_name); +} + +#ifdef R__USE_IMT +TEST(EmptyRNTupleTest, MT) +{ + IMTRAII _; + + std::string dataset_name{"ReadSingleEmptyRNTupleMT"}; + std::string file_name{"ReadSingleEmptyRNTupleMT.root"}; + ReadSingleEmptyRNTuple(dataset_name, file_name); +} +#endif From 9160a01ef59daf5edfc34e9e73d1f200b82f0ff1 Mon Sep 17 00:00:00 2001 From: Vincenzo Eduardo Padulano Date: Thu, 19 Nov 2026 17:01:13 +0100 Subject: [PATCH 2/2] [df] Guard against empty page source in RNTupleDS When building tasks for IMT, RNTupleDS acesses the cluster boundaries. In 6.42 and before commit https://github.com/root-project/root/commit/043e7afa35192b5e805bb745ef9bc3a9a36b5fdb, this operation may trigger an out-of-bounds access, so we skip it entirely in case the page source has zero entries. --- tree/dataframe/src/RNTupleDS.cxx | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tree/dataframe/src/RNTupleDS.cxx b/tree/dataframe/src/RNTupleDS.cxx index 97953c08bc86e..7fa44868cfa8b 100644 --- a/tree/dataframe/src/RNTupleDS.cxx +++ b/tree/dataframe/src/RNTupleDS.cxx @@ -843,7 +843,12 @@ ROOT::Internal::RDF::GetClustersAndEntries(std::string_view ntupleName, std::str // TODO(jblomer): improve MT scheduling such that not all page lists are required right away source->LoadAllPageLists(); auto descGuard = source->GetSharedDescriptorGuard(); - return std::make_pair(ROOT::Internal::GetClusterBoundaries(descGuard.GetConstRef()), descGuard->GetNEntries()); + ROOT::NTupleSize_t nEntries = descGuard->GetNEntries(); + // TODO: In ROOT 6.42.00, writing an empty RNTuple with the RNTupleParallelWriter results in a RNTuple which + // cluster boundaries cannot be properly retrieved. Avoid that scenario for now with an early return. + if (nEntries == 0) + return {{}, nEntries}; + return std::make_pair(ROOT::Internal::GetClusterBoundaries(descGuard.GetConstRef()), nEntries); } #ifdef R__USE_IMT @@ -1120,9 +1125,15 @@ void ROOT::RDF::RNTupleDS::ScheduleTasksForFile(std::size_t fileIdx, ROOT::TThre source->Attach(); // TODO: improve MT scheduling such that not all page lists are required right away source->LoadAllPageLists(); - const auto clustersAndEntries = [&]() { + const auto clustersAndEntries = + [&]() -> std::pair, ROOT::NTupleSize_t> { const auto descGuard = source->GetSharedDescriptorGuard(); - return std::make_pair(ROOT::Internal::GetClusterBoundaries(descGuard.GetConstRef()), descGuard->GetNEntries()); + ROOT::NTupleSize_t nEntries = descGuard->GetNEntries(); + // TODO: In ROOT 6.42.00, writing an empty RNTuple with the RNTupleParallelWriter results in a RNTuple which + // cluster boundaries cannot be properly retrieved. Avoid that scenario for now with an early return. + if (nEntries == 0) + return {{}, nEntries}; + return std::make_pair(ROOT::Internal::GetClusterBoundaries(descGuard.GetConstRef()), nEntries); }(); const ROOT::NTupleSize_t nEntries = clustersAndEntries.second; if (nEntries == 0)