Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JsonMaterialDecorator.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JsonMaterialDecorator.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 
9 // This file is part of the Acts project.
10 //
11 // Copyright (C) 2017-2019 CERN for the benefit of the Acts project
12 //
13 // This Source Code Form is subject to the terms of the Mozilla Public
14 // License, v. 2.0. If a copy of the MPL was not distributed with this
15 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
16 
18 
19 namespace Acts {
20 
22  const MaterialMapJsonConverter::Config& rConfig,
23  const std::string& jFileName, Acts::Logging::Level level,
24  bool clearSurfaceMaterial, bool clearVolumeMaterial)
25  : m_readerConfig(rConfig),
26  m_clearSurfaceMaterial(clearSurfaceMaterial),
27  m_clearVolumeMaterial(clearVolumeMaterial),
28  m_logger{getDefaultLogger("JsonMaterialDecorator", level)} {
29  // the material reader
30  Acts::MaterialMapJsonConverter jmConverter(rConfig, level);
31 
32  ACTS_VERBOSE("Reading JSON material description from: " << jFileName);
33  std::ifstream ifj(jFileName.c_str());
34  if (!ifj.good()) {
35  throw std::runtime_error{"Unable to open input JSON material file: " +
36  jFileName};
37  }
38  nlohmann::json jin;
39 
40  if (jFileName.find(".cbor") != std::string::npos) {
41  std::vector<std::uint8_t> iCbor((std::istreambuf_iterator<char>(ifj)),
42  std::istreambuf_iterator<char>());
43  jin = nlohmann::json::from_cbor(iCbor);
44  } else {
45  ifj >> jin;
46  }
47 
48  auto maps = jmConverter.jsonToMaterialMaps(jin);
51  ACTS_VERBOSE("JSON material description read complete");
52 }
53 
55  ACTS_VERBOSE("Processing surface: " << surface.geometryId());
56  // Clear the material if registered to do so
58  ACTS_VERBOSE("-> Clearing surface material");
59  surface.assignSurfaceMaterial(nullptr);
60  }
61  // Try to find the surface in the map
62  auto sMaterial = m_surfaceMaterialMap.find(surface.geometryId());
63  if (sMaterial != m_surfaceMaterialMap.end()) {
64  ACTS_VERBOSE("-> Found material for surface, assigning");
65  surface.assignSurfaceMaterial(sMaterial->second);
66  }
67 }
68 
73  ACTS_VERBOSE("Processing volume: " << volume.geometryId());
74  // Clear the material if registered to do so
76  ACTS_VERBOSE("-> Clearing volume material");
77  volume.assignVolumeMaterial(nullptr);
78  }
79  // Try to find the volume in the map
80  auto vMaterial = m_volumeMaterialMap.find(volume.geometryId());
81  if (vMaterial != m_volumeMaterialMap.end()) {
82  ACTS_VERBOSE("-> Found material for volume, assigning");
83  volume.assignVolumeMaterial(vMaterial->second);
84  }
85 }
86 } // namespace Acts