Belle II Software  release-06-02-00
EventInfoPrinterModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <framework/modules/core/EventInfoPrinterModule.h>
10 
11 #include <boost/format.hpp>
12 #include <chrono>
13 #include <ctime>
14 
15 using namespace std;
16 using namespace Belle2;
17 
18 //-----------------------------------------------------------------
19 // Register the Module
20 //-----------------------------------------------------------------
21 REG_MODULE(EventInfoPrinter)
22 
23 //-----------------------------------------------------------------
24 // Implementation
25 //-----------------------------------------------------------------
26 
28 {
29  //Set module properties
30  setDescription("Prints the current event meta data information (exp, run, event numbers). LogLevel need to be set to INFO, at least for this module.");
31 
32  addParam("printTime", m_printTime, "Print time in addition to exp/run/evt numbers.", false);
33 }
34 
35 EventInfoPrinterModule::~EventInfoPrinterModule() = default;
36 
37 void EventInfoPrinterModule::initialize()
38 {
39  m_eventMetaData.isRequired();
40 }
41 
42 void EventInfoPrinterModule::beginRun()
43 {
44  B2INFO("========================================================================");
45  B2INFO(">>> Start new run: " << m_eventMetaData->getRun());
46  B2INFO("------------------------------------------------------------------------");
47 }
48 
49 
50 void EventInfoPrinterModule::event()
51 {
52  //Print event meta data information
53  if (!m_eventMetaData) return;
54  if (m_printTime) {
55  B2INFO(boost::format("EXP: %5d RUN: %6d EVT: %8d TIME: %s") % m_eventMetaData->getExperiment() % m_eventMetaData->getRun() %
56  m_eventMetaData->getEvent() % formatDateTime(m_eventMetaData->getTime()));
57  } else {
58  B2INFO(boost::format("EXP: %8d RUN: %8d EVT: %8d") % m_eventMetaData->getExperiment() % m_eventMetaData->getRun() %
59  m_eventMetaData->getEvent());
60  }
61 }
62 
63 
64 void EventInfoPrinterModule::endRun()
65 {
66  B2INFO("------------------------------------------------------------------------");
67  B2INFO("<<< End run: " << m_eventMetaData->getRun());
68  B2INFO("========================================================================");
69 }
70 
71 std::string EventInfoPrinterModule::formatDateTime(unsigned long long int nsec)
72 {
73  using namespace chrono;
74  high_resolution_clock::time_point time(duration_cast<high_resolution_clock::duration>(nanoseconds(nsec)));
75  time_t ttime = high_resolution_clock::to_time_t(time);
76  tm* tm = gmtime(&ttime);
77 
78  char timeStr[32];
79  strftime(timeStr, 32, "%F %T", tm);
80  unsigned int sec_decimals = (nsec / 1000) % 1000000;
81 
82  return str(boost::format("%s.%06d") % timeStr % sec_decimals);
83 }
The event meta data info module.
Base class for Modules.
Definition: Module.h:72
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.