BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnOnlineSource.cxx
Go to the documentation of this file.
1#include "BmnOnlineSource.h"
2
3#include <FairRootManager.h>
4#include <TBranchElement.h>
5
7 : fEndpoint(inputTransportAddress)
8 , fZmqContext(nullptr)
9 , fZmqSubSocket(nullptr)
11{}
12
14
16{
17 LOG(info) << "BmnOnlineSource::Init()";
18
19 fZmqContext.reset(zmq_ctx_new(), zmq_ctx_term);
20 fZmqSubSocket.reset(zmq_socket(fZmqContext.get(), ZMQ_SUB), zmq_close);
21
22 if (zmq_connect(fZmqSubSocket.get(), fEndpoint.Data()) != 0) {
23 LOG(info) << "BmnOnlineSource::Init() | An error occurred while "
24 "connecting the socket to the address '"
25 << fEndpoint << "': " << zmq_strerror(errno);
26 return kFALSE;
27 }
28
29 if (zmq_setsockopt(fZmqSubSocket.get(), ZMQ_SUBSCRIBE, "", 0) != 0) {
30 LOG(error) << "BmnOnlineSource::Init() | An error occurred while "
31 "subscribing the socket to the address '"
32 << fEndpoint << "': " << zmq_strerror(errno);
33 return kFALSE;
34 }
35
36 return kTRUE;
37}
38
40{
41 zmq_msg_t _message;
42 zmq_msg_init(&_message);
43
44 const auto _messageSize = zmq_msg_recv(&_message, fZmqSubSocket.get(), 0);
45
46 if (_messageSize == -1) {
47 LOG(warn) << "BmnOnlineSource::ReadEvent() | An error occurred while "
48 "receiving a message: "
49 << zmq_strerror(errno);
50 zmq_msg_close(&_message);
51 return kFALSE;
52 } else {
53 LOG(info) << "BmnOnlineSource::ReadEvent() | Message received: " << _messageSize << " B";
54 }
55
56 std::unique_ptr<TTree> tree;
57
58 DeserializeZmqMessage(&_message, tree);
59 RegisterBranches(tree);
61
62 zmq_msg_close(&_message);
63
64 return 0;
65}
66
68{
69 fZmqSubSocket.reset();
70 fZmqContext.reset();
71
72 fRegisteredBranches.clear();
73}
74
75void BmnOnlineSource::RegisterBranches(std::unique_ptr<TTree>& tree)
76{
78 return;
79 }
80
81 const auto _branches = tree->GetListOfBranches();
82 for (Long64_t branchIndex = 0; branchIndex < _branches->GetEntriesFast(); branchIndex++) {
83 const auto _branch = static_cast<TBranchElement*>(_branches->At(branchIndex));
84 const auto _branchName = _branch->GetName();
85
86 if (fRegisteredBranches.find(_branchName) == fRegisteredBranches.end()
87 && strcmp(_branch->GetClassName(), "TClonesArray") == 0)
88 {
89 auto _branch_data = std::make_unique<TClonesArray>(_branch->GetClonesName());
90
91 FairRootManager::Instance()->RegisterInputObject(_branchName, _branch_data.get());
92
93 fRegisteredBranches[_branchName] = std::move(_branch_data);
94
95 LOG(info) << "BmnOnlineSource::RegisterBranches() | Register " << _branchName;
96 }
97 }
98
100}
101
102void BmnOnlineSource::UploadDataToRegisteredBranches(std::unique_ptr<TTree>& tree)
103{
104 for (auto& _branch : fRegisteredBranches) {
105 _branch.second->Delete();
106 auto tmp = _branch.second.get();
107 if (tree->SetBranchAddress(_branch.first.c_str(), &tmp)) {
108 LOG(error) << "BmnOnlineSource::UploadData(): Branch '" << _branch.first << "' not found!";
109 }
110 }
111
112 tree->GetEntry(0);
113}
bool Bool_t
int Int_t
unsigned int UInt_t
constexpr Bool_t kFALSE
long long Long64_t
constexpr Bool_t kTRUE
std::shared_ptr< void > fZmqContext
static void DeserializeZmqMessage(zmq_msg_t *zmq_message, std::unique_ptr< T > &output)
Bool_t fAreBranchesRegistered
std::shared_ptr< void > fZmqSubSocket
virtual Int_t ReadEvent(UInt_t=0)
std::unordered_map< std::string, std::unique_ptr< TClonesArray > > fRegisteredBranches
void RegisterBranches(std::unique_ptr< TTree > &tree)
void UploadDataToRegisteredBranches(std::unique_ptr< TTree > &tree)
virtual ~BmnOnlineSource()
virtual Bool_t Init()
BmnOnlineSource(TString endpoint="tcp://localhost:5555")
virtual void Close()
const char * GetName() const override