Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-30 07:46:19

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
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 namespace ActsExamples {
0021 
0022 JsonMaterialWriter::JsonMaterialWriter(const JsonMaterialWriter::Config& config,
0023                                        Acts::Logging::Level level)
0024     : m_logger{Acts::getDefaultLogger("JsonMaterialWriter", level)},
0025       m_cfg(config),
0026       m_converter{std::make_unique<Acts::MaterialMapJsonConverter>(
0027           m_cfg.converterCfg, level)} {}
0028 
0029 JsonMaterialWriter::~JsonMaterialWriter() = default;
0030 
0031 void JsonMaterialWriter::writeMaterial(
0032     const Acts::TrackingGeometryMaterial& detMaterial) {
0033   // Evoke the converter
0034   auto jOut = m_converter->materialMapsToJson(detMaterial);
0035   // And write the file(s)
0036   if (ACTS_CHECK_BIT(m_cfg.writeFormat, JsonFormat::Json)) {
0037     std::string fileName = m_cfg.fileName + ".json";
0038     ACTS_VERBOSE("Writing to file: " << fileName);
0039     std::ofstream ofj(fileName);
0040     ofj << std::setw(4) << jOut << std::endl;
0041   }
0042   if (ACTS_CHECK_BIT(m_cfg.writeFormat, JsonFormat::Cbor)) {
0043     std::vector<std::uint8_t> cborOut = nlohmann::json::to_cbor(jOut);
0044     std::string fileName = m_cfg.fileName + ".cbor";
0045     ACTS_VERBOSE("Writing to file: " << fileName);
0046     std::ofstream ofj(fileName, std::ios::out | std::ios::binary);
0047     ofj.write(reinterpret_cast<char*>(cborOut.data()), cborOut.size());
0048   }
0049 }
0050 
0051 void JsonMaterialWriter::write(const Acts::TrackingGeometry& tGeometry) {
0052   // Evoke the converter
0053   auto jOut = m_converter->trackingGeometryToJson(tGeometry);
0054   // And write the file(s)
0055   if (ACTS_CHECK_BIT(m_cfg.writeFormat, 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, 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 }
0066 
0067 }  // namespace ActsExamples