File indexing completed on 2025-01-31 09:17:02
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ActsExamples/Io/Json/JsonMaterialWriter.hpp"
0010
0011 #include "Acts/Utilities/Helpers.hpp"
0012
0013 #include <fstream>
0014 #include <iomanip>
0015 #include <ios>
0016 #include <vector>
0017
0018 #include <nlohmann/json.hpp>
0019
0020 ActsExamples::JsonMaterialWriter::JsonMaterialWriter(
0021 const ActsExamples::JsonMaterialWriter::Config& config,
0022 Acts::Logging::Level level)
0023 : m_logger{Acts::getDefaultLogger("JsonMaterialWriter", level)},
0024 m_cfg(config),
0025 m_converter{std::make_unique<Acts::MaterialMapJsonConverter>(
0026 m_cfg.converterCfg, level)} {}
0027
0028 ActsExamples::JsonMaterialWriter::~JsonMaterialWriter() = default;
0029
0030 void ActsExamples::JsonMaterialWriter::writeMaterial(
0031 const Acts::DetectorMaterialMaps& detMaterial) {
0032
0033 auto jOut = m_converter->materialMapsToJson(detMaterial);
0034
0035 if (ACTS_CHECK_BIT(m_cfg.writeFormat, ActsExamples::JsonFormat::Json)) {
0036 std::string fileName = m_cfg.fileName + ".json";
0037 ACTS_VERBOSE("Writing to file: " << fileName);
0038 std::ofstream ofj(fileName);
0039 ofj << std::setw(4) << jOut << std::endl;
0040 }
0041 if (ACTS_CHECK_BIT(m_cfg.writeFormat, ActsExamples::JsonFormat::Cbor)) {
0042 std::vector<std::uint8_t> cborOut = nlohmann::json::to_cbor(jOut);
0043 std::string fileName = m_cfg.fileName + ".cbor";
0044 ACTS_VERBOSE("Writing to file: " << fileName);
0045 std::ofstream ofj(fileName, std::ios::out | std::ios::binary);
0046 ofj.write(reinterpret_cast<char*>(cborOut.data()), cborOut.size());
0047 }
0048 }
0049
0050 void ActsExamples::JsonMaterialWriter::write(
0051 const Acts::TrackingGeometry& tGeometry) {
0052
0053 auto jOut = m_converter->trackingGeometryToJson(tGeometry);
0054
0055 if (ACTS_CHECK_BIT(m_cfg.writeFormat, ActsExamples::JsonFormat::Json)) {
0056 std::ofstream ofj(m_cfg.fileName + ".json");
0057 ofj << std::setw(4) << jOut << std::endl;
0058 }
0059 if (ACTS_CHECK_BIT(m_cfg.writeFormat, ActsExamples::JsonFormat::Cbor)) {
0060 std::vector<std::uint8_t> cborOut = nlohmann::json::to_cbor(jOut);
0061 std::ofstream ofj(m_cfg.fileName + ".cbor",
0062 std::ios::out | std::ios::binary);
0063 ofj.write(reinterpret_cast<char*>(cborOut.data()), cborOut.size());
0064 }
0065 }