Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:09:46

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/Geometry/VolumeAssociationTest.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Detector/Detector.hpp"
0013 #include "Acts/Detector/DetectorVolume.hpp"
0014 
0015 #include <exception>
0016 #include <memory>
0017 #include <string>
0018 #include <vector>
0019 
0020 ActsExamples::VolumeAssociationTest::VolumeAssociationTest(
0021     const Config& cfg, Acts::Logging::Level level)
0022     : IAlgorithm(cfg.name, level), m_cfg(cfg) {
0023   if (m_cfg.detector == nullptr) {
0024     throw std::invalid_argument("Missing detector object");
0025   }
0026   if (m_cfg.randomNumbers == nullptr) {
0027     throw std::invalid_argument("Missing random numbers tool");
0028   }
0029   if (m_cfg.randomRange.size() < 2) {
0030     throw std::invalid_argument(
0031         "Random range needs to be at least 2-dimensional");
0032   }
0033 }
0034 
0035 ActsExamples::ProcessCode ActsExamples::VolumeAssociationTest::execute(
0036     const AlgorithmContext& ctx) const {
0037   auto rng = m_cfg.randomNumbers->spawnGenerator(ctx);
0038 
0039   // Setup random number distributions for some quantities
0040   std::uniform_real_distribution<Acts::ActsScalar> phiDist(-M_PI, M_PI);
0041   std::uniform_real_distribution<Acts::ActsScalar> rDist(0.,
0042                                                          m_cfg.randomRange[0u]);
0043   std::uniform_real_distribution<Acts::ActsScalar> zDist(-m_cfg.randomRange[1u],
0044                                                          m_cfg.randomRange[1u]);
0045 
0046   // Lemma for vector creation
0047   auto testPosition = [&]() -> Acts::Vector3 {
0048     Acts::ActsScalar r = rDist(rng);
0049     Acts::ActsScalar phi = phiDist(rng);
0050     Acts::ActsScalar z = zDist(rng);
0051     return Acts::Vector3(r * cos(phi), r * sin(phi), z);
0052   };
0053 
0054   std::size_t failedSearch = 0;
0055   std::size_t failedAssignment = 0;
0056   for (std::size_t it = 0; it < m_cfg.ntests; ++it) {
0057     auto pos = testPosition();
0058     auto dv = m_cfg.detector->findDetectorVolume(ctx.geoContext, pos);
0059     if (dv == nullptr) {
0060       ++failedSearch;
0061     }
0062     if (!dv->inside(ctx.geoContext, pos)) {
0063       ++failedAssignment;
0064     }
0065   }
0066   if (failedSearch > 0) {
0067     ACTS_ERROR("Failed to find detector volume " << failedSearch << " times");
0068   }
0069   if (failedAssignment > 0) {
0070     ACTS_ERROR("Failed to assign detector volume " << failedAssignment
0071                                                    << " times");
0072   }
0073 
0074   return ProcessCode::SUCCESS;
0075 }