Belle II Software  release-05-01-25
ZMQHistogramConnection.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/pcore/zmq/connections/ZMQConnection.h>
13 #include <daq/hbasf2/utils/HistogramMapping.h>
14 
15 #include <framework/pcore/zmq/connections/ZMQNullConnection.h>
16 #include <framework/pcore/zmq/connections/ZMQConfirmedConnection.h>
17 #include <framework/pcore/zmq/connections/ZMQRawConnection.h>
18 
19 #include <framework/pcore/zmq/messages/ZMQIdMessage.h>
20 
21 #include <framework/pcore/EvtMessage.h>
22 #include <daq/dqm/DqmMemFile.h>
23 #include <daq/rfarm/manager/SharedMem.h>
24 
25 #include <string>
26 #include <memory>
27 #include <map>
28 #include <optional>
29 
30 namespace Belle2 {
54  class ZMQHistoServerToFileOutput : public ZMQConnection {
55  public:
57  ZMQHistoServerToFileOutput(unsigned int maximalUncompressedBufferSize,
58  const std::string& dqmFileName,
59  const std::string& rootFileName);
60 
62  void mergeAndSend(const std::map<std::string, HistogramMapping>& storedMessages, const std::optional<unsigned int>& experiment,
63  const std::optional<unsigned int>& run,
64  EMessageTypes messageType);
66  void clear();
67 
69  std::vector<zmq::socket_t*> getSockets() const final;
71  void handleIncomingData();
72 
73  private:
75  SharedMem* m_sharedMemory = nullptr;
77  std::string m_dqmMemFileName;
79  std::string m_rootFileName;
80  };
81 
91  public:
93  ZMQHistoServerToZMQOutput(const std::string& outputAddress, const std::shared_ptr<ZMQParent>& parent);
94 
96  void mergeAndSend(const std::map<std::string, HistogramMapping>& storedMessages, const std::optional<unsigned int>& experiment,
97  const std::optional<unsigned int>& run,
98  EMessageTypes messageType);
100  void clear() {}
101 
103  std::string getMonitoringJSON() const final { return m_output.getMonitoringJSON(); }
105  void handleIncomingData() { m_output.handleIncomingData(); }
107  bool isReady() const { return m_output.isReady(); }
109  std::vector<zmq::socket_t*> getSockets() const final { return m_output.getSockets(); }
110 
111  private:
114 
116  std::vector<char> m_outputBuffer;
118  unsigned int m_maximalCompressedSize = 100'000'000;
119  };
120 
129  public:
131  ZMQHistoServerToRawOutput(const std::string& outputAddress, const std::shared_ptr<ZMQParent>& parent);
132 
134  void mergeAndSend(const std::map<std::string, HistogramMapping>& storedMessages, const std::optional<unsigned int>& experiment,
135  const std::optional<unsigned int>& run,
136  EMessageTypes messageType);
138  void clear() {}
139 
141  std::string getMonitoringJSON() const final { return m_output.getMonitoringJSON(); }
143  void handleIncomingData() { m_output.handleIncomingData(); }
145  bool isReady() const { return m_output.isReady(); }
147  std::vector<zmq::socket_t*> getSockets() const final { return m_output.getSockets(); }
148 
149  private:
151  ZMQRawOutput m_output;
152  };
154 }
Belle2::ZMQHistoServerToFileOutput::clear
void clear()
Clear the shared memory.
Definition: ZMQHistogramConnection.cc:104
Belle2::ZMQConfirmedOutput
Output part of a confirmed connection.
Definition: ZMQConfirmedConnection.h:116
Belle2::EMessageTypes
EMessageTypes
Type the messages can have.
Definition: ZMQDefinitions.h:26
Belle2::ZMQHistoServerToRawOutput
Same as ZMQHistoServerToZMQOutput just send uncompressed to a raw output.
Definition: ZMQHistogramConnection.h:136
Belle2::ZMQHistoServerToFileOutput::m_rootFileName
std::string m_rootFileName
Output file name (possible with placeholders)
Definition: ZMQHistogramConnection.h:87
Belle2::ZMQHistoServerToFileOutput::getSockets
std::vector< zmq::socket_t * > getSockets() const final
No sockets to poll on, so return an empty list.
Definition: ZMQHistogramConnection.cc:99
Belle2::ZMQConnection::isReady
virtual bool isReady() const
Return true of this connection is able to send messages right now. Can be overloaded in derived class...
Definition: ZMQConnection.cc:17
Belle2::ZMQConnection
Base class for every connection with virtual functions to be implemented:
Definition: ZMQConnection.h:40
Belle2::ZMQHistoServerToFileOutput::ZMQHistoServerToFileOutput
ZMQHistoServerToFileOutput(unsigned int maximalUncompressedBufferSize, const std::string &dqmFileName, const std::string &rootFileName)
Create a new connection initializing the DQMMemFile.
Definition: ZMQHistogramConnection.cc:27
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::SharedMem
Definition: SharedMem.h:20
Belle2::ZMQHistoServerToFileOutput::m_dqmMemFileName
std::string m_dqmMemFileName
Name of the shared memory.
Definition: ZMQHistogramConnection.h:85
Belle2::ZMQHistoServerToZMQOutput
Send the histograms as compressed byte stream via a ZMQConfirmedOutput connection after merging with ...
Definition: ZMQHistogramConnection.h:98
Belle2::ZMQHistoServerToFileOutput::mergeAndSend
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.
Definition: ZMQHistogramConnection.cc:46
Belle2::ZMQLogger::getMonitoringJSON
virtual std::string getMonitoringJSON() const
Convert the stored monitoring values to a JSON string ready for sending out via a message.
Definition: ZMQLogger.cc:17
Belle2::ZMQHistoServerToFileOutput::m_sharedMemory
SharedMem * m_sharedMemory
The SHM file. Please note that we do not call its destructor on purpose.
Definition: ZMQHistogramConnection.h:83
Belle2::ZMQRawOutput
Output connection to speak to non-zmq sockets via a ZMQ_STREAM socket.
Definition: ZMQRawConnection.h:97
Belle2::ZMQHistoServerToFileOutput::handleIncomingData
void handleIncomingData()
There should be never incoming data, so raise an exception if called anyways.
Definition: ZMQHistogramConnection.cc:94