Belle II Software  release-08-01-10
ZMQHistogramConnection.h
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 #pragma once
9 
10 #include <framework/pcore/zmq/connections/ZMQConnection.h>
11 #include <daq/hbasf2/utils/HistogramMapping.h>
12 
13 #include <framework/pcore/zmq/connections/ZMQNullConnection.h>
14 #include <framework/pcore/zmq/connections/ZMQConfirmedConnection.h>
15 #include <framework/pcore/zmq/connections/ZMQRawConnection.h>
16 
17 #include <framework/pcore/zmq/messages/ZMQIdMessage.h>
18 
19 #include <framework/pcore/EvtMessage.h>
20 #include <daq/dqm/DqmSharedMem.h>
21 
22 #include <string>
23 #include <memory>
24 #include <map>
25 #include <optional>
26 
27 namespace Belle2 {
52  public:
54  ZMQHistoServerToFileOutput(unsigned int maximalUncompressedBufferSize,
55  const std::string& dqmFileName,
56  const std::string& rootFileName);
57 
59  void mergeAndSend(const std::map<std::string, HistogramMapping>& storedMessages, const std::optional<unsigned int>& experiment,
60  const std::optional<unsigned int>& run,
61  EMessageTypes messageType);
63  void clear();
64 
66  std::vector<zmq::socket_t*> getSockets() const final;
68  void handleIncomingData();
70  std::string getEndPoint() const { return "file://" + m_rootFileName; }
71  private:
75  std::string m_dqmMemFileName;
77  std::string m_rootFileName;
78  };
79 
89  public:
91  ZMQHistoServerToZMQOutput(const std::string& outputAddress, const std::shared_ptr<ZMQParent>& parent);
92 
94  void mergeAndSend(const std::map<std::string, HistogramMapping>& storedMessages, const std::optional<unsigned int>& experiment,
95  const std::optional<unsigned int>& run,
96  EMessageTypes messageType);
98  void clear() {}
99 
101  std::string getMonitoringJSON() const final { return m_output.getMonitoringJSON(); }
105  bool isReady() const override { return m_output.isReady(); }
107  std::vector<zmq::socket_t*> getSockets() const final { return m_output.getSockets(); }
109  std::string getEndPoint() const { return m_output.getEndPoint(); }
110  private:
113 
115  std::vector<char> m_outputBuffer;
117  unsigned int m_maximalCompressedSize = 100'000'000;
118  };
119 
128  public:
130  ZMQHistoServerToRawOutput(const std::string& outputAddress, const std::shared_ptr<ZMQParent>& parent);
131 
133  void mergeAndSend(const std::map<std::string, HistogramMapping>& storedMessages, const std::optional<unsigned int>& experiment,
134  const std::optional<unsigned int>& run,
135  EMessageTypes messageType);
137  void clear() {}
138 
140  std::string getMonitoringJSON() const final { return m_output.getMonitoringJSON(); }
144  bool isReady() const override { return m_output.isReady(); }
146  std::vector<zmq::socket_t*> getSockets() const final { return m_output.getSockets(); }
148  std::string getEndPoint() const { return m_output.getEndPoint(); }
149  private:
152  };
154 }
Output part of a confirmed connection.
void handleIncomingData()
Blocks until it can receive the (hopefully confirmation) message from the output.
std::string getEndPoint() const
Return the connection string for this socket.
std::vector< zmq::socket_t * > getSockets() const final
The socket used for polling is just the stored socket.
Base class for every connection with virtual functions to be implemented:
Definition: ZMQConnection.h:30
virtual bool isReady() const
Return true of this connection is able to send messages right now. Can be overloaded in derived class...
Output histograms into a ROOT file and shared memory after merging.
std::string m_rootFileName
Output file name (possible with placeholders)
DqmSharedMem * m_sharedMemory
The SHM file. Please note that we do not call its destructor on purpose.
std::string getEndPoint() const
Return the connection string.
void handleIncomingData()
There should be never incoming data, so raise an exception if called anyways.
ZMQHistoServerToFileOutput(unsigned int maximalUncompressedBufferSize, const std::string &dqmFileName, const std::string &rootFileName)
Create a new connection initializing the DQMMemFile.
std::string m_dqmMemFileName
Name of the shared memory.
void mergeAndSend(const std::map< std::string, HistogramMapping > &storedMessages, const std::optional< unsigned int > &experiment, const std::optional< unsigned int > &run, EMessageTypes messageType)
Merge the given histograms into a single set of histograms and store them to file/shm.
void clear()
Clear the shared memory.
std::vector< zmq::socket_t * > getSockets() const final
No sockets to poll on, so return an empty list.
Same as ZMQHistoServerToZMQOutput just send uncompressed to a raw output.
std::string getEndPoint() const
Return the connection string.
ZMQHistoServerToRawOutput(const std::string &outputAddress, const std::shared_ptr< ZMQParent > &parent)
Create a new raw output with the given address.
void handleIncomingData()
The input data handling is passed from the ZMQRawOutput.
ZMQRawOutput m_output
The output connection used for sending the histograms.
void mergeAndSend(const std::map< std::string, HistogramMapping > &storedMessages, const std::optional< unsigned int > &experiment, const std::optional< unsigned int > &run, EMessageTypes messageType)
Merge the histograms and send them via the connection. Stop/Terminate messages are not sent.
bool isReady() const override
The is ready is passed from the ZMQRawOutput.
std::vector< zmq::socket_t * > getSockets() const final
The sockets are passed from ZMQRawOutput.
std::string getMonitoringJSON() const final
The monitoring JSON is just passed from the ZMQRawOutput.
void clear()
Nothing to do on clear.
Send the histograms as compressed byte stream via a ZMQConfirmedOutput connection after merging with ...
ZMQConfirmedOutput m_output
The output connection used for sending the histograms.
std::string getEndPoint() const
Return the connection string.
std::vector< char > m_outputBuffer
Buffer used for compression.
void handleIncomingData()
The input data handling is passed from the ZMQConfirmedOutput.
ZMQHistoServerToZMQOutput(const std::string &outputAddress, const std::shared_ptr< ZMQParent > &parent)
Initialize the ZMQConfirmedOutput with the given address.
void mergeAndSend(const std::map< std::string, HistogramMapping > &storedMessages, const std::optional< unsigned int > &experiment, const std::optional< unsigned int > &run, EMessageTypes messageType)
Merge the histograms and send them via the connection. Stop/Terminate messages are sent after that.
unsigned int m_maximalCompressedSize
Maximal size of the compression buffer.
bool isReady() const override
The is ready is passed from the ZMQConfirmedOutput.
std::vector< zmq::socket_t * > getSockets() const final
The sockets are passed from ZMQConfirmedOutput.
std::string getMonitoringJSON() const final
The monitoring JSON is just passed from the ZMQConfirmedOutput.
void clear()
Nothing to do on clear.
virtual std::string getMonitoringJSON() const
Convert the stored monitoring values to a JSON string ready for sending out via a message.
Definition: ZMQLogger.cc:15
Output connection to speak to non-zmq sockets via a ZMQ_STREAM socket.
void handleIncomingData()
Handle incoming data: a socket (dis)connect.
bool isReady() const final
If no socket is connected, this connection is not ready.
EMessageTypes
Type the messages can have.
Abstract base class for different kinds of events.