From d88a82df9db2cf65a27b13ca1ce818bbbcd2eed3 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:49:55 +0100 Subject: [PATCH 1/2] AOD: add o2-aod-validator to find what interrupted writes leave behind Checks AO2D files, local or alien://, for what an interrupted write or merge leaves behind, reading only the metadata (one read per tree): - a file which was not closed (ROOT recovers its keys, but the DF folders come back without trees); - a file with no DF folder, or a DF folder with no tree; - a branch with fewer entries than its tree, or a basket with no size or location (a basket lost in a fast copy). --deep also checks that the key of every basket belongs to its branch. Exit code 0 if all files are fine, 1 if one has problems, 2 if one cannot be read. --- Framework/AODMerger/CMakeLists.txt | 5 + Framework/AODMerger/src/aodValidator.cxx | 188 +++++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 Framework/AODMerger/src/aodValidator.cxx diff --git a/Framework/AODMerger/CMakeLists.txt b/Framework/AODMerger/CMakeLists.txt index da74261cf64c8..75bd99e8731bd 100644 --- a/Framework/AODMerger/CMakeLists.txt +++ b/Framework/AODMerger/CMakeLists.txt @@ -23,3 +23,8 @@ o2_add_executable(strainer COMPONENT_NAME aod SOURCES src/aodStrainer.cxx PUBLIC_LINK_LIBRARIES ROOT::Core ROOT::Net) + +o2_add_executable(validator + COMPONENT_NAME aod + SOURCES src/aodValidator.cxx + PUBLIC_LINK_LIBRARIES ROOT::Core ROOT::Net ROOT::Tree) diff --git a/Framework/AODMerger/src/aodValidator.cxx b/Framework/AODMerger/src/aodValidator.cxx new file mode 100644 index 0000000000000..ac713fad7d30f --- /dev/null +++ b/Framework/AODMerger/src/aodValidator.cxx @@ -0,0 +1,188 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#include +#include +#include +#include +#include +#include +#include + +#include "TFile.h" +#include "TKey.h" +#include "TTree.h" +#include "TBranch.h" +#include "TDirectory.h" +#include + +// Checks the structure of AO2D files, local or on the grid (alien://), for +// what an interrupted write or merge leaves behind: a file which was not +// closed (ROOT has to recover its keys), no DF folder, a DF folder with no +// tree, a branch with fewer entries than its tree, or a basket with no size +// or location. Only the metadata is read (one read per tree); --deep also +// reads every basket's key to check that it belongs to its branch (one small +// read per basket). +namespace +{ +int validate(const char* url, bool deep, int maxReports) +{ + if (!std::strncmp(url, "alien:", 6) && !gGrid) { + TGrid::Connect("alien:"); + if (!gGrid) { + printf("ERROR: cannot connect to AliEn for %s\n", url); + return -1; + } + } + std::unique_ptr file(TFile::Open(url)); + if (!file || file->IsZombie()) { + printf("ERROR: cannot open %s\n", url); + return -1; + } + const Long64_t size = file->GetSize(); + int problems = 0, dfs = 0, trees = 0, baskets = 0; + auto report = [&](const char* df, const char* tree, std::string const& what) { + if (problems++ < maxReports) { + printf("BAD %s/%s: %s\n", df, tree, what.c_str()); + } + }; + if (file->TestBit(TFile::kRecovered)) { + report("", "", "the file was not closed: ROOT recovered its keys, the DF folders may be empty"); + } + + for (auto* key : TRangeDynCast(file->GetListOfKeys())) { + if (!key || std::strncmp(key->GetName(), "DF_", 3) != 0) { + continue; + } + ++dfs; + std::unique_ptr dir((TDirectory*)key->ReadObj()); + if (!dir) { + report(key->GetName(), "", "cannot read the folder"); + continue; + } + int treesInFolder = 0; + for (auto* treeKey : TRangeDynCast(dir->GetListOfKeys())) { + if (!treeKey || std::strcmp(treeKey->GetClassName(), "TTree") != 0) { + continue; + } + std::unique_ptr tree((TTree*)treeKey->ReadObj()); + if (!tree) { + report(key->GetName(), treeKey->GetName(), "cannot read the tree"); + continue; + } + ++trees; + ++treesInFolder; + TObjArray* branches = tree->GetListOfBranches(); + for (int i = 0; i < branches->GetEntriesFast(); ++i) { + auto* br = (TBranch*)branches->UncheckedAt(i); + if (br->GetEntries() != tree->GetEntries()) { + report(key->GetName(), tree->GetName(), Form("branch %s has %lld entries, the tree %lld", br->GetName(), br->GetEntries(), tree->GetEntries())); + } + for (int b = 0; b < br->GetWriteBasket(); ++b) { + ++baskets; + Int_t bytes = br->GetBasketBytes()[b]; + Long64_t seek = br->GetBasketSeek(b); + if (bytes <= 0 || seek <= 0 || seek + bytes > size) { + report(key->GetName(), tree->GetName(), Form("branch %s basket %d: %d bytes at %lld", br->GetName(), b, bytes, seek)); + continue; + } + if (deep) { + char buffer[256]; + if (file->ReadBuffer(buffer, seek, std::min(sizeof(buffer), size - seek))) { + report(key->GetName(), tree->GetName(), Form("branch %s basket %d: cannot read its key at %lld", br->GetName(), b, seek)); + continue; + } + TKey basketKey(file.get()); + char* cursor = buffer; + basketKey.ReadKeyBuffer(cursor); + if (std::strcmp(basketKey.GetName(), br->GetName()) != 0) { + report(key->GetName(), tree->GetName(), Form("branch %s basket %d at %lld is a basket of %s", br->GetName(), b, seek, basketKey.GetName())); + } + } + } + } + } + if (treesInFolder == 0) { + report(key->GetName(), "", "the folder has no tree"); + } + } + if (dfs == 0) { + report("", "", "the file has no DF folder"); + } + printf("%s: %s, %d DFs, %d trees, %d baskets checked%s\n", url, + problems ? Form("%d PROBLEMS", problems) : "OK", dfs, trees, baskets, deep ? " (with keys)" : ""); + return problems; +} +} // namespace + +int main(int argc, char* argv[]) +{ + bool deep = false; + int maxReports = 50; + std::vector files; + + static struct option longOptions[] = { + {"input", required_argument, nullptr, 'i'}, + {"deep", no_argument, nullptr, 'd'}, + {"max-reports", required_argument, nullptr, 'm'}, + {"help", no_argument, nullptr, 'h'}, + {nullptr, 0, nullptr, 0}}; + + int c; + while ((c = getopt_long(argc, argv, "", longOptions, nullptr)) != -1) { + if (c == 'i') { + std::ifstream in(optarg); + if (!in) { + printf("ERROR: cannot read %s\n", optarg); + return 2; + } + for (std::string line; std::getline(in, line);) { + if (!line.empty() && line[0] != '#') { + files.push_back(line); + } + } + } else if (c == 'd') { + deep = true; + } else if (c == 'm') { + maxReports = std::stoi(optarg); + } else { + printf( + "AO2D validator: checks AO2D files for what an interrupted write or merge leaves behind:\n" + "a file not closed, no DF folder, a DF folder with no tree, a branch with fewer entries\n" + "than its tree, a basket with no size or location. Reads only metadata.\n\n" + "Usage: %s [options] FILE... (local paths or alien:// names)\n" + " --input LIST also check the files listed in LIST, one per line\n" + " --deep also read every basket's key (one small read per basket)\n" + " --max-reports N print at most N problems per file (default 50)\n\n" + "Exit code: 0 all files fine, 1 a file has problems, 2 a file could not be read.\n", + argv[0]); + return c == 'h' ? 0 : 2; + } + } + for (int i = optind; i < argc; ++i) { + files.push_back(argv[i]); + } + if (files.empty()) { + printf("ERROR: no input file, see --help\n"); + return 2; + } + + int bad = 0, unreadable = 0; + for (auto const& f : files) { + int problems = validate(f.c_str(), deep, maxReports); + bad += problems > 0; + unreadable += problems < 0; + } + if (files.size() > 1) { + printf("%zu files: %d with problems, %d unreadable\n", files.size(), bad, unreadable); + } + return unreadable ? 2 : (bad ? 1 : 0); +} From 0231139586e9f2f774aac3dbea35e0fbe2eb1a41 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:49:55 +0100 Subject: [PATCH 2/2] aodMerger: fail when the input or a copied tree is incomplete A basket which cannot be read during a fast copy is not reported: TTree::CopyEntries runs TTreeCloner with kNoWarnings, and the basket is written with 0 bytes and the location of another branch's basket, its branch left short of entries. The merger exited 0 and the file passed validation. LHC25h3c/0/564414/AOD/1170/AO2D.root was merged this way after an expired XRootD read: fIndexArray_Mothers of DF_2424139774636032/O2mcparticle_001 lost its first basket, and ROOT reads fIndexMcCollisions in its place. An input which was not closed (a job which stopped while writing it) has its keys recovered by ROOT, but its DF folders come back without trees; merging it gave an AO2D without any DF folder, exit code 0. After copying a tree, check that every branch has the entries of the tree and every written basket a size and a location, count the ROOT errors raised while copying it, and check GetEntry in the index loop. Refuse an input whose keys had to be recovered and a DF folder without trees. Any of these fails the merge with exit code 6 and removes the output; the check for no DF merged no longer overrides an earlier code. --- Framework/AODMerger/src/aodMerger.cxx | 71 ++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/Framework/AODMerger/src/aodMerger.cxx b/Framework/AODMerger/src/aodMerger.cxx index 11dae2dc5eac4..a1e7e4481e7a7 100644 --- a/Framework/AODMerger/src/aodMerger.cxx +++ b/Framework/AODMerger/src/aodMerger.cxx @@ -25,10 +25,51 @@ #include #include #include +#include #include "aodMerger.h" #include +namespace +{ +// ROOT errors (e.g. an expired XRootD read) seen while copying a tree. A fast +// copy does not fail on them: TTree::CopyEntries runs TTreeCloner with +// kNoWarnings, and a basket which could not be read is written with 0 bytes. +int rootErrors = 0; +ErrorHandlerFunc_t previousErrorHandler = nullptr; + +void countingErrorHandler(int level, Bool_t abort, const char* location, const char* msg) +{ + if (level >= kError) { + ++rootErrors; + } + previousErrorHandler(level, abort, location, msg); +} + +// True if every branch of the copied tree has all its entries and every +// written basket a location. A basket lost in a fast copy shows up here. +bool isCopyComplete(TTree* tree) +{ + bool complete = true; + TObjArray* branches = tree->GetListOfBranches(); + for (int i = 0; i < branches->GetEntriesFast(); ++i) { + auto* br = (TBranch*)branches->UncheckedAt(i); + if (br->GetEntries() != tree->GetEntries()) { + printf(" *** FATAL ***: Branch %s of tree %s has %lld entries, the tree %lld\n", br->GetName(), tree->GetName(), br->GetEntries(), tree->GetEntries()); + complete = false; + } + for (int b = 0; b < br->GetWriteBasket(); ++b) { + if (br->GetBasketBytes()[b] <= 0 || br->GetBasketSeek(b) <= 0) { + printf(" *** FATAL ***: Basket %d of branch %s of tree %s has %d bytes at %lld\n", b, br->GetName(), tree->GetName(), br->GetBasketBytes()[b], br->GetBasketSeek(b)); + complete = false; + break; + } + } + } + return complete; +} +} // namespace + // AOD merger with correct index rewriting // No need to know the datamodel because the branch names follow a canonical standard (identified by fIndex) int main(int argc, char* argv[]) @@ -103,6 +144,8 @@ int main(int argc, char* argv[]) printf(" Merging only folders with the same name.\n"); } + previousErrorHandler = SetErrorHandler(countingErrorHandler); + std::map trees; std::map sizeCompressed; std::map sizeUncompressed; @@ -167,6 +210,12 @@ int main(int argc, char* argv[]) break; } } + if (inputFile->TestBit(TFile::kRecovered)) { + // Written by a job which stopped before closing it: its DF folders come back without trees. + printf("*** FATAL ***: Input file %s was not closed, ROOT had to recover its keys. Aborting merge!\n", line.Data()); + exitCode = 6; + break; + } TList* keyList = inputFile->GetListOfKeys(); keyList->Sort(); @@ -227,6 +276,11 @@ int main(int argc, char* argv[]) ++totalMergedDFs; auto folder = (TDirectoryFile*)inputFile->Get(dfName); auto treeList = folder->GetListOfKeys(); + if (treeList->GetEntries() == 0) { + printf(" *** FATAL ***: Folder %s of %s has no tree\n", dfName, line.Data()); + exitCode = 6; + break; + } treeList->Sort(); @@ -268,6 +322,7 @@ int main(int argc, char* argv[]) printf(" Processing tree %s with %lld entries with total size %lld (fast copy: %d)\n", treeName, inputTree->GetEntries(), inputTree->GetTotBytes(), fastCopy); } + int errorsBefore = rootErrors; bool alreadyCopied = false; if (trees.count(treeName) == 0) { if (mergedDFs > 1) { @@ -359,7 +414,11 @@ int main(int argc, char* argv[]) for (auto& index : indexList) { *(index.first) = 0; // Any positive number will do, in any case it will not be filled in the output. Otherwise the previous entry is used and manipulated in the following. } - inputTree->GetEntry(i); + if (inputTree->GetEntry(i) < 0) { + printf(" *** FATAL ***: Could not read entry %d of tree %s\n", i, treeName); + exitCode = 6; + break; + } // shift index columns by offset for (const auto& idx : indexList) { // if negative, the index is unassigned. In this case, the different unassigned blocks have to get unique negative IDs @@ -385,6 +444,14 @@ int main(int argc, char* argv[]) } } + if (rootErrors > errorsBefore) { + printf(" *** FATAL ***: %d ROOT error(s) while copying tree %s\n", rootErrors - errorsBefore, treeName); + exitCode = 6; + } + if (!isCopyComplete(outputTree)) { + exitCode = 6; + } + delete inputTree; for (auto& buffer : indexPointers) { @@ -450,7 +517,7 @@ int main(int argc, char* argv[]) outputFile->Write(); outputFile->Close(); - if (totalMergedDFs == 0) { + if (totalMergedDFs == 0 && exitCode == 0) { printf("ERROR: Did not merge a single DF. This does not seem right.\n"); exitCode = 2; }