-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #772 from krasznaa/JsonIO-main-20241114
Digitization Config JSON I/O Update, main branch (2024.11.14.)
- Loading branch information
Showing
10 changed files
with
274 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.