Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:13:38

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Geometry/GeometryContext.hpp"
0013 #include "Acts/Utilities/AxisDefinitions.hpp"
0014 #include "Acts/Utilities/VectorHelpers.hpp"
0015 
0016 #include <functional>
0017 #include <memory>
0018 
0019 namespace Acts {
0020 
0021 template <class T>
0022 class ObjectSorterT {
0023  public:
0024   /// Constructor from a binning value
0025   ///
0026   /// @param aDir is the direction in which the sorting is done
0027   explicit ObjectSorterT(AxisDirection aDir) : m_sortingDirection(aDir) {}
0028 
0029   /// Comparison operator
0030   ///
0031   /// @param one first object
0032   /// @param two second object
0033   ///
0034   /// @return boolean indicator
0035   bool operator()(T one, T two) const {
0036     using VectorHelpers::eta;
0037     using VectorHelpers::perp;
0038     using VectorHelpers::phi;
0039     using enum AxisDirection;
0040     switch (m_sortingDirection) {
0041       // compare on x
0042       case AxisX: {
0043         return one.x() < two.x();
0044       }
0045       // compare on y
0046       case AxisY: {
0047         return one.y() < two.y();
0048       }
0049       // compare on z
0050       case AxisZ: {
0051         return one.z() < two.z();
0052       }
0053       // compare on r
0054       case AxisR: {
0055         return perp(one) < perp(two);
0056       }
0057       // compare on phi
0058       case AxisPhi: {
0059         return phi(one) < phi(two);
0060       }
0061       // compare on eta
0062       case AxisEta: {
0063         return eta(one) < eta(two);
0064       }
0065       // default for the moment
0066       default: {
0067         return one.norm() < two.norm();
0068       }
0069     }
0070   }
0071 
0072   AxisDirection sortingDirection() const { return m_sortingDirection; }
0073 
0074  private:
0075   AxisDirection m_sortingDirection;  ///< the binning value
0076 };
0077 
0078 /// This will check on absolute distance
0079 template <class T>
0080 class DistanceSorterT {
0081  public:
0082   /// Constructor from a binning value
0083   ///
0084   /// @param aDir is the value in which the sorting is done
0085   /// @param reference is the reference point
0086   DistanceSorterT(AxisDirection aDir, Vector3 reference)
0087       : m_sortingDirection(aDir),
0088         m_reference(reference),
0089         m_refR(VectorHelpers::perp(reference)),
0090         m_refPhi(VectorHelpers::phi(reference)),
0091         m_refEta(VectorHelpers::eta(reference)) {}
0092 
0093   /// Comparison operator
0094   ///
0095   /// @tparam one first object
0096   /// @tparam two second object
0097   ///
0098   /// @return boolean indicator
0099   bool operator()(T one, T two) const {
0100     using Acts::VectorHelpers::eta;
0101     using Acts::VectorHelpers::perp;
0102     using Acts::VectorHelpers::phi;
0103     // switch the sorting value
0104     // - AxisX, AxisY, AxisZ, AxisR, AxisPhi, AxisRPhi, AxisTheta, AxisEta
0105     switch (m_sortingDirection) {
0106       // compare on diff x
0107       case AxisDirection::AxisX: {
0108         double diffOneX = one.x() - m_reference.x();
0109         double diffTwoX = two.x() - m_reference.x();
0110         return std::abs(diffOneX) < std::abs(diffTwoX);
0111       }
0112       // compare on diff y
0113       case AxisDirection::AxisY: {
0114         double diffOneY = one.y() - m_reference.y();
0115         double diffTwoY = two.y() - m_reference.y();
0116         return std::abs(diffOneY) < std::abs(diffTwoY);
0117       }
0118       // compare on diff z
0119       case AxisDirection::AxisZ: {
0120         double diffOneZ = one.z() - m_reference.z();
0121         double diffTwoZ = two.z() - m_reference.z();
0122         return std::abs(diffOneZ) < std::abs(diffTwoZ);
0123       }
0124       // compare on r
0125       case AxisDirection::AxisR: {
0126         double diffOneR = perp(one) - m_refR;
0127         double diffTwoR = perp(two) - m_refR;
0128         return std::abs(diffOneR) < std::abs(diffTwoR);
0129       }
0130       // compare on phi /// @todo add cyclic value
0131       case AxisDirection::AxisPhi: {
0132         double diffOnePhi = phi(one) - m_refPhi;
0133         double diffTwoPhi = phi(two) - m_refPhi;
0134         return std::abs(diffOnePhi) < std::abs(diffTwoPhi);
0135       }
0136       // compare on eta
0137       case AxisDirection::AxisEta: {
0138         double diffOneEta = eta(one) - m_refEta;
0139         double diffTwoEta = eta(two) - m_refEta;
0140         return std::abs(diffOneEta) < std::abs(diffTwoEta);
0141       }
0142       // default for the moment
0143       default: {
0144         T diffOne(one - m_reference);
0145         T diffTwo(two - m_reference);
0146         return diffOne.mag2() < diffTwo.mag2();
0147       }
0148     }
0149   }
0150 
0151  private:
0152   AxisDirection m_sortingDirection;  ///< the sorting direction
0153   T m_reference;
0154   double m_refR;
0155   double m_refPhi;
0156   double m_refEta;
0157 };
0158 
0159 template <class T>
0160 class GeometryObjectSorterT {
0161  public:
0162   /// Constructor from a sorting direction
0163   ///
0164   /// @param gctx The geometry context to use
0165   /// @param aDir is the direction in which the sorting is done
0166   /// @param transform is an optional transform to be performed
0167   GeometryObjectSorterT(const GeometryContext& gctx, AxisDirection aDir,
0168                         std::shared_ptr<const Transform3> transform = nullptr)
0169       : m_context(gctx),
0170         m_objectSorter(aDir),
0171         m_transform(std::move(transform)) {}
0172 
0173   /// Comparison operator
0174   ///
0175   /// @tparam one is the first object
0176   /// @tparam two is the second object
0177   ///
0178   /// @return boolean indicator
0179   bool operator()(T one, T two) const {
0180     // get the pos one / pos two
0181     Vector3 posOne = m_transform
0182                          ? m_transform->inverse() *
0183                                one->referencePosition(
0184                                    m_context, m_objectSorter.sortingDirection())
0185                          : one->referencePosition(
0186                                m_context, m_objectSorter.sortingDirection());
0187     Vector3 posTwo = m_transform
0188                          ? m_transform->inverse() *
0189                                two->referencePosition(
0190                                    m_context, m_objectSorter.sortingDirection())
0191                          : two->referencePosition(
0192                                m_context, m_objectSorter.sortingDirection());
0193     // now call the distance sorter
0194     return m_objectSorter.operator()(posOne, posTwo);
0195   }
0196 
0197  protected:
0198   std::reference_wrapper<const GeometryContext> m_context;
0199   ObjectSorterT<Vector3> m_objectSorter;
0200   std::shared_ptr<const Transform3> m_transform;
0201 };
0202 }  // namespace Acts