R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BUcesbSource2.cxx
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2025 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#include "R3BUcesbSource2.h"
15#include "R3BUcesbLauncher.h"
16#include <FairRootManager.h>
17#include <FairRun.h>
18#include <R3BEventHeader.h>
19#include <R3BException.h>
20#include <R3BUcesbDecl.h>
21#include <array>
22#include <boost/core/span.hpp>
23#include <chrono>
24#include <cstddef>
25#include <cstdint>
26#include <exception>
27#include <ext_data_client.h>
28#include <fairlogger/Logger.h>
29#include <fmt/chrono.h> // NOLINT
30#include <fmt/core.h>
31#include <fmt/format.h>
32#include <memory>
33#include <mutex>
34#include <string_view>
35#include <sys/types.h>
36#include <thread>
37
38namespace R3B
39{
40 namespace
41 {
42 void print_uint32_with_size(const uint32_t* data, ssize_t size)
43 {
44 // TODO: use ranges library instead of reinterpret_cast
45 constexpr auto column_size = 8;
46 using SubDataType = const std::array<uint32_t, column_size>;
47
48 auto data_span = boost::span<SubDataType>(reinterpret_cast<SubDataType*>(data), size / column_size);
49 LOGP(info, "Raw data:");
50 auto index = uint32_t{};
51 for (const auto& row_data : data_span)
52 {
53 fmt::print("RAW{0:04x}: {1:08x}\n", index, fmt::join(row_data, " "));
54 index += column_size;
55 }
56 }
57
58 } // namespace
59
60 UcesbSource::UcesbSource() = default;
61
62 UcesbSource::UcesbSource(std::string_view lmdfile_name,
63 std::string_view ntuple_options,
64 std::string_view ucesb_path,
65 EventStructType* event_struct,
66 size_t event_struct_size)
67 : event_struct_size_{ event_struct_size }
68 , event_struct_{ event_struct }
69 , lmdfile_name_{ lmdfile_name }
70 , ntuple_options_{ ntuple_options }
71 , ucesb_path_{ ucesb_path }
72 {
73 }
74
76 {
77 init_runID();
78 init_ucesb();
79 return true;
80 }
81
83
85 {
86 auto command_string = fmt::format("{0} {1} --ntuple={2},STRUCT,-", ucesb_path_, lmdfile_name_, ntuple_options_);
87 if (max_event_num_ > 0)
88 {
89 command_string = fmt::format("{} --max-events={}", command_string, max_event_num_);
90 }
91 LOGP(info, "Calling ucesb with command: {}", command_string);
92 ucesb_server_launcher_ = std::make_unique<UcesbServerLauncher>(&ucesb_client_);
93 ucesb_server_launcher_->SetLaunchCmd(command_string);
94 ucesb_server_launcher_->Launch();
95 }
96
98 {
99 if (auto* frm = FairRootManager::Instance(); frm != nullptr)
100 {
101 LOGP(debug, "Checking the register of R3BEventHeader");
102 if (event_header_ = dynamic_cast<R3BEventHeader*>(frm->GetObject("EventHeader.")); event_header_ == nullptr)
103 {
104 throw R3B::runtime_error("EventHeader. was not defined properly!");
105 }
106 }
107 LOGP(debug, "EventHeader. was defined properly");
108
109 init_readers();
110 setup_ucesb();
111
112 return true;
113 }
114
116 {
117 // TODO: convert to std::bitset
118 // could be initialzed in type UcesbMap. But C++ doesn't allow static cast of enum class pointer to its
119 // underlying type
120 auto is_struct_map_success = uint32_t{};
121 LOGP(info, "Setting up ucesb client...");
122 if (ucesb_client_.setup(
123 nullptr, 0, ucesb_client_struct_info_.Get(), &is_struct_map_success, event_struct_size_) == 0)
124 {
125 ucesb_client_struct_info_.CheckStructMapping(this);
126 }
127 else
128 {
129 LOGP(error, "ext_data_clnt::setup() failed");
130 const auto* msg = (ucesb_client_.last_error() == nullptr) ? UCESB_NULL_STR_MSG : ucesb_client_.last_error();
131 throw R3B::runtime_error(fmt::format("UCESB error: {}", msg));
132 }
133 }
134
136 {
137 auto lock = std::scoped_lock{ event_reader_mutex_ };
138 try
139 {
141 }
142 catch (std::exception& ex)
143 {
144 throw;
145 }
146 }
147
148 int UcesbSource::ReadEvent(unsigned int /*eventID*/)
149 {
150 auto lock = std::scoped_lock{ event_reader_mutex_ };
151 auto ret_val = ucesb_client_.fetch_event(event_struct_, event_struct_size_);
152 if (ret_val > 0)
153 {
154 ForEachReader([](auto& reader) { reader->R3BRead(); });
155 }
156 else if (ret_val == 0)
157 {
158 LOGP(info, "Reached the maximal event num on the ucesb server.");
160 {
162 return 0;
163 }
164 return 1;
165 }
166 else
167 {
168 LOGP(error, "ext_data_clnt::fetch_event() failed");
169 const auto* msg = (ucesb_client_.last_error() == nullptr) ? UCESB_NULL_STR_MSG : ucesb_client_.last_error();
170 throw R3B::runtime_error(fmt::format("UCESB error: {}", msg));
171 }
172 return 0;
173 }
174
176 {
177 // TODO: what's the best way to deal with this void** monstrosity
178 const void* raw_data = nullptr;
179 ssize_t raw_data_size = 0;
180 auto ret_val = ucesb_client_.get_raw_data(&raw_data, &raw_data_size);
181
182 if (ret_val == 0)
183 {
184 if (raw_data != nullptr)
185 {
186 const auto* data = reinterpret_cast<const uint32_t*>(raw_data);
187 print_uint32_with_size(data, raw_data_size);
188 }
189 }
190 else
191 {
192 LOGP(error, "ext_data_clnt::get_raw_data()");
193 throw R3B::runtime_error("Failed to get raw data.");
194 }
195 }
196
198 {
199 auto* run = FairRun::Instance();
200 if (run == nullptr)
201 {
202 throw R3B::runtime_error("FairRun is not available!");
203 }
204
205 if (run_id_ != 0)
206 {
207 LOGP(info, "Setting the run ID of the FairRun to be {} from FairSource", run_id_);
208 run->SetRunId(run_id_);
209 }
210 else if (auto run_id = run->GetRunId(); run_id != 0)
211 {
212 LOGP(info, "Setting the run ID of the FairSource to be {} from FairRun", run_id);
213 run_id_ = run_id;
214 }
215 else
216 {
217 LOGP(warn, "Run ID of neither FairRun nor FairSource is set!");
218 }
219 }
220
221 void UcesbSource::FillEventHeader(FairEventHeader* feh) { feh->SetRunId(run_id_); }
222
224 {
225 max_event_num_ = (EvtEnd == 0) ? max_event_num_ : EvtEnd;
226 return max_event_num_ >= 0 ? max_event_num_ : -1;
227 }
228
229 // readers looping methods:
231 {
233 [this](auto& reader)
234 {
235 if (not reader->Init(ucesb_client_struct_info_.Get()))
236 {
237 const auto* msg =
238 (ucesb_client_.last_error() == nullptr) ? UCESB_NULL_STR_MSG : ucesb_client_.last_error();
239 throw R3B::runtime_error(fmt::format("UCESB error: {}", msg));
240 }
241 });
242 }
243
245 {
247 [](auto& reader)
248 {
249 if (!reader->ReInit())
250 {
251 throw R3B::runtime_error("ReInit of a reader failed.");
252 }
253 });
254 return true;
255 }
256
258 {
259 ucesb_server_launcher_->Close();
260 LOGP(info, "Trying to restart ucesb server...");
261 ucesb_server_launcher_->Launch();
262 setup_ucesb();
263 }
264
266 {
267 constexpr auto minimum_duration = std::chrono::minutes{ 30 };
268 constexpr auto max_waiting_time = std::chrono::minutes{ 600 };
269 constexpr auto waiting_time_increment = std::chrono::minutes{ 30 };
270 auto time_now = std::chrono::system_clock::now();
271 auto duration = std::chrono::duration_cast<std::chrono::minutes>(time_now - last_start_time_);
272 if (duration < minimum_duration)
273 {
274 LOGP(info, "The program has been running shortly for {}", duration);
276 (waiting_time_ < max_waiting_time) ? waiting_time_ + waiting_time_increment : max_waiting_time;
277 }
278 else
279 {
280 LOGP(info, "The program has been running for {}", duration);
281 waiting_time_ = std::chrono::minutes{ 0 };
282 }
283 LOGP(info,
284 fmt::format(
285 "Infinite run enabled! Relaunching ucesb server after {}. Time now: {}", waiting_time_, time_now));
286 std::this_thread::sleep_for(waiting_time_);
288 last_start_time_ = std::chrono::system_clock::now();
289 }
290} // namespace R3B
constexpr auto UCESB_NULL_STR_MSG
EXT_STR_h101_t EventStructType
std::string ntuple_options_
std::chrono::time_point< std::chrono::system_clock > last_start_time_
R3BEventHeader * event_header_
ext_data_clnt ucesb_client_
std::unique_ptr< UcesbServerLauncher > ucesb_server_launcher_
std::mutex event_reader_mutex_
bool InitUnpackers() override
bool Init() override
bool ReInitUnpackers() override
UcesbStructInfo ucesb_client_struct_info_
unsigned int run_id_
void FillEventHeader(FairEventHeader *feh) override
std::string lmdfile_name_
int ReadEvent(unsigned int eventID=0) override
void restart_ucesb_server_delayed()
int CheckMaxEventNo(int EvtEnd=0) override
std::chrono::minutes waiting_time_
void ForEachReader(UnaryOp opt)
std::string ucesb_path_
EventStructType * event_struct_