SRS-control 0.1.4
 
Loading...
Searching...
No Matches
RootFileWriter.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef HAS_ROOT
4#include <TFile.h>
5#include <TSystem.h>
6#include <TTree.h>
7
10
11namespace srs::writer
12{
13 class RootFile
14 {
15 public:
16 using InputType = const StructData*;
17 using OutputType = int;
18 using CoroType = asio::experimental::coro<OutputType(std::optional<InputType>)>;
19 using InputFuture = boost::shared_future<std::optional<InputType>>;
20 using OutputFuture = boost::unique_future<std::optional<OutputType>>;
21 static constexpr auto IsStructType = true;
22
23 explicit RootFile(asio::thread_pool& thread_pool, auto&&... args)
24 : root_file{ std::forward<decltype(args)>(args)... }
25 {
26 spdlog::debug("Root file {:?} has been opened.", root_file.GetName());
27 tree.SetDirectory(&root_file);
28 tree.Branch("srs_frame_data", &output_buffer_);
29 coro_ = generate_coro(thread_pool.get_executor());
30 common::coro_sync_start(coro_, std::optional<InputType>{}, asio::use_awaitable);
31 // spdlog::trace("ROOT INCLUDE DIRS: {}", gSystem->GetIncludePath());
32 // spdlog::trace("ROOT LIBRARIES: {}", gSystem->GetLibraries());
33 }
34
35 ~RootFile()
36 {
37 if (not is_closed_)
38 {
39 spdlog::error("Root file {:?} is not closed successfully. Data might not be written completely.",
40 root_file.GetName());
41 }
42 }
43
44 auto write(auto pre_future) -> OutputFuture { return common::create_coro_future(coro_, pre_future); }
45 [[nodiscard]] static auto get_convert_mode() -> process::DataConvertOptions
46 {
47 return process::DataConvertOptions::structure;
48 }
49
50 private:
51 bool is_closed_ = false;
52 TFile root_file;
53 CoroType coro_;
54 TTree tree{ "srs_data_tree", "Data structures from SRS system" };
55 StructData output_buffer_;
56
57 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
58 auto generate_coro(asio::any_io_executor /*unused*/) -> CoroType
59 {
60 InputType data_struct{};
61 auto res = 0;
62 while (true)
63 {
64 res = 0;
65 if (data_struct != nullptr)
66 {
67 output_buffer_ = *data_struct;
68 tree.Fill();
69 res = 1;
70 }
71 auto data_temp = co_yield res;
72 if (data_temp.has_value())
73 {
74 data_struct = data_temp.value();
75 }
76 else
77 {
78 root_file.Write();
79 spdlog::info("ROOT file {} is closed successfully", root_file.GetName());
80 is_closed_ = true;
81 co_return;
82 }
83 }
84 }
85
86 public:
87 RootFile(const RootFile&) = delete;
88 RootFile(RootFile&&) = delete;
89 RootFile& operator=(const RootFile&) = delete;
90 RootFile& operator=(RootFile&&) = delete;
91 };
92
93} // namespace srs::writer
94
95#endif