File indexing completed on 2026-04-08 07:48:22
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/unit_test.hpp>
0010
0011 #include "Acts/Definitions/TrackParametrization.hpp"
0012 #include "Acts/Utilities/Zip.hpp"
0013 #include "ActsExamples/EventData/Cluster.hpp"
0014 #include "ActsExamples/EventData/Measurement.hpp"
0015 #include "ActsExamples/Io/Csv/CsvMeasurementReader.hpp"
0016 #include "ActsExamples/Io/Csv/CsvMeasurementWriter.hpp"
0017 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0018 #include "ActsTests/CommonHelpers/WhiteBoardUtilities.hpp"
0019
0020 #include <algorithm>
0021 #include <random>
0022
0023 using namespace Acts;
0024 using namespace ActsExamples;
0025
0026 namespace ActsTests {
0027
0028 BOOST_AUTO_TEST_SUITE(CsvSuite)
0029
0030 BOOST_AUTO_TEST_CASE(CsvMeasurementRoundTrip) {
0031 MeasurementContainer measOriginal;
0032 ClusterContainer clusterOriginal;
0033 IndexMultimap<Index> mapOriginal;
0034
0035
0036
0037
0038 const std::size_t nMeasurements = 3;
0039 GeometryIdentifier someGeoId{298453};
0040
0041 std::mt19937 gen(23);
0042 std::uniform_int_distribution<std::uint32_t> disti(1, 10);
0043 std::uniform_real_distribution<double> distf(0.0, 1.0);
0044
0045 for (auto i = 0ul; i < nMeasurements; ++i) {
0046 Vector2 p = Vector2::Random();
0047 SquareMatrix2 c = SquareMatrix2::Random();
0048
0049 FixedBoundMeasurementProxy<2> m =
0050 measOriginal.makeMeasurement<2>(someGeoId);
0051 m.setSubspaceIndices(std::array{eBoundLoc0, eBoundLoc1});
0052 m.parameters() = p;
0053 m.covariance() = c;
0054
0055 Cluster cl;
0056
0057 using Bin2D = ActsFatras::Segmentizer::Bin2D;
0058 using Seg2D = ActsFatras::Segmentizer::Segment2D;
0059
0060
0061 const auto o = disti(gen);
0062 cl.channels.emplace_back(Bin2D{o + 0, o + 0},
0063 Seg2D{Vector2::Random(), Vector2::Random()},
0064 distf(gen));
0065 cl.channels.emplace_back(Bin2D{o + 0, o + 1},
0066 Seg2D{Vector2::Random(), Vector2::Random()},
0067 distf(gen));
0068 if (distf(gen) < 0.5) {
0069 cl.channels.emplace_back(Bin2D{o + 0, o + 2},
0070 Seg2D{Vector2::Random(), Vector2::Random()},
0071 distf(gen));
0072 cl.sizeLoc0 = 1;
0073 cl.sizeLoc1 = 3;
0074 } else {
0075 cl.channels.emplace_back(Bin2D{o + 1, o + 0},
0076 Seg2D{Vector2::Random(), Vector2::Random()},
0077 distf(gen));
0078 cl.sizeLoc0 = 2;
0079 cl.sizeLoc1 = 2;
0080 }
0081
0082 clusterOriginal.push_back(cl);
0083
0084
0085 mapOriginal.insert(std::pair<Index, Index>{i, disti(gen)});
0086 }
0087
0088
0089
0090
0091 CsvMeasurementWriter::Config writerConfig;
0092 writerConfig.inputClusters = "clusters";
0093 writerConfig.inputMeasurements = "meas";
0094 writerConfig.inputMeasurementSimHitsMap = "map";
0095 writerConfig.outputDir = "";
0096
0097 CsvMeasurementWriter writer(writerConfig, Logging::WARNING);
0098
0099 auto writeTool =
0100 GenericReadWriteTool<>()
0101 .add(writerConfig.inputMeasurements, measOriginal)
0102 .add(writerConfig.inputClusters, clusterOriginal)
0103 .add(writerConfig.inputMeasurementSimHitsMap, mapOriginal);
0104
0105 writeTool.write(writer);
0106
0107
0108
0109
0110 CsvMeasurementReader::Config readerConfig;
0111 readerConfig.inputDir = writerConfig.outputDir;
0112 readerConfig.outputMeasurements = writerConfig.inputMeasurements;
0113 readerConfig.outputMeasurementSimHitsMap =
0114 writerConfig.inputMeasurementSimHitsMap;
0115 readerConfig.outputClusters = writerConfig.inputClusters;
0116
0117 CsvMeasurementReader reader(readerConfig, Logging::WARNING);
0118
0119 auto readTool = writeTool.add(readerConfig.outputMeasurements, measOriginal);
0120
0121 const auto [measRead, clusterRead, mapRead, measRead2] =
0122 readTool.read(reader);
0123
0124
0125
0126
0127 static_assert(
0128 std::is_same_v<std::decay_t<decltype(measRead)>, decltype(measOriginal)>);
0129 BOOST_REQUIRE(measRead.size() == measOriginal.size());
0130 for (const auto &[a, b] : zip(measRead, measOriginal)) {
0131 if (a.size() == b.size()) {
0132 CHECK_CLOSE_REL(a.parameters(), b.parameters(), 1e-4);
0133 }
0134 }
0135
0136 static_assert(std::is_same_v<std::decay_t<decltype(clusterRead)>,
0137 decltype(clusterOriginal)>);
0138 BOOST_REQUIRE(clusterRead.size() == clusterOriginal.size());
0139 for (auto [a, b] : zip(clusterRead, clusterOriginal)) {
0140 BOOST_REQUIRE(a.sizeLoc0 == b.sizeLoc0);
0141 BOOST_REQUIRE(a.sizeLoc1 == b.sizeLoc1);
0142
0143 for (const auto &ca : a.channels) {
0144 auto match = [&](const auto &cb) {
0145 return ca.bin == cb.bin &&
0146 std::abs(ca.activation - cb.activation) < 1.e-4;
0147 };
0148
0149 BOOST_CHECK(std::ranges::any_of(b.channels, match));
0150 }
0151 }
0152
0153 static_assert(
0154 std::is_same_v<std::decay_t<decltype(mapRead)>, decltype(mapOriginal)>);
0155 BOOST_REQUIRE(mapRead.size() == mapOriginal.size());
0156 for (const auto &[a, b] : zip(mapRead, mapOriginal)) {
0157 BOOST_REQUIRE(a == b);
0158 }
0159
0160 static_assert(
0161 std::is_same_v<std::decay_t<decltype(measRead)>, decltype(measOriginal)>);
0162 BOOST_REQUIRE(measRead.size() == measOriginal.size());
0163 for (const auto &[a, b] : zip(measRead, measOriginal)) {
0164 BOOST_REQUIRE(a.geometryId() == b.geometryId());
0165 BOOST_REQUIRE(a.index() == b.index());
0166 }
0167 }
0168
0169 BOOST_AUTO_TEST_SUITE_END()
0170
0171 }