21 #include "ActsPodioEdm/BoundParametersCollection.h"
22 #include "ActsPodioEdm/JacobianCollection.h"
23 #include "ActsPodioEdm/TrackStateCollection.h"
24 #include "ActsPodioEdm/TrackStateInfo.h"
30 #include <type_traits>
32 #include <podio/CollectionBase.h>
33 #include <podio/Frame.h>
35 #include "podio/UserDataCollection.h"
39 class MutablePodioTrackStateContainer;
40 class ConstPodioTrackStateContainer;
59 using namespace Acts::HashedStringLiteral;
60 auto trackState = instance.m_collection->at(istate);
61 const auto&
data = trackState.getData();
63 case "predicted"_hash:
69 case "calibrated"_hash:
70 return data.measdim != 0;
71 case "calibratedCov"_hash:
72 return data.measdim != 0;
75 case "projector"_hash:
76 return data.hasProjector;
77 case "uncalibratedSourceLink"_hash:
81 case "referenceSurface"_hash:
83 case "pathLength"_hash:
84 case "typeFlags"_hash:
87 return instance.m_dynamic.find(key) != instance.m_dynamic.end();
93 template <
bool EnsureConst,
typename T>
96 if constexpr (EnsureConst) {
97 static_assert(std::is_const_v<std::remove_reference_t<T>>,
100 using namespace Acts::HashedStringLiteral;
101 auto trackState = instance.m_collection->at(istate);
102 std::conditional_t<EnsureConst,
const ActsPodioEdm::TrackStateInfo*,
103 ActsPodioEdm::TrackStateInfo*>
105 if constexpr (EnsureConst) {
106 dataPtr = &trackState.getData();
108 dataPtr = &trackState.data();
110 auto&
data = *dataPtr;
112 case "previous"_hash:
113 return &
data.previous;
114 case "predicted"_hash:
115 return &
data.ipredicted;
116 case "filtered"_hash:
117 return &
data.ifiltered;
118 case "smoothed"_hash:
119 return &
data.ismoothed;
120 case "projector"_hash:
121 return &
data.projector;
123 return &
data.measdim;
126 case "pathLength"_hash:
127 return &
data.pathLength;
128 case "typeFlags"_hash:
129 return &
data.typeFlags;
131 auto it = instance.m_dynamic.find(key);
132 if (
it == instance.m_dynamic.end()) {
133 throw std::runtime_error(
"Unable to handle this component");
135 std::conditional_t<EnsureConst,
138 col =
it->second.get();
139 assert(col &&
"Dynamic column is null");
140 return col->get(istate);
144 template <
typename T>
146 using namespace Acts::HashedStringLiteral;
148 case "predicted"_hash:
149 case "filtered"_hash:
150 case "smoothed"_hash:
151 case "jacobian"_hash:
152 case "projector"_hash:
153 case "previous"_hash:
154 case "uncalibratedSourceLink"_hash:
155 case "referenceSurface"_hash:
158 case "pathLength"_hash:
159 case "typeFlags"_hash:
162 return instance.m_dynamic.find(key) != instance.m_dynamic.end();
168 const ActsPodioEdm::TrackStateCollection& collection,
169 std::vector<std::shared_ptr<const Surface>>& surfaces) noexcept {
170 surfaces.reserve(collection.size());
171 for (ActsPodioEdm::TrackState trackState : collection) {
173 helper, trackState.getReferenceSurface()));
188 const ActsPodioEdm::TrackStateCollection& trackStates,
189 const ActsPodioEdm::BoundParametersCollection& params,
190 const ActsPodioEdm::JacobianCollection& jacs)
192 m_collection{&trackStates},
198 ConstPodioTrackStateContainer(
const PodioUtil::ConversionHelper& helper,
199 const podio::Frame& frame,
202 m_collection{
nullptr},
207 std::vector<std::string> available = frame.getAvailableCollections();
213 if (std::find(available.begin(), available.end(), trackStatesKey) ==
215 throw std::runtime_error{
"Track state collection '" + trackStatesKey +
216 "'not found in frame"};
219 if (std::find(available.begin(), available.end(), paramsKey) ==
221 throw std::runtime_error{
"Track state parameters collection '" +
222 paramsKey +
"'not found in frame"};
225 if (std::find(available.begin(), available.end(), jacsKey) ==
227 throw std::runtime_error{
"Track state jacobian collection '" + jacsKey +
228 "'not found in frame"};
231 loadCollection<ActsPodioEdm::TrackStateCollection>(m_collection, frame,
233 loadCollection<ActsPodioEdm::BoundParametersCollection>(
m_params, frame,
235 loadCollection<ActsPodioEdm::JacobianCollection>(m_jacs, frame, jacsKey);
241 using load_type = std::unique_ptr<podio_detail::DynamicColumnBase> (*)(
242 const podio::CollectionBase*);
245 std::tuple<int32_t, int64_t, uint32_t, uint64_t, float, double>;
247 for (
const auto&
col : available) {
249 std::size_t
p =
col.find(prefix);
250 if (p == std::string::npos) {
254 const podio::CollectionBase* coll = frame.get(
col);
256 std::unique_ptr<podio_detail::ConstDynamicColumnBase> up;
260 auto inner = [&](
auto arg) {
264 using T = decltype(arg);
266 dynamic_cast<const podio::UserDataCollection<T>*
>(coll);
267 if (dyn ==
nullptr) {
270 up = std::make_unique<podio_detail::ConstDynamicColumn<T>>(
274 ((inner(
args)), ...);
279 throw std::runtime_error{
"Dynamic column '" + dynName +
280 "' is not of allowed type"};
288 template <
typename collection_t>
289 static void loadCollection(collection_t
const*&
dest,
290 const podio::Frame& frame,
292 const auto* collection = frame.get(key);
294 if (
const auto* d = dynamic_cast<const collection_t*>(collection);
298 throw std::runtime_error{
"Unable to get collection " + key};
308 return ConstCovariance{m_params->at(istate).getData().covariance.data()};
312 IndexType ijacobian = m_collection->at(istate).getData().ijacobian;
316 template <
size_t measdim>
317 ConstTrackStateProxy::Measurement<measdim> measurement_impl(
319 return ConstTrackStateProxy::Measurement<measdim>{
320 m_collection->at(index).getData().measurement.data()};
323 template <
size_t measdim>
324 ConstTrackStateProxy::MeasurementCovariance<measdim>
325 measurementCovariance_impl(
IndexType index)
const {
326 return ConstTrackStateProxy::MeasurementCovariance<measdim>{
327 m_collection->at(index).getData().measurementCovariance.data()};
330 IndexType size_impl()
const {
return m_collection->size(); }
333 return PodioTrackStateContainerBase::component_impl<true>(*
this, key,
346 return m_collection->at(istate).getData().measdim;
350 return m_helper.get().identifierToSourceLink(
351 m_collection->at(istate).getData().uncalibratedIdentifier);
355 return m_surfaces.at(istate).get();
359 friend class PodioTrackStateContainerBase;
361 std::reference_wrapper<const PodioUtil::ConversionHelper>
m_helper;
363 const ActsPodioEdm::BoundParametersCollection*
m_params;
364 const ActsPodioEdm::JacobianCollection*
m_jacs;
368 std::unique_ptr<podio_detail::ConstDynamicColumnBase>>
373 "MutablePodioTrackStateContainer should not be read-only");
388 m_collection = std::make_unique<ActsPodioEdm::TrackStateCollection>();
389 m_jacs = std::make_unique<ActsPodioEdm::JacobianCollection>();
390 m_params = std::make_unique<ActsPodioEdm::BoundParametersCollection>();
400 return Parameters{m_params->at(istate).data().values.data()};
404 return ConstCovariance{m_params->at(istate).getData().covariance.data()};
408 return Covariance{m_params->at(istate).data().covariance.data()};
412 IndexType ijacobian = m_collection->at(istate).getData().ijacobian;
417 IndexType ijacobian = m_collection->at(istate).getData().ijacobian;
418 return Covariance{m_jacs->at(ijacobian).data().values.data()};
421 template <
size_t measdim>
425 m_collection->at(index).getData().measurement.data()};
428 template <
size_t measdim>
431 m_collection->at(index).data().measurement.data()};
434 template <
size_t measdim>
435 ConstTrackStateProxy::MeasurementCovariance<measdim>
438 m_collection->at(index).getData().measurementCovariance.data()};
441 template <
size_t measdim>
445 m_collection->at(index).data().measurementCovariance.data()};
451 return PodioTrackStateContainerBase::component_impl<true>(*
this, key,
456 return PodioTrackStateContainerBase::component_impl<false>(*
this, key,
471 auto trackState = m_collection->create();
472 auto&
data = trackState.data();
473 data.previous = iprevious;
482 data.ipredicted = m_params->size() - 1;
486 data.ifiltered = m_params->size() - 1;
490 data.ismoothed = m_params->size() - 1;
494 data.ijacobian = m_jacs->size() - 1;
497 data.hasProjector =
false;
499 data.hasProjector =
true;
501 m_surfaces.emplace_back();
504 assert(m_collection->size() == m_surfaces.size() &&
505 "Inconsistent surface buffer");
507 for (
const auto& [key,
vec] : m_dynamic) {
511 return m_collection->size() - 1;
517 auto&
self = m_collection->at(iself).data();
518 auto& other = m_collection->at(iother).data();
521 "Source has incompatible allocation");
526 switch (shareSource) {
528 sourceIndex = other.ipredicted;
531 sourceIndex = other.ifiltered;
534 sourceIndex = other.ismoothed;
537 sourceIndex = other.ijacobian;
540 throw std::domain_error{
"Unable to share this component"};
545 switch (shareTarget) {
548 self.ipredicted = sourceIndex;
552 self.ifiltered = sourceIndex;
556 self.ismoothed = sourceIndex;
560 self.ijacobian = sourceIndex;
563 throw std::domain_error{
"Unable to share this component"};
568 auto&
data = m_collection->at(istate).data();
570 case TrackStatePropMask::Predicted:
573 case TrackStatePropMask::Filtered:
576 case TrackStatePropMask::Smoothed:
582 case TrackStatePropMask::Calibrated:
586 throw std::domain_error{
"Unable to unset this component"};
591 m_collection->clear();
594 for (
const auto& [key,
vec] : m_dynamic) {
599 template <
typename T>
602 std::make_unique<podio_detail::DynamicColumn<T>>(key)});
606 assert(measdim > 0 &&
"Zero measdim not supported");
607 auto&
data = m_collection->at(istate).data();
608 data.measdim = measdim;
614 m_helper.get().sourceLinkToIdentifier(sourceLink);
615 m_collection->at(istate).data().uncalibratedIdentifier =
id;
619 std::shared_ptr<const Surface>
surface) {
620 auto trackState = m_collection->at(istate);
621 trackState.setReferenceSurface(
623 m_surfaces.at(istate) =
std::move(surface);
627 return m_collection->at(istate).getData().measdim;
631 return m_helper.
get().identifierToSourceLink(
632 m_collection->at(istate).getData().uncalibratedIdentifier);
636 return m_surfaces.at(istate).get();
644 frame.put(
std::move(m_collection),
"trackStates" + s);
645 frame.put(
std::move(m_params),
"trackStateParameters" + s);
646 frame.put(
std::move(m_jacs),
"trackStateJacobians" + s);
649 for (
const auto& [key,
col] : m_dynamic) {
650 col->releaseInto(frame,
"trackStates" + s +
"_extra__");
658 std::reference_wrapper<PodioUtil::ConversionHelper>
m_helper;
660 std::unique_ptr<ActsPodioEdm::BoundParametersCollection>
m_params;
661 std::unique_ptr<ActsPodioEdm::JacobianCollection>
m_jacs;
665 std::unique_ptr<podio_detail::DynamicColumnBase>>
671 "MutablePodioTrackStateContainer should not be read-only");
674 "MutablePodioTrackStateContainer should not be read-only");