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