Skip to content

Commit

Permalink
Merge pull request #772 from krasznaa/JsonIO-main-20241114
Browse files Browse the repository at this point in the history
Digitization Config JSON I/O Update, main branch (2024.11.14.)
  • Loading branch information
krasznaa authored Nov 15, 2024
2 parents 21f3453 + f473552 commit 25c71d4
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 66 deletions.
4 changes: 4 additions & 0 deletions io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ traccc_add_library( traccc_io io TYPE SHARED
"src/csv/make_particle_reader.cpp"
"src/csv/read_particles.hpp"
"src/csv/read_particles.cpp"
"src/json/read_digitization_config.hpp"
"src/json/read_digitization_config.cpp"
"src/json/write_digitization_config.hpp"
"src/json/write_digitization_config.cpp"
"src/obj/write_seeds.hpp"
"src/obj/write_seeds.cpp"
"src/obj/write_spacepoints.hpp"
Expand Down
10 changes: 10 additions & 0 deletions io/include/traccc/io/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "traccc/edm/spacepoint.hpp"
#include "traccc/edm/track_candidate.hpp"
#include "traccc/io/data_format.hpp"
#include "traccc/io/digitization_config.hpp"

// Detray include(s).
#include "detray/core/detector.hpp"
Expand Down Expand Up @@ -81,4 +82,13 @@ void write(std::size_t event, std::string_view directory,
track_candidate_container_types::const_view tracks,
const detray::detector<>& detector);

/// Write a digitization configuration to a file
///
/// @param filename The name of the file to write the data to
/// @param format The format of the output file
/// @param config The digitization configuration to write
///
void write(std::string_view filename, data_format format,
const digitization_config& config);

} // namespace traccc::io
71 changes: 71 additions & 0 deletions io/src/json/read_digitization_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// Local include(s).
#include "read_digitization_config.hpp"

// Acts include(s).
#include <Acts/Plugins/Json/ActsJson.hpp>
#include <Acts/Plugins/Json/GeometryHierarchyMapJsonConverter.hpp>
#include <Acts/Plugins/Json/UtilitiesJsonConverter.hpp>

// System include(s).
#include <fstream>

namespace traccc {

/// Function allowing the read of @c traccc::module_digitization_config objects
///
/// Note that this function must be declared in the same namespace as
/// @c traccc::module_digitization_config for nlohmann_json to work correctly.
///
void from_json(const nlohmann::json& json, module_digitization_config& cfg) {

// Names/keywords used in the JSON file.
static const char* geometric = "geometric";
static const char* segmentation = "segmentation";
static const char* binningdata = "binningdata";
static const char* bins = "bins";

// Read the binning information, if possible.
if (json.find(geometric) != json.end()) {
const auto& json_geom = json[geometric];
if (json_geom.find(segmentation) != json_geom.end()) {
Acts::from_json(json_geom[segmentation], cfg.segmentation);
// If we only have 1 bins along any axis, then this is a 1D module.
const auto& json_segm = json_geom[segmentation];
for (const auto& bindata : json_segm[binningdata]) {
if (bindata[bins].get<int>() == 1) {
cfg.dimensions = 1;
break;
}
}
}
}
}

namespace io::json {

digitization_config read_digitization_config(std::string_view filename) {

// Open the input file. Relying on exceptions for the error handling.
std::ifstream infile(filename.data(), std::ifstream::binary);
infile.exceptions(std::ofstream::failbit | std::ofstream::badbit);

// Read the contents of the file into a JSON object.
nlohmann::json json;
infile >> json;

// Construct the object from the JSON configuration.
static const Acts::GeometryHierarchyMapJsonConverter<
module_digitization_config>
converter{"digitization-configuration"};
return converter.fromJson(json);
}

} // namespace io::json
} // namespace traccc
26 changes: 26 additions & 0 deletions io/src/json/read_digitization_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

// Project include(s).
#include "traccc/io/digitization_config.hpp"

// System include(s).
#include <string_view>

namespace traccc::io::json {

/// Read the detector digitization configuration from a JSON input file
///
/// @param filename The name of the file to read the data from
/// @return An object describing the digitization configuration of the
/// detector
///
digitization_config read_digitization_config(std::string_view filename);

} // namespace traccc::io::json
59 changes: 59 additions & 0 deletions io/src/json/write_digitization_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// Local include(s).
#include "write_digitization_config.hpp"

// Acts include(s).
#include <Acts/Plugins/Json/ActsJson.hpp>
#include <Acts/Plugins/Json/GeometryHierarchyMapJsonConverter.hpp>
#include <Acts/Plugins/Json/UtilitiesJsonConverter.hpp>

// System include(s).
#include <fstream>

