DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
HDF5SourceIDHandler.cpp
Go to the documentation of this file.
1
7
9
14#include "confmodel/GeoId.hpp"
15
16#include "logging/Logging.hpp"
17#include <nlohmann/json.hpp>
18
19namespace dunedaq {
20namespace hdf5libs {
21
22uint64_t
23encode_geoid(int det_id, int crate_id, int slot_id, int stream_id)
24{
25 return (static_cast<uint64_t>(stream_id) << 48) | (static_cast<uint64_t>(slot_id) << 32) |
26 (static_cast<uint64_t>(crate_id) << 16) | det_id;
27}
28
31{
33
34 for (auto& app : session.all_applications()) {
35 auto ro_app = app->cast<appmodel::ReadoutApplication>();
36 if (!ro_app)
37 continue;
38
39 for (auto d2d_conn : ro_app->get_detector_connections()) {
40
41 if (d2d_conn->is_excluded(session)) {
42 TLOG_DEBUG(7) << "Ignoring excluded DetectorToDaqConnection " << d2d_conn->UID();
43 continue;
44 }
45
46 TLOG() << "Processing DetectorToDaqConnection " << d2d_conn->UID();
47 // get the readout groups and the interfaces and streams therein; 1 reaout group corresponds to 1 data reader
48 // module
49
50 // Loop over senders
51 for (auto dros : d2d_conn->streams()) {
52
53 // Are we sure?
54 if (dros->is_excluded(session)) {
55 TLOG_DEBUG(7) << "Ignoring excluded DetectorStream " << dros->UID();
56 continue;
57 }
58 auto stream = dros->cast<confmodel::DetectorStream>();
59 if (!stream)
60 continue;
61 auto geoid = stream->get_geo_id();
62 auto geoid_int =
63 encode_geoid(geoid->get_detector_id(), geoid->get_crate_id(), geoid->get_slot_id(), geoid->get_stream_id());
66 sid.id = stream->get_source_id();
67
68 output_map[sid].push_back(geoid_int);
69 }
70 }
71 }
72 return output_map;
73}
74
75void
77{
78 write_attribute(h5_file, "source_id_geo_id_map", get_json_string(the_map));
79}
80
81void
83 const daqdataformats::SourceID& source_id)
84{
85 write_attribute(record_group, "record_header_source_id", get_json_string(source_id));
86}
87
88void
89HDF5SourceIDHandler::store_record_level_path_info(HighFive::Group& record_group, const source_id_path_map_t& the_map)
90{
91 write_attribute(record_group, "source_id_path_map", get_json_string(the_map));
92}
93
94void
96 const fragment_type_source_id_map_t& the_map)
97{
98 write_attribute(record_group, "fragment_type_source_id_map", get_json_string(the_map));
99}
100
101void
103 const subdetector_source_id_map_t& the_map)
104{
105 write_attribute(record_group, "subdetector_source_id_map", get_json_string(the_map));
106}
107
108HDF5SourceIDHandler::HDF5SourceIDHandler(const uint32_t version) // NOLINT(build/unsigned)
110{
111}
112
113void
115 source_id_geo_id_map_t& source_id_geo_id_map)
116{
117 if (m_version >= 3) {
118 try {
119 std::string map_string = get_attribute<HighFive::File, std::string>(h5_file, "source_id_geo_id_map");
120 parse_json_string(map_string, source_id_geo_id_map);
121 } catch (...) {
122 }
123 }
124}
125
126void
127HDF5SourceIDHandler::fetch_record_level_geo_id_info(const HighFive::Group& /*record_group*/,
128 source_id_geo_id_map_t& /*source_id_geo_id_map*/)
129{
130 // In versions 3 and 4, there is no record-level geo_id information stored in the file
131 if (m_version >= 3) {
132 return;
133 }
134}
135
137HDF5SourceIDHandler::fetch_record_header_source_id(const HighFive::Group& record_group)
138{
139 daqdataformats::SourceID source_id;
140 if (m_version >= 3) {
141 try {
142 std::string sid_string = get_attribute<HighFive::Group, std::string>(record_group, "record_header_source_id");
143 parse_json_string(sid_string, source_id);
144 } catch (...) {
145 }
146 }
147 return source_id;
148}
149
150void
151HDF5SourceIDHandler::fetch_source_id_path_info(const HighFive::Group& record_group,
152 source_id_path_map_t& source_id_path_map)
153{
154 if (m_version >= 3) {
155 try {
156 std::string map_string = get_attribute<HighFive::Group, std::string>(record_group, "source_id_path_map");
157 parse_json_string(map_string, source_id_path_map);
158 } catch (...) {
159 }
160 }
161}
162
163void
165 fragment_type_source_id_map_t& fragment_type_source_id_map)
166{
167 if (m_version >= 3) {
168 try {
169 std::string map_string = get_attribute<HighFive::Group, std::string>(record_group, "fragment_type_source_id_map");
170 parse_json_string(map_string, fragment_type_source_id_map);
171 } catch (...) {
172 }
173 }
174}
175
176void
178 subdetector_source_id_map_t& subdetector_source_id_map)
179{
180 if (m_version >= 3) {
181 try {
182 std::string map_string = get_attribute<HighFive::Group, std::string>(record_group, "subdetector_source_id_map");
183 parse_json_string(map_string, subdetector_source_id_map);
184 } catch (...) {
185 }
186 }
187}
188
189void
191 const daqdataformats::SourceID& source_id,
192 const std::string& hdf5_path)
193{
194 source_id_path_map[source_id] = hdf5_path;
195}
196
197void
199 const daqdataformats::SourceID& source_id,
200 uint64_t geo_id) // NOLINT(build/unsigned)
201{
202 if (source_id_geo_id_map.count(source_id) == 0) {
203 std::vector<uint64_t> tmp_vec; // NOLINT(build/unsigned)
204 tmp_vec.push_back(geo_id);
205 source_id_geo_id_map[source_id] = tmp_vec;
206 } else {
207 source_id_geo_id_map[source_id].push_back(geo_id);
208 }
209}
210
211void
213 const daqdataformats::FragmentType fragment_type,
214 const daqdataformats::SourceID& source_id)
215{
216 if (fragment_type_source_id_map.count(fragment_type) == 0) {
217 std::set<daqdataformats::SourceID> tmp_set;
218 tmp_set.insert(source_id);
219 fragment_type_source_id_map[fragment_type] = tmp_set;
220 } else {
221 fragment_type_source_id_map[fragment_type].insert(source_id);
222 }
223}
224
225void
227 const detdataformats::DetID::Subdetector subdetector,
228 const daqdataformats::SourceID& source_id)
229{
230 if (subdetector_source_id_map.count(subdetector) == 0) {
231 std::set<daqdataformats::SourceID> tmp_set;
232 tmp_set.insert(source_id);
233 subdetector_source_id_map[subdetector] = tmp_set;
234 } else {
235 subdetector_source_id_map[subdetector].insert(source_id);
236 }
237}
238
239void
242 const daqdataformats::SourceID& source_id)
243{
244 if (subsystem_source_id_map.count(subsystem) == 0) {
245 std::set<daqdataformats::SourceID> tmp_set;
246 tmp_set.insert(source_id);
247 subsystem_source_id_map[subsystem] = tmp_set;
248 } else {
249 subsystem_source_id_map[subsystem].insert(source_id);
250 }
251}
252
253std::string
255{
256 nlohmann::json json_struct;
257 json_struct["subsys"] = static_cast<uint32_t>(source_id.subsystem); // NOLINT(build/unsigned)
258 json_struct["id"] = source_id.id;
259 return json_struct.dump();
260}
261
262std::string
264{
265 nlohmann::json json_struct;
266 for (auto const& map_element : the_map) {
267 nlohmann::json json_element;
268 json_element["subsys"] = static_cast<uint32_t>(map_element.first.subsystem); // NOLINT(build/unsigned)
269 json_element["id"] = map_element.first.id;
270 json_element["path"] = map_element.second;
271 json_struct["map_entries"].push_back(json_element);
272 }
273 return json_struct.dump();
274}
275
276std::string
278{
279 nlohmann::json json_struct;
280 for (auto const& map_element : the_map) {
281 nlohmann::json json_geo_id_list;
282 for (auto const& geo_id_from_map : map_element.second) {
283 json_geo_id_list.push_back(geo_id_from_map);
284 }
285 nlohmann::json json_element;
286 json_element["subsys"] = static_cast<uint32_t>(map_element.first.subsystem); // NOLINT(build/unsigned)
287 json_element["id"] = map_element.first.id;
288 json_element["geoids"] = json_geo_id_list;
289 json_struct["map_entries"].push_back(json_element);
290 }
291 return json_struct.dump();
292}
293
294std::string
296{
297 nlohmann::json json_struct;
298 for (auto const& map_element : the_map) {
299 nlohmann::json json_source_id_list;
300 for (auto const& source_id_from_map : map_element.second) {
301 nlohmann::json json_source_id;
302 json_source_id["subsys"] = static_cast<uint32_t>(source_id_from_map.subsystem); // NOLINT(build/unsigned)
303 json_source_id["id"] = source_id_from_map.id;
304 json_source_id_list.push_back(json_source_id);
305 }
306 nlohmann::json json_element;
307 json_element["fragment_type"] = static_cast<uint32_t>(map_element.first);
308 json_element["sourceids"] = json_source_id_list;
309 json_struct["map_entries"].push_back(json_element);
310 }
311 return json_struct.dump();
312}
313
314std::string
316{
317 nlohmann::json json_struct;
318 for (auto const& map_element : the_map) {
319 nlohmann::json json_source_id_list;
320 for (auto const& source_id_from_map : map_element.second) {
321 nlohmann::json json_source_id;
322 json_source_id["subsys"] = static_cast<uint32_t>(source_id_from_map.subsystem); // NOLINT(build/unsigned)
323 json_source_id["id"] = source_id_from_map.id;
324 json_source_id_list.push_back(json_source_id);
325 }
326 nlohmann::json json_element;
327 json_element["subdetector"] = static_cast<uint32_t>(map_element.first);
328 json_element["sourceids"] = json_source_id_list;
329 json_struct["map_entries"].push_back(json_element);
330 }
331 return json_struct.dump();
332}
333
334void
335HDF5SourceIDHandler::parse_json_string(const std::string& json_string, daqdataformats::SourceID& source_id)
336{
337 nlohmann::json json_struct = nlohmann::json::parse(json_string);
338 daqdataformats::SourceID::Subsystem subsys = static_cast<daqdataformats::SourceID::Subsystem>(json_struct["subsys"]);
339 daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_struct["id"]);
340 source_id.subsystem = subsys;
341 source_id.id = id;
342}
343
344void
345HDF5SourceIDHandler::parse_json_string(const std::string& json_string, source_id_path_map_t& source_id_path_map)
346{
347 nlohmann::json json_struct = nlohmann::json::parse(json_string);
348 for (auto const& json_element : json_struct["map_entries"]) {
350 static_cast<daqdataformats::SourceID::Subsystem>(json_element["subsys"]);
351 daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_element["id"]);
352 daqdataformats::SourceID source_id(subsys, id);
353 source_id_path_map[source_id] = json_element["path"];
354 }
355}
356
357void
358HDF5SourceIDHandler::parse_json_string(const std::string& json_string, source_id_geo_id_map_t& source_id_geo_id_map)
359{
360 nlohmann::json json_struct = nlohmann::json::parse(json_string);
361 for (auto const& json_element : json_struct["map_entries"]) {
363 static_cast<daqdataformats::SourceID::Subsystem>(json_element["subsys"]);
364 daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_element["id"]);
365 daqdataformats::SourceID source_id(subsys, id);
366 std::vector<uint64_t> local_geo_id_list; // NOLINT(build/unsigned)
367 nlohmann::json json_geo_id_list = json_element["geoids"];
368 for (nlohmann::json json_geo_id_value : json_geo_id_list) {
369 local_geo_id_list.push_back(json_geo_id_value);
370 }
371 source_id_geo_id_map[source_id] = local_geo_id_list;
372 }
373}
374
375void
376HDF5SourceIDHandler::parse_json_string(const std::string& json_string,
377 fragment_type_source_id_map_t& fragment_type_source_id_map)
378{
379 nlohmann::json json_struct = nlohmann::json::parse(json_string);
380 for (auto const& json_element : json_struct["map_entries"]) {
381 daqdataformats::FragmentType fragment_type =
382 static_cast<daqdataformats::FragmentType>(json_element["fragment_type"]);
383 std::set<daqdataformats::SourceID> local_source_id_list;
384 nlohmann::json json_source_id_list = json_element["sourceids"];
385 for (nlohmann::json json_source_id : json_source_id_list) {
387 static_cast<daqdataformats::SourceID::Subsystem>(json_source_id["subsys"]);
388 daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_source_id["id"]);
389 daqdataformats::SourceID source_id(subsys, id);
390 local_source_id_list.insert(source_id);
391 }
392 fragment_type_source_id_map[fragment_type] = local_source_id_list;
393 }
394}
395
396void
397HDF5SourceIDHandler::parse_json_string(const std::string& json_string,
398 subdetector_source_id_map_t& subdetector_source_id_map)
399{
400 nlohmann::json json_struct = nlohmann::json::parse(json_string);
401 for (auto const& json_element : json_struct["map_entries"]) {
403 static_cast<detdataformats::DetID::Subdetector>(json_element["subdetector"]);
404 std::set<daqdataformats::SourceID> local_source_id_list;
405 nlohmann::json json_source_id_list = json_element["sourceids"];
406 for (nlohmann::json json_source_id : json_source_id_list) {
408 static_cast<daqdataformats::SourceID::Subsystem>(json_source_id["subsys"]);
409 daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_source_id["id"]);
410 daqdataformats::SourceID source_id(subsys, id);
411 local_source_id_list.insert(source_id);
412 }
413 subdetector_source_id_map[subdetector] = local_source_id_list;
414 }
415}
416
417} // namespace hdf5libs
418} // namespace dunedaq
const dunedaq::confmodel::GeoId * get_geo_id() const
Get "geo_id" relationship value.
void fetch_source_id_path_info(const HighFive::Group &record_group, source_id_path_map_t &the_map)
std::map< daqdataformats::SourceID, std::vector< uint64_t > > source_id_geo_id_map_t
static void add_source_id_geo_id_to_map(source_id_geo_id_map_t &source_id_geo_id_map, const daqdataformats::SourceID &source_id, uint64_t geo_id)
static source_id_geo_id_map_t make_source_id_geo_id_map(const confmodel::Session &session)
static void store_record_header_source_id(HighFive::Group &record_group, const daqdataformats::SourceID &source_id)
static T get_attribute(const HighFive::AnnotateTraits< C > &h5annt, const std::string &name)
static void store_file_level_geo_id_info(HighFive::File &h5_file, const source_id_geo_id_map_t &the_map)
static void add_fragment_type_source_id_to_map(fragment_type_source_id_map_t &fragment_type_source_id_map, const daqdataformats::FragmentType fragment_type, const daqdataformats::SourceID &source_id)
static void store_record_level_path_info(HighFive::Group &record_group, const source_id_path_map_t &the_map)
static void write_attribute(HighFive::AnnotateTraits< C > &h5annt, const std::string &name, T value)
static std::string get_json_string(const daqdataformats::SourceID &source_id)
static void parse_json_string(const std::string &json_string, daqdataformats::SourceID &source_id)
std::map< daqdataformats::SourceID, std::string > source_id_path_map_t
HDF5SourceIDHandler(const uint32_t version)
Constructor.
void fetch_subdetector_source_id_info(const HighFive::Group &record_group, subdetector_source_id_map_t &the_map)
static void store_record_level_subdetector_map(HighFive::Group &record_group, const subdetector_source_id_map_t &the_map)
std::map< daqdataformats::SourceID::Subsystem, std::set< daqdataformats::SourceID > > subsystem_source_id_map_t
daqdataformats::SourceID fetch_record_header_source_id(const HighFive::Group &record_group)
static void store_record_level_fragment_type_map(HighFive::Group &record_group, const fragment_type_source_id_map_t &the_map)
void fetch_file_level_geo_id_info(const HighFive::File &h5_file, source_id_geo_id_map_t &the_map)
static void add_subdetector_source_id_to_map(subdetector_source_id_map_t &subdetector_source_id_map, const detdataformats::DetID::Subdetector subdetector, const daqdataformats::SourceID &source_id)
std::map< daqdataformats::FragmentType, std::set< daqdataformats::SourceID > > fragment_type_source_id_map_t
static void add_source_id_path_to_map(source_id_path_map_t &source_id_path_map, const daqdataformats::SourceID &source_id, const std::string &hdf5_path)
void fetch_record_level_geo_id_info(const HighFive::Group &record_group, source_id_geo_id_map_t &the_map)
static void add_subsystem_source_id_to_map(subsystem_source_id_map_t &subsystem_source_id_map, const daqdataformats::SourceID::Subsystem subsystem, const daqdataformats::SourceID &source_id)
std::map< detdataformats::DetID::Subdetector, std::set< daqdataformats::SourceID > > subdetector_source_id_map_t
void fetch_fragment_type_source_id_info(const HighFive::Group &record_group, fragment_type_source_id_map_t &the_map)
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:116
#define TLOG(...)
Definition macro.hpp:21
FragmentType
All defined Fragment types.
uint64_t encode_geoid(int det_id, int crate_id, int slot_id, int stream_id)
The DUNE-DAQ namespace.
SourceID is a generalized representation of the source of a piece of data in the DAQ....
Definition SourceID.hpp:32
Subsystem subsystem
The general subsystem of the source of the data.
Definition SourceID.hpp:56
Subsystem
The Subsystem enum describes the kind of source we're dealing with.
Definition SourceID.hpp:40
ID_t id
Unique identifier of the source of the data.
Definition SourceID.hpp:59
Subdetector
The Subdetector enum describes the kind of source we're dealing with.
Definition DetID.hpp:36