Project
Loading...
Searching...
No Matches
ASoA.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
12#include "Framework/ASoA.h"
13#include "ArrowDebugHelpers.h"
15#include <arrow/util/key_value_metadata.h>
16#include <arrow/util/config.h>
17#include <TMemFile.h>
18#include <TClass.h>
19#include <TTree.h>
20#include <TH1.h>
21#include <TError.h>
22
23namespace o2::soa
24{
25void accessingInvalidIndexFor(const char* getter)
26{
27 throw o2::framework::runtime_error_f("Accessing invalid index for %s", getter);
28}
29void dereferenceWithWrongType(const char* getter, const char* target)
30{
31 throw o2::framework::runtime_error_f("Trying to dereference index with a wrong type in %s_as<T> for base target \"%s\". Note that if you have several compatible index targets in your process() signature, the last one will be the one actually bound.", getter, target);
32}
34{
35 throw o2::framework::runtime_error_f("Null selection for %d (arg %d), missing Filter declaration?", hash, ai);
36}
37
38void getterNotFound(const char* targetColumnLabel)
39{
40 throw o2::framework::runtime_error_f("Getter for \"%s\" not found", targetColumnLabel);
41}
42
44{
45 throw framework::runtime_error("columnLabel: must not be empty");
46}
47
49{
51 rows.resize(sel->GetNumSlots());
52 for (auto i = 0; i < sel->GetNumSlots(); ++i) {
53 rows[i] = sel->GetIndex(i);
54 }
55 return rows;
56}
57
58SelectionVector sliceSelection(std::span<int64_t const> const& mSelectedRows, int64_t nrows, uint64_t offset)
59{
60 auto start = offset;
61 auto end = start + nrows;
62 auto start_iterator = std::lower_bound(mSelectedRows.begin(), mSelectedRows.end(), start);
63 auto stop_iterator = std::lower_bound(start_iterator, mSelectedRows.end(), end);
64 SelectionVector slicedSelection{start_iterator, stop_iterator};
65 std::ranges::transform(slicedSelection.begin(), slicedSelection.end(), slicedSelection.begin(),
66 [&start](int64_t idx) {
67 return idx - static_cast<int64_t>(start);
68 });
69 return slicedSelection;
70}
71
72std::shared_ptr<arrow::Table> ArrowHelpers::joinTables(std::vector<std::shared_ptr<arrow::Table>>&& tables)
73{
74 std::vector<std::shared_ptr<arrow::Field>> fields;
75 std::vector<std::shared_ptr<arrow::ChunkedArray>> columns;
76 bool notEmpty = (tables[0]->num_rows() != 0);
77 std::ranges::for_each(tables, [&fields, &columns, notEmpty](auto const& t) {
78 std::ranges::copy(t->fields(), std::back_inserter(fields));
79 if (notEmpty) {
80 std::ranges::copy(t->columns(), std::back_inserter(columns));
81 }
82 });
83 auto schema = std::make_shared<arrow::Schema>(fields);
84 return arrow::Table::Make(schema, columns);
85}
86
87namespace
88{
89template <typename T>
90 requires(std::same_as<T, std::string>)
91auto makeString(T const& str)
92{
93 return str.c_str();
94}
95template <typename T>
96 requires(std::same_as<T, const char*>)
97auto makeString(T const& str)
98{
99 return str;
100}
101
102template <typename T>
103void canNotJoin(std::vector<std::shared_ptr<arrow::Table>> const& tables, std::span<T> labels)
104{
105 for (auto i = 0U; i < tables.size() - 1; ++i) {
106 if (tables[i]->num_rows() != tables[i + 1]->num_rows()) {
107 throw o2::framework::runtime_error_f("Tables %s and %s have different sizes (%d vs %d) and cannot be joined!",
108 makeString(labels[i]), makeString(labels[i + 1]), tables[i]->num_rows(), tables[i + 1]->num_rows());
109 }
110 }
111}
112} // namespace
113
114std::shared_ptr<arrow::Table> ArrowHelpers::joinTables(std::vector<std::shared_ptr<arrow::Table>>&& tables, std::span<const char* const> labels)
115{
116 if (tables.size() == 1) {
117 return tables[0];
118 }
119 canNotJoin(tables, labels);
120 return joinTables(std::forward<std::vector<std::shared_ptr<arrow::Table>>>(tables));
121}
122
123std::shared_ptr<arrow::Table> ArrowHelpers::joinTables(std::vector<std::shared_ptr<arrow::Table>>&& tables, std::span<const std::string> labels)
124{
125 if (tables.size() == 1) {
126 return tables[0];
127 }
128 canNotJoin(tables, labels);
129 return joinTables(std::forward<std::vector<std::shared_ptr<arrow::Table>>>(tables));
130}
131
132std::shared_ptr<arrow::Table> ArrowHelpers::concatTables(std::vector<std::shared_ptr<arrow::Table>>&& tables)
133{
134 if (tables.size() == 1) {
135 return tables[0];
136 }
137 std::vector<std::shared_ptr<arrow::ChunkedArray>> columns;
138 std::vector<std::shared_ptr<arrow::Field>> resultFields = tables[0]->schema()->fields();
139 auto compareFields = [](std::shared_ptr<arrow::Field> const& f1, std::shared_ptr<arrow::Field> const& f2) {
140 // Let's do this with stable sorting.
141 return (!f1->Equals(f2)) && (f1->name() < f2->name());
142 };
143 for (size_t i = 1; i < tables.size(); ++i) {
144 auto& fields = tables[i]->schema()->fields();
145 std::vector<std::shared_ptr<arrow::Field>> intersection;
146
147 std::set_intersection(resultFields.begin(), resultFields.end(),
148 fields.begin(), fields.end(),
149 std::back_inserter(intersection), compareFields);
150 resultFields.swap(intersection);
151 }
152
153 for (auto& field : resultFields) {
154 arrow::ArrayVector chunks;
155 for (auto& table : tables) {
156 auto ci = table->schema()->GetFieldIndex(field->name());
157 if (ci == -1) {
158 throw std::runtime_error("Unable to find field " + field->name());
159 }
160 auto column = table->column(ci);
161 auto otherChunks = column->chunks();
162 chunks.insert(chunks.end(), otherChunks.begin(), otherChunks.end());
163 }
164 columns.push_back(std::make_shared<arrow::ChunkedArray>(chunks));
165 }
166
167 return arrow::Table::Make(std::make_shared<arrow::Schema>(resultFields), columns);
168}
169
170arrow::ChunkedArray* getIndexFromLabel(arrow::Table* table, std::string_view label)
171{
172 auto field = std::ranges::find_if(table->schema()->fields(), [&](std::shared_ptr<arrow::Field> const& f) {
173 auto caseInsensitiveCompare = [](const std::string_view& str1, const std::string& str2) {
174 return std::ranges::equal(
175 str1, str2,
176 [](char c1, char c2) {
177 return std::tolower(static_cast<unsigned char>(c1)) ==
178 std::tolower(static_cast<unsigned char>(c2));
179 });
180 };
181
182 return caseInsensitiveCompare(label, f->name());
183 });
184 if (field == table->schema()->fields().end()) {
185 o2::framework::throw_error(o2::framework::runtime_error_f("Unable to find column with label %s.", label));
186 }
187 auto index = std::distance(table->schema()->fields().begin(), field);
188 return table->column(index).get();
189}
190
191void notBoundTable(const char* tableName)
192{
193 throw o2::framework::runtime_error_f("Index pointing to %s is not bound! Did you subscribe to the table?", tableName);
194}
195
196void notFoundColumn(const char* label, const char* key)
197{
198 throw o2::framework::runtime_error_f(R"(Preslice not valid: table "%s" (or join based on it) does not have column "%s")", label, key);
199}
200
201void missingOptionalPreslice(const char* label, const char* key)
202{
203 throw o2::framework::runtime_error_f(R"(Optional Preslice with missing binding used: table "%s" (or join based on it) does not have column "%s")", label, key);
204}
205
206void* extractCCDBPayload(char* payload, size_t size, TClass const* cl, const char* what)
207{
208 Int_t previousErrorLevel = gErrorIgnoreLevel;
209 gErrorIgnoreLevel = kFatal;
210 // does it have a flattened headers map attached in the end?
211 TMemFile file("name", (char*)payload, size, "READ");
212 gErrorIgnoreLevel = previousErrorLevel;
213 if (file.IsZombie()) {
214 return nullptr;
215 }
216
217 if (!cl) {
218 return nullptr;
219 }
220 auto object = file.GetObjectChecked(what, cl);
221 if (!object) {
222 // it could be that object was stored with previous convention
223 // where the classname was taken as key
224 std::string objectName(cl->GetName());
225 objectName.erase(std::find_if(objectName.rbegin(), objectName.rend(), [](unsigned char ch) {
226 return !std::isspace(ch);
227 }).base(),
228 objectName.end());
229 objectName.erase(objectName.begin(), std::find_if(objectName.begin(), objectName.end(), [](unsigned char ch) {
230 return !std::isspace(ch);
231 }));
232
233 object = file.GetObjectChecked(objectName.c_str(), cl);
234 LOG(warn) << "Did not find object under expected name " << what;
235 if (!object) {
236 return nullptr;
237 }
238 LOG(warn) << "Found object under deprecated name " << cl->GetName();
239 }
240 auto result = object;
241 // We need to handle some specific cases as ROOT ties them deeply
242 // to the file they are contained in
243 if (cl->InheritsFrom("TObject")) {
244 // make a clone
245 // detach from the file
246 auto tree = dynamic_cast<TTree*>((TObject*)object);
247 if (tree) {
248 tree->LoadBaskets(0x1L << 32); // make tree memory based
249 tree->SetDirectory(nullptr);
250 result = tree;
251 } else {
252 auto h = dynamic_cast<TH1*>((TObject*)object);
253 if (h) {
254 h->SetDirectory(nullptr);
255 result = h;
256 }
257 }
258 }
259 return result;
260}
261
262} // namespace o2::soa
263
264namespace o2::framework
265{
266std::string cutString(std::string&& str)
267{
268 auto pos = str.find('_');
269 if (pos != std::string::npos) {
270 str.erase(pos);
271 }
272 return str;
273}
274
275std::string strToUpper(std::string&& str)
276{
277 std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return std::toupper(c); });
278 return str;
279}
280
282{
283 return binding == "[MISSING]";
284}
285
287{
288 return bindingKey;
289}
290
295
300
301std::shared_ptr<arrow::Table> PreslicePolicySorted::getSliceFor(int value, std::shared_ptr<arrow::Table> const& input, uint64_t& offset) const
302{
303 auto [offset_, count] = this->sliceInfo.getSliceFor(value);
304 auto output = input->Slice(offset_, count);
305 offset = static_cast<int64_t>(offset_);
306 return output;
307}
308
309std::span<const int64_t> PreslicePolicyGeneral::getSliceFor(int value) const
310{
311 return this->sliceInfo.getSliceFor(value);
312}
313} // namespace o2::framework
std::vector< std::string > labels
uint32_t hash
std::shared_ptr< arrow::Schema > schema
std::vector< std::shared_ptr< arrow::Field > > fields
int32_t i
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
uint16_t pos
Definition RawData.h:3
uint32_t c
Definition RawData.h:2
StringRef key
Class for time synchronization of RawReader instances.
GLint GLsizei count
Definition glcorearb.h:399
GLuint64EXT * result
Definition glcorearb.h:5662
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLuint index
Definition glcorearb.h:781
GLdouble f
Definition glcorearb.h:310
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLenum target
Definition glcorearb.h:1641
GLintptr offset
Definition glcorearb.h:660
GLuint GLsizei const GLchar * label
Definition glcorearb.h:2519
GLuint object
Definition glcorearb.h:4041
GLuint start
Definition glcorearb.h:469
std::shared_ptr< gandiva::SelectionVector > Selection
Definition Expressions.h:46
Defining PrimaryVertex explicitly as messageable.
RuntimeErrorRef runtime_error(const char *)
void throw_error(RuntimeErrorRef)
std::string strToUpper(std::string &&str)
Definition ASoA.cxx:275
RuntimeErrorRef runtime_error_f(const char *,...)
std::string cutString(std::string &&str)
Definition ASoA.cxx:266
void * extractCCDBPayload(char *payload, size_t size, TClass const *cl, const char *what)
Definition ASoA.cxx:206
SelectionVector selectionToVector(gandiva::Selection const &sel)
Definition ASoA.cxx:48
void notBoundTable(const char *tableName)
Definition ASoA.cxx:191
SelectionVector sliceSelection(std::span< int64_t const > const &mSelectedRows, int64_t nrows, uint64_t offset)
Definition ASoA.cxx:58
std::vector< int64_t > SelectionVector
Definition ASoA.h:429
void missingFilterDeclaration(int hash, int ai)
Definition ASoA.cxx:33
void accessingInvalidIndexFor(const char *getter)
Definition ASoA.cxx:25
void dereferenceWithWrongType(const char *getter, const char *target)
Definition ASoA.cxx:29
void emptyColumnLabel()
Definition ASoA.cxx:43
void getterNotFound(const char *targetColumnLabel)
Definition ASoA.cxx:38
void missingOptionalPreslice(const char *label, const char *key)
Definition ASoA.cxx:201
arrow::ChunkedArray * getIndexFromLabel(arrow::Table *table, std::string_view label)
Definition ASoA.cxx:170
void notFoundColumn(const char *label, const char *key)
Definition ASoA.cxx:196
const std::string binding
Definition ASoA.h:1498
Entry const & getBindingKey() const
Definition ASoA.cxx:286
SliceInfoUnsortedPtr sliceInfo
Definition ASoA.h:1515
std::span< const int64_t > getSliceFor(int value) const
Definition ASoA.cxx:309
void updateSliceInfo(SliceInfoUnsortedPtr &&si)
Definition ASoA.cxx:296
void updateSliceInfo(SliceInfoPtr &&si)
Definition ASoA.cxx:291
std::shared_ptr< arrow::Table > getSliceFor(int value, std::shared_ptr< arrow::Table > const &input, uint64_t &offset) const
Definition ASoA.cxx:301
std::pair< int64_t, int64_t > getSliceFor(int value) const
std::span< int64_t const > getSliceFor(int value) const
static std::shared_ptr< arrow::Table > joinTables(std::vector< std::shared_ptr< arrow::Table > > &&tables)
Definition ASoA.cxx:72
static std::shared_ptr< arrow::Table > concatTables(std::vector< std::shared_ptr< arrow::Table > > &&tables)
Definition ASoA.cxx:132
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))
std::vector< ReadoutWindowData > rows
const std::string str