DQMStreamerOutputRepackerTest

Line Code
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
#include <iomanip>
#include <filesystem>
#include <memory>
#include <sstream>

#include <zlib.h>
#include <fmt/printf.h>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>

#include "EventFilter/Utilities/interface/FastMonitor.h"
#include "EventFilter/Utilities/interface/FastMonitoringService.h"
#include "EventFilter/Utilities/interface/FileIO.h"
#include "EventFilter/Utilities/interface/JSONSerializer.h"
#include "EventFilter/Utilities/interface/JsonMonitorable.h"
#include "FWCore/Framework/interface/LuminosityBlockForOutput.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/Utilities/interface/Adler32Calculator.h"
#include "IOPool/Streamer/interface/EventMessage.h"
#include "IOPool/Streamer/interface/EventMsgBuilder.h"
#include "IOPool/Streamer/interface/InitMessage.h"
#include "IOPool/Streamer/interface/InitMsgBuilder.h"
#include "IOPool/Streamer/interface/MsgTools.h"
#include "IOPool/Streamer/interface/StreamerOutputFile.h"
#include "IOPool/Streamer/interface/StreamerOutputModuleBase.h"

namespace dqmservices {
  using namespace edm::streamer;

  class DQMStreamerOutputRepackerTest : public StreamerOutputModuleBase {
  public:
    explicit DQMStreamerOutputRepackerTest(edm::ParameterSet const& ps);
    ~DQMStreamerOutputRepackerTest() override;
    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

  private:
    void start() override;
    void stop() override;
    void doOutputHeader(InitMsgBuilder const& init_message) override;
    void doOutputEvent(EventMsgBuilder const& msg) override;

    void beginLuminosityBlock(edm::LuminosityBlockForOutput const&) override {}
    void endLuminosityBlock(edm::LuminosityBlockForOutput const&) override {}

  private:
    void openFile_(uint32_t run, uint32_t lumi);
    void closeFile();

  private:
    std::string streamLabel_;
    std::string outputPath_;

    std::unique_ptr<uint8_t[]> init_message_cache_;
    std::unique_ptr<StreamerOutputFile> streamFile_;
    uint32_t streamRun_;
    uint32_t streamLumi_;

    uint32_t eventsProcessedFile_;
    uint32_t eventsProcessedTotal_;

    std::string currentFileBase_;
    std::string currentFilePath_;
    std::string currentJsonPath_;
  };  // end-of-class-def

  DQMStreamerOutputRepackerTest::DQMStreamerOutputRepackerTest(edm::ParameterSet const& ps)
      : edm::one::OutputModuleBase::OutputModuleBase(ps), StreamerOutputModuleBase(ps) {
    outputPath_ = ps.getUntrackedParameter<std::string>("outputPath");
    streamLabel_ = ps.getUntrackedParameter<std::string>("streamLabel");

    eventsProcessedTotal_ = 0;
    eventsProcessedFile_ = 0;
  }

  DQMStreamerOutputRepackerTest::~DQMStreamerOutputRepackerTest() {}

  void DQMStreamerOutputRepackerTest::openFile_(uint32_t run, uint32_t lumi) {
    if (streamFile_) {
      closeFile();
    }

    eventsProcessedFile_ = 0;

    currentFileBase_ = fmt::sprintf("run%06d_ls%04d_stream%s_local", run, lumi, streamLabel_);

    std::filesystem::path p = outputPath_;
    p /= fmt::sprintf("run%06d", run);

    std::filesystem::create_directories(p);

    currentFilePath_ = (p / currentFileBase_).string() + ".dat";
    currentJsonPath_ = (p / currentFileBase_).string() + ".jsn";

    edm::LogAbsolute("DQMStreamerOutputRepackerTest") << "Writing file: " << currentFilePath_;

    streamFile_.reset(new StreamerOutputFile(currentFilePath_));
    streamRun_ = run;
    streamLumi_ = lumi;

    if (init_message_cache_) {
      InitMsgView iview(init_message_cache_.get());
      streamFile_->write(iview);
    } else {
      edm::LogWarning("DQMStreamerOutputRepackerTest") << "Open file called before init message.";
    }
  }

  void DQMStreamerOutputRepackerTest::closeFile() {
    edm::LogAbsolute("DQMStreamerOutputRepackerTest") << "Writing json: " << currentJsonPath_;
    size_t fsize = std::filesystem::file_size(currentFilePath_);

    using namespace boost::property_tree;
    ptree pt;
    ptree data;

    ptree child1, child2, child3, child4, child5;
    child1.put("", eventsProcessedTotal_);  // Processed
    child2.put("", eventsProcessedFile_);   // Accepted
    child3.put("", 0);                      // Errors
    child4.put("", currentFileBase_ + ".dat");
    child5.put("", fsize);

    data.push_back(std::make_pair("", child1));
    data.push_back(std::make_pair("", child2));
    data.push_back(std::make_pair("", child3));
    data.push_back(std::make_pair("", child4));
    data.push_back(std::make_pair("", child5));

    pt.add_child("data", data);
    pt.put("definition", "");
    pt.put("source", "");

    std::string json_tmp = currentJsonPath_ + ".open";
    write_json(json_tmp, pt);
    ::rename(json_tmp.c_str(), currentJsonPath_.c_str());

    streamFile_.reset();
  }

  void DQMStreamerOutputRepackerTest::start() {}

  void DQMStreamerOutputRepackerTest::stop() { closeFile(); }

  void DQMStreamerOutputRepackerTest::doOutputHeader(InitMsgBuilder const& init_message_bldr) {
    edm::LogWarning("DQMStreamerOutputRepackerTest") << "doOutputHeader() method, initializing streams.";

    uint8_t* x = new uint8_t[init_message_bldr.size()];
    std::memcpy(x, init_message_bldr.startAddress(), init_message_bldr.size());
    init_message_cache_.reset(x);
  }

  void DQMStreamerOutputRepackerTest::doOutputEvent(EventMsgBuilder const& msg_bldr) {
    EventMsgView view(msg_bldr.startAddress());

    auto run = view.run();
    auto lumi = view.lumi();

    if ((!streamFile_) || (streamRun_ != run) || (streamLumi_ != lumi)) {
      openFile_(run, lumi);
    }

    eventsProcessedFile_ += 1;
    eventsProcessedTotal_ += 1;
    edm::LogAbsolute("DQMStreamerOutputRepackerTest") << "Writing event.";

    streamFile_->write(view);
  }

  void DQMStreamerOutputRepackerTest::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
    edm::ParameterSetDescription desc;
    StreamerOutputModuleBase::fillDescription(desc);

    desc.addUntracked<std::string>("outputPath", "./output/")->setComment("File output path.");

    desc.addUntracked<std::string>("streamLabel", "DQM")->setComment("Stream label used in json discovery.");

    descriptions.add("DQMStreamerOutputRepackerTest", desc);
  }

}  // namespace dqmservices

#include "FWCore/Framework/interface/MakerMacros.h"

typedef dqmservices::DQMStreamerOutputRepackerTest DQMStreamerOutputRepackerTest;
DEFINE_FWK_MODULE(DQMStreamerOutputRepackerTest);