7#ifndef CALORECGPU_STANDALONEDATAIO_H
8#define CALORECGPU_STANDALONEDATAIO_H
32 inline static void report_error(
const std::filesystem::path &
file,
const std::string & kind,
const bool really_report =
false)
36 std::cerr <<
"ERROR: when " << kind <<
" from '" <<
file <<
"'." << std::endl;
50 const bool report =
false)
52 std::ifstream in(
file.native(), std::ios_base::binary);
65 const bool report =
false)
67 std::ifstream in(
file.native(), std::ios_base::binary);
68 noise.binary_input(in);
80 const bool report =
false)
82 file.replace_extension(
".geometry");
83 std::ofstream out(
file, std::ios_base::binary);
84 geo.binary_output(out);
96 const bool report =
false)
98 file.replace_extension(
".noise");
99 std::ofstream out(
file, std::ios_base::binary);
100 noise.binary_output(out);
118 const bool report =
false)
122 std::ifstream in(
file.native(), std::ios_base::binary);
124 auto reader = [&](
auto * ptr,
const auto & size)
126 in.read(
reinterpret_cast<char *
>(ptr),
sizeof(*ptr) * size);
137 if (clusters->has_basic_info())
139 reader(clusters->clusterEnergy, clusters->number);
140 reader(clusters->clusterEt, clusters->number);
141 reader(clusters->clusterEta, clusters->number);
142 reader(clusters->clusterPhi, clusters->number);
143 reader(clusters->seedCellIndex, clusters->number);
146 switch (clusters->state)
160 reader(clusters->cellsPrefixSum, clusters->number + 1);
161 reader(clusters->cells.indices, clusters->number_cells);
162 reader(clusters->cellWeights, clusters->number_cells);
163 reader(clusters->clusterIndices, clusters->number_cells);
169 if (clusters->has_moments())
171 clusters->for_all_moments([&](
auto & arr)
173 unsigned long long base_size;
174 if constexpr (std::is_pointer_v<std::decay_t<
decltype(*arr)>>)
176 base_size =
sizeof(**arr) *
sizeof(arr)/
sizeof(*arr);
180 base_size =
sizeof(*arr);
182 in.read(
reinterpret_cast<char *
>(arr), base_size * clusters->number);
202 const bool report =
false)
204 std::ifstream in(
file.native(), std::ios_base::binary);
205 cell_info.binary_input(in);
217 const bool report =
false)
219 file.replace_extension(
".clusterinfo");
220 std::ofstream out(
file, std::ios_base::binary);
222 auto writer = [&](
const auto * ptr,
const auto & size)
224 out.write(
reinterpret_cast<const char *
>(ptr),
sizeof(*ptr) * size);
230 if (clusters->has_basic_info())
232 writer(clusters->clusterEnergy, clusters->number);
233 writer(clusters->clusterEt, clusters->number);
234 writer(clusters->clusterEta, clusters->number);
235 writer(clusters->clusterPhi, clusters->number);
236 writer(clusters->seedCellIndex, clusters->number);
239 switch (clusters->state)
253 writer(clusters->cellsPrefixSum, clusters->number + 1);
254 writer(clusters->cells.indices, clusters->number_cells);
255 writer(clusters->cellWeights, clusters->number_cells);
256 writer(clusters->clusterIndices, clusters->number_cells);
262 if (clusters->has_moments())
264 clusters->for_all_moments([&](
const auto & arr)
266 unsigned long long base_size;
267 if constexpr (std::is_pointer_v<std::decay_t<
decltype(*arr)>>)
269 base_size =
sizeof(**arr) *
sizeof(arr)/
sizeof(*arr);
273 base_size =
sizeof(*arr);
275 out.write(
reinterpret_cast<const char *
>(arr), base_size * clusters->number);
292 const bool report =
false)
294 file.replace_extension(
".cellinfo");
295 std::ofstream out(
file, std::ios_base::binary);
296 cell_info.binary_output(out);
312 if (!std::filesystem::exists(folder))
314 if (!std::filesystem::create_directory(folder))
318 std::cout <<
"ERROR: folder '" << folder <<
"' could not be created." << std::endl;
323 else if (!std::filesystem::is_directory(folder))
327 std::cout <<
"ERROR: folder '" << folder <<
"' is not a valid folder." << std::endl;
346 const std::string & text,
347 const std::string & suffix,
348 const std::string & ext )
350 return prefix + (prefix.size() > 0 ?
"_" :
"") + text +
351 (suffix.size() > 0 ?
"_" :
"") + suffix +
"." + ext;
355 const size_t event_number,
356 const std::string & suffix,
357 const std::string & ext,
358 const unsigned int num_width = 9 )
360 std::ostringstream event_ID_format;
361 event_ID_format << std::setfill(
'0') << std::setw(num_width) << event_number;
362 const std::string event_ID = event_ID_format.str();
370 const std::string & prefix =
"",
371 const std::string & suffix =
"",
372 const bool output_errors =
true)
379 auto filename = [&] (
const std::string & text,
const std::string & ext)
396 const std::filesystem::path & folder,
399 const std::string & prefix =
"",
400 const std::string & suffix =
"",
401 const unsigned int num_width = 9,
402 const bool output_errors =
true)
409 std::ostringstream event_ID_format;
410 event_ID_format << std::setfill(
'0') << std::setw(num_width) << event_number;
411 const std::string event_ID = event_ID_format.str();
413 auto filename = [&] (
const std::string & ext)
430 const std::filesystem::path & folder,
432 const std::string & prefix =
"",
433 const std::string & suffix =
"",
434 const unsigned int num_width = 9,
435 const bool output_errors =
true)
442 std::ostringstream event_ID_format;
443 event_ID_format << std::setfill(
'0') << std::setw(num_width) << event_number;
444 const std::string event_ID = event_ID_format.str();
446 auto filename = [&] (
const std::string & ext)
459 const std::filesystem::path & folder,
461 const std::string & prefix =
"",
462 const std::string & suffix =
"",
463 const unsigned int num_width = 9,
464 const bool output_errors =
true)
471 std::ostringstream event_ID_format;
472 event_ID_format << std::setfill(
'0') << std::setw(num_width) << event_number;
473 const std::string event_ID = event_ID_format.str();
475 auto filename = [&] (
const std::string & ext)
489 std::map<std::string, CaloRecGPU::Helpers::CPU_object<CaloRecGPU::GeometryArr>>
geometry;
490 std::map<std::string, CaloRecGPU::Helpers::CPU_object<CaloRecGPU::CellNoiseArr>>
noise;
491 std::map<std::string, CaloRecGPU::Helpers::CPU_object<CaloRecGPU::ClusterInfoArr>>
cluster_info;
492 std::map<std::string, CaloRecGPU::Helpers::CPU_object<CaloRecGPU::CellInfoArr>>
cell_info;
528 const std::filesystem::path & folder,
531 const bool output_messages =
true)
534 if (!std::filesystem::is_directory(folder))
538 std::cout <<
"ERROR: '" << folder <<
"' is not a valid folder." << std::endl;
542 std::set<std::string> read_one_part_of_v1_cells;
544 for (
const std::filesystem::path &
file : std::filesystem::directory_iterator(folder))
546 if ( max_events > 0 && event_count >= max_events &&
552 const bool can_load_events = (max_events < 0) || (event_count < max_events);
553 const std::string filename =
file.stem().native();
555 if (filter_function(filename))
560 auto check_error = [&](
const ErrorState & es,
const std::string &
str)
568 std::cout <<
"ERROR: '" <<
file <<
"' is not a valid " <<
str <<
" file (" << (int) es <<
")." << std::endl;
573 auto output_loading_message = [&](
const std::string &
str)
577 std::cout <<
"Loaded " <<
str <<
" from '" <<
file <<
"'." << std::endl;
581 if (
file.extension() ==
".geometry")
583 if (!flo.load_geometry)
592 ret.
geometry[filename] = std::move(tempgeo);
593 output_loading_message(
"geometry");
595 else if (
file.extension() ==
".cellinfo")
597 if (!flo.load_cell_info || !can_load_events)
606 ret.
cell_info[filename] = std::move(tempcellinfo);
607 output_loading_message(
"cell info");
608 if (ret.
cluster_info.count(filename) > 0 || !flo.load_cluster_info)
613 else if (
file.extension() ==
".clusterinfo")
615 if (!flo.load_cluster_info || !can_load_events)
625 output_loading_message(
"cluster info");
626 if (ret.
cell_info.count(filename) > 0 || !flo.load_cell_info)
631 else if (
file.extension() ==
".noise")
642 ret.
noise[filename] = std::move(tempnois);
643 output_loading_message(
"noise");
652 const bool output_messages =
true)
658 folder, max_events, flo, output_messages);
SimpleHolder< T, MemoryContext::CPU, true > CPU_object
Holds an object of type T in CPU memory.
constexpr int NMaxClusters
Contains the fundamental information that allows interacting with ClusterInfoArr so that it is easier...
The members of this structure should all be initialised by default to false.
static constexpr FolderLoadOptions None()
static constexpr FolderLoadOptions All()
std::map< std::string, CaloRecGPU::Helpers::CPU_object< CaloRecGPU::CellInfoArr > > cell_info
std::map< std::string, CaloRecGPU::Helpers::CPU_object< CaloRecGPU::ClusterInfoArr > > cluster_info
std::map< std::string, CaloRecGPU::Helpers::CPU_object< CaloRecGPU::GeometryArr > > geometry
std::map< std::string, CaloRecGPU::Helpers::CPU_object< CaloRecGPU::CellNoiseArr > > noise
static std::string build_filename(const std::string &prefix, const size_t event_number, const std::string &suffix, const std::string &ext, const unsigned int num_width=9)
static FolderLoad load_folder(const std::filesystem::path &folder, int max_events=-1, const FolderLoadOptions &flo=FolderLoadOptions::None(), const bool output_messages=true)
static bool create_or_check_folder(const std::filesystem::path &folder, const bool output_errors=true)
static std::string build_filename(const std::string &prefix, const std::string &text, const std::string &suffix, const std::string &ext)
static ErrorState save_constants_to_folder(const std::filesystem::path &folder, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::GeometryArr > &geo, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::CellNoiseArr > &noise, const std::string &prefix="", const std::string &suffix="", const bool output_errors=true)
static ErrorState prepare_folder_for_output(const std::filesystem::path &folder, const bool output_errors=true)
static void report_error(const std::filesystem::path &file, const std::string &kind, const bool really_report=false)
static ErrorState save_event_to_folder(const size_t event_number, const std::filesystem::path &folder, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::CellInfoArr > &cell_info, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::ClusterInfoArr > &clusters, const std::string &prefix="", const std::string &suffix="", const unsigned int num_width=9, const bool output_errors=true)
static FolderLoad load_folder_filter(F &&filter_function, const std::filesystem::path &folder, int max_events=-1, const FolderLoadOptions &flo=FolderLoadOptions::None(), const bool output_messages=true)
static ErrorState save_cell_info_to_folder(const size_t event_number, const std::filesystem::path &folder, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::CellInfoArr > &cell_info, const std::string &prefix="", const std::string &suffix="", const unsigned int num_width=9, const bool output_errors=true)
static ErrorState save_clusters_to_folder(const size_t event_number, const std::filesystem::path &folder, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::ClusterInfoArr > &clusters, const std::string &prefix="", const std::string &suffix="", const unsigned int num_width=9, const bool output_errors=true)