File indexing completed on 2025-08-06 08:09:56
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Acts/EventData/VectorMultiTrajectory.hpp"
0010 #include "Acts/EventData/VectorTrackContainer.hpp"
0011
0012 template <typename fitter_t>
0013 template <typename source_link_t, typename start_parameters_t,
0014 typename fit_options_t>
0015 Acts::Result<ActsAlignment::detail::TrackAlignmentState>
0016 ActsAlignment::Alignment<fitter_t>::evaluateTrackAlignmentState(
0017 const Acts::GeometryContext& gctx,
0018 const std::vector<source_link_t>& sourcelinks,
0019 const start_parameters_t& sParameters, const fit_options_t& fitOptions,
0020 const std::unordered_map<const Acts::Surface*, std::size_t>&
0021 idxedAlignSurfaces,
0022 const ActsAlignment::AlignmentMask& alignMask) const {
0023 Acts::TrackContainer tracks{Acts::VectorTrackContainer{},
0024 Acts::VectorMultiTrajectory{}};
0025
0026
0027 Acts::SourceLinkAdapterIterator begin{sourcelinks.begin()};
0028 Acts::SourceLinkAdapterIterator end{sourcelinks.end()};
0029
0030
0031 auto fitRes = m_fitter.fit(begin, end, sParameters, fitOptions, tracks);
0032
0033 if (!fitRes.ok()) {
0034 ACTS_WARNING("Fit failure");
0035 return fitRes.error();
0036 }
0037
0038 const auto& track = fitRes.value();
0039
0040 const auto& globalTrackParamsCov =
0041 Acts::detail::globalTrackParametersCovariance(
0042 tracks.trackStateContainer(), track.tipIndex());
0043
0044 const auto alignState = detail::trackAlignmentState(
0045 gctx, tracks.trackStateContainer(), track.tipIndex(),
0046 globalTrackParamsCov, idxedAlignSurfaces, alignMask);
0047 if (alignState.alignmentDof == 0) {
0048 ACTS_VERBOSE("No alignment dof on track!");
0049 return AlignmentError::NoAlignmentDofOnTrack;
0050 }
0051 return alignState;
0052 }
0053
0054 template <typename fitter_t>
0055 template <typename trajectory_container_t,
0056 typename start_parameters_container_t, typename fit_options_t>
0057 void ActsAlignment::Alignment<fitter_t>::calculateAlignmentParameters(
0058 const trajectory_container_t& trajectoryCollection,
0059 const start_parameters_container_t& startParametersCollection,
0060 const fit_options_t& fitOptions,
0061 ActsAlignment::AlignmentResult& alignResult,
0062 const ActsAlignment::AlignmentMask& alignMask) const {
0063
0064
0065 assert(trajectoryCollection.size() == startParametersCollection.size());
0066
0067
0068 alignResult.alignmentDof =
0069 alignResult.idxedAlignSurfaces.size() * Acts::eAlignmentSize;
0070
0071 Acts::ActsDynamicVector sumChi2Derivative =
0072 Acts::ActsDynamicVector::Zero(alignResult.alignmentDof);
0073 Acts::ActsDynamicMatrix sumChi2SecondDerivative =
0074 Acts::ActsDynamicMatrix::Zero(alignResult.alignmentDof,
0075 alignResult.alignmentDof);
0076
0077 fit_options_t fitOptionsWithRefSurface = fitOptions;
0078
0079
0080 alignResult.chi2 = 0;
0081 alignResult.measurementDim = 0;
0082 alignResult.numTracks = trajectoryCollection.size();
0083 double sumChi2ONdf = 0;
0084 for (unsigned int iTraj = 0; iTraj < trajectoryCollection.size(); iTraj++) {
0085 const auto& sourcelinks = trajectoryCollection.at(iTraj);
0086 const auto& sParameters = startParametersCollection.at(iTraj);
0087
0088 fitOptionsWithRefSurface.referenceSurface = &sParameters.referenceSurface();
0089
0090 auto evaluateRes = evaluateTrackAlignmentState(
0091 fitOptions.geoContext, sourcelinks, sParameters,
0092 fitOptionsWithRefSurface, alignResult.idxedAlignSurfaces, alignMask);
0093 if (!evaluateRes.ok()) {
0094 ACTS_DEBUG("Evaluation of alignment state for track " << iTraj
0095 << " failed");
0096 continue;
0097 }
0098 const auto& alignState = evaluateRes.value();
0099 for (const auto& [rowSurface, rows] : alignState.alignedSurfaces) {
0100 const auto& [dstRow, srcRow] = rows;
0101
0102 sumChi2Derivative.segment<Acts::eAlignmentSize>(dstRow *
0103 Acts::eAlignmentSize) +=
0104 alignState.alignmentToChi2Derivative.segment(
0105 srcRow * Acts::eAlignmentSize, Acts::eAlignmentSize);
0106
0107 for (const auto& [colSurface, cols] : alignState.alignedSurfaces) {
0108 const auto& [dstCol, srcCol] = cols;
0109 sumChi2SecondDerivative
0110 .block<Acts::eAlignmentSize, Acts::eAlignmentSize>(
0111 dstRow * Acts::eAlignmentSize, dstCol * Acts::eAlignmentSize) +=
0112 alignState.alignmentToChi2SecondDerivative.block(
0113 srcRow * Acts::eAlignmentSize, srcCol * Acts::eAlignmentSize,
0114 Acts::eAlignmentSize, Acts::eAlignmentSize);
0115 }
0116 }
0117 alignResult.chi2 += alignState.chi2;
0118 alignResult.measurementDim += alignState.measurementDim;
0119 sumChi2ONdf += alignState.chi2 / alignState.measurementDim;
0120 }
0121 alignResult.averageChi2ONdf = sumChi2ONdf / alignResult.numTracks;
0122
0123
0124
0125
0126 std::size_t alignDof = alignResult.alignmentDof;
0127 Acts::ActsDynamicMatrix sumChi2SecondDerivativeInverse =
0128 Acts::ActsDynamicMatrix::Zero(alignDof, alignDof);
0129 sumChi2SecondDerivativeInverse = sumChi2SecondDerivative.inverse();
0130 if (sumChi2SecondDerivativeInverse.hasNaN()) {
0131 ACTS_DEBUG("Chi2 second derivative inverse has NaN");
0132
0133 }
0134
0135
0136 alignResult.deltaAlignmentParameters =
0137 Acts::ActsDynamicVector::Zero(alignDof);
0138 alignResult.alignmentCovariance =
0139 Acts::ActsDynamicMatrix::Zero(alignDof, alignDof);
0140
0141 alignResult.deltaAlignmentParameters =
0142 -sumChi2SecondDerivative.fullPivLu().solve(sumChi2Derivative);
0143 ACTS_VERBOSE("sumChi2SecondDerivative = \n" << sumChi2SecondDerivative);
0144 ACTS_VERBOSE("sumChi2Derivative = \n" << sumChi2Derivative);
0145 ACTS_VERBOSE("alignResult.deltaAlignmentParameters \n");
0146
0147
0148 alignResult.alignmentCovariance = 2 * sumChi2SecondDerivativeInverse;
0149
0150 alignResult.deltaChi2 = 0.5 * sumChi2Derivative.transpose() *
0151 alignResult.deltaAlignmentParameters;
0152 }
0153
0154 template <typename fitter_t>
0155 Acts::Result<void>
0156 ActsAlignment::Alignment<fitter_t>::updateAlignmentParameters(
0157 const Acts::GeometryContext& gctx,
0158 const std::vector<Acts::DetectorElementBase*>& alignedDetElements,
0159 const ActsAlignment::AlignedTransformUpdater& alignedTransformUpdater,
0160 ActsAlignment::AlignmentResult& alignResult) const {
0161
0162 Acts::AlignmentVector deltaAlignmentParam = Acts::AlignmentVector::Zero();
0163 for (const auto& [surface, index] : alignResult.idxedAlignSurfaces) {
0164
0165 const Acts::Vector3& oldCenter = surface->center(gctx);
0166 const Acts::Transform3& oldTransform = surface->transform(gctx);
0167 const Acts::RotationMatrix3& oldRotation = oldTransform.rotation();
0168
0169 const Acts::Vector3& oldEulerAngles = oldRotation.eulerAngles(2, 1, 0);
0170
0171
0172 deltaAlignmentParam = alignResult.deltaAlignmentParameters.segment(
0173 Acts::eAlignmentSize * index, Acts::eAlignmentSize);
0174
0175 Acts::Vector3 deltaCenter =
0176 deltaAlignmentParam.segment<3>(Acts::eAlignmentCenter0);
0177
0178 Acts::Vector3 deltaEulerAngles =
0179 deltaAlignmentParam.segment<3>(Acts::eAlignmentRotation0);
0180
0181
0182 const Acts::Vector3 newCenter = oldCenter + deltaCenter;
0183
0184 Acts::AngleAxis3 rotZ(oldEulerAngles(0) + deltaEulerAngles(2),
0185 Acts::Vector3::UnitZ());
0186
0187 Acts::AngleAxis3 rotY(oldEulerAngles(1) + deltaEulerAngles(1),
0188 Acts::Vector3::UnitY());
0189
0190 Acts::AngleAxis3 rotX(oldEulerAngles(2) + deltaEulerAngles(0),
0191 Acts::Vector3::UnitX());
0192 Eigen::Quaternion<Acts::ActsScalar> newRotation = rotZ * rotY * rotX;
0193 const Acts::Transform3 newTransform =
0194 Acts::Translation3(newCenter) * newRotation;
0195
0196
0197
0198
0199 ACTS_VERBOSE("Delta of alignment parameters at element "
0200 << index << "= \n"
0201 << deltaAlignmentParam);
0202 bool updated = alignedTransformUpdater(alignedDetElements.at(index), gctx,
0203 newTransform);
0204 if (!updated) {
0205 ACTS_ERROR("Update alignment parameters for detector element failed");
0206 return AlignmentError::AlignmentParametersUpdateFailure;
0207 }
0208 }
0209
0210 return Acts::Result<void>::success();
0211 }
0212
0213 template <typename fitter_t>
0214 template <typename trajectory_container_t,
0215 typename start_parameters_container_t, typename fit_options_t>
0216 Acts::Result<ActsAlignment::AlignmentResult>
0217 ActsAlignment::Alignment<fitter_t>::align(
0218 const trajectory_container_t& trajectoryCollection,
0219 const start_parameters_container_t& startParametersCollection,
0220 const ActsAlignment::AlignmentOptions<fit_options_t>& alignOptions) const {
0221
0222 AlignmentResult alignResult;
0223
0224
0225 for (unsigned int iDetElement = 0;
0226 iDetElement < alignOptions.alignedDetElements.size(); iDetElement++) {
0227 alignResult.idxedAlignSurfaces.emplace(
0228 &alignOptions.alignedDetElements.at(iDetElement)->surface(),
0229 iDetElement);
0230 }
0231 ACTS_VERBOSE("There are " << alignResult.idxedAlignSurfaces.size()
0232 << " detector elements to be aligned");
0233
0234
0235 bool converged = false;
0236 bool alignmentParametersUpdated = false;
0237 std::queue<double> recentChi2ONdf;
0238 ACTS_INFO("Max number of iterations: " << alignOptions.maxIterations);
0239 for (unsigned int iIter = 0; iIter < alignOptions.maxIterations; iIter++) {
0240
0241
0242 AlignmentMask alignMask = AlignmentMask::All;
0243
0244 auto iter_it = alignOptions.iterationState.find(iIter);
0245 if (iter_it != alignOptions.iterationState.end()) {
0246 alignMask = iter_it->second;
0247 }
0248
0249 calculateAlignmentParameters(
0250 trajectoryCollection, startParametersCollection,
0251 alignOptions.fitOptions, alignResult, alignMask);
0252
0253 ACTS_INFO("iIter = " << iIter << ", total chi2 = " << alignResult.chi2
0254 << ", total measurementDim = "
0255 << alignResult.measurementDim
0256 << " and average chi2/ndf = "
0257 << alignResult.averageChi2ONdf);
0258
0259
0260
0261 if (recentChi2ONdf.size() >=
0262 alignOptions.deltaAverageChi2ONdfCutOff.first) {
0263 if (std::abs(recentChi2ONdf.front() - alignResult.averageChi2ONdf) <=
0264 alignOptions.deltaAverageChi2ONdfCutOff.second) {
0265 ACTS_INFO(
0266 "Alignment has converged with change of chi2/ndf < "
0267 << alignOptions.deltaAverageChi2ONdfCutOff.second << " in the last "
0268 << alignOptions.deltaAverageChi2ONdfCutOff.first << " iterations"
0269 << " after " << iIter << " iteration(s)");
0270 converged = true;
0271 break;
0272 }
0273 recentChi2ONdf.pop();
0274 }
0275
0276 if (alignResult.averageChi2ONdf <= alignOptions.averageChi2ONdfCutOff) {
0277 ACTS_INFO("Alignment has converged with average chi2/ndf < "
0278 << alignOptions.averageChi2ONdfCutOff << " after " << iIter
0279 << " iteration(s)");
0280 converged = true;
0281 break;
0282 }
0283
0284
0285 recentChi2ONdf.push(alignResult.averageChi2ONdf);
0286
0287 ACTS_INFO("The solved delta of alignmentParameters = \n "
0288 << alignResult.deltaAlignmentParameters);
0289
0290 auto updateRes = updateAlignmentParameters(
0291 alignOptions.fitOptions.geoContext, alignOptions.alignedDetElements,
0292 alignOptions.alignedTransformUpdater, alignResult);
0293 if (!updateRes.ok()) {
0294 ACTS_ERROR("Update alignment parameters failed: " << updateRes.error());
0295 return updateRes.error();
0296 }
0297 alignmentParametersUpdated = true;
0298 }
0299
0300
0301 if (!converged) {
0302 ACTS_ERROR("Alignment is not converged.");
0303 alignResult.result = AlignmentError::ConvergeFailure;
0304 }
0305
0306
0307
0308 if (alignmentParametersUpdated) {
0309 for (const auto& det : alignOptions.alignedDetElements) {
0310 const auto& surface = &det->surface();
0311 const auto& transform =
0312 det->transform(alignOptions.fitOptions.geoContext);
0313
0314 alignResult.alignedParameters.emplace(det, transform);
0315 const auto& translation = transform.translation();
0316 const auto& rotation = transform.rotation();
0317 const Acts::Vector3 rotAngles = rotation.eulerAngles(2, 1, 0);
0318 ACTS_VERBOSE("Detector element with surface "
0319 << surface->geometryId()
0320 << " has aligned geometry position as below:");
0321 ACTS_VERBOSE("Center (cenX, cenY, cenZ) = " << translation.transpose());
0322 ACTS_VERBOSE(
0323 "Euler angles (rotZ, rotY, rotX) = " << rotAngles.transpose());
0324 ACTS_VERBOSE("Rotation matrix = \n" << rotation);
0325 }
0326 } else {
0327 ACTS_DEBUG("Alignment parameters is not updated.");
0328 }
0329
0330 return alignResult;
0331 }