namespace traccc {

/// Function allowing the write of @c traccc::module_digitization_config objects
///
/// Note that this function must be declared in the same namespace as
/// @c traccc::module_digitization_config for nlohmann_json to work correctly.
///
void to_json(nlohmann::json& json, const module_digitization_config& cfg) {

// Names/keywords used in the JSON file.
static const char* geometric = "geometric";
static const char* segmentation = "segmentation";

// Write the binning information.
json[geometric][segmentation] = cfg.segmentation;

// The dimensions variable is determined on reading from the segmentation
// information, so it does not need to be written separately.
}

namespace io::json {

void write_digitization_config(std::string_view filename,
const digitization_config& config) {

// Construct the JSON object to be written.
static const Acts::GeometryHierarchyMapJsonConverter<
module_digitization_config>
converter{"digitization-configuration"};
const nlohmann::json json = converter.toJson(config, nullptr);

// Open the input file. Relying on exceptions for the error handling.
std::ofstream outfile(filename.data(), std::ifstream::binary);
outfile.exceptions(std::ofstream::failbit | std::ofstream::badbit);

// Write the JSON object to the file.
outfile << json.dump(4);
}

} // namespace io::json
} // namespace traccc
26 changes: 26 additions & 0 deletions io/src/json/write_digitization_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

// Project include(s).
#include "traccc/io/digitization_config.hpp"

// System include(s).
#include <string_view>

namespace traccc::io::json {

/// Write a digitization configuration to a file
///
/// @param filename The name of the file to write the data to
/// @param config The digitization configuration to write
///
void write_digitization_config(std::string_view filename,
const digitization_config& config);

} // namespace traccc::io::json
65 changes: 3 additions & 62 deletions io/src/read_digitization_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,13 @@
// Local include(s).
#include "traccc/io/read_digitization_config.hpp"

#include "json/read_digitization_config.hpp"
#include "traccc/io/utils.hpp"

// Acts include(s).
#include <Acts/Plugins/Json/ActsJson.hpp>
#include <Acts/Plugins/Json/GeometryHierarchyMapJsonConverter.hpp>
#include <Acts/Plugins/Json/UtilitiesJsonConverter.hpp>

// System include(s).
#include <algorithm>
#include <fstream>
#include <string>

namespace traccc {

/// Function allowing the read of @c traccc::module_digitization_config objects
///
/// Note that this function must be declared in the same namespace as
/// @c traccc::module_digitization_config for nlohmann_json to work correctly.
///
void from_json(const nlohmann::json& json, module_digitization_config& cfg) {

// Names/keywords used in the JSON file.
static const char* geometric = "geometric";
static const char* segmentation = "segmentation";
static const char* binningdata = "binningdata";
static const char* bins = "bins";

// Read the binning information, if possible.
if (json.find(geometric) != json.end()) {
const auto& json_geom = json[geometric];
if (json_geom.find(segmentation) != json_geom.end()) {
Acts::from_json(json_geom[segmentation], cfg.segmentation);
// If we only have 1 bins along any axis, then this is a 1D module.
const auto& json_segm = json_geom[segmentation];
for (const auto& bindata : json_segm[binningdata]) {
if (bindata[bins].get<int>() == 1) {
cfg.dimensions = 1;
break;
}
}
}
}
}

namespace io {
namespace json {

digitization_config read_digitization_config(std::string_view filename) {

// Open the input file. Relying on exceptions for the error handling.
std::ifstream infile(filename.data(), std::ifstream::binary);
infile.exceptions(std::ofstream::failbit | std::ofstream::badbit);

// Read the contents of the file into a JSON object.
nlohmann::json json;
infile >> json;

// Construct the object from the JSON configuration.
static const Acts::GeometryHierarchyMapJsonConverter<
module_digitization_config>
converter{"digitization-configuration"};
return converter.fromJson(json);
}

} // namespace json
namespace traccc::io {

digitization_config read_digitization_config(std::string_view filename,
data_format format) {
Expand All @@ -89,5 +31,4 @@ digitization_config read_digitization_config(std::string_view filename,
}
}

} // namespace io
} // namespace traccc
} // namespace traccc::io
14 changes: 14 additions & 0 deletions io/src/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Local include(s).
#include "traccc/io/write.hpp"

#include "json/write_digitization_config.hpp"
#include "obj/write_seeds.hpp"
#include "obj/write_spacepoints.hpp"
#include "obj/write_track_candidates.hpp"
Expand Down Expand Up @@ -120,4 +121,17 @@ void write(std::size_t event, std::string_view directory,
}
}

void write(std::string_view filename, data_format format,
const digitization_config& config) {

switch (format) {
case data_format::json:
json::write_digitization_config(get_absolute_path(filename),
config);
break;
default:
throw std::invalid_argument("Unsupported data format");
}
}

} // namespace traccc::io
9 changes: 5 additions & 4 deletions tests/io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# TRACCC library, part of the ACTS project (R&D line)
#
# (c) 2021-2022 CERN for the benefit of the ACTS project
# (c) 2021-2024 CERN for the benefit of the ACTS project
#
# Mozilla Public License Version 2.0

# Declare the io library test(s).
traccc_add_test( io
"test_binary.cpp"
"test_csv.cpp"
traccc_add_test( io
"test_binary.cpp"
"test_csv.cpp"
"test_event_data.cpp"
"test_json.cpp"
LINK_LIBRARIES GTest::gtest_main traccc_tests_common
traccc::core traccc::io traccc::performance )

Expand Down
Loading

0 comments on commit 25c71d4

Please sign in to comment.