ATLAS Offline Software
Loading...
Searching...
No Matches
StandaloneDataIO Struct Reference

#include <StandaloneDataIO.h>

Collaboration diagram for StandaloneDataIO:

Classes

struct  ConstantInformation
struct  EventInformation
struct  FolderLoad
class  FolderLoadOptions
 The members of this structure should all be initialised by default to false. More...

Public Types

enum class  ErrorState { OK = 0 , ReadError , WriteError }

Static Public Member Functions

static ErrorState prepare_folder_for_output (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 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 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 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 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)
template<class F>
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 FolderLoad load_folder (const std::filesystem::path &folder, int max_events=-1, const FolderLoadOptions &flo=FolderLoadOptions::None(), const bool output_messages=true)

Static Protected Member Functions

static void report_error (const std::filesystem::path &file, const std::string &kind, const bool really_report=false)
static bool create_or_check_folder (const std::filesystem::path &folder, const bool output_errors=true)

Detailed Description

Definition at line 23 of file StandaloneDataIO.h.

Member Enumeration Documentation

◆ ErrorState

enum class StandaloneDataIO::ErrorState
strong
Enumerator
OK 
ReadError 
WriteError 

Definition at line 25 of file StandaloneDataIO.h.

26 {
27 OK = 0, ReadError, WriteError
28 };

Member Function Documentation

◆ build_filename() [1/2]

std::string StandaloneDataIO::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 )
inlinestatic

Definition at line 354 of file StandaloneDataIO.h.

359 {
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();
363 return build_filename(prefix, event_ID, suffix, ext);
364 }
static std::string build_filename(const std::string &prefix, const std::string &text, const std::string &suffix, const std::string &ext)

◆ build_filename() [2/2]

std::string StandaloneDataIO::build_filename ( const std::string & prefix,
const std::string & text,
const std::string & suffix,
const std::string & ext )
inlinestatic

Definition at line 345 of file StandaloneDataIO.h.

349 {
350 return prefix + (prefix.size() > 0 ? "_" : "") + text +
351 (suffix.size() > 0 ? "_" : "") + suffix + "." + ext;
352 }

◆ create_or_check_folder()

bool StandaloneDataIO::create_or_check_folder ( const std::filesystem::path & folder,
const bool output_errors = true )
inlinestaticprotected

Definition at line 310 of file StandaloneDataIO.h.

311 {
312 if (!std::filesystem::exists(folder))
313 {
314 if (!std::filesystem::create_directory(folder))
315 {
316 if (output_errors)
317 {
318 std::cout << "ERROR: folder '" << folder << "' could not be created." << std::endl;
319 }
320 return false;
321 }
322 }
323 else if (!std::filesystem::is_directory(folder))
324 {
325 if (output_errors)
326 {
327 std::cout << "ERROR: folder '" << folder << "' is not a valid folder." << std::endl;
328 }
329 return false;
330 }
331 return true;
332 }

◆ load_folder()

FolderLoad StandaloneDataIO::load_folder ( const std::filesystem::path & folder,
int max_events = -1,
const FolderLoadOptions & flo = FolderLoadOptions::None(),
const bool output_messages = true )
inlinestatic

Definition at line 649 of file StandaloneDataIO.h.

653 {
654 return load_folder_filter([&](const std::string &)
655 {
656 return false;
657 },
658 folder, max_events, flo, output_messages);
659 }
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)

◆ load_folder_filter()

template<class F>
FolderLoad StandaloneDataIO::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 )
inlinestatic
Parameters
filter_functionreceives a std::string (the filename), returns true if the file should be filtered out.

Definition at line 525 of file StandaloneDataIO.h.

532 {
533 FolderLoad ret;
534 if (!std::filesystem::is_directory(folder))
535 {
536 if (output_messages)
537 {
538 std::cout << "ERROR: '" << folder << "' is not a valid folder." << std::endl;
539 }
540 return ret;
541 }
542 std::set<std::string> read_one_part_of_v1_cells;
543 int event_count = 0;
544 for (const std::filesystem::path & file : std::filesystem::directory_iterator(folder))
545 {
546 if ( max_events > 0 && event_count >= max_events &&
547 ret.geometry.size() > 0 && ret.noise.size() > 0 )
548 {
549 break;
550 }
551
552 const bool can_load_events = (max_events < 0) || (event_count < max_events);
553 const std::string filename = file.stem().native();
554
555 if (filter_function(filename))
556 {
557 continue;
558 }
559
560 auto check_error = [&](const ErrorState & es, const std::string & str)
561 {
562 if (es == ErrorState::OK)
563 {
564 return false;
565 }
566 if (output_messages)
567 {
568 std::cout << "ERROR: '" << file << "' is not a valid " << str << " file (" << (int) es << ")." << std::endl;
569 }
570 return true;
571 };
572
573 auto output_loading_message = [&](const std::string & str)
574 {
575 if (output_messages)
576 {
577 std::cout << "Loaded " << str << " from '" << file << "'." << std::endl;
578 }
579 };
580
581 if (file.extension() == ".geometry")
582 {
583 if (!flo.load_geometry)
584 {
585 continue;
586 }
588 if (check_error(ConstantInformation::read_geometry(file, tempgeo), "geometry"))
589 {
590 continue;
591 }
592 ret.geometry[filename] = std::move(tempgeo);
593 output_loading_message("geometry");
594 }
595 else if (file.extension() == ".cellinfo")
596 {
597 if (!flo.load_cell_info || !can_load_events)
598 {
599 continue;
600 }
602 if (check_error(EventInformation::read_cell_info(file, tempcellinfo), "cell info"))
603 {
604 continue;
605 }
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)
609 {
610 ++event_count;
611 }
612 }
613 else if (file.extension() == ".clusterinfo")
614 {
615 if (!flo.load_cluster_info || !can_load_events)
616 {
617 continue;
618 }
620 if (check_error(EventInformation::read_cluster_info(file, tempclu), "cluster info"))
621 {
622 continue;
623 }
624 ret.cluster_info[filename] = std::move(tempclu);
625 output_loading_message("cluster info");
626 if (ret.cell_info.count(filename) > 0 || !flo.load_cell_info)
627 {
628 ++event_count;
629 }
630 }
631 else if (file.extension() == ".noise")
632 {
633 if (!flo.load_noise)
634 {
635 continue;
636 }
638 if (check_error(ConstantInformation::read_noise(file, tempnois), "noise"))
639 {
640 continue;
641 }
642 ret.noise[filename] = std::move(tempnois);
643 output_loading_message("noise");
644 }
645 }
646 return ret;
647 }
SimpleHolder< T, MemoryContext::CPU, true > CPU_object
Holds an object of type T in CPU memory.
static ErrorState read_noise(const std::filesystem::path &file, CaloRecGPU::Helpers::CPU_object< CaloRecGPU::CellNoiseArr > &noise, const bool report=false)
static ErrorState read_geometry(const std::filesystem::path &file, CaloRecGPU::Helpers::CPU_object< CaloRecGPU::GeometryArr > &geo, const bool report=false)
static ErrorState read_cluster_info(const std::filesystem::path &file, CaloRecGPU::Helpers::CPU_object< CaloRecGPU::ClusterInfoArr > &clusters, const bool report=false)
static ErrorState read_cell_info(const std::filesystem::path &file, CaloRecGPU::Helpers::CPU_object< CaloRecGPU::CellInfoArr > &cell_info, const bool report=false)
TFile * file

