30HDF5RawDataFile::HDF5RawDataFile(std::string file_name,
33 std::string application_name,
36 unsigned compression_level,
37 std::string inprogress_filename_suffix,
39 : m_bare_file_name(file_name)
40 , m_compression_level(compression_level)
41 , m_open_flags(open_flags)
45 if (m_open_flags == HighFive::File::ReadOnly) {
46 throw IncompatibleOpenFlags(
ERS_HERE, file_name, m_open_flags);
49 auto filename_to_open = m_bare_file_name + inprogress_filename_suffix;
53 m_file_ptr.reset(
new HighFive::File(filename_to_open, m_open_flags));
54 }
catch (std::exception
const& excpt) {
59 m_uncompressed_raw_data_size = 0;
60 m_total_file_size = 0;
62 size_t file_creation_timestamp =
63 std::chrono::duration_cast<std::chrono::milliseconds>(system_clock::now().time_since_epoch()).count();
65 TLOG_DEBUG(TLVL_BASIC) <<
"Created HDF5 file (" << file_name <<
") at time " << file_creation_timestamp <<
" .";
68 write_attribute(
"run_number", run_number);
69 write_attribute(
"file_index", file_index);
70 write_attribute(
"creation_timestamp", file_creation_timestamp);
71 write_attribute(
"application_name", application_name);
74 m_file_layout_ptr.reset(
new HDF5FileLayout(fl_params));
79 m_file_level_source_id_geo_id_map = srcid_geoid_map;
84 write_attribute(
"record_type", m_record_type);
87 write_attribute(
"compression_level", m_compression_level);
91HDF5RawDataFile::~HDF5RawDataFile()
93 if (m_file_ptr.get() !=
nullptr && m_open_flags != HighFive::File::ReadOnly) {
94 if (! m_file_ptr->hasAttribute(
"recorded_size")) {
95 write_attribute(
"recorded_size", m_recorded_size);
98 if (! m_file_ptr->hasAttribute(
"uncompressed_raw_data_size")) {
99 write_attribute(
"uncompressed_raw_data_size", m_uncompressed_raw_data_size);
102 if (! m_file_ptr->hasAttribute(
"total_file_size")) {
103 write_attribute(
"total_file_size", m_total_file_size);
106 if (! m_file_ptr->hasAttribute(
"closing_timestamp")) {
107 size_t file_closing_timestamp =
108 std::chrono::duration_cast<std::chrono::milliseconds>(system_clock::now().time_since_epoch()).count();
109 write_attribute(
"closing_timestamp", file_closing_timestamp);
115 if (m_file_ptr->getName() != m_bare_file_name) {
116 std::filesystem::rename(m_file_ptr->getName(), m_bare_file_name);
122 m_file_layout_ptr.reset();
128std::vector<std::string>
129HDF5RawDataFile::HDF5RawDataFile::get_attribute_names()
131 return m_file_ptr->listAttributeNames();
138HDF5RawDataFile::write(
const daqdataformats::TriggerRecord& tr)
142 HDF5SourceIDHandler::source_id_path_map_t source_id_path_map;
145 HDF5SourceIDHandler::fragment_type_source_id_map_t fragment_type_source_id_map;
148 HDF5SourceIDHandler::subdetector_source_id_map_t subdetector_source_id_map;
151 HighFive::Group record_level_group =
write(tr.get_header_ref(), source_id_path_map);
155 for (
auto const& source_id_path : source_id_path_map) {
156 HDF5SourceIDHandler::store_record_header_source_id(record_level_group, source_id_path.first);
160 for (
auto const& frag_ptr : tr.get_fragments_ref()) {
161 write(*frag_ptr, source_id_path_map);
162 HDF5SourceIDHandler::add_fragment_type_source_id_to_map(
163 fragment_type_source_id_map, frag_ptr->get_fragment_type(), frag_ptr->get_element_id());
164 HDF5SourceIDHandler::add_subdetector_source_id_to_map(
165 subdetector_source_id_map,
167 frag_ptr->get_element_id());
171 HDF5SourceIDHandler::store_record_level_path_info(record_level_group, source_id_path_map);
172 HDF5SourceIDHandler::store_record_level_fragment_type_map(record_level_group, fragment_type_source_id_map);
173 HDF5SourceIDHandler::store_record_level_subdetector_map(record_level_group, subdetector_source_id_map);
180HDF5RawDataFile::write(
const daqdataformats::TimeSlice&
ts)
182 std::string tsh_path = m_file_layout_ptr->get_path_string(
ts.get_header());
183 if (m_file_ptr->exist(tsh_path)) {
throw TimeSliceAlreadyExists(
ERS_HERE, tsh_path);}
187 HDF5SourceIDHandler::source_id_path_map_t source_id_path_map;
190 HDF5SourceIDHandler::fragment_type_source_id_map_t fragment_type_source_id_map;
193 HDF5SourceIDHandler::subdetector_source_id_map_t subdetector_source_id_map;
196 HighFive::Group record_level_group =
write(
ts.get_header(), source_id_path_map);
200 for (
auto const& source_id_path : source_id_path_map) {
201 HDF5SourceIDHandler::store_record_header_source_id(record_level_group, source_id_path.first);
205 for (
auto const& frag_ptr :
ts.get_fragments_ref()) {
206 write(*frag_ptr, source_id_path_map);
207 HDF5SourceIDHandler::add_fragment_type_source_id_to_map(
208 fragment_type_source_id_map, frag_ptr->get_fragment_type(), frag_ptr->get_element_id());
209 HDF5SourceIDHandler::add_subdetector_source_id_to_map(
210 subdetector_source_id_map,
212 frag_ptr->get_element_id());
216 HDF5SourceIDHandler::store_record_level_path_info(record_level_group, source_id_path_map);
217 HDF5SourceIDHandler::store_record_level_fragment_type_map(record_level_group, fragment_type_source_id_map);
218 HDF5SourceIDHandler::store_record_level_subdetector_map(record_level_group, subdetector_source_id_map);
225HDF5RawDataFile::write(
const daqdataformats::TriggerRecordHeader& trh,
226 HDF5SourceIDHandler::source_id_path_map_t& path_map)
228 std::tuple<size_t, std::string, HighFive::Group> write_results =
229 do_write(m_file_layout_ptr->get_path_elements(trh),
230 static_cast<const char*
>(trh.get_storage_location()),
231 trh.get_total_size_bytes(),
232 m_compression_level);
233 m_recorded_size += std::get<0>(write_results);
234 HDF5SourceIDHandler::add_source_id_path_to_map(path_map, trh.get_header().element_id, std::get<1>(write_results));
235 return std::get<2>(write_results);
242HDF5RawDataFile::write(
const daqdataformats::TimeSliceHeader& tsh, HDF5SourceIDHandler::source_id_path_map_t& path_map)
244 std::tuple<size_t, std::string, HighFive::Group> write_results =
245 do_write(m_file_layout_ptr->get_path_elements(tsh),
247 sizeof(daqdataformats::TimeSliceHeader),
248 m_compression_level);
249 m_recorded_size += std::get<0>(write_results);
250 HDF5SourceIDHandler::add_source_id_path_to_map(path_map, tsh.element_id, std::get<1>(write_results));
251 return std::get<2>(write_results);
258HDF5RawDataFile::write(
const daqdataformats::Fragment& frag, HDF5SourceIDHandler::source_id_path_map_t& path_map)
260 std::tuple<size_t, std::string, HighFive::Group> write_results =
261 do_write(m_file_layout_ptr->get_path_elements(frag.get_header()),
262 static_cast<const char*
>(frag.get_storage_location()),
264 m_compression_level);
265 m_recorded_size += std::get<0>(write_results);
267 daqdataformats::SourceID source_id = frag.get_element_id();
268 HDF5SourceIDHandler::add_source_id_path_to_map(path_map, source_id, std::get<1>(write_results));
275HDF5RawDataFile::write_file_layout()
277 auto fl_json = m_file_layout_ptr->get_file_layout_params().to_json();
278 write_attribute(
"filelayout_params", fl_json.dump());
279 write_attribute(
"filelayout_version", m_file_layout_ptr->get_version());
285std::tuple<size_t, std::string, HighFive::Group>
286HDF5RawDataFile::do_write(std::vector<std::string>
const& group_and_dataset_path_elements,
287 const char* raw_data_ptr,
288 size_t raw_data_size_bytes,
289 unsigned compression_level)
291 const std::string dataset_name = group_and_dataset_path_elements.back();
294 std::string
const& top_level_group_name = group_and_dataset_path_elements.at(0);
295 if (!m_file_ptr->exist(top_level_group_name))
296 m_file_ptr->createGroup(top_level_group_name);
299 HighFive::Group sub_group = m_file_ptr->getGroup(top_level_group_name);
300 if (!sub_group.isValid()) {
301 throw InvalidHDF5Group(
ERS_HERE, top_level_group_name);
303 HighFive::Group top_level_group = sub_group;
306 for (
size_t idx = 1; idx < group_and_dataset_path_elements.size() - 1; ++idx) {
308 std::string
const& child_group_name = group_and_dataset_path_elements[idx];
309 if (child_group_name.empty()) {
310 throw InvalidHDF5Group(
ERS_HERE, child_group_name);
312 if (!sub_group.exist(child_group_name)) {
313 sub_group.createGroup(child_group_name);
315 HighFive::Group child_group = sub_group.getGroup(child_group_name);
316 if (!child_group.isValid()) {
317 throw InvalidHDF5Group(
ERS_HERE, child_group_name);
319 sub_group = child_group;
323 HighFive::DataSpace data_space = HighFive::DataSpace({ raw_data_size_bytes, 1 });
324 HighFive::DataSetCreateProps data_set_create_props;
325 HighFive::DataSetAccessProps data_set_access_props;
327 if (compression_level > 0) {
328 std::vector<hsize_t> chunk_size = {raw_data_size_bytes, 1};
329 data_set_create_props.add(HighFive::Chunking(chunk_size));
330 data_set_create_props.add(HighFive::Deflate(compression_level));
333 m_uncompressed_raw_data_size += raw_data_size_bytes;
335 auto data_set = sub_group.createDataSet<
char>(dataset_name, data_space, data_set_create_props, data_set_access_props);
337 if (data_set.isValid()) {
338 data_set.write_raw(raw_data_ptr);
339 m_total_file_size = m_file_ptr->getFileSize();
341 return std::make_tuple(data_set.getStorageSize(), data_set.getPath(), top_level_group);
343 throw InvalidHDF5Dataset(
ERS_HERE, dataset_name, m_file_ptr->getName());
350HDF5RawDataFile::HDF5RawDataFile(
const std::string& file_name,
bool allow_writing)
351 : m_open_flags(HighFive::File::ReadOnly)
353 if (allow_writing) {m_open_flags = HighFive::File::ReadWrite;}
354 m_bare_file_name = file_name;
355 size_t pos = m_bare_file_name.rfind(s_inprogress_suffix);
356 if (pos != std::string::npos) {
357 m_bare_file_name.erase(pos);
362 m_file_ptr = std::make_unique<HighFive::File>(file_name, m_open_flags);
363 }
catch (std::exception
const& excpt) {
367 if (m_file_ptr->hasAttribute(
"recorded_size"))
368 m_recorded_size = get_attribute<size_t>(
"recorded_size");
372 if (m_file_ptr->hasAttribute(
"uncompressed_raw_data_size"))
373 m_uncompressed_raw_data_size = get_attribute<size_t>(
"uncompressed_raw_data_size");
375 m_uncompressed_raw_data_size = 0;
377 if (m_file_ptr->hasAttribute(
"total_file_size"))
378 m_total_file_size = get_attribute<size_t>(
"total_file_size");
380 m_total_file_size = 0;
382 if (m_file_ptr->hasAttribute(
"compression_level"))
383 m_compression_level = get_attribute<unsigned>(
"compression_level");
385 m_compression_level = 0;
389 if (m_file_ptr->hasAttribute(
"record_type"))
390 m_record_type = get_attribute<std::string>(
"record_type");
392 m_record_type = m_file_layout_ptr->get_record_name_prefix();
398 HDF5SourceIDHandler sid_handler(get_version());
399 sid_handler.fetch_file_level_geo_id_info(*m_file_ptr, m_file_level_source_id_geo_id_map);
403HDF5RawDataFile::read_file_layout()
405 HDF5FileLayoutParameters fl_params;
410 fl_str = get_attribute<std::string>(
"filelayout_params");
411 nlohmann::json fl_json = nlohmann::json::parse(fl_str);
412 fl_params = HDF5FileLayoutParameters(fl_json);
414 version = get_attribute<uint32_t>(
"filelayout_version");
416 }
catch (InvalidHDF5Attribute
const&) {
421 m_file_layout_ptr.reset(
new HDF5FileLayout(fl_params,
version));
425HDF5RawDataFile::check_file_layout()
427 if (get_version() < 2)
430 std::string record_type = get_attribute<std::string>(
"record_type");
431 if (record_type.compare(m_file_layout_ptr->get_record_name_prefix()) != 0)
432 throw BadRecordType(
ERS_HERE, record_type, m_file_layout_ptr->get_record_name_prefix());
436HDF5RawDataFile::check_record_type(std::string rt_name)
438 if (get_version() < 2)
441 if (m_file_layout_ptr->get_record_name_prefix().compare(rt_name) != 0)
442 throw WrongRecordTypeRequested(
ERS_HERE, rt_name, m_file_layout_ptr->get_record_name_prefix());
447HDF5RawDataFile::explore_subgroup(
const HighFive::Group& parent_group,
448 std::string relative_path,
449 std::vector<std::string>& path_list)
451 if (relative_path.size() > 0 && relative_path.compare(relative_path.size() - 1, 1,
"/") == 0)
452 relative_path.pop_back();
454 std::vector<std::string> childNames = parent_group.listObjectNames();
456 for (
auto& child_name : childNames) {
457 std::string full_path = relative_path +
"/" + child_name;
458 HighFive::ObjectType child_type = parent_group.getObjectType(child_name);
460 if (child_type == HighFive::ObjectType::Dataset) {
461 path_list.push_back(full_path);
462 }
else if (child_type == HighFive::ObjectType::Group) {
463 HighFive::Group child_group = parent_group.getGroup(child_name);
465 std::string new_path = relative_path +
"/" + child_name;
466 explore_subgroup(child_group, new_path, path_list);
472HDF5RawDataFile::add_record_level_info_to_caches_if_needed(record_id_t rid)
478 if (m_source_id_path_cache.count(rid) != 0) {
483 HDF5SourceIDHandler sid_handler(get_version());
486 std::string record_level_group_name = m_file_layout_ptr->get_record_number_string(rid.first, rid.second);
487 HighFive::Group record_group = m_file_ptr->getGroup(record_level_group_name);
488 if (!record_group.isValid()) {
489 throw InvalidHDF5Group(
ERS_HERE, record_level_group_name);
494 HDF5SourceIDHandler::source_id_geo_id_map_t local_source_id_geo_id_map = m_file_level_source_id_geo_id_map;
495 sid_handler.fetch_record_level_geo_id_info(record_group, local_source_id_geo_id_map);
498 HDF5SourceIDHandler::source_id_path_map_t source_id_path_map;
499 sid_handler.fetch_source_id_path_info(record_group, source_id_path_map);
502 HDF5SourceIDHandler::fragment_type_source_id_map_t fragment_type_source_id_map;
503 sid_handler.fetch_fragment_type_source_id_info(record_group, fragment_type_source_id_map);
506 HDF5SourceIDHandler::subdetector_source_id_map_t subdetector_source_id_map;
507 sid_handler.fetch_subdetector_source_id_info(record_group, subdetector_source_id_map);
510 daqdataformats::SourceID rh_sid = sid_handler.fetch_record_header_source_id(record_group);
511 std::set<daqdataformats::SourceID> full_source_id_set;
512 std::set<daqdataformats::SourceID> fragment_source_id_set;
513 HDF5SourceIDHandler::subsystem_source_id_map_t subsystem_source_id_map;
514 for (
auto const& source_id_path : source_id_path_map) {
515 full_source_id_set.insert(source_id_path.first);
516 if (source_id_path.first != rh_sid) {
517 fragment_source_id_set.insert(source_id_path.first);
519 HDF5SourceIDHandler::add_subsystem_source_id_to_map(
520 subsystem_source_id_map, source_id_path.first.subsystem, source_id_path.first);
526 m_source_id_cache[rid] = full_source_id_set;
527 m_record_header_source_id_cache[rid] = rh_sid;
528 m_fragment_source_id_cache[rid] = fragment_source_id_set;
529 m_source_id_geo_id_cache[rid] = local_source_id_geo_id_map;
530 m_source_id_path_cache[rid] = source_id_path_map;
531 m_subsystem_source_id_cache[rid] = subsystem_source_id_map;
532 m_fragment_type_source_id_cache[rid] = fragment_type_source_id_map;
533 m_subdetector_source_id_cache[rid] = subdetector_source_id_map;
539std::vector<std::string>
540HDF5RawDataFile::get_dataset_paths(std::string top_level_group_name)
542 if (top_level_group_name.empty())
543 top_level_group_name = m_file_ptr->getPath();
546 std::vector<std::string> path_list;
548 HighFive::Group parent_group = m_file_ptr->getGroup(top_level_group_name);
549 if (!parent_group.isValid())
550 throw InvalidHDF5Group(
ERS_HERE, top_level_group_name);
552 explore_subgroup(parent_group, top_level_group_name, path_list);
560HDF5RawDataFile::record_id_set
561HDF5RawDataFile::get_all_record_ids()
563 if (!m_all_record_ids_in_file.empty())
564 return m_all_record_ids_in_file;
568 HighFive::Group parent_group = m_file_ptr->getGroup(m_file_ptr->getPath());
570 std::vector<std::string> childNames = parent_group.listObjectNames();
571 const std::string record_prefix = m_file_layout_ptr->get_record_name_prefix();
572 const size_t record_prefix_size = record_prefix.size();
574 for (
auto const& name : childNames) {
575 auto loc = name.find(record_prefix);
577 if (loc == std::string::npos)
580 auto rec_num_string = name.substr(loc + record_prefix_size);
582 loc = rec_num_string.find(
".");
583 if (loc == std::string::npos) {
584 m_all_record_ids_in_file.insert(std::make_pair(std::stoll(rec_num_string), 0));
586 auto seq_num_string = rec_num_string.substr(loc + 1);
587 rec_num_string.resize(loc);
588 m_all_record_ids_in_file.insert(std::make_pair(std::stoll(rec_num_string), std::stoi(seq_num_string)));
593 return m_all_record_ids_in_file;
597HDF5RawDataFile::get_all_record_numbers()
600 "get_all_record_numbers()",
601 "Use get_all_record_ids(), which returns a record_number,sequence_number pair."));
603 std::set<uint64_t> record_numbers;
604 for (
auto const& rid : get_all_record_ids())
605 record_numbers.insert(rid.first);
607 return record_numbers;
610HDF5RawDataFile::record_id_set
611HDF5RawDataFile::get_all_trigger_record_ids()
613 check_record_type(
"TriggerRecord");
614 return get_all_record_ids();
617std::set<daqdataformats::trigger_number_t>
618HDF5RawDataFile::get_all_trigger_record_numbers()
622 "get_all_trigger_record_numbers()",
623 "Use get_all_trigger_record_ids(), which returns a record_number,sequence_number pair."));
625 return get_all_record_numbers();
628HDF5RawDataFile::record_id_set
629HDF5RawDataFile::get_all_timeslice_ids()
631 check_record_type(
"TimeSlice");
632 return get_all_record_ids();
635std::set<daqdataformats::timeslice_number_t>
636HDF5RawDataFile::get_all_timeslice_numbers()
638 check_record_type(
"TimeSlice");
639 return get_all_record_numbers();
645std::vector<std::string>
646HDF5RawDataFile::get_record_header_dataset_paths()
649 std::vector<std::string> rec_paths;
651 if (get_version() >= 2) {
652 for (
auto const& rec_id : get_all_record_ids())
653 rec_paths.push_back(get_record_header_dataset_path(rec_id));
655 for (
auto const& path : get_dataset_paths()) {
656 if (path.find(m_file_layout_ptr->get_record_header_dataset_name()) != std::string::npos) {
657 rec_paths.push_back(path);
665std::vector<std::string>
666HDF5RawDataFile::get_trigger_record_header_dataset_paths()
668 check_record_type(
"TriggerRecord");
669 return get_record_header_dataset_paths();
672std::vector<std::string>
673HDF5RawDataFile::get_timeslice_header_dataset_paths()
675 check_record_type(
"TimeSlice");
676 return get_record_header_dataset_paths();
680HDF5RawDataFile::get_record_header_dataset_path(
const record_id_t& rid)
682 auto rec_id = get_all_record_ids().find(rid);
683 if (rec_id == get_all_record_ids().end())
684 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
686 if (get_version() <= 2) {
687 return (m_file_ptr->getPath() + m_file_layout_ptr->get_record_header_path(rid.first, rid.second));
689 daqdataformats::SourceID source_id = get_record_header_source_id(rid);
690 return m_source_id_path_cache[rid][source_id];
695HDF5RawDataFile::get_record_header_dataset_path(
const uint64_t rec_num,
698 return get_record_header_dataset_path(std::make_pair(rec_num, seq_num));
702HDF5RawDataFile::get_trigger_record_header_dataset_path(
const record_id_t& rid)
704 check_record_type(
"TriggerRecord");
705 return get_record_header_dataset_path(rid);
712 check_record_type(
"TriggerRecord");
713 return get_record_header_dataset_path(trig_num, seq_num);
717HDF5RawDataFile::get_timeslice_header_dataset_path(
const record_id_t& rid)
719 check_record_type(
"TimeSlice");
720 return get_record_header_dataset_path(rid.first, 0);
726 check_record_type(
"TimeSlice");
727 return get_record_header_dataset_path(ts_num);
736std::vector<std::string>
737HDF5RawDataFile::get_all_fragment_dataset_paths()
739 std::vector<std::string> frag_paths;
741 for (
auto const& path : get_dataset_paths()) {
742 if (path.find(m_file_layout_ptr->get_record_header_dataset_name()) == std::string::npos)
743 frag_paths.push_back(path);
750std::vector<std::string>
751HDF5RawDataFile::get_fragment_dataset_paths(
const record_id_t& rid)
753 auto rec_id = get_all_record_ids().find(rid);
754 if (rec_id == get_all_record_ids().end())
755 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
757 std::vector<std::string> frag_paths;
758 if (get_version() <= 2) {
759 std::string record_group_path =
760 m_file_ptr->getPath() + m_file_layout_ptr->get_record_number_string(rid.first, rid.second);
762 for (
auto const& path : get_dataset_paths(record_group_path)) {
763 if (path.find(m_file_layout_ptr->get_record_header_dataset_name()) == std::string::npos)
764 frag_paths.push_back(path);
767 std::set<daqdataformats::SourceID> source_id_list = get_fragment_source_ids(rid);
768 for (
auto const& source_id : source_id_list) {
769 frag_paths.push_back(m_source_id_path_cache[rid][source_id]);
776std::vector<std::string>
777HDF5RawDataFile::get_fragment_dataset_paths(
const uint64_t rec_num,
780 return get_fragment_dataset_paths(std::make_pair(rec_num, seq_num));
784std::vector<std::string>
787 std::vector<std::string> frag_paths;
788 for (
auto const& rid : get_all_record_ids()) {
789 if (get_version() <= 2) {
790 auto datasets = get_dataset_paths(m_file_ptr->getPath() +
791 m_file_layout_ptr->get_fragment_type_path(rid.first, rid.second, subsystem));
792 frag_paths.insert(frag_paths.end(), datasets.begin(), datasets.end());
794 std::set<daqdataformats::SourceID> source_id_list = get_source_ids_for_subsystem(rid, subsystem);
795 for (
auto const& source_id : source_id_list) {
796 frag_paths.push_back(m_source_id_path_cache[rid][source_id]);
803std::vector<std::string>
804HDF5RawDataFile::get_fragment_dataset_paths(
const std::string& subsystem_name)
807 return get_fragment_dataset_paths(subsystem);
810std::vector<std::string>
813 auto rec_id = get_all_record_ids().find(rid);
814 if (rec_id == get_all_record_ids().end())
815 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
817 if (get_version() <= 2) {
818 return get_dataset_paths(m_file_ptr->getPath() +
819 m_file_layout_ptr->get_fragment_type_path(rid.first, rid.second, subsystem));
821 std::vector<std::string> frag_paths;
822 std::set<daqdataformats::SourceID> source_id_list = get_source_ids_for_subsystem(rid, subsystem);
823 for (
auto const& source_id : source_id_list) {
824 frag_paths.push_back(m_source_id_path_cache[rid][source_id]);
830std::vector<std::string>
831HDF5RawDataFile::get_fragment_dataset_paths(
const record_id_t& rid,
const std::string& subsystem_name)
834 return get_fragment_dataset_paths(rid, subsystem);
839std::vector<std::string>
840HDF5RawDataFile::get_fragment_dataset_paths(
const daqdataformats::SourceID& source_id)
842 std::vector<std::string> frag_paths;
844 for (
auto const& rid : get_all_record_ids())
845 frag_paths.push_back(m_file_ptr->getPath() +
846 m_file_layout_ptr->get_fragment_path(rid.first, rid.second, source_id));
851std::vector<std::string>
855 return get_fragment_dataset_paths(daqdataformats::SourceID(type, source_id));
857std::vector<std::string>
858HDF5RawDataFile::get_fragment_dataset_paths(
const std::string& typestring,
861 return get_fragment_dataset_paths(
865std::set<daqdataformats::SourceID>
866HDF5RawDataFile::get_source_ids(std::vector<std::string>
const& frag_dataset_paths)
868 std::set<daqdataformats::SourceID> source_ids;
869 std::vector<std::string> path_elements;
871 for (
auto const& frag_dataset : frag_dataset_paths) {
872 path_elements.clear();
873 std::istringstream iss(frag_dataset);
874 while (std::getline(iss, s,
'/')) {
876 path_elements.push_back(s);
878 source_ids.insert(m_file_layout_ptr->get_source_id_from_path_elements(path_elements));
885HDF5SourceIDHandler::source_id_geo_id_map_t
886HDF5RawDataFile::get_srcid_geoid_map()
const {
888 return m_file_level_source_id_geo_id_map;
892HDF5RawDataFile::get_all_geo_ids()
const
894 std::set<uint64_t> set_of_geo_ids;
900 for (
auto const& map_entry : m_file_level_source_id_geo_id_map) {
901 for (
auto const& geo_id : map_entry.second) {
902 set_of_geo_ids.insert(geo_id);
905 return set_of_geo_ids;
909HDF5RawDataFile::get_geo_ids(
const record_id_t& rid)
911 auto rec_id = get_all_record_ids().find(rid);
912 if (rec_id == get_all_record_ids().end())
913 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
915 add_record_level_info_to_caches_if_needed(rid);
917 std::set<uint64_t> set_of_geo_ids;
918 for (
auto const& map_entry : m_source_id_geo_id_cache[rid]) {
919 for (
auto const& geo_id : map_entry.second) {
920 set_of_geo_ids.insert(geo_id);
923 return set_of_geo_ids;
927HDF5RawDataFile::get_geo_ids_for_subdetector(
const record_id_t& rid,
930 auto rec_id = get_all_record_ids().find(rid);
931 if (rec_id == get_all_record_ids().end())
932 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
934 add_record_level_info_to_caches_if_needed(rid);
936 std::set<uint64_t> set_of_geo_ids;
937 for (
auto const& map_entry : m_source_id_geo_id_cache[rid]) {
938 for (
auto const& geo_id : map_entry.second) {
941 uint16_t
det_id = 0xffff & geo_id;
942 if (det_id ==
static_cast<uint16_t
>(subdet)) {
943 set_of_geo_ids.insert(geo_id);
947 return set_of_geo_ids;
952std::set<daqdataformats::SourceID>
953HDF5RawDataFile::get_source_ids(
const record_id_t& rid)
955 auto rec_id = get_all_record_ids().find(rid);
956 if (rec_id == get_all_record_ids().end())
957 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
959 add_record_level_info_to_caches_if_needed(rid);
961 return m_source_id_cache[rid];
964daqdataformats::SourceID
965HDF5RawDataFile::get_record_header_source_id(
const record_id_t& rid)
967 auto rec_id = get_all_record_ids().find(rid);
968 if (rec_id == get_all_record_ids().end())
969 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
971 add_record_level_info_to_caches_if_needed(rid);
973 return m_record_header_source_id_cache[rid];
976std::set<daqdataformats::SourceID>
977HDF5RawDataFile::get_fragment_source_ids(
const record_id_t& rid)
979 auto rec_id = get_all_record_ids().find(rid);
980 if (rec_id == get_all_record_ids().end())
981 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
983 add_record_level_info_to_caches_if_needed(rid);
985 return m_fragment_source_id_cache[rid];
988std::set<daqdataformats::SourceID>
989HDF5RawDataFile::get_source_ids_for_subsystem(
const record_id_t& rid,
992 auto rec_id = get_all_record_ids().find(rid);
993 if (rec_id == get_all_record_ids().end())
994 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
996 add_record_level_info_to_caches_if_needed(rid);
998 return m_subsystem_source_id_cache[rid][subsystem];
1001std::set<daqdataformats::SourceID>
1004 auto rec_id = get_all_record_ids().find(rid);
1005 if (rec_id == get_all_record_ids().end())
1006 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1008 add_record_level_info_to_caches_if_needed(rid);
1010 return m_fragment_type_source_id_cache[rid][frag_type];
1013std::set<daqdataformats::SourceID>
1014HDF5RawDataFile::get_source_ids_for_fragtype_and_subdetector(
const record_id_t& rid,
1015 const std::string& frag_type_name,
1016 const std::string& subdet_name)
1020 throw InvalidFragmentTypeString(
ERS_HERE, frag_type_name);
1023 throw InvalidSubdetectorString(
ERS_HERE, subdet_name);
1025 auto rec_id = get_all_record_ids().find(rid);
1026 if (rec_id == get_all_record_ids().end())
1027 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1029 add_record_level_info_to_caches_if_needed(rid);
1031 std::set<daqdataformats::SourceID> fragtype_match_sids = m_fragment_type_source_id_cache[rid][frag_type];
1032 std::set<daqdataformats::SourceID> detid_match_sids = m_subdetector_source_id_cache[rid][subdet];
1033 std::set<daqdataformats::SourceID> combined_set_sids;
1034 for (
auto ftsid : fragtype_match_sids) {
1035 if (detid_match_sids.contains(ftsid)) {
1036 combined_set_sids.insert(ftsid);
1039 return combined_set_sids;
1042std::set<daqdataformats::SourceID>
1045 auto rec_id = get_all_record_ids().find(rid);
1046 if (rec_id == get_all_record_ids().end())
1047 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1049 add_record_level_info_to_caches_if_needed(rid);
1051 return m_subdetector_source_id_cache[rid][subdet];
1054std::unique_ptr<char[]>
1055HDF5RawDataFile::get_dataset_raw_data(
const std::string& dataset_path)
1057 HighFive::Group parent_group = m_file_ptr->getGroup(
"/");
1058 HighFive::DataSet data_set = parent_group.getDataSet(dataset_path);
1060 if (!data_set.isValid())
1061 throw InvalidHDF5Dataset(
ERS_HERE, dataset_path, get_file_name());
1063 size_t data_size = data_set.getSpace().getElementCount() *
sizeof(char);
1065 auto membuffer = std::make_unique<char[]>(data_size);
1067 data_set.read(membuffer.get());
1071std::unique_ptr<daqdataformats::Fragment>
1072HDF5RawDataFile::get_frag_ptr(
const std::string& dataset_name)
1074 auto membuffer = get_dataset_raw_data(dataset_name);
1075 auto frag_ptr = std::make_unique<daqdataformats::Fragment>(
1080std::unique_ptr<daqdataformats::Fragment>
1081HDF5RawDataFile::get_frag_ptr(
const record_id_t& rid,
const daqdataformats::SourceID& source_id)
1083 if (get_version() < 2)
1084 throw IncompatibleFileLayoutVersion(
ERS_HERE, get_version(), 2, MAX_FILELAYOUT_VERSION);
1086 auto rec_id = get_all_record_ids().find(rid);
1087 if (rec_id == get_all_record_ids().end())
1088 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1090 add_record_level_info_to_caches_if_needed(rid);
1092 return get_frag_ptr(m_source_id_path_cache[rid][source_id]);
1095std::unique_ptr<daqdataformats::Fragment>
1096HDF5RawDataFile::get_frag_ptr(
const uint64_t rec_num,
1098 const daqdataformats::SourceID& source_id)
1100 record_id_t rid = std::make_pair(rec_num, seq_num);
1101 return get_frag_ptr(rid, source_id);
1104std::unique_ptr<daqdataformats::Fragment>
1105HDF5RawDataFile::get_frag_ptr(
const record_id_t& rid,
1109 daqdataformats::SourceID source_id(type,
id);
1110 return get_frag_ptr(rid, source_id);
1113std::unique_ptr<daqdataformats::Fragment>
1114HDF5RawDataFile::get_frag_ptr(
const uint64_t rec_num,
1119 record_id_t rid = std::make_pair(rec_num, seq_num);
1120 daqdataformats::SourceID source_id(type,
id);
1121 return get_frag_ptr(rid, source_id);
1124std::unique_ptr<daqdataformats::Fragment>
1125HDF5RawDataFile::get_frag_ptr(
const record_id_t& rid,
1126 const std::string& typestring,
1130 return get_frag_ptr(rid, source_id);
1133std::unique_ptr<daqdataformats::Fragment>
1134HDF5RawDataFile::get_frag_ptr(
const uint64_t rec_num,
1136 const std::string& typestring,
1139 record_id_t rid = std::make_pair(rec_num, seq_num);
1141 return get_frag_ptr(rid, source_id);
1144std::unique_ptr<daqdataformats::Fragment>
1145HDF5RawDataFile::get_frag_ptr(
const record_id_t& rid,
1146 const uint64_t geo_id)
1148 daqdataformats::SourceID sid = get_source_id_for_geo_id(rid, geo_id);
1149 return get_frag_ptr(rid, sid);
1152std::unique_ptr<daqdataformats::Fragment>
1153HDF5RawDataFile::get_frag_ptr(
const uint64_t rec_num,
1155 const uint64_t geo_id)
1157 record_id_t rid = std::make_pair(rec_num, seq_num);
1158 return get_frag_ptr(rid, geo_id);
1161std::unique_ptr<daqdataformats::TriggerRecordHeader>
1162HDF5RawDataFile::get_trh_ptr(
const std::string& dataset_name)
1164 auto membuffer = get_dataset_raw_data(dataset_name);
1165 auto trh_ptr = std::make_unique<daqdataformats::TriggerRecordHeader>(membuffer.release(),
true);
1169std::unique_ptr<daqdataformats::TriggerRecordHeader>
1170HDF5RawDataFile::get_trh_ptr(
const record_id_t& rid)
1172 if (get_version() < 2)
1173 throw IncompatibleFileLayoutVersion(
ERS_HERE, get_version(), 2, MAX_FILELAYOUT_VERSION);
1175 auto rec_id = get_all_record_ids().find(rid);
1176 if (rec_id == get_all_record_ids().end())
1177 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1179 add_record_level_info_to_caches_if_needed(rid);
1181 daqdataformats::SourceID rh_source_id = m_record_header_source_id_cache[rid];
1182 return get_trh_ptr(m_source_id_path_cache[rid][rh_source_id]);
1185std::unique_ptr<daqdataformats::TimeSliceHeader>
1186HDF5RawDataFile::get_tsh_ptr(
const std::string& dataset_name)
1188 auto membuffer = get_dataset_raw_data(dataset_name);
1189 auto tsh_ptr = std::make_unique<daqdataformats::TimeSliceHeader>(
1190 *(
reinterpret_cast<daqdataformats::TimeSliceHeader*
>(membuffer.release())));
1194std::unique_ptr<daqdataformats::TimeSliceHeader>
1195HDF5RawDataFile::get_tsh_ptr(
const record_id_t& rid)
1197 if (get_version() < 2)
1198 throw IncompatibleFileLayoutVersion(
ERS_HERE, get_version(), 2, MAX_FILELAYOUT_VERSION);
1200 auto rec_id = get_all_record_ids().find(rid);
1201 if (rec_id == get_all_record_ids().end())
1202 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1204 add_record_level_info_to_caches_if_needed(rid);
1206 daqdataformats::SourceID rh_source_id = m_record_header_source_id_cache[rid];
1207 return get_tsh_ptr(m_source_id_path_cache[rid][rh_source_id]);
1210daqdataformats::TriggerRecord
1211HDF5RawDataFile::get_trigger_record(
const record_id_t& rid)
1213 daqdataformats::TriggerRecord trigger_record(*get_trh_ptr(rid));
1214 for (
auto const& frag_path : get_fragment_dataset_paths(rid)) {
1215 trigger_record.add_fragment(get_frag_ptr(frag_path));
1218 return trigger_record;
1221daqdataformats::TimeSlice
1224 daqdataformats::TimeSlice timeslice(*get_tsh_ptr(ts_num));
1225 for (
auto const& frag_path : get_fragment_dataset_paths(ts_num)) {
1226 timeslice.add_fragment(get_frag_ptr(frag_path));
1232std::vector<uint64_t>
1233HDF5RawDataFile::get_geo_ids_for_source_id(
const record_id_t& rid,
const daqdataformats::SourceID& source_id)
1235 auto rec_id = get_all_record_ids().find(rid);
1236 if (rec_id == get_all_record_ids().end())
1237 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1239 add_record_level_info_to_caches_if_needed(rid);
1241 return m_source_id_geo_id_cache[rid][source_id];
1244daqdataformats::SourceID
1245HDF5RawDataFile::get_source_id_for_geo_id(
const record_id_t& rid,
1246 const uint64_t requested_geo_id)
1248 auto rec_id = get_all_record_ids().find(rid);
1249 if (rec_id == get_all_record_ids().end())
1250 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1252 add_record_level_info_to_caches_if_needed(rid);
1256 for (
auto const& map_entry : m_source_id_geo_id_cache[rid]) {
1257 auto geoid_list = map_entry.second;
1258 for (
auto const& geoid_from_list : geoid_list) {
1259 if (geoid_from_list == requested_geo_id) {
1260 return map_entry.first;
1265 daqdataformats::SourceID empty_sid;
static void store_file_level_geo_id_info(HighFive::File &h5_file, const source_id_geo_id_map_t &the_map)
std::map< daqdataformats::SourceID, std::vector< uint64_t > > source_id_geo_id_map_t
#define TLOG_DEBUG(lvl,...)
constexpr uint32_t MAX_FILELAYOUT_VERSION
PDS Frame with unphysical timestamp detected with ts
void warning(const Issue &issue)
void info(const Issue &issue)
std::string record_name_prefix