Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChannelizerTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ChannelizerTests.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 
24 
25 #include <cmath>
26 #include <fstream>
27 #include <memory>
28 #include <string>
29 #include <tuple>
30 #include <utility>
31 #include <vector>
32 
35 
36 namespace bdata = boost::unit_test::data;
37 
38 namespace ActsFatras {
39 
40 BOOST_AUTO_TEST_SUITE(Digitization)
41 
42 BOOST_AUTO_TEST_CASE(ChannelizerCartesian) {
44 
45  auto rectangleBounds = std::make_shared<Acts::RectangleBounds>(1., 1.);
46  auto planeSurface = Acts::Surface::makeShared<Acts::PlaneSurface>(
47  Acts::Transform3::Identity(), rectangleBounds);
48 
49  // The segmentation
50  Acts::BinUtility pixelated(20, -1., 1., Acts::open, Acts::binX);
51  pixelated += Acts::BinUtility(20, -1., 1., Acts::open, Acts::binY);
52 
53  Channelizer cl;
54 
55  // Test: Normal hit into the surface
56  Acts::Vector2 nPosition(0.37, 0.76);
57  auto nSegments =
58  cl.segments(geoCtx, *planeSurface, pixelated, {nPosition, nPosition});
59  BOOST_CHECK(nSegments.size() == 1);
60  BOOST_CHECK(nSegments[0].bin[0] == 13);
61  BOOST_CHECK(nSegments[0].bin[1] == 17);
62 
63  // Test: Inclined hit into the surface - negative x direction
64  Acts::Vector2 ixPositionS(0.37, 0.76);
65  Acts::Vector2 ixPositionE(0.02, 0.73);
66  auto ixSegments =
67  cl.segments(geoCtx, *planeSurface, pixelated, {ixPositionS, ixPositionE});
68  BOOST_CHECK(ixSegments.size() == 4);
69 
70  // Test: Inclined hit into the surface - positive y direction
71  Acts::Vector2 iyPositionS(0.37, 0.76);
72  Acts::Vector2 iyPositionE(0.39, 0.91);
73  auto iySegments =
74  cl.segments(geoCtx, *planeSurface, pixelated, {iyPositionS, iyPositionE});
75  BOOST_CHECK(iySegments.size() == 3);
76 
77  // Test: Inclined hit into the surface - x/y direction
78  Acts::Vector2 ixyPositionS(-0.27, 0.76);
79  Acts::Vector2 ixyPositionE(-0.02, -0.73);
80  auto ixySegments = cl.segments(geoCtx, *planeSurface, pixelated,
81  {ixyPositionS, ixyPositionE});
82  BOOST_CHECK(ixySegments.size() == 18);
83 }
84 
85 BOOST_AUTO_TEST_CASE(ChannelizerPolarRadial) {
87 
88  auto radialBounds =
89  std::make_shared<const Acts::RadialBounds>(5., 10., 0.25, 0.);
90  auto radialDisc = Acts::Surface::makeShared<Acts::DiscSurface>(
91  Acts::Transform3::Identity(), radialBounds);
92 
93  // The segmentation
94  Acts::BinUtility strips(2, 5., 10., Acts::open, Acts::binR);
95  strips += Acts::BinUtility(250, -0.25, 0.25, Acts::open, Acts::binPhi);
96 
97  Channelizer cl;
98 
99  // Test: Normal hit into the surface
100  Acts::Vector2 nPosition(6.76, 0.5);
101  auto nSegments =
102  cl.segments(geoCtx, *radialDisc, strips, {nPosition, nPosition});
103  BOOST_CHECK(nSegments.size() == 1);
104  BOOST_CHECK(nSegments[0].bin[0] == 0);
105  BOOST_CHECK(nSegments[0].bin[1] == 161);
106 
107  // Test: now opver more phi strips
108  Acts::Vector2 sPositionS(6.76, 0.5);
109  Acts::Vector2 sPositionE(7.03, -0.3);
110  auto sSegment =
111  cl.segments(geoCtx, *radialDisc, strips, {sPositionS, sPositionE});
112  BOOST_CHECK(sSegment.size() == 59);
113 
114  // Test: jump over R boundary, but stay in phi bin
115  sPositionS = Acts::Vector2(6.76, 0.);
116  sPositionE = Acts::Vector2(7.83, 0.);
117  sSegment = cl.segments(geoCtx, *radialDisc, strips, {sPositionS, sPositionE});
118  BOOST_CHECK(sSegment.size() == 2);
119 }
120 
122 BOOST_DATA_TEST_CASE(RandomChannelizerTest,
123  bdata::random(0., 1.) ^ bdata::random(0., 1.) ^
124  bdata::random(0., 1.) ^ bdata::random(0., 1.) ^
125  bdata::xrange(25),
126  startR0, startR1, endR0, endR1, index) {
128  Channelizer cl;
129 
130  // Test beds with random numbers generated inside
132  auto testBeds = pstd(1.);
133 
134  DigitizationCsvOutput csvHelper;
135 
136  for (const auto& tb : testBeds) {
137  const auto& name = std::get<0>(tb);
138  const auto* surface = (std::get<1>(tb)).get();
139  const auto& segmentation = std::get<2>(tb);
140  const auto& randomizer = std::get<3>(tb);
141 
142  if (index == 0) {
143  std::ofstream shape;
144  std::ofstream grid;
145  const auto centerXY = surface->center(geoCtx).segment<2>(0);
146  // 0 - write the shape
147  shape.open("Channelizer" + name + "Borders.csv");
148  if (surface->type() == Acts::Surface::Plane) {
149  const auto* pBounds =
150  static_cast<const Acts::PlanarBounds*>(&(surface->bounds()));
151  csvHelper.writePolygon(shape, pBounds->vertices(1), -centerXY);
152  } else if (surface->type() == Acts::Surface::Disc) {
153  const auto* dBounds =
154  static_cast<const Acts::DiscBounds*>(&(surface->bounds()));
155  csvHelper.writePolygon(shape, dBounds->vertices(72), -centerXY);
156  }
157  // 1 - write the grid
158  grid.open("Channelizer" + name + "Grid.csv");
159  if (segmentation.binningData()[0].binvalue == Acts::binX and
160  segmentation.binningData()[1].binvalue == Acts::binY) {
161  double bxmin = segmentation.binningData()[0].min;
162  double bxmax = segmentation.binningData()[0].max;
163  double bymin = segmentation.binningData()[1].min;
164  double bymax = segmentation.binningData()[1].max;
165  const auto& xboundaries = segmentation.binningData()[0].boundaries();
166  const auto& yboundaries = segmentation.binningData()[1].boundaries();
167  for (const auto xval : xboundaries) {
168  csvHelper.writeLine(grid, {xval, bymin}, {xval, bymax});
169  }
170  for (const auto yval : yboundaries) {
171  csvHelper.writeLine(grid, {bxmin, yval}, {bxmax, yval});
172  }
173  } else if (segmentation.binningData()[0].binvalue == Acts::binR and
174  segmentation.binningData()[1].binvalue == Acts::binPhi) {
175  double brmin = segmentation.binningData()[0].min;
176  double brmax = segmentation.binningData()[0].max;
177  double bphimin = segmentation.binningData()[1].min;
178  double bphimax = segmentation.binningData()[1].max;
179  const auto& rboundaries = segmentation.binningData()[0].boundaries();
180  const auto& phiboundaries = segmentation.binningData()[1].boundaries();
181  for (const auto r : rboundaries) {
182  csvHelper.writeArc(grid, r, bphimin, bphimax);
183  }
184  for (const auto phi : phiboundaries) {
185  double cphi = std::cos(phi);
186  double sphi = std::sin(phi);
187  csvHelper.writeLine(grid, {brmin * cphi, brmin * sphi},
188  {brmax * cphi, brmax * sphi});
189  }
190  }
191  }
192 
193  auto start = randomizer(startR0, startR1);
194  auto end = randomizer(endR0, endR1);
195 
196  std::ofstream segments;
197  segments.open("Channelizer" + name + "Segments_n" + std::to_string(index) +
198  ".csv");
199 
200  std::ofstream cluster;
201  cluster.open("Channelizer" + name + "Cluster_n" + std::to_string(index) +
202  ".csv");
203 
205  auto cSegement = cl.segments(geoCtx, *surface, segmentation, {start, end});
206 
207  for (const auto& cs : cSegement) {
208  csvHelper.writeLine(segments, cs.path2D[0], cs.path2D[1]);
209  }
210 
211  segments.close();
212  cluster.close();
213  }
214 }
215 
216 BOOST_AUTO_TEST_SUITE_END()
217 
218 } // namespace ActsFatras