Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 58 additions & 12 deletions Framework/AnalysisSupport/src/AODInputFile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
#include "AODNative/Errors.h"
#include "Framework/Logger.h"
#include <TDirectory.h>
#include <TError.h>
#include <TFile.h>
#include <TGrid.h>
#include <TGridResult.h>
#include <TKey.h>
#include <TMap.h>
#include <TObjString.h>
#include <TUrl.h>
#include <fmt/format.h>
#include <algorithm>
#include <chrono>
#if __has_include(<TJAlienFile.h>)
#include <TJAlien.h>
#include <TJAlienFile.h>
#endif

Expand Down Expand Up @@ -105,24 +107,72 @@ std::string RootAODInputFile::monitoringDetails() const
return "";
}

void connectToGrid()
{
if (gGrid) {
return;
}
#if __has_include(<TJAlienFile.h>)
// What TGrid::Connect("alien://") does, minus the plugin handler, which starts the interpreter.
// So does ROOT's default error handler on the first message (TJAlien prints one), when it
// reads Root.ErrorIgnoreLevel through gROOT: set its default, Print, beforehand.
if (gErrorIgnoreLevel == kUnset) {
gErrorIgnoreLevel = kPrint;
}
new TJAlien("alien://", "", "", "");
#else
TGrid::Connect("alien://");
#endif
}

std::vector<GridReplica> gridReplicas(std::string const& lfn)
{
TUrl url(lfn.c_str());
if (url.GetAnchor() && url.GetAnchor()[0] != '\0') {
// alien://[host]/path[?options][#member], split by hand: a TUrl reads
// Url.Special from gEnv, which goes through gROOT and starts the interpreter.
// Archive members are not for the native reader.
std::string_view path = lfn;
if (!path.starts_with("alien://") || path.find('#') != std::string_view::npos) {
return {};
}
path.remove_prefix(std::string_view("alien://").size());
path = path.substr(0, path.find('?'));
// After a host, TUrl keeps the path without its leading '/'.
auto slash = path.find('/');
path = slash == std::string_view::npos ? std::string_view{} : path.substr(slash == 0 ? 0 : slash + 1);
if (!gGrid) {
TGrid::Connect("alien://");
connectToGrid();
}
if (!gGrid) {
return {};
}
// As TJAlienFile::Open does for reading.
std::unique_ptr<TGridResult> result(gGrid->Command(fmt::format("access read {}", url.GetFile()).c_str()));
auto command = fmt::format("access read {}", path);
std::vector<GridReplica> replicas;
auto add = [&replicas](std::string const& pfn, std::string const& envelope, std::string const& se) {
if (!pfn.empty() && !envelope.empty() && pfn.find('#') == std::string::npos) {
replicas.push_back({pfn + "?&authz=" + envelope, se});
}
};
#ifdef TJALIEN_HAS_COMMAND_RAW
// Plain strings rather than a TGridResult of TMaps, whose hashing starts the interpreter.
if (auto* jalien = dynamic_cast<TJAlien*>(gGrid)) {
TJAlien::RawResult results;
std::map<std::string, std::string> metadata;
jalien->CommandRaw(command.c_str(), results, metadata);
for (auto const& entry : results) {
auto value = [&entry](std::string const& key) -> std::string {
auto it = std::ranges::find(entry, key, &std::pair<std::string, std::string>::first);
return it != entry.end() ? it->second : "";
};
add(value("url"), value("envelope"), value("se"));
}
return replicas;
}
#endif
std::unique_ptr<TGridResult> result(gGrid->Command(command.c_str()));
if (!result) {
return {};
}
std::vector<GridReplica> replicas;
for (auto* object : *result) {
auto* map = dynamic_cast<TMap*>(object);
if (!map) {
Expand All @@ -132,12 +182,7 @@ std::vector<GridReplica> gridReplicas(std::string const& lfn)
auto* v = map->GetValue(key);
return v ? v->GetName() : "";
};
auto pfn = value("url");
auto envelope = value("envelope");
if (pfn.empty() || envelope.empty() || pfn.find('#') != std::string::npos) {
continue;
}
replicas.push_back({pfn + "?&authz=" + envelope, value("se")});
add(value("url"), value("envelope"), value("se"));
}
return replicas;
}
Expand All @@ -157,6 +202,7 @@ std::shared_ptr<AODInputFile> openNativeAOD(std::string const& filename, RootObj
// is retried and then continues on the next replica (FailoverByteSource).
std::vector<o2::aod::native::Replica> list;
for (auto const& replica : replicas) {
LOGP(debug, "Replica of \"{}\": {} {}", filename, replica.se, replica.url.substr(0, replica.url.find("?&authz=")));
list.push_back({replica.url, replica.se});
}
auto source = o2::aod::native::ByteSource::openReplicas(std::move(list), options, [filename](std::string const& message) {
Expand Down
4 changes: 4 additions & 0 deletions Framework/AnalysisSupport/src/AODInputFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ struct GridReplica {
std::string se;
};

/// Connects gGrid to AliEn, unless it is already, without going through
/// ROOT's plugin manager when JAliEn-ROOT is linked.
void connectToGrid();

/// Asks the AliEn catalogue (through gGrid, connecting if needed) for the
/// replicas to read @a lfn from, in the order it proposes. Empty if there are
/// none, or for a file inside an archive (alien://...#member), which only ROOT reads.
Expand Down
2 changes: 1 addition & 1 deletion Framework/AnalysisSupport/src/DataInputDirector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void DataInputDescriptor::addFileNameHolder(FileNameHolder fn)
fn.fileName.erase(0, 7);
} else if (!mAlienSupport && fn.fileName.rfind("alien://", 0) == 0 && !gGrid) {
LOGP(debug, "AliEn file requested. Enabling support.");
TGrid::Connect("alien://");
connectToGrid();
mAlienSupport = true;
}

Expand Down
4 changes: 2 additions & 2 deletions Framework/AnalysisSupport/src/Plugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ FileAtLevel openParent(FileAtLevel const& child, ConfigParamRegistry& registry)
}

if (parentFilename.starts_with("alien://") && !gGrid) {
TGrid::Connect("alien://");
connectToGrid();
}

auto parentFile = openForDiscovery(parentFilename);
Expand Down Expand Up @@ -252,7 +252,7 @@ struct DiscoverMetadataInAOD : o2::framework::ConfigDiscoveryPlugin {
file.close();
}
if (filename.rfind("alien://", 0) == 0 && !gGrid) {
TGrid::Connect("alien://");
connectToGrid();
}
LOGP(info, "Loading metadata from file {} in PID {}", filename, getpid());
auto currentFile = openForDiscovery(filename);
Expand Down