Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:20:48

0001 #ifndef WEIGHTED_TRACK_H
0002 #define WEIGHTED_TRACK_H
0003 
0004 #include <phool/PHObject.h>
0005 #include <trackbase/TrkrDefs.h>
0006 #include <Eigen/Dense>
0007 
0008 #ifdef __clang__
0009     #pragma GCC diagnostic push
0010     #pragma GCC diagnostic ignored "-Wundefined-internal"
0011     #include <Eigen/Geometry>
0012     #pragma GCC diagnostic pop
0013 #else
0014     #include <Eigen/Geometry>
0015 #endif
0016 
0017 #include <Math/Minimizer.h>
0018 
0019 #include <iostream>
0020 #include <utility>
0021 #include <vector>
0022 
0023 class ClusterFitPoint : public PHObject {
0024 public:
0025     ClusterFitPoint() = default;
0026     virtual ~ClusterFitPoint() = default;
0027 
0028     PHObject* CloneMe() const override { return new ClusterFitPoint(*this); }
0029 
0030     /// The key of the associated cluster
0031     TrkrDefs::cluskey cluster_key{};
0032     /// The position of the cluster in global coordinates
0033     Eigen::Vector3d cluster_position = Eigen::Vector3d::Zero();
0034     /// The size of the cluster along the local coordinate axes
0035     Eigen::Vector2d cluster_errors = Eigen::Vector2d::Ones();
0036 
0037     /// The augmented affine transform matrix that acts on vectors in the sensor coordinates to convert them to global coordinates
0038     Eigen::Affine3d sensor_local_to_global_transform = Eigen::Affine3d::Identity();
0039 
0040     /// Returns the residuals w.r.t. a given intersection point (in global coordinates)
0041     Eigen::Vector2d get_residuals (Eigen::Vector3d const& /*intersection*/) const;
0042     Eigen::Vector2d get_weighted_residuals (Eigen::Vector3d const& /*intersection*/) const;
0043 
0044 private:
0045     ClassDefOverride(ClusterFitPoint, 1)
0046 };
0047 
0048 class WeightedTrack : public PHObject, private std::vector<ClusterFitPoint> {
0049 public:
0050     virtual ~WeightedTrack() = default;
0051 
0052     // Force derived classes to re-implement this method, since the class is pure virtual
0053     virtual PHObject* CloneMe() const override = 0;
0054 
0055     /// Sets member parameters (of derived classes which actually implement the track) using an array of double
0056     /// Unfortunately, helper members of derived classes must be declared mutable so this can be declared const
0057     /// All the following methods assume the parameters have been set using this method
0058     virtual void set_parameters (double const* /*params*/) = 0;
0059     /// Mutates the members of the given array such that calling set_parameters would result in the same track parameterization (inverse of set_parameters)
0060     virtual void get_parameters (double* /*params*/) const = 0;
0061     /// Returns the number of parameters needed for the fit, e.g. the minimum length of the array passed to set_parameters, get_parameters
0062     virtual std::size_t get_n_parameters () const = 0;
0063     /// Modifies the minimizer so it is ready to begin the fit optimization, including setting initial parameters and ranges
0064     virtual void configure_minimizer (ROOT::Math::Minimizer& /*minimizer*/) = 0;
0065 
0066     /// Returns the partial derivative of the track position w.r.t. the parameter given by index evaluated at the given path length
0067     /// Used as a helper to get_residual_derivatives, which accounts for the implicit dependence of the intersection on the path length
0068     virtual Eigen::Vector3d get_partial_derivative (int /*index*/, double /*path_length*/) const = 0;
0069 
0070     /// Evaluates the track position at the given path length
0071     virtual Eigen::Vector3d get_position_at_path_length (double /*path_length*/) const = 0;
0072     /// Evaluates the track slope at the given path length
0073     virtual Eigen::Vector3d get_slope_at_path_length (double /*path_length*/) const = 0;
0074 
0075     /// Finds the path length that gives the intersection with the plane described by the given transform
0076     /// For use with the sensor_transform member of ClusterFitPoint to find track states
0077     virtual double get_path_length_of_intersection (Eigen::Affine3d const& /*plane*/) const = 0;
0078     /// Finds the path length that gives the point of closest approach to the given point
0079     virtual double get_path_length_of_pca (Eigen::Vector3d const& /*point*/) const = 0;
0080 
0081 
0082     /// Returns the 3D position (in global coordinates) of the intersection of this track, with its current parameters, and the plane defined by the given Affine3d
0083     /// For use with the sensor_transform member of ClusterFitPoint to find track states
0084     Eigen::Vector3d get_intersection (Eigen::Affine3d const& plane) const { return get_position_at_path_length(get_path_length_of_intersection(plane)); }
0085     /// Returns the 3D position (in global coordinates) of the point on the track, with its current parameters, closest to the given point
0086     Eigen::Vector3d get_pca (Eigen::Vector3d const& point) const { return get_position_at_path_length(get_path_length_of_pca(point)); }
0087 
0088     /// Returns the sum of the weighted errors, for use as a functor for a minimizer instance
0089     double get_squared_error (double const*);
0090     /// Returns the projection vectors associated with a state as a matrix
0091     /// See Eqn. 31 of "Global $\Chi^{2}$ approach to the Alignment of the ATLAS Silicon Tracking Detectors"
0092     Eigen::Matrix<double, 2, 3> get_projection (Eigen::Affine3d const& /*plane*/) const;
0093     /// Returns the derivatives of the residuals w.r.t. the parameter given by index for the track intersection with the given plane
0094     Eigen::Vector2d get_residual_derivative (int /*index*/, Eigen::Affine3d const& /*plane*/) const;
0095 
0096     /// Sets whether to use the member vertex for the fit
0097     void use_vertex (bool use = true) { m_use_vertex = use; }
0098     /// Returns the vertex position
0099     Eigen::Vector3d get_vertex_position () { return m_vertex_position; }
0100     /// Returns the vertex covariance
0101     Eigen::Matrix3d get_vertex_covariance () { return m_vertex_covariance; }
0102     /// Sets the vertex position
0103     void get_vertex_position (Eigen::Vector3d&& vertex) { m_vertex_position = std::forward<Eigen::Vector3d>(vertex); }
0104     /// Sets the vertex covariance
0105     void set_vertex_covariance (Eigen::Matrix3d&& covariance) { m_vertex_covariance = std::forward<Eigen::Matrix3d>(covariance); }
0106     /// Component-wise access to the vertex position
0107     double& vertex_position (std::size_t i) { return m_vertex_position(i); }
0108     /// Component-wise access to the vertex covariance
0109     double& vertex_covariance (std::size_t i, std::size_t j) { return m_vertex_covariance(i, j); }
0110 
0111 
0112     // Types
0113     using std::vector<ClusterFitPoint>::value_type;
0114     using std::vector<ClusterFitPoint>::allocator_type;
0115     using std::vector<ClusterFitPoint>::size_type;
0116     using std::vector<ClusterFitPoint>::difference_type;
0117     using std::vector<ClusterFitPoint>::reference;
0118     using std::vector<ClusterFitPoint>::const_reference;
0119     using std::vector<ClusterFitPoint>::pointer;
0120     using std::vector<ClusterFitPoint>::const_pointer;
0121     using std::vector<ClusterFitPoint>::iterator;
0122     using std::vector<ClusterFitPoint>::const_iterator;
0123     using std::vector<ClusterFitPoint>::reverse_iterator;
0124     using std::vector<ClusterFitPoint>::const_reverse_iterator;
0125 
0126     // Element access
0127     using std::vector<ClusterFitPoint>::at;
0128     using std::vector<ClusterFitPoint>::operator[];
0129     using std::vector<ClusterFitPoint>::front;
0130     using std::vector<ClusterFitPoint>::back;
0131     using std::vector<ClusterFitPoint>::data;
0132 
0133     // Iterators
0134     using std::vector<ClusterFitPoint>::begin;
0135     using std::vector<ClusterFitPoint>::cbegin;
0136     using std::vector<ClusterFitPoint>::end;
0137     using std::vector<ClusterFitPoint>::cend;
0138     using std::vector<ClusterFitPoint>::rbegin;
0139     using std::vector<ClusterFitPoint>::crbegin;
0140     using std::vector<ClusterFitPoint>::rend;
0141     using std::vector<ClusterFitPoint>::crend;
0142 
0143     // Capacity
0144     using std::vector<ClusterFitPoint>::empty;
0145     using std::vector<ClusterFitPoint>::size;
0146     using std::vector<ClusterFitPoint>::max_size;
0147     using std::vector<ClusterFitPoint>::reserve;
0148     using std::vector<ClusterFitPoint>::capacity;
0149     using std::vector<ClusterFitPoint>::shrink_to_fit;
0150 
0151     // Modifiers
0152     using std::vector<ClusterFitPoint>::clear;
0153     using std::vector<ClusterFitPoint>::insert;
0154     // using std::vector<ClusterFitPoint>::insert_range; c++23
0155     using std::vector<ClusterFitPoint>::emplace;
0156     using std::vector<ClusterFitPoint>::erase;
0157     using std::vector<ClusterFitPoint>::push_back;
0158     using std::vector<ClusterFitPoint>::emplace_back;
0159     // using std::vector<ClusterFitPoint>::append_range; c++23
0160     using std::vector<ClusterFitPoint>::pop_back;
0161     using std::vector<ClusterFitPoint>::resize;
0162     using std::vector<ClusterFitPoint>::swap;
0163 
0164 protected:
0165     WeightedTrack() = default;
0166 
0167 private:
0168     bool m_use_vertex{false};
0169     Eigen::Vector3d m_vertex_position = Eigen::Vector3d::Zero();
0170     Eigen::Matrix3d m_vertex_covariance = Eigen::Matrix3d::Identity();
0171 
0172     ClassDefOverride(WeightedTrack, 1)
0173 };
0174 
0175 #endif//WEIGHTED_TRACK_H