Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-05 08:55:46

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