|
| 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 | +/// \file A unit test of mergers.cxx |
| 13 | +/// \brief |
| 14 | +/// |
| 15 | +/// \author Michal Tichak, michal.tichak@cern.ch |
| 16 | + |
| 17 | +#include <chrono> |
| 18 | +#include <cstdlib> |
| 19 | +#include <sstream> |
| 20 | +#include <thread> |
| 21 | +#include "Mergers/MergerBuilder.h" |
| 22 | +#include <Framework/CompletionPolicy.h> |
| 23 | +#include <Framework/CompletionPolicyHelpers.h> |
| 24 | +#include <TH1F.h> |
| 25 | +#include <fairlogger/Logger.h> |
| 26 | +#include "Framework/ControlService.h" |
| 27 | + |
| 28 | +using namespace o2::framework; |
| 29 | +using namespace o2::mergers; |
| 30 | + |
| 31 | +void customize(std::vector<CompletionPolicy>& policies) |
| 32 | +{ |
| 33 | + MergerBuilder::customizeInfrastructure(policies); |
| 34 | +} |
| 35 | + |
| 36 | +#include "Framework/runDataProcessing.h" |
| 37 | +#include "Mergers/MergerInfrastructureBuilder.h" |
| 38 | +#include <Framework/CompletionPolicy.h> |
| 39 | + |
| 40 | +using SubSpecificationType = o2::header::DataHeader::SubSpecificationType; |
| 41 | +constexpr size_t producersAmount = 2; |
| 42 | + |
| 43 | +void print_histo(const TH1F& h) |
| 44 | +{ |
| 45 | + std::stringstream ss; |
| 46 | + for (size_t i = 0; i != h.GetSize(); ++i) { |
| 47 | + ss << h[i] << " "; |
| 48 | + } |
| 49 | + LOG(info) << ss.str(); |
| 50 | +} |
| 51 | + |
| 52 | +template <typename T> |
| 53 | +void print_histo(const T& histo_ptr) |
| 54 | +{ |
| 55 | + print_histo(*histo_ptr.get()); |
| 56 | +} |
| 57 | + |
| 58 | +constexpr size_t binsCount = 10; |
| 59 | +constexpr double min = 0; |
| 60 | +constexpr double max = 10; |
| 61 | + |
| 62 | +constexpr std::array<float, 12> expectedVector{ |
| 63 | + 1., |
| 64 | + 1., |
| 65 | + 1., |
| 66 | + 0., |
| 67 | + 0., |
| 68 | + 0., |
| 69 | + 2., |
| 70 | + 0., |
| 71 | + 0., |
| 72 | + 0., |
| 73 | + 0., |
| 74 | + 0., |
| 75 | +}; |
| 76 | + |
| 77 | +bool compareHistoToExpected(const TH1F& histo) |
| 78 | +{ |
| 79 | + return gsl::span{expectedVector} == gsl::span(histo.GetArray(), histo.GetSize()); |
| 80 | +} |
| 81 | + |
| 82 | +WorkflowSpec defineDataProcessing(ConfigContext const&) |
| 83 | +{ |
| 84 | + WorkflowSpec specs; |
| 85 | + |
| 86 | + Inputs mergersInputs; |
| 87 | + for (size_t p = 0; p < producersAmount; p++) { |
| 88 | + mergersInputs.push_back({"mo", "TST", "HISTO", static_cast<SubSpecificationType>(p + 1), Lifetime::Sporadic}); |
| 89 | + DataProcessorSpec producer{ |
| 90 | + "producer-histo" + std::to_string(p), |
| 91 | + Inputs{}, |
| 92 | + Outputs{{{"mo"}, "TST", "HISTO", static_cast<SubSpecificationType>(p + 1), Lifetime::Sporadic}}, |
| 93 | + AlgorithmSpec{static_cast<AlgorithmSpec::ProcessCallback>([p, dataSent = false](ProcessingContext& processingContext) mutable { |
| 94 | + if (dataSent) { |
| 95 | + std::this_thread::sleep_for(std::chrono::milliseconds{100}); |
| 96 | + return; |
| 97 | + } |
| 98 | + auto subspec = static_cast<SubSpecificationType>(p + 1); |
| 99 | + TH1F& histo = processingContext.outputs().make<TH1F>(Output{"TST", "HISTO", subspec}, "histo", "histo", binsCount, min, max); |
| 100 | + histo.Fill(5); |
| 101 | + histo.Fill(p); |
| 102 | + print_histo(histo); |
| 103 | + dataSent = true; |
| 104 | + })}}; |
| 105 | + specs.push_back(producer); |
| 106 | + } |
| 107 | + |
| 108 | + MergerInfrastructureBuilder mergersBuilder; |
| 109 | + mergersBuilder.setInfrastructureName("histos"); |
| 110 | + mergersBuilder.setInputSpecs(mergersInputs); |
| 111 | + mergersBuilder.setOutputSpec({{"main"}, "TST", "HISTO", 0}); |
| 112 | + MergerConfig config; |
| 113 | + config.inputObjectTimespan = {InputObjectsTimespan::FullHistory}; |
| 114 | + std::vector<std::pair<size_t, size_t>> param = {{5, 1}}; |
| 115 | + config.publicationDecision = {PublicationDecision::EachNSeconds, param}; |
| 116 | + config.mergedObjectTimespan = {MergedObjectTimespan::FullHistory}; |
| 117 | + config.topologySize = {TopologySize::NumberOfLayers, 2}; |
| 118 | + mergersBuilder.setConfig(config); |
| 119 | + |
| 120 | + mergersBuilder.generateInfrastructure(specs); |
| 121 | + |
| 122 | + DataProcessorSpec printer{ |
| 123 | + "data-checker", |
| 124 | + Inputs{{"histo", "TST", "HISTO", 0, Lifetime::Sporadic}}, |
| 125 | + Outputs{}, |
| 126 | + AlgorithmSpec{ |
| 127 | + AlgorithmSpec::InitCallback{[](InitContext&) { |
| 128 | + return AlgorithmSpec::ProcessCallback{[](ProcessingContext& processingContext) mutable { |
| 129 | + auto histo = processingContext.inputs().get<TH1F*>("histo"); |
| 130 | + print_histo(*histo); |
| 131 | + processingContext.services().get<ControlService>().readyToQuit(QuitRequest::All); |
| 132 | + if (!compareHistoToExpected(*histo.get())) { |
| 133 | + LOG(fatal) << "received incorrect data"; |
| 134 | + } |
| 135 | + }}; |
| 136 | + }}}}; |
| 137 | + specs.push_back(printer); |
| 138 | + return specs; |
| 139 | +} |
0 commit comments