Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-01 08:07:48

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