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 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "EventFilter/Utilities/interface/EvFDaqDirector.h"
#include "EventFilter/Utilities/interface/FastMonitoringService.h"
#include "EventFilter/Utilities/interface/FileIO.h"
#include "EventFilter/Utilities/interface/JSONSerializer.h"
#include "EventFilter/Utilities/plugins/RawEventFileWriterForBU.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/Adler32Calculator.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "IOPool/Streamer/interface/FRDEventMessage.h"
#include "IOPool/Streamer/interface/FRDFileHeader.h"

using namespace jsoncollector;
using namespace edm::streamer;

//TODO:get run directory information from DaqDirector

RawEventFileWriterForBU::RawEventFileWriterForBU(edm::ParameterSet const& ps)
    : microSleep_(ps.getParameter<int>("microSleep")),
      frdFileVersion_(ps.getParameter<unsigned int>("frdFileVersion")) {
  if (edm::Service<evf::FastMonitoringService>().isAvailable())
    fms_ = static_cast<evf::FastMonitoringService*>(edm::Service<evf::FastMonitoringService>().operator->());

  //per-file JSD and FastMonitor
  rawJsonDef_.setDefaultGroup("legend");
  rawJsonDef_.addLegendItem("NEvents", "integer", DataPointDefinition::SUM);

  perFileEventCount_.setName("NEvents");
  perFileSize_.setName("NBytes");

  fileMon_ = new FastMonitor(&rawJsonDef_, false);
  fileMon_->registerGlobalMonitorable(&perFileEventCount_, false, nullptr);
  fileMon_->registerGlobalMonitorable(&perFileSize_, false, nullptr);
  fileMon_->commit(nullptr);

  //per-lumi JSD and FastMonitor
  eolJsonDef_.setDefaultGroup("legend");
  eolJsonDef_.addLegendItem("NEvents", "integer", DataPointDefinition::SUM);
  eolJsonDef_.addLegendItem("NFiles", "integer", DataPointDefinition::SUM);
  eolJsonDef_.addLegendItem("TotalEvents", "integer", DataPointDefinition::SUM);
  eolJsonDef_.addLegendItem("NLostEvents", "integer", DataPointDefinition::SUM);
  eolJsonDef_.addLegendItem("NBytes", "integer", DataPointDefinition::SUM);

  perLumiEventCount_.setName("NEvents");
  perLumiFileCount_.setName("NFiles");
  perLumiTotalEventCount_.setName("TotalEvents");
  perLumiLostEventCount_.setName("NLostEvents");
  perLumiSize_.setName("NBytes");

  lumiMon_ = new FastMonitor(&eolJsonDef_, false);
  lumiMon_->registerGlobalMonitorable(&perLumiEventCount_, false, nullptr);
  lumiMon_->registerGlobalMonitorable(&perLumiFileCount_, false, nullptr);
  lumiMon_->registerGlobalMonitorable(&perLumiTotalEventCount_, false, nullptr);
  lumiMon_->registerGlobalMonitorable(&perLumiLostEventCount_, false, nullptr);
  lumiMon_->registerGlobalMonitorable(&perLumiSize_, false, nullptr);
  lumiMon_->commit(nullptr);

  //per-run JSD and FastMonitor
  eorJsonDef_.setDefaultGroup("legend");
  eorJsonDef_.addLegendItem("NEvents", "integer", DataPointDefinition::SUM);
  eorJsonDef_.addLegendItem("NFiles", "integer", DataPointDefinition::SUM);
  eorJsonDef_.addLegendItem("NLumis", "integer", DataPointDefinition::SUM);
  eorJsonDef_.addLegendItem("LastLumi", "integer", DataPointDefinition::SUM);
  eorJsonDef_.addLegendItem("TotalEvents", "integer", DataPointDefinition::SUM);
  eorJsonDef_.addLegendItem("NLostEvents", "integer", DataPointDefinition::SUM);

  perRunEventCount_.setName("NEvents");
  perRunFileCount_.setName("NFiles");
  perRunLumiCount_.setName("NLumis");
  perRunLastLumi_.setName("LastLumi");
  perRunTotalEventCount_.setName("TotalEvents");
  perRunLostEventCount_.setName("NLostEvents");

  runMon_ = new FastMonitor(&eorJsonDef_, false);
  runMon_->registerGlobalMonitorable(&perRunEventCount_, false, nullptr);
  runMon_->registerGlobalMonitorable(&perRunFileCount_, false, nullptr);
  runMon_->registerGlobalMonitorable(&perRunLumiCount_, false, nullptr);
  runMon_->registerGlobalMonitorable(&perRunLastLumi_, false, nullptr);
  runMon_->registerGlobalMonitorable(&perRunTotalEventCount_, false, nullptr);
  runMon_->registerGlobalMonitorable(&perRunLostEventCount_, false, nullptr);

  runMon_->commit(nullptr);
}

