-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathAnalysisHelpers.h
More file actions
1047 lines (899 loc) · 34.5 KB
/
AnalysisHelpers.h
File metadata and controls
1047 lines (899 loc) · 34.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
#ifndef o2_framework_AnalysisHelpers_H_DEFINED
#define o2_framework_AnalysisHelpers_H_DEFINED
#include "ConfigParamSpec.h"
#include "Framework/ASoA.h"
#include "Framework/DataAllocator.h"
#include "Framework/IndexBuilderHelpers.h"
#include "Framework/InputSpec.h"
#include "Framework/Output.h"
#include "Framework/OutputObjHeader.h"
#include "Framework/OutputRef.h"
#include "Framework/OutputSpec.h"
#include "Framework/Plugins.h"
#include "Framework/StringHelpers.h"
#include "Framework/TableBuilder.h"
#include "Framework/Traits.h"
#include <string>
namespace o2::soa
{
struct IndexRecord {
std::string label;
framework::ConcreteDataMatcher matcher;
std::string columnLabel;
IndexKind kind;
int pos;
std::shared_ptr<arrow::DataType> type = [](IndexKind kind) -> std::shared_ptr<arrow::DataType> {
switch (kind) {
case IndexKind::IdxSingle:
case IndexKind::IdxSelf:
return arrow::int32();
case IndexKind::IdxSlice:
return arrow::fixed_size_list(arrow::int32(), 2);
case IndexKind::IdxArray:
return arrow::list(arrow::int32());
default:
return {nullptr};
}
}(kind);
auto operator==(IndexRecord const& other) const
{
return (this->label == other.label) && (this->columnLabel == other.columnLabel) && (this->kind == other.kind) && (this->pos == other.pos);
}
std::shared_ptr<arrow::Field> field() const
{
return std::make_shared<arrow::Field>(columnLabel, type);
}
};
struct IndexBuilder {
static std::vector<framework::IndexColumnBuilder> makeBuilders(std::vector<std::shared_ptr<arrow::Table>>&& tables, std::vector<soa::IndexRecord> const& records);
static void resetBuilders(std::vector<framework::IndexColumnBuilder>& builders, std::vector<std::shared_ptr<arrow::Table>>&& tables);
static std::shared_ptr<arrow::Table> materialize(std::vector<framework::IndexColumnBuilder>& builders, std::vector<std::shared_ptr<arrow::Table>>&& tables, std::vector<soa::IndexRecord> const& records, std::shared_ptr<arrow::Schema> const& schema, bool exclusive);
};
} // namespace o2::soa
namespace o2::framework
{
std::shared_ptr<arrow::Table> makeEmptyTableImpl(const char* name, std::shared_ptr<arrow::Schema>& schema);
template <soa::is_table T>
auto makeEmptyTable(const char* name)
{
auto schema = std::make_shared<arrow::Schema>(soa::createFieldsFromColumns(typename T::table_t::persistent_columns_t{}));
return makeEmptyTableImpl(name, schema);
}
template <soa::TableRef R>
auto makeEmptyTable()
{
auto schema = std::make_shared<arrow::Schema>(soa::createFieldsFromColumns(typename aod::MetadataTrait<aod::Hash<R.desc_hash>>::metadata::persistent_columns_t{}));
return makeEmptyTableImpl(o2::aod::label<R>(), schema);
}
template <typename... Cs>
auto makeEmptyTable(const char* name, framework::pack<Cs...> p)
{
auto schema = std::make_shared<arrow::Schema>(soa::createFieldsFromColumns(p));
return makeEmptyTableImpl(name, schema);
}
template <aod::is_aod_hash D>
auto makeEmptyTable(const char* name)
{
auto schema = std::make_shared<arrow::Schema>(soa::createFieldsFromColumns(typename aod::MetadataTrait<D>::metadata::persistent_columns_t{}));
return makeEmptyTableImpl(name, schema);
}
std::shared_ptr<arrow::Table> spawnerHelper(std::shared_ptr<arrow::Table> const& fullTable, std::shared_ptr<arrow::Schema> newSchema, size_t nColumns,
expressions::Projector* projectors, const char* name, std::shared_ptr<gandiva::Projector>& projector);
std::shared_ptr<arrow::Table> spawnerHelper(std::shared_ptr<arrow::Table> const& fullTable, std::shared_ptr<arrow::Schema> newSchema,
const char* name, size_t nColumns,
const std::shared_ptr<gandiva::Projector>& projector);
/// Expression-based column generator to materialize columns
template <aod::is_aod_hash D>
requires(soa::has_extension<typename o2::aod::MetadataTrait<D>::metadata>)
auto spawner(std::shared_ptr<arrow::Table> const& fullTable, const char* name, o2::framework::expressions::Projector* projectors, std::shared_ptr<gandiva::Projector>& projector, std::shared_ptr<arrow::Schema> const& schema)
{
if (fullTable->num_rows() == 0) {
return makeEmptyTable<D>(name);
}
constexpr auto Ncol = []<typename M>() {
if constexpr (soa::has_configurable_extension<M>) {
return framework::pack_size(typename M::placeholders_pack_t{});
} else {
return framework::pack_size(typename M::expression_pack_t{});
}
}.template operator()<typename o2::aod::MetadataTrait<D>::metadata>();
return spawnerHelper(fullTable, schema, Ncol, projectors, name, projector);
}
template <typename... C>
auto spawner(framework::pack<C...>, std::vector<std::shared_ptr<arrow::Table>>&& tables, const char* name, expressions::Projector* projectors, std::shared_ptr<gandiva::Projector>& projector, std::shared_ptr<arrow::Schema> const& schema)
{
std::array<const char*, 1> labels{"original"};
auto fullTable = soa::ArrowHelpers::joinTables(std::move(tables), std::span<const char* const>{labels});
if (fullTable->num_rows() == 0) {
return makeEmptyTable(name, framework::pack<C...>{});
}
return spawnerHelper(fullTable, schema, sizeof...(C), projectors, name, projector);
}
std::string serializeProjectors(std::vector<framework::expressions::Projector>& projectors);
std::string serializeSchema(std::shared_ptr<arrow::Schema> schema);
std::string serializeIndexRecords(std::vector<o2::soa::IndexRecord>& irs);
std::vector<std::shared_ptr<arrow::Table>> extractSources(ProcessingContext& pc, std::vector<std::string> const& labels);
struct Spawner {
std::string binding;
std::vector<std::string> labels;
std::vector<framework::ConcreteDataMatcher> matchers;
std::vector<std::shared_ptr<gandiva::Expression>> expressions;
std::shared_ptr<gandiva::Projector> projector = nullptr;
std::shared_ptr<arrow::Schema> schema = nullptr;
std::shared_ptr<arrow::Schema> inputSchema = nullptr;
header::DataOrigin origin;
header::DataDescription description;
header::DataHeader::SubSpecificationType version;
std::shared_ptr<arrow::Table> materialize(ProcessingContext& pc) const;
};
struct Builder {
bool exclusive;
std::vector<std::string> labels;
std::vector<framework::ConcreteDataMatcher> matchers;
std::vector<o2::soa::IndexRecord> records;
std::shared_ptr<arrow::Schema> outputSchema;
header::DataOrigin origin;
header::DataDescription description;
header::DataHeader::SubSpecificationType version;
std::shared_ptr<std::vector<framework::IndexColumnBuilder>> builders = nullptr;
std::shared_ptr<arrow::Table> materialize(ProcessingContext& pc);
};
} // namespace o2::framework
namespace o2::soa
{
template <TableRef R>
constexpr auto tableRef2ConfigParamSpec()
{
return o2::framework::ConfigParamSpec{
std::string{"input:"} + o2::aod::label<R>(),
framework::VariantType::String,
aod::sourceSpec<R>(),
{"\"\""}};
}
template <TableRef R>
constexpr auto tableRef2Schema()
{
return o2::framework::ConfigParamSpec{
std::string{"input-schema:"} + o2::aod::label<R>(),
framework::VariantType::String,
framework::serializeSchema(o2::aod::MetadataTrait<o2::aod::Hash<R.desc_hash>>::metadata::getSchema()),
{"\"\""}};
}
namespace
{
template <soa::with_sources T>
inline constexpr auto getSources()
{
return []<size_t N, std::array<soa::TableRef, N> refs>() {
return []<size_t... Is>(std::index_sequence<Is...>) {
return std::vector{soa::tableRef2ConfigParamSpec<refs[Is]>()...};
}(std::make_index_sequence<N>());
}.template operator()<T::sources.size(), T::sources>();
}
template <soa::with_sources T>
inline constexpr auto getSourceSchemas()
{
return []<size_t N, std::array<soa::TableRef, N> refs>() {
return []<size_t... Is>(std::index_sequence<Is...>) {
return std::vector{soa::tableRef2Schema<refs[Is]>()...};
}(std::make_index_sequence<N>());
}.template operator()<T::sources.size(), T::sources>();
}
template <soa::with_ccdb_urls T>
inline constexpr auto getCCDBUrls()
{
std::vector<framework::ConfigParamSpec> result;
for (size_t i = 0; i < T::ccdb_urls.size(); ++i) {
result.push_back({std::string{"ccdb:"} + std::string{T::ccdb_bindings[i]},
framework::VariantType::String,
T::ccdb_urls[i],
{"\"\""}});
}
return result;
}
template <typename T>
requires(std::same_as<T, int>)
consteval IndexKind getIndexKind()
{
return IndexKind::IdxSingle;
}
template <typename T>
requires(std::is_bounded_array_v<T>)
consteval IndexKind getIndexKind()
{
return IndexKind::IdxSlice;
}
template <typename T>
requires(framework::is_specialization_v<T, std::vector>)
consteval IndexKind getIndexKind()
{
return IndexKind::IdxArray;
}
template <soa::with_index_pack T>
inline constexpr auto getIndexMapping()
{
std::vector<IndexRecord> idx;
using indices = T::index_pack_t;
using Key = T::Key;
[&idx]<size_t... Is>(std::index_sequence<Is...>) mutable {
constexpr auto refs = T::sources;
([&idx]<TableRef ref, typename C>() mutable {
constexpr auto pos = o2::aod::MetadataTrait<o2::aod::Hash<ref.desc_hash>>::metadata::template getIndexPosToKey<Key>();
if constexpr (pos == -1) {
idx.emplace_back(o2::aod::label<ref>(), o2::aod::matcher<ref>(), C::columnLabel(), IndexKind::IdxSelf, pos);
} else {
idx.emplace_back(o2::aod::label<ref>(), o2::aod::matcher<ref>(), C::columnLabel(), getIndexKind<typename C::type>(), pos);
}
}.template operator()<refs[Is], typename framework::pack_element_t<Is, indices>>(),
...);
}(std::make_index_sequence<framework::pack_size(indices{})>());
;
return idx;
}
template <soa::with_sources T>
constexpr auto getInputMetadata() -> std::vector<framework::ConfigParamSpec>
{
std::vector<framework::ConfigParamSpec> inputMetadata;
auto inputSources = getSources<T>();
std::sort(inputSources.begin(), inputSources.end(), [](framework::ConfigParamSpec const& a, framework::ConfigParamSpec const& b) { return a.name < b.name; });
auto last = std::unique(inputSources.begin(), inputSources.end(), [](framework::ConfigParamSpec const& a, framework::ConfigParamSpec const& b) { return a.name == b.name; });
inputSources.erase(last, inputSources.end());
inputMetadata.insert(inputMetadata.end(), inputSources.begin(), inputSources.end());
auto inputSchemas = getSourceSchemas<T>();
std::sort(inputSchemas.begin(), inputSchemas.end(), [](framework::ConfigParamSpec const& a, framework::ConfigParamSpec const& b) { return a.name < b.name; });
last = std::unique(inputSchemas.begin(), inputSchemas.end(), [](framework::ConfigParamSpec const& a, framework::ConfigParamSpec const& b) { return a.name == b.name; });
inputSchemas.erase(last, inputSchemas.end());
inputMetadata.insert(inputMetadata.end(), inputSchemas.begin(), inputSchemas.end());
return inputMetadata;
}
template <typename T>
requires(!soa::with_sources<T>)
constexpr auto getInputMetadata() -> std::vector<framework::ConfigParamSpec>
{
return {};
}
template <soa::with_ccdb_urls T>
constexpr auto getCCDBMetadata() -> std::vector<framework::ConfigParamSpec>
{
std::vector<framework::ConfigParamSpec> results = getCCDBUrls<T>();
std::sort(results.begin(), results.end(), [](framework::ConfigParamSpec const& a, framework::ConfigParamSpec const& b) { return a.name < b.name; });
auto last = std::unique(results.begin(), results.end(), [](framework::ConfigParamSpec const& a, framework::ConfigParamSpec const& b) { return a.name == b.name; });
results.erase(last, results.end());
return results;
}
template <typename T>
constexpr auto getCCDBMetadata() -> std::vector<framework::ConfigParamSpec>
{
return {};
}
template <soa::with_expression_pack T>
constexpr auto getExpressionMetadata() -> std::vector<framework::ConfigParamSpec>
{
using expression_pack_t = T::expression_pack_t;
auto projectors = []<typename... C>(framework::pack<C...>) -> std::vector<framework::expressions::Projector> {
std::vector<framework::expressions::Projector> result;
(result.emplace_back(std::move(C::Projector())), ...);
return result;
}(expression_pack_t{});
auto json = framework::serializeProjectors(projectors);
return {framework::ConfigParamSpec{"projectors", framework::VariantType::String, json, {"\"\""}}};
}
template <typename T>
requires(!soa::with_expression_pack<T>)
constexpr auto getExpressionMetadata() -> std::vector<framework::ConfigParamSpec>
{
return {};
}
template <soa::with_index_pack T>
constexpr auto getIndexMetadata() -> std::vector<framework::ConfigParamSpec>
{
auto map = getIndexMapping<T>();
return {framework::ConfigParamSpec{"index-records", framework::VariantType::String, framework::serializeIndexRecords(map), {"\"\""}},
{framework::ConfigParamSpec{"index-exclusive", framework::VariantType::Bool, T::exclusive, {"\"\""}}}};
}
template <typename T>
requires(!soa::with_index_pack<T>)
constexpr auto getIndexMetadata() -> std::vector<framework::ConfigParamSpec>
{
return {};
}
} // namespace
template <TableRef R>
constexpr auto tableRef2InputSpec()
{
std::vector<framework::ConfigParamSpec> metadata;
auto m = getInputMetadata<typename o2::aod::MetadataTrait<o2::aod::Hash<R.desc_hash>>::metadata>();
metadata.insert(metadata.end(), m.begin(), m.end());
auto ccdbMetadata = getCCDBMetadata<typename o2::aod::MetadataTrait<o2::aod::Hash<R.desc_hash>>::metadata>();
metadata.insert(metadata.end(), ccdbMetadata.begin(), ccdbMetadata.end());
auto p = getExpressionMetadata<typename o2::aod::MetadataTrait<o2::aod::Hash<R.desc_hash>>::metadata>();
metadata.insert(metadata.end(), p.begin(), p.end());
auto idx = getIndexMetadata<typename o2::aod::MetadataTrait<o2::aod::Hash<R.desc_hash>>::metadata>();
metadata.insert(metadata.end(), idx.begin(), idx.end());
if constexpr (!soa::with_ccdb_urls<typename o2::aod::MetadataTrait<o2::aod::Hash<R.desc_hash>>::metadata>) {
metadata.emplace_back(framework::ConfigParamSpec{"schema", framework::VariantType::String, framework::serializeSchema(o2::aod::MetadataTrait<o2::aod::Hash<R.desc_hash>>::metadata::getSchema()), {"\"\""}});
}
return framework::InputSpec{
o2::aod::label<R>(),
o2::aod::origin<R>(),
o2::aod::description(o2::aod::signature<R>()),
R.version,
framework::Lifetime::Timeframe,
metadata};
}
template <TableRef R>
constexpr auto tableRef2OutputSpec()
{
return framework::OutputSpec{
framework::OutputLabel{o2::aod::label<R>()},
o2::aod::origin<R>(),
o2::aod::description(o2::aod::signature<R>()),
R.version};
}
template <TableRef R>
constexpr auto tableRef2Output()
{
return framework::Output{
o2::aod::origin<R>(),
o2::aod::description(o2::aod::signature<R>()),
R.version};
}
template <TableRef R>
constexpr auto tableRef2OutputRef()
{
return framework::OutputRef{
o2::aod::label<R>(),
R.version};
}
} // namespace o2::soa
namespace o2::framework
{
class TableConsumer;
/// Helper class actually implementing the cursor which can write to
/// a table. The provided template arguments are if type Column and
/// therefore refer only to the persisted columns.
template <typename T>
concept is_producable = soa::has_metadata<aod::MetadataTrait<T>> || soa::has_metadata<aod::MetadataTrait<typename T::parent_t>>;
template <typename T>
concept is_enumerated_iterator = requires(T t) { t.globalIndex(); };
template <is_producable T>
struct WritingCursor {
public:
using persistent_table_t = decltype([]() { if constexpr (soa::is_iterator<T>) { return typename T::parent_t{nullptr}; } else { return T{nullptr}; } }());
using cursor_t = decltype(std::declval<TableBuilder>().cursor<persistent_table_t>());
template <typename... Ts>
void operator()(Ts&&... args)
requires(sizeof...(Ts) == framework::pack_size(typename persistent_table_t::persistent_columns_t{}))
{
++mCount;
cursor(0, extract(args)...);
}
/// Last index inserted in the table
int64_t lastIndex()
{
return mCount;
}
bool resetCursor(LifetimeHolder<TableBuilder> builder)
{
mBuilder = std::move(builder);
cursor = std::move(FFL(mBuilder->cursor<persistent_table_t>()));
mCount = -1;
return true;
}
void setLabel(const char* label)
{
mBuilder->setLabel(label);
}
/// reserve @a size rows when filling, so that we do not
/// spend time reallocating the buffers.
void reserve(int64_t size)
{
mBuilder->reserve(typename persistent_table_t::column_types{}, size);
}
void release()
{
mBuilder.release();
}
decltype(FFL(std::declval<cursor_t>())) cursor;
private:
static decltype(auto) extract(is_enumerated_iterator auto const& arg)
{
return arg.globalIndex();
}
template <typename A>
requires(!is_enumerated_iterator<A>)
static decltype(auto) extract(A&& arg)
{
return arg;
}
/// The table builder which actually performs the
/// construction of the table. We keep it around to be
/// able to do all-columns methods like reserve.
LifetimeHolder<TableBuilder> mBuilder = nullptr;
int64_t mCount = -1;
};
/// Helper to define output for a Table
template <soa::is_table T>
consteval auto typeWithRef() -> T
{
}
template <soa::is_iterator T>
consteval auto typeWithRef() -> typename T::parent_t
{
}
template <typename T>
requires soa::is_table<T> || soa::is_iterator<T>
struct OutputForTable {
using table_t = decltype(typeWithRef<T>());
using metadata = aod::MetadataTrait<o2::aod::Hash<table_t::ref.desc_hash>>::metadata;
static OutputSpec const spec()
{
return OutputSpec{OutputLabel{aod::label<table_t::ref>()}, o2::aod::origin<table_t::ref>(), o2::aod::description(o2::aod::signature<table_t::ref>()), table_t::ref.version};
}
static OutputRef ref()
{
return OutputRef{aod::label<table_t::ref>(), table_t::ref.version};
}
};
/// This helper class allows you to declare things which will be created by a
/// given analysis task. Notice how the actual cursor is implemented by the
/// means of the WritingCursor helper class, from which produces actually
/// derives.
template <is_producable T>
struct Produces : WritingCursor<T> {
};
template <typename T>
concept is_produces = requires(T t) { typename T::cursor_t; typename T::persistent_table_t; &T::cursor; };
/// Use this to group together produces. Useful to separate them logically
/// or simply to stay within the 100 elements per Task limit.
/// Use as:
///
/// struct MySetOfProduces : ProducesGroup {
/// } products;
///
/// Notice the label MySetOfProduces is just a mnemonic and can be omitted.
struct ProducesGroup {
};
template <typename T>
concept is_produces_group = std::derived_from<T, ProducesGroup>;
/// Helper template for table transformations
template <soa::is_metadata M, soa::TableRef Ref>
struct TableTransform {
using metadata = M;
constexpr static auto sources = M::sources;
template <soa::TableRef R>
static auto base_spec()
{
return soa::tableRef2InputSpec<R>();
}
static auto base_specs()
{
return []<size_t... Is>(std::index_sequence<Is...>) {
return std::array{base_spec<sources[Is]>()...};
}(std::make_index_sequence<sources.size()>{});
}
static constexpr auto spec()
{
return soa::tableRef2OutputSpec<Ref>();
}
static constexpr auto output()
{
return soa::tableRef2Output<Ref>();
}
static constexpr auto ref()
{
return soa::tableRef2OutputRef<Ref>();
}
};
/// This helper struct allows you to declare extended tables which should be
/// created by the task (as opposed to those pre-defined by data model)
template <typename T>
concept is_spawnable = soa::has_metadata<aod::MetadataTrait<o2::aod::Hash<T::ref.desc_hash>>> && soa::has_extension<typename aod::MetadataTrait<o2::aod::Hash<T::ref.desc_hash>>::metadata>;
template <typename T>
concept is_dynamically_spawnable = soa::has_metadata<aod::MetadataTrait<o2::aod::Hash<T::ref.desc_hash>>> && soa::has_configurable_extension<typename aod::MetadataTrait<o2::aod::Hash<T::ref.desc_hash>>::metadata>;
template <is_spawnable T>
constexpr auto transformBase()
{
using metadata = typename aod::MetadataTrait<o2::aod::Hash<T::ref.desc_hash>>::metadata;
return TableTransform<metadata, metadata::extension_table_t::ref>{};
}
template <is_spawnable T>
struct Spawns : decltype(transformBase<T>()) {
using spawnable_t = T;
using metadata = decltype(transformBase<T>())::metadata;
using extension_t = typename metadata::extension_table_t;
using expression_pack_t = typename metadata::expression_pack_t;
static constexpr size_t N = framework::pack_size(expression_pack_t{});
typename T::table_t* operator->()
{
return table.get();
}
typename T::table_t const& operator*() const
{
return *table;
}
auto asArrowTable()
{
return extension->asArrowTable();
}
std::shared_ptr<typename T::table_t> table = nullptr;
std::shared_ptr<extension_t> extension = nullptr;
std::array<o2::framework::expressions::Projector, N> projectors = []<typename... C>(framework::pack<C...>) -> std::array<expressions::Projector, sizeof...(C)>
{
return {{std::move(C::Projector())...}};
}
(expression_pack_t{});
std::shared_ptr<gandiva::Projector> projector = nullptr;
std::shared_ptr<arrow::Schema> schema = []() {
auto s = std::make_shared<arrow::Schema>(o2::soa::createFieldsFromColumns(expression_pack_t{}));
s->WithMetadata(std::make_shared<arrow::KeyValueMetadata>(std::vector{std::string{"label"}}, std::vector{std::string{o2::aod::label<T::ref>()}}));
return s;
}();
};
template <typename T>
concept is_spawns = requires(T t) {
typename T::metadata;
typename T::expression_pack_t;
requires std::same_as<decltype(t.projector), std::shared_ptr<gandiva::Projector>>;
};
/// This helper struct allows you to declare extended tables with dynamically-supplied
/// expressions to be created by the task
/// The actual expressions have to be set in init() for the configurable expression
/// columns, used to define the table
template <is_dynamically_spawnable T, bool DELAYED = false>
struct Defines : decltype(transformBase<T>()) {
static constexpr bool delayed = DELAYED;
using spawnable_t = T;
using metadata = decltype(transformBase<T>())::metadata;
using extension_t = typename metadata::extension_table_t;
using placeholders_pack_t = typename metadata::placeholders_pack_t;
static constexpr size_t N = framework::pack_size(placeholders_pack_t{});
typename T::table_t* operator->()
{
return table.get();
}
typename T::table_t const& operator*() const
{
return *table;
}
auto asArrowTable()
{
return extension->asArrowTable();
}
std::shared_ptr<typename T::table_t> table = nullptr;
std::shared_ptr<extension_t> extension = nullptr;
std::array<o2::framework::expressions::Projector, N> projectors;
std::shared_ptr<gandiva::Projector> projector = nullptr;
std::shared_ptr<arrow::Schema> schema = []() {
auto s = std::make_shared<arrow::Schema>(o2::soa::createFieldsFromColumns(placeholders_pack_t{}));
s->WithMetadata(std::make_shared<arrow::KeyValueMetadata>(std::vector{std::string{"label"}}, std::vector{std::string{o2::aod::label<T::ref>()}}));
return s;
}();
std::shared_ptr<arrow::Schema> inputSchema = nullptr;
bool needRecompilation = false;
void recompile()
{
projector = framework::expressions::createProjectorHelper(N, projectors.data(), inputSchema, schema->fields());
}
};
template <is_dynamically_spawnable T>
using DefinesDelayed = Defines<T, true>;
template <typename T>
concept is_defines = requires(T t) {
typename T::metadata;
typename T::placeholders_pack_t;
requires std::same_as<decltype(t.projector), std::shared_ptr<gandiva::Projector>>;
requires std::same_as<decltype(t.needRecompilation), bool>;
&T::recompile;
};
/// Policy to control index building
/// Exclusive index: each entry in a row has a valid index
/// Sparse index: values in a row can be (-1), index table is isomorphic (joinable)
/// to T1
struct Exclusive {
};
struct Sparse {
};
/// This helper struct allows you to declare index tables to be created in a task
template <soa::is_index_table T>
constexpr auto transformBase()
{
using metadata = typename aod::MetadataTrait<o2::aod::Hash<T::ref.desc_hash>>::metadata;
return TableTransform<metadata, T::ref>{};
}
template <soa::is_index_table T>
struct Builds : decltype(transformBase<T>()) {
using buildable_t = T;
using metadata = decltype(transformBase<T>())::metadata;
using Key = metadata::Key;
using H = typename T::first_t;
using Ts = typename T::rest_t;
using index_pack_t = metadata::index_pack_t;
std::shared_ptr<arrow::Schema> outputSchema = []() { return std::make_shared<arrow::Schema>(soa::createFieldsFromColumns(index_pack_t{}))->WithMetadata(std::make_shared<arrow::KeyValueMetadata>(std::vector{std::string{"label"}}, std::vector{std::string{o2::aod::label<T::ref>()}})); }();
std::vector<soa::IndexRecord> map = soa::getIndexMapping<metadata>();
std::vector<framework::IndexColumnBuilder> builders;
T* operator->()
{
return table.get();
}
T const& operator*() const
{
return *table;
}
auto asArrowTable()
{
return table->asArrowTable();
}
std::shared_ptr<T> table = nullptr;
constexpr auto pack()
{
return index_pack_t{};
}
auto build(std::vector<std::shared_ptr<arrow::Table>>&& tables)
{
this->table = std::make_shared<T>(soa::IndexBuilder::materialize(builders, std::forward<std::vector<std::shared_ptr<arrow::Table>>>(tables), map, outputSchema, metadata::exclusive));
return (this->table != nullptr);
}
};
template <typename T>
concept is_builds = requires(T t) {
typename T::metadata;
typename T::Key;
requires std::same_as<decltype(t.map), std::vector<soa::IndexRecord>>;
};
/// This helper class allows you to declare things which will be created by a
/// given analysis task. Currently wrapped objects are limited to be TNamed
/// descendants. Objects will be written to a ROOT file at the end of the
/// workflow, in directories, corresponding to the task they were declared in.
/// Each object has associated handling policy, which is used by the framework
/// to determine the target file, e.g. analysis result, QA or control histogram,
/// etc.
template <typename T>
struct OutputObj {
using obj_t = T;
OutputObj(T&& t, OutputObjHandlingPolicy policy_ = OutputObjHandlingPolicy::AnalysisObject, OutputObjSourceType sourceType_ = OutputObjSourceType::OutputObjSource)
: object(std::make_shared<T>(t)),
label(t.GetName()),
policy{policy_},
sourceType{sourceType_},
mTaskHash{0}
{
}
OutputObj(std::string const& label_, OutputObjHandlingPolicy policy_ = OutputObjHandlingPolicy::AnalysisObject, OutputObjSourceType sourceType_ = OutputObjSourceType::OutputObjSource)
: object(nullptr),
label(label_),
policy{policy_},
sourceType{sourceType_},
mTaskHash{0}
{
}
void setObject(T const& t)
{
object = std::make_shared<T>(t);
object->SetName(label.c_str());
}
void setObject(T&& t)
{
object = std::make_shared<T>(t);
object->SetName(label.c_str());
}
void setObject(T* t)
{
object.reset(t);
object->SetName(label.c_str());
}
void setObject(std::shared_ptr<T> t)
{
object = t;
object->SetName(label.c_str());
}
void setHash(uint32_t hash)
{
mTaskHash = hash;
}
/// @return the associated OutputSpec
OutputSpec const spec()
{
header::DataDescription desc{};
auto lhash = runtime_hash(label.c_str());
std::memset(desc.str, '_', 16);
std::stringstream s;
s << std::hex << lhash;
s << std::hex << mTaskHash;
s << std::hex << reinterpret_cast<uint64_t>(this);
std::memcpy(desc.str, s.str().c_str(), 12);
return OutputSpec{OutputLabel{label}, "ATSK", desc, 0, Lifetime::QA};
}
T* operator->()
{
return object.get();
}
T& operator*()
{
return *object.get();
}
OutputRef ref(uint16_t index, uint16_t max)
{
return OutputRef{std::string{label}, 0,
o2::header::Stack{OutputObjHeader{policy, sourceType, mTaskHash, index, max}}};
}
std::shared_ptr<T> object;
std::string label;
OutputObjHandlingPolicy policy;
OutputObjSourceType sourceType;
uint32_t mTaskHash;
};
template <typename T>
concept is_outputobj = requires(T t) {
&T::setHash;
&T::spec;
&T::ref;
requires std::same_as<decltype(t.operator->()), typename T::obj_t*>;
requires std::same_as<decltype(t.object), std::shared_ptr<typename T::obj_t>>;
};
/// This helper allows you to fetch a Sevice from the context or
/// by using some singleton. This hopefully will hide the Singleton and
/// We will be able to retrieve it in a more thread safe manner later on.
template <typename T>
struct Service {
using service_t = T;
T* service;
decltype(auto) operator->() const
{
if constexpr (base_of_template<LoadableServicePlugin, T>) {
return service->get();
} else {
return service;
}
}
};
template <typename T>
concept is_service = requires(T t) {
requires std::same_as<decltype(t.service), typename T::service_t*>;
&T::operator->;
};
auto getTableFromFilter(soa::is_filtered_table auto const& table, soa::SelectionVector&& selection)
{
return std::make_unique<o2::soa::Filtered<std::decay_t<decltype(table)>>>(std::vector{table}, std::forward<soa::SelectionVector>(selection));
}
auto getTableFromFilter(soa::is_not_filtered_table auto const& table, soa::SelectionVector&& selection)
{
return std::make_unique<o2::soa::Filtered<std::decay_t<decltype(table)>>>(std::vector{table.asArrowTable()}, std::forward<soa::SelectionVector>(selection));
}
void initializePartitionCaches(std::set<uint32_t> const& hashes, std::shared_ptr<arrow::Schema> const& schema, expressions::Filter const& filter, gandiva::NodePtr& tree, gandiva::FilterPtr& gfilter);
template <typename T>
struct Partition {
using content_t = T;
Partition(expressions::Node&& filter_) : filter{std::forward<expressions::Node>(filter_)}
{
}
Partition(expressions::Node&& filter_, T const& table)
: filter{std::forward<expressions::Node>(filter_)}
{
setTable(table);
}
void intializeCaches(std::set<uint32_t> const& hashes, std::shared_ptr<arrow::Schema> const& schema)
{
initializePartitionCaches(hashes, schema, filter, tree, gfilter);
}
void bindTable(T const& table)
{
intializeCaches(T::table_t::hashes(), table.asArrowTable()->schema());
if (dataframeChanged) {
mFiltered = getTableFromFilter(table, soa::selectionToVector(framework::expressions::createSelection(table.asArrowTable(), gfilter)));
dataframeChanged = false;
}
}
template <typename... Ts>
void bindExternalIndices(Ts*... tables)
{
if (mFiltered != nullptr) {
mFiltered->bindExternalIndices(tables...);
}
}
template <typename E>
void bindInternalIndicesTo(E* ptr)
{
if (mFiltered != nullptr) {
mFiltered->bindInternalIndicesTo(ptr);
}
}
void updatePlaceholders(InitContext& context)
{
expressions::updatePlaceholders(filter, context);
}
[[nodiscard]] std::shared_ptr<arrow::Table> asArrowTable() const
{
return mFiltered->asArrowTable();
}
o2::soa::Filtered<T>* operator->()
{
return mFiltered.get();
}
template <typename T1>
[[nodiscard]] auto rawSliceBy(o2::framework::Preslice<T1> const& container, int value) const
{
return mFiltered->rawSliceBy(container, value);
}
[[nodiscard]] auto sliceByCached(framework::expressions::BindingNode const& node, int value, o2::framework::SliceCache& cache) const
{
return mFiltered->sliceByCached(node, value, cache);
}
[[nodiscard]] auto sliceByCachedUnsorted(framework::expressions::BindingNode const& node, int value, o2::framework::SliceCache& cache) const
{
return mFiltered->sliceByCachedUnsorted(node, value, cache);
}
template <typename T1, typename Policy, bool OPT>
[[nodiscard]] auto sliceBy(o2::framework::PresliceBase<T1, Policy, OPT> const& container, int value) const
{
return mFiltered->sliceBy(container, value);
}
expressions::Filter filter;
std::unique_ptr<o2::soa::Filtered<T>> mFiltered = nullptr;
gandiva::NodePtr tree = nullptr;
gandiva::FilterPtr gfilter = nullptr;
bool dataframeChanged = true;
using iterator = typename o2::soa::Filtered<T>::iterator;
using const_iterator = typename o2::soa::Filtered<T>::const_iterator;
using filtered_iterator = typename o2::soa::Filtered<T>::iterator;
using filtered_const_iterator = typename o2::soa::Filtered<T>::const_iterator;
inline filtered_iterator begin()
{
return mFiltered->begin();
}
inline o2::soa::RowViewSentinel end()
{
return mFiltered->end();
}