30HDF5RawDataFile::HDF5RawDataFile(
const std::string file_name,
32 const size_t file_index,
33 const std::string application_name,
36 const unsigned compression_level,
37 const std::string inprogress_filename_suffix,
38 const unsigned open_flags)
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);
90HDF5RawDataFile::~HDF5RawDataFile()
92 if (m_file_ptr.get() !=
nullptr && m_open_flags != HighFive::File::ReadOnly) {
93 if (!m_file_ptr->hasAttribute(
"recorded_size")) {
94 write_attribute(
"recorded_size", m_recorded_size);
97 if (!m_file_ptr->hasAttribute(
"uncompressed_raw_data_size")) {
98 write_attribute(
"uncompressed_raw_data_size", m_uncompressed_raw_data_size);
101 if (!m_file_ptr->hasAttribute(
"total_file_size")) {
102 write_attribute(
"total_file_size", m_total_file_size);
105 if (!m_file_ptr->hasAttribute(
"closing_timestamp")) {
106 size_t file_closing_timestamp =
107 std::chrono::duration_cast<std::chrono::milliseconds>(system_clock::now().time_since_epoch()).count();
108 write_attribute(
"closing_timestamp", file_closing_timestamp);
114 if (m_file_ptr->getName() != m_bare_file_name) {
115 std::filesystem::rename(m_file_ptr->getName(), m_bare_file_name);
121 m_file_layout_ptr.reset();
127std::vector<std::string>
128HDF5RawDataFile::HDF5RawDataFile::get_attribute_names()
130 return m_file_ptr->listAttributeNames();
141 HDF5SourceIDHandler::source_id_path_map_t source_id_path_map;
144 HDF5SourceIDHandler::fragment_type_source_id_map_t fragment_type_source_id_map;
147 HDF5SourceIDHandler::subdetector_source_id_map_t subdetector_source_id_map;
150 HighFive::Group record_level_group =
write(tr.get_header_ref(), source_id_path_map);
154 for (
auto const& source_id_path : source_id_path_map) {
155 HDF5SourceIDHandler::store_record_header_source_id(record_level_group, source_id_path.first);
159 for (
auto const& frag_ptr : tr.get_fragments_ref()) {
160 write(*frag_ptr, source_id_path_map);
161 HDF5SourceIDHandler::add_fragment_type_source_id_to_map(
162 fragment_type_source_id_map, frag_ptr->get_fragment_type(), frag_ptr->get_element_id());
163 HDF5SourceIDHandler::add_subdetector_source_id_to_map(
164 subdetector_source_id_map,
166 frag_ptr->get_element_id());
170 HDF5SourceIDHandler::store_record_level_path_info(record_level_group, source_id_path_map);
171 HDF5SourceIDHandler::store_record_level_fragment_type_map(record_level_group, fragment_type_source_id_map);
172 HDF5SourceIDHandler::store_record_level_subdetector_map(record_level_group, subdetector_source_id_map);
183 HDF5SourceIDHandler::source_id_path_map_t source_id_path_map;
186 HDF5SourceIDHandler::fragment_type_source_id_map_t fragment_type_source_id_map;
189 HDF5SourceIDHandler::subdetector_source_id_map_t subdetector_source_id_map;
192 HighFive::Group record_level_group =
write(ts.get_header(), source_id_path_map);
196 for (
auto const& source_id_path : source_id_path_map) {
197 HDF5SourceIDHandler::store_record_header_source_id(record_level_group, source_id_path.first);
201 for (
auto const& frag_ptr : ts.get_fragments_ref()) {
202 write(*frag_ptr, source_id_path_map);
203 HDF5SourceIDHandler::add_fragment_type_source_id_to_map(
204 fragment_type_source_id_map, frag_ptr->get_fragment_type(), frag_ptr->get_element_id());
205 HDF5SourceIDHandler::add_subdetector_source_id_to_map(
206 subdetector_source_id_map,
208 frag_ptr->get_element_id());
212 HDF5SourceIDHandler::store_record_level_path_info(record_level_group, source_id_path_map);
213 HDF5SourceIDHandler::store_record_level_fragment_type_map(record_level_group, fragment_type_source_id_map);
214 HDF5SourceIDHandler::store_record_level_subdetector_map(record_level_group, subdetector_source_id_map);
222 HDF5SourceIDHandler::source_id_path_map_t& path_map)
224 std::tuple<size_t, std::string, HighFive::Group> write_results =
225 do_write(m_file_layout_ptr->get_path_elements(trh),
226 static_cast<const char*
>(trh.get_storage_location()),
227 trh.get_total_size_bytes(),
228 m_compression_level);
229 m_recorded_size += std::get<0>(write_results);
230 HDF5SourceIDHandler::add_source_id_path_to_map(path_map, trh.get_header().element_id, std::get<1>(write_results));
231 return std::get<2>(write_results);
240 std::tuple<size_t, std::string, HighFive::Group> write_results = do_write(m_file_layout_ptr->get_path_elements(tsh),
243 m_compression_level);
244 m_recorded_size += std::get<0>(write_results);
245 HDF5SourceIDHandler::add_source_id_path_to_map(path_map, tsh.element_id, std::get<1>(write_results));
246 return std::get<2>(write_results);
255 std::tuple<size_t, std::string, HighFive::Group> write_results =
256 do_write(m_file_layout_ptr->get_path_elements(frag.get_header()),
257 static_cast<const char*
>(frag.get_storage_location()),
259 m_compression_level);
260 m_recorded_size += std::get<0>(write_results);
263 HDF5SourceIDHandler::add_source_id_path_to_map(path_map, source_id, std::get<1>(write_results));
270HDF5RawDataFile::write_file_layout()
272 auto fl_json = m_file_layout_ptr->get_file_layout_params().to_json();
273 write_attribute(
"filelayout_params", fl_json.dump());
274 write_attribute(
"filelayout_version", m_file_layout_ptr->get_version());
280std::tuple<size_t, std::string, HighFive::Group>
281HDF5RawDataFile::do_write(std::vector<std::string>
const& group_and_dataset_path_elements,
282 const char* raw_data_ptr,
283 size_t raw_data_size_bytes,
284 unsigned compression_level)
286 const std::string dataset_name = group_and_dataset_path_elements.back();
289 std::string
const& top_level_group_name = group_and_dataset_path_elements.at(0);
290 if (!m_file_ptr->exist(top_level_group_name))
291 m_file_ptr->createGroup(top_level_group_name);
294 HighFive::Group sub_group = m_file_ptr->getGroup(top_level_group_name);
295 if (!sub_group.isValid()) {
296 throw InvalidHDF5Group(
ERS_HERE, top_level_group_name);
298 HighFive::Group top_level_group = sub_group;
301 for (
size_t idx = 1; idx < group_and_dataset_path_elements.size() - 1; ++idx) {
303 std::string
const& child_group_name = group_and_dataset_path_elements[idx];
304 if (child_group_name.empty()) {
305 throw InvalidHDF5Group(
ERS_HERE, child_group_name);
307 if (!sub_group.exist(child_group_name)) {
308 sub_group.createGroup(child_group_name);
310 HighFive::Group child_group = sub_group.getGroup(child_group_name);
311 if (!child_group.isValid()) {
312 throw InvalidHDF5Group(
ERS_HERE, child_group_name);
314 sub_group = child_group;
318 HighFive::DataSpace data_space = HighFive::DataSpace({ raw_data_size_bytes, 1 });
319 HighFive::DataSetCreateProps data_set_create_props;
320 HighFive::DataSetAccessProps data_set_access_props;
322 if (compression_level > 0) {
323 std::vector<hsize_t> chunk_size = { raw_data_size_bytes, 1 };
324 data_set_create_props.add(HighFive::Chunking(chunk_size));
325 data_set_create_props.add(HighFive::Deflate(compression_level));
328 m_uncompressed_raw_data_size += raw_data_size_bytes;
330 auto data_set = sub_group.createDataSet<
char>(dataset_name, data_space, data_set_create_props, data_set_access_props);
332 if (data_set.isValid()) {
333 data_set.write_raw(raw_data_ptr);
334 m_total_file_size = m_file_ptr->getFileSize();
336 return std::make_tuple(data_set.getStorageSize(), data_set.getPath(), top_level_group);
338 throw InvalidHDF5Dataset(
ERS_HERE, dataset_name, m_file_ptr->getName());
345HDF5RawDataFile::HDF5RawDataFile(
const std::string& file_name,
bool allow_writing)
346 : m_open_flags(HighFive::File::ReadOnly)
349 m_open_flags = HighFive::File::ReadWrite;
351 m_bare_file_name = file_name;
352 size_t pos = m_bare_file_name.rfind(s_inprogress_suffix);
353 if (pos != std::string::npos) {
354 m_bare_file_name.erase(pos);
359 m_file_ptr = std::make_unique<HighFive::File>(file_name, m_open_flags);
360 }
catch (std::exception
const& excpt) {
364 if (m_file_ptr->hasAttribute(
"recorded_size"))
365 m_recorded_size = get_attribute<size_t>(
"recorded_size");
369 if (m_file_ptr->hasAttribute(
"uncompressed_raw_data_size"))
370 m_uncompressed_raw_data_size = get_attribute<size_t>(
"uncompressed_raw_data_size");
372 m_uncompressed_raw_data_size = 0;
374 if (m_file_ptr->hasAttribute(
"total_file_size"))
375 m_total_file_size = get_attribute<size_t>(
"total_file_size");
377 m_total_file_size = 0;
379 if (m_file_ptr->hasAttribute(
"compression_level"))
380 m_compression_level = get_attribute<unsigned>(
"compression_level");
382 m_compression_level = 0;
386 if (m_file_ptr->hasAttribute(
"record_type"))
387 m_record_type = get_attribute<std::string>(
"record_type");
389 m_record_type = m_file_layout_ptr->get_record_name_prefix();
396 sid_handler.fetch_file_level_geo_id_info(*m_file_ptr, m_file_level_source_id_geo_id_map);
400HDF5RawDataFile::read_file_layout()
402 HDF5FileLayoutParameters fl_params;
407 fl_str = get_attribute<std::string>(
"filelayout_params");
408 nlohmann::json fl_json = nlohmann::json::parse(fl_str);
409 fl_params = HDF5FileLayoutParameters(fl_json);
411 version = get_attribute<uint32_t>(
"filelayout_version");
413 }
catch (InvalidHDF5Attribute
const&) {
418 m_file_layout_ptr.reset(
new HDF5FileLayout(fl_params,
version));
422HDF5RawDataFile::check_file_layout()
424 if (get_version() < 2)
427 std::string record_type = get_attribute<std::string>(
"record_type");
428 if (record_type.compare(m_file_layout_ptr->get_record_name_prefix()) != 0)
429 throw BadRecordType(
ERS_HERE, record_type, m_file_layout_ptr->get_record_name_prefix());
433HDF5RawDataFile::check_record_type(std::string rt_name)
435 if (get_version() < 2)
438 if (m_file_layout_ptr->get_record_name_prefix().compare(rt_name) != 0)
439 throw WrongRecordTypeRequested(
ERS_HERE, rt_name, m_file_layout_ptr->get_record_name_prefix());
444HDF5RawDataFile::explore_subgroup(
const HighFive::Group& parent_group,
445 std::string relative_path,
446 std::vector<std::string>& path_list)
448 if (relative_path.size() > 0 && relative_path.compare(relative_path.size() - 1, 1,
"/") == 0)
449 relative_path.pop_back();
451 std::vector<std::string> childNames = parent_group.listObjectNames();
453 for (
auto& child_name : childNames) {
454 std::string full_path = relative_path +
"/" + child_name;
455 HighFive::ObjectType child_type = parent_group.getObjectType(child_name);
457 if (child_type == HighFive::ObjectType::Dataset) {
458 path_list.push_back(full_path);
459 }
else if (child_type == HighFive::ObjectType::Group) {
460 HighFive::Group child_group = parent_group.getGroup(child_name);
462 std::string new_path = relative_path +
"/" + child_name;
463 explore_subgroup(child_group, new_path, path_list);
469HDF5RawDataFile::add_record_level_info_to_caches_if_needed(record_id_t rid)
475 if (m_source_id_path_cache.count(rid) != 0) {
480 HDF5SourceIDHandler sid_handler(get_version());
483 std::string record_level_group_name = m_file_layout_ptr->get_record_number_string(rid.first, rid.second);
484 HighFive::Group record_group = m_file_ptr->getGroup(record_level_group_name);
485 if (!record_group.isValid()) {
486 throw InvalidHDF5Group(
ERS_HERE, record_level_group_name);
491 HDF5SourceIDHandler::source_id_geo_id_map_t local_source_id_geo_id_map = m_file_level_source_id_geo_id_map;
492 sid_handler.fetch_record_level_geo_id_info(record_group, local_source_id_geo_id_map);
495 HDF5SourceIDHandler::source_id_path_map_t source_id_path_map;
496 sid_handler.fetch_source_id_path_info(record_group, source_id_path_map);
499 HDF5SourceIDHandler::fragment_type_source_id_map_t fragment_type_source_id_map;
500 sid_handler.fetch_fragment_type_source_id_info(record_group, fragment_type_source_id_map);
503 HDF5SourceIDHandler::subdetector_source_id_map_t subdetector_source_id_map;
504 sid_handler.fetch_subdetector_source_id_info(record_group, subdetector_source_id_map);
508 std::set<daqdataformats::SourceID> full_source_id_set;
509 std::set<daqdataformats::SourceID> fragment_source_id_set;
510 HDF5SourceIDHandler::subsystem_source_id_map_t subsystem_source_id_map;
511 for (
auto const& source_id_path : source_id_path_map) {
512 full_source_id_set.insert(source_id_path.first);
513 if (source_id_path.first != rh_sid) {
514 fragment_source_id_set.insert(source_id_path.first);
516 HDF5SourceIDHandler::add_subsystem_source_id_to_map(
517 subsystem_source_id_map, source_id_path.first.subsystem, source_id_path.first);
523 m_source_id_cache[rid] = full_source_id_set;
524 m_record_header_source_id_cache[rid] = rh_sid;
525 m_fragment_source_id_cache[rid] = fragment_source_id_set;
526 m_source_id_geo_id_cache[rid] = local_source_id_geo_id_map;
527 m_source_id_path_cache[rid] = source_id_path_map;
528 m_subsystem_source_id_cache[rid] = subsystem_source_id_map;
529 m_fragment_type_source_id_cache[rid] = fragment_type_source_id_map;
530 m_subdetector_source_id_cache[rid] = subdetector_source_id_map;
536std::vector<std::string>
537HDF5RawDataFile::get_dataset_paths(std::string top_level_group_name)
539 if (top_level_group_name.empty())
540 top_level_group_name = m_file_ptr->getPath();
543 std::vector<std::string> path_list;
545 HighFive::Group parent_group = m_file_ptr->getGroup(top_level_group_name);
546 if (!parent_group.isValid())
547 throw InvalidHDF5Group(
ERS_HERE, top_level_group_name);
549 explore_subgroup(parent_group, top_level_group_name, path_list);
557HDF5RawDataFile::record_id_set
558HDF5RawDataFile::get_all_record_ids()
560 if (!m_all_record_ids_in_file.empty())
561 return m_all_record_ids_in_file;
565 HighFive::Group parent_group = m_file_ptr->getGroup(m_file_ptr->getPath());
567 std::vector<std::string> childNames = parent_group.listObjectNames();
568 const std::string record_prefix = m_file_layout_ptr->get_record_name_prefix();
569 const size_t record_prefix_size = record_prefix.size();
571 for (
auto const& name : childNames) {
572 auto loc = name.find(record_prefix);
574 if (loc == std::string::npos)
577 auto rec_num_string = name.substr(loc + record_prefix_size);
579 loc = rec_num_string.find(
".");
580 if (loc == std::string::npos) {
581 m_all_record_ids_in_file.insert(std::make_pair(std::stoll(rec_num_string), 0));
583 auto seq_num_string = rec_num_string.substr(loc + 1);
584 rec_num_string.resize(loc);
585 m_all_record_ids_in_file.insert(std::make_pair(std::stoll(rec_num_string), std::stoi(seq_num_string)));
590 return m_all_record_ids_in_file;
594HDF5RawDataFile::get_all_record_numbers()
597 "get_all_record_numbers()",
598 "Use get_all_record_ids(), which returns a record_number,sequence_number pair."));
600 std::set<uint64_t> record_numbers;
601 for (
auto const& rid : get_all_record_ids())
602 record_numbers.insert(rid.first);
604 return record_numbers;
607HDF5RawDataFile::record_id_set
608HDF5RawDataFile::get_all_trigger_record_ids()
610 check_record_type(
"TriggerRecord");
611 return get_all_record_ids();
614std::set<daqdataformats::trigger_number_t>
615HDF5RawDataFile::get_all_trigger_record_numbers()
619 "get_all_trigger_record_numbers()",
620 "Use get_all_trigger_record_ids(), which returns a record_number,sequence_number pair."));
622 return get_all_record_numbers();
625HDF5RawDataFile::record_id_set
626HDF5RawDataFile::get_all_timeslice_ids()
628 check_record_type(
"TimeSlice");
629 return get_all_record_ids();
632std::set<daqdataformats::timeslice_number_t>
633HDF5RawDataFile::get_all_timeslice_numbers()
635 check_record_type(
"TimeSlice");
636 return get_all_record_numbers();
642std::vector<std::string>
643HDF5RawDataFile::get_record_header_dataset_paths()
646 std::vector<std::string> rec_paths;
648 if (get_version() >= 2) {
649 for (
auto const& rec_id : get_all_record_ids())
650 rec_paths.push_back(get_record_header_dataset_path(rec_id));
652 for (
auto const& path : get_dataset_paths()) {
653 if (path.find(m_file_layout_ptr->get_record_header_dataset_name()) != std::string::npos) {
654 rec_paths.push_back(path);
662std::vector<std::string>
663HDF5RawDataFile::get_trigger_record_header_dataset_paths()
665 check_record_type(
"TriggerRecord");
666 return get_record_header_dataset_paths();
669std::vector<std::string>
670HDF5RawDataFile::get_timeslice_header_dataset_paths()
672 check_record_type(
"TimeSlice");
673 return get_record_header_dataset_paths();
677HDF5RawDataFile::get_record_header_dataset_path(
const record_id_t& rid)
679 auto rec_id = get_all_record_ids().find(rid);
680 if (rec_id == get_all_record_ids().end())
681 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
683 if (get_version() <= 2) {
684 return (m_file_ptr->getPath() + m_file_layout_ptr->get_record_header_path(rid.first, rid.second));
687 return m_source_id_path_cache[rid][source_id];
692HDF5RawDataFile::get_record_header_dataset_path(
const uint64_t rec_num,
695 return get_record_header_dataset_path(std::make_pair(rec_num, seq_num));
699HDF5RawDataFile::get_trigger_record_header_dataset_path(
const record_id_t& rid)
701 check_record_type(
"TriggerRecord");
702 return get_record_header_dataset_path(rid);
709 check_record_type(
"TriggerRecord");
710 return get_record_header_dataset_path(trig_num, seq_num);
714HDF5RawDataFile::get_timeslice_header_dataset_path(
const record_id_t& rid)
716 check_record_type(
"TimeSlice");
717 return get_record_header_dataset_path(rid.first, 0);
723 check_record_type(
"TimeSlice");
724 return get_record_header_dataset_path(ts_num);
733std::vector<std::string>
734HDF5RawDataFile::get_all_fragment_dataset_paths()
736 std::vector<std::string> frag_paths;
738 for (
auto const& path : get_dataset_paths()) {
739 if (path.find(m_file_layout_ptr->get_record_header_dataset_name()) == std::string::npos)
740 frag_paths.push_back(path);
747std::vector<std::string>
748HDF5RawDataFile::get_fragment_dataset_paths(
const record_id_t& rid)
750 auto rec_id = get_all_record_ids().find(rid);
751 if (rec_id == get_all_record_ids().end())
752 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
754 std::vector<std::string> frag_paths;
755 if (get_version() <= 2) {
756 std::string record_group_path =
757 m_file_ptr->getPath() + m_file_layout_ptr->get_record_number_string(rid.first, rid.second);
759 for (
auto const& path : get_dataset_paths(record_group_path)) {
760 if (path.find(m_file_layout_ptr->get_record_header_dataset_name()) == std::string::npos)
761 frag_paths.push_back(path);
764 std::set<daqdataformats::SourceID> source_id_list = get_fragment_source_ids(rid);
765 for (
auto const& source_id : source_id_list) {
766 frag_paths.push_back(m_source_id_path_cache[rid][source_id]);
773std::vector<std::string>
774HDF5RawDataFile::get_fragment_dataset_paths(
const uint64_t rec_num,
777 return get_fragment_dataset_paths(std::make_pair(rec_num, seq_num));
781std::vector<std::string>
784 std::vector<std::string> frag_paths;
785 for (
auto const& rid : get_all_record_ids()) {
786 if (get_version() <= 2) {
787 auto datasets = get_dataset_paths(m_file_ptr->getPath() +
788 m_file_layout_ptr->get_fragment_type_path(rid.first, rid.second, subsystem));
789 frag_paths.insert(frag_paths.end(), datasets.begin(), datasets.end());
791 std::set<daqdataformats::SourceID> source_id_list = get_source_ids_for_subsystem(rid, subsystem);
792 for (
auto const& source_id : source_id_list) {
793 frag_paths.push_back(m_source_id_path_cache[rid][source_id]);
800std::vector<std::string>
801HDF5RawDataFile::get_fragment_dataset_paths(
const std::string& subsystem_name)
804 return get_fragment_dataset_paths(subsystem);
807std::vector<std::string>
810 auto rec_id = get_all_record_ids().find(rid);
811 if (rec_id == get_all_record_ids().end())
812 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
814 if (get_version() <= 2) {
815 return get_dataset_paths(m_file_ptr->getPath() +
816 m_file_layout_ptr->get_fragment_type_path(rid.first, rid.second, subsystem));
818 std::vector<std::string> frag_paths;
819 std::set<daqdataformats::SourceID> source_id_list = get_source_ids_for_subsystem(rid, subsystem);
820 for (
auto const& source_id : source_id_list) {
821 frag_paths.push_back(m_source_id_path_cache[rid][source_id]);
827std::vector<std::string>
828HDF5RawDataFile::get_fragment_dataset_paths(
const record_id_t& rid,
const std::string& subsystem_name)
831 return get_fragment_dataset_paths(rid, subsystem);
836std::vector<std::string>
839 std::vector<std::string> frag_paths;
841 for (
auto const& rid : get_all_record_ids())
842 frag_paths.push_back(m_file_ptr->getPath() +
843 m_file_layout_ptr->get_fragment_path(rid.first, rid.second, source_id));
848std::vector<std::string>
854std::vector<std::string>
855HDF5RawDataFile::get_fragment_dataset_paths(
const std::string& typestring,
858 return get_fragment_dataset_paths(
862std::set<daqdataformats::SourceID>
863HDF5RawDataFile::get_source_ids(std::vector<std::string>
const& frag_dataset_paths)
865 std::set<daqdataformats::SourceID> source_ids;
866 std::vector<std::string> path_elements;
868 for (
auto const& frag_dataset : frag_dataset_paths) {
869 path_elements.clear();
870 std::istringstream iss(frag_dataset);
871 while (std::getline(iss, s,
'/')) {
873 path_elements.push_back(s);
875 source_ids.insert(m_file_layout_ptr->get_source_id_from_path_elements(path_elements));
882HDF5SourceIDHandler::source_id_geo_id_map_t
883HDF5RawDataFile::get_srcid_geoid_map()
const
886 return m_file_level_source_id_geo_id_map;
890HDF5RawDataFile::get_all_geo_ids()
const
892 std::set<uint64_t> set_of_geo_ids;
898 for (
auto const& map_entry : m_file_level_source_id_geo_id_map) {
899 for (
auto const& geo_id : map_entry.second) {
900 set_of_geo_ids.insert(geo_id);
903 return set_of_geo_ids;
907HDF5RawDataFile::get_geo_ids(
const record_id_t& rid)
909 auto rec_id = get_all_record_ids().find(rid);
910 if (rec_id == get_all_record_ids().end())
911 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
913 add_record_level_info_to_caches_if_needed(rid);
915 std::set<uint64_t> set_of_geo_ids;
916 for (
auto const& map_entry : m_source_id_geo_id_cache[rid]) {
917 for (
auto const& geo_id : map_entry.second) {
918 set_of_geo_ids.insert(geo_id);
921 return set_of_geo_ids;
927 auto rec_id = get_all_record_ids().find(rid);
928 if (rec_id == get_all_record_ids().end())
929 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
931 add_record_level_info_to_caches_if_needed(rid);
933 std::set<uint64_t> set_of_geo_ids;
934 for (
auto const& map_entry : m_source_id_geo_id_cache[rid]) {
935 for (
auto const& geo_id : map_entry.second) {
938 uint16_t
det_id = 0xffff & geo_id;
939 if (det_id ==
static_cast<uint16_t
>(subdet)) {
940 set_of_geo_ids.insert(geo_id);
944 return set_of_geo_ids;
948std::set<daqdataformats::SourceID>
949HDF5RawDataFile::get_source_ids(
const record_id_t& rid)
951 auto rec_id = get_all_record_ids().find(rid);
952 if (rec_id == get_all_record_ids().end())
953 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
955 add_record_level_info_to_caches_if_needed(rid);
957 return m_source_id_cache[rid];
961HDF5RawDataFile::get_record_header_source_id(
const record_id_t& rid)
963 auto rec_id = get_all_record_ids().find(rid);
964 if (rec_id == get_all_record_ids().end())
965 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
967 add_record_level_info_to_caches_if_needed(rid);
969 return m_record_header_source_id_cache[rid];
972std::set<daqdataformats::SourceID>
973HDF5RawDataFile::get_fragment_source_ids(
const record_id_t& rid)
975 auto rec_id = get_all_record_ids().find(rid);
976 if (rec_id == get_all_record_ids().end())
977 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
979 add_record_level_info_to_caches_if_needed(rid);
981 return m_fragment_source_id_cache[rid];
984std::set<daqdataformats::SourceID>
985HDF5RawDataFile::get_source_ids_for_subsystem(
const record_id_t& rid,
988 auto rec_id = get_all_record_ids().find(rid);
989 if (rec_id == get_all_record_ids().end())
990 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
992 add_record_level_info_to_caches_if_needed(rid);
994 return m_subsystem_source_id_cache[rid][subsystem];
997std::set<daqdataformats::SourceID>
1000 auto rec_id = get_all_record_ids().find(rid);
1001 if (rec_id == get_all_record_ids().end())
1002 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1004 add_record_level_info_to_caches_if_needed(rid);
1006 return m_fragment_type_source_id_cache[rid][frag_type];
1009std::set<daqdataformats::SourceID>
1010HDF5RawDataFile::get_source_ids_for_fragtype_and_subdetector(
const record_id_t& rid,
1011 const std::string& frag_type_name,
1012 const std::string& subdet_name)
1016 throw InvalidFragmentTypeString(
ERS_HERE, frag_type_name);
1019 throw InvalidSubdetectorString(
ERS_HERE, subdet_name);
1021 auto rec_id = get_all_record_ids().find(rid);
1022 if (rec_id == get_all_record_ids().end())
1023 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1025 add_record_level_info_to_caches_if_needed(rid);
1027 std::set<daqdataformats::SourceID> fragtype_match_sids = m_fragment_type_source_id_cache[rid][frag_type];
1028 std::set<daqdataformats::SourceID> detid_match_sids = m_subdetector_source_id_cache[rid][subdet];
1029 std::set<daqdataformats::SourceID> combined_set_sids;
1030 for (
auto ftsid : fragtype_match_sids) {
1031 if (detid_match_sids.contains(ftsid)) {
1032 combined_set_sids.insert(ftsid);
1035 return combined_set_sids;
1038std::set<daqdataformats::SourceID>
1041 auto rec_id = get_all_record_ids().find(rid);
1042 if (rec_id == get_all_record_ids().end())
1043 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1045 add_record_level_info_to_caches_if_needed(rid);
1047 return m_subdetector_source_id_cache[rid][subdet];
1050std::unique_ptr<char[]>
1051HDF5RawDataFile::get_dataset_raw_data(
const std::string& dataset_path)
1053 HighFive::Group parent_group = m_file_ptr->getGroup(
"/");
1054 HighFive::DataSet data_set = parent_group.getDataSet(dataset_path);
1056 if (!data_set.isValid())
1057 throw InvalidHDF5Dataset(
ERS_HERE, dataset_path, get_file_name());
1059 size_t data_size = data_set.getSpace().getElementCount() *
sizeof(char);
1061 auto membuffer = std::make_unique<char[]>(data_size);
1063 data_set.read(membuffer.get());
1067std::unique_ptr<daqdataformats::Fragment>
1068HDF5RawDataFile::get_frag_ptr(
const std::string& dataset_name)
1070 auto membuffer = get_dataset_raw_data(dataset_name);
1071 auto frag_ptr = std::make_unique<daqdataformats::Fragment>(
1076std::unique_ptr<daqdataformats::Fragment>
1079 if (get_version() < 2)
1080 throw IncompatibleFileLayoutVersion(
ERS_HERE, get_version(), 2, MAX_FILELAYOUT_VERSION);
1082 auto rec_id = get_all_record_ids().find(rid);
1083 if (rec_id == get_all_record_ids().end())
1084 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1086 add_record_level_info_to_caches_if_needed(rid);
1088 return get_frag_ptr(m_source_id_path_cache[rid][source_id]);
1091std::unique_ptr<daqdataformats::Fragment>
1092HDF5RawDataFile::get_frag_ptr(
const uint64_t rec_num,
1096 record_id_t rid = std::make_pair(rec_num, seq_num);
1097 return get_frag_ptr(rid, source_id);
1100std::unique_ptr<daqdataformats::Fragment>
1101HDF5RawDataFile::get_frag_ptr(
const record_id_t& rid,
1106 return get_frag_ptr(rid, source_id);
1109std::unique_ptr<daqdataformats::Fragment>
1110HDF5RawDataFile::get_frag_ptr(
const uint64_t rec_num,
1115 record_id_t rid = std::make_pair(rec_num, seq_num);
1117 return get_frag_ptr(rid, source_id);
1120std::unique_ptr<daqdataformats::Fragment>
1121HDF5RawDataFile::get_frag_ptr(
const record_id_t& rid,
1122 const std::string& typestring,
1126 return get_frag_ptr(rid, source_id);
1129std::unique_ptr<daqdataformats::Fragment>
1130HDF5RawDataFile::get_frag_ptr(
const uint64_t rec_num,
1132 const std::string& typestring,
1135 record_id_t rid = std::make_pair(rec_num, seq_num);
1137 return get_frag_ptr(rid, source_id);
1140std::unique_ptr<daqdataformats::Fragment>
1141HDF5RawDataFile::get_frag_ptr(
const record_id_t& rid,
1142 const uint64_t geo_id)
1145 return get_frag_ptr(rid, sid);
1148std::unique_ptr<daqdataformats::Fragment>
1149HDF5RawDataFile::get_frag_ptr(
const uint64_t rec_num,
1151 const uint64_t geo_id)
1153 record_id_t rid = std::make_pair(rec_num, seq_num);
1154 return get_frag_ptr(rid, geo_id);
1157std::unique_ptr<daqdataformats::TriggerRecordHeader>
1158HDF5RawDataFile::get_trh_ptr(
const std::string& dataset_name)
1160 auto membuffer = get_dataset_raw_data(dataset_name);
1161 auto trh_ptr = std::make_unique<daqdataformats::TriggerRecordHeader>(membuffer.release(),
true);
1165std::unique_ptr<daqdataformats::TriggerRecordHeader>
1166HDF5RawDataFile::get_trh_ptr(
const record_id_t& rid)
1168 if (get_version() < 2)
1169 throw IncompatibleFileLayoutVersion(
ERS_HERE, get_version(), 2, MAX_FILELAYOUT_VERSION);
1171 auto rec_id = get_all_record_ids().find(rid);
1172 if (rec_id == get_all_record_ids().end())
1173 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1175 add_record_level_info_to_caches_if_needed(rid);
1178 return get_trh_ptr(m_source_id_path_cache[rid][rh_source_id]);
1181std::unique_ptr<daqdataformats::TimeSliceHeader>
1182HDF5RawDataFile::get_tsh_ptr(
const std::string& dataset_name)
1184 auto membuffer = get_dataset_raw_data(dataset_name);
1185 auto tsh_ptr = std::make_unique<daqdataformats::TimeSliceHeader>(
1190std::unique_ptr<daqdataformats::TimeSliceHeader>
1191HDF5RawDataFile::get_tsh_ptr(
const record_id_t& rid)
1193 if (get_version() < 2)
1194 throw IncompatibleFileLayoutVersion(
ERS_HERE, get_version(), 2, MAX_FILELAYOUT_VERSION);
1196 auto rec_id = get_all_record_ids().find(rid);
1197 if (rec_id == get_all_record_ids().end())
1198 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1200 add_record_level_info_to_caches_if_needed(rid);
1203 return get_tsh_ptr(m_source_id_path_cache[rid][rh_source_id]);
1207HDF5RawDataFile::get_trigger_record(
const record_id_t& rid)
1210 for (
auto const& frag_path : get_fragment_dataset_paths(rid)) {
1211 trigger_record.add_fragment(get_frag_ptr(frag_path));
1214 return trigger_record;
1221 for (
auto const& frag_path : get_fragment_dataset_paths(ts_num)) {
1222 timeslice.add_fragment(get_frag_ptr(frag_path));
1228std::vector<uint64_t>
1231 auto rec_id = get_all_record_ids().find(rid);
1232 if (rec_id == get_all_record_ids().end())
1233 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1235 add_record_level_info_to_caches_if_needed(rid);
1237 return m_source_id_geo_id_cache[rid][source_id];
1241HDF5RawDataFile::get_source_id_for_geo_id(
const record_id_t& rid,
1242 const uint64_t requested_geo_id)
1244 auto rec_id = get_all_record_ids().find(rid);
1245 if (rec_id == get_all_record_ids().end())
1246 throw RecordIDNotFound(
ERS_HERE, rid.first, rid.second);
1248 add_record_level_info_to_caches_if_needed(rid);
1252 for (
auto const& map_entry : m_source_id_geo_id_cache[rid]) {
1253 auto geoid_list = map_entry.second;
1254 for (
auto const& geoid_from_list : geoid_list) {
1255 if (geoid_from_list == requested_geo_id) {
1256 return map_entry.first;
std::map< daqdataformats::SourceID, std::vector< uint64_t > > source_id_geo_id_map_t
static void store_file_level_geo_id_info(HighFive::File &h5_file, const source_id_geo_id_map_t &the_map)
#define TLOG_DEBUG(lvl,...)
constexpr uint32_t MAX_FILELAYOUT_VERSION
void warning(const Issue &issue)
void info(const Issue &issue)
std::string record_name_prefix