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