ATLAS Offline Software
Loading...
Searching...
No Matches
SegmentLineFitter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Compile this file assuming that FP operations may trap.
6// Prevents spurious FPEs in the clang build.
9
12
16
17#include <ActsInterop/Logger.h>
20
21
22#include <format>
23
28
29
30namespace MuonR4::SegmentFit{
31 using namespace Acts;
32 using namespace Acts::UnitLiterals;
33
37
38 namespace {
39
40 constexpr double calcRedChi2(const Result_t& result) {
41 return result.nDoF > 0ul ? result.chi2 / result.nDoF : result.chi2;
42 }
45 inline unsigned countPrecHits(const HitVec_t& hits) {
46 return std::ranges::count_if(hits, [](const Hit_t& hit){
47 return isPrecisionHit(*hit);
48 });
49 }
51 inline unsigned countPhiHits(const HitVec_t& hits) {
52 return std::ranges::count_if(hits, [](const Hit_t& hit){
53 return isGoodHit(*hit) && hit->measuresPhi();
54 });
55 }
59 inline void removeBeamSpot(HitVec_t& hits){
60 hits.erase(std::remove_if(hits.begin(), hits.end(),
61 [](const Hit_t& a){
62 return a->type() == xAOD::UncalibMeasType::Other;
63 }), hits.end());
64 }
65 }
66 SegmentLineFitter::Config::RangeArray
68 RangeArray rng{};
69 constexpr double spatRang = 10._m;
70 constexpr double timeTange = 50._ns;
71 using enum ParamDefs;
72 rng[toUnderlying(y0)] = std::array{-spatRang, spatRang};
73 rng[toUnderlying(x0)] = std::array{-spatRang, spatRang};
74 rng[toUnderlying(phi)] = std::array{-179._degree, 179._degree};
75 rng[toUnderlying(theta)] = std::array{0._degree, 175._degree};
76 rng[toUnderlying(t0)] = std::array{-timeTange, timeTange};
77 return rng;
78 }
79 SegmentLineFitter::SegmentLineFitter(const std::string& name, Config&& config):
80 AthMessaging{name},
81 m_fitter{config, makeActsAthenaLogger(this, name)},
82 m_cfg{config} {
83 m_goodHitSel.connect<isGoodHit>();
84 }
85 Result_t SegmentLineFitter::callLineFit(const Acts::CalibrationContext& cctx,
86 const Parameters& startPars,
87 const Amg::Transform3D& localToGlobal,
88 HitVec_t&& calibHits) const {
89
91 bool appendsBS = m_cfg.doBeamSpot && countPhiHits(calibHits) > 0;
92
93
94 Result_t result{};
95 //check the degrees of freedom before try the fit
96 if (const std::size_t nPars = m_fitter.config().parsToUse.size(); nPars > 0ul) {
97 auto dOF = m_fitter.countDoF(calibHits, m_goodHitSel);
98 if (dOF.bending + dOF.nonBending < nPars) {
99 return result;
100 }
101 // check that there are at least two crossing stereo measurements
102 if (dOF.nonBending == 0ul && nPars == 4ul){
103 bool foundU{false}, foundV{false};
104 for (const HitVec_t::value_type& hit : calibHits) {
105 if (hit->type() != xAOD::UncalibMeasType::MMClusterType || !isGoodHit(*hit)) {
106 continue;
107 }
108 const auto* mmClust = dynamic_cast<const xAOD::MMCluster*>(hit->spacePoint()->primaryMeasurement());
109 assert(mmClust != nullptr);
110 const auto& design = mmClust->readoutElement()->stripLayer(mmClust->layerHash()).design();
111 if (!design.hasStereoAngle()) {
112 continue;
113 }
114 if (design.stereoAngle() > 0.) {
115 foundU = true;
116 } else {
117 foundV = true;
118 }
119 if (foundU && foundV) {
120 break;
121 }
122 }
123 if (!foundU || !foundV) {
124 result.measurements = std::move(calibHits);
125 result.parameters = startPars;
126 return result;
127 }
128 if (m_cfg.doBeamSpot) {
129 appendsBS = true;
130 }
131 }
132 }
133 if (appendsBS) {
134 const Amg::Transform3D globToLoc{localToGlobal.inverse()};
135 Amg::Vector3D beamSpot{globToLoc.translation()};
136 Amg::Vector3D beamLine{globToLoc.linear().col(2)};
137 SpacePoint::Cov_t covariance{};
138 covariance[toUnderlying(AxisDefs::etaCov)] = square(m_cfg.beamSpotRadius);
139 covariance[toUnderlying(AxisDefs::phiCov)] = square(m_cfg.beamSpotLength);
141 auto beamSpotSP = std::make_unique<CalibratedSpacePoint>(nullptr, std::move(beamSpot));
142 beamSpotSP->setBeamDirection(std::move(beamLine));
143 beamSpotSP->setCovariance(std::move(covariance));
144 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Beam spot constraint "
145 <<Amg::toString(beamSpotSP->localPosition())<<", "<<beamSpotSP->covariance());
146 calibHits.emplace_back(std::move(beamSpotSP));
147 }
148 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__ <<": Start segment fit with parameters "
149 <<toString(startPars) <<", plane location: "<<Amg::toString(localToGlobal)
150 <<std::endl<<print(calibHits));
151
152 FitOpts_t fitOpts{};
153 fitOpts.calibContext = cctx;
154 fitOpts.calibrator = m_cfg.calibrator;
155 fitOpts.selector = m_goodHitSel;
156
157 fitOpts.measurements = std::move(calibHits);
158 fitOpts.localToGlobal = localToGlobal;
159 fitOpts.startParameters = startPars;
161 constexpr auto t0idx = toUnderlying(ParamDefs::t0);
162 fitOpts.startParameters[t0idx] = ActsTrk::timeToActs(fitOpts.startParameters[t0idx]);
164 result = m_fitter.fit(std::move(fitOpts));
166 if (m_fitter.config().fitT0) {
167 result.parameters[t0idx] = ActsTrk::timeToAthena(result.parameters[t0idx]);
168 result.covariance(t0idx, t0idx) = Acts::square(ActsTrk::timeToAthena(1.)) * result.covariance(t0idx, t0idx);
169 for (ParamDefs p : {ParamDefs::x0, ParamDefs::y0, ParamDefs::phi, ParamDefs::theta}) {
170 auto pidx = toUnderlying(p);
171 result.covariance(t0idx, pidx) = ActsTrk::timeToAthena(result.covariance(t0idx, pidx));
172 result.covariance(pidx, t0idx) = ActsTrk::timeToAthena(result.covariance(pidx, t0idx));
173 }
174 }
176 {
177 const auto[segPos, segDir] = makeLine(result.parameters);
178 for (Hit_t& hit : result.measurements) {
179 hit->setChi2Term(SeedingAux::chi2Term(segPos, segDir, *hit));
180 }
181 }
182 return result;
183 }
184 std::unique_ptr<Segment>
185 SegmentLineFitter::fitSegment(const EventContext& ctx,
186 const SegmentSeed* parent,
187 const Parameters& startPars,
188 const Amg::Transform3D& localToGlobal,
189 HitVec_t&& calibHits) const {
190
191 const Acts::CalibrationContext cctx = ActsTrk::getCalibrationContext(ctx);
192 if (m_cfg.visionTool) {
193 Result_t preFit{};
194 preFit.parameters = startPars;
195 preFit.measurements = calibHits;
196 auto seedCopy = convertToSegment(localToGlobal, parent, std::move(preFit));
197 m_cfg.visionTool->visualizeSegment(ctx, *seedCopy, "Prefit");
198 }
199 Result_t segFit = callLineFit(cctx, startPars, localToGlobal, std::move(calibHits));
200 if (m_cfg.visionTool && segFit.converged) {
201 auto seedCopy = convertToSegment(localToGlobal, parent, Result_t{segFit});
202 m_cfg.visionTool->visualizeSegment(ctx, *seedCopy, "Intermediate fit");
203 }
204 if (!removeOutliers(cctx, *parent, localToGlobal,
205 segFit.converged? segFit.parameters : startPars,
206 segFit)) {
207 return nullptr;
208 }
209 if (!plugHoles(cctx, *parent, localToGlobal, segFit)) {
210 return nullptr;
211 }
212 auto finalSeg = convertToSegment(localToGlobal, parent, std::move(segFit));
213 if (m_cfg.visionTool) {
214 m_cfg.visionTool->visualizeSegment(ctx, *finalSeg, "Final fit");
215 }
216 return finalSeg;
217 }
218 std::unique_ptr<Segment>
220 const SegmentSeed* patternSeed,
221 Result_t&& data) const {
222 const auto [locPos, locDir] = makeLine(data.parameters);
223 Amg::Vector3D globPos = locToGlob * locPos;
224 Amg::Vector3D globDir = locToGlob.linear()* locDir;
225
226 std::ranges::sort(data.measurements, [](const Hit_t& a, const Hit_t& b){
227 return a->localPosition().z() < b->localPosition().z();
228 });
229 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__ <<": Create new segment "
230 <<toString(data.parameters)<<" in "<<patternSeed->msSector()->identString()
231 <<"built from:\n"<<print(data.measurements));
232
233 auto finalSeg = std::make_unique<Segment>(std::move(globPos), std::move(globDir),
234 patternSeed, std::move(data.measurements),
235 data.chi2, data.nDoF);
236 finalSeg->setCallsToConverge(data.nIter);
237 finalSeg->setParUncertainties(std::move(data.covariance));
238 if (m_fitter.config().fitT0) {
239 finalSeg->setSegmentT0(data.parameters[toUnderlying(ParamDefs::t0)]);
240 }
241 return finalSeg;
242 }
243
244 bool SegmentLineFitter::removeOutliers(const Acts::CalibrationContext& cctx,
245 const SegmentSeed& seed,
246 const Amg::Transform3D& localToGlobal,
247 const LinePar_t& startPars,
248 Result_t& fitResult) const {
249
250 if (!checkPrecHitCount(fitResult.measurements) ||
251 fitResult.nIter > m_fitter.config().maxIter) {
252 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__
253 <<": No degree of freedom available. What shall be removed?!. nDoF: "
254 <<fitResult.nDoF<<", n-meas: "<<countPrecHits(fitResult.measurements)
255 <<std::endl<<print(fitResult.measurements));
256 return false;
257 }
258 if (fitResult.converged && calcRedChi2(fitResult) < m_cfg.outlierRemovalCut) {
259 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__ <<": The segment "<<toString(fitResult.parameters)
260 <<" is already of good quality "<< calcRedChi2(fitResult)<<". Don't remove outliers");
261 return true;
262 }
263 if (fitResult.nDoF == 0u){
264 return false;
265 }
266 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__ <<": Segment "
267 <<toString(fitResult.parameters)<<", nIter: "<<fitResult.nIter
268 <<" is of badish quality. "<<print(fitResult.measurements)
269 <<std::endl<<"Remove worst hit");
270
273 if (m_cfg.doBeamSpot) {
274 removeBeamSpot(fitResult.measurements);
275 }
276
278 std::ranges::sort(fitResult.measurements,
279 [](const HitVec_t::value_type& a, const HitVec_t::value_type& b){
281 if (isGoodHit(*a) != isGoodHit(*b)) {
282 return !isGoodHit(*a);
283 }
284 return a->chi2Term() < b->chi2Term();
285 });
286 fitResult.measurements.back()->setFitState(HitState::Outlier);
287 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<" Mark "<<(*fitResult.measurements.back())<<" as outlier");
288
290 if (!checkPrecHitCount(fitResult.measurements)) {
291 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__
292 <<": No degree of freedom available after outlier removal. n-meas: "
293 <<countPrecHits(fitResult.measurements)<<std::endl<<print(fitResult.measurements));
294 return false;
295 }
296
298 Result_t newAttempt = callLineFit(cctx, startPars, localToGlobal,
299 std::move(fitResult.measurements));
300 if (newAttempt.converged) {
301 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<" The outlier removal converged.");
302 newAttempt.nIter+=fitResult.nIter;
303 fitResult = std::move(newAttempt);
304 if (m_cfg.visionTool) {
305 const EventContext& ctx{*cctx.get<const EventContext*>()};
306 auto seedCopy = convertToSegment(localToGlobal, &seed, Result_t{fitResult});
307 m_cfg.visionTool->visualizeSegment(ctx, *seedCopy, "Bad fit recovery");
308 }
309 } else {
310 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__
311 <<" Outlier removal fit did not converge. Needed iterations: "<<newAttempt.nIter);
312 if (newAttempt.nIter == 0ul) {
313 return false;
314 }
315 fitResult.nIter+=newAttempt.nIter;
316 fitResult.measurements = std::move(newAttempt.measurements);
317 }
318 return removeOutliers(cctx, seed, localToGlobal,
319 fitResult.converged ? fitResult.parameters : startPars,
320 fitResult);
321 }
322
324 auto [segPos, segDir] = makeLine(candidate.parameters);
325 cleanStripLayers(candidate.measurements);
326 candidate.measurements.erase(std::remove_if(candidate.measurements.begin(),
327 candidate.measurements.end(),
328 [&](const HitVec_t::value_type& hit){
329 if (hit->fitState() == HitState::Valid) {
330 return false;
331 } else if (hit->fitState() == HitState::Duplicate) {
332 return true;
333 }
336 const double dist = Amg::lineDistance(segPos, segDir,
337 hit->localPosition(),
338 hit->sensorDirection());
339 const auto* dc = static_cast<const xAOD::MdtDriftCircle*>(hit->spacePoint()->primaryMeasurement());
340 return dist >= dc->readoutElement()->innerTubeRadius();
341 }
342 return false;
343 }), candidate.measurements.end());
344 }
346 const SpacePointPerLayerSorter sorter{};
348 std::ranges::sort(hits, [&](const Hit_t&a ,const Hit_t& b){
349 if (a->isStraw() || b->isStraw()) {
350 return !a->isStraw();
351 }
352 if (a->type() == xAOD::UncalibMeasType::Other ||
353 b->type() == xAOD::UncalibMeasType::Other) {
354 return a->type() != xAOD::UncalibMeasType::Other;
355 }
356 const unsigned lay_a = sorter.sectorLayerNum(*a->spacePoint());
357 const unsigned lay_b = sorter.sectorLayerNum(*b->spacePoint());
358 if (lay_a != lay_b) {
359 return lay_a < lay_b;
360 }
361 const double chi2a = a->chi2Term();
362 const double chi2b = b->chi2Term();
363 /* Do not accept pad hits even though they've smaller chi2
364 * than the neighbouring strip */
366 const auto* sTgcA = static_cast<const xAOD::sTgcMeasurement*>(a->spacePoint()->primaryMeasurement());
367 const auto* sTgcB = static_cast<const xAOD::sTgcMeasurement*>(b->spacePoint()->primaryMeasurement());
368 if (sTgcA->channelType() == xAOD::sTgcMeasurement::sTgcChannelTypes::Pad &&
369 sTgcB->channelType() == xAOD::sTgcMeasurement::sTgcChannelTypes::Strip) {
370 return chi2b > m_cfg.recoveryPull;
371 } else if (sTgcB->channelType() == xAOD::sTgcMeasurement::sTgcChannelTypes::Pad &&
372 sTgcA->channelType() == xAOD::sTgcMeasurement::sTgcChannelTypes::Strip) {
373 return chi2a < m_cfg.recoveryPull;
374 }
375 }
376 return chi2a < chi2b;
377 });
378
379 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__ <<": Check for duplicate strip hits");
381 for (HitVec_t::iterator itr = hits.begin(); itr != hits.end(); ++itr) {
382 const Hit_t& hit_a{*itr};
383 if (hit_a->isStraw()){
384 break;
385 }
386 if(hit_a->fitState() == HitState::Duplicate ||
387 hit_a->type() == xAOD::UncalibMeasType::Other) {
388 continue;
389 }
390 const unsigned lay_a = sorter.sectorLayerNum(*hit_a->spacePoint());
392 for (HitVec_t::iterator itr2 = itr + 1; itr2 != hits.end(); ++itr2) {
393 const Hit_t& hit_b{*itr2};
394 if (hit_b->type() == xAOD::UncalibMeasType::Other ||
395 hit_b->fitState() == HitState::Duplicate) {
396 continue;
397 }
398 if (lay_a != sorter.sectorLayerNum(*hit_b->spacePoint())) {
399 break;
400 }
402 if ((hit_a->measuresEta() && hit_b->measuresEta()) ||
403 (hit_a->measuresPhi() && hit_b->measuresPhi())) {
404 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__ <<": Duplicate hit on same layer"<<std::endl
405 <<" -- reject: "<<(*hit_b)<<std::endl
406 <<" -- accept: "<<(*hit_a));
407 hit_b->setFitState(HitState::Duplicate);
408 }
409 }
410 }
411 }
412
413 inline bool SegmentLineFitter::betterResult(const Result_t& newResult, const Result_t& oldResult) const {
414 if (!newResult.converged) {
415 ATH_MSG_VERBOSE(__func__<<"() "<<__LINE__<<" The new result did not converge");
416 return false;
417 }
418 const double redChi2New = calcRedChi2(newResult);
419 const double redChi2Old = calcRedChi2(oldResult);
420 ATH_MSG_VERBOSE(__func__<<"() "<<__LINE__<<" Compare results -- old chi2: "<<redChi2Old<<", nDoF: "
421 <<oldResult.nDoF<<" vs. new chi2: "<<redChi2New<<", nDoF: "<<newResult.nDoF
422 <<" -- outlier removal: "<<m_cfg.outlierRemovalCut);
423 if (newResult.nDoF == oldResult.nDoF) {
424 return redChi2New < redChi2Old;
425 }
426 return (redChi2New < m_cfg.outlierRemovalCut && newResult.nDoF > oldResult.nDoF) ||
427 (redChi2New > m_cfg.outlierRemovalCut && redChi2New < redChi2Old);
428 }
429 bool SegmentLineFitter::plugHoles(const Acts::CalibrationContext& cctx,
430 const SegmentSeed& seed,
431 const Amg::Transform3D& localToGlobal,
432 Result_t& toRecover) const {
434 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__ <<": segment "<<toString(toRecover.parameters)
435 <<", chi2: "<< calcRedChi2(toRecover) <<", nDoF: "<<toRecover.nDoF);
437
438
439 std::unordered_set<const SpacePoint*> usedSpacePoints{};
440 for (auto& hit : toRecover.measurements) {
441 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__ <<": "<<(*hit)<<" is known");
442 usedSpacePoints.insert(hit->spacePoint());
443 }
445 const EventContext& ctx{*cctx.get<const EventContext*>()};
446
447 const double timeOff = toRecover.parameters[toUnderlying(ParamDefs::t0)];
448 HitVec_t candidateHits{};
449 std::size_t hasCandidate{0};
450 const auto [locPos, locDir] = makeLine(toRecover.parameters);
451
453 for (const auto& hit : *seed.parentBucket()){
455 if (usedSpacePoints.count(hit.get())){
456 continue;
457 }
458 Hit_t calibHit{};
459 double pull{-1.};
460 if (hit->isStraw()) {
461 using namespace Acts::detail::LineHelper;
462 const double dist = signedDistance(locPos, locDir, hit->localPosition(), hit->sensorDirection());
463 const auto* dc = static_cast<const xAOD::MdtDriftCircle*>(hit->primaryMeasurement());
464 // Check whether the tube is crossed by the hit
465 if (std::abs(dist) >= dc->readoutElement()->innerTubeRadius()) {
466 continue;
467 }
468 } else {
470 if (!hit->measuresEta() &&
471 std::abs(hit->sensorDirection().dot(hit->localPosition() -
472 SeedingAux::extrapolateToPlane(locPos,locDir, *hit))) >
473 1.1*std::sqrt(hit->covariance()[toUnderlying(AxisDefs::etaCov)])){
474 continue;
475 }
478 pull = std::sqrt(SeedingAux::chi2Term(locPos, locDir, *hit));
479 if (pull > 1.1 * m_cfg.recoveryPull) {
480 continue;
481 }
482 }
483 calibHit = m_cfg.calibrator->calibrate(ctx, hit.get(), locPos, locDir, ActsTrk::timeToActs(timeOff));
484 calibHit->setChi2Term(SeedingAux::chi2Term(locPos, locDir, *calibHit));
485 if (calibHit->chi2Term() <= Acts::square(m_cfg.recoveryPull)) {
486 hasCandidate += calibHit->fitState() == HitState::Valid;
487 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Candidate hit for recovery "
488 <<(*calibHit));
489 } else {
490 calibHit->setFitState(HitState::Outlier);
491 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Outlier hit "
492 <<(*calibHit)<<" -> limit: "<<m_cfg.recoveryPull);
493 }
494 candidateHits.push_back(std::move(calibHit));
495 }
497 if (!hasCandidate) {
498 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": No space point candidates for recovery were found");
499 toRecover.measurements.insert(toRecover.measurements.end(),
500 std::make_move_iterator(candidateHits.begin()),
501 std::make_move_iterator(candidateHits.end()));
502 eraseWrongHits(toRecover);
503 return toRecover.nDoF > 0;
504 }
505 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Found "<<hasCandidate<<" space points for recovery. ");
506
507
508 HitVec_t hitsForRecovery = toRecover.measurements;
510 if (m_cfg.doBeamSpot) {
511 removeBeamSpot(hitsForRecovery);
512 }
513
514 hitsForRecovery.insert(hitsForRecovery.end(),
515 candidateHits.begin(),
516 candidateHits.end());
517
518 cleanStripLayers(hitsForRecovery);
519
520 Result_t recovered = callLineFit(cctx, toRecover.parameters, localToGlobal,
521 std::move(hitsForRecovery));
522
525 if (betterResult(recovered, toRecover)) {
526 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Accept segment with recovered "
527 <<(recovered.nDoF - toRecover.nDoF)<<" extra nDoF.");
528 recovered.nIter += toRecover.nIter;
529 toRecover = std::move(recovered);
530
531 std::vector<const CalibratedSpacePoint*> stripOutliers{};
532 stripOutliers.reserve(toRecover.measurements.size());
535 unsigned recovLoop{(candidateHits.size() != hasCandidate)*m_cfg.nRecoveryLoops};
536 while (++recovLoop <= m_cfg.nRecoveryLoops) {
537 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Enter recovery loop "<<recovLoop<<".");
538 hitsForRecovery = toRecover.measurements;
539 // Remove the beamspot
540 if (m_cfg.doBeamSpot) {
541 removeBeamSpot(hitsForRecovery);
542 }
543 // Check whether an outlier can be lifted to on-track
544 for (HitVec_t::value_type& hit : hitsForRecovery) {
545 if (hit->fitState() != HitState::Outlier) {
546 continue;
547 }
548 if (hit->chi2Term() < Acts::square(m_cfg.recoveryPull)) {
549 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Try to recover outlier "<<(*hit));
550 hit->setFitState(HitState::Valid);
551 stripOutliers.push_back(hit.get());
552 }
553 }
554 // Nothing to recover
555 if (stripOutliers.empty()) {
556 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": No additional measurement found");
557 break;
558 }
559 // Ensure that only one hit per layer is fit
560 cleanStripLayers(hitsForRecovery);
561 // Recovery turned out to be duplicates on the same layer
562 if (std::ranges::none_of(stripOutliers,[](const CalibratedSpacePoint* sp){
563 return sp->fitState() == HitState::Valid;
564 })) {
565 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Outliers turned out to be duplicates.");
566 break;
567 }
568 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Start fit without the outliers.");
569 stripOutliers.clear();
570 recovered = callLineFit(cctx, toRecover.parameters, localToGlobal, std::move(hitsForRecovery));
571 if (!betterResult(recovered, toRecover)) {
572 break;
573 }
574 recovered.nIter += toRecover.nIter;
575 toRecover = std::move(recovered);
576 }
577 } else{
578 for (HitVec_t::value_type& hit : candidateHits) {
579 hit->setFitState(HitState::Outlier);
580 toRecover.measurements.push_back(std::move(hit));
581 }
582 }
583 eraseWrongHits(toRecover);
584 return true;
585 }
586 inline bool SegmentLineFitter::checkPrecHitCount(const HitVec_t& candidateHits) const {
587 using namespace Muon::MuonStationIndex;
588
589 const size_t nPrecHits = countPrecHits(candidateHits);
590 if (nPrecHits < m_cfg.nPrecHitCut) {
591 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": Not enough precision hits for segment fit. "
592 <<nPrecHits<<" < "<<m_cfg.nPrecHitCut);
593 return false;
594 }
595
596 const auto firstHit {std::ranges::find_if(candidateHits, [](const Hit_t& hit){
597 return hit->spacePoint() != nullptr;
598 })};
599 assert(firstHit != candidateHits.end());
600 if (toStationIndex((*firstHit)->spacePoint()->msSector()->chamberIndex()) == StIndex::EI &&
601 std::ranges::any_of(candidateHits, [](const Hit_t& hit){
602 return xAOD::isNSW(hit->type()); })) {
603
604 std::array<std::size_t, 3> nStrips{Acts::filledArray<std::size_t, 3>(0u)};
605 std::size_t nPhiHits {0u};
606 for (const Hit_t& hit : candidateHits) {
607 if (!isGoodHit(*hit)) {
608 continue;
609 }
610 if (hit->type() == xAOD::UncalibMeasType::sTgcStripType) {
611 nStrips[0] += isPrecisionHit(*hit);
612 nPhiHits += hit->measuresPhi();
613 continue;
614 }
615
616 const auto* mmClust =
617 dynamic_cast<const xAOD::MMCluster*>(hit->spacePoint()->primaryMeasurement());
618 assert(mmClust);
619 const auto& design =
620 mmClust->readoutElement()->stripLayer(mmClust->measurementHash()).design();
621 if (!design.hasStereoAngle()) {
622 ++nStrips[0];
623 } else if (design.stereoAngle() > 0.) {
624 ++nStrips[1];
625 } else {
626 ++nStrips[2];
627 }
628 }
631 const std::size_t nEtaOrientations =
632 std::ranges::count_if(nStrips, [](std::size_t n){ return n > 0; }) +
633 std::ranges::any_of( nStrips, [](std::size_t n){ return n > 1; });
634 ATH_MSG_VERBOSE(__func__<<"() - "<<__LINE__<<": nHits: "<<candidateHits.size()
635 <<", nPhiHits: "<<nPhiHits<<", nEtaOrientations: "<<nEtaOrientations
636 <<", N X-strips: "<<nStrips[0]<<", U-strips: "<<nStrips[1]<<", V-strips: "<<nStrips[2]);
637
638 if ( nEtaOrientations == 4u ||
639 (nEtaOrientations == 3u && nPhiHits >= 1u) ||
640 (nEtaOrientations == 2u && nPhiHits >= 2u)) {
641 return true;
642 }
643 return false;
644 }
645 return true;
646 }
647}
Scalar phi() const
phi method
Scalar theta() const
theta method
#define ATH_MSG_VERBOSE(x)
static Double_t sp
static Double_t a
static Double_t t0
if(pathvar)
std::unique_ptr< const Acts::Logger > makeActsAthenaLogger(IMessageSvc *svc, const std::string &name, int level, std::optional< std::string > parent_name)
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
std::string identString() const
Returns a string encoding the chamber index & the sector of the MS sector.
The calibrated Space point is created during the calibration process.
Segment::MeasType Hit_t
Abrivation of the space point type to use.
bool plugHoles(const Acts::CalibrationContext &cctx, const SegmentSeed &seed, const Amg::Transform3D &localToGlobal, Result_t &toRecover) const
Recovery of missed hits.
Selector_t m_goodHitSel
Selector to identify the valid hits.
Result_t callLineFit(const Acts::CalibrationContext &cctx, const Parameters &startPars, const Amg::Transform3D &localToGlobal, HitVec_t &&calibHits) const
Calls the underlying line fitter to determine the segment parameters.
ConfigSwitches m_cfg
Configuration switches of the ATLAS fitter implementation.
Fitter_t::FitResult< HitVec_t > Result_t
Abrivation of the fit result.
bool betterResult(const Result_t &newResult, const Result_t &oldResult) const
Returns whether the new fit result is better than the one from the previous iteration.
void eraseWrongHits(Result_t &candidate) const
Removes all hits from the segment which are obvious outliers.
void cleanStripLayers(HitVec_t &hits) const
Marks duplicate hits on a strip layer as outliers to avoid competing contributions from the same laye...
std::unique_ptr< Segment > convertToSegment(const Amg::Transform3D &locToGlobTrf, const SegmentSeed *parentSeed, Result_t &&toConvert) const
Converts the fit result into a segment object.
bool checkPrecHitCount(const HitVec_t &candidateHits) const
Checks if the candidate has enough precision hits to fit a segment.
Fitter_t::FitOptions< HitVec_t, ISpacePointCalibrator > FitOpts_t
Abrivation of the fit options.
std::unique_ptr< Segment > fitSegment(const EventContext &ctx, const SegmentSeed *parent, const LinePar_t &startPars, const Amg::Transform3D &localToGlobal, HitVec_t &&calibHits) const
Fit a set of measurements to a straight segment line.
Fitter_t m_fitter
Actual implementation of the straight line fit.
Fitter_t::ParamVec_t LinePar_t
Abrivation of the fitted line parameters.
std::vector< Hit_t > HitVec_t
Collection of space points.
SegmentLineFitter(const std::string &name, Config &&config)
Standard constructor.
bool removeOutliers(const Acts::CalibrationContext &cctx, const SegmentSeed &seed, const Amg::Transform3D &localToGlobal, const LinePar_t &startPars, Result_t &fitResult) const
Cleans the fitted segment from the most outlier hit and then attempts to refit the segment.
Representation of a segment seed (a fully processed hough maximum) produced by the hough transform.
Definition SegmentSeed.h:14
const MuonGMR4::SpectrometerSector * msSector() const
Returns the associated chamber.
The SpacePointPerLayerSorter sort two given space points by their layer Identifier.
std::array< double, 3 > Cov_t
Abrivation of the covariance type.
constexpr double timeToAthena(T actsT)
Converts a time unit from Acts to Athena units.
Acts::CalibrationContext getCalibrationContext(const EventContext &ctx)
The Acts::Calibration context is piped through the Acts fitters to (re)calibrate the Acts::SourceLink...
constexpr auto timeToActs(T athenaT)
Converts a time unit from Athena to Acts units.
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 3, 1 > Vector3D
SegmentLineFitter::Result_t Result_t
SegmentLineFitter::HitVec_t HitVec_t
SeedingAux::FitParIndex ParamDefs
Use the same parameter indices as used by the CompSpacePointAuxiliaries.
SegmentLineFitter::Hit_t Hit_t
std::pair< Amg::Vector3D, Amg::Vector3D > makeLine(const Parameters &pars)
Returns the parsed parameters into an Eigen line parametrization.
Acts::Experimental::CompositeSpacePointLineFitter::ParamVec_t Parameters
std::string toString(const Parameters &pars)
Dumps the parameters into a string with labels in front of each number.
bool isPrecisionHit(const SpacePoint &hit)
Returns whether the uncalibrated spacepoint is a precision hit (Mdt, micromegas, stgc strips).
std::string print(const cont_t &container)
Print a space point container to string.
bool isGoodHit(const CalibratedSpacePoint &hit)
Returns whether the calibrated spacepoint is valid and therefore suitable to be used in the segment f...
StIndex toStationIndex(ChIndex index)
convert ChIndex into StIndex
DataModel_detail::iterator< DVL > remove_if(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, Predicate pred)
Specialization of remove_if for DataVector/List.
MdtDriftCircle_v1 MdtDriftCircle
MMCluster_v1 MMCluster
sTgcMeasurement_v1 sTgcMeasurement
static RangeArray defaultRanges()
Function that returns a set of predefined ranges for testing.
Tell the compiler to optimize assuming that FP may trap.
#define CXXUTILS_TRAPPING_FP
Definition trapping_fp.h:24