LCOV - code coverage report
Current view: top level - hdf5libs/src - HDF5SourceIDHandler.cpp (source / functions) Coverage Total Hit
Test: code.result Lines: 91.9 % 260 239
Test Date: 2026-10-11 15:05:20 Functions: 90.6 % 32 29

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

Generated by: LCOV version 2.0-1