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