Belle II Software  release-06-00-14
HLTDs2ZMQ.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 #include <daq/hbasf2/modules/HLTDs2ZMQ.h>
9 #include <framework/pcore/zmq/messages/ZMQMessageFactory.h>
10 
11 using namespace std;
12 using namespace Belle2;
13 
14 REG_MODULE(HLTDs2ZMQ)
15 
17 {
18  setDescription(
19  "On every event, serialize the data store and send the binary data out to "
20  "the connected ZMQ application (most likely a collector or final collector). "
21  "The sending is handled via a confirmed connection (output in this case), "
22  "so all the typical behaviour applies. Also sends out end run and termination "
23  "messages. Depending on the module setting, can send out events in "
24  "raw format (with send header and trailer and a serialized event message as buffer) "
25  "or only as normal ROOT serialized stream (evt message). "
26  "The former is the typical use case when talking with e.g. storage, the "
27  "latter can be used for local tests or when sending full events e.g. to the event display. "
28  "Please note that the environment setting of the stream objects heavily "
29  "influences the time spent in this module (because serialization needs time). "
30  "This module is only useful in the HLT context or for testing it and it optimized to be used "
31  "together with the HLTEventProcessor. Please note the special handling of the first event in the "
32  "HLTEventProcessor (therefore we do not stream the first event)"
33  );
34  setPropertyFlags(EModulePropFlags::c_Output | EModulePropFlags::c_ParallelProcessingCertified);
35 
36  addParam("output", m_param_output, "The ZMQ address of the connected application (to receive the messages).");
37  addParam("raw", m_param_raw, "Send out raw data with send header and trailer around the evtmessage instead of just the evtmessage. "
38  "The former is the typical use case when talking with e.g. storage, "
39  "the latter can be used for local tests or when sending full events e.g. to the event display.");
40 }
41 
42 void HLTDs2ZMQModule::event()
43 {
44  try {
45  if (m_firstEvent) {
46  m_streamHelper.initialize();
47  m_parent.reset(new ZMQParent);
48  m_output.reset(new ZMQConfirmedOutput(m_param_output, m_parent));
49 
50  m_firstEvent = false;
51  return;
52  }
53 
54  if (m_param_raw) {
55  auto zmqMessage = m_streamHelper.streamRaw();
56  m_output->handleEvent(std::move(zmqMessage));
57  } else {
58  auto zmqMessage = m_streamHelper.stream(false, false);
59  m_output->handleEvent(std::move(zmqMessage));
60  }
61  } catch (zmq::error_t& error) {
62  if (error.num() == EINTR) {
63  // Well, that is probably ok. It will be handled by the framework, just go out here.
64  B2DEBUG(10, "Received an signal interrupt during the event call. Will return");
65  return;
66  }
67  // This is an unexpected error: better report it.
68  B2ERROR("ZMQ Error while calling the event: " << error.num());
69  }
70 }
71 
72 void HLTDs2ZMQModule::endRun()
73 {
74  try {
75  B2DEBUG(10, "Sending out old run message");
76  auto message = ZMQMessageFactory::createMessage(EMessageTypes::c_lastEventMessage);
77  m_output->handleEvent(std::move(message));
78  } catch (zmq::error_t& error) {
79  if (error.num() == EINTR) {
80  // Well, that is probably ok. It will be handled by the framework, just go out here.
81  B2DEBUG(10, "Received an signal interrupt during the event call. Will return");
82  return;
83  }
84  // This is an unexpected error: better report it.
85  B2ERROR("ZMQ Error while calling the event: " << error.num());
86  }
87 }
88 
89 void HLTDs2ZMQModule::terminate()
90 {
91  try {
92  B2DEBUG(10, "Sending out terminate message");
93  auto message = ZMQMessageFactory::createMessage(EMessageTypes::c_terminateMessage);
94  m_output->handleEvent(std::move(message));
95  } catch (zmq::error_t& error) {
96  if (error.num() == EINTR) {
97  // Well, that is probably ok. It will be handled by the framework, just go out here.
98  B2DEBUG(10, "Received an signal interrupt during the event call. Will return");
99  return;
100  }
101  // This is an unexpected error: better report it.
102  B2ERROR("ZMQ Error while calling the event: " << error.num());
103  }
104 }
On every event, serialize the data store and send the binary data out to the connected ZMQ applicatio...
Definition: HLTDs2ZMQ.h:45
Base class for Modules.
Definition: Module.h:72
Output part of a confirmed connection.
A helper class for creating ZMQ sockets keeping track of the ZMQ context and terminating it if needed...
Definition: ZMQParent.h:39
#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.