Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:10:33

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