Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PlanarSurfaceMaskTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PlanarSurfaceMaskTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 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/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
11 
26 
27 #include <array>
28 #include <cmath>
29 #include <fstream>
30 #include <memory>
31 #include <string>
32 #include <tuple>
33 #include <utility>
34 #include <vector>
35 
38 
39 namespace bdata = boost::unit_test::data;
40 
41 namespace ActsFatras {
42 
43 std::vector<std::array<std::ofstream, 3>> out;
44 
45 BOOST_AUTO_TEST_SUITE(Digitization)
46 
47 BOOST_AUTO_TEST_CASE(PlaneMaskRectangleBounds) {
48  auto rectangleBounds = std::make_shared<Acts::RectangleBounds>(2., 3.5);
49  auto planeSurface = Acts::Surface::makeShared<Acts::PlaneSurface>(
50  Acts::Transform3::Identity(), rectangleBounds);
51 
53 
55  std::array<Acts::Vector2, 2> segment = {Acts::Vector2(2.5, -4.5),
56  Acts::Vector2(-1., -1.)};
57  auto clipped = psm.apply(*planeSurface, segment).value();
58 
59  CHECK_CLOSE_ABS(clipped[1].x(), segment[1].x(), Acts::s_epsilon);
60  CHECK_CLOSE_ABS(clipped[1].y(), segment[1].y(), Acts::s_epsilon);
61  CHECK_CLOSE_ABS(clipped[0].x(), 1.5, Acts::s_epsilon);
62  CHECK_CLOSE_ABS(clipped[0].y(), -3.5, Acts::s_epsilon);
63 
65  segment = {Acts::Vector2(1., 4.), Acts::Vector2(3., 2.)};
66  clipped = psm.apply(*planeSurface, segment).value();
67 
68  CHECK_CLOSE_ABS(clipped[1].x(), 2., Acts::s_epsilon);
69  CHECK_CLOSE_ABS(clipped[1].y(), 3., Acts::s_epsilon);
70  CHECK_CLOSE_ABS(clipped[0].x(), 1.5, Acts::s_epsilon);
71  CHECK_CLOSE_ABS(clipped[0].y(), 3.5, Acts::s_epsilon);
72 
74  segment = {Acts::Vector2(-1., 0.5), Acts::Vector2(0., 2.)};
75  clipped = psm.apply(*planeSurface, segment).value();
76 
77  CHECK_CLOSE_ABS(clipped[0].x(), segment[0].x(), Acts::s_epsilon);
78  CHECK_CLOSE_ABS(clipped[0].y(), segment[0].y(), Acts::s_epsilon);
79  CHECK_CLOSE_ABS(clipped[1].x(), segment[1].x(), Acts::s_epsilon);
80  CHECK_CLOSE_ABS(clipped[1].y(), segment[1].y(), Acts::s_epsilon);
81 }
82 
83 BOOST_AUTO_TEST_CASE(DiscMaskRadialBounds) {
84  auto discRadial =
85  std::make_shared<Acts::RadialBounds>(2., 7.5, M_PI_4, M_PI_2);
86  auto discSurface = Acts::Surface::makeShared<Acts::DiscSurface>(
87  Acts::Transform3::Identity(), discRadial);
88 
90 
92  std::array<Acts::Vector2, 2> segment = {Acts::Vector2(0.5, 1.8),
93  Acts::Vector2(0.9, 6.)};
94  auto clipped = psm.apply(*discSurface, segment).value();
95 
96  CHECK_CLOSE_ABS(clipped[1].x(), segment[1].x(), Acts::s_epsilon);
97  CHECK_CLOSE_ABS(clipped[1].y(), segment[1].y(), Acts::s_epsilon);
99  5 * Acts::s_epsilon);
100 
102  segment = {Acts::Vector2(0.5, 2.8), Acts::Vector2(0.9, 8.5)};
103  clipped = psm.apply(*discSurface, segment).value();
104 
105  CHECK_CLOSE_ABS(clipped[0].x(), segment[0].x(), Acts::s_epsilon);
106  CHECK_CLOSE_ABS(clipped[0].y(), segment[0].y(), Acts::s_epsilon);
108  5 * Acts::s_epsilon);
109 
111  segment = {Acts::Vector2(0.5, 1.8), Acts::Vector2(0.9, 8.5)};
112  clipped = psm.apply(*discSurface, segment).value();
114  5 * Acts::s_epsilon);
116  5 * Acts::s_epsilon);
118  segment = {Acts::Vector2(2.8, 2.5), Acts::Vector2(0., 3.5)};
119  clipped = psm.apply(*discSurface, segment).value();
120  CHECK_CLOSE_ABS(Acts::VectorHelpers::phi(clipped[0]), M_PI_4,
122 
124  segment = {Acts::Vector2(0., 3.5), Acts::Vector2(-8., 5.)};
125  clipped = psm.apply(*discSurface, segment).value();
126  CHECK_CLOSE_ABS(Acts::VectorHelpers::phi(clipped[1]), M_PI_2 + M_PI_4,
128 }
129 
130 std::vector<std::array<std::ofstream, 3>> segmentOutput;
131 int ntests = 100;
132 
134 BOOST_DATA_TEST_CASE(RandomPlanarSurfaceMask,
135  bdata::random(0., 1.) ^ bdata::random(0., 1.) ^
136  bdata::random(0., 1.) ^ bdata::random(0., 1.) ^
137  bdata::xrange(ntests),
138  startR0, startR1, endR0, endR1, index) {
140 
142 
143  // Test beds with random numbers generated inside
145  // Smearing 10 percent outside
146  auto testBeds = pstd(1.1);
147 
148  DigitizationCsvOutput csvHelper;
149 
150  int itb = 0;
151  for (const auto& tb : testBeds) {
152  const auto& name = std::get<0>(tb);
153  const auto* surface = (std::get<1>(tb)).get();
154  const auto& randomizer = std::get<3>(tb);
155 
156  if (index == 0) {
157  std::ofstream shape;
158  const Acts::Vector2 centerXY = surface->center(geoCtx).segment<2>(0);
159 
160  // 0 - write the shape
161  shape.open("PlanarSurfaceMask" + name + "Borders.csv");
162  if (surface->type() == Acts::Surface::Plane) {
163  const auto* pBounds =
164  static_cast<const Acts::PlanarBounds*>(&(surface->bounds()));
165  csvHelper.writePolygon(shape, pBounds->vertices(1), -centerXY);
166  } else if (surface->type() == Acts::Surface::Disc) {
167  const auto* dBounds =
168  static_cast<const Acts::DiscBounds*>(&(surface->bounds()));
169  csvHelper.writePolygon(shape, dBounds->vertices(72), -centerXY);
170  }
171 
172  segmentOutput.push_back(std::array<std::ofstream, 3>());
173  segmentOutput[itb][0].open("PlanarSurfaceMask" + name + "Inside.csv");
174  segmentOutput[itb][1].open("PlanarSurfaceMask" + name + "Clipped.csv");
175  segmentOutput[itb][2].open("PlanarSurfaceMask" + name + "Outside.csv");
176  }
177 
178  auto start = randomizer(startR0, startR1);
179  auto end = randomizer(endR0, endR1);
180 
181  std::array<Acts::Vector2, 2> segment = {start, end};
182  auto clippedTest = psm.apply(*surface, segment);
183  if (clippedTest.ok()) {
184  auto clipped = clippedTest.value();
185  if (segment == clipped) {
186  csvHelper.writeLine(segmentOutput[itb][0], start, end);
187  } else {
188  csvHelper.writeLine(segmentOutput[itb][1], clipped[0], clipped[1]);
189  }
190  } else {
191  csvHelper.writeLine(segmentOutput[itb][2], start, end);
192  }
193  ++itb;
194  }
195 
196  // close the lines
197  if (itb == ntests - 1) {
198  segmentOutput[itb][0].close();
199  segmentOutput[itb][1].close();
200  segmentOutput[itb][2].close();
201  }
202 }
203 
204 BOOST_AUTO_TEST_SUITE_END()
205 
206 } // namespace ActsFatras