CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GlobalEvFOutputModule.cc
Go to the documentation of this file.
2 
6 
10 
16 
18 
21 
24 
28 
31 
33 
34 #include <sys/stat.h>
35 #include <filesystem>
36 #include <boost/algorithm/string.hpp>
37 
39 
40 namespace evf {
41 
43 
45  public:
47  : filePath_(filePath), accepted_(0), stream_writer_events_(new StreamerOutputFile(filePath)) {}
48 
50 
51  void close() { stream_writer_events_->close(); }
52 
54  EventMsgView eview(msg.startAddress());
55  stream_writer_events_->write(eview);
56  incAccepted();
57  }
58 
59  void doOutputEventAsync(std::unique_ptr<EventMsgBuilder> msg, edm::WaitingTaskHolder iHolder) {
61  auto group = iHolder.group();
62  writeQueue_.push(*group, [holder = std::move(iHolder), msg = msg.release(), this]() {
63  try {
64  std::unique_ptr<EventMsgBuilder> own(msg);
65  doOutputEvent(*msg); //msg is written and discarded at this point
66  } catch (...) {
67  auto tmp = holder;
68  tmp.doneWaiting(std::current_exception());
69  }
70  });
71  }
72 
73  inline void throttledCheck() {
74  unsigned int counter = 0;
75  while (edm::Service<evf::EvFDaqDirector>()->inputThrottled()) {
76  if (edm::shutdown_flag.load(std::memory_order_relaxed))
77  break;
78  if (!(counter % 100))
79  edm::LogWarning("FedRawDataInputSource") << "Input throttled detected, writing is paused...";
80  usleep(100000);
81  counter++;
82  }
83  }
84 
85  uint32 get_adler32() const { return stream_writer_events_->adler32(); }
86 
87  std::string const& getFilePath() const { return filePath_; }
88 
89  unsigned long getAccepted() const { return accepted_; }
90  void incAccepted() { accepted_++; }
91 
93 
94  private:
96  std::atomic<unsigned long> accepted_;
99  };
100 
102  public:
104 
107  };
108 
110  public:
113  std::string const& outJsonDefName);
114 
126  std::shared_ptr<jsoncollector::FastMonitor> jsonMonitor_;
127  };
128 
134 
136  public:
137  explicit GlobalEvFOutputModule(edm::ParameterSet const& ps);
138  ~GlobalEvFOutputModule() override;
139  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
140 
141  private:
142  std::unique_ptr<edm::StreamerOutputModuleCommon> beginStream(edm::StreamID) const final;
143 
144  std::shared_ptr<GlobalEvFOutputJSONDef> globalBeginRun(edm::RunForOutput const& run) const final;
145 
147  void write(edm::EventForOutput const& e) final;
148 
149  //pure in parent class but unused here
151  void writeRun(edm::RunForOutput const&) final {}
152  void globalEndRun(edm::RunForOutput const&) const final {}
153 
154  std::shared_ptr<GlobalEvFOutputEventWriter> globalBeginLuminosityBlock(
155  edm::LuminosityBlockForOutput const& iLB) const final;
156  void globalEndLuminosityBlock(edm::LuminosityBlockForOutput const& iLB) const final;
157 
159 
164 
166 
167  }; //end-of-class-def
168 
170  std::string baseRunDir = edm::Service<evf::EvFDaqDirector>()->baseRunDir();
171  LogDebug("GlobalEvFOutputModule") << "writing .dat files to -: " << baseRunDir;
172 
173  edm::Service<evf::EvFDaqDirector>()->createRunOpendirMaybe();
174 
184  outJsonDef_.addLegendItem("TransferDestination", "string", jsoncollector::DataPointDefinition::SAME);
187 
188  std::stringstream tmpss, ss;
189  tmpss << baseRunDir << "/open/"
190  << "output_" << getpid() << ".jsd";
191  ss << baseRunDir << "/"
192  << "output_" << getpid() << ".jsd";
193  std::string outTmpJsonDefName = tmpss.str();
194  outJsonDefName_ = ss.str();
195 
196  edm::Service<evf::EvFDaqDirector>()->lockInitLock();
197  struct stat fstat;
198  if (stat(outJsonDefName_.c_str(), &fstat) != 0) { //file does not exist
199  LogDebug("GlobalEvFOutputModule") << "writing output definition file -: " << outJsonDefName_;
202  jsoncollector::FileIO::writeStringToFile(outTmpJsonDefName, content);
203  std::filesystem::rename(outTmpJsonDefName, outJsonDefName_);
204  }
205  edm::Service<evf::EvFDaqDirector>()->unlockInitLock();
206  }
208  jsoncollector::DataPointDefinition const& outJsonDef,
209  std::string const& outJsonDefName)
210  : processed_(0),
211  accepted_(0),
212  errorEvents_(0),
213  retCodeMask_(0),
214  filelist_(),
215  filesize_(0),
216  inputFiles_(),
217  fileAdler32_(1),
218  hltErrorEvents_(0) {
219  transferDestination_ = edm::Service<evf::EvFDaqDirector>()->getStreamDestinations(streamLabel);
220  mergeType_ = edm::Service<evf::EvFDaqDirector>()->getStreamMergeType(streamLabel, evf::MergeTypeDAT);
221 
222  processed_.setName("Processed");
223  accepted_.setName("Accepted");
224  errorEvents_.setName("ErrorEvents");
225  retCodeMask_.setName("ReturnCodeMask");
226  filelist_.setName("Filelist");
227  filesize_.setName("Filesize");
228  inputFiles_.setName("InputFiles");
229  fileAdler32_.setName("FileAdler32");
230  transferDestination_.setName("TransferDestination");
231  mergeType_.setName("MergeType");
232  hltErrorEvents_.setName("HLTErrorEvents");
233 
234  jsonMonitor_.reset(new jsoncollector::FastMonitor(&outJsonDef, true));
235  jsonMonitor_->setDefPath(outJsonDefName);
236  jsonMonitor_->registerGlobalMonitorable(&processed_, false);
237  jsonMonitor_->registerGlobalMonitorable(&accepted_, false);
238  jsonMonitor_->registerGlobalMonitorable(&errorEvents_, false);
239  jsonMonitor_->registerGlobalMonitorable(&retCodeMask_, false);
240  jsonMonitor_->registerGlobalMonitorable(&filelist_, false);
241  jsonMonitor_->registerGlobalMonitorable(&filesize_, false);
242  jsonMonitor_->registerGlobalMonitorable(&inputFiles_, false);
243  jsonMonitor_->registerGlobalMonitorable(&fileAdler32_, false);
244  jsonMonitor_->registerGlobalMonitorable(&transferDestination_, false);
245  jsonMonitor_->registerGlobalMonitorable(&mergeType_, false);
246  jsonMonitor_->registerGlobalMonitorable(&hltErrorEvents_, false);
247  jsonMonitor_->commit(nullptr);
248  }
249 
251  : edm::global::OutputModuleBase(ps),
253  ps_(ps),
254  streamLabel_(ps.getParameter<std::string>("@module_label")),
255  trToken_(consumes<edm::TriggerResults>(edm::InputTag("TriggerResults"))),
256  psetToken_(consumes<edm::SendJobHeader::ParameterSetMap, edm::InRun>(
257  ps.getUntrackedParameter<edm::InputTag>("psetMap"))) {
258  //replace hltOutoputA with stream if the HLT menu uses this convention
259  std::string testPrefix = "hltOutput";
260  if (streamLabel_.find(testPrefix) == 0)
261  streamLabel_ = std::string("stream") + streamLabel_.substr(testPrefix.size());
262 
263  if (streamLabel_.find('_') != std::string::npos) {
264  throw cms::Exception("GlobalEvFOutputModule")
265  << "Underscore character is reserved can not be used for stream names in "
266  "FFF, but was detected in stream name -: "
267  << streamLabel_;
268  }
269 
270  std::string streamLabelLow = streamLabel_;
271  boost::algorithm::to_lower(streamLabelLow);
272  auto streampos = streamLabelLow.rfind("stream");
273  if (streampos != 0 && streampos != std::string::npos)
274  throw cms::Exception("GlobalEvFOutputModule")
275  << "stream (case-insensitive) sequence was found in stream suffix. This is reserved and can not be used for "
276  "names in FFF based HLT, but was detected in stream name";
277 
279  }
280 
282 
287  desc.addUntracked<edm::InputTag>("psetMap", {"hltPSetMap"})
288  ->setComment("Optionally allow the map of ParameterSets to be calculated externally.");
289  descriptions.add("globalEvfOutputModule", desc);
290  }
291 
292  std::unique_ptr<edm::StreamerOutputModuleCommon> GlobalEvFOutputModule::beginStream(edm::StreamID) const {
293  return std::make_unique<edm::StreamerOutputModuleCommon>(
295  }
296 
297  std::shared_ptr<GlobalEvFOutputJSONDef> GlobalEvFOutputModule::globalBeginRun(edm::RunForOutput const& run) const {
298  //create run Cache holding JSON file writer and variables
299  auto jsonDef = std::make_unique<GlobalEvFOutputJSONDef>();
300 
301  edm::StreamerOutputModuleCommon streamerCommon(ps_, &keptProducts()[edm::InEvent], description().moduleLabel());
302 
303  //output INI file (non-const). This doesn't require globalBeginRun to be finished
304  const std::string openIniFileName = edm::Service<evf::EvFDaqDirector>()->getOpenInitFilePath(streamLabel_);
305  edm::LogInfo("GlobalEvFOutputModule") << "beginRun init stream -: " << openIniFileName;
306 
307  StreamerOutputFile stream_writer_preamble(openIniFileName);
308  uint32 preamble_adler32 = 1;
309  edm::BranchIDLists const* bidlPtr = branchIDLists();
310 
311  auto psetMapHandle = run.getHandle(psetToken_);
312 
313  std::unique_ptr<InitMsgBuilder> init_message =
314  streamerCommon.serializeRegistry(*streamerCommon.getSerializerBuffer(),
315  *bidlPtr,
320  psetMapHandle.isValid() ? psetMapHandle.product() : nullptr);
321 
322  //Let us turn it into a View
323  InitMsgView view(init_message->startAddress());
324 
325  //output header
326  stream_writer_preamble.write(view);
327  preamble_adler32 = stream_writer_preamble.adler32();
328  stream_writer_preamble.close();
329 
330  struct stat istat;
331  stat(openIniFileName.c_str(), &istat);
332  //read back file to check integrity of what was written
333  off_t readInput = 0;
334  uint32_t adlera = 1, adlerb = 0;
335  FILE* src = fopen(openIniFileName.c_str(), "r");
336 
337  //allocate buffer to write INI file
338  std::unique_ptr<unsigned char[]> outBuf = std::make_unique<unsigned char[]>(1024 * 1024);
339  while (readInput < istat.st_size) {
340  size_t toRead = readInput + 1024 * 1024 < istat.st_size ? 1024 * 1024 : istat.st_size - readInput;
341  fread(outBuf.get(), toRead, 1, src);
342  cms::Adler32(const_cast<const char*>(reinterpret_cast<char*>(outBuf.get())), toRead, adlera, adlerb);
343  readInput += toRead;
344  }
345  fclose(src);
346 
347  //clear serialization buffers
348  streamerCommon.getSerializerBuffer()->clearHeaderBuffer();
349 
350  //free output buffer needed only for the file write
351  outBuf.reset();
352 
353  uint32_t adler32c = (adlerb << 16) | adlera;
354  if (adler32c != preamble_adler32) {
355  throw cms::Exception("GlobalEvFOutputModule") << "Checksum mismatch of ini file -: " << openIniFileName
356  << " expected:" << preamble_adler32 << " obtained:" << adler32c;
357  } else {
358  LogDebug("GlobalEvFOutputModule") << "Ini file checksum -: " << streamLabel_ << " " << adler32c;
359  std::filesystem::rename(openIniFileName, edm::Service<evf::EvFDaqDirector>()->getInitFilePath(streamLabel_));
360  }
361 
362  return jsonDef;
363  }
364 
366  edm::EventForOutput const& e) const {
367  Trig result;
369  return result;
370  }
371 
372  std::shared_ptr<GlobalEvFOutputEventWriter> GlobalEvFOutputModule::globalBeginLuminosityBlock(
373  edm::LuminosityBlockForOutput const& iLB) const {
374  auto openDatFilePath = edm::Service<evf::EvFDaqDirector>()->getOpenDatFilePath(iLB.luminosityBlock(), streamLabel_);
375 
376  return std::make_shared<GlobalEvFOutputEventWriter>(openDatFilePath);
377  }
378 
380  edm::EventForOutput const& e,
381  edm::WaitingTaskWithArenaHolder iHolder) const {
383 
384  auto streamerCommon = streamCache(id);
385  std::unique_ptr<EventMsgBuilder> msg =
386  streamerCommon->serializeEvent(*streamerCommon->getSerializerBuffer(), e, triggerResults, selectorConfig());
387 
388  auto lumiWriter = luminosityBlockCache(e.getLuminosityBlock().index());
389  const_cast<evf::GlobalEvFOutputEventWriter*>(lumiWriter)
390  ->doOutputEventAsync(std::move(msg), iHolder.makeWaitingTaskHolderAndRelease());
391  }
393 
395  auto lumiWriter = luminosityBlockCache(iLB.index());
396  //close dat file
397  const_cast<evf::GlobalEvFOutputEventWriter*>(lumiWriter)->close();
398 
399  //auto jsonWriter = const_cast<GlobalEvFOutputJSONWriter*>(runCache(iLB.getRun().index()));
400  auto jsonDef = runCache(iLB.getRun().index());
401  GlobalEvFOutputJSONWriter jsonWriter(streamLabel_, jsonDef->outJsonDef_, jsonDef->outJsonDefName_);
402 
403  jsonWriter.fileAdler32_.value() = lumiWriter->get_adler32();
404  jsonWriter.accepted_.value() = lumiWriter->getAccepted();
405 
406  bool abortFlag = false;
407  jsonWriter.processed_.value() = fms_->getEventsProcessedForLumi(iLB.luminosityBlock(), &abortFlag);
408  if (abortFlag) {
409  edm::LogInfo("GlobalEvFOutputModule") << "Abort flag has been set. Output is suppressed";
410  return;
411  }
412 
413  if (jsonWriter.processed_.value() != 0) {
414  struct stat istat;
415  std::filesystem::path openDatFilePath = lumiWriter->getFilePath();
416  stat(openDatFilePath.string().c_str(), &istat);
417  jsonWriter.filesize_ = istat.st_size;
418  std::filesystem::rename(openDatFilePath.string().c_str(),
420  jsonWriter.filelist_ = openDatFilePath.filename().string();
421  } else {
422  //remove empty file when no event processing has occurred
423  remove(lumiWriter->getFilePath().c_str());
424  jsonWriter.filesize_ = 0;
425  jsonWriter.filelist_ = "";
426  jsonWriter.fileAdler32_.value() = -1; //no files in signed long
427  }
428 
429  //produce JSON file
430  jsonWriter.jsonMonitor_->snap(iLB.luminosityBlock());
431  const std::string outputJsonNameStream =
432  edm::Service<evf::EvFDaqDirector>()->getOutputJsonFilePath(iLB.luminosityBlock(), streamLabel_);
433  jsonWriter.jsonMonitor_->outputFullJSON(outputJsonNameStream, iLB.luminosityBlock());
434  }
435 
436 } // namespace evf
437 
438 using namespace evf;
void addLegendItem(std::string const &name, std::string const &type, std::string const &operation)
ParameterSetID const & mainParameterSetID() const
std::shared_ptr< jsoncollector::FastMonitor > jsonMonitor_
edm::global::OutputModule< edm::RunCache< GlobalEvFOutputJSONDef >, edm::LuminosityBlockCache< evf::GlobalEvFOutputEventWriter >, edm::StreamCache< edm::StreamerOutputModuleCommon >, edm::ExternalWork > GlobalEvFOutputModuleType
GlobalEvFOutputEventWriter(std::string const &filePath)
jsoncollector::DataPointDefinition outJsonDef_
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
std::string const & getFilePath() const
RunIndex index() const
Definition: RunForOutput.cc:32
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
GlobalEvFOutputJSONWriter(std::string const &streamLabel, jsoncollector::DataPointDefinition const &, std::string const &outJsonDefName)
volatile std::atomic< bool > shutdown_flag
std::shared_ptr< GlobalEvFOutputJSONDef > globalBeginRun(edm::RunForOutput const &run) const final
static bool serialize(JsonSerializable *pObj, std::string &output)
void push(oneapi::tbb::task_group &, const T &iAction)
asynchronously pushes functor iAction into queue
void write(const InitMsgBuilder &)
edm::EDGetTokenT< edm::TriggerResults > trToken_
uint8 * startAddress() const
std::string to_lower(const std::string &s)
std::unique_ptr< edm::StreamerOutputModuleCommon > beginStream(edm::StreamID) const final
void doOutputEvent(EventMsgBuilder const &msg)
std::string const & moduleLabel() const
edm::EDGetTokenT< edm::SendJobHeader::ParameterSetMap > psetToken_
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
tuple result
Definition: mps_fire.py:311
void globalEndRun(edm::RunForOutput const &) const final
LuminosityBlockIndex index() const
oneapi::tbb::task_group * group() const noexcept
Trig getTriggerResults(edm::EDGetTokenT< edm::TriggerResults > const &token, edm::EventForOutput const &e) const
void writeRun(edm::RunForOutput const &) final
LuminosityBlockForOutput const & getLuminosityBlock() const
LuminosityBlockNumber_t luminosityBlock() const
static void fillDescription(ParameterSetDescription &desc)
virtual void setName(std::string name)
ModuleDescription const & description() const
def move
Definition: eostools.py:511
def load
Definition: svgfig.py:547
std::atomic< unsigned long > accepted_
std::shared_ptr< GlobalEvFOutputEventWriter > globalBeginLuminosityBlock(edm::LuminosityBlockForOutput const &iLB) const final
static std::string const triggerResults
Definition: EdmProvDump.cc:44
RunForOutput const & getRun() const
ThinnedAssociationsHelper const * thinnedAssociationsHelper() const
static void writeStringToFile(std::string const &filename, std::string &content)
Definition: FileIO.cc:21
evf::FastMonitoringService * fms_
void globalEndLuminosityBlock(edm::LuminosityBlockForOutput const &iLB) const final
unsigned int uint32
Definition: MsgTools.h:13
BranchIDLists const * branchIDLists() const
Log< level::Info, false > LogInfo
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
tuple group
Definition: watchdog.py:82
uint32 adler32() const
ParameterSetID selectorConfig() const
jsoncollector::StringJ transferDestination_
void write(edm::EventForOutput const &e) final
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
tuple msg
Definition: mps_check.py:285
BasicHandle getByToken(EDGetToken token, TypeID const &typeID) const
std::map< ParameterSetID, ParameterSetBlob > ParameterSetMap
edm::ParameterSet const & ps_
void acquire(edm::StreamID, edm::EventForOutput const &, edm::WaitingTaskWithArenaHolder) const final
void doOutputEventAsync(std::unique_ptr< EventMsgBuilder > msg, edm::WaitingTaskHolder iHolder)
bool isValid() const
Definition: Hash.h:141
static std::atomic< unsigned int > counter
SelectedProductsForBranchType const & keptProducts() const
void writeLuminosityBlock(edm::LuminosityBlockForOutput const &) final
const ModuleDescription & moduleDescription() const
unsigned int getEventsProcessedForLumi(unsigned int lumi, bool *abortFlag=nullptr)
GlobalEvFOutputModule(edm::ParameterSet const &ps)
edm::propagate_const< std::unique_ptr< StreamerOutputFile > > stream_writer_events_
void setDefaultGroup(std::string const &group)
Log< level::Warning, false > LogWarning
tmp
align.sh
Definition: createJobs.py:716
edm::detail::TriggerResultsBasedEventSelector::handle_t Trig
static void fillDescription(ParameterSetDescription &desc, std::vector< std::string > const &iDefaultOutputCommands=ProductSelectorRules::defaultSelectionStrings())
#define LogDebug(id)