Project
Loading...
Searching...
No Matches
Plugin.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#include "Framework/Plugins.h"
17#include "Framework/Signpost.h"
19#include "AODWriterHelpers.h"
20#include "AODInputFile.h"
21#include <TDataType.h>
22#include <TGrid.h>
23#include <fmt/format.h>
24#include <cstring>
25#include <fstream>
26#include <memory>
27#include <unistd.h>
28
29O2_DECLARE_DYNAMIC_LOG(analysis_support);
30
32 EDataType type;
33 char suffix[3];
34 int size;
35};
36
43
50
57
58using namespace o2::framework;
61 {
63 .name = "analysis-run-summary",
64 .init = [](ServiceRegistryRef ref, DeviceState&, fair::mq::ProgOptions&) -> ServiceHandle {
65 return ServiceHandle{TypeIdHelpers::uniqueId<RunSummary>(), nullptr, ServiceKind::Serial, "analysis-run-summary"};
66 },
67 .summaryHandling = [](ServiceMetricsInfo const& info) {
68 LOGP(info, "## Analysis Run Summary ##");
70 for (size_t mi = 0; mi < info.deviceMetricsInfos.size(); ++mi) {
71 DeviceMetricsInfo &metrics = info.deviceMetricsInfos[mi];
72 for (size_t li = 0; li < metrics.metricLabels.size(); ++li) {
73 MetricLabel const&label = metrics.metricLabels[li];
74 if (strcmp(label.label, "aod-file-read-info") != 0) {
75 continue;
76 }
77 MetricInfo const&metric = metrics.metrics[li];
78 auto &files = metrics.stringMetrics[metric.storeIdx];
79 if (metric.filledMetrics) {
80 LOGP(info, "### Files read stats ###");
81 }
82 for (size_t fi = 0; fi < metric.filledMetrics; ++fi) {
83 LOGP(info, "{}", files[fi % files.size()].data);
84 }
85 }
86 for (size_t li = 0; li < metrics.metricLabels.size(); ++li) {
87 MetricLabel const&label = metrics.metricLabels[li];
88 if (strcmp(label.label, "aod-file-open-info") != 0) {
89 continue;
90 }
91 MetricInfo const&metric = metrics.metrics[li];
92 auto &files = metrics.stringMetrics[metric.storeIdx];
93 if (metric.filledMetrics) {
94 LOGP(info, "### Files opened stats ###");
95 }
96 std::string lastFileRead;
97 for (size_t fi = 0; fi < metric.filledMetrics; ++fi) {
98 lastFileRead = files[fi % files.size()].data;
99 }
100 if (lastFileRead.empty() == false) {
101 LOGP(info, "Last file opened: {}", lastFileRead);
102 }
103 }
104 } },
105 .kind = ServiceKind::Serial};
106 }
107};
108
109std::vector<std::string> getListOfTables(AODInputFile& f)
110{
111 std::vector<std::string> r;
112 // We should handle two cases, one where the list of tables in a TDirectory,
113 // the other one where the dataframe number is just a prefix
114 std::string first = "";
115
116 for (auto& key : f.topLevelKeys()) {
117 if (!std::string_view(key.name).starts_with("DF_") && !std::string_view(key.name).starts_with("/DF_")) {
118 continue;
119 }
120 // Objects are in a folder, list it.
121 if (key.className == "TDirectoryFile" || key.className == "TDirectory") {
122 r = f.keysIn(key.name);
123 break;
124 }
125
126 if (key.className == "ROOT::RNTuple" || key.className == "ROOT::Experimental::RNTuple") {
127 std::string const& s = key.name;
128 size_t pos = s.find('-');
129 // Check if '-' is found
130 // Skip metaData and parentFiles
131 if (pos == std::string::npos) {
132 continue;
133 }
134 std::string t = s.substr(pos + 1);
135 // If we find a duplicate table name, it means we are in the next DF and we can stop.
136 if (t == first) {
137 break;
138 }
139 if (first.empty()) {
140 first = t;
141 }
142 // Create a new string starting after the '-'
143 r.emplace_back(t);
144 }
145 }
146 return r;
147}
148
149auto readMetadata(AODInputFile& currentFile) -> std::vector<ConfigParamSpec>
150{
151 // Get the metadata, if any
152 auto m = currentFile.metaData();
153 std::vector<ConfigParamSpec> results;
154
155 // Serialise metadata into a ; separated string with : separating key and value
156 bool first = true;
157 for (auto& [name, value] : m) {
158 if (first) {
159 LOGP(info, "Metadata for file \"{}\":", currentFile.name());
160 first = false;
161 }
162 LOGP(info, "- {}: {}", name, value);
163 std::string key = "aod-metadata-" + name;
164 char const* valueCopy = strdup(value.c_str());
165 results.push_back(ConfigParamSpec{key, VariantType::String, valueCopy, {"Metadata in AOD"}});
166 }
167
168 return results;
169}
170
173std::shared_ptr<AODInputFile> openForDiscovery(std::string const& filename)
174{
175 static RootObjectReadingFactory noTables;
176 return RootAODInputFile::open(filename, noTables);
177}
178
181 {
182 return new ConfigDiscovery{
183 .init = []() {},
184 .discover = [](ConfigParamRegistry& registry, int argc, char** argv) -> std::vector<ConfigParamSpec> {
185 auto filename = registry.get<std::string>("aod-file");
186 if (filename.empty()) {
187 return {};
188 }
189 if (filename.at(0) == '@') {
190 filename.erase(0, 1);
191 // read the text file and set filename to the contents of the first line
192 std::ifstream file(filename);
193 if (!file.is_open()) {
194 LOGP(fatal, "Couldn't open file \"{}\"!", filename);
195 }
196 std::getline(file, filename);
197 file.close();
198 }
199 if (filename.rfind("alien://", 0) == 0 && !gGrid) {
200 TGrid::Connect("alien://");
201 }
202 LOGP(info, "Loading metadata from file {} in PID {}", filename, getpid());
203 auto currentFile = openForDiscovery(filename);
204 if (currentFile.get() == nullptr) {
205 throw std::runtime_error(fmt::format("Couldn't open file \"{}\"!", filename));
206 }
207 std::vector<ConfigParamSpec> results = readMetadata(*currentFile);
208 const bool metaDataEmpty = results.empty();
209 auto tables = getListOfTables(*currentFile);
210 if (tables.empty() == false) {
211 results.push_back(ConfigParamSpec{"aod-metadata-tables", VariantType::ArrayString, tables, {"Tables in first AOD"}});
212 }
213
214 // Found metadata already in the main file.
215 if (!metaDataEmpty) {
216 results.push_back(ConfigParamSpec{"aod-metadata-source", VariantType::String, filename, {"File from which the metadata was extracted."}});
217 return results;
218 }
219
220 if (!registry.isSet("aod-parent-access-level") || registry.get<int>("aod-parent-access-level") == 0) {
221 LOGP(info, "No metadata found in file \"{}\" and parent level 0 prevents further lookup.", filename);
222 results.push_back(ConfigParamSpec{"aod-metadata-disable", VariantType::String, "1", {"Metadata not found in AOD"}});
223 return results;
224 }
225
226 // Lets try in the parent files, up to the allowed parent level.
227 auto childFile = currentFile;
228 for (int level = 1; level <= registry.get<int>("aod-parent-access-level"); ++level) {
229 auto parentFiles = childFile->parentFiles();
230 if (parentFiles.empty()) {
231 break;
232 }
233 LOGP(info, "No metadata found in file \"{}\", checking in its parents.", childFile->name());
234 std::string parentFilename = parentFiles.begin()->second;
235 // Do the replacement. Notice this will require changing aod-parent-base-path-replacement to be
236 // a workflow option (because the metadata itself is potentially changing the topology).
237 if (registry.isSet("aod-parent-base-path-replacement")) {
238 auto parentFileReplacement = registry.get<std::string>("aod-parent-base-path-replacement");
239 auto pos = parentFileReplacement.find(';');
240 if (pos == std::string::npos) {
241 throw std::runtime_error(fmt::format("Invalid syntax in aod-parent-base-path-replacement: \"{}\"", parentFileReplacement.c_str()));
242 }
243 auto from = parentFileReplacement.substr(0, pos);
244 auto to = parentFileReplacement.substr(pos + 1);
245 pos = parentFilename.find(from);
246 if (pos != std::string::npos) {
247 parentFilename.replace(pos, from.length(), to);
248 }
249 }
250
251 if (parentFilename.starts_with("alien://") && !gGrid) {
252 TGrid::Connect("alien://");
253 }
254
255 auto parentFile = openForDiscovery(parentFilename);
256 if (parentFile.get() == nullptr) {
257 LOGP(fatal, "Couldn't open derived file \"{}\"!", parentFilename);
258 }
259 results = readMetadata(*parentFile);
260 // Found metadata already in the main file.
261 if (!results.empty()) {
262 auto tables = getListOfTables(*parentFile);
263 if (tables.empty() == false) {
264 results.push_back(ConfigParamSpec{"aod-metadata-tables", VariantType::ArrayString, tables, {"Tables in first AOD"}});
265 }
266 results.push_back(ConfigParamSpec{"aod-metadata-source", VariantType::String, filename, {"File from which the metadata was extracted."}});
267 return results;
268 }
269 LOGP(info, "No metadata found in parent file \"{}\" at level {}", parentFilename, level);
270 childFile = parentFile;
271 }
272 LOGP(info, "No metadata found in file \"{}\" nor in its parents", filename);
273 results.push_back(ConfigParamSpec{"aod-metadata-disable", VariantType::String, "1", {"Metadata not found in AOD"}});
274 return results;
275 }};
276 }
277};
278
auto readMetadata(AODInputFile &currentFile) -> std::vector< ConfigParamSpec >
Definition Plugin.cxx:149
std::shared_ptr< AODInputFile > openForDiscovery(std::string const &filename)
Definition Plugin.cxx:173
std::vector< std::string > getListOfTables(AODInputFile &f)
Definition Plugin.cxx:109
configStandalone Light OpenGL display display_light Apply smoothing to points Enable Z buffer Highlight links Highlight seeds Highlight sector tracks Highlight final tracks Show individual Show related Highlight grid Show only individual collision Color clusters belonging to track objects Set line Enable drawing TPC data Enable drawing TOF data Enable drawing magnetic field Set field line step count Invert colors configStandalone Heavy OpenGL display display_heavy Show tracks with ITS contribution Show tracks with TOF contribution Enable propagation of loopers Show clusters at nominal x of pad row for early transformed data Split CE tracks when they cross the central Mark clusters Mark adjacent clusters Hide rejected tracks Apply filter on tracks to be displayed Separate increase X h configStandalone Camera window renderer OpenGL display display_camera Orient the camera such that the y axis is always upwards Full Screen MultiSample Anti Downsampling Anti Enable Vertical Sync Target frame or if the backend cannot limit to the refresh Use OpenGL indirect draws to reduce number of draw calls Display h configStandalone Vulkan display display_vulkan Have uniform buffers in host accessible device memory h configStandalone Internal display display_internal Number of updates the Qt GUI may send ahead of the last one the display has before it waits for the display Opacity of the console background Number of console lines the history is shortened to this when it reaches twice the size Time in seconds an info message fades out after setInfoTime h OpenGL display display Font size Do not use Freetype for font Renderer for event Frontend to use for event ROOT macros used as track filter Console script file
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error s
#define DEFINE_DPL_PLUGIN_INSTANCE(NAME, KIND)
Definition Plugins.h:112
#define DEFINE_DPL_PLUGINS_END
Definition Plugins.h:115
#define DEFINE_DPL_PLUGINS_BEGIN
Definition Plugins.h:107
uint16_t pos
Definition RawData.h:3
#define O2_DECLARE_DYNAMIC_LOG(name)
Definition Signpost.h:490
StringRef key
static std::shared_ptr< RootAODInputFile > open(std::string const &name, RootObjectReadingFactory &factory, TFile *external=nullptr)
const GLfloat * m
Definition glcorearb.h:4066
GLuint const GLchar * name
Definition glcorearb.h:781
GLint first
Definition glcorearb.h:399
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
Definition glcorearb.h:5500
GLdouble f
Definition glcorearb.h:310
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLuint GLsizei const GLchar * label
Definition glcorearb.h:2519
GLint level
Definition glcorearb.h:275
GLboolean r
Definition glcorearb.h:1233
GLint ref
Definition glcorearb.h:291
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
std::string filename()
ConfigDiscovery * create() override
Definition Plugin.cxx:180
o2::framework::AlgorithmSpec create(o2::framework::ConfigContext const &config) override
Definition Plugin.cxx:38
o2::framework::AlgorithmSpec create(o2::framework::ConfigContext const &config) override
Definition Plugin.cxx:45
o2::framework::AlgorithmSpec create(o2::framework::ConfigContext const &config) override
Definition Plugin.cxx:52
EDataType type
Definition Plugin.cxx:32
o2::framework::ServiceSpec * create() final
Definition Plugin.cxx:60
Helper class for an algorithm which is loaded as a plugin.
Running state information of a given device.
Definition DeviceState.h:34
std::string name
Name of the service.
static AlgorithmSpec rootFileReaderCallback(ConfigContext const &context)
static AlgorithmSpec getOutputObjHistWriter(ConfigContext const &context)
static AlgorithmSpec getOutputTTreeWriter(ConfigContext const &context)