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/Tests/CommonHelpers/FloatComparisons.hpp"
0012 #include "Acts/Tests/CommonHelpers/WhiteBoardUtilities.hpp"
0013 #include "Acts/Utilities/Zip.hpp"
0014 #include "ActsExamples/EventData/SimHit.hpp"
0015 #include "ActsExamples/EventData/SimParticle.hpp"
0016 #include "ActsExamples/Io/Root/RootSimHitReader.hpp"
0017 #include "ActsExamples/Io/Root/RootSimHitWriter.hpp"
0018 
0019 #include <fstream>
0020 #include <iostream>
0021 #include <random>
0022 
0023 using namespace ActsExamples;
0024 using namespace Acts::Test;
0025 
0026 std::mt19937 gen(23);
0027 
0028 auto makeTestSimhits(std::size_t nSimHits) {
0029   std::uniform_int_distribution<std::uint64_t> distIds(
0030       1, std::numeric_limits<std::uint64_t>::max());
0031   std::uniform_int_distribution<std::int32_t> distIndex(1, 20);
0032 
0033   SimHitContainer simhits;
0034   for (auto i = 0ul; i < nSimHits; ++i) {
0035     Acts::GeometryIdentifier geoid(distIds(gen));
0036     SimBarcode pid(distIds(gen));
0037 
0038     Acts::Vector4 pos4 = Acts::Vector4::Random();
0039     Acts::Vector4 before4 = Acts::Vector4::Random();
0040     Acts::Vector4 after4 = Acts::Vector4::Random();
0041 
0042     auto index = distIndex(gen);
0043 
0044     simhits.insert(SimHit(geoid, pid, pos4, before4, after4, index));
0045   }
0046 
0047   return simhits;
0048 }
0049 
0050 BOOST_AUTO_TEST_SUITE(RootSimHitReaderWriter)
0051 
0052 BOOST_AUTO_TEST_CASE(RoundTripTest) {
0053   ////////////////////////////
0054   // Create some dummy data //
0055   ////////////////////////////
0056   auto simhits1 = makeTestSimhits(20);
0057   auto simhits2 = makeTestSimhits(15);
0058 
0059   ///////////
0060   // Write //
0061   ///////////
0062   RootSimHitWriter::Config writerConfig;
0063   writerConfig.inputSimHits = "hits";
0064   writerConfig.filePath = "./testhits.root";
0065 
0066   RootSimHitWriter writer(writerConfig, Acts::Logging::WARNING);
0067 
0068   auto readWriteTool =
0069       GenericReadWriteTool<>().add(writerConfig.inputSimHits, simhits1);
0070 
0071   // Write two different events
0072   readWriteTool.write(writer, 11);
0073 
0074   std::get<0>(readWriteTool.tuple) = simhits2;
0075   readWriteTool.write(writer, 22);
0076 
0077   writer.finalize();
0078 
0079   //////////
0080   // Read //
0081   //////////
0082   RootSimHitReader::Config readerConfig;
0083   readerConfig.outputSimHits = "hits";
0084   readerConfig.filePath = "./testhits.root";
0085 
0086   RootSimHitReader reader(readerConfig, Acts::Logging::WARNING);
0087   // Read two different events
0088   const auto [hitsRead2] = readWriteTool.read(reader, 22);
0089   const auto [hitsRead1] = readWriteTool.read(reader, 11);
0090   reader.finalize();
0091 
0092   ///////////
0093   // Check //
0094   ///////////
0095 
0096   auto check = [](const auto &testhits, const auto &refhits, auto tol) {
0097     BOOST_CHECK_EQUAL(testhits.size(), refhits.size());
0098 
0099     for (const auto &[ref, test] : Acts::zip(refhits, testhits)) {
0100       CHECK_CLOSE_ABS(test.fourPosition(), ref.fourPosition(), tol);
0101       CHECK_CLOSE_ABS(test.momentum4After(), ref.momentum4After(), tol);
0102       CHECK_CLOSE_ABS(test.momentum4Before(), ref.momentum4Before(), tol);
0103 
0104       BOOST_CHECK_EQUAL(ref.geometryId(), test.geometryId());
0105       BOOST_CHECK_EQUAL(ref.particleId(), test.particleId());
0106       BOOST_CHECK_EQUAL(ref.index(), test.index());
0107     }
0108   };
0109 
0110   check(hitsRead1, simhits1, 1.e-6);
0111   check(hitsRead2, simhits2, 1.e-6);
0112 }
0113 
0114 BOOST_AUTO_TEST_SUITE_END()