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
12
13#include "traccc/io/read_detector_description.hpp"
14#include "traccc/io/read_detector.hpp"
15#include "traccc/io/data_format.hpp"
16
17#include "vecmem/utils/copy.hpp"
18
19#include <stdexcept>
20#include <algorithm>
21#include <fstream>
22#include <sstream>
23
24namespace ActsTrk {
25
27{
28 ATH_MSG_DEBUG("Initializing device detector description provider service ");
29
30 ATH_CHECK(m_MRs.retrieve());
31 ATH_CHECK(m_copy.retrieve());
33
34 auto hostDesign = std::make_unique<traccc::detector_design_description::host>(*m_MRs->hostMR());
35 auto hostCond = std::make_unique<traccc::detector_conditions_description::host>(*m_MRs->hostMR());
36
37 std::unique_ptr<traccc::detector_design_description::buffer> deviceDesign;
38 std::unique_ptr<traccc::detector_conditions_description::buffer> deviceCond;
39
40 auto copy = m_copy->copy(EventContext{});
41
42 if (m_geometryFile.value().empty() ||
43 m_digitizationFile.value().empty() ||
44 m_conditionsFile.value().empty()) {
45 ATH_MSG_FATAL("GeometryFile, " << m_geometryFile.value() <<
46 ", DigitizationFile, " << m_digitizationFile.value() << " or ConditionsFile, " << m_conditionsFile.value() << ", is empty!");
47 return StatusCode::FAILURE;
48 }
49
50 ATH_MSG_INFO("Reading detector description from files:"
51 << " geometry: " << m_geometryFile.value()
52 << ", digitization: " << m_digitizationFile.value()
53 << ", conditions: " << m_conditionsFile.value());
54
55 // Construct detector geometry
56 ATH_MSG_INFO("Loading traccc detector");
57 auto hostDetector = std::make_unique<traccc::host_detector>();
58 traccc::io::read_detector(
59 *hostDetector, *m_MRs->hostMR(),
61
62 auto deviceDetector =
63 std::make_unique<traccc::detector_buffer>(traccc::buffer_from_host_detector(*hostDetector, m_MRs->mainMR(), const_cast<vecmem::copy&>(*copy)));
64
65 // Construct detector description
66 traccc::io::read_detector_description(
67 *hostDesign, *hostCond,
71 traccc::data_format::json);
72
73 ATH_MSG_DEBUG(hostDesign->size() << " design entries, "
74 << hostCond->size() << " conditions entries");
75
76 // Copy design to device
77 std::vector<unsigned int> sizes;
78 sizes.reserve(hostDesign->size());
79 for (std::size_t i = 0; i < hostDesign->size(); ++i) {
80 const auto& e = hostDesign->at(i);
81 sizes.push_back(static_cast<unsigned int>(
82 std::max(e.bin_edges_x().size(), e.bin_edges_y().size())));
83 }
84
85 deviceDesign =
86 std::make_unique<traccc::detector_design_description::buffer>(
87 sizes, m_MRs->mainMR(), m_MRs->hostMR(),
88 vecmem::data::buffer_type::resizable);
89 (*copy).setup(*deviceDesign)->wait();
90 (*copy)(vecmem::get_data(*hostDesign), *deviceDesign)->wait();
91
92 // Copy conditions to device
93 deviceCond =
94 std::make_unique<traccc::detector_conditions_description::buffer>(
95 static_cast<traccc::detector_conditions_description::buffer::size_type>(
96 hostCond->size()),
97 m_MRs->mainMR());
98 (*copy).setup(*deviceCond)->wait();
99 (*copy)(vecmem::get_data(*hostCond), *deviceCond)->wait();
100
101 ATH_MSG_INFO("Detector description built from files");
102
103 // Record device and host objects
104 // Host objects are needed for EDM conversions
105 constexpr bool allowMods = false;
106 ATH_CHECK(m_detStore->record(std::move(deviceDesign), m_deviceDesignObjectName.value(), allowMods));
107 ATH_CHECK(m_detStore->record(std::move(deviceCond), m_deviceCondObjectName.value(), allowMods));
108 ATH_CHECK(m_detStore->record(std::move(hostDesign), m_hostDesignObjectName.value(), allowMods));
109 ATH_CHECK(m_detStore->record(std::move(hostCond), m_hostCondObjectName.value(), allowMods));
110
111 ATH_CHECK(m_detStore->record(std::move(deviceDetector), m_deviceDetectorName.value(), allowMods));
112 ATH_CHECK(m_detStore->record(std::move(hostDetector), m_hostDetectorName.value(), allowMods));
113
114 ATH_MSG_DEBUG("Successfully initialized");
115 return StatusCode::SUCCESS;
116}
117
118const std::unordered_map<uint64_t, Identifier>& JSONDeviceDetectorDescriptionProviderSvc::detrayToAthenaMap() const {
119 return m_detrayToAthena;
120}
121
122const std::unordered_map<Identifier, uint64_t>& JSONDeviceDetectorDescriptionProviderSvc::athenaToDetrayMap() const {
123 return m_athenaToDetray;
124}
125
127{
128 if (m_mapFile.value().empty()) {
129 ATH_MSG_FATAL("MapFile not set — detray<->Athena maps will be empty");
130 return StatusCode::FAILURE;
131 }
132
133 ATH_MSG_INFO("Loading detray<->Athena map from "
134 << m_mapFile.value());
135
136 std::ifstream mapFile(PathResolverFindCalibFile(m_mapFile.value()));
137 if (!mapFile.is_open()) {
138 ATH_MSG_FATAL("Cannot open map file: " << m_mapFile.value());
139 return StatusCode::FAILURE;
140 }
141
142 std::string line;
143 while (std::getline(mapFile, line)) {
144 if (line.empty()) continue;
145 std::stringstream ss(line);
146 std::string athenaStr, detrayStr;
147 if (!std::getline(ss, athenaStr, ',') ||
148 !std::getline(ss, detrayStr, ',')) continue;
149
150 if (athenaStr.empty()) {
151 ATH_MSG_ERROR("Empty Athena identifier string in map file — skipping");
152 return StatusCode::FAILURE;
153 }
154 Identifier athenaId;
155 athenaId.set(athenaStr);
156
157 uint64_t detrayId = 0;
158 try {
159 detrayId = std::stoull(detrayStr);
160 } catch (const std::exception& e) {
161 ATH_MSG_ERROR("Failed to parse detray identifier '" << detrayStr << "': " << e.what());
162 return StatusCode::FAILURE;
163 }
164
165 m_athenaToDetray[athenaId] = detrayId;
166 m_detrayToAthena[detrayId] = athenaId;
167 }
168
169 ATH_MSG_INFO("Loaded " << m_athenaToDetray.size()
170 << " detray<->Athena module mappings");
171 return StatusCode::SUCCESS;
172}
173
174} // 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)
ToolHandle< AthDevice::ICopyTool > m_copy
The copy tool used for copying data to device.
virtual const std::unordered_map< uint64_t, Identifier > & detrayToAthenaMap() const override
virtual StatusCode initialize() override
Function initializing and executing the file loading.
virtual const std::unordered_map< Identifier, uint64_t > & athenaToDetrayMap() const override
StatusCode loadIdMaps()
Helper function to load Athena<->detray ID maps from csv.
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...