Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:16:19

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