-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy pathvectorTopologyCommon.h
More file actions
166 lines (140 loc) · 6.85 KB
/
Copy pathvectorTopologyCommon.h
File metadata and controls
166 lines (140 loc) · 6.85 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
// 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 ALICEO2_VECTORTOPOLOGYCOMMON_H_
#define ALICEO2_VECTORTOPOLOGYCOMMON_H_
#include <array>
#include <chrono>
#include <cstddef>
#include <cstdlib>
#include <fairlogger/Logger.h>
#include <thread>
#include <TH1F.h>
#include <Framework/CompletionPolicy.h>
#include <Framework/CompletionPolicy.h>
#include <Framework/CompletionPolicy.h>
#include <Framework/CompletionPolicyHelpers.h>
#include <Framework/ControlService.h>
#include <Mergers/MergerInfrastructureBuilder.h>
#include <Mergers/MergerBuilder.h>
#include <Mergers/ObjectStore.h>
#include "common.h"
void customize(std::vector<o2::framework::CompletionPolicy>& policies)
{
o2::mergers::MergerBuilder::customizeInfrastructure(policies);
}
// keep this include here
#include <Framework/runDataProcessing.h>
namespace o2::framework
{
using SubSpecificationType = header::DataHeader::SubSpecificationType;
template <size_t Size>
using ExpectedType = std::array<float, Size>;
template <typename Deleter, size_t Size>
bool operator==(const std::unique_ptr<const std::vector<TObject*>, Deleter>& vectorOfHistos, std::vector<ExpectedType<Size>>& expected)
{
return std::equal(vectorOfHistos->begin(), vectorOfHistos->end(), expected.begin(), expected.end(),
[](TObject* const object, const ExpectedType<Size>& array) {
const auto& histo = *dynamic_cast<TH1F const*>(object);
return std::equal(array.begin(), array.end(), histo.GetArray(), histo.GetArray() + histo.GetSize());
});
}
template <size_t HistoSize>
class VectorMergerTestGenerator
{
static constexpr const char origin[] = {"TST"};
static constexpr const char description[] = {"VEC"};
public:
VectorMergerTestGenerator(std::vector<std::array<float, HistoSize>>&& expectedResult, size_t histoBinsCount, double histoMin, double histoMax)
: mExpectedResult{expectedResult}, mHistoBinsCount{histoBinsCount}, mHistoMin{histoMin}, mHistoMax{histoMax}
{
}
Inputs generateHistoProducers(WorkflowSpec& specs, size_t numberOfProducers)
{
Inputs mergersInputs;
for (size_t producerIdx = 1; producerIdx < numberOfProducers + 1; ++producerIdx) {
mergersInputs.push_back({"mo", origin, description, static_cast<SubSpecificationType>(producerIdx), Lifetime::Sporadic});
DataProcessorSpec producer{
"producer-vec" + std::to_string(producerIdx),
Inputs{},
Outputs{{{"mo"}, origin, description, static_cast<SubSpecificationType>(producerIdx), Lifetime::Sporadic}},
AlgorithmSpec{static_cast<AlgorithmSpec::ProcessCallback>([producerIdx, numberOfProducers, binsCount = mHistoBinsCount, histoMin = mHistoMin, histoMax = mHistoMax](ProcessingContext& processingContext) mutable {
const auto subspec = static_cast<SubSpecificationType>(producerIdx);
auto vectorOfHistos = std::make_unique<mergers::VectorOfRawTObjects>(2);
int i = 0;
for (auto& hist_ptr : *vectorOfHistos) {
const auto histoname = std::string{"histo"} + std::to_string(++i);
auto* hist = new TH1F(histoname.c_str(), histoname.c_str(), binsCount, histoMin, histoMax);
hist->Fill(producerIdx);
hist->Fill(5);
hist_ptr = hist;
}
processingContext.outputs().snapshot(OutputRef{"mo", subspec}, *vectorOfHistos);
for_each(vectorOfHistos->begin(), vectorOfHistos->end(), [](auto& histoPtr) { delete histoPtr; });
processingContext.services().get<ControlService>().readyToQuit(QuitRequest::Me);
})}};
specs.push_back(producer);
}
return mergersInputs;
}
void generateMergers(WorkflowSpec& specs, const Inputs& mergersInputs, mergers::InputObjectsTimespan mergerType)
{
using namespace mergers;
MergerInfrastructureBuilder mergersBuilder;
mergersBuilder.setInfrastructureName("vec");
mergersBuilder.setInputSpecs(mergersInputs);
mergersBuilder.setOutputSpec({{"main"}, origin, description, 0});
MergerConfig config;
config.inputObjectTimespan = {mergerType};
std::vector<std::pair<size_t, size_t>> param = {{5, 1}};
config.publicationDecision = {PublicationDecision::EachNSeconds, param};
config.mergedObjectTimespan = {MergedObjectTimespan::FullHistory};
config.topologySize = {TopologySize::NumberOfLayers, 2};
mergersBuilder.setConfig(config);
mergersBuilder.generateInfrastructure(specs);
}
void generateChecker(WorkflowSpec& specs)
{
specs.push_back(DataProcessorSpec{
"data-checker",
Inputs{{"vec", origin, description, 0, Lifetime::Sporadic}},
Outputs{},
AlgorithmSpec{
AlgorithmSpec::InitCallback{[expectedResult = mExpectedResult](InitContext& initContext) {
auto success = std::make_shared<bool>(false);
mergers::test::registerCallbacksForTestFailure(initContext.services().get<CallbackService>(), success);
// reason for this crude retry is that multiple layers are not synchronized between each other and publish on their own timers.
// number of retries was chosen a bit randomly, as we need to have at least 2 runs through this function because of publish
// timers inside of the mergers
return AlgorithmSpec::ProcessCallback{[expectedResult, retryNumber = 1, retries = 5, success](ProcessingContext& processingContext) mutable {
const auto vectorOfHistos = processingContext.inputs().get<std::vector<TObject*>*>("vec");
LOG(info) << "RETRY: " << retryNumber << ": comparing: " << std::to_string(vectorOfHistos) << " to the expected: " << std::to_string(expectedResult);
if (vectorOfHistos == expectedResult) {
LOG(info) << "Received the expected object, test successful";
*success = true;
processingContext.services().get<ControlService>().readyToQuit(QuitRequest::All);
return;
}
if (retryNumber++ > retries) {
processingContext.services().get<ControlService>().readyToQuit(QuitRequest::All);
LOG(fatal) << "received wrong data: " << std::to_string(vectorOfHistos) << ", expected: " << std::to_string(expectedResult);
return;
}
}};
}}}});
}
private:
std::vector<ExpectedType<HistoSize>> mExpectedResult;
size_t mHistoBinsCount;
double mHistoMin;
double mHistoMax;
};
} // namespace o2::framework
#endif