Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:16:52

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 "ActsExamples/GenericDetector/GenericDetectorElement.hpp"
0014 
0015 #include <mutex>
0016 #include <unordered_map>
0017 
0018 namespace ActsExamples {
0019 
0020 /// @class InternallyAlignedDetectorElement extends GenericDetectorElement
0021 ///
0022 /// This is a lightweight type of detector element,
0023 /// it simply implements the base class.
0024 ///
0025 /// The AlignedDetectorElement demonstrates how a GeometryContext
0026 /// can be used if it carries an interval of validity concept
0027 ///
0028 /// The nominal transform is only used to once create the alignment
0029 /// store and then in a contextual call the actual detector element
0030 /// position is taken internal multi component store - the latter
0031 /// has to be filled though from an external source
0032 class InternallyAlignedDetectorElement : public GenericDetectorElement {
0033  public:
0034   struct ContextType {
0035     /// The current interval of validity
0036     unsigned int iov = 0;
0037     bool nominal = false;
0038   };
0039 
0040   // Inherit constructor
0041   using GenericDetectorElement::GenericDetectorElement;
0042 
0043   /// Return local to global transform associated with this identifier
0044   ///
0045   /// @param gctx The current geometry context object, e.g. alignment
0046   ///
0047   /// @note this is called from the surface().transform(gctx)
0048   const Acts::Transform3& transform(
0049       const Acts::GeometryContext& gctx) const override;
0050 
0051   /// Return the nominal local to global transform
0052   ///
0053   /// @note the geometry context will hereby be ignored
0054   const Acts::Transform3& nominalTransform(
0055       const Acts::GeometryContext& gctx) const;
0056 
0057   /// Return local to global transform associated with this identifier
0058   ///
0059   /// @param alignedTransform is a new transform
0060   /// @param iov is the batch for which it is meant
0061   void addAlignedTransform(const Acts::Transform3& alignedTransform,
0062                            unsigned int iov);
0063 
0064   void clearAlignedTransform(unsigned int iov);
0065 
0066  private:
0067   std::unordered_map<unsigned int, Acts::Transform3> m_alignedTransforms;
0068   mutable std::mutex m_alignmentMutex;
0069 };
0070 
0071 inline const Acts::Transform3& InternallyAlignedDetectorElement::transform(
0072     const Acts::GeometryContext& gctx) const {
0073   if (!gctx.hasValue()) {
0074     // Return the standard transform if geo context is empty
0075     return nominalTransform(gctx);
0076   }
0077   const auto& alignContext = gctx.get<ContextType&>();
0078 
0079   std::lock_guard lock{m_alignmentMutex};
0080   if (alignContext.nominal) {
0081     // nominal alignment
0082     return nominalTransform(gctx);
0083   }
0084   auto aTransform = m_alignedTransforms.find(alignContext.iov);
0085   if (aTransform == m_alignedTransforms.end()) {
0086     throw std::runtime_error{
0087         "Aligned transform for IOV " + std::to_string(alignContext.iov) +
0088         " not found. This can happen if the garbage collection runs too "
0089         "early (--align-flushsize too low)"};
0090   }
0091   return aTransform->second;
0092 }
0093 
0094 inline const Acts::Transform3&
0095 InternallyAlignedDetectorElement::nominalTransform(
0096     const Acts::GeometryContext& gctx) const {
0097   return GenericDetectorElement::transform(gctx);
0098 }
0099 
0100 inline void InternallyAlignedDetectorElement::addAlignedTransform(
0101     const Acts::Transform3& alignedTransform, unsigned int iov) {
0102   std::lock_guard lock{m_alignmentMutex};
0103   m_alignedTransforms[iov] = alignedTransform;
0104 }
0105 
0106 inline void InternallyAlignedDetectorElement::clearAlignedTransform(
0107     unsigned int iov) {
0108   std::lock_guard lock{m_alignmentMutex};
0109   if (auto it = m_alignedTransforms.find(iov);
0110       it != m_alignedTransforms.end()) {
0111     m_alignedTransforms.erase(it);
0112   }
0113 }
0114 
0115 }  // namespace ActsExamples