SRS-control 0.1.4
Loading...
Searching...
No Matches
RootFileWriter.cpp
Go to the documentation of this file.
1#ifdef HAS_ROOT
2#include "RootFileWriter.hpp"
6#include <TFile.h>
7#include <TTree.h>
8#include <cstddef>
9#include <memory>
10#include <ranges>
11#include <spdlog/spdlog.h>
12#include <string>
13
14namespace srs::writer
15{
16
17 RootFile::RootFile(const std::string& filename, process::DataConvertOptions convert_mode, std::size_t n_lines)
18 : WriterTask{ "RootFile", convert_mode, n_lines }
19 , base_filename_{ filename }
20 {
21 root_files_.resize(n_lines);
22 trees_.resize(n_lines);
23 output_data_.resize(n_lines);
24 data_struct_buffers_.resize(n_lines);
25 for (auto [idx, root_file, tree] : std::views::zip(std::views::iota(0), root_files_, trees_))
26 {
27 auto full_filename = (n_lines == 1) ? filename : common::insert_index_to_filename(filename, idx);
28 root_file = std::make_unique<TFile>(full_filename.c_str(), "RECREATE");
29 // NOTE: tree is owned by the TFile
30 tree = std::make_unique<TTree>("srs_data_tree", "Data structures from SRS system").release();
31 tree->SetDirectory(root_file.get());
32 tree->Branch("srs_frame_data", &data_struct_buffers_[static_cast<std::size_t>(idx)]);
33 }
34 }
35
36 RootFile::~RootFile()
37 {
38 for (auto [root_file, tree] : std::views::zip(root_files_, trees_))
39 {
40 root_file->WriteObject(tree, tree->GetName());
41 root_file->Close();
42 spdlog::info("Writer: Root file {:?} is Closed.", root_file->GetName());
43 }
44 }
45} // namespace srs::writer
46#endif
constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string