Line data Source code
1 : /**
2 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
3 : * Licensing/copyright details are in the COPYING file that you should have
4 : * received with this code.
5 : *
6 : */
7 :
8 : #include "hdf5libs/HDF5SourceIDHandler.hpp"
9 :
10 : #include "confmodel/Application.hpp"
11 : #include "confmodel/DetectorStream.hpp"
12 : #include "confmodel/DetectorToDaqConnection.hpp"
13 : #include "confmodel/GeoId.hpp"
14 : #include "appmodel/ReadoutApplication.hpp"
15 :
16 : #include "logging/Logging.hpp"
17 : #include <nlohmann/json.hpp>
18 :
19 : namespace dunedaq {
20 : namespace hdf5libs {
21 :
22 : uint64_t
23 196 : encode_geoid(int det_id, int crate_id, int slot_id, int stream_id)
24 : {
25 196 : return (static_cast<uint64_t>(stream_id) << 48) | (static_cast<uint64_t>(slot_id) << 32) |
26 196 : (static_cast<uint64_t>(crate_id) << 16) | det_id;
27 : }
28 :
29 : HDF5SourceIDHandler::source_id_geo_id_map_t
30 15 : HDF5SourceIDHandler::make_source_id_geo_id_map(const confmodel::Session* session)
31 : {
32 15 : HDF5SourceIDHandler::source_id_geo_id_map_t output_map;
33 :
34 45 : for (auto& app : session->all_applications()) {
35 30 : auto ro_app = app->cast<appmodel::ReadoutApplication>();
36 30 : if (!ro_app)
37 15 : continue;
38 :
39 76 : for (auto d2d_conn : ro_app->get_detector_connections()) {
40 :
41 : // Are we sure?
42 61 : if (d2d_conn->is_disabled(*session)) {
43 0 : TLOG_DEBUG(7) << "Ignoring disabled DetectorToDaqConnection " << d2d_conn->UID();
44 0 : continue;
45 0 : }
46 :
47 122 : 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 257 : for (auto dros : d2d_conn->streams()) {
53 :
54 : // Are we sure?
55 196 : if (dros->is_disabled(*session)) {
56 0 : TLOG_DEBUG(7) << "Ignoring disabled DetectorStream " << dros->UID();
57 0 : continue;
58 0 : }
59 196 : auto stream = dros->cast<confmodel::DetectorStream>();
60 196 : if (!stream)
61 0 : continue;
62 196 : auto geoid = stream->get_geo_id();
63 196 : auto geoid_int =
64 196 : encode_geoid(geoid->get_detector_id(), geoid->get_crate_id(), geoid->get_slot_id(), geoid->get_stream_id());
65 196 : daqdataformats::SourceID sid;
66 196 : sid.subsystem = daqdataformats::SourceID::Subsystem::kDetectorReadout;
67 196 : sid.id = stream->get_source_id();
68 :
69 196 : output_map[sid].push_back(geoid_int);
70 61 : }
71 : }
72 15 : }
73 15 : return output_map;
74 0 : }
75 :
76 : void
77 29 : HDF5SourceIDHandler::store_file_level_geo_id_info(HighFive::File& h5_file, const source_id_geo_id_map_t& the_map)
78 : {
79 29 : write_attribute(h5_file, "source_id_geo_id_map", get_json_string(the_map));
80 29 : }
81 :
82 : void
83 105 : HDF5SourceIDHandler::store_record_header_source_id(HighFive::Group& record_group,
84 : const daqdataformats::SourceID& source_id)
85 : {
86 105 : write_attribute(record_group, "record_header_source_id", get_json_string(source_id));
87 105 : }
88 :
89 : void
90 105 : HDF5SourceIDHandler::store_record_level_path_info(HighFive::Group& record_group, const source_id_path_map_t& the_map)
91 : {
92 105 : write_attribute(record_group, "source_id_path_map", get_json_string(the_map));
93 105 : }
94 :
95 : void
96 105 : HDF5SourceIDHandler::store_record_level_fragment_type_map(HighFive::Group& record_group,
97 : const fragment_type_source_id_map_t& the_map)
98 : {
99 105 : write_attribute(record_group, "fragment_type_source_id_map", get_json_string(the_map));
100 105 : }
101 :
102 : void
103 105 : HDF5SourceIDHandler::store_record_level_subdetector_map(HighFive::Group& record_group,
104 : const subdetector_source_id_map_t& the_map)
105 : {
106 105 : write_attribute(record_group, "subdetector_source_id_map", get_json_string(the_map));
107 105 : }
108 :
109 54 : HDF5SourceIDHandler::HDF5SourceIDHandler(const uint32_t version) // NOLINT(build/unsigned)
110 54 : : m_version(version)
111 : {
112 54 : }
113 :
114 : void
115 14 : HDF5SourceIDHandler::fetch_file_level_geo_id_info(const HighFive::File& h5_file,
116 : source_id_geo_id_map_t& source_id_geo_id_map)
117 : {
118 14 : if (m_version >= 3) {
119 14 : try {
120 14 : std::string map_string = get_attribute<HighFive::File, std::string>(h5_file, "source_id_geo_id_map");
121 14 : parse_json_string(map_string, source_id_geo_id_map);
122 14 : } catch (...) {
123 0 : }
124 : }
125 14 : }
126 :
127 : void
128 40 : HDF5SourceIDHandler::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 40 : if (m_version >= 3) {
133 : return;
134 : }
135 : }
136 :
137 : daqdataformats::SourceID
138 40 : HDF5SourceIDHandler::fetch_record_header_source_id(const HighFive::Group& record_group)
139 : {
140 40 : daqdataformats::SourceID source_id;
141 40 : if (m_version >= 3) {
142 40 : try {
143 40 : std::string sid_string = get_attribute<HighFive::Group, std::string>(record_group, "record_header_source_id");
144 40 : parse_json_string(sid_string, source_id);
145 40 : } catch (...) {
146 0 : }
147 : }
148 40 : return source_id;
149 : }
150 :
151 : void
152 40 : HDF5SourceIDHandler::fetch_source_id_path_info(const HighFive::Group& record_group,
153 : source_id_path_map_t& source_id_path_map)
154 : {
155 40 : if (m_version >= 3) {
156 40 : try {
157 40 : std::string map_string = get_attribute<HighFive::Group, std::string>(record_group, "source_id_path_map");
158 40 : parse_json_string(map_string, source_id_path_map);
159 40 : } catch (...) {
160 0 : }
161 : }
162 40 : }
163 :
164 : void
165 40 : HDF5SourceIDHandler::fetch_fragment_type_source_id_info(const HighFive::Group& record_group,
166 : fragment_type_source_id_map_t& fragment_type_source_id_map)
167 : {
168 40 : if (m_version >= 3) {
169 40 : try {
170 40 : std::string map_string = get_attribute<HighFive::Group, std::string>(record_group, "fragment_type_source_id_map");
171 40 : parse_json_string(map_string, fragment_type_source_id_map);
172 40 : } catch (...) {
173 0 : }
174 : }
175 40 : }
176 :
177 : void
178 40 : HDF5SourceIDHandler::fetch_subdetector_source_id_info(const HighFive::Group& record_group,
179 : subdetector_source_id_map_t& subdetector_source_id_map)
180 : {
181 40 : if (m_version >= 3) {
182 40 : try {
183 40 : 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 40 : } catch (...) {
186 0 : }
187 : }
188 40 : }
189 :
190 : void
191 1685 : HDF5SourceIDHandler::add_source_id_path_to_map(source_id_path_map_t& source_id_path_map,
192 : const daqdataformats::SourceID& source_id,
193 : const std::string& hdf5_path)
194 : {
195 1685 : source_id_path_map[source_id] = hdf5_path;
196 1685 : }
197 :
198 : void
199 0 : HDF5SourceIDHandler::add_source_id_geo_id_to_map(source_id_geo_id_map_t& source_id_geo_id_map,
200 : const daqdataformats::SourceID& source_id,
201 : uint64_t geo_id) // NOLINT(build/unsigned)
202 : {
203 0 : if (source_id_geo_id_map.count(source_id) == 0) {
204 0 : std::vector<uint64_t> tmp_vec; // NOLINT(build/unsigned)
205 0 : tmp_vec.push_back(geo_id);
206 0 : source_id_geo_id_map[source_id] = tmp_vec;
207 0 : } else {
208 0 : source_id_geo_id_map[source_id].push_back(geo_id);
209 : }
210 0 : }
211 :
212 : void
213 1580 : HDF5SourceIDHandler::add_fragment_type_source_id_to_map(fragment_type_source_id_map_t& fragment_type_source_id_map,
214 : const daqdataformats::FragmentType fragment_type,
215 : const daqdataformats::SourceID& source_id)
216 : {
217 1580 : if (fragment_type_source_id_map.count(fragment_type) == 0) {
218 255 : std::set<daqdataformats::SourceID> tmp_set;
219 255 : tmp_set.insert(source_id);
220 255 : fragment_type_source_id_map[fragment_type] = tmp_set;
221 255 : } else {
222 1325 : fragment_type_source_id_map[fragment_type].insert(source_id);
223 : }
224 1580 : }
225 :
226 : void
227 1580 : HDF5SourceIDHandler::add_subdetector_source_id_to_map(subdetector_source_id_map_t& subdetector_source_id_map,
228 : const detdataformats::DetID::Subdetector subdetector,
229 : const daqdataformats::SourceID& source_id)
230 : {
231 1580 : if (subdetector_source_id_map.count(subdetector) == 0) {
232 215 : std::set<daqdataformats::SourceID> tmp_set;
233 215 : tmp_set.insert(source_id);
234 215 : subdetector_source_id_map[subdetector] = tmp_set;
235 215 : } else {
236 1365 : subdetector_source_id_map[subdetector].insert(source_id);
237 : }
238 1580 : }
239 :
240 : void
241 460 : HDF5SourceIDHandler::add_subsystem_source_id_to_map(subsystem_source_id_map_t& subsystem_source_id_map,
242 : const daqdataformats::SourceID::Subsystem subsystem,
243 : const daqdataformats::SourceID& source_id)
244 : {
245 460 : if (subsystem_source_id_map.count(subsystem) == 0) {
246 100 : std::set<daqdataformats::SourceID> tmp_set;
247 100 : tmp_set.insert(source_id);
248 100 : subsystem_source_id_map[subsystem] = tmp_set;
249 100 : } else {
250 360 : subsystem_source_id_map[subsystem].insert(source_id);
251 : }
252 460 : }
253 :
254 : std::string
255 105 : HDF5SourceIDHandler::get_json_string(const daqdataformats::SourceID& source_id)
256 : {
257 105 : nlohmann::json json_struct;
258 105 : json_struct["subsys"] = static_cast<uint32_t>(source_id.subsystem); // NOLINT(build/unsigned)
259 105 : json_struct["id"] = source_id.id;
260 210 : return json_struct.dump();
261 105 : }
262 :
263 : std::string
264 105 : HDF5SourceIDHandler::get_json_string(const HDF5SourceIDHandler::source_id_path_map_t& the_map)
265 : {
266 105 : nlohmann::json json_struct;
267 1790 : for (auto const& map_element : the_map) {
268 1685 : nlohmann::json json_element;
269 1685 : json_element["subsys"] = static_cast<uint32_t>(map_element.first.subsystem); // NOLINT(build/unsigned)
270 1685 : json_element["id"] = map_element.first.id;
271 1685 : json_element["path"] = map_element.second;
272 1685 : json_struct["map_entries"].push_back(json_element);
273 1685 : }
274 210 : return json_struct.dump();
275 105 : }
276 :
277 : std::string
278 29 : HDF5SourceIDHandler::get_json_string(const HDF5SourceIDHandler::source_id_geo_id_map_t& the_map)
279 : {
280 29 : nlohmann::json json_struct;
281 323 : for (auto const& map_element : the_map) {
282 294 : nlohmann::json json_geo_id_list;
283 602 : for (auto const& geo_id_from_map : map_element.second) {
284 308 : json_geo_id_list.push_back(geo_id_from_map);
285 : }
286 294 : nlohmann::json json_element;
287 294 : json_element["subsys"] = static_cast<uint32_t>(map_element.first.subsystem); // NOLINT(build/unsigned)
288 294 : json_element["id"] = map_element.first.id;
289 294 : json_element["geoids"] = json_geo_id_list;
290 294 : json_struct["map_entries"].push_back(json_element);
291 294 : }
292 58 : return json_struct.dump();
293 29 : }
294 :
295 : std::string
296 105 : HDF5SourceIDHandler::get_json_string(const HDF5SourceIDHandler::fragment_type_source_id_map_t& the_map)
297 : {
298 105 : nlohmann::json json_struct;
299 360 : for (auto const& map_element : the_map) {
300 255 : nlohmann::json json_source_id_list;
301 1835 : for (auto const& source_id_from_map : map_element.second) {
302 1580 : nlohmann::json json_source_id;
303 1580 : json_source_id["subsys"] = static_cast<uint32_t>(source_id_from_map.subsystem); // NOLINT(build/unsigned)
304 1580 : json_source_id["id"] = source_id_from_map.id;
305 1580 : json_source_id_list.push_back(json_source_id);
306 1580 : }
307 255 : nlohmann::json json_element;
308 255 : json_element["fragment_type"] = static_cast<uint32_t>(map_element.first);
309 255 : json_element["sourceids"] = json_source_id_list;
310 255 : json_struct["map_entries"].push_back(json_element);
311 255 : }
312 210 : return json_struct.dump();
313 105 : }
314 :
315 : std::string
316 105 : HDF5SourceIDHandler::get_json_string(const HDF5SourceIDHandler::subdetector_source_id_map_t& the_map)
317 : {
318 105 : nlohmann::json json_struct;
319 320 : for (auto const& map_element : the_map) {
320 215 : nlohmann::json json_source_id_list;
321 1795 : for (auto const& source_id_from_map : map_element.second) {
322 1580 : nlohmann::json json_source_id;
323 1580 : json_source_id["subsys"] = static_cast<uint32_t>(source_id_from_map.subsystem); // NOLINT(build/unsigned)
324 1580 : json_source_id["id"] = source_id_from_map.id;
325 1580 : json_source_id_list.push_back(json_source_id);
326 1580 : }
327 215 : nlohmann::json json_element;
328 215 : json_element["subdetector"] = static_cast<uint32_t>(map_element.first);
329 215 : json_element["sourceids"] = json_source_id_list;
330 215 : json_struct["map_entries"].push_back(json_element);
331 215 : }
332 210 : return json_struct.dump();
333 105 : }
334 :
335 : void
336 40 : HDF5SourceIDHandler::parse_json_string(const std::string& json_string, daqdataformats::SourceID& source_id)
337 : {
338 40 : nlohmann::json json_struct = nlohmann::json::parse(json_string);
339 40 : daqdataformats::SourceID::Subsystem subsys = static_cast<daqdataformats::SourceID::Subsystem>(json_struct["subsys"]);
340 40 : daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_struct["id"]);
341 40 : source_id.subsystem = subsys;
342 40 : source_id.id = id;
343 40 : }
344 :
345 : void
346 40 : HDF5SourceIDHandler::parse_json_string(const std::string& json_string, source_id_path_map_t& source_id_path_map)
347 : {
348 40 : nlohmann::json json_struct = nlohmann::json::parse(json_string);
349 500 : for (auto const& json_element : json_struct["map_entries"]) {
350 460 : daqdataformats::SourceID::Subsystem subsys =
351 460 : static_cast<daqdataformats::SourceID::Subsystem>(json_element["subsys"]);
352 460 : daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_element["id"]);
353 460 : daqdataformats::SourceID source_id(subsys, id);
354 460 : source_id_path_map[source_id] = json_element["path"];
355 : }
356 40 : }
357 :
358 : void
359 14 : HDF5SourceIDHandler::parse_json_string(const std::string& json_string, source_id_geo_id_map_t& source_id_geo_id_map)
360 : {
361 14 : nlohmann::json json_struct = nlohmann::json::parse(json_string);
362 112 : for (auto const& json_element : json_struct["map_entries"]) {
363 98 : daqdataformats::SourceID::Subsystem subsys =
364 98 : static_cast<daqdataformats::SourceID::Subsystem>(json_element["subsys"]);
365 98 : daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_element["id"]);
366 98 : daqdataformats::SourceID source_id(subsys, id);
367 98 : std::vector<uint64_t> local_geo_id_list; // NOLINT(build/unsigned)
368 98 : nlohmann::json json_geo_id_list = json_element["geoids"];
369 210 : for (nlohmann::json json_geo_id_value : json_geo_id_list) {
370 112 : local_geo_id_list.push_back(json_geo_id_value);
371 112 : }
372 98 : source_id_geo_id_map[source_id] = local_geo_id_list;
373 98 : }
374 14 : }
375 :
376 : void
377 40 : HDF5SourceIDHandler::parse_json_string(const std::string& json_string,
378 : fragment_type_source_id_map_t& fragment_type_source_id_map)
379 : {
380 40 : nlohmann::json json_struct = nlohmann::json::parse(json_string);
381 160 : for (auto const& json_element : json_struct["map_entries"]) {
382 120 : daqdataformats::FragmentType fragment_type =
383 120 : static_cast<daqdataformats::FragmentType>(json_element["fragment_type"]);
384 120 : std::set<daqdataformats::SourceID> local_source_id_list;
385 120 : nlohmann::json json_source_id_list = json_element["sourceids"];
386 540 : for (nlohmann::json json_source_id : json_source_id_list) {
387 420 : daqdataformats::SourceID::Subsystem subsys =
388 420 : static_cast<daqdataformats::SourceID::Subsystem>(json_source_id["subsys"]);
389 420 : daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_source_id["id"]);
390 420 : daqdataformats::SourceID source_id(subsys, id);
391 420 : local_source_id_list.insert(source_id);
392 420 : }
393 120 : fragment_type_source_id_map[fragment_type] = local_source_id_list;
394 120 : }
395 40 : }
396 :
397 : void
398 40 : HDF5SourceIDHandler::parse_json_string(const std::string& json_string,
399 : subdetector_source_id_map_t& subdetector_source_id_map)
400 : {
401 40 : nlohmann::json json_struct = nlohmann::json::parse(json_string);
402 140 : for (auto const& json_element : json_struct["map_entries"]) {
403 100 : detdataformats::DetID::Subdetector subdetector =
404 100 : static_cast<detdataformats::DetID::Subdetector>(json_element["subdetector"]);
405 100 : std::set<daqdataformats::SourceID> local_source_id_list;
406 100 : nlohmann::json json_source_id_list = json_element["sourceids"];
407 520 : for (nlohmann::json json_source_id : json_source_id_list) {
408 420 : daqdataformats::SourceID::Subsystem subsys =
409 420 : static_cast<daqdataformats::SourceID::Subsystem>(json_source_id["subsys"]);
410 420 : daqdataformats::SourceID::ID_t id = static_cast<daqdataformats::SourceID::ID_t>(json_source_id["id"]);
411 420 : daqdataformats::SourceID source_id(subsys, id);
412 420 : local_source_id_list.insert(source_id);
413 420 : }
414 100 : subdetector_source_id_map[subdetector] = local_source_id_list;
415 100 : }
416 40 : }
417 :
418 : } // namespace hdf5libs
419 : } // namespace dunedaq
|