ATLAS Offline Software
Loading...
Searching...
No Matches
xAODToTracccSpacePointConverterAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
5
8
11
12#include <limits>
13#include <memory>
14
15namespace ActsTrk {
16
18{
19 ATH_MSG_DEBUG("Initializing.");
20
21 if (m_inputSpacePointKeys.empty()) {
22 ATH_MSG_FATAL("No input space point containers configured");
23 return StatusCode::FAILURE;
24 }
25
26 ATH_CHECK(m_inputSpacePointKeys.initialize());
28
29 ATH_CHECK(m_outputSPKey.initialize());
31 ATH_CHECK(m_outputSPIndexKey.initialize());
32
33 ATH_CHECK(m_MRs.retrieve());
34 ATH_CHECK(m_copiesTool.retrieve());
35
36 ATH_MSG_DEBUG("Successfully initialized");
37 return StatusCode::SUCCESS;
38}
39
40StatusCode xAODToTracccSpacePointConverterAlg::execute(const EventContext& ctx) const
41{
42 using size_type = traccc::edm::spacepoint_collection::buffer::size_type;
43 constexpr unsigned int invalidIndex = std::numeric_limits<unsigned int>::max();
44
45 auto measToCluster = SG::makeHandle(m_inputMeasToClusterKey, ctx);
46 ATH_CHECK(measToCluster.isValid());
47
48 std::vector<unsigned int> clusterToMeas;
49 for (unsigned int measIdx = 0; measIdx < measToCluster->size(); ++measIdx) {
50 const unsigned int clusterIdx = (*measToCluster)[measIdx];
51 if (clusterIdx == invalidIndex) continue;
52 if (clusterIdx >= clusterToMeas.size()) clusterToMeas.resize(clusterIdx + 1, invalidIndex);
53 clusterToMeas[clusterIdx] = measIdx;
54 }
55
56 std::vector<SG::ReadHandle<xAOD::SpacePointContainer>> spacePointHandles =
57 m_inputSpacePointKeys.makeHandles(ctx);
58 size_type nSpacePoints = 0;
59 for (auto& handle : spacePointHandles) {
60 ATH_CHECK(handle.isValid());
61 nSpacePoints += handle->size();
62 }
63 ATH_MSG_DEBUG("Found " << nSpacePoints << " space points in "
64 << spacePointHandles.size() << " containers");
65
66 std::pmr::memory_resource* hostMR = m_MRs->hostMR();
67 auto hostCopy = m_copiesTool->hostCopy(ctx);
68 auto spHostBuffer = std::make_unique<traccc::edm::spacepoint_collection::buffer>(
69 nSpacePoints, hostMR ? *hostMR : m_MRs->mainMR());
70 hostCopy->setup(*spHostBuffer)->wait();
71
72 // Create a "device" collection around the buffer to work on it
73 traccc::edm::spacepoint_collection::device spacepoints{*spHostBuffer};
74
75 auto spToContainer = std::make_unique<std::vector<unsigned int>>(nSpacePoints, invalidIndex);
76 auto spToIndex = std::make_unique<std::vector<unsigned int>>(nSpacePoints, invalidIndex);
77
78 // The traccc buffers are not default initialized: all members must be set.
79 auto measurementIndexOf = [&](const xAOD::UncalibratedMeasurement* m) -> unsigned int {
80 if (m == nullptr || m->index() >= clusterToMeas.size()) return invalidIndex;
81 return clusterToMeas[m->index()];
82 };
83
85 size_type spIndex = 0;
86 for (unsigned int c = 0; c < spacePointHandles.size(); ++c) {
87 const xAOD::SpacePointContainer& container = *spacePointHandles[c];
88 for (unsigned int j = 0; j < container.size(); ++j) {
89 const xAOD::SpacePoint* xsp = container[j];
90 const auto& xmeas = xsp->measurements();
91 if (xmeas.empty() || xmeas.front() == nullptr) {
92 ATH_MSG_FATAL("Space point " << j << " in '" << m_inputSpacePointKeys[c].key()
93 << "' has no associated measurements");
94 return StatusCode::FAILURE;
95 }
96
97 const xAOD::UncalibMeasType type = xmeas.front()->type();
98 if (spacePointType == xAOD::UncalibMeasType::Other) {
99 spacePointType = type;
100 } else if (type != spacePointType) {
101 ATH_MSG_FATAL("Space point " << j << " in '" << m_inputSpacePointKeys[c].key()
102 << "' is of a different type than the previous space points, "
103 << "all input space points must be either pixel or strip");
104 return StatusCode::FAILURE;
105 }
106
107 const unsigned int idx1 = measurementIndexOf(xmeas[0]);
108 const unsigned int idx2 = (xmeas.size() > 1) ? measurementIndexOf(xmeas[1])
109 : traccc::edm::spacepoint_collection::device::INVALID_MEASUREMENT_INDEX;
110 if (idx1 == invalidIndex || (xmeas.size() > 1 && idx2 == invalidIndex)) {
111 ATH_MSG_FATAL("Space point " << j << " in '" << m_inputSpacePointKeys[c].key()
112 << "' references a cluster not covered by '" << m_inputMeasToClusterKey.key() << "'");
113 return StatusCode::FAILURE;
114 }
115
116 auto sp = spacepoints.at(spIndex);
117 sp.measurement_index_1() = idx1;
118 sp.measurement_index_2() = idx2;
119 sp.global() = {xsp->x(), xsp->y(), xsp->z()};
120 sp.z_variance() = xsp->varianceZ();
121 sp.radius_variance() = xsp->varianceR();
122
123 (*spToContainer)[spIndex] = c;
124 (*spToIndex)[spIndex] = j;
125 ++spIndex;
126 }
127 }
128
129 auto spHandle = SG::makeHandle(m_outputSPKey, ctx);
130 if (hostMR) {
131 auto deviceCopy = m_copiesTool->deviceCopy(ctx);
132 auto spDeviceBuffer = std::make_unique<traccc::edm::spacepoint_collection::buffer>(
133 spHostBuffer->capacity(), m_MRs->mainMR());
134
135 // We ignore() the setup and wait() on the copy to allow parallelism.
136 deviceCopy->setup(*spDeviceBuffer)->ignore();
137 (*deviceCopy)(*spHostBuffer, *spDeviceBuffer)->wait();
138 ATH_CHECK(spHandle.record(std::move(spDeviceBuffer)));
139 } else {
140 ATH_CHECK(spHandle.record(std::move(spHostBuffer)));
141 }
142 auto spToContainerHandle = SG::makeHandle(m_outputSPContainerKey, ctx);
143 ATH_CHECK(spToContainerHandle.record(std::move(spToContainer)));
144 auto spToIndexHandle = SG::makeHandle(m_outputSPIndexKey, ctx);
145 ATH_CHECK(spToIndexHandle.record(std::move(spToIndex)));
146
147 m_nSpacePoints += nSpacePoints;
148
149 ATH_MSG_DEBUG("Wrote " << nSpacePoints << " spacepoints to '"
150 << m_outputSPKey.key() << "'");
151 return StatusCode::SUCCESS;
152}
153
155{
156 ATH_MSG_DEBUG("Finalizing.");
157
158 ATH_MSG_INFO("Converted total number of space points = " << m_nSpacePoints);
159
160 return StatusCode::SUCCESS;
161}
162
163} // namespace ActsTrk
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(x,...)
static Double_t sp
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
virtual StatusCode initialize() override
Function initializing the algorithm.
SG::WriteHandleKey< std::vector< unsigned int > > m_outputSPContainerKey
SG::WriteHandleKey< std::vector< unsigned int > > m_outputSPIndexKey
virtual StatusCode finalize() override
Function finalizing the algorithm.
SG::ReadHandleKey< std::vector< unsigned int > > m_inputMeasToClusterKey
SG::ReadHandleKeyArray< xAOD::SpacePointContainer > m_inputSpacePointKeys
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
std::atomic< unsigned long > m_nSpacePoints
The object counter for debug prints in finalize method.
ToolHandle< AthDevice::IMemoryResourcesTool > m_MRs
SG::WriteHandleKey< traccc::edm::spacepoint_collection::buffer > m_outputSPKey
float varianceZ() const
float varianceR() const
Returns the variances.
float z() const
const std::vector< const xAOD::UncalibratedMeasurement * > & measurements() const
Returns the index of the measurements.
float y() const
float x() const
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
SpacePointContainer_v1 SpacePointContainer
Define the version of the space point container.
UncalibratedMeasurement_v1 UncalibratedMeasurement
Define the version of the uncalibrated measurement class.
UncalibMeasType
Define the type of the uncalibrated measurement.
ElementLink_p1< typename GenerateELinkIndexType_p1< typename LINK::index_type >::type > type