◆ prepare_folder_for_output()

ErrorState StandaloneDataIO::prepare_folder_for_output ( const std::filesystem::path & folder,
const bool output_errors = true )
inlinestatic

Definition at line 336 of file StandaloneDataIO.h.

337 {
338 if (!create_or_check_folder(folder, output_errors))
339 {
341 }
342 return ErrorState::OK;
343 }
static bool create_or_check_folder(const std::filesystem::path &folder, const bool output_errors=true)

◆ report_error()

void StandaloneDataIO::report_error ( const std::filesystem::path & file,
const std::string & kind,
const bool really_report = false )
inlinestaticprotected

Definition at line 32 of file StandaloneDataIO.h.

33 {
34 if (really_report)
35 {
36 std::cerr << "ERROR: when " << kind << " from '" << file << "'." << std::endl;
37 }
38 }

◆ save_cell_info_to_folder()

ErrorState StandaloneDataIO::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 )
inlinestatic

Definition at line 429 of file StandaloneDataIO.h.

436 {
437 if (!create_or_check_folder(folder, output_errors))
438 {
440 }
441
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();
445
446 auto filename = [&] (const std::string & ext)
447 {
448 return folder / build_filename(prefix, event_ID, suffix, ext);
449 };
450
451 if (EventInformation::write_cell_info(filename("cellinfo"), cell_info) != ErrorState::OK)
452 {
454 }
455 return ErrorState::OK;
456 }
static ErrorState write_cell_info(std::filesystem::path file, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::CellInfoArr > &cell_info, const bool report=false)

◆ save_clusters_to_folder()

ErrorState StandaloneDataIO::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 )
inlinestatic

Definition at line 458 of file StandaloneDataIO.h.

465 {
466 if (!create_or_check_folder(folder, output_errors))
467 {
469 }
470
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();
474
475 auto filename = [&] (const std::string & ext)
476 {
477 return folder / build_filename(prefix, event_ID, suffix, ext);
478 };
479
481 {
483 }
484 return ErrorState::OK;
485 }
static ErrorState write_cluster_info(std::filesystem::path file, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::ClusterInfoArr > &clusters, const bool report=false)

◆ save_constants_to_folder()

ErrorState StandaloneDataIO::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 )
inlinestatic

Definition at line 367 of file StandaloneDataIO.h.

373 {
374 if (!create_or_check_folder(folder, output_errors))
375 {
377 }
378
379 auto filename = [&] (const std::string & text, const std::string & ext)
380 {
381 return folder / build_filename(prefix, text, suffix, ext);
382 };
383
384 if (ConstantInformation::write_geometry(filename("geometry", "geo"), geo) != ErrorState::OK)
385 {
387 }
388 if (ConstantInformation::write_noise(filename("noise", "noise"), noise) != ErrorState::OK)
389 {
391 }
392 return ErrorState::OK;
393 }
static ErrorState write_noise(std::filesystem::path file, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::CellNoiseArr > &noise, const bool report=false)
static ErrorState write_geometry(std::filesystem::path file, const CaloRecGPU::Helpers::CPU_object< CaloRecGPU::GeometryArr > &geo, const bool report=false)

◆ save_event_to_folder()

ErrorState StandaloneDataIO::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 )
inlinestatic

Definition at line 395 of file StandaloneDataIO.h.

403 {
404 if (!create_or_check_folder(folder, output_errors))
405 {
407 }
408
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();
412
413 auto filename = [&] (const std::string & ext)
414 {
415 return folder / build_filename(prefix, event_ID, suffix, ext);
416 };
417
418 if (EventInformation::write_cell_info(filename("cellinfo"), cell_info) != ErrorState::OK)
419 {
421 }
422 if (EventInformation::write_cluster_info(filename("clusterinfo"), clusters) != ErrorState::OK)
423 {
425 }
426 return ErrorState::OK;
427 }

The documentation for this struct was generated from the following file: