File indexing completed on 2025-08-06 08:11:21
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Geometry/CylinderLayer.hpp"
0014 #include "Acts/Geometry/DiscLayer.hpp"
0015 #include "Acts/Geometry/Extent.hpp"
0016 #include "Acts/Geometry/GeometryContext.hpp"
0017 #include "Acts/Geometry/LayerCreator.hpp"
0018 #include "Acts/Geometry/ProtoLayer.hpp"
0019 #include "Acts/Geometry/SurfaceArrayCreator.hpp"
0020 #include "Acts/Surfaces/CylinderBounds.hpp"
0021 #include "Acts/Surfaces/PlanarBounds.hpp"
0022 #include "Acts/Surfaces/PlaneSurface.hpp"
0023 #include "Acts/Surfaces/RadialBounds.hpp"
0024 #include "Acts/Surfaces/RectangleBounds.hpp"
0025 #include "Acts/Surfaces/Surface.hpp"
0026 #include "Acts/Surfaces/SurfaceArray.hpp"
0027 #include "Acts/Surfaces/SurfaceBounds.hpp"
0028 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0029 #include "Acts/Utilities/BinningType.hpp"
0030 #include "Acts/Utilities/IAxis.hpp"
0031 #include "Acts/Utilities/Logger.hpp"
0032
0033 #include <algorithm>
0034 #include <array>
0035 #include <cmath>
0036 #include <cstddef>
0037 #include <iomanip>
0038 #include <iostream>
0039 #include <memory>
0040 #include <set>
0041 #include <string>
0042 #include <utility>
0043 #include <vector>
0044
0045 #include <boost/format.hpp>
0046
0047 namespace Acts::Test {
0048
0049
0050 GeometryContext tgContext = GeometryContext();
0051
0052 using SrfVec = std::vector<std::shared_ptr<const Surface>>;
0053
0054 void draw_surfaces(const SrfVec& surfaces, const std::string& fname) {
0055 std::ofstream os;
0056 os.open(fname);
0057
0058 os << std::fixed << std::setprecision(4);
0059
0060 std::size_t nVtx = 0;
0061 for (const auto& srfx : surfaces) {
0062 std::shared_ptr<const PlaneSurface> srf =
0063 std::dynamic_pointer_cast<const PlaneSurface>(srfx);
0064 const PlanarBounds* bounds =
0065 dynamic_cast<const PlanarBounds*>(&srf->bounds());
0066
0067 for (const auto& vtxloc : bounds->vertices()) {
0068 Vector3 vtx =
0069 srf->transform(tgContext) * Vector3(vtxloc.x(), vtxloc.y(), 0);
0070 os << "v " << vtx.x() << " " << vtx.y() << " " << vtx.z() << "\n";
0071 }
0072
0073
0074 os << "f";
0075 for (std::size_t i = 1; i <= bounds->vertices().size(); ++i) {
0076 os << " " << nVtx + i;
0077 }
0078 os << "\n";
0079
0080 nVtx += bounds->vertices().size();
0081 }
0082
0083 os.close();
0084 }
0085
0086 struct LayerCreatorFixture {
0087 std::shared_ptr<const SurfaceArrayCreator> p_SAC;
0088 std::shared_ptr<LayerCreator> p_LC;
0089
0090 std::vector<std::shared_ptr<const Surface>> m_surfaces;
0091
0092 LayerCreatorFixture() {
0093 p_SAC = std::make_shared<const SurfaceArrayCreator>(
0094 SurfaceArrayCreator::Config(),
0095 Acts::getDefaultLogger("SurfaceArrayCreator", Acts::Logging::VERBOSE));
0096 LayerCreator::Config cfg;
0097 cfg.surfaceArrayCreator = p_SAC;
0098 p_LC = std::make_shared<LayerCreator>(
0099 cfg, Acts::getDefaultLogger("LayerCreator", Acts::Logging::VERBOSE));
0100 }
0101
0102 template <typename... Args>
0103 bool checkBinning(Args&&... args) {
0104 return p_LC->checkBinning(std::forward<Args>(args)...);
0105 }
0106
0107 bool checkBinContentSize(const SurfaceArray* sArray, std::size_t n) {
0108 std::size_t nBins = sArray->size();
0109 bool result = true;
0110 for (std::size_t i = 0; i < nBins; ++i) {
0111 if (!sArray->isValidBin(i)) {
0112 continue;
0113 }
0114 std::vector<const Surface*> binContent = sArray->at(i);
0115 BOOST_TEST_INFO("Bin: " << i);
0116 BOOST_CHECK_EQUAL(binContent.size(), n);
0117 result = result && binContent.size() == n;
0118 }
0119
0120 return result;
0121 }
0122
0123 SrfVec fullPhiTestSurfacesEC(std::size_t n = 10, double shift = 0,
0124 double zbase = 0, double r = 10) {
0125 SrfVec res;
0126
0127 double phiStep = 2 * M_PI / n;
0128 for (std::size_t i = 0; i < n; ++i) {
0129 double z = zbase + ((i % 2 == 0) ? 1 : -1) * 0.2;
0130
0131 Transform3 trans;
0132 trans.setIdentity();
0133 trans.rotate(Eigen::AngleAxisd(i * phiStep + shift, Vector3(0, 0, 1)));
0134 trans.translate(Vector3(r, 0, z));
0135
0136 auto bounds = std::make_shared<const RectangleBounds>(2, 1);
0137 std::shared_ptr<PlaneSurface> srf =
0138 Surface::makeShared<PlaneSurface>(trans, bounds);
0139
0140 res.push_back(srf);
0141 m_surfaces.push_back(
0142 std::move(srf));
0143 }
0144
0145 return res;
0146 }
0147
0148 SrfVec fullPhiTestSurfacesBRL(int n = 10, double shift = 0, double zbase = 0,
0149 double incl = M_PI / 9., double w = 2,
0150 double h = 1.5) {
0151 SrfVec res;
0152
0153 double phiStep = 2 * M_PI / n;
0154 for (int i = 0; i < n; ++i) {
0155 double z = zbase;
0156
0157 Transform3 trans;
0158 trans.setIdentity();
0159 trans.rotate(Eigen::AngleAxisd(i * phiStep + shift, Vector3(0, 0, 1)));
0160 trans.translate(Vector3(10, 0, z));
0161 trans.rotate(Eigen::AngleAxisd(incl, Vector3(0, 0, 1)));
0162 trans.rotate(Eigen::AngleAxisd(M_PI / 2., Vector3(0, 1, 0)));
0163
0164 auto bounds = std::make_shared<const RectangleBounds>(w, h);
0165 std::shared_ptr<PlaneSurface> srf =
0166 Surface::makeShared<PlaneSurface>(trans, bounds);
0167
0168 res.push_back(srf);
0169 m_surfaces.push_back(
0170 std::move(srf));
0171 }
0172
0173 return res;
0174 }
0175
0176 SrfVec makeBarrel(int nPhi, int nZ, double w, double h) {
0177 double z0 = -(nZ - 1) * w;
0178 SrfVec res;
0179
0180 for (int i = 0; i < nZ; i++) {
0181 double z = i * w * 2 + z0;
0182 std::cout << "z=" << z << std::endl;
0183 SrfVec ring = fullPhiTestSurfacesBRL(nPhi, 0, z, M_PI / 9., w, h);
0184 res.insert(res.end(), ring.begin(), ring.end());
0185 }
0186
0187 return res;
0188 }
0189
0190 std::pair<SrfVec, std::vector<std::pair<const Surface*, const Surface*>>>
0191 makeBarrelStagger(int nPhi, int nZ, double shift = 0, double incl = M_PI / 9.,
0192 double w = 2, double h = 1.5) {
0193 double z0 = -(nZ - 1) * w;
0194 SrfVec res;
0195
0196 std::vector<std::pair<const Surface*, const Surface*>> pairs;
0197
0198 for (int i = 0; i < nZ; i++) {
0199 double z = i * w * 2 + z0;
0200
0201 double phiStep = 2 * M_PI / nPhi;
0202 for (int j = 0; j < nPhi; ++j) {
0203 Transform3 trans;
0204 trans.setIdentity();
0205 trans.rotate(Eigen::AngleAxisd(j * phiStep + shift, Vector3(0, 0, 1)));
0206 trans.translate(Vector3(10, 0, z));
0207 trans.rotate(Eigen::AngleAxisd(incl, Vector3(0, 0, 1)));
0208 trans.rotate(Eigen::AngleAxisd(M_PI / 2., Vector3(0, 1, 0)));
0209
0210 auto bounds = std::make_shared<const RectangleBounds>(w, h);
0211 std::shared_ptr<PlaneSurface> srfA =
0212 Surface::makeShared<PlaneSurface>(trans, bounds);
0213
0214 Vector3 nrm = srfA->normal(tgContext);
0215 Transform3 transB = trans;
0216 transB.pretranslate(nrm * 0.1);
0217 std::shared_ptr<PlaneSurface> srfB =
0218 Surface::makeShared<PlaneSurface>(transB, bounds);
0219
0220 pairs.push_back(std::make_pair(srfA.get(), srfB.get()));
0221
0222 res.push_back(srfA);
0223 res.push_back(srfB);
0224 m_surfaces.push_back(std::move(srfA));
0225 m_surfaces.push_back(std::move(srfB));
0226 }
0227 }
0228
0229 return std::make_pair(res, pairs);
0230 }
0231 };
0232
0233 BOOST_AUTO_TEST_SUITE(Tools)
0234
0235 BOOST_FIXTURE_TEST_CASE(LayerCreator_createCylinderLayer, LayerCreatorFixture) {
0236 std::vector<std::shared_ptr<const Surface>> srf;
0237
0238 srf = makeBarrel(30, 7, 2, 1.5);
0239 draw_surfaces(srf, "LayerCreator_createCylinderLayer_BRL_1.obj");
0240
0241
0242 double envR = 0.1, envZ = 0.5;
0243 ProtoLayer pl(tgContext, srf);
0244 pl.envelope[Acts::binR] = {envR, envR};
0245 pl.envelope[Acts::binZ] = {envZ, envZ};
0246 std::shared_ptr<CylinderLayer> layer =
0247 std::dynamic_pointer_cast<CylinderLayer>(
0248 p_LC->cylinderLayer(tgContext, srf, equidistant, equidistant, pl));
0249
0250
0251 double rMax = 10.6071, rMin = 9.59111;
0252 CHECK_CLOSE_REL(layer->thickness(), (rMax - rMin) + 2. * envR, 1e-3);
0253
0254 const CylinderBounds* bounds = &layer->bounds();
0255 CHECK_CLOSE_REL(bounds->get(CylinderBounds::eR), (rMax + rMin) / 2., 1e-3);
0256 CHECK_CLOSE_REL(bounds->get(CylinderBounds::eHalfLengthZ), 14 + envZ, 1e-3);
0257 BOOST_CHECK(checkBinning(tgContext, *layer->surfaceArray()));
0258 auto axes = layer->surfaceArray()->getAxes();
0259 BOOST_CHECK_EQUAL(axes.at(0)->getNBins(), 30u);
0260 BOOST_CHECK_EQUAL(axes.at(1)->getNBins(), 7u);
0261 CHECK_CLOSE_REL(axes.at(0)->getMin(), -M_PI, 1e-3);
0262 CHECK_CLOSE_REL(axes.at(0)->getMax(), M_PI, 1e-3);
0263 CHECK_CLOSE_REL(axes.at(1)->getMin(), -14, 1e-3);
0264 CHECK_CLOSE_REL(axes.at(1)->getMax(), 14, 1e-3);
0265
0266
0267
0268 ProtoLayer pl2(tgContext, srf);
0269 pl2.envelope[Acts::binR] = {envR, envR};
0270 pl2.envelope[Acts::binZ] = {envZ, envZ};
0271 layer = std::dynamic_pointer_cast<CylinderLayer>(
0272 p_LC->cylinderLayer(tgContext, srf, 30, 7, pl2));
0273 CHECK_CLOSE_REL(layer->thickness(), (rMax - rMin) + 2 * envR, 1e-3);
0274 bounds = &layer->bounds();
0275 CHECK_CLOSE_REL(bounds->get(CylinderBounds::eR), (rMax + rMin) / 2., 1e-3);
0276 CHECK_CLOSE_REL(bounds->get(CylinderBounds::eHalfLengthZ), 14 + envZ, 1e-3);
0277 BOOST_CHECK(checkBinning(tgContext, *layer->surfaceArray()));
0278 axes = layer->surfaceArray()->getAxes();
0279 BOOST_CHECK_EQUAL(axes.at(0)->getNBins(), 30u);
0280 BOOST_CHECK_EQUAL(axes.at(1)->getNBins(), 7u);
0281 CHECK_CLOSE_REL(axes.at(0)->getMin(), -M_PI, 1e-3);
0282 CHECK_CLOSE_REL(axes.at(0)->getMax(), M_PI, 1e-3);
0283 CHECK_CLOSE_REL(axes.at(1)->getMin(), -14, 1e-3);
0284 CHECK_CLOSE_REL(axes.at(1)->getMax(), 14, 1e-3);
0285
0286 layer = std::dynamic_pointer_cast<CylinderLayer>(
0287 p_LC->cylinderLayer(tgContext, srf, 13, 3, pl2));
0288 CHECK_CLOSE_REL(layer->thickness(), (rMax - rMin) + 2 * envR, 1e-3);
0289 bounds = &layer->bounds();
0290 CHECK_CLOSE_REL(bounds->get(CylinderBounds::eR), (rMax + rMin) / 2., 1e-3);
0291 CHECK_CLOSE_REL(bounds->get(CylinderBounds::eHalfLengthZ), 14 + envZ, 1e-3);
0292
0293
0294 BOOST_CHECK(checkBinning(tgContext, *layer->surfaceArray()));
0295 axes = layer->surfaceArray()->getAxes();
0296 BOOST_CHECK_EQUAL(axes.at(0)->getNBins(), 13u);
0297 BOOST_CHECK_EQUAL(axes.at(1)->getNBins(), 3u);
0298 CHECK_CLOSE_REL(axes.at(0)->getMin(), -M_PI, 1e-3);
0299 CHECK_CLOSE_REL(axes.at(0)->getMax(), M_PI, 1e-3);
0300 CHECK_CLOSE_REL(axes.at(1)->getMin(), -14, 1e-3);
0301 CHECK_CLOSE_REL(axes.at(1)->getMax(), 14, 1e-3);
0302
0303
0304 ProtoLayer pl3;
0305 pl3.extent.range(Acts::binR).set(1, 20);
0306 pl3.extent.range(Acts::binZ).set(-25, 25);
0307 layer = std::dynamic_pointer_cast<CylinderLayer>(
0308 p_LC->cylinderLayer(tgContext, srf, equidistant, equidistant, pl3));
0309 CHECK_CLOSE_REL(layer->thickness(), 19, 1e-3);
0310 bounds = &layer->bounds();
0311 CHECK_CLOSE_REL(bounds->get(CylinderBounds::eR), 10.5, 1e-3);
0312 CHECK_CLOSE_REL(bounds->get(CylinderBounds::eHalfLengthZ), 25, 1e-3);
0313
0314
0315
0316
0317 BOOST_CHECK(checkBinning(tgContext, *layer->surfaceArray()));
0318
0319 axes = layer->surfaceArray()->getAxes();
0320 BOOST_CHECK_EQUAL(axes.at(0)->getNBins(), 30u);
0321 BOOST_CHECK_EQUAL(axes.at(1)->getNBins(), 7u);
0322 CHECK_CLOSE_REL(axes.at(0)->getMin(), -M_PI, 1e-3);
0323 CHECK_CLOSE_REL(axes.at(0)->getMax(), M_PI, 1e-3);
0324 CHECK_CLOSE_REL(axes.at(1)->getMin(), -25, 1e-3);
0325 CHECK_CLOSE_REL(axes.at(1)->getMax(), 25, 1e-3);
0326 }
0327
0328 BOOST_FIXTURE_TEST_CASE(LayerCreator_createDiscLayer, LayerCreatorFixture) {
0329 std::vector<std::shared_ptr<const Surface>> surfaces;
0330 auto ringa = fullPhiTestSurfacesEC(30, 0, 0, 10);
0331 surfaces.insert(surfaces.end(), ringa.begin(), ringa.end());
0332 auto ringb = fullPhiTestSurfacesEC(30, 0, 0, 15);
0333 surfaces.insert(surfaces.end(), ringb.begin(), ringb.end());
0334 auto ringc = fullPhiTestSurfacesEC(30, 0, 0, 20);
0335 surfaces.insert(surfaces.end(), ringc.begin(), ringc.end());
0336 draw_surfaces(surfaces, "LayerCreator_createDiscLayer_EC_1.obj");
0337
0338 ProtoLayer pl(tgContext, surfaces);
0339 pl.extent.range(binZ).set(-10, 10);
0340 pl.extent.range(binR).set(5., 25.);
0341 std::shared_ptr<DiscLayer> layer = std::dynamic_pointer_cast<DiscLayer>(
0342 p_LC->discLayer(tgContext, surfaces, equidistant, equidistant, pl));
0343 CHECK_CLOSE_REL(layer->thickness(), 20, 1e-3);
0344 const RadialBounds* bounds =
0345 dynamic_cast<const RadialBounds*>(&layer->bounds());
0346 CHECK_CLOSE_REL(bounds->rMin(), 5, 1e-3);
0347 CHECK_CLOSE_REL(bounds->rMax(), 25, 1e-3);
0348 BOOST_CHECK(checkBinning(tgContext, *layer->surfaceArray()));
0349 auto axes = layer->surfaceArray()->getAxes();
0350 BOOST_CHECK_EQUAL(axes.at(0)->getNBins(), 3u);
0351 BOOST_CHECK_EQUAL(axes.at(1)->getNBins(), 30u);
0352 CHECK_CLOSE_REL(axes.at(0)->getMin(), 5, 1e-3);
0353 CHECK_CLOSE_REL(axes.at(0)->getMax(), 25, 1e-3);
0354 CHECK_CLOSE_REL(axes.at(1)->getMin(), -M_PI, 1e-3);
0355 CHECK_CLOSE_REL(axes.at(1)->getMax(), M_PI, 1e-3);
0356 checkBinContentSize(layer->surfaceArray(), 1);
0357
0358
0359
0360
0361
0362
0363
0364 double envMinR = 1, envMaxR = 1, envZ = 5;
0365 std::size_t nBinsR = 3, nBinsPhi = 30;
0366 ProtoLayer pl2(tgContext, surfaces);
0367 pl2.envelope[binR] = {envMinR, envMaxR};
0368 pl2.envelope[binZ] = {envZ, envZ};
0369 layer = std::dynamic_pointer_cast<DiscLayer>(
0370 p_LC->discLayer(tgContext, surfaces, nBinsR, nBinsPhi, pl2));
0371
0372 double rMin = 8, rMax = 22.0227;
0373 CHECK_CLOSE_REL(layer->thickness(), 0.4 + 2 * envZ, 1e-3);
0374 bounds = dynamic_cast<const RadialBounds*>(&layer->bounds());
0375 CHECK_CLOSE_REL(bounds->rMin(), rMin - envMinR, 1e-3);
0376 CHECK_CLOSE_REL(bounds->rMax(), rMax + envMaxR, 1e-3);
0377 BOOST_CHECK(checkBinning(tgContext, *layer->surfaceArray()));
0378 axes = layer->surfaceArray()->getAxes();
0379 BOOST_CHECK_EQUAL(axes.at(0)->getNBins(), nBinsR);
0380 BOOST_CHECK_EQUAL(axes.at(1)->getNBins(), nBinsPhi);
0381 CHECK_CLOSE_REL(axes.at(0)->getMin(), rMin, 1e-3);
0382 CHECK_CLOSE_REL(axes.at(0)->getMax(), rMax, 1e-3);
0383 CHECK_CLOSE_REL(axes.at(1)->getMin(), -M_PI, 1e-3);
0384 CHECK_CLOSE_REL(axes.at(1)->getMax(), M_PI, 1e-3);
0385 checkBinContentSize(layer->surfaceArray(), 1);
0386
0387
0388
0389
0390
0391
0392
0393 layer = std::dynamic_pointer_cast<DiscLayer>(
0394 p_LC->discLayer(tgContext, surfaces, equidistant, equidistant, pl2));
0395 CHECK_CLOSE_REL(layer->thickness(), 0.4 + 2 * envZ, 1e-3);
0396 bounds = dynamic_cast<const RadialBounds*>(&layer->bounds());
0397 CHECK_CLOSE_REL(bounds->rMin(), rMin - envMinR, 1e-3);
0398 CHECK_CLOSE_REL(bounds->rMax(), rMax + envMaxR, 1e-3);
0399 BOOST_CHECK(checkBinning(tgContext, *layer->surfaceArray()));
0400 axes = layer->surfaceArray()->getAxes();
0401 BOOST_CHECK_EQUAL(axes.at(0)->getNBins(), nBinsR);
0402 BOOST_CHECK_EQUAL(axes.at(1)->getNBins(), nBinsPhi);
0403 CHECK_CLOSE_REL(axes.at(0)->getMin(), rMin, 1e-3);
0404 CHECK_CLOSE_REL(axes.at(0)->getMax(), rMax, 1e-3);
0405 CHECK_CLOSE_REL(axes.at(1)->getMin(), -M_PI, 1e-3);
0406 CHECK_CLOSE_REL(axes.at(1)->getMax(), M_PI, 1e-3);
0407 checkBinContentSize(layer->surfaceArray(), 1);
0408
0409
0410
0411
0412
0413
0414 }
0415
0416 BOOST_FIXTURE_TEST_CASE(LayerCreator_barrelStagger, LayerCreatorFixture) {
0417 auto barrel = makeBarrelStagger(30, 7, 0, M_PI / 9.);
0418 auto brl = barrel.first;
0419 draw_surfaces(brl, "LayerCreator_barrelStagger.obj");
0420
0421 double envR = 0, envZ = 0;
0422 ProtoLayer pl(tgContext, brl);
0423 pl.envelope[binR] = {envR, envR};
0424 pl.envelope[binZ] = {envZ, envZ};
0425 std::shared_ptr<CylinderLayer> layer =
0426 std::dynamic_pointer_cast<CylinderLayer>(
0427 p_LC->cylinderLayer(tgContext, brl, equidistant, equidistant, pl));
0428
0429 auto axes = layer->surfaceArray()->getAxes();
0430 BOOST_CHECK_EQUAL(axes.at(0)->getNBins(), 30u);
0431 BOOST_CHECK_EQUAL(axes.at(1)->getNBins(), 7u);
0432
0433
0434 for (const auto& pr : barrel.second) {
0435 auto A = pr.first;
0436 auto B = pr.second;
0437
0438
0439
0440
0441
0442
0443 Vector3 ctr = A->binningPosition(tgContext, binR);
0444 auto binContent = layer->surfaceArray()->at(ctr);
0445 BOOST_CHECK_EQUAL(binContent.size(), 2u);
0446 std::set<const Surface*> act;
0447 act.insert(binContent[0]);
0448 act.insert(binContent[1]);
0449
0450 std::set<const Surface*> exp;
0451 exp.insert(A);
0452 exp.insert(B);
0453 BOOST_CHECK(exp == act);
0454 }
0455
0456
0457 checkBinning(tgContext, *layer->surfaceArray());
0458 }
0459
0460 BOOST_AUTO_TEST_SUITE_END()
0461 }