Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PodioTrackStateContainerTest.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PodioTrackStateContainerTest.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/tools/old/interface.hpp>
11 #include <boost/test/unit_test.hpp>
12 #include <boost/test/unit_test_suite.hpp>
13 
23 #include "ActsPodioEdm/BoundParametersCollection.h"
24 #include "ActsPodioEdm/JacobianCollection.h"
25 #include "ActsPodioEdm/TrackStateCollection.h"
26 
27 #include <filesystem>
28 
29 #include <podio/ROOTFrameReader.h>
30 #include <podio/ROOTFrameWriter.h>
31 #include <podio/UserDataCollection.h>
32 
33 namespace {
34 
35 using namespace Acts;
36 using namespace Acts::UnitLiterals;
37 using namespace Acts::Test;
38 namespace bd = boost::unit_test::data;
39 
40 std::default_random_engine rng(31415);
41 
42 class NullHelper : public PodioUtil::ConversionHelper {
43  public:
44  std::optional<PodioUtil::Identifier> surfaceToIdentifier(
45  const Surface& /*surface*/) const override {
46  return {};
47  }
48  const Surface* identifierToSurface(
49  PodioUtil::Identifier /*identifier*/) const override {
50  return nullptr;
51  }
52 
53  SourceLink identifierToSourceLink(
54  PodioUtil::Identifier /*identifier*/) const override {
55  return SourceLink{0};
56  }
57 
58  PodioUtil::Identifier sourceLinkToIdentifier(
59  const SourceLink& /*sourceLink*/) override {
60  return 0;
61  }
62 };
63 
64 struct MapHelper : public NullHelper {
65  std::optional<PodioUtil::Identifier> surfaceToIdentifier(
66  const Surface& surface) const override {
67  for (auto&& [id, srf] : surfaces) {
68  if (srf == &surface) {
69  return id;
70  }
71  }
72  return {};
73  }
74  const Surface* identifierToSurface(PodioUtil::Identifier id) const override {
75  auto it = surfaces.find(id);
76  if (it == surfaces.end()) {
77  return nullptr;
78  }
79 
80  return it->second;
81  }
82 
83  PodioUtil::Identifier sourceLinkToIdentifier(const SourceLink& sl) override {
84  sourceLinks.push_back(sl);
85  return sourceLinks.size() - 1;
86  }
87 
88  SourceLink identifierToSourceLink(PodioUtil::Identifier id) const override {
89  return sourceLinks.at(id);
90  }
91 
92  std::unordered_map<PodioUtil::Identifier, const Surface*> surfaces;
93  std::vector<SourceLink> sourceLinks;
94 };
95 
96 struct Factory {
97  using trajectory_t = MutablePodioTrackStateContainer;
98  using const_trajectory_t = ConstPodioTrackStateContainer;
99 
100  // list used to have stable addresses, irrelevant for testing
101  std::list<ActsPodioEdm::TrackStateCollection> m_collections;
102  std::list<ActsPodioEdm::BoundParametersCollection> m_params;
103  std::list<ActsPodioEdm::JacobianCollection> m_jacs;
104  MapHelper m_helper;
105 
106  MutablePodioTrackStateContainer create() { return {m_helper}; }
107 };
108 
109 using CommonTests = MultiTrajectoryTestsCommon<Factory>;
110 
111 } // namespace
112 
113 BOOST_AUTO_TEST_SUITE(PodioTrackStateContainerTest)
114 
116  CommonTests ct;
117  ct.testBuild();
118 }
119 
120 BOOST_AUTO_TEST_CASE(ConstCorrectness) {
121  // @TODO: Const version can only be non-owning!
122 }
123 
125  CommonTests ct;
126  ct.testClear();
127 }
128 
129 BOOST_AUTO_TEST_CASE(ApplyWithAbort) {
130  CommonTests ct;
131  ct.testApplyWithAbort();
132 }
133 
134 BOOST_AUTO_TEST_CASE(AddTrackStateWithBitMask) {
135  CommonTests ct;
136  ct.testAddTrackStateWithBitMask();
137 }
138 
139 // assert expected "cross-talk" between trackstate proxies
140 BOOST_AUTO_TEST_CASE(TrackStateProxyCrossTalk) {
141  CommonTests ct;
142  ct.testTrackStateProxyCrossTalk(rng);
143 }
144 
145 BOOST_AUTO_TEST_CASE(TrackStateReassignment) {
146  CommonTests ct;
147  ct.testTrackStateReassignment(rng);
148 }
149 
150 BOOST_DATA_TEST_CASE(TrackStateProxyStorage, bd::make({1u, 2u}),
151  nMeasurements) {
152  CommonTests ct;
153  ct.testTrackStateProxyStorage(rng, nMeasurements);
154 }
155 
156 BOOST_AUTO_TEST_CASE(TrackStateProxyAllocations) {
157  CommonTests ct;
158  ct.testTrackStateProxyAllocations(rng);
159 }
160 
161 BOOST_AUTO_TEST_CASE(TrackStateProxyGetMask) {
162  CommonTests ct;
163  ct.testTrackStateProxyGetMask();
164 }
165 
166 BOOST_AUTO_TEST_CASE(TrackStateProxyCopy) {
167  CommonTests ct;
168  ct.testTrackStateProxyCopy(rng);
169 }
170 
171 BOOST_AUTO_TEST_CASE(TrackStateProxyCopyDiffMTJ) {
172  CommonTests ct;
173  ct.testTrackStateProxyCopyDiffMTJ();
174 }
175 
176 BOOST_AUTO_TEST_CASE(ProxyAssignment) {
177  CommonTests ct;
178  ct.testProxyAssignment();
179 }
180 
181 BOOST_AUTO_TEST_CASE(CopyFromConst) {
182  CommonTests ct;
183  ct.testCopyFromConst();
184 }
185 
186 BOOST_AUTO_TEST_CASE(TrackStateProxyShare) {
187  CommonTests ct;
188  ct.testTrackStateProxyShare(rng);
189 }
190 
191 BOOST_AUTO_TEST_CASE(MultiTrajectoryExtraColumns) {
192  CommonTests ct;
193  ct.testMultiTrajectoryExtraColumns();
194 }
195 
196 BOOST_AUTO_TEST_CASE(MultiTrajectoryExtraColumnsRuntime) {
197  CommonTests ct;
198  ct.testMultiTrajectoryExtraColumnsRuntime();
199 }
200 
201 BOOST_AUTO_TEST_CASE(WriteToPodioFrame) {
202  using namespace HashedStringLiteral;
203 
204  MapHelper helper;
205 
206  // auto tmp_path = std::filesystem::temp_directory_path();
207  auto tmp_path = std::filesystem::current_path();
208  auto outfile = tmp_path / "trackstates.root";
209 
210  BoundVector tv1;
211  tv1 << 1, 1, 1, 1, 1, 1;
212 
213  BoundVector tv2 = tv1 * 2;
214  BoundVector tv3 = tv1 * 3;
215  BoundVector tv4 = tv1 * 4;
216 
217  BoundMatrix cov1;
218  cov1.setOnes();
219 
220  BoundMatrix cov2 = cov1 * 2;
221  BoundMatrix cov3 = cov1 * 3;
222  BoundMatrix cov4 = cov1 * 4;
223 
224  auto rBounds = std::make_shared<RectangleBounds>(15, 20);
225  auto trf = Transform3::Identity();
226  trf.translation().setRandom();
227  auto free = Acts::Surface::makeShared<PlaneSurface>(trf, rBounds);
228  auto reg = Acts::Surface::makeShared<PlaneSurface>(trf, rBounds);
229 
230  helper.surfaces[666] = reg.get();
231 
232  podio::Frame frame;
233 
235  BOOST_CHECK(!c.hasColumn("int_column"_hash));
236  BOOST_CHECK(!c.hasColumn("float_column"_hash));
237  c.addColumn<int32_t>("int_column");
238  c.addColumn<float>("float_column");
239  BOOST_CHECK(c.hasColumn("int_column"_hash));
240  BOOST_CHECK(c.hasColumn("float_column"_hash));
241 
242  {
243  auto t1 = c.getTrackState(c.addTrackState(TrackStatePropMask::Predicted));
244  t1.predicted() = tv1;
245  t1.predictedCovariance() = cov1;
246 
247  t1.setReferenceSurface(free);
248 
249  auto t2 =
250  c.getTrackState(c.addTrackState(TrackStatePropMask::All, t1.index()));
251  t2.predicted() = tv2;
252  t2.predictedCovariance() = cov2;
253 
254  t2.filtered() = tv3;
255  t2.filteredCovariance() = cov3;
256 
257  t2.smoothed() = tv4;
258  t2.smoothedCovariance() = cov4;
259 
260  t2.jacobian() = cov2;
261 
262  auto t3 = c.getTrackState(c.addTrackState());
263  t3.setReferenceSurface(reg);
264 
265  t1.component<int32_t, "int_column"_hash>() = -11;
266  t2.component<int32_t, "int_column"_hash>() = 42;
267  t3.component<int32_t, "int_column"_hash>() = -98;
268 
269  t1.component<float, "float_column"_hash>() = -11.2f;
270  t2.component<float, "float_column"_hash>() = 42.4f;
271  t3.component<float, "float_column"_hash>() = -98.9f;
272  }
273 
274  c.releaseInto(frame, "test");
275 
276  BOOST_CHECK_EQUAL(frame.get("trackStates_test")->size(), 3);
277  BOOST_CHECK_EQUAL(frame.get("trackStateParameters_test")->size(), 7);
278  BOOST_CHECK_EQUAL(frame.get("trackStateJacobians_test")->size(), 2);
279  BOOST_CHECK_NE(frame.get("trackStates_test_extra__int_column"), nullptr);
280  BOOST_CHECK_NE(frame.get("trackStates_test_extra__float_column"), nullptr);
281 
282  ConstPodioTrackStateContainer cc{helper, frame, "test"};
283 
284  BOOST_CHECK_EQUAL(cc.size(), 3);
285  BOOST_CHECK(cc.hasColumn("int_column"_hash));
286  BOOST_CHECK(cc.hasColumn("float_column"_hash));
287 
288  auto t1 = cc.getTrackState(0);
289  auto t2 = cc.getTrackState(1);
290  auto t3 = cc.getTrackState(2);
291 
292  BOOST_CHECK_EQUAL(t2.previous(), 0);
293 
294  BOOST_CHECK(t1.hasReferenceSurface());
295  BOOST_CHECK(!t2.hasReferenceSurface());
296  BOOST_CHECK(t3.hasReferenceSurface());
297 
299 
300  const auto& ext = t1.referenceSurface();
301  BOOST_CHECK_NE(&ext, free.get());
302  BOOST_CHECK_EQUAL(trf.matrix(), ext.transform(gctx).matrix());
303  BOOST_CHECK_EQUAL(free->bounds().type(), ext.bounds().type());
304  BOOST_CHECK_EQUAL(free->type(), ext.type());
305  const auto* rBounds2 = dynamic_cast<const RectangleBounds*>(&ext.bounds());
306  BOOST_REQUIRE_NE(rBounds2, nullptr);
307  BOOST_CHECK_EQUAL(rBounds->halfLengthX(), rBounds2->halfLengthX());
308  BOOST_CHECK_EQUAL(rBounds->halfLengthY(), rBounds2->halfLengthY());
309 
310  BOOST_CHECK_EQUAL(t1.predicted(), tv1);
311  BOOST_CHECK_EQUAL(t1.predictedCovariance(), cov1);
312 
313  BOOST_CHECK_EQUAL(t2.predicted(), tv2);
314  BOOST_CHECK_EQUAL(t2.predictedCovariance(), cov2);
315  BOOST_CHECK_EQUAL(t2.filtered(), tv3);
316  BOOST_CHECK_EQUAL(t2.filteredCovariance(), cov3);
317  BOOST_CHECK_EQUAL(t2.smoothed(), tv4);
318  BOOST_CHECK_EQUAL(t2.smoothedCovariance(), cov4);
319 
320  BOOST_CHECK_EQUAL(t2.jacobian(), cov2);
321 
322  BOOST_CHECK_EQUAL(&t3.referenceSurface(), reg.get());
323 
324  BOOST_CHECK_EQUAL((t1.component<int32_t, "int_column"_hash>()), -11);
325  BOOST_CHECK_EQUAL((t2.component<int32_t, "int_column"_hash>()), 42);
326  BOOST_CHECK_EQUAL((t3.component<int32_t, "int_column"_hash>()), -98);
327 
328  BOOST_CHECK_EQUAL((t1.component<float, "float_column"_hash>()), -11.2f);
329  BOOST_CHECK_EQUAL((t2.component<float, "float_column"_hash>()), 42.4f);
330  BOOST_CHECK_EQUAL((t3.component<float, "float_column"_hash>()), -98.9f);
331 }
332 
333 BOOST_AUTO_TEST_SUITE_END()