Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:42

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 #pragma once
0010 
0011 #include "Acts/Utilities/BinningType.hpp"
0012 #include "Acts/Utilities/Delegate.hpp"
0013 
0014 #include <array>
0015 #include <string>
0016 #include <vector>
0017 
0018 #include "G4VPhysicalVolume.hh"
0019 
0020 namespace Acts {
0021 
0022 /// @brief Convert Acts binning value to Geant4 axis
0023 /// as Geant4 uses a different axis convention
0024 /// @param bv the Acts binning value
0025 EAxis binToGeant4Axis(const Acts::BinningValue& bv) {
0026   switch (bv) {
0027     case Acts::BinningValue::binX:
0028       return EAxis::kXAxis;
0029     case Acts::BinningValue::binY:
0030       return EAxis::kYAxis;
0031     case Acts::BinningValue::binZ:
0032       return EAxis::kZAxis;
0033     case Acts::BinningValue::binR:
0034       return EAxis::kRho;
0035     case Acts::BinningValue::binPhi:
0036       return EAxis::kPhi;
0037     default:
0038       throw std::invalid_argument(
0039           "No Geant4 axis conversion for this binning value");
0040   }
0041 }
0042 
0043 /// Interface class for selectors from physical volumes
0044 class IGeant4PhysicalVolumeSelector {
0045  public:
0046   virtual ~IGeant4PhysicalVolumeSelector() = default;
0047   /// @brief  The main interface method
0048   /// @param g4Phys the physical volume to be checked
0049   /// @return a boolean indicating if it should be selected or not
0050   virtual bool select(const G4VPhysicalVolume& g4Phys) const = 0;
0051 };
0052 
0053 namespace Geant4PhysicalVolumeSelectors {
0054 
0055 /// @brief  Struct that selects all G4VPhysicalVolume objects
0056 struct AllSelector : public IGeant4PhysicalVolumeSelector {
0057   bool select(const G4VPhysicalVolume& /*g4Phys*/) const final { return true; }
0058 };
0059 
0060 /// @brief Struct that selects G4VPhysicalVolume objects
0061 /// that match one of the provided names, exact or partially
0062 struct NameSelector : public IGeant4PhysicalVolumeSelector {
0063   std::vector<std::string> names = {};
0064   bool exact = false;
0065 
0066   /// Constructor with arguments
0067   /// @param ns the provided list of names
0068   /// @param e whether to select them exact or not
0069   NameSelector(const std::vector<std::string>& ns, bool e = false)
0070       : names(ns), exact(e) {}
0071 
0072   /// Secect function for the volume
0073   /// @param g4PhysVol the volume that is checked
0074   /// @return a boolean indicating the selection
0075   bool select(const G4VPhysicalVolume& g4PhysVol) const final {
0076     std::string volumeName = g4PhysVol.GetName();
0077     bool matched = false;
0078     for (const auto& name : names) {
0079       matched = exact ? (volumeName == name)
0080                       : volumeName.find(name) != std::string::npos;
0081       if (matched) {
0082         break;
0083       }
0084     }
0085     return matched;
0086   }
0087 };
0088 
0089 /// @brief Struct that selects G4VPhysicalVolume objects
0090 /// based on the allowed range of their position
0091 ///
0092 /// @note Can be used for preselection of volumes
0093 /// before a KDTree search. This way the memory
0094 /// consumption can be reduced, compromising the
0095 /// execution speed
0096 ///
0097 /// @note Careful with axis conventions as
0098 /// Geant4 uses a different one than Acts
0099 struct PositionSelector : public IGeant4PhysicalVolumeSelector {
0100   std::map<unsigned int, std::tuple<double, double>> m_ranges;
0101 
0102   /// Constructor with arguments
0103   /// @param ranges the provided map of axes of ranges
0104   PositionSelector(
0105       const std::map<unsigned int, std::tuple<double, double>>& ranges)
0106       : m_ranges(ranges) {}
0107 
0108   /// Secect function for the volume
0109   /// @param g4PhysVol the volume that is checked
0110   /// @return a boolean indicating the selection
0111   bool select(const G4VPhysicalVolume& g4PhysVol) const final {
0112     bool matched = false;
0113     G4ThreeVector pos = g4PhysVol.GetTranslation();
0114     for (auto range : m_ranges) {
0115       auto& [min, max] = range.second;
0116       EAxis axis = static_cast<EAxis>(range.first);
0117       matched = (pos[axis] >= min) && (pos[axis] <= max);
0118       if (!matched) {
0119         break;
0120       }
0121     }
0122     return matched;
0123   }
0124 };
0125 
0126 }  // namespace Geant4PhysicalVolumeSelectors
0127 }  // namespace Acts