Belle II Software development
ROISenderModule.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 <tracking/modules/roiFinding/pxd/ROISenderModule.h>
10#include <framework/dataobjects/EventMetaData.h>
11#include <tracking/dataobjects/ROIpayload.h>
12
13#include <sys/stat.h>
14#include <chrono>
15
16using namespace Belle2;
17
18//-----------------------------------------------------------------
19// Register the Module
20//-----------------------------------------------------------------
21
22REG_MODULE(ROISender);
23
24//-----------------------------------------------------------------
25// Implementation
26//-----------------------------------------------------------------
27
29 Module(), m_messageQueueNameCstring(nullptr)
30{
31 //Set module properties
32 setDescription("Send the ROI payload to the external ring buffer");
33
34 addParam("MessageQueueName", m_messageQueueName, "name of the output message queue", std::string("/roi"));
35 addParam("ROIpayloadName", m_ROIpayloadName, "name of the payload of ROIs", std::string(""));
36 addParam("MessageQueueDepth", m_messageQueueDepth, "depth of the output message queue", 10);
37 addParam("MessageSize", m_messageQueueMsgSize, "max size of themessage", 8192);
38
39 B2INFO("ROI Sender created");
40}
41
42
43void
45{
46 // Required input
47 m_eventMetaData.isRequired();
49
51
52 bool slashFree = (nullptr == strchr(m_messageQueueNameCstring + 1, '/'));
53
54 if (! slashFree || m_messageQueueNameCstring[0] != '/')
55 B2FATAL(__FILE__ << ":" << __LINE__ <<
56 m_messageQueueName << " invalid. cfr: man mq_overview ");
57
58 m_histo.resize(101, 0); // 0-99, 100 is used as overflow bin
59
60 // unlinkMessageQueue("on initialize");
61 openMessageQueue("on initialize");
62
63}
64
65
66void
68{
69
70 int length = m_roiPayload->getPacketLengthByte();
71 const char* data = (const char*) m_roiPayload->getRootdata();
72
73 mqd_t ret;
74
75 if (length <= m_messageQueueMsgSize) {
76 ret = mq_send(m_messageQueue, data, length, 0 /* priority */);
77
78 if (ret == (mqd_t) - 1) {
79 B2FATAL(std::string(__FILE__) << ":" << __LINE__ <<
80 ": error: " <<
81 strerror(errno) <<
82 " on mq_send");
83 }
84 } else {
85 B2FATAL(std::string(__FILE__) << ":" << __LINE__ <<
86 "ROI payload too long." << std::endl <<
87 "Payload length = " << length << std::endl <<
88 "Message max length = " << m_messageQueueMsgSize << std::endl <<
89 "We stop here, as this will result in event mismatch on EB! Please increase mqueue message length on HLT and/or check size limit in ROIPayload Assembler"
90 << std::endl);
91 }
92
93 // Calculate the time difference between now and the trigger time
94 // This tells you how much delay we have summed up (it is NOT the processing time!)
96 unsigned long long int meta_time = m_eventMetaData->getTime();
97
98 using namespace std::chrono;
99 nanoseconds ns = duration_cast< nanoseconds >(system_clock::now().time_since_epoch());
100 Float_t deltaT = (std::chrono::duration_cast<seconds> (ns - (nanoseconds)meta_time)).count();
101 if (deltaT < 0) {
102 m_histo[0]++;// just in case the clocks are slightly out of sync
103 } else if (deltaT < 100) {
104 m_histo[int(deltaT)]++;
105 } else {
106 m_histo[100]++;// overflow bin
107 }
108 if (deltaT > 60) {
109 B2ERROR("Event took too long on HLT, PXD data for Event might be lost!" << LogVar("deltaT in s", deltaT));
110 } else if (deltaT > 30) {
111 B2WARNING("Event took too long on HLT, PXD data for Event might be lost!" << LogVar("deltaT in s", deltaT));
112 }
113}
114
115
116
117
118void
120{
121 closeMessageQueue("on terminate");
122 // unlinkMessageQueue("on terminate");
123 std::string str = "HLT Delay time distribution: ( ";
124 for (auto& a : m_histo) str += std::to_string(a) + ";";
125 str += " )";
126 B2RESULT(str);
127}
128
129void
131{
132 struct mq_attr attr;
133 attr.mq_flags = 0;
134 attr.mq_maxmsg = m_messageQueueDepth;
135 attr.mq_msgsize = m_messageQueueMsgSize; // bytes
136
137 int oflags = O_WRONLY ; //| O_CREAT | O_EXCL;
138 mode_t mode = S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP ;
139
140
141 mqd_t ret = mq_open(m_messageQueueNameCstring, oflags, mode, &attr);
142
143
144 if (ret == (mqd_t) - 1)
145 B2FATAL(std::string(__FILE__) << ":" <<
146 __LINE__ << ": error: " <<
147 strerror(errno) <<
148 " on mq_open " << log_string);
149
150 m_messageQueue = ret;
151
152}
153
154void
156{
157 mqd_t ret;
158
159 ret = mq_close(m_messageQueue);
160 if (ret == (mqd_t) - 1)
161 B2WARNING(std::string(__FILE__) << ":" <<
162 __LINE__ << ": error: " <<
163 strerror(errno) <<
164 " on mq_close " << log_string);
165
166}
167
168
170void
172{
173 mqd_t ret;
174 ret = mq_unlink(m_messageQueueNameCstring);
175 if (ret == (mqd_t) - 1)
176 B2WARNING(std::string(__FILE__) << ":" <<
177 __LINE__ << ": error: " <<
178 strerror(errno) <<
179 " on mq_unlink " << log_string);
180
181}
182
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void openMessageQueue(const char *log_string)
open message queue
void initialize() override final
Initializes the Module.
void unlinkMessageQueue(const char *log_string)
unlink message queue
std::string m_messageQueueName
message queue name
std::string m_ROIpayloadName
ROI payload name.
const char * m_messageQueueNameCstring
message queue name c string
ROISenderModule()
Constructor of the module.
int m_messageQueueMsgSize
message queue message size
int m_messageQueueDepth
message queue depth
mqd_t m_messageQueue
message queue
void terminate() override final
Termination action.
StoreObjPtr< EventMetaData > m_eventMetaData
Input ptr for EventMetaData.
void event() override final
This method is the core of the module.
void closeMessageQueue(const char *log_string)
close message queue
std::vector< int > m_histo
poor mans histogramming in a vector
StoreObjPtr< ROIpayload > m_roiPayload
Input ptr for RoiPayload.
Class to store variables with their name which were sent to the logging service.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#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.