Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SimhitReaderWriterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SimhitReaderWriterTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
13 #include "Acts/Utilities/Zip.hpp"
17 
18 #include <fstream>
19 #include <iostream>
20 #include <random>
21 
22 using namespace ActsExamples;
23 using namespace Acts::Test;
24 
25 std::mt19937 gen(23);
26 
27 auto makeTestSimhits(std::size_t nSimHits) {
28  std::uniform_int_distribution<uint64_t> distIds(
29  1, std::numeric_limits<uint64_t>::max());
30  std::uniform_int_distribution<int32_t> distIndex(1, 20);
31 
32  SimHitContainer simhits;
33  for (auto i = 0ul; i < nSimHits; ++i) {
34  Acts::GeometryIdentifier geoid(distIds(gen));
35  SimBarcode pid(distIds(gen));
36 
37  Acts::Vector4 pos4 = Acts::Vector4::Random();
38  Acts::Vector4 before4 = Acts::Vector4::Random();
39  Acts::Vector4 after4 = Acts::Vector4::Random();
40 
41  auto index = distIndex(gen);
42 
43  simhits.insert(SimHit(geoid, pid, pos4, before4, after4, index));
44  }
45 
46  return simhits;
47 }
48 
49 BOOST_AUTO_TEST_SUITE(RootSimHitReaderWriter)
50 
51 BOOST_AUTO_TEST_CASE(RoundTripTest) {
53  // Create some dummy data //
55  auto simhits1 = makeTestSimhits(20);
56  auto simhits2 = makeTestSimhits(15);
57 
59  // Write //
61  RootSimHitWriter::Config writerConfig;
62  writerConfig.inputSimHits = "hits";
63  writerConfig.filePath = "./testhits.root";
64 
66 
67  auto readWriteTool =
68  GenericReadWriteTool<>().add(writerConfig.inputSimHits, simhits1);
69 
70  // Write two different events
71  readWriteTool.write(writer, 11);
72 
73  std::get<0>(readWriteTool.tuple) = simhits2;
74  readWriteTool.write(writer, 22);
75 
76  writer.finalize();
77 
79  // Read //
81  RootSimHitReader::Config readerConfig;
82  readerConfig.simHitCollection = "hits";
83  readerConfig.filePath = "./testhits.root";
84 
86  // Read two different events
87  const auto [hitsRead2] = readWriteTool.read(reader, 22);
88  const auto [hitsRead1] = readWriteTool.read(reader, 11);
89  reader.finalize();
90 
92  // Check //
94 
95  auto check = [](const auto &testhits, const auto &refhits, auto tol) {
96  BOOST_CHECK(testhits.size() == refhits.size());
97 
98  for (const auto &[ref, test] : Acts::zip(refhits, testhits)) {
99  CHECK_CLOSE_ABS(test.fourPosition(), ref.fourPosition(), tol);
100  CHECK_CLOSE_ABS(test.momentum4After(), ref.momentum4After(), tol);
101  CHECK_CLOSE_ABS(test.momentum4Before(), ref.momentum4Before(), tol);
102 
103  BOOST_CHECK(ref.geometryId() == test.geometryId());
104  BOOST_CHECK(ref.particleId() == test.particleId());
105  BOOST_CHECK(ref.index() == test.index());
106  }
107  };
108 
109  check(hitsRead1, simhits1, 1.e-6);
110  check(hitsRead2, simhits2, 1.e-6);
111 }
112 
113 BOOST_AUTO_TEST_SUITE_END()