Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-23 08:24:39

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