forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergersTopologyExample.cxx
More file actions
253 lines (219 loc) · 9.32 KB
/
mergersTopologyExample.cxx
File metadata and controls
253 lines (219 loc) · 9.32 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
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// 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.
///
/// \file mergersTopologyExample.cxx
/// \author Piotr Konopka
///
/// \brief This is a DPL workflow to see Mergers in action
#include "Mergers/MergerBuilder.h"
#include <Framework/CompletionPolicy.h>
#include <TH1F.h>
#include <memory>
#include <random>
using namespace o2::framework;
using namespace o2::experimental::mergers;
void customize(std::vector<CompletionPolicy>& policies)
{
MergerBuilder::customizeInfrastructure(policies);
}
#include <Framework/runDataProcessing.h>
#include <fairmq/FairMQLogger.h>
#include <Mergers/MergeInterfaceOverrideExample.h>
#include "Mergers/MergerInfrastructureBuilder.h"
using namespace std::chrono;
// clang-format off
WorkflowSpec defineDataProcessing(ConfigContext const&)
{
WorkflowSpec specs;
// one 1D histo, binwise
{
// WorkflowSpec specs; // enable comment to disable the workflow
size_t producersAmount = 8;
Inputs mergersInputs;
for (size_t p = 0; p < producersAmount; p++) {
mergersInputs.push_back({ "mo", "TST",
"HISTO", static_cast<o2::header::DataHeader::SubSpecificationType>(p + 1),
Lifetime::Timeframe });
DataProcessorSpec producer{
"producer-histo" + std::to_string(p), Inputs{},
Outputs{ { { "mo" },
"TST",
"HISTO",
static_cast<o2::header::DataHeader::SubSpecificationType>(p + 1),
Lifetime::Timeframe } },
AlgorithmSpec{(AlgorithmSpec::ProcessCallback)
[ p, producersAmount, srand(p) ](ProcessingContext & processingContext) mutable {
// usleep(100000 + (rand() % 10000) - 5000);
usleep(100000);
static int i = 0;
if (i++ >= 1000) { return; }
TH1F* histo = new TH1F("gauss", "gauss", producersAmount, 0, 1);
histo->Fill(p / (double) producersAmount);
processingContext.outputs().adopt(
Output{ "TST", "HISTO", static_cast<o2::header::DataHeader::SubSpecificationType>(p + 1) }, histo);
}
}
};
specs.push_back(producer);
}
MergerInfrastructureBuilder mergersBuilder;
mergersBuilder.setInfrastructureName("histos");
mergersBuilder.setInputSpecs(mergersInputs);
mergersBuilder.setOutputSpec({{ "main" }, "TST", "HISTO", 0 });
MergerConfig config;
config.ownershipMode = { OwnershipMode::Integral };
config.publicationDecision = { PublicationDecision::EachNSeconds, 5 };
config.mergingTime = { MergingTime::BeforePublication };
config.timespan = { Timespan::FullHistory };
config.topologySize = { TopologySize::NumberOfLayers, 2 };
mergersBuilder.setConfig(config);
mergersBuilder.generateInfrastructure(specs);
DataProcessorSpec printer{
"printer-bins",
Inputs{
{ "histo", "TST", "HISTO", 0 }
},
Outputs{},
AlgorithmSpec{
(AlgorithmSpec::InitCallback) [](InitContext&) {
return (AlgorithmSpec::ProcessCallback) [](ProcessingContext& processingContext) mutable {
// LOG(INFO) << "printer invoked";
auto histo = processingContext.inputs().get<TH1F*>("histo");
std::string bins = "BINS:";
for (int i = 1; i <= histo->GetNbinsX(); i++) {
bins += " " + std::to_string((int) histo->GetBinContent(i));
}
LOG(INFO) << bins;
};
}
}
};
specs.push_back(printer);
}
// concatenation test
{
// WorkflowSpec specs; // enable comment to disable the workflow
size_t producersAmount = 4;
Inputs mergersInputs;
for (size_t p = 0; p < producersAmount; p++) {
mergersInputs.push_back({ "mo", "TST",
"STRING", static_cast<o2::header::DataHeader::SubSpecificationType>(p + 1),
Lifetime::Timeframe });
DataProcessorSpec producer{ "producer-str" + std::to_string(p), Inputs{},
Outputs{ { { "mo" },
"TST",
"STRING",
static_cast<o2::header::DataHeader::SubSpecificationType>(p + 1),
Lifetime::Timeframe } },
AlgorithmSpec{(AlgorithmSpec::ProcessCallback)
[p, producersAmount](ProcessingContext& processingContext) mutable {
usleep(1000000);
char str[2] = "a";
str[0] += p;
processingContext.outputs().adopt(
Output{ "TST", "STRING", static_cast<o2::header::DataHeader::SubSpecificationType>(p + 1) },
new TObjString(str));
} } };
specs.push_back(producer);
}
MergerInfrastructureBuilder mergersBuilder;
mergersBuilder.setInfrastructureName("strings");
mergersBuilder.setInputSpecs(mergersInputs);
mergersBuilder.setOutputSpec({{ "main" }, "TST", "STRING", 0 });
MergerConfig config;
config.ownershipMode = { OwnershipMode::Full };
config.mergingMode = { MergingMode::Concatenate };
config.publicationDecision = { PublicationDecision::WhenXInputsUpdated, 1 };
config.mergingTime = { MergingTime::BeforePublication };
config.topologySize = { TopologySize::NumberOfLayers, 2 };
mergersBuilder.setConfig(config);
mergersBuilder.generateInfrastructure(specs);
DataProcessorSpec printer{
"printer-collections",
Inputs{
{ "string", "TST", "STRING", 0 }
},
Outputs{},
AlgorithmSpec{
(AlgorithmSpec::InitCallback) [](InitContext&) {
return (AlgorithmSpec::ProcessCallback) [](ProcessingContext& processingContext) mutable {
// LOG(INFO) << "printer invoked";
auto stringArray = processingContext.inputs().get<TObjArray*>("string");
std::string full = "FULL: ";
for (const auto& obj : *stringArray) {
auto str = dynamic_cast<TObjString*>(obj);
full += str->GetString();
}
LOG(INFO) << full;
};
}
}
};
specs.push_back(printer);
}
// custom merge
{
// WorkflowSpec specs; // enable comment to disable the workflow
size_t producersAmount = 4;
Inputs mergersInputs;
for (size_t p = 0; p < producersAmount; p++) {
mergersInputs.push_back({ "mo", "TST",
"CUSTOM", static_cast<o2::header::DataHeader::SubSpecificationType>(p + 1),
Lifetime::Timeframe });
DataProcessorSpec producer{ "producer-custom" + std::to_string(p), Inputs{},
Outputs{ { { "mo" },
"TST",
"CUSTOM",
static_cast<o2::header::DataHeader::SubSpecificationType>(p + 1),
Lifetime::Timeframe } },
AlgorithmSpec{(AlgorithmSpec::ProcessCallback)[ p, producersAmount, srand(p) ](
ProcessingContext & processingContext) mutable { usleep(100000);
static int i = 0;
if (i++ >= 1000) { return; }
auto* histo = new MergeInterfaceOverrideExample(1);
processingContext.outputs().adopt(
OutputRef{ "mo", static_cast<o2::header::DataHeader::SubSpecificationType>(p + 1) }, histo);
}
}
};
specs.push_back(producer);
}
MergerInfrastructureBuilder mergersBuilder;
mergersBuilder.setInfrastructureName("custom");
mergersBuilder.setInputSpecs(mergersInputs);
mergersBuilder.setOutputSpec({{ "main" }, "TST", "CUSTOM", 0 });
MergerConfig config;
config.ownershipMode = { OwnershipMode::Integral };
config.publicationDecision = { PublicationDecision::EachNSeconds, 5 };
config.mergingTime = { MergingTime::BeforePublication };
config.timespan = { Timespan::FullHistory };
config.topologySize = { TopologySize::NumberOfLayers, 1 };
mergersBuilder.setConfig(config);
mergersBuilder.generateInfrastructure(specs);
DataProcessorSpec printer{
"printer-custom",
Inputs{
{ "custom", "TST", "CUSTOM", 0 }
},
Outputs{},
AlgorithmSpec{
(AlgorithmSpec::InitCallback) [](InitContext&) {
return (AlgorithmSpec::ProcessCallback) [](ProcessingContext& processingContext) mutable {
auto obj = processingContext.inputs().get<MergeInterfaceOverrideExample*>("custom");
LOG(INFO) << "SECRET:" << obj->getSecret();
};
}
}
};
specs.push_back(printer);
}
return specs;
}
// clang-format on