Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:34

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