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 <regex>
14#include <span>
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 throw std::runtime_error(
27 std::string("prefixFromTrackContainerName: key does not contain "
28 "Tracks in the name ") + tracks);
29 }
30
31 return match_regex[1].str();
32}
33
34#define RECORD_xAOD(key, container, auxContainer, ctx) \
35 { \
36 SG::WriteHandle handle{key, ctx}; \
37 if (!handle.record(std::move(container), \
38 std::move(auxContainer)).isSuccess()){ \
39 THROW_EXCEPTION("Cannot record "<<key.fullKey()<<" backend"); \
40 } \
41 }
42
43#define INIT_CHECK(key) \
44 if (!key.initialize().isSuccess()) { \
45 THROW_EXCEPTION("Failed to initialize key "<<key.fullKey()); \
46 }
47
49 const std::string& prefix) {
50 m_statesKey = prefix + "TrackStates";
51 m_parametersKey = prefix + "TrackParameters";
52 m_jacobiansKey = prefix + "TrackJacobians";
53 m_measurementsKey = prefix + "TrackMeasurements";
54 m_surfacesKey = prefix + "TrackStateSurfaces";
55 m_mtjKey =
56 prefix + "MultiTrajectory"; // identical names, underlying types are distinct
63
64 // Track Container backends
65 m_xAODTrackSummaryKey = prefix + "TrackSummary";
66 m_trackSurfacesKey = prefix + "TrackSurfaces";
68 prefix +
69 "TrackSummary"; // identical names, underlying types are distinct
70
74 return StatusCode::SUCCESS;
75}
76
77std::unique_ptr<ActsTrk::MultiTrajectory>
79 ActsTrk::MutableMultiTrajectory&& mmtj, const EventContext& evtContext) const {
80
81 mmtj.trim();
82
83 auto statesBackendHandle = SG::makeHandle(m_statesKey, evtContext);
84 auto statesInterface =
86 mmtj.trackStatesAux());
87 RECORD_xAOD(m_statesKey, statesInterface, mmtj.m_trackStatesAux, evtContext);
88
89 auto parametersInterface =
91 mmtj.trackParametersAux());
92 RECORD_xAOD(m_parametersKey, parametersInterface, mmtj.m_trackParametersAux, evtContext);
93
94 auto jacobiansInterface =
96 mmtj.trackJacobiansAux());
97 RECORD_xAOD(m_jacobiansKey, jacobiansInterface, mmtj.m_trackJacobiansAux, evtContext);
98
99 auto measurementsInterface =
101 mmtj.trackMeasurementsAux());
102 RECORD_xAOD(m_measurementsKey, measurementsInterface, mmtj.m_trackMeasurementsAux, evtContext);
103
104 auto surfacesBackendHandle = SG::makeHandle(m_surfacesKey, evtContext);
105 RECORD_xAOD(m_surfacesKey, mmtj.m_surfacesBackend, mmtj.m_surfacesBackendAux, evtContext);
106
107 // construct const MTJ version
108 auto cmtj = std::make_unique<ActsTrk::MultiTrajectory>(
110 evtContext),
112 m_parametersKey.key() + "Aux.", evtContext),
114 m_jacobiansKey.key() + "Aux.", evtContext),
116 m_measurementsKey.key() + "Aux.", evtContext),
118 m_surfacesKey.key() + "Aux.", evtContext)
119 );
120 cmtj->moveSurfaces(&mmtj);
121 cmtj->moveLinks(&mmtj);
122
123 return cmtj;
124}
125
126std::unique_ptr<ActsTrk::PersistentTrackContainer>
128 ActsTrk::MutablePersistentTrackContainer&& tc, const Acts::GeometryContext& geoContext, const EventContext& evtContext) const {
129
130
131 std::unique_ptr<ActsTrk::MultiTrajectory> constMtj =
132 moveToConst(std::move(tc.trackStateContainer()), evtContext);
133
134 auto constMtjHandle = SG::makeHandle(m_mtjKey, evtContext);
135 if (constMtjHandle.record(std::move(constMtj)).isFailure()) {
136 throw std::runtime_error(
137 "MutableTrackContainerHandlesHelper::moveToConst, can't record "
138 "ConstMultiTrajectory");
139 }
140
141 auto trackSurfacesAux = std::make_unique<xAOD::TrackSurfaceAuxContainer>();
142 tc.container().encodeSurfaces(trackSurfacesAux.get(), geoContext);
143
144 auto interfaceTrackSummaryContainer =
146 tc.container().m_mutableTrackBackendAux.get());
147 RECORD_xAOD(m_xAODTrackSummaryKey, interfaceTrackSummaryContainer, tc.container().m_mutableTrackBackendAux, evtContext);
148
150 trackSurfacesAux.get());
151 RECORD_xAOD(m_trackSurfacesKey, trackSurfaces, trackSurfacesAux, evtContext);
152
153 auto constTrackSummary = std::make_unique<ActsTrk::TrackSummaryContainer>(
155 evtContext));
156 constTrackSummary->restoreDecorations();
157 constTrackSummary->fillFrom(tc.container());
158
159 auto constTrackSummaryHandle = SG::makeHandle(m_trackSummaryKey, evtContext);
160 if (constTrackSummaryHandle.record(std::move(constTrackSummary))
161 .isFailure()) {
162 throw std::runtime_error(
163 "MutableTrackContainerHandlesHelper::moveToConst, can't record "
164 "TrackSummary");
165 }
166 auto constTrack = std::make_unique<ActsTrk::PersistentTrackContainer>(
168 evtContext),
170 return constTrack;
171}
172
173// const version
175 const std::string& prefix) {
176 m_statesKey = prefix + "TrackStates";
177 m_parametersKey = prefix + "TrackParameters";
178 m_jacobiansKey = prefix + "TrackJacobians";
179 m_measurementsKey = prefix + "TrackMeasurements";
180 m_surfacesKey = prefix + "TrackSurfaces";
181 m_mtjKey = prefix + "MultiTrajectory";
182
189
190 m_xAODTrackSummaryKey = prefix + "TrackSummary";
191 m_trackSurfacesKey = prefix + "TrackSurfaces";
192 m_trackSummaryKey = prefix + "TrackSummary";
193
197
198 return StatusCode::SUCCESS;
199}
200
201std::unique_ptr<ActsTrk::MultiTrajectory>
202ConstTrackContainerHandlesHelper::buildMtj(const Acts::TrackingGeometry* geo,
203 const EventContext& evtContext) const {
204 // we need to build it from backends
205 DataLink<xAOD::TrackStateAuxContainer> statesLink(m_statesKey.key() + "Aux.",
206 evtContext);
207 if (not statesLink.isValid()) {
208 throw std::runtime_error(
209 "ConstMultiTrajectoryHandle::build, StatesLink is invalid");
210 }
211 // The restoration of the pointers to uncalibrated measurements should only
212 // be done once, if it is done in parallel by multiple callers all callers should
213 // write exactly the same value to exactly the same memory location
214 xAOD::TrackStateAuxContainer *nonConstStatesLink ATLAS_THREAD_SAFE = const_cast<xAOD::TrackStateAuxContainer *>(statesLink.getDataPtr());
215 restoreUncalibMeasurementPtr(*nonConstStatesLink);
216
218 m_parametersKey.key() + "Aux.", evtContext);
219 if (not parametersLink.isValid()) {
220 throw std::runtime_error(
221 "ConstMultiTrajectoryHandle::build, ParametersLink is invalid");
222 }
223
225 m_jacobiansKey.key() + "Aux.", evtContext);
226 if (not jacobiansLink.isValid()) {
227 throw std::runtime_error(
228 "ConstMultiTrajectoryHandle::build, JacobiansLink is invalid");
229 }
230
232 m_measurementsKey.key() + "Aux.", evtContext);
233 if (not measurementsLink.isValid()) {
234 throw std::runtime_error(
235 "ConstMultiTrajectoryHandle::build, MeasurementsLink is invalid");
236 }
237
239 m_surfacesKey.key() + "Aux.", evtContext);
240 if (not surfacesLink.isValid()) {
241 throw std::runtime_error(
242 "ConstMultiTrajectoryHandle::build, SurfacesLink is invalid");
243 }
244
245
246 auto cmtj = std::make_unique<ActsTrk::MultiTrajectory>(
247 statesLink, parametersLink, jacobiansLink, measurementsLink, surfacesLink);
248 cmtj->fillSurfaces(geo);
249 return cmtj;
250}
251
252std::unique_ptr<ActsTrk::PersistentTrackContainer>
253ConstTrackContainerHandlesHelper::build(const Acts::TrackingGeometry* geo,
254 const Acts::GeometryContext& /*geoContext*/,
255 const EventContext& evtContext) const {
256
257 std::unique_ptr<ActsTrk::MultiTrajectory> mtj = buildMtj(geo,evtContext);
258 auto mtjHandle = SG::makeHandle(m_mtjKey, evtContext);
259 if (mtjHandle.record(std::move(mtj)).isFailure()) {
260 throw std::runtime_error(
261 "ConstTrackContainerHandle<C>::build failed recording MTJ");
262 }
264 evtContext);
265 if (not summaryLink.isValid()) {
266 throw std::runtime_error(
267 "ConstTrackContainerHandle::build, SummaryLink is invalid");
268 }
269
270 auto surfacesHandle = SG::makeHandle(m_trackSurfacesKey, evtContext);
271 if (not surfacesHandle.isValid()) {
272 throw std::runtime_error(
273 "ConstTrackContainerHandle::build, SurfaceHandle is invalid");
274 }
275
276 auto constTrackSummary = std::make_unique<ActsTrk::TrackSummaryContainer>(summaryLink);
277 constTrackSummary->decodeSurfaces( surfacesHandle.cptr());
278
279 auto summaryHandle = SG::makeHandle(m_trackSummaryKey, evtContext);
280 if (summaryHandle.record(std::move(constTrackSummary)).isFailure()) {
281 throw std::runtime_error(
282 "MutableTrackContainerHandle::build, can't record "
283 "TrackSummary");
284 }
285
286 auto constTrack = std::make_unique<ActsTrk::PersistentTrackContainer>(
288 evtContext),
290
291 return constTrack;
292
293}
294
295namespace {
296 template <typename T>
297 using const_span = std::span<T const>;
298
299 // get an aux variable for elements (read only)
300 template <typename T>
301 const_span<T> getElementVector( const xAOD::AuxContainerBase &aux_container, const SG::ConstAccessor<T> &accessor) {
302 const T *data = static_cast<const T *>(aux_container.getData (accessor.auxid()));
303 const_span<T> ret( data, aux_container.size() );
304 return ret;
305 }
306
307 // create a new decoration for all elements using the default value
308 template <typename T>
309 std::span<T> createDecoration(xAOD::AuxContainerBase &aux_container, const SG::Decorator<T> &decor) {
310 std::size_t sz=aux_container.size();
311 T *data = static_cast<T *>(aux_container.getDecoration(decor.auxid(), sz, sz));
312 return std::span<T>( data, sz );
313 }
314
315 void throwConflictingUncalibratedMeasurementPointerValue(const xAOD::UncalibratedMeasurement *is,
316 const xAOD::UncalibratedMeasurement *should) {
317 std::stringstream msg;
318 msg << "Conflicting values for TrackState.uncalibratedMeasurement. Already set " << static_cast<const void *>(is)
319 << " would set " << static_cast<const void *>(should);
320 throw std::runtime_error(msg.str());
321 }
322}
323
326 static const SG::ConstAccessor< link_t > link_accessor("uncalibratedMeasurementLink");
327
328 if (statesLink.getAuxIDs().test(link_accessor.auxid())){
329 const_span<link_t> elementLinks = getElementVector(statesLink, link_accessor);
330
331 static const SG::AuxElement::Decorator< const xAOD::UncalibratedMeasurement * >
332 decor("uncalibratedMeasurement");
333
334 std::span<const xAOD::UncalibratedMeasurement *> uncalibratedMeasurements
335 = createDecoration( statesLink, decor);
336
337 for (unsigned int index = 0; index < elementLinks.size(); ++index) {
338 const link_t &el = elementLinks[index];
339 const xAOD::UncalibratedMeasurement *a_measurement = (el.isValid() ? *el : nullptr);
340 // @TODO is this check necessary ?
341 if (uncalibratedMeasurements[index] != nullptr && a_measurement != uncalibratedMeasurements[index]) {
342 throwConflictingUncalibratedMeasurementPointerValue(uncalibratedMeasurements[index], a_measurement);
343 }
344 uncalibratedMeasurements[index]=a_measurement;
345 }
346 }
347 else {
348 std::cerr << "WARNING no uncalibratedMeasurementLink aux data " << std::endl;
349 }
350}
351} // namespace ActsTrk
352
353#undef INIT_CHECK
354#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