ATLAS Offline Software
Loading...
Searching...
No Matches
TrackContainerHandlesHelper.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
8
9#include "GeoModelKernel/throwExcept.h"
10
11#include <string>
12#include <sstream>
13#include <span>
14#include <regex>
15
18
19namespace ActsTrk {
20
21std::string prefixFromTrackContainerName(const std::string& tracks) {
22 std::regex word_regex("(.*)Tracks$");
23 std::smatch match_regex;
24
25 if ( not std::regex_search(tracks, match_regex, word_regex) or match_regex.size() < 2) {
26 return tracks;
27 }
28
29 return match_regex[1].str();
30}
31
32#define RECORD_xAOD(key, container, auxContainer, ctx) \
33 { \
34 SG::WriteHandle handle{key, ctx}; \
35 if (!handle.record(std::move(container), \
36 std::move(auxContainer)).isSuccess()){ \
37 THROW_EXCEPTION("Cannot record "<<key.fullKey()<<" backend"); \
38 } \
39 }
40
41#define INIT_CHECK(key) \
42 if (!key.initialize().isSuccess()) { \
43 THROW_EXCEPTION("Failed to initialize key "<<key.fullKey()); \
44 }
45
47 const std::string& prefix) {
48 m_statesKey = prefix + "TrackStates";
49 m_parametersKey = prefix + "TrackParameters";
50 m_jacobiansKey = prefix + "TrackJacobians";
51 m_measurementsKey = prefix + "TrackMeasurements";
52 m_surfacesKey = prefix + "TrackStateSurfaces";
53 m_mtjKey =
54 prefix + "MultiTrajectory"; // identical names, underlying types are distinct
61
62 // Track Container backends
63 m_xAODTrackSummaryKey = prefix + "TrackSummary";
64 m_trackSurfacesKey = prefix + "TrackSurfaces";
66 prefix +
67 "TrackSummary"; // identical names, underlying types are distinct
68
72 return StatusCode::SUCCESS;
73}
74
75std::unique_ptr<ActsTrk::MultiTrajectory>
77 ActsTrk::MutableMultiTrajectory&& mmtj, const EventContext& evtContext) const {
78
79 mmtj.trim();
80
81 auto statesBackendHandle = SG::makeHandle(m_statesKey, evtContext);
82 auto statesInterface =
84 mmtj.trackStatesAux());
85 RECORD_xAOD(m_statesKey, statesInterface, mmtj.m_trackStatesAux, evtContext);
86
87 auto parametersInterface =
89 mmtj.trackParametersAux());
90 RECORD_xAOD(m_parametersKey, parametersInterface, mmtj.m_trackParametersAux, evtContext);
91
92 auto jacobiansInterface =
94 mmtj.trackJacobiansAux());
95 RECORD_xAOD(m_jacobiansKey, jacobiansInterface, mmtj.m_trackJacobiansAux, evtContext);
96
97 auto measurementsInterface =
99 mmtj.trackMeasurementsAux());
100 RECORD_xAOD(m_measurementsKey, measurementsInterface, mmtj.m_trackMeasurementsAux, evtContext);
101
102 auto surfacesBackendHandle = SG::makeHandle(m_surfacesKey, evtContext);
103 RECORD_xAOD(m_surfacesKey, mmtj.m_surfacesBackend, mmtj.m_surfacesBackendAux, evtContext);
104
105 // construct const MTJ version
106 auto cmtj = std::make_unique<ActsTrk::MultiTrajectory>(
108 evtContext),
110 m_parametersKey.key() + "Aux.", evtContext),
112 m_jacobiansKey.key() + "Aux.", evtContext),
114 m_measurementsKey.key() + "Aux.", evtContext),
116 m_surfacesKey.key() + "Aux.", evtContext)
117 );
118 cmtj->moveSurfaces(&mmtj);
119 cmtj->moveLinks(&mmtj);
120
121 return cmtj;
122}
123
124std::unique_ptr<ActsTrk::PersistentTrackContainer>
126 ActsTrk::MutablePersistentTrackContainer&& tc, const Acts::GeometryContext& geoContext, const EventContext& evtContext) const {
127
128
129 std::unique_ptr<ActsTrk::MultiTrajectory> constMtj =
130 moveToConst(std::move(tc.trackStateContainer()), evtContext);
131
132 auto constMtjHandle = SG::makeHandle(m_mtjKey, evtContext);
133 if (constMtjHandle.record(std::move(constMtj)).isFailure()) {
134 throw std::runtime_error(
135 "MutableTrackContainerHandlesHelper::moveToConst, can't record "
136 "ConstMultiTrajectory");
137 }
138
139 auto trackSurfacesAux = std::make_unique<xAOD::TrackSurfaceAuxContainer>();
140 tc.container().encodeSurfaces(trackSurfacesAux.get(), geoContext);
141
142 auto interfaceTrackSummaryContainer =
144 tc.container().m_mutableTrackBackendAux.get());
145 RECORD_xAOD(m_xAODTrackSummaryKey, interfaceTrackSummaryContainer, tc.container().m_mutableTrackBackendAux, evtContext);
146
148 trackSurfacesAux.get());
149 RECORD_xAOD(m_trackSurfacesKey, trackSurfaces, trackSurfacesAux, evtContext);
150
151 auto constTrackSummary = std::make_unique<ActsTrk::TrackSummaryContainer>(
153 evtContext));
154 constTrackSummary->restoreDecorations();
155 constTrackSummary->fillFrom(tc.container());
156
157 auto constTrackSummaryHandle = SG::makeHandle(m_trackSummaryKey, evtContext);
158 if (constTrackSummaryHandle.record(std::move(constTrackSummary))
159 .isFailure()) {
160 throw std::runtime_error(
161 "MutableTrackContainerHandlesHelper::moveToConst, can't record "
162 "TrackSummary");
163 }
164 auto constTrack = std::make_unique<ActsTrk::PersistentTrackContainer>(
166 evtContext),
168 return constTrack;
169}
170
171// const version
173 const std::string& prefix) {
174 m_statesKey = prefix + "TrackStates";
175 m_parametersKey = prefix + "TrackParameters";
176 m_jacobiansKey = prefix + "TrackJacobians";
177 m_measurementsKey = prefix + "TrackMeasurements";
178 m_surfacesKey = prefix + "TrackSurfaces";
179 m_mtjKey = prefix + "MultiTrajectory";
180
187
188 m_xAODTrackSummaryKey = prefix + "TrackSummary";
189 m_trackSurfacesKey = prefix + "TrackSurfaces";
190 m_trackSummaryKey = prefix + "TrackSummary";
191
195
196 return StatusCode::SUCCESS;
197}
198
199std::unique_ptr<ActsTrk::MultiTrajectory>
200ConstTrackContainerHandlesHelper::buildMtj(const Acts::TrackingGeometry* geo,
201 const EventContext& evtContext) const {
202 // we need to build it from backends
203 DataLink<xAOD::TrackStateAuxContainer> statesLink(m_statesKey.key() + "Aux.",
204 evtContext);
205 if (not statesLink.isValid()) {
206 throw std::runtime_error(
207 "ConstMultiTrajectoryHandle::build, StatesLink is invalid");
208 }
209 // The restoration of the pointers to uncalibrated measurements should only
210 // be done once, if it is done in parallel by multiple callers all callers should
211 // write exactly the same value to exactly the same memory location
212 xAOD::TrackStateAuxContainer *nonConstStatesLink ATLAS_THREAD_SAFE = const_cast<xAOD::TrackStateAuxContainer *>(statesLink.getDataPtr());
213 restoreUncalibMeasurementPtr(*nonConstStatesLink);
214
216 m_parametersKey.key() + "Aux.", evtContext);
217 if (not parametersLink.isValid()) {
218 throw std::runtime_error(
219 "ConstMultiTrajectoryHandle::build, ParametersLink is invalid");
220 }
221
223 m_jacobiansKey.key() + "Aux.", evtContext);
224 if (not jacobiansLink.isValid()) {
225 throw std::runtime_error(
226 "ConstMultiTrajectoryHandle::build, JacobiansLink is invalid");
227 }
228
230 m_measurementsKey.key() + "Aux.", evtContext);
231 if (not measurementsLink.isValid()) {
232 throw std::runtime_error(
233 "ConstMultiTrajectoryHandle::build, MeasurementsLink is invalid");
234 }
235
237 m_surfacesKey.key() + "Aux.", evtContext);
238 if (not surfacesLink.isValid()) {
239 throw std::runtime_error(
240 "ConstMultiTrajectoryHandle::build, SurfacesLink is invalid");
241 }
242
243
244 auto cmtj = std::make_unique<ActsTrk::MultiTrajectory>(
245 statesLink, parametersLink, jacobiansLink, measurementsLink, surfacesLink);
246 cmtj->fillSurfaces(geo);
247 return cmtj;
248}
249
250std::unique_ptr<ActsTrk::PersistentTrackContainer>
251ConstTrackContainerHandlesHelper::build(const Acts::TrackingGeometry* geo,
252 const Acts::GeometryContext& /*geoContext*/,
253 const EventContext& evtContext) const {
254
255 std::unique_ptr<ActsTrk::MultiTrajectory> mtj = buildMtj(geo,evtContext);
256 auto mtjHandle = SG::makeHandle(m_mtjKey, evtContext);
257 if (mtjHandle.record(std::move(mtj)).isFailure()) {
258 throw std::runtime_error(
259 "ConstTrackContainerHandle<C>::build failed recording MTJ");
260 }
262 evtContext);
263 if (not summaryLink.isValid()) {
264 throw std::runtime_error(
265 "ConstTrackContainerHandle::build, SummaryLink is invalid");
266 }
267
268 auto surfacesHandle = SG::makeHandle(m_trackSurfacesKey, evtContext);
269 if (not surfacesHandle.isValid()) {
270 throw std::runtime_error(
271 "ConstTrackContainerHandle::build, SurfaceHandle is invalid");
272 }
273
274 auto constTrackSummary = std::make_unique<ActsTrk::TrackSummaryContainer>(summaryLink);
275 constTrackSummary->decodeSurfaces( surfacesHandle.cptr());
276
277 auto summaryHandle = SG::makeHandle(m_trackSummaryKey, evtContext);
278 if (summaryHandle.record(std::move(constTrackSummary)).isFailure()) {
279 throw std::runtime_error(
280 "MutableTrackContainerHandle::build, can't record "
281 "TrackSummary");
282 }
283
284 auto constTrack = std::make_unique<ActsTrk::PersistentTrackContainer>(
286 evtContext),
288
289 return constTrack;
290
291}
292
293namespace {
294 template <typename T>
295 using const_span = std::span<T const>;
296
297 // get an aux variable for elements (read only)
298 template <typename T>
299 const_span<T> getElementVector( const xAOD::AuxContainerBase &aux_container, const SG::ConstAccessor<T> &accessor) {
300 const T *data = static_cast<const T *>(aux_container.getData (accessor.auxid()));
301 const_span<T> ret( data, aux_container.size() );
302 return ret;
303 }
304
305 // create a new decoration for all elements using the default value
306 template <typename T>
307 std::span<T> createDecoration(xAOD::AuxContainerBase &aux_container, const SG::Decorator<T> &decor) {
308 std::size_t sz=aux_container.size();
309 T *data = static_cast<T *>(aux_container.getDecoration(decor.auxid(), sz, sz));
310 return std::span<T>( data, sz );
311 }
312
313 void throwConflictingUncalibratedMeasurementPointerValue(const xAOD::UncalibratedMeasurement *is,
314 const xAOD::UncalibratedMeasurement *should) {
315 std::stringstream msg;
316 msg << "Conflicting values for TrackState.uncalibratedMeasurement. Already set " << static_cast<const void *>(is)
317 << " would set " << static_cast<const void *>(should);
318 throw std::runtime_error(msg.str());
319 }
320}
321
324 static const SG::ConstAccessor< link_t > link_accessor("uncalibratedMeasurementLink");
325
326 if (statesLink.getAuxIDs().test(link_accessor.auxid())){
327 const_span<link_t> elementLinks = getElementVector(statesLink, link_accessor);
328
329 static const SG::AuxElement::Decorator< const xAOD::UncalibratedMeasurement * >
330 decor("uncalibratedMeasurement");
331
332 std::span<const xAOD::UncalibratedMeasurement *> uncalibratedMeasurements
333 = createDecoration( statesLink, decor);
334
335 for (unsigned int index = 0; index < elementLinks.size(); ++index) {
336 const link_t &el = elementLinks[index];
337 const xAOD::UncalibratedMeasurement *a_measurement = (el.isValid() ? *el : nullptr);
338 // @TODO is this check necessary ?
339 if (uncalibratedMeasurements[index] != nullptr && a_measurement != uncalibratedMeasurements[index]) {
340 throwConflictingUncalibratedMeasurementPointerValue(uncalibratedMeasurements[index], a_measurement);
341 }
342 uncalibratedMeasurements[index]=a_measurement;
343 }
344 }
345 else {
346 std::cerr << "WARNING no uncalibratedMeasurementLink aux data " << std::endl;
347 }
348}
349} // namespace ActsTrk
350
351#undef INIT_CHECK
352#undef RECORD_xAOD
static Double_t sz
static Double_t tc
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
#define INIT_CHECK(key)
#define RECORD_xAOD(key, container, auxContainer, ctx)
#define ATLAS_THREAD_SAFE
SG::ReadHandleKey< xAOD::TrackMeasurementContainer > m_measurementsKey
std::unique_ptr< ActsTrk::PersistentTrackContainer > build(const Acts::TrackingGeometry *geo, const Acts::GeometryContext &geoContext, const EventContext &context) const
SG::ReadHandleKey< xAOD::TrackSurfaceContainer > m_surfacesKey
SG::WriteHandleKey< ActsTrk::MultiTrajectory > m_mtjKey
SG::ReadHandleKey< xAOD::TrackStateContainer > m_statesKey
void restoreUncalibMeasurementPtr(xAOD::TrackStateAuxContainer &statesLink) const
SG::ReadHandleKey< xAOD::TrackJacobianContainer > m_jacobiansKey
SG::WriteHandleKey< ActsTrk::TrackSummaryContainer > m_trackSummaryKey
SG::ReadHandleKey< xAOD::TrackParametersContainer > m_parametersKey
StatusCode initialize(const std::string &prefix)
Sets up the handles.
SG::ReadHandleKey< xAOD::TrackSurfaceContainer > m_trackSurfacesKey
SG::ReadHandleKey< xAOD::TrackSummaryContainer > m_xAODTrackSummaryKey
std::unique_ptr< ActsTrk::MultiTrajectory > buildMtj(const Acts::TrackingGeometry *geo, const EventContext &context) const
Athena implementation of ACTS::MultiTrajectory (ReadWrite version) The data is stored in 4 external b...
SG::WriteHandleKey< xAOD::TrackParametersContainer > m_parametersKey
std::unique_ptr< ActsTrk::PersistentTrackContainer > moveToConst(ActsTrk::MutablePersistentTrackContainer &&tc, const Acts::GeometryContext &geoContext, const EventContext &evtContext) const
produces ActsTrk::ConstTrackContainer with all backends stored in SG
SG::WriteHandleKey< xAOD::TrackMeasurementContainer > m_measurementsKey
SG::WriteHandleKey< xAOD::TrackSurfaceContainer > m_trackSurfacesKey
SG::WriteHandleKey< xAOD::TrackJacobianContainer > m_jacobiansKey
SG::WriteHandleKey< ActsTrk::MultiTrajectory > m_mtjKey
SG::WriteHandleKey< xAOD::TrackSummaryContainer > m_xAODTrackSummaryKey
StatusCode initialize(const std::string &prefix)
Sets up the handles.
SG::WriteHandleKey< ActsTrk::TrackSummaryContainer > m_trackSummaryKey
SG::WriteHandleKey< xAOD::TrackStateContainer > m_statesKey
SG::WriteHandleKey< xAOD::TrackSurfaceContainer > m_surfacesKey
Helper class to provide constant type-safe access to aux data.
SG::auxid_t auxid() const
Return the aux id for this variable.
Common base class for the auxiliary containers.
virtual const void * getData(auxid_t auxid) const override
Get a pointer to a given array.
virtual const auxid_set_t & getAuxIDs() const override
Get the types(names) of variables handled by this container.
virtual size_t size() const override
Get the size of the container.
virtual void * getDecoration(auxid_t auxid, size_t size, size_t capacity) override
Get a pointer to a given array, as a decoration.
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
std::string prefixFromTrackContainerName(const std::string &tracks)
Parse TrackContainer name to get the prefix for backends The name has to contain XYZTracks,...
std::unique_ptr< IFACE > makeInterfaceContainer(const AUX *aux)
helper to construct interface container for already filled Aux container TODO maybe should be moved t...
SG::Decorator< T, ALLOC > Decorator
Helper class to provide type-safe access to aux data, specialized for JaggedVecElt.
Definition AuxElement.h:576
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition index.py:1
TrackStateAuxContainer_v1 TrackStateAuxContainer
UncalibratedMeasurement_v1 UncalibratedMeasurement
Define the version of the uncalibrated measurement class.
MsgStream & msg
Definition testRead.cxx:32