forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAODReaderHelpers.cxx
More file actions
276 lines (250 loc) · 11.4 KB
/
AODReaderHelpers.cxx
File metadata and controls
276 lines (250 loc) · 11.4 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
// 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/AODReaderHelpers.h"
#include "Framework/AnalysisHelpers.h"
#include "Framework/AnalysisDataModelHelpers.h"
#include "Framework/ExpressionHelpers.h"
#include "Framework/DataProcessingHelpers.h"
#include "Framework/AlgorithmSpec.h"
#include "Framework/ControlService.h"
#include "Framework/CallbackService.h"
#include "Framework/EndOfStreamContext.h"
#include "Framework/DataSpecUtils.h"
#include "ExpressionJSONHelpers.h"
#include "Framework/ConfigContext.h"
#include "Framework/AnalysisContext.h"
#include <Monitoring/Monitoring.h>
#include <TGrid.h>
#include <TFile.h>
#include <TTreeCache.h>
#include <arrow/ipc/reader.h>
#include <arrow/ipc/writer.h>
#include <arrow/io/interfaces.h>
#include <arrow/table.h>
#include <arrow/util/key_value_metadata.h>
namespace o2::framework::readers
{
auto setEOSCallback(InitContext& ic)
{
ic.services().get<CallbackService>().set<CallbackService::Id::EndOfStream>(
[](EndOfStreamContext& eosc) {
auto& control = eosc.services().get<ControlService>();
control.endOfStream();
control.readyToQuit(QuitRequest::Me);
});
}
template <size_t N, std::array<soa::TableRef, N> refs>
static inline auto extractOriginals(ProcessingContext& pc)
{
return [&]<size_t... Is>(std::index_sequence<Is...>) -> std::vector<std::shared_ptr<arrow::Table>> {
return {pc.inputs().get<TableConsumer>(o2::aod::label<refs[Is]>())->asArrowTable()...};
}(std::make_index_sequence<refs.size()>());
}
namespace
{
template <typename D>
requires(D::exclusive)
auto make_build(D metadata, InputSpec const& input, ProcessingContext& pc)
{
using metadata_t = decltype(metadata);
using Key = typename metadata_t::Key;
using index_pack_t = typename metadata_t::index_pack_t;
constexpr auto sources = metadata_t::sources;
return o2::framework::IndexBuilder<o2::framework::Exclusive>::indexBuilder<Key, sources.size(), sources>(input.binding.c_str(),
extractOriginals<sources.size(), sources>(pc),
index_pack_t{});
}
template <typename D>
requires(!D::exclusive)
auto make_build(D metadata, InputSpec const& input, ProcessingContext& pc)
{
using metadata_t = decltype(metadata);
using Key = typename metadata_t::Key;
using index_pack_t = typename metadata_t::index_pack_t;
constexpr auto sources = metadata_t::sources;
return o2::framework::IndexBuilder<o2::framework::Sparse>::indexBuilder<Key, sources.size(), sources>(input.binding.c_str(),
extractOriginals<sources.size(), sources>(pc),
index_pack_t{});
}
} // namespace
AlgorithmSpec AODReaderHelpers::indexBuilderCallback(std::vector<InputSpec>& requested)
{
return AlgorithmSpec::InitCallback{[requested](InitContext& /*ic*/) {
return [requested](ProcessingContext& pc) {
auto outputs = pc.outputs();
// spawn tables
for (auto& input : requested) {
auto&& [origin, description, version] = DataSpecUtils::asConcreteDataMatcher(input);
if (description == header::DataDescription{"MA_RN2_EX"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::Run2MatchedExclusiveMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_RN2_SP"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::Run2MatchedSparseMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_RN3_EX"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::Run3MatchedExclusiveMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_RN3_SP"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::Run3MatchedSparseMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_BCCOL_EX"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::MatchedBCCollisionsExclusiveMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_BCCOL_SP"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::MatchedBCCollisionsSparseMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_BCCOLS_EX"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::MatchedBCCollisionsExclusiveMultiMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_BCCOLS_SP"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::MatchedBCCollisionsSparseMultiMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_RN3_BC_SP"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::Run3MatchedToBCSparseMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_RN3_BC_EX"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::Run3MatchedToBCExclusiveMetadata{}, input, pc));
} else if (description == header::DataDescription{"MA_RN2_BC_SP"}) {
outputs.adopt(Output{origin, description, version}, make_build(o2::aod::Run2MatchedToBCSparseMetadata{}, input, pc));
} else {
throw std::runtime_error("Not an index table");
}
}
};
}};
}
namespace
{
template <o2::aod::is_aod_hash D>
auto make_spawn(InputSpec const& input, ProcessingContext& pc)
{
using metadata_t = o2::aod::MetadataTrait<D>::metadata;
constexpr auto sources = metadata_t::sources;
static std::shared_ptr<gandiva::Projector> projector = nullptr;
static std::shared_ptr<arrow::Schema> schema = std::make_shared<arrow::Schema>(o2::soa::createFieldsFromColumns(typename metadata_t::expression_pack_t{}));
static auto projectors = []<typename... C>(framework::pack<C...>) -> std::array<expressions::Projector, sizeof...(C)>
{
return {{std::move(C::Projector())...}};
}
(typename metadata_t::expression_pack_t{});
return o2::framework::spawner<D>(extractOriginals<sources.size(), sources>(pc), input.binding.c_str(), projectors.data(), projector, schema);
}
struct Maker {
std::string binding;
std::vector<std::string> labels;
std::vector<std::shared_ptr<gandiva::Expression>> expressions;
std::shared_ptr<gandiva::Projector> projector = nullptr;
std::shared_ptr<arrow::Schema> schema;
header::DataOrigin origin;
header::DataDescription description;
header::DataHeader::SubSpecificationType version;
std::shared_ptr<arrow::Table> make(ProcessingContext& pc)
{
std::vector<std::shared_ptr<arrow::Table>> originals;
for (auto const& label : labels) {
originals.push_back(pc.inputs().get<TableConsumer>(label)->asArrowTable());
}
auto fullTable = soa::ArrowHelpers::joinTables(std::move(originals), std::span{labels.begin(), labels.size()});
if (projector == nullptr) {
auto s = gandiva::Projector::Make(
fullTable->schema(),
expressions,
&projector);
if (!s.ok()) {
throw o2::framework::runtime_error_f("Failed to create projector: %s", s.ToString().c_str());
}
}
return spawnerHelper(fullTable, schema, binding.c_str(), schema->num_fields(), projector);
}
};
struct Spawnable {
std::string binding;
std::vector<std::string> labels;
std::vector<expressions::Projector> projectors;
std::vector<std::shared_ptr<gandiva::Expression>> expressions;
std::shared_ptr<arrow::Schema> outputSchema;
std::shared_ptr<arrow::Schema> inputSchema;
header::DataOrigin origin;
header::DataDescription description;
header::DataHeader::SubSpecificationType version;
Spawnable(InputSpec const& spec)
: binding{spec.binding}
{
auto&& [origin_, description_, version_] = DataSpecUtils::asConcreteDataMatcher(spec);
origin = origin_;
description = description_;
version = version_;
auto loc = std::find_if(spec.metadata.begin(), spec.metadata.end(), [](ConfigParamSpec const& cps) { return cps.name.compare("projectors") == 0; });
std::stringstream iws(loc->defaultValue.get<std::string>());
projectors = ExpressionJSONHelpers::read(iws);
loc = std::find_if(spec.metadata.begin(), spec.metadata.end(), [](ConfigParamSpec const& cps) { return cps.name.compare("schema") == 0; });
iws.clear();
iws.str(loc->defaultValue.get<std::string>());
outputSchema = ArrowJSONHelpers::read(iws);
for (auto& i : spec.metadata) {
if (i.name.starts_with("input:")) {
labels.emplace_back(i.name.substr(6));
}
}
std::vector<std::shared_ptr<arrow::Field>> fields;
for (auto& p : projectors) {
expressions::walk(p.node.get(),
[&fields](expressions::Node* n) mutable {
if (n->self.index() == 1) {
auto& b = std::get<expressions::BindingNode>(n->self);
if (std::find_if(fields.begin(), fields.end(), [&b](std::shared_ptr<arrow::Field> const& field) { return field->name() == b.name; }) == fields.end()) {
fields.emplace_back(std::make_shared<arrow::Field>(b.name, expressions::concreteArrowType(b.type)));
}
}
});
}
inputSchema = std::make_shared<arrow::Schema>(fields);
int i = 0;
for (auto& p : projectors) {
expressions.push_back(
expressions::makeExpression(
expressions::createExpressionTree(
expressions::createOperations(p),
inputSchema),
outputSchema->field(i)));
++i;
}
}
std::shared_ptr<gandiva::Projector> makeProjector()
{
return expressions::createProjectorHelper(projectors.size(), projectors.data(), inputSchema, outputSchema->fields());
}
Maker createMaker()
{
return {
binding,
labels,
expressions,
nullptr,
outputSchema,
origin,
description,
version};
}
};
} // namespace
AlgorithmSpec AODReaderHelpers::aodSpawnerCallback(/*std::vector<InputSpec>& requested*/ ConfigContext const& ctx)
{
auto& ac = ctx.services().get<AnalysisContext>();
return AlgorithmSpec::InitCallback{[requested = ac.spawnerInputs](InitContext& /*ic*/) {
std::vector<Spawnable> spawnables;
for (auto& i : requested) {
spawnables.emplace_back(i);
}
std::vector<Maker> makers;
for (auto& s : spawnables) {
makers.push_back(s.createMaker());
}
return [makers](ProcessingContext& pc) mutable {
auto outputs = pc.outputs();
for (auto& maker : makers) {
outputs.adopt(Output{maker.origin, maker.description, maker.version}, maker.make(pc));
}
};
}};
}
} // namespace o2::framework::readers