Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-16 08:02:08

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   /// @param one First object to compare
0095   /// @param two Second object to compare
0096   ///
0097   /// @return boolean indicator
0098   bool operator()(T one, T two) const {
0099     using Acts::VectorHelpers::eta;
0100     using Acts::VectorHelpers::perp;
0101     using Acts::VectorHelpers::phi;
0102     // switch the sorting value
0103     // - AxisX, AxisY, AxisZ, AxisR, AxisPhi, AxisRPhi, AxisTheta, AxisEta
0104     switch (m_sortingDirection) {
0105       // compare on diff x
0106       case AxisDirection::AxisX: {
0107         double diffOneX = one.x() - m_reference.x();
0108         double diffTwoX = two.x() - m_reference.x();
0109         return std::abs(diffOneX) < std::abs(diffTwoX);
0110       }
0111       // compare on diff y
0112       case AxisDirection::AxisY: {
0113         double diffOneY = one.y() - m_reference.y();
0114         double diffTwoY = two.y() - m_reference.y();
0115         return std::abs(diffOneY) < std::abs(diffTwoY);
0116       }
0117       // compare on diff z
0118       case AxisDirection::AxisZ: {
0119         double diffOneZ = one.z() - m_reference.z();
0120         double diffTwoZ = two.z() - m_reference.z();
0121         return std::abs(diffOneZ) < std::abs(diffTwoZ);
0122       }
0123       // compare on r
0124       case AxisDirection::AxisR: {
0125         double diffOneR = perp(one) - m_refR;
0126         double diffTwoR = perp(two) - m_refR;
0127         return std::abs(diffOneR) < std::abs(diffTwoR);
0128       }
0129       // compare on phi /// @todo add cyclic value
0130       case AxisDirection::AxisPhi: {
0131         double diffOnePhi = phi(one) - m_refPhi;
0132         double diffTwoPhi = phi(two) - m_refPhi;
0133         return std::abs(diffOnePhi) < std::abs(diffTwoPhi);
0134       }
0135       // compare on eta
0136       case AxisDirection::AxisEta: {
0137         double diffOneEta = eta(one) - m_refEta;
0138         double diffTwoEta = eta(two) - m_refEta;
0139         return std::abs(diffOneEta) < std::abs(diffTwoEta);
0140       }
0141       // default for the moment
0142       default: {
0143         T diffOne(one - m_reference);
0144         T diffTwo(two - m_reference);
0145         return diffOne.mag2() < diffTwo.mag2();
0146       }
0147     }
0148   }
0149 
0150  private:
0151   AxisDirection m_sortingDirection;  ///< the sorting direction
0152   T m_reference;
0153   double m_refR;
0154   double m_refPhi;
0155   double m_refEta;
0156 };
0157 
0158 template <class T>
0159 class GeometryObjectSorterT {
0160  public:
0161   /// Constructor from a sorting direction
0162   ///
0163   /// @param gctx The geometry context to use
0164   /// @param aDir is the direction in which the sorting is done
0165   /// @param transform is an optional transform to be performed
0166   GeometryObjectSorterT(const GeometryContext& gctx, AxisDirection aDir,
0167                         std::shared_ptr<const Transform3> transform = nullptr)
0168       : m_context(gctx),
0169         m_objectSorter(aDir),
0170         m_transform(std::move(transform)) {}
0171 
0172   /// Comparison operator
0173   ///
0174   /// @tparam one is the first object
0175   /// @tparam two is the second object
0176   ///
0177   /// @return boolean indicator
0178   bool operator()(T one, T two) const {
0179     // get the pos one / pos two
0180     Vector3 posOne = m_transform
0181                          ? m_transform->inverse() *
0182                                one->referencePosition(
0183                                    m_context, m_objectSorter.sortingDirection())
0184                          : one->referencePosition(
0185                                m_context, m_objectSorter.sortingDirection());
0186     Vector3 posTwo = m_transform
0187                          ? m_transform->inverse() *
0188                                two->referencePosition(
0189                                    m_context, m_objectSorter.sortingDirection())
0190                          : two->referencePosition(
0191                                m_context, m_objectSorter.sortingDirection());
0192     // now call the distance sorter
0193     return m_objectSorter.operator()(posOne, posTwo);
0194   }
0195 
0196  protected:
0197   std::reference_wrapper<const GeometryContext> m_context;
0198   ObjectSorterT<Vector3> m_objectSorter;
0199   std::shared_ptr<const Transform3> m_transform;
0200 };
0201 }  // namespace Acts