|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | +#include "Framework/CommonDataProcessors.h" |
| 11 | + |
| 12 | +#include "Framework/AlgorithmSpec.h" |
| 13 | +#include "Framework/DataProcessingHeader.h" |
| 14 | +#include "Framework/DataDescriptorQueryBuilder.h" |
| 15 | +#include "Framework/DataDescriptorMatcher.h" |
| 16 | +#include "Framework/DataProcessorSpec.h" |
| 17 | +#include "Framework/InitContext.h" |
| 18 | +#include "Framework/InitContext.h" |
| 19 | +#include "Framework/InputSpec.h" |
| 20 | +#include "Framework/OutputSpec.h" |
| 21 | +#include "Framework/Variant.h" |
| 22 | + |
| 23 | +#include <exception> |
| 24 | +#include <fstream> |
| 25 | +#include <functional> |
| 26 | +#include <memory> |
| 27 | +#include <string> |
| 28 | + |
| 29 | +namespace o2 |
| 30 | +{ |
| 31 | +namespace framework |
| 32 | +{ |
| 33 | + |
| 34 | +DataProcessorSpec CommonDataProcessors::getGlobalFileSink(std::vector<InputSpec> const& danglingOutputInputs, |
| 35 | + std::vector<InputSpec>& unmatched) |
| 36 | +{ |
| 37 | + auto writerFunction = [danglingOutputInputs](InitContext& ic) -> std::function<void(ProcessingContext&)> { |
| 38 | + auto filename = ic.options().get<std::string>("outfile"); |
| 39 | + auto keepString = ic.options().get<std::string>("keep"); |
| 40 | + |
| 41 | + if (filename.empty()) { |
| 42 | + throw std::runtime_error("output file missing"); |
| 43 | + } |
| 44 | + |
| 45 | + bool hasOutputsToWrite = false; |
| 46 | + auto outputMatcher = DataDescriptorQueryBuilder::buildFromKeepConfig(keepString); |
| 47 | + for (auto& spec : danglingOutputInputs) { |
| 48 | + if (outputMatcher->match(spec)) { |
| 49 | + hasOutputsToWrite = true; |
| 50 | + } |
| 51 | + } |
| 52 | + if (hasOutputsToWrite == false) { |
| 53 | + return std::move([](ProcessingContext& pc) mutable -> void { |
| 54 | + static bool once = false; |
| 55 | + /// We do it like this until we can use the interruptible sleep |
| 56 | + /// provided by recent FairMQ releases. |
| 57 | + if (!once) { |
| 58 | + LOG(INFO) << "No dangling output to be dumped."; |
| 59 | + once = true; |
| 60 | + } |
| 61 | + sleep(1); |
| 62 | + }); |
| 63 | + } |
| 64 | + auto output = std::make_shared<std::ofstream>(filename.c_str(), std::ios_base::binary); |
| 65 | + return std::move([ output, matcher = outputMatcher ](ProcessingContext & pc) mutable->void { |
| 66 | + LOG(INFO) << "processing data set with " << pc.inputs().size() << " entries"; |
| 67 | + for (const auto& entry : pc.inputs()) { |
| 68 | + LOG(INFO) << " " << *(entry.spec); |
| 69 | + auto header = DataRefUtils::getHeader<header::DataHeader*>(entry); |
| 70 | + auto dataProcessingHeader = DataRefUtils::getHeader<DataProcessingHeader*>(entry); |
| 71 | + if (matcher->match(*header) == false) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + output->write(reinterpret_cast<char const*>(header), sizeof(header::DataHeader)); |
| 75 | + output->write(reinterpret_cast<char const*>(dataProcessingHeader), sizeof(DataProcessingHeader)); |
| 76 | + output->write(entry.payload, o2::framework::DataRefUtils::getPayloadSize(entry)); |
| 77 | + LOG(INFO) << "wrote data, size " << o2::framework::DataRefUtils::getPayloadSize(entry); |
| 78 | + } |
| 79 | + }); |
| 80 | + }; |
| 81 | + |
| 82 | + std::vector<InputSpec> validBinaryInputs; |
| 83 | + auto onlyTimeframe = [](InputSpec const& input) { |
| 84 | + return input.lifetime == Lifetime::Timeframe; |
| 85 | + }; |
| 86 | + |
| 87 | + auto noTimeframe = [](InputSpec const& input) { |
| 88 | + return input.lifetime != Lifetime::Timeframe; |
| 89 | + }; |
| 90 | + |
| 91 | + std::copy_if(danglingOutputInputs.begin(), danglingOutputInputs.end(), |
| 92 | + std::back_inserter(validBinaryInputs), onlyTimeframe); |
| 93 | + std::copy_if(danglingOutputInputs.begin(), danglingOutputInputs.end(), |
| 94 | + std::back_inserter(unmatched), noTimeframe); |
| 95 | + |
| 96 | + DataProcessorSpec spec{ |
| 97 | + "dpl-global-binary-file-sink", |
| 98 | + validBinaryInputs, |
| 99 | + Outputs{}, |
| 100 | + AlgorithmSpec(writerFunction), |
| 101 | + { { "outfile", VariantType::String, "dpl-out.bin", { "Name of the output file" } }, |
| 102 | + { "keep", VariantType::String, "", { "Comma separated list of ORIGIN/DESCRIPTION/SUBSPECIFICATION to save in outfile" } } } |
| 103 | + }; |
| 104 | + |
| 105 | + return spec; |
| 106 | +} |
| 107 | + |
| 108 | +DataProcessorSpec CommonDataProcessors::getDummySink(std::vector<InputSpec> const& danglingOutputInputs) |
| 109 | +{ |
| 110 | + return DataProcessorSpec{ |
| 111 | + "dpl-dummy-sink", |
| 112 | + danglingOutputInputs, |
| 113 | + Outputs{}, |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace framework |
| 118 | +} // namespace o2 |
0 commit comments