-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathPlugin.cxx
More file actions
291 lines (272 loc) · 11.5 KB
/
Plugin.cxx
File metadata and controls
291 lines (272 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// 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 "Framework/Plugins.h"
#include "Framework/AlgorithmSpec.h"
#include "Framework/ServiceSpec.h"
#include "Framework/ServiceMetricsInfo.h"
#include "Framework/ConfigParamDiscovery.h"
#include "Framework/Capability.h"
#include "Framework/Signpost.h"
#include "AODJAlienReaderHelpers.h"
#include "AODWriterHelpers.h"
#include <TFile.h>
#include <TMap.h>
#include <TGrid.h>
#include <TObjString.h>
#include <TString.h>
#include <fmt/format.h>
#include <memory>
O2_DECLARE_DYNAMIC_LOG(analysis_support);
struct ROOTTypeInfo {
EDataType type;
char suffix[3];
int size;
};
struct ROOTFileReader : o2::framework::AlgorithmPlugin {
o2::framework::AlgorithmSpec create(o2::framework::ConfigContext const& config) override
{
return o2::framework::readers::AODJAlienReaderHelpers::rootFileReaderCallback(config);
}
};
struct ROOTObjWriter : o2::framework::AlgorithmPlugin {
o2::framework::AlgorithmSpec create(o2::framework::ConfigContext const& config) override
{
return o2::framework::writers::AODWriterHelpers::getOutputObjHistWriter(config);
}
};
struct ROOTTTreeWriter : o2::framework::AlgorithmPlugin {
o2::framework::AlgorithmSpec create(o2::framework::ConfigContext const& config) override
{
return o2::framework::writers::AODWriterHelpers::getOutputTTreeWriter(config);
}
};
using namespace o2::framework;
struct RunSummary : o2::framework::ServicePlugin {
o2::framework::ServiceSpec* create() final
{
return new o2::framework::ServiceSpec{
.name = "analysis-run-summary",
.init = [](ServiceRegistryRef ref, DeviceState&, fair::mq::ProgOptions&) -> ServiceHandle {
return ServiceHandle{TypeIdHelpers::uniqueId<RunSummary>(), nullptr, ServiceKind::Serial, "analysis-run-summary"};
},
.summaryHandling = [](ServiceMetricsInfo const& info) {
LOGP(info, "## Analysis Run Summary ##");
/// Find the metrics of the reader and dump the list of files read.
for (size_t mi = 0; mi < info.deviceMetricsInfos.size(); ++mi) {
DeviceMetricsInfo &metrics = info.deviceMetricsInfos[mi];
for (size_t li = 0; li < metrics.metricLabels.size(); ++li) {
MetricLabel const&label = metrics.metricLabels[li];
if (strcmp(label.label, "aod-file-read-info") != 0) {
continue;
}
MetricInfo const&metric = metrics.metrics[li];
auto &files = metrics.stringMetrics[metric.storeIdx];
if (metric.filledMetrics) {
LOGP(info, "### Files read stats ###");
}
for (size_t fi = 0; fi < metric.filledMetrics; ++fi) {
LOGP(info, "{}", files[fi % files.size()].data);
}
}
for (size_t li = 0; li < metrics.metricLabels.size(); ++li) {
MetricLabel const&label = metrics.metricLabels[li];
if (strcmp(label.label, "aod-file-open-info") != 0) {
continue;
}
MetricInfo const&metric = metrics.metrics[li];
auto &files = metrics.stringMetrics[metric.storeIdx];
if (metric.filledMetrics) {
LOGP(info, "### Files opened stats ###");
}
std::string lastFileRead;
for (size_t fi = 0; fi < metric.filledMetrics; ++fi) {
lastFileRead = files[fi % files.size()].data;
}
if (lastFileRead.empty() == false) {
LOGP(info, "Last file opened: {}", lastFileRead);
}
}
} },
.kind = ServiceKind::Serial};
}
};
std::vector<std::string> getListOfTables(std::unique_ptr<TFile>& f)
{
std::vector<std::string> r;
TList* keyList = f->GetListOfKeys();
// We should handle two cases, one where the list of tables in a TDirectory,
// the other one where the dataframe number is just a prefix
std::string first = "";
for (auto key : *keyList) {
if (!std::string_view(key->GetName()).starts_with("DF_") && !std::string_view(key->GetName()).starts_with("/DF_")) {
continue;
}
auto* d = (TDirectory*)f->GetObjectChecked(key->GetName(), TClass::GetClass("TDirectory"));
// Objects are in a folder, list it.
if (d) {
TList* branchList = d->GetListOfKeys();
for (auto b : *branchList) {
r.emplace_back(b->GetName());
}
break;
}
#if __has_include(<ROOT/RFieldBase.hxx>)
void* v = f->GetObjectChecked(key->GetName(), TClass::GetClass("ROOT::RNTuple"));
#else
void* v = f->GetObjectChecked(key->GetName(), TClass::GetClass("ROOT::Experimental::RNTuple"));
#endif
if (v) {
std::string s = key->GetName();
size_t pos = s.find('-');
// Check if '-' is found
// Skip metaData and parentFiles
if (pos == std::string::npos) {
continue;
}
std::string t = s.substr(pos + 1);
// If we find a duplicate table name, it means we are in the next DF and we can stop.
if (t == first) {
break;
}
if (first.empty()) {
first = t;
}
// Create a new string starting after the '-'
r.emplace_back(t);
}
}
return r;
}
auto readMetadata(std::unique_ptr<TFile>& currentFile) -> std::vector<ConfigParamSpec>
{
// Get the metadata, if any
auto m = (TMap*)currentFile->Get("metaData");
if (!m) {
return {};
}
std::vector<ConfigParamSpec> results;
auto it = m->MakeIterator();
// Serialise metadata into a ; separated string with : separating key and value
bool first = true;
while (auto obj = it->Next()) {
if (first) {
LOGP(info, "Metadata for file \"{}\":", currentFile->GetName());
first = false;
}
auto objString = (TObjString*)m->GetValue(obj);
LOGP(info, "- {}: {}", obj->GetName(), objString->String().Data());
std::string key = "aod-metadata-" + std::string(obj->GetName());
char const* value = strdup(objString->String());
results.push_back(ConfigParamSpec{key, VariantType::String, value, {"Metadata in AOD"}});
}
return results;
}
struct DiscoverMetadataInAOD : o2::framework::ConfigDiscoveryPlugin {
ConfigDiscovery* create() override
{
return new ConfigDiscovery{
.init = []() {},
.discover = [](ConfigParamRegistry& registry, int argc, char** argv) -> std::vector<ConfigParamSpec> {
auto filename = registry.get<std::string>("aod-file");
if (filename.empty()) {
return {};
}
if (filename.at(0) == '@') {
filename.erase(0, 1);
// read the text file and set filename to the contents of the first line
std::ifstream file(filename);
if (!file.is_open()) {
LOGP(fatal, "Couldn't open file \"{}\"!", filename);
}
std::getline(file, filename);
file.close();
}
if (filename.rfind("alien://", 0) == 0 && !gGrid) {
TGrid::Connect("alien://");
}
LOGP(info, "Loading metadata from file {} in PID {}", filename, getpid());
std::unique_ptr<TFile> currentFile{TFile::Open(filename.c_str())};
if (currentFile.get() == nullptr) {
LOGP(fatal, "Couldn't open file \"{}\"!", filename);
}
std::vector<ConfigParamSpec> results = readMetadata(currentFile);
const bool metaDataEmpty = results.empty();
auto tables = getListOfTables(currentFile);
if (tables.empty() == false) {
results.push_back(ConfigParamSpec{"aod-metadata-tables", VariantType::ArrayString, tables, {"Tables in first AOD"}});
}
// Found metadata already in the main file.
if (!metaDataEmpty) {
results.push_back(ConfigParamSpec{"aod-metadata-source", VariantType::String, filename, {"File from which the metadata was extracted."}});
return results;
}
if (!registry.isSet("aod-parent-access-level") || registry.get<int>("aod-parent-access-level") == 0) {
LOGP(info, "No metadata found in file \"{}\" and parent level 0 prevents further lookup.", filename);
results.push_back(ConfigParamSpec{"aod-metadata-disable", VariantType::String, "1", {"Metadata not found in AOD"}});
return results;
}
// Lets try in parent file.
auto parentFiles = (TMap*)currentFile->Get("parentFiles");
if (!parentFiles) {
LOGP(info, "No metadata found in file \"{}\"", filename);
results.push_back(ConfigParamSpec{"aod-metadata-disable", VariantType::String, "1", {"Metadata not found in AOD"}});
return results;
}
LOGP(info, "No metadata found in file \"{}\", checking in its parents.", filename);
for (auto* p : *parentFiles) {
std::string parentFilename = ((TPair*)p)->Value()->GetName();
// Do the replacement. Notice this will require changing aod-parent-base-path-replacement to be
// a workflow option (because the metadata itself is potentially changing the topology).
if (registry.isSet("aod-parent-base-path-replacement")) {
auto parentFileReplacement = registry.get<std::string>("aod-parent-base-path-replacement");
auto pos = parentFileReplacement.find(';');
if (pos == std::string::npos) {
throw std::runtime_error(fmt::format("Invalid syntax in aod-parent-base-path-replacement: \"{}\"", parentFileReplacement.c_str()));
}
auto from = parentFileReplacement.substr(0, pos);
auto to = parentFileReplacement.substr(pos + 1);
pos = parentFilename.find(from);
if (pos != std::string::npos) {
parentFilename.replace(pos, from.length(), to);
}
}
if (parentFilename.starts_with("alien://") && !gGrid) {
TGrid::Connect("alien://");
}
std::unique_ptr<TFile> parentFile{TFile::Open(parentFilename.c_str())};
if (parentFile.get() == nullptr) {
LOGP(fatal, "Couldn't open derived file \"{}\"!", parentFilename);
}
results = readMetadata(parentFile);
// Found metadata already in the main file.
if (!results.empty()) {
auto tables = getListOfTables(parentFile);
if (tables.empty() == false) {
results.push_back(ConfigParamSpec{"aod-metadata-tables", VariantType::ArrayString, tables, {"Tables in first AOD"}});
}
results.push_back(ConfigParamSpec{"aod-metadata-source", VariantType::String, filename, {"File from which the metadata was extracted."}});
return results;
}
LOGP(info, "No metadata found in file \"{}\" nor in its parent file \"{}\"", filename, parentFilename);
break;
}
results.push_back(ConfigParamSpec{"aod-metadata-disable", VariantType::String, "1", {"Metadata not found in AOD"}});
return results;
}};
}
};
DEFINE_DPL_PLUGINS_BEGIN
DEFINE_DPL_PLUGIN_INSTANCE(ROOTFileReader, CustomAlgorithm);
DEFINE_DPL_PLUGIN_INSTANCE(ROOTObjWriter, CustomAlgorithm);
DEFINE_DPL_PLUGIN_INSTANCE(ROOTTTreeWriter, CustomAlgorithm);
DEFINE_DPL_PLUGIN_INSTANCE(RunSummary, CustomService);
DEFINE_DPL_PLUGIN_INSTANCE(DiscoverMetadataInAOD, ConfigDiscovery);
DEFINE_DPL_PLUGINS_END