Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2022 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 "Acts/Geometry/Extent.hpp"
0010 
0011 #include "Acts/Utilities/VectorHelpers.hpp"
0012 
0013 #include <algorithm>
0014 #include <cmath>
0015 #include <cstddef>
0016 #include <iomanip>
0017 #include <limits>
0018 
0019 Acts::Extent::Extent(
0020     const std::array<std::array<ActsScalar, 2>, binValues>& envelope)
0021     : m_constrains(0), m_envelope(envelope) {
0022   m_range[binR] =
0023       Range1D<ActsScalar>(0., std::numeric_limits<ActsScalar>::max());
0024   m_range[binPhi] = Range1D<ActsScalar>(-M_PI, M_PI);
0025   m_range[binRPhi] =
0026       Range1D<ActsScalar>(0., std::numeric_limits<ActsScalar>::max());
0027   m_range[binMag] =
0028       Range1D<ActsScalar>(0., std::numeric_limits<ActsScalar>::max());
0029 }
0030 
0031 void Acts::Extent::extend(const Vector3& vtx,
0032                           const std::vector<BinningValue>& bValues,
0033                           bool applyEnv, bool fillHistograms) {
0034   for (auto bValue : bValues) {
0035     // Get the casted value given the binning value description
0036     ActsScalar cValue = VectorHelpers::cast(vtx, bValue);
0037     if (fillHistograms) {
0038       m_valueHistograms[bValue].push_back(cValue);
0039     }
0040     // Apply envelope as suggested
0041     ActsScalar lEnv = applyEnv ? m_envelope[bValue][0] : 0.;
0042     ActsScalar hEnv = applyEnv ? m_envelope[bValue][1] : 0.;
0043     ActsScalar mValue = cValue - lEnv;
0044     // Special protection for radial value
0045     if (bValue == binR && mValue < 0.) {
0046       mValue = std::max(mValue, 0.);
0047     }
0048     if (constrains(bValue)) {
0049       m_range[bValue].expand(mValue, cValue + hEnv);
0050     } else {
0051       m_range[bValue].shrink(mValue, cValue + hEnv);
0052     }
0053     m_constrains.set(bValue);
0054   }
0055 }
0056 
0057 void Acts::Extent::extend(const Extent& rhs,
0058                           const std::vector<BinningValue>& bValues,
0059                           bool applyEnv) {
0060   for (auto bValue : bValues) {
0061     // The value is constraint, envelope can be optional
0062     if (rhs.constrains(bValue)) {
0063       ActsScalar lEnv = applyEnv ? m_envelope[bValue][0] : 0.;
0064       ActsScalar hEnv = applyEnv ? m_envelope[bValue][1] : 0.;
0065       if (constrains(bValue)) {
0066         m_range[bValue].expand(rhs.range()[bValue].min() - lEnv,
0067                                rhs.range()[bValue].max() + hEnv);
0068       } else {
0069         m_range[bValue].shrink(rhs.range()[bValue].min() - lEnv,
0070                                rhs.range()[bValue].max() + hEnv);
0071       }
0072       m_constrains.set(bValue);
0073     } else if (rhs.envelope()[bValue] != zeroEnvelope) {
0074       // Only an envelope given, but value is not constraint -> apply envelope
0075       m_range[bValue].expand(m_range[bValue].min() - rhs.envelope()[bValue][0],
0076                              m_range[bValue].max() + rhs.envelope()[bValue][1]);
0077       m_constrains.set(bValue);
0078     }
0079   }
0080 }
0081 
0082 void Acts::Extent::addConstrain(const Acts::Extent& rhs,
0083                                 const ExtentEnvelope& envelope) {
0084   for (const auto& bValue : s_binningValues) {
0085     if (rhs.constrains(bValue) && !constrains(bValue)) {
0086       const auto& cRange = rhs.range(bValue);
0087       m_range[bValue].setMin(cRange.min() - envelope[bValue][0u]);
0088       m_range[bValue].setMax(cRange.max() + envelope[bValue][1u]);
0089       m_constrains.set(bValue);
0090     }
0091   }
0092 }
0093 
0094 void Acts::Extent::set(BinningValue bValue, ActsScalar min, ActsScalar max) {
0095   ActsScalar minval = min;
0096   if (bValue == binR && minval < 0.) {
0097     minval = 0.;
0098   }
0099   m_range[bValue] = Range1D<ActsScalar>{minval, max};
0100   m_constrains.set(bValue);
0101 }
0102 
0103 void Acts::Extent::setEnvelope(const ExtentEnvelope& envelope) {
0104   m_envelope = envelope;
0105 }
0106 
0107 bool Acts::Extent::contains(const Vector3& vtx) const {
0108   Extent checkExtent;
0109   for (const auto& bv : s_binningValues) {
0110     if (constrains(bv)) {
0111       ActsScalar vtxVal = VectorHelpers::cast(vtx, bv);
0112       checkExtent.set(bv, vtxVal, vtxVal);
0113     }
0114   }
0115   return contains(checkExtent);
0116 }
0117 
0118 bool Acts::Extent::contains(const Extent& rhs, BinningValue bValue) const {
0119   // Helper to check including a constraint bit set check
0120   auto checkContainment = [&](BinningValue bvc) -> bool {
0121     if (!constrains(bvc)) {
0122       return true;
0123     }
0124     return (rhs.range()[bvc] <= m_range[bvc]);
0125   };
0126 
0127   // Check all
0128   if (bValue == binValues) {
0129     for (const auto& bv : s_binningValues) {
0130       if (!checkContainment(bv)) {
0131         return false;
0132       }
0133     }
0134     return true;
0135   }
0136   // Check specific
0137   return checkContainment(bValue);
0138 }
0139 
0140 bool Acts::Extent::intersects(const Extent& rhs, BinningValue bValue) const {
0141   // Helper to check including a constraint bit set check
0142   auto checkIntersect = [&](BinningValue bvc) -> bool {
0143     if (!constrains(bvc) || !rhs.constrains(bvc)) {
0144       return false;
0145     }
0146     return (m_range[bvc] && rhs.range()[bvc]);
0147   };
0148 
0149   // Check all
0150   if (bValue == binValues) {
0151     for (const auto& bv : s_binningValues) {
0152       if (checkIntersect(bv)) {
0153         return true;
0154       }
0155     }
0156     return false;
0157   }
0158   // Check specific
0159   return checkIntersect(bValue);
0160 }
0161 
0162 bool Acts::Extent::constrains(BinningValue bValue) const {
0163   if (bValue == binValues) {
0164     return (m_constrains.count() > 0);
0165   }
0166   return m_constrains.test(std::size_t(bValue));
0167 }
0168 
0169 bool Acts::Extent::operator==(const Extent& e) const {
0170   if (m_constrains != e.m_constrains) {
0171     return false;
0172   }
0173   if (m_envelope != e.m_envelope) {
0174     return false;
0175   }
0176   if (!(m_range == e.m_range)) {
0177     return false;
0178   }
0179   if (m_valueHistograms != e.m_valueHistograms) {
0180     return false;
0181   }
0182   return true;
0183 }
0184 
0185 std::string Acts::Extent::toString(const std::string& indent) const {
0186   std::stringstream sl;
0187   sl << indent << "Extent in space : " << std::endl;
0188   for (const auto& bv : s_binningValues) {
0189     if (constrains(bv)) {
0190       sl << indent << "  - value :" << std::setw(10) << binningValueNames()[bv]
0191          << " | range = [" << m_range[bv].min() << ", " << m_range[bv].max()
0192          << "]" << std::endl;
0193     }
0194   }
0195   return sl.str();
0196 }
0197 
0198 // Overload of << operator for std::ostream for debug output
0199 std::ostream& Acts::operator<<(std::ostream& sl, const Extent& rhs) {
0200   sl << rhs.toString();
0201   return sl;
0202 }