-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathAODReaderHelpers.cxx
More file actions
202 lines (180 loc) · 7.54 KB
/
AODReaderHelpers.cxx
File metadata and controls
202 lines (180 loc) · 7.54 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
// 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 "AODReaderHelpers.h"
#include "../src/ExpressionJSONHelpers.h"
#include "../src/IndexJSONHelpers.h"
#include "Framework/AnalysisDataModel.h"
#include "Framework/AnalysisHelpers.h"
#include "Framework/DataProcessingHelpers.h"
#include "Framework/AlgorithmSpec.h"
#include "Framework/DataSpecUtils.h"
#include "Framework/DataSpecViews.h"
#include "Framework/ConfigContext.h"
#include "Framework/DanglingEdgesContext.h"
namespace o2::framework::readers
{
namespace
{
struct Buildable {
bool exclusive = false;
std::string binding;
std::vector<std::string> labels;
std::vector<framework::ConcreteDataMatcher> matchers;
header::DataOrigin origin;
header::DataDescription description;
header::DataHeader::SubSpecificationType version;
std::vector<o2::soa::IndexRecord> records;
std::shared_ptr<arrow::Schema> outputSchema;
explicit Buildable(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("index-records") == 0; });
std::stringstream iws(loc->defaultValue.get<std::string>());
records = IndexJSONHelpers::read(iws);
loc = std::find_if(spec.metadata.begin(), spec.metadata.end(), [](ConfigParamSpec const& cps) { return cps.name.compare("index-exclusive") == 0; });
exclusive = loc->defaultValue.get<bool>();
for (auto const& r : records) {
labels.emplace_back(r.label);
matchers.emplace_back(r.matcher);
}
outputSchema = std::make_shared<arrow::Schema>([](std::vector<o2::soa::IndexRecord> const& recs) {
std::vector<std::shared_ptr<arrow::Field>> fields;
fields.reserve(recs.size());
std::ranges::transform(recs, std::back_inserter(fields), [](auto& r) { return r.field(); });
return fields;
}(records))
->WithMetadata(std::make_shared<arrow::KeyValueMetadata>(std::vector{std::string{"label"}}, std::vector{std::string{binding}}));
}
framework::Builder createBuilder() const
{
return {
exclusive,
labels,
matchers,
records,
outputSchema,
origin,
description,
version,
nullptr};
}
};
} // namespace
AlgorithmSpec AODReaderHelpers::indexBuilderCallback(ConfigContext const& /*ctx*/)
{
return AlgorithmSpec::InitCallback{[](InitContext& ic) {
auto const& requested = ic.services().get<DanglingEdgesContext>().requestedIDXs;
std::vector<Builder> builders;
builders.reserve(requested.size());
std::ranges::transform(requested, std::back_inserter(builders), [](auto const& i) { return Buildable{i}.createBuilder(); });
return [builders](ProcessingContext& pc) mutable {
auto outputs = pc.outputs();
std::ranges::for_each(builders, [&pc, &outputs](auto& builder) { outputs.adopt(Output{builder.origin, builder.description, builder.version}, builder.materialize(pc)); });
};
}};
}
namespace
{
struct Spawnable {
std::string binding;
std::vector<std::string> labels;
std::vector<framework::ConcreteDataMatcher> matchers;
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;
explicit 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);
o2::framework::addLabelToSchema(outputSchema, binding.c_str());
std::vector<std::shared_ptr<arrow::Schema>> schemas;
for (auto const& i : spec.metadata | views::filter_string_params_starts_with("input-schema:")) {
labels.emplace_back(i.name.substr(13));
iws.clear();
auto json = i.defaultValue.get<std::string>();
iws.str(json);
schemas.emplace_back(ArrowJSONHelpers::read(iws));
}
std::ranges::transform(spec.metadata |
views::filter_string_params_starts_with("input:") |
std::ranges::views::transform(
[](auto const& param) {
return DataSpecUtils::fromMetadataString(param.defaultValue.template get<std::string>());
}),
std::back_inserter(matchers), [](auto const& i) { return std::get<ConcreteDataMatcher>(i.matcher); });
std::vector<std::shared_ptr<arrow::Field>> fields;
std::ranges::for_each(schemas,
[&fields](auto const& s) {
std::ranges::copy(s->fields(), std::back_inserter(fields));
});
inputSchema = std::make_shared<arrow::Schema>(fields);
expressions = expressions::materializeProjectors(projectors, inputSchema, outputSchema->fields());
}
std::shared_ptr<gandiva::Projector> makeProjector() const
{
std::shared_ptr<gandiva::Projector> p = nullptr;
auto s = gandiva::Projector::Make(
inputSchema,
expressions,
&p);
if (!s.ok()) {
throw o2::framework::runtime_error_f("Failed to create projector: %s", s.ToString().c_str());
}
return p;
}
framework::Spawner createMaker() const
{
return {
binding,
labels,
matchers,
expressions,
makeProjector(),
outputSchema,
inputSchema,
origin,
description,
version};
}
};
} // namespace
AlgorithmSpec AODReaderHelpers::aodSpawnerCallback(ConfigContext const& /*ctx*/)
{
return AlgorithmSpec::InitCallback{[](InitContext& ic) {
auto const& requested = ic.services().get<DanglingEdgesContext>().spawnerInputs;
std::vector<Spawner> spawners;
spawners.reserve(requested.size());
std::ranges::transform(requested, std::back_inserter(spawners), [](auto const& i) { return Spawnable{i}.createMaker(); });
return [spawners](ProcessingContext& pc) mutable {
auto outputs = pc.outputs();
std::ranges::for_each(spawners, [&pc, &outputs](auto& spawner) { outputs.adopt(Output{spawner.origin, spawner.description, spawner.version}, spawner.materialize(pc)); });
};
}};
}
} // namespace o2::framework::readers