ATLAS Offline Software
Loading...
Searching...
No Matches
JSONDeviceDetectorDescriptionProviderSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
6
11
12#include "traccc/io/read_detector_description.hpp"
13#include "traccc/io/data_format.hpp"
14
15#include "vecmem/utils/copy.hpp"
16
17#include <stdexcept>
18#include <algorithm>
19#include <fstream>
20#include <sstream>
21
22namespace ActsTrk {
23
25{
26 ATH_MSG_DEBUG("Initializing device detector description provider service ");
27
28 ATH_CHECK(m_hostMR.retrieve());
29 ATH_CHECK(m_deviceMR.retrieve());
30 ATH_CHECK(m_copy.retrieve());
31
32 std::unique_ptr<traccc::detector_design_description::host> hostDesign;
33 std::unique_ptr<traccc::detector_conditions_description::host> hostCond;
34 std::unique_ptr<traccc::detector_design_description::buffer> deviceDesign;
35 std::unique_ptr<traccc::detector_conditions_description::buffer> deviceCond;
36
38 auto copy = m_copy->copy(EventContext{});
39 ATH_CHECK(buildFromFile(m_hostMR->mr(), m_deviceMR->mr(), *copy, hostDesign, hostCond, deviceDesign, deviceCond));
40
41 // Record device and host objects
42 // Host objects are needed for EDM conversions
43 ATH_CHECK(m_detStore->record(std::move(deviceDesign), m_deviceDesignObjectName.value()));
44 ATH_CHECK(m_detStore->record(std::move(deviceCond), m_deviceCondObjectName.value()));
45 ATH_CHECK(m_detStore->record(std::move(hostDesign), m_hostDesignObjectName.value()));
46 ATH_CHECK(m_detStore->record(std::move(hostCond), m_hostCondObjectName.value()));
47
48 ATH_MSG_DEBUG("Successfully initialized");
49 return StatusCode::SUCCESS;
50}
51
52const std::unordered_map<uint64_t, Identifier>& JSONDeviceDetectorDescriptionProviderSvc::detrayToAthenaMap() const {
53 return m_detrayToAthena;
54}
55
56const std::unordered_map<Identifier, uint64_t>& JSONDeviceDetectorDescriptionProviderSvc::athenaToDetrayMap() const {
57 return m_athenaToDetray;
58}
59
61{
62 if (m_mapFile.value().empty()) {
63 ATH_MSG_FATAL("MapFile not set — detray<->Athena maps will be empty");
64 return StatusCode::FAILURE;
65 }
66
67 ATH_MSG_INFO("Loading detray<->Athena map from "
68 << m_mapFile.value());
69
70 std::ifstream mapFile(PathResolverFindCalibFile(m_mapFile.value()));
71 if (!mapFile.is_open()) {
72 ATH_MSG_FATAL("Cannot open map file: " << m_mapFile.value());
73 return StatusCode::FAILURE;
74 }
75
76 std::string line;
77 while (std::getline(mapFile, line)) {
78 if (line.empty()) continue;
79 std::stringstream ss(line);
80 std::string athenaStr, detrayStr;
81 if (!std::getline(ss, athenaStr, ',') ||
82 !std::getline(ss, detrayStr, ',')) continue;
83
84 if (athenaStr.empty()) {
85 ATH_MSG_ERROR("Empty Athena identifier string in map file — skipping");
86 return StatusCode::FAILURE;
87 }
88 Identifier athenaId;
89 athenaId.set(athenaStr);
90
91 uint64_t detrayId = 0;
92 try {
93 detrayId = std::stoull(detrayStr);
94 } catch (const std::exception& e) {
95 ATH_MSG_ERROR("Failed to parse detray identifier '" << detrayStr << "': " << e.what());
96 return StatusCode::FAILURE;
97 }
98
99 m_athenaToDetray[athenaId] = detrayId;
100 m_detrayToAthena[detrayId] = athenaId;
101 }
102
103 ATH_MSG_INFO("Loaded " << m_athenaToDetray.size()
104 << " detray<->Athena module mappings");
105 return StatusCode::SUCCESS;
106}
107
109 std::pmr::memory_resource& hostMR,
110 std::pmr::memory_resource& deviceMR,
111 const vecmem::copy& copy,
112 std::unique_ptr<traccc::detector_design_description::host>& hostDesign,
113 std::unique_ptr<traccc::detector_conditions_description::host>& hostCond,
114 std::unique_ptr<traccc::detector_design_description::buffer>& deviceDesign,
115 std::unique_ptr<traccc::detector_conditions_description::buffer>& deviceCond)
116{
117 if (m_geometryFile.value().empty() ||
118 m_digitizationFile.value().empty() ||
119 m_conditionsFile.value().empty()) {
120 ATH_MSG_FATAL("GeometryFile, " << m_geometryFile.value() <<
121 ", DigitizationFile, " << m_digitizationFile.value() << " or ConditionsFile, " << m_conditionsFile.value() << ", is empty!");
122 return StatusCode::FAILURE;
123 }
124
125 ATH_MSG_INFO("Reading detector description from files:"
126 << " geometry: " << m_geometryFile.value()
127 << ", digitization: " << m_digitizationFile.value()
128 << ", conditions: " << m_conditionsFile.value());
129
130 hostDesign = std::make_unique<traccc::detector_design_description::host>(hostMR);
131 hostCond = std::make_unique<traccc::detector_conditions_description::host>(hostMR);
132
133 traccc::io::read_detector_description(
134 *hostDesign, *hostCond,
138 traccc::data_format::json);
139
140 ATH_MSG_DEBUG(hostDesign->size() << " design entries, "
141 << hostCond->size() << " conditions entries");
142
143 // Copy design to device
144 std::vector<unsigned int> sizes;
145 sizes.reserve(hostDesign->size());
146 for (std::size_t i = 0; i < hostDesign->size(); ++i) {
147 const auto& e = hostDesign->at(i);
148 sizes.push_back(static_cast<unsigned int>(
149 std::max(e.bin_edges_x().size(), e.bin_edges_y().size())));
150 }
151
152 deviceDesign =
153 std::make_unique<traccc::detector_design_description::buffer>(
154 sizes, deviceMR, &hostMR,
155 vecmem::data::buffer_type::resizable);
156 copy.setup(*deviceDesign)->wait();
157 copy(vecmem::get_data(*hostDesign), *deviceDesign)->wait();
158
159 // Copy conditions to device
160 deviceCond =
161 std::make_unique<traccc::detector_conditions_description::buffer>(
162 static_cast<traccc::detector_conditions_description::buffer::size_type>(
163 hostCond->size()),
164 deviceMR);
165 copy.setup(*deviceCond)->wait();
166 copy(vecmem::get_data(*hostCond), *deviceCond)->wait();
167
168 ATH_MSG_INFO("Detector description built from files");
169 return StatusCode::SUCCESS;
170}
171
172} // namespace ActsTrk
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
static Double_t ss
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
virtual const std::unordered_map< uint64_t, Identifier > & detrayToAthenaMap() const override
virtual const std::unordered_map< Identifier, uint64_t > & athenaToDetrayMap() const override
StatusCode buildFromFile(std::pmr::memory_resource &hostMR, std::pmr::memory_resource &deviceMR, const vecmem::copy &copy, std::unique_ptr< traccc::detector_design_description::host > &hostDesign, std::unique_ptr< traccc::detector_conditions_description::host > &hostCond, std::unique_ptr< traccc::detector_design_description::buffer > &deviceDesign, std::unique_ptr< traccc::detector_conditions_description::buffer > &deviceCond)
void set(std::string_view id)
build from a string form - hexadecimal
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...