RawEventFileWriterForBU::RawEventFileWriterForBU(std::string const& fileName) {}

RawEventFileWriterForBU::~RawEventFileWriterForBU() {
  delete fileMon_;
  delete lumiMon_;
  delete runMon_;
}

void RawEventFileWriterForBU::doOutputEvent(void* startAddress, size_t size) {
  ssize_t retval = write(outfd_, startAddress, size);

  if ((unsigned)retval != size) {
    throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
        << "Error writing FED Raw Data event data to " << fileName_ << ".  Possibly the output disk "
        << "is full?" << std::endl;
  }

  // throttle event output
  usleep(microSleep_);
  perFileEventCount_.value()++;
  perFileSize_.value() += size;

  //  cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
}

void RawEventFileWriterForBU::doOutputEvent(FRDEventMsgView const& msg) {
  ssize_t retval = write(outfd_, (void*)msg.startAddress(), msg.size());

  if ((unsigned)retval != msg.size()) {
    throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
        << "Error writing FED Raw Data event data to " << fileName_ << ".  Possibly the output disk "
        << "is full?" << std::endl;
  }

  // throttle event output
  usleep(microSleep_);
  perFileEventCount_.value()++;
  perFileSize_.value() += msg.size();

  //  cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
}

void RawEventFileWriterForBU::initialize(std::string const& destinationDir,
                                         std::string const& name,
                                         int run,
                                         unsigned int ls) {
  destinationDir_ = destinationDir;
  run_ = run;

  std::stringstream ss;
  ss << "run" << std::setfill('0') << std::setw(6) << run_;
  runPrefix_ = ss.str();

  if (outfd_ != -1) {
    if (!fms_ || !fms_->exceptionDetected() || !fms_->getAbortFlagForLumi(ls))
      finishFileWrite(ls);
    closefd();
  }

  fileName_ = name;

  if (!writtenJSDs_) {
    writeJsds();
    writtenJSDs_ = true;
  }

  outfd_ = open(fileName_.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
  edm::LogInfo("RawEventFileWriterForBU") << " opened " << fileName_;

  if (outfd_ < 0) {  //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
    throw cms::Exception("RawEventFileWriterForBU", "initialize")
        << "Error opening FED Raw Data event output file: " << name << ": " << strerror(errno) << "\n";
  }

  perFileEventCount_.value() = 0;
  perFileSize_.value() = 0;

  adlera_ = 1;
  adlerb_ = 0;

  if (frdFileVersion_ == 1) {
    //reserve space for file header
    ftruncate(outfd_, sizeof(FRDFileHeader_v1));
    lseek(outfd_, sizeof(FRDFileHeader_v1), SEEK_SET);
    perFileSize_.value() = sizeof(FRDFileHeader_v1);
  } else if (frdFileVersion_ == 2) {
    ftruncate(outfd_, sizeof(FRDFileHeader_v2));
    lseek(outfd_, sizeof(FRDFileHeader_v2), SEEK_SET);
    perFileSize_.value() = sizeof(FRDFileHeader_v2);
  }
  assert(frdFileVersion_ <= 2);
}

void RawEventFileWriterForBU::writeJsds() {
  std::stringstream ss;
  ss << destinationDir_ << "/jsd";
  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);

  std::string rawJSDName = ss.str() + "/rawData.jsd";
  std::string eolJSDName = ss.str() + "/EoLS.jsd";
  std::string eorJSDName = ss.str() + "/EoR.jsd";

  fileMon_->setDefPath(rawJSDName);
  lumiMon_->setDefPath(eolJSDName);
  runMon_->setDefPath(eorJSDName);

  struct stat fstat;
  if (stat(rawJSDName.c_str(), &fstat) != 0) {
    std::string content;
    JSONSerializer::serialize(&rawJsonDef_, content);
    FileIO::writeStringToFile(rawJSDName, content);
  }

  if (stat(eolJSDName.c_str(), &fstat) != 0) {
    std::string content;
    JSONSerializer::serialize(&eolJsonDef_, content);
    FileIO::writeStringToFile(eolJSDName, content);
  }

  if (stat(eorJSDName.c_str(), &fstat) != 0) {
    std::string content;
    JSONSerializer::serialize(&eorJsonDef_, content);
    FileIO::writeStringToFile(eorJSDName, content);
  }
}

void RawEventFileWriterForBU::finishFileWrite(unsigned int ls) {
  if (frdFileVersion_ == 1) {
    //rewind
    lseek(outfd_, 0, SEEK_SET);
    FRDFileHeader_v1 frdFileHeader(perFileEventCount_.value(), (uint32_t)ls, perFileSize_.value());
    write(outfd_, (char*)&frdFileHeader, sizeof(FRDFileHeader_v1));
    closefd();
    //move raw file from open to run directory
    rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind('/'))).c_str());

    edm::LogInfo("RawEventFileWriterForBU")
        << "Wrote RAW input file: " << fileName_ << " with perFileEventCount = " << perFileEventCount_.value()
        << " and size " << perFileSize_.value();
  } else if (frdFileVersion_ == 2) {
    lseek(outfd_, 0, SEEK_SET);
    FRDFileHeader_v2 frdFileHeader(0, perFileEventCount_.value(), (uint32_t)run_, (uint32_t)ls, perFileSize_.value());
    write(outfd_, (char*)&frdFileHeader, sizeof(FRDFileHeader_v2));
    closefd();
    //move raw file from open to run directory
    rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind('/'))).c_str());
    edm::LogInfo("RawEventFileWriterForBU")
        << "Wrote RAW input file: " << fileName_ << " with perFileEventCount = " << perFileEventCount_.value()
        << " and size " << perFileSize_.value();
  } else {
    closefd();
    //move raw file from open to run directory
    rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind('/'))).c_str());
    //create equivalent JSON file
    //TODO:fix this to use DaqDirector convention and better extension replace
    std::filesystem::path source(fileName_);
    std::string path = source.replace_extension(".jsn").string();

    fileMon_->snap(ls);
    fileMon_->outputFullJSON(path, ls);
    fileMon_->discardCollected(ls);

    //move the json file from open
    rename(path.c_str(), (destinationDir_ + path.substr(path.rfind('/'))).c_str());

    edm::LogInfo("RawEventFileWriterForBU")
        << "Wrote JSON input file: " << path << " with perFileEventCount = " << perFileEventCount_.value()
        << " and size " << perFileSize_.value();
  }
  //there is a small chance that script gets interrupted while this isn't consistent (non-atomic)
  perLumiFileCount_.value()++;
  perLumiEventCount_.value() += perFileEventCount_.value();
  perLumiSize_.value() += perFileSize_.value();
  perLumiTotalEventCount_.value() += perFileEventCount_.value();
  //update open lumi value when first file is completed
  lumiOpen_ = ls;
}

void RawEventFileWriterForBU::endOfLS(unsigned int ls) {
  if (outfd_ != -1) {
    finishFileWrite(ls);
    closefd();
  }
  lumiMon_->snap(ls);

  std::ostringstream ostr;

  ostr << destinationDir_ << "/" << runPrefix_ << "_ls" << std::setfill('0') << std::setw(4) << ls << "_EoLS"
       << ".jsn";
  //outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT,  S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
  //closefd();

  std::string path = ostr.str();
  lumiMon_->outputFullJSON(path, ls);
  lumiMon_->discardCollected(ls);

  perRunEventCount_.value() += perLumiEventCount_.value();
  perRunTotalEventCount_.value() = perRunEventCount_.value();
  perRunFileCount_.value() += perLumiFileCount_.value();
  perRunLumiCount_.value() += 1;
  perRunLastLumi_.value() = ls;

  perLumiEventCount_ = 0;
  perLumiFileCount_ = 0;
  perLumiTotalEventCount_ = 0;
  perLumiSize_ = 0;
  lumiClosed_ = ls;
}

void RawEventFileWriterForBU::stop() {
  if (lumiOpen_ > lumiClosed_)
    endOfLS(lumiOpen_);
  edm::LogInfo("RawEventFileWriterForBU") << "Writing EOR file!";
  if (!destinationDir_.empty()) {
    // create EoR file
    std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
    runMon_->snap(0);
    runMon_->outputFullJSON(path, 0);
  }
}

void RawEventFileWriterForBU::extendDescription(edm::ParameterSetDescription& desc) {
  desc.add<int>("microSleep", 0);
  desc.add<unsigned int>("frdFileVersion", 0);
}