From 31b145e3fb95ada06a142170d554626e840cb5b9 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:23:20 +0100 Subject: [PATCH 1/4] DPL Analysis: fewer round trips when the native AOD reader opens a file Opening a file took 7 sequential reads: the header, the top directory record, the key list, the metaData map, the first DF's directory record, its key list and its TTrees. ROOT's TFile::Init takes 3. Each is a round trip on a remote file. - The first read takes 4 KiB instead of 128 bytes: it then also holds the top directory record and, as ROOT writes them first, the first DF's. - metaData is read when asked for: the reader never asks (metadata discovery opens the file with ROOT). Now 4 reads for an AO2D with metaData, 5 for a derived one with parentFiles. --- Framework/AODNative/include/AODNative/Container.h | 3 +++ Framework/AODNative/src/Container.cxx | 10 +++++++--- Framework/AnalysisSupport/src/NativeAOD.cxx | 1 - Framework/AnalysisSupport/src/NativeAOD.h | 6 +++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Framework/AODNative/include/AODNative/Container.h b/Framework/AODNative/include/AODNative/Container.h index 567927542a8fc..257bd89cd0d35 100644 --- a/Framework/AODNative/include/AODNative/Container.h +++ b/Framework/AODNative/include/AODNative/Container.h @@ -87,6 +87,9 @@ class RootFile std::shared_ptr mSource; std::string mUrl; FileHeader mHeader; + // The first bytes of the file, read with the header: they also hold the + // directory records of the file and, usually, of its first DF. + std::vector mHead; std::optional> mTopKeys; // Stored bytes of the TTrees of the last DF listed, by key offset, until read. std::map> mPrefetched; diff --git a/Framework/AODNative/src/Container.cxx b/Framework/AODNative/src/Container.cxx index 5136c8557afcb..665b7efbf7dba 100644 --- a/Framework/AODNative/src/Container.cxx +++ b/Framework/AODNative/src/Container.cxx @@ -25,6 +25,9 @@ namespace // Big enough for the file header and for a TDirectory record (including its UUID). constexpr size_t kHeaderReadSize = 128; constexpr size_t kDirectoryReadSize = 64; +// Read at open: the header, the top directory record and, as ROOT writes them +// first, the key and directory record of the first DF, in one round trip. +constexpr size_t kHeadReadSize = 4096; // Largest gap between the TTrees of a DF still read together with them. constexpr uint64_t kMetadataGap = 4 * 1024; // Smallest possible key header: 32-bit seeks and three empty strings. @@ -49,8 +52,8 @@ RootFile::RootFile(std::shared_ptr source) : mSource(std::move(source)), mUrl(mSource->url()) { - auto buf = readRange(*mSource, 0, std::min(kHeaderReadSize, mSource->size())); - Cursor c{buf, mUrl}; + mHead = readRange(*mSource, 0, std::min(kHeadReadSize, mSource->size())); + Cursor c{mHead, mUrl}; auto magic = c.bytes(4); if (std::memcmp(magic.data(), "root", 4) != 0) { throw FormatError(mUrl + " is not a ROOT file"); @@ -100,7 +103,8 @@ DirectoryInfo RootFile::readDirectory(uint64_t offset) if (offset >= mSource->size()) { throw CorruptData(mUrl, offset, "directory record past the end of the file"); } - auto buf = readRange(*mSource, offset, std::min(kDirectoryReadSize, mSource->size() - offset)); + auto len = std::min(kDirectoryReadSize, mSource->size() - offset); + auto buf = offset + len <= mHead.size() ? std::vector(mHead.begin() + offset, mHead.begin() + offset + len) : readRange(*mSource, offset, len); Cursor c{buf, mUrl, offset}; DirectoryInfo d; d.version = c.i16(); diff --git a/Framework/AnalysisSupport/src/NativeAOD.cxx b/Framework/AnalysisSupport/src/NativeAOD.cxx index 4c4fb6ef31cb8..09bb1142e4e7b 100644 --- a/Framework/AnalysisSupport/src/NativeAOD.cxx +++ b/Framework/AnalysisSupport/src/NativeAOD.cxx @@ -338,7 +338,6 @@ std::shared_ptr NativeAODInputFile::open(std::unique_ptrmFilesystem = std::make_shared(result->mFile, options, factory); result->mFilesystem->setName(result->mName); result->mParentFiles = result->mFile->readStringMap("parentFiles"); - result->mMetaData = result->mFile->readStringMap("metaData"); // Parse the trees of the first DF now: a layout or version we do not support // is then found while the file can still be read with ROOT instead. auto dfs = result->mFile->dataFrames(); diff --git a/Framework/AnalysisSupport/src/NativeAOD.h b/Framework/AnalysisSupport/src/NativeAOD.h index 70e1b515ff547..7632fc704d4ac 100644 --- a/Framework/AnalysisSupport/src/NativeAOD.h +++ b/Framework/AnalysisSupport/src/NativeAOD.h @@ -126,7 +126,7 @@ class NativeAODFileSystem : public VirtualRootFileSystemBase class NativeAODInputFile : public AODInputFile { public: - /// Opens @a url and parses its header, keys, maps and the trees of its first + /// Opens @a url and parses its header, keys, parent files and the trees of its first /// DF, so that layouts and versions we cannot read are found here, when the /// file can still go to ROOT. Throws o2::aod::native::FormatError. /// @a name is what the file is called (an LFN for a grid file); @a url by default. @@ -144,7 +144,8 @@ class NativeAODInputFile : public AODInputFile std::vector topLevelKeys() override; std::vector keysIn(std::string const& dir) override; std::map parentFiles() override { return mParentFiles; } - std::map metaData() override { return mMetaData; } + /// Read when asked for: the reader does not need it, and it costs a round trip at open. + std::map metaData() override { return mFile->readStringMap("metaData"); } std::shared_ptr filesystem() override { return mFilesystem; } void setReadOrder(std::vector const& dfs) override { mFilesystem->setReadOrder(dfs); } @@ -156,7 +157,6 @@ class NativeAODInputFile : public AODInputFile std::shared_ptr mFile; std::shared_ptr mFilesystem; std::map mParentFiles; - std::map mMetaData; }; } // namespace o2::framework From 04263c0433a2e9fbada2eefb5f9ac1a444d7e6d2 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:23:20 +0100 Subject: [PATCH 2/4] DPL Analysis: time in open_time what ROOT times for the native AOD reader TJAlienFile::GetElapsed(), the open_time of the ROOT reader, covers the open of the replica which is read and TFile::Init. The native reader's also covered the catalogue lookup, replicas which failed to open, retry delays, parentFiles and the first DF's directory, keys and trees, so the two could not be compared. open_time now covers the open of the replica read and its header and keys. The rest goes in new fields of the same lines, before se= so that open_time stays last: access_time (catalogue), failover_time (failed opens, retry delays and reopens, for the life of the file) and prepare_time (parentFiles and the first DF). --- .../AODNative/include/AODNative/ByteSource.h | 3 +++ .../AODNative/src/FailoverByteSource.cxx | 17 ++++++++++++ Framework/AODNative/src/FailoverByteSource.h | 2 ++ .../AnalysisSupport/src/AODInputFile.cxx | 4 ++- Framework/AnalysisSupport/src/NativeAOD.cxx | 26 +++++++++++++++---- Framework/AnalysisSupport/src/NativeAOD.h | 9 ++++--- 6 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Framework/AODNative/include/AODNative/ByteSource.h b/Framework/AODNative/include/AODNative/ByteSource.h index 9ba49c284faa2..46c6cd98ab60b 100644 --- a/Framework/AODNative/include/AODNative/ByteSource.h +++ b/Framework/AODNative/include/AODNative/ByteSource.h @@ -98,6 +98,9 @@ class ByteSource virtual std::string url() const = 0; /// The label of the replica being read, for a source opened with openReplicas(). virtual std::string replicaLabel() const { return {}; } + /// Seconds spent so far on replicas which failed to open, retry delays and + /// reopens, for a source opened with openReplicas(). + virtual double failoverSeconds() const { return 0; } static std::unique_ptr open(std::string url, const Options&); /// Opens the first replica which opens, and on a read error retries it /// (Options::readRetries times, reopened) and then moves on to the next diff --git a/Framework/AODNative/src/FailoverByteSource.cxx b/Framework/AODNative/src/FailoverByteSource.cxx index c44c1640fe80d..f421d15f01848 100644 --- a/Framework/AODNative/src/FailoverByteSource.cxx +++ b/Framework/AODNative/src/FailoverByteSource.cxx @@ -20,6 +20,20 @@ namespace o2::aod::native namespace { +/// Adds the time until it goes out of scope to @a total, unless cancelled. +struct FailoverTimer { + std::atomic& total; + std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); + bool counting = true; + void cancel() { counting = false; } + ~FailoverTimer() + { + if (counting) { + total.fetch_add(std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count(), std::memory_order_relaxed); + } + } +}; + std::string labelOf(Replica const& r) { return r.label.empty() ? r.url.substr(0, r.url.find('?')) : r.label; @@ -33,10 +47,12 @@ FailoverByteSource::FailoverByteSource(std::vector replicas, Options co throw IoError("(no replica)", "no replica to open"); } for (mIndex = 0; mIndex < mReplicas.size(); ++mIndex) { + FailoverTimer timer{mFailoverNs}; try { mSource = openReplica(mIndex); mSize = mSource->size(); mRemote = mSource->isRemote(); + timer.cancel(); return; } catch (IoError const& e) { report("Could not open replica on " + labelOf(mReplicas[mIndex]) + ": " + e.what()); @@ -109,6 +125,7 @@ void FailoverByteSource::withFailover(std::function const& re return; } catch (IoError const& e) { std::lock_guard lock{mMutex}; + FailoverTimer timer{mFailoverNs}; if (generation != mGeneration) { continue; // another read already replaced the replica: try the new one } diff --git a/Framework/AODNative/src/FailoverByteSource.h b/Framework/AODNative/src/FailoverByteSource.h index 15e0aee055f75..a4564c86a95e5 100644 --- a/Framework/AODNative/src/FailoverByteSource.h +++ b/Framework/AODNative/src/FailoverByteSource.h @@ -46,6 +46,7 @@ class FailoverByteSource : public ByteSource uint64_t readCalls() const override; std::string url() const override { return current()->url(); } std::string replicaLabel() const override; + double failoverSeconds() const override { return mFailoverNs.load(std::memory_order_relaxed) / 1e9; } private: std::shared_ptr current() const; @@ -71,6 +72,7 @@ class FailoverByteSource : public ByteSource bool mRemote = false; std::atomic mRetiredBytes = 0; std::atomic mRetiredCalls = 0; + std::atomic mFailoverNs = 0; }; } // namespace o2::aod::native diff --git a/Framework/AnalysisSupport/src/AODInputFile.cxx b/Framework/AnalysisSupport/src/AODInputFile.cxx index d73df2924587a..0e369e8d6824a 100644 --- a/Framework/AnalysisSupport/src/AODInputFile.cxx +++ b/Framework/AnalysisSupport/src/AODInputFile.cxx @@ -149,6 +149,7 @@ std::shared_ptr openNativeAOD(std::string const& filename, RootObj } auto start = std::chrono::steady_clock::now(); auto replicas = gridReplicas(filename); + auto accessed = std::chrono::steady_clock::now(); if (replicas.empty()) { throw o2::aod::native::IoError(filename, "no replica the native reader can read"); } @@ -161,9 +162,10 @@ std::shared_ptr openNativeAOD(std::string const& filename, RootObj auto source = o2::aod::native::ByteSource::openReplicas(std::move(list), options, [filename](std::string const& message) { LOGP(info, "\"{}\": {}", filename, message); }); + auto replicaOpen = std::chrono::duration(std::chrono::steady_clock::now() - accessed).count() - source->failoverSeconds(); auto se = source->replicaLabel(); auto file = NativeAODInputFile::open(std::move(source), factory, options, filename); - file->setGridInfo(se, std::chrono::duration(std::chrono::steady_clock::now() - start).count()); + file->setGridInfo(se, std::chrono::duration(accessed - start).count(), replicaOpen); return file; } diff --git a/Framework/AnalysisSupport/src/NativeAOD.cxx b/Framework/AnalysisSupport/src/NativeAOD.cxx index 09bb1142e4e7b..350191be11091 100644 --- a/Framework/AnalysisSupport/src/NativeAOD.cxx +++ b/Framework/AnalysisSupport/src/NativeAOD.cxx @@ -13,6 +13,7 @@ #include "AODNative/Errors.h" #include "Framework/RuntimeError.h" #include +#include #include namespace o2::framework @@ -334,7 +335,16 @@ std::shared_ptr NativeAODInputFile::open(std::unique_ptr(new NativeAODInputFile); result->mName = name; + auto start = std::chrono::steady_clock::now(); + double failover = source->failoverSeconds(); result->mFile = std::make_shared(std::move(source)); + // What TFile::Init reads (header and keys) counts as opening, as for ROOT; + // the rest is the preparation of the first DF. + result->mFile->topKeys(); + auto opened = std::chrono::steady_clock::now(); + auto& file = result->mFile->source(); + result->mOpenTime = std::chrono::duration(opened - start).count() - (file.failoverSeconds() - failover); + failover = file.failoverSeconds(); result->mFilesystem = std::make_shared(result->mFile, options, factory); result->mFilesystem->setName(result->mName); result->mParentFiles = result->mFile->readStringMap("parentFiles"); @@ -348,13 +358,15 @@ std::shared_ptr NativeAODInputFile::open(std::unique_ptrmPrepareTime = std::chrono::duration(std::chrono::steady_clock::now() - opened).count() - (file.failoverSeconds() - failover); return result; } -void NativeAODInputFile::setGridInfo(std::string se, double openTime) +void NativeAODInputFile::setGridInfo(std::string se, double accessTime, double replicaOpenTime) { mSE = std::move(se); - mOpenTime = openTime; + mAccessTime = accessTime; + mOpenTime += replicaOpenTime; } std::string NativeAODInputFile::monitoringDetails() const @@ -362,9 +374,13 @@ std::string NativeAODInputFile::monitoringDetails() const if (mSE.empty()) { return ""; } - char openTime[32]; - std::snprintf(openTime, sizeof(openTime), "%.1f", mOpenTime); - return ",se=" + mSE + ",open_time=" + openTime; + // open_time covers what TJAlienFile::GetElapsed() does for ROOT: opening the + // replica which is read and TFile::Init. The other times come before it, so + // that open_time stays last in the open line, as for ROOT. + char times[256]; + std::snprintf(times, sizeof(times), ",access_time=%.3f,failover_time=%.3f,prepare_time=%.3f,se=%s,open_time=%.1f", + mAccessTime, mFile->source().failoverSeconds(), mPrepareTime, mSE.c_str(), mOpenTime); + return times; } uint64_t NativeAODInputFile::size() const { return mFile->source().size(); } diff --git a/Framework/AnalysisSupport/src/NativeAOD.h b/Framework/AnalysisSupport/src/NativeAOD.h index 7632fc704d4ac..d60148515e292 100644 --- a/Framework/AnalysisSupport/src/NativeAOD.h +++ b/Framework/AnalysisSupport/src/NativeAOD.h @@ -133,8 +133,9 @@ class NativeAODInputFile : public AODInputFile static std::shared_ptr open(std::string const& url, RootObjectReadingFactory& factory, o2::aod::native::Options const& options, std::string const& name = {}); /// The same, reading from @a source (e.g. the replicas of a grid file). static std::shared_ptr open(std::unique_ptr source, RootObjectReadingFactory& factory, o2::aod::native::Options const& options, std::string const& name); - /// The storage element and the seconds the grid lookup and open took, for monitoring. - void setGridInfo(std::string se, double openTime); + /// For monitoring: the storage element, the seconds the catalogue lookup took, + /// and those the opening of the replica read took (failed replicas excluded). + void setGridInfo(std::string se, double accessTime, double replicaOpenTime); std::string monitoringDetails() const override; std::string name() const override { return mName; } @@ -153,7 +154,9 @@ class NativeAODInputFile : public AODInputFile NativeAODInputFile() = default; std::string mName; std::string mSE; - double mOpenTime = 0; + double mAccessTime = 0; + double mOpenTime = 0; // replica open and the reads of TFile::Init, as TJAlienFile + double mPrepareTime = 0; // parentFiles and the first DF's directory, keys and trees std::shared_ptr mFile; std::shared_ptr mFilesystem; std::map mParentFiles; From ca88cd3df098e55711065c00669027b4ebc5c04f Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:23:20 +0100 Subject: [PATCH 3/4] DPL Analysis: prefetch only the learned TTrees from the second DF on in the native reader The trees to prefetch were set when the plan of a new DF was made, but the DF's keys are listed earlier, when DPL asks for the tree handle. So the second DF of a file still fetched every TTree, as the first. Set the list as trees join the plan instead. --- Framework/AnalysisSupport/src/NativeAOD.cxx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Framework/AnalysisSupport/src/NativeAOD.cxx b/Framework/AnalysisSupport/src/NativeAOD.cxx index 350191be11091..6ac52b2085280 100644 --- a/Framework/AnalysisSupport/src/NativeAOD.cxx +++ b/Framework/AnalysisSupport/src/NativeAOD.cxx @@ -98,9 +98,6 @@ std::shared_ptr NativeAODFileSystem::planFor(std::string const& path) if (std::ranges::find(trees, treeName) == trees.end()) { trees.push_back(treeName); } - if (!previous.empty()) { - mFile->setTreesToPrefetch(trees); - } for (auto& name : previous) { addToPlan(name); } @@ -256,6 +253,8 @@ void NativeAODFileSystem::addToPlan(std::string const& treeName) mPlanBytes += bytes; mPlanOf[treeName] = mPlan; mPlanTrees.push_back(treeName); + // The next DF's keys can be listed before its plan is made (by getTreeHandle). + mFile->setTreesToPrefetch(mPlanTrees); } } From 462a584272375bc9d9ef98411aa6d8dafc96749a Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:23:20 +0100 Subject: [PATCH 4/4] AODNative: read the directory records and key lists of the next DFs ahead Listing a DF took two dependent reads, its directory record and then its key list, each a round trip on a remote file. Read those of the next 16 DFs, in read order, with them: one vector read for the records and one for the key lists, at most 1 MiB of key lists. Metadata only; the cache is replaced at each read-ahead. --- .../AODNative/include/AODNative/Container.h | 8 ++ Framework/AODNative/src/Container.cxx | 81 ++++++++++++++++++- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/Framework/AODNative/include/AODNative/Container.h b/Framework/AODNative/include/AODNative/Container.h index 257bd89cd0d35..b14b5d0793384 100644 --- a/Framework/AODNative/include/AODNative/Container.h +++ b/Framework/AODNative/include/AODNative/Container.h @@ -81,6 +81,11 @@ class RootFile KeyInfo parseKey(std::span buf, uint64_t fileOffset); DirectoryInfo readDirectory(uint64_t offset); std::vector readKeyList(DirectoryInfo const&); + /// The bytes at @a offset: from the head, the metadata read ahead, or the source. + std::vector readMetadata(uint64_t offset, size_t len); + /// Reads the directory records and key lists of DF @a df and of the next + /// ones not listed yet, in two vector reads, into mAhead. + void listAhead(std::string const& df); std::optional findTopKey(std::string const& name); std::vector readStored(const KeyInfo&); @@ -94,6 +99,9 @@ class RootFile // Stored bytes of the TTrees of the last DF listed, by key offset, until read. std::map> mPrefetched; std::map> mKeysOf; + // Directory records and key lists of the next DFs, by offset (small and capped). + std::map> mAhead; + std::vector mOrder; // dataFrames(), for the read-ahead std::vector mTreesToPrefetch; std::map> mStringMaps; }; diff --git a/Framework/AODNative/src/Container.cxx b/Framework/AODNative/src/Container.cxx index 665b7efbf7dba..7578fecf0e0e0 100644 --- a/Framework/AODNative/src/Container.cxx +++ b/Framework/AODNative/src/Container.cxx @@ -28,6 +28,10 @@ constexpr size_t kDirectoryReadSize = 64; // Read at open: the header, the top directory record and, as ROOT writes them // first, the key and directory record of the first DF, in one round trip. constexpr size_t kHeadReadSize = 4096; +// Directory records and key lists of the next DFs read along with those of a +// DF: at most this many DFs and this many bytes of key lists. +constexpr size_t kListAheadDFs = 16; +constexpr uint64_t kListAheadBytes = 1024 * 1024; // Largest gap between the TTrees of a DF still read together with them. constexpr uint64_t kMetadataGap = 4 * 1024; // Smallest possible key header: 32-bit seeks and three empty strings. @@ -103,8 +107,7 @@ DirectoryInfo RootFile::readDirectory(uint64_t offset) if (offset >= mSource->size()) { throw CorruptData(mUrl, offset, "directory record past the end of the file"); } - auto len = std::min(kDirectoryReadSize, mSource->size() - offset); - auto buf = offset + len <= mHead.size() ? std::vector(mHead.begin() + offset, mHead.begin() + offset + len) : readRange(*mSource, offset, len); + auto buf = readMetadata(offset, std::min(kDirectoryReadSize, mSource->size() - offset)); Cursor c{buf, mUrl, offset}; DirectoryInfo d; d.version = c.i16(); @@ -124,7 +127,7 @@ std::vector RootFile::readKeyList(DirectoryInfo const& dir) if (dir.seekKeys == 0 || dir.nbytesKeys <= 0) { return {}; } - auto buf = readRange(*mSource, dir.seekKeys, dir.nbytesKeys); + auto buf = readMetadata(dir.seekKeys, dir.nbytesKeys); auto header = parseKey(buf, dir.seekKeys); Cursor c{buf, mUrl, dir.seekKeys}; c.seek(header.keylen); @@ -144,6 +147,67 @@ std::vector RootFile::readKeyList(DirectoryInfo const& dir) return keys; } +std::vector RootFile::readMetadata(uint64_t offset, size_t len) +{ + if (offset + len <= mHead.size()) { + return {mHead.begin() + offset, mHead.begin() + offset + len}; + } + if (auto it = mAhead.find(offset); it != mAhead.end() && it->second.size() == len) { + return it->second; + } + return readRange(*mSource, offset, len); +} + +void RootFile::listAhead(std::string const& df) +{ + mAhead.clear(); + auto fetch = [this](std::vector> const& ranges) { + std::vector requests; + for (auto [offset, len] : ranges) { + auto& bytes = mAhead[offset]; + bytes.resize(len); + requests.push_back({offset, len, bytes.data()}); + } + if (!requests.empty()) { + mSource->readvInto(requests); + } + }; + // The directory records of this DF and of the next ones, in read order. + std::vector records; + std::vector> ranges; + if (mOrder.empty()) { + mOrder = dataFrames(); + } + for (auto it = std::ranges::find(mOrder, df); it != mOrder.end() && records.size() < kListAheadDFs; ++it) { + auto key = findTopKey(*it); + auto offset = key ? key->seekKey + key->keylen : 0; + if (!key || (*it != df && mKeysOf.contains(*it)) || (key->className != "TDirectoryFile" && key->className != "TDirectory") || key->compressed() || offset + kDirectoryReadSize > mSource->size()) { + continue; + } + records.push_back(offset); + if (offset + kDirectoryReadSize > mHead.size()) { + ranges.emplace_back(offset, kDirectoryReadSize); + } + } + fetch(ranges); + // Then their key lists. Anything odd is left to the normal path, which reports it. + ranges.clear(); + uint64_t bytes = 0; + for (auto offset : records) { + try { + auto dir = readDirectory(offset); + if (dir.seekKeys == 0 || dir.nbytesKeys <= 0 || dir.seekKeys > mSource->size() || (uint64_t)dir.nbytesKeys > mSource->size() - dir.seekKeys || + (!ranges.empty() && bytes + dir.nbytesKeys > kListAheadBytes)) { + continue; + } + bytes += dir.nbytesKeys; + ranges.emplace_back(dir.seekKeys, dir.nbytesKeys); + } catch (FormatError const&) { + } + } + fetch(ranges); +} + const std::vector& RootFile::topKeys() { if (!mTopKeys) { @@ -235,7 +299,16 @@ std::vector RootFile::keysOf(const std::string& df) if (key->compressed()) { throw UnsupportedLayout("compressed directory record for " + df + " in " + mUrl); } - auto dir = readDirectory(key->seekKey + key->keylen); + // The records of the next DFs are read ahead with those of this one; the + // first DF's directory record comes with the head, its key list does not. + auto offset = key->seekKey + key->keylen; + if (!mSource->canMap() && offset + kDirectoryReadSize > mHead.size() && !mAhead.contains(offset)) { + listAhead(df); + } + auto dir = readDirectory(offset); + if (!mSource->canMap() && !mAhead.contains(dir.seekKeys)) { + listAhead(df); + } auto keys = highestCycles(readKeyList(dir)); // The TTrees of a DF are written next to each other: ranges less than