Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-07 07:46:18

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/Geometry/GeometryHierarchyMap.hpp"
0012 #include "Acts/TrackFitting/KalmanFitter.hpp"
0013 #include "ActsAlignment/Kernel/Alignment.hpp"
0014 #include "ActsExamples/EventData/Measurement.hpp"
0015 #include "ActsExamples/EventData/ProtoTrack.hpp"
0016 #include "ActsExamples/EventData/Track.hpp"
0017 #include "ActsExamples/Framework/DataHandle.hpp"
0018 #include "ActsExamples/Framework/IAlgorithm.hpp"
0019 
0020 #include <memory>
0021 #include <vector>
0022 
0023 namespace Acts {
0024 class MagneticFieldProvider;
0025 }
0026 
0027 namespace ActsExamples {
0028 
0029 class AlignmentGroup {
0030  public:
0031   AlignmentGroup(const std::string& name,
0032                  const std::vector<Acts::GeometryIdentifier>& geoIds)
0033       : m_name(name), m_map(constructHierarchyMap(geoIds)) {}
0034 
0035   // Access the name of the group
0036   std::string getNameOfGroup() const { return m_name; }
0037 
0038   // Useful for testing
0039   bool has(Acts::GeometryIdentifier geoId) {
0040     auto it = m_map.find(geoId);
0041     return (it == m_map.end()) ? false : *it;
0042   }
0043 
0044  private:
0045   std::string m_name;  //  storing the name in the class
0046   Acts::GeometryHierarchyMap<bool> m_map;
0047 
0048   Acts::GeometryHierarchyMap<bool> constructHierarchyMap(
0049       const std::vector<Acts::GeometryIdentifier>& geoIds) {
0050     std::vector<Acts::GeometryHierarchyMap<bool>::InputElement> ies;
0051     for (const auto& geoId : geoIds) {
0052       ies.emplace_back(geoId, true);
0053     }
0054     return Acts::GeometryHierarchyMap<bool>(ies);
0055   }
0056 };
0057 
0058 class AlignmentAlgorithm final : public IAlgorithm {
0059  public:
0060   using AlignmentResult = Acts::Result<ActsAlignment::AlignmentResult>;
0061   using AlignmentParameters =
0062       std::unordered_map<Acts::SurfacePlacementBase*, Acts::Transform3>;
0063   /// Alignment function that takes sets of input measurements, initial
0064   /// trackstate and alignment options and returns some alignment-specific
0065   /// result.
0066   using TrackFitterOptions =
0067       Acts::KalmanFitterOptions<Acts::VectorMultiTrajectory>;
0068 
0069   /// Alignment function that takes the above parameters and runs alignment
0070   /// @note This is separated into a virtual interface to keep compilation units
0071   /// small
0072   class AlignmentFunction {
0073    public:
0074     virtual ~AlignmentFunction() = default;
0075     virtual AlignmentResult operator()(
0076         const std::vector<std::vector<IndexSourceLink>>&,
0077         const TrackParametersContainer&,
0078         const ActsAlignment::AlignmentOptions<TrackFitterOptions>&) const = 0;
0079   };
0080 
0081   /// Create the alignment function implementation.
0082   ///
0083   /// The magnetic field is intentionally given by-value since the variant
0084   /// contains shared_ptr anyway.
0085   static std::shared_ptr<AlignmentFunction> makeAlignmentFunction(
0086       std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
0087       std::shared_ptr<const Acts::MagneticFieldProvider> magneticField);
0088 
0089   struct Config {
0090     /// Input measurements collection.
0091     std::string inputMeasurements;
0092     /// Input proto tracks collection, i.e. groups of hit indices.
0093     std::string inputProtoTracks;
0094     /// Input initial track parameter estimates for for each proto track.
0095     std::string inputInitialTrackParameters;
0096     /// Output aligned parameters collection.
0097     std::string outputAlignmentParameters;
0098     /// Type erased fitter function.
0099     std::shared_ptr<AlignmentFunction> align;
0100     /// The aligned transform updater
0101     ActsAlignment::AlignedTransformUpdater alignedTransformUpdater;
0102     /// The surfaces (with detector elements) to be aligned
0103     std::vector<Acts::SurfacePlacementBase*> alignedDetElements;
0104     /// The alignment mask at each iteration
0105     std::map<unsigned int, std::bitset<6>> iterationState;
0106     /// Cutoff value for average chi2/ndf
0107     double chi2ONdfCutOff = 0.10;
0108     /// Cutoff value for delta of average chi2/ndf within a couple of iterations
0109     std::pair<std::size_t, double> deltaChi2ONdfCutOff = {10, 0.00001};
0110     /// Maximum number of iterations
0111     std::size_t maxNumIterations = 100;
0112     /// Number of tracks to be used for alignment
0113     int maxNumTracks = -1;
0114     std::vector<AlignmentGroup> m_groups;
0115   };
0116 
0117   /// Constructor of the alignment algorithm
0118   ///
0119   /// @param cfg is the config struct to configure the algorithm
0120   /// @param level is the logging level
0121   explicit AlignmentAlgorithm(
0122       Config cfg, std::unique_ptr<const Acts::Logger> logger = nullptr);
0123 
0124   /// Framework execute method of the alignment algorithm
0125   ///
0126   /// @param ctx is the algorithm context that holds event-wise information
0127   /// @return a process code to steer the algorithm flow
0128   ProcessCode execute(const AlgorithmContext& ctx) const override;
0129 
0130  private:
0131   Config m_cfg;
0132 
0133   ReadDataHandle<MeasurementContainer> m_inputMeasurements{this,
0134                                                            "InputMeasurements"};
0135   ReadDataHandle<TrackParametersContainer> m_inputInitialTrackParameters{
0136       this, "InputInitialTrackParameters"};
0137   ReadDataHandle<ProtoTrackContainer> m_inputProtoTracks{this,
0138                                                          "InputProtoTracks"};
0139   WriteDataHandle<AlignmentParameters> m_outputAlignmentParameters{
0140       this, "OutputAlignmentParameters"};
0141 };
0142 
0143 }  // namespace ActsExamples