srs-control
0.2.2
Toggle main menu visibility
Loading...
Searching...
No Matches
JsonWriter.cpp
Go to the documentation of this file.
1
#include "
JsonWriter.hpp
"
2
#include "
srs/converters/DataConvertOptions.hpp
"
3
#include "
srs/data/SRSDataStructs.hpp
"
4
#include "
srs/utils/CommonFunctions.hpp
"
5
#include "
srs/workflow/BaseTask.hpp
"
6
#include <cassert>
7
#include <cstddef>
8
#include <cstdint>
9
#include <glaze/core/opts.hpp>
10
#include <glaze/core/write.hpp>
11
#include <ios>
12
#include <ranges>
13
#include <spdlog/spdlog.h>
14
#include <stdexcept>
15
#include <string>
16
#include <vector>
17
18
namespace
srs::sink
19
{
20
void
CompactExportData::set_value
(
const
StructData
& data_struct)
21
{
22
header
= data_struct.
header
;
23
fill_hit_data
(data_struct.
hit_data
);
24
fill_marker_data
(data_struct.
marker_data
);
25
}
26
27
void
CompactExportData::fill_hit_data
(
const
std::vector<HitData>& hits)
28
{
29
hit_size
= hits.size();
30
for
(
auto
& [key, value] :
hit_data
)
31
{
32
value.clear();
33
value.reserve(
hit_size
);
34
}
35
for
(
const
auto
& hit : hits)
36
{
37
hit_data
.at(
"is_over_threshold"
).push_back(
static_cast<
uint16_t
>
(hit.is_over_threshold));
38
hit_data
.at(
"channel_num"
).push_back(hit.channel_num);
39
hit_data
.at(
"tdc"
).push_back(hit.tdc);
40
hit_data
.at(
"offset"
).push_back(hit.offset);
41
hit_data
.at(
"vmm_id"
).push_back(hit.vmm_id);
42
hit_data
.at(
"adc"
).push_back(hit.adc);
43
hit_data
.at(
"bc_id"
).push_back(hit.bc_id);
44
}
45
}
46
47
void
CompactExportData::fill_marker_data
(
const
std::vector<MarkerData>& markers)
48
{
49
marker_size
= markers.size();
50
for
(
auto
& [key, value] :
marker_data
)
51
{
52
value.clear();
53
value.reserve(
marker_size
);
54
}
55
for
(
const
auto
& marker : markers)
56
{
57
marker_data
.at(
"vmm_id"
).push_back(marker.vmm_id);
58
marker_data
.at(
"srs_timestamp"
).push_back(marker.srs_timestamp);
59
}
60
}
61
62
Json::Json
(
const
std::string& filename,
process::DataConvertOptions
convert_mode, std::size_t n_lines)
63
:
SinkTask
{
"JSONWriter"
, convert_mode, n_lines }
64
,
filename_
{ filename }
65
{
66
assert(n_lines > 0);
67
is_first_item_
.resize(n_lines);
68
output_data_
.resize(n_lines);
69
data_buffers_
.resize(n_lines);
70
string_buffers_
.resize(n_lines);
71
file_streams_
.reserve(n_lines);
72
73
for
(
auto
idx : std::views::iota(0,
static_cast<
int
>
(n_lines)))
74
{
75
auto
full_filename = (n_lines == 1) ? filename :
common::insert_index_to_filename
(filename, idx);
76
file_streams_
.emplace_back(full_filename, std::ios::out | std::ios::trunc);
77
}
78
79
for
(
auto
& file_stream :
file_streams_
)
80
{
81
if
(not file_stream.is_open())
82
{
83
spdlog::critical(
"JsonWriter: cannot open the file with filename {:?}"
, filename);
84
throw
std::runtime_error(
"Error occurred with JsonWriter"
);
85
}
86
file_stream <<
"[\n"
;
87
}
88
}
89
Json::~Json
()
90
{
91
for
(
auto
[idx, file_stream] : std::views::zip(std::views::iota(0),
file_streams_
))
92
{
93
file_stream <<
"]\n"
;
94
file_stream.close();
95
spdlog::debug(
"JSON file {} with index {} is closed successfully"
,
filename_
, idx);
96
}
97
spdlog::info(
"Writer: JSON file writer with the base name {:?} is closed successfully."
,
filename_
);
98
}
99
100
void
Json::write_json
(
const
StructData
& data_struct, std::size_t line_num)
101
{
102
if
(not
is_first_item_
[line_num].value)
103
{
104
file_streams_
[line_num] <<
", "
;
105
}
106
else
107
{
108
is_first_item_
[line_num].value =
false
;
109
}
110
111
data_buffers_
[line_num].set_value(data_struct);
112
auto
error_code = glz::write<glz::opts{ .prettify =
true
}>(
data_buffers_
[line_num],
string_buffers_
[line_num]);
113
if
(error_code)
114
{
115
spdlog::critical(
"JsonWriter: cannot interpret data struct to json. Error: {}"
,
116
error_code.custom_error_message);
117
throw
std::runtime_error(
"Error occurred with JsonWriter"
);
118
}
119
output_data_
[line_num] =
string_buffers_
[line_num].size();
120
file_streams_
[line_num] <<
string_buffers_
[line_num];
121
string_buffers_
[line_num].clear();
122
}
123
}
// namespace srs::sink
BaseTask.hpp
CommonFunctions.hpp
DataConvertOptions.hpp
JsonWriter.hpp
SRSDataStructs.hpp
srs::process::SinkTask< DataWriterOption::json, const StructData *, std::size_t >::SinkTask
SinkTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines=1)
Definition
BaseTask.hpp:109
srs::sink::Json::file_streams_
std::vector< std::fstream > file_streams_
Definition
JsonWriter.hpp:85
srs::sink::Json::filename_
std::string filename_
Definition
JsonWriter.hpp:83
srs::sink::Json::output_data_
std::vector< OutputType > output_data_
Definition
JsonWriter.hpp:84
srs::sink::Json::is_first_item_
std::vector< Bool > is_first_item_
Definition
JsonWriter.hpp:82
srs::sink::Json::write_json
void write_json(const StructData &data_struct, std::size_t line_num)
Definition
JsonWriter.cpp:100
srs::sink::Json::string_buffers_
std::vector< std::string > string_buffers_
Definition
JsonWriter.hpp:87
srs::sink::Json::~Json
~Json()
Definition
JsonWriter.cpp:89
srs::sink::Json::Json
Json(const std::string &filename, process::DataConvertOptions convert_mode, std::size_t n_lines=1)
Definition
JsonWriter.cpp:62
srs::sink::Json::data_buffers_
std::vector< CompactExportData > data_buffers_
Definition
JsonWriter.hpp:86
srs::common::insert_index_to_filename
constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string
Definition
CommonFunctions.hpp:109
srs::process::DataConvertOptions
DataConvertOptions
Definition
DataConvertOptions.hpp:14
srs::sink
Definition
BinaryFileWriter.cpp:16
srs::StructData
Definition
SRSDataStructs.hpp:59
srs::StructData::header
ReceiveDataHeader header
Header data.
Definition
SRSDataStructs.hpp:60
srs::StructData::marker_data
std::vector< MarkerData > marker_data
Marker data.
Definition
SRSDataStructs.hpp:61
srs::StructData::hit_data
std::vector< HitData > hit_data
Hit data.
Definition
SRSDataStructs.hpp:62
srs::sink::CompactExportData::fill_hit_data
void fill_hit_data(const std::vector< HitData > &hits)
Definition
JsonWriter.cpp:27
srs::sink::CompactExportData::marker_size
std::size_t marker_size
Definition
JsonWriter.hpp:29
srs::sink::CompactExportData::fill_marker_data
void fill_marker_data(const std::vector< MarkerData > &markers)
Definition
JsonWriter.cpp:47
srs::sink::CompactExportData::set_value
void set_value(const StructData &data_struct)
Definition
JsonWriter.cpp:20
srs::sink::CompactExportData::hit_data
std ::map< std::string, std::vector< uint16_t > > hit_data
Definition
JsonWriter.hpp:32
srs::sink::CompactExportData::marker_data
std ::map< std::string, std::vector< uint64_t > > marker_data
Definition
JsonWriter.hpp:31
srs::sink::CompactExportData::hit_size
std::size_t hit_size
Definition
JsonWriter.hpp:30
srs::sink::CompactExportData::header
ReceiveDataHeader header
Definition
JsonWriter.hpp:28
backend
srs
sinks
JsonWriter.cpp
Impressum
Datenschutzerklärung
Generated by
1.17.0