Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JsonMaterialWriter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JsonMaterialWriter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
12 
13 #include <fstream>
14 #include <iomanip>
15 #include <ios>
16 #include <vector>
17 
18 #include <nlohmann/json.hpp>
19 
23  : m_logger{Acts::getDefaultLogger("JsonMaterialWriter", level)},
24  m_cfg(config),
25  m_converter{std::make_unique<Acts::MaterialMapJsonConverter>(
26  m_cfg.converterCfg, level)} {}
27 
29 
31  const Acts::DetectorMaterialMaps& detMaterial) {
32  // Evoke the converter
33  auto jOut = m_converter->materialMapsToJson(detMaterial);
34  // And write the file(s)
35  if (ACTS_CHECK_BIT(m_cfg.writeFormat, ActsExamples::JsonFormat::Json)) {
36  std::string fileName = m_cfg.fileName + ".json";
37  ACTS_VERBOSE("Writing to file: " << fileName);
38  std::ofstream ofj(fileName);
39  ofj << std::setw(4) << jOut << std::endl;
40  }
41  if (ACTS_CHECK_BIT(m_cfg.writeFormat, ActsExamples::JsonFormat::Cbor)) {
42  std::vector<uint8_t> cborOut = nlohmann::json::to_cbor(jOut);
43  std::string fileName = m_cfg.fileName + ".cbor";
44  ACTS_VERBOSE("Writing to file: " << fileName);
45  std::ofstream ofj(fileName, std::ios::out | std::ios::binary);
46  ofj.write((char*)cborOut.data(), cborOut.size());
47  }
48 }
49 
52  // Evoke the converter
53  auto jOut = m_converter->trackingGeometryToJson(tGeometry);
54  // And write the file(s)
55  if (ACTS_CHECK_BIT(m_cfg.writeFormat, ActsExamples::JsonFormat::Json)) {
56  std::ofstream ofj(m_cfg.fileName + ".json");
57  ofj << std::setw(4) << jOut << std::endl;
58  }
59  if (ACTS_CHECK_BIT(m_cfg.writeFormat, ActsExamples::JsonFormat::Cbor)) {
60  std::vector<uint8_t> cborOut = nlohmann::json::to_cbor(jOut);
61  std::ofstream ofj(m_cfg.fileName + ".cbor",
62  std::ios::out | std::ios::binary);
63  ofj.write((char*)cborOut.data(), cborOut.size());
64  }
65 }