31 using namespace Acts::UnitLiterals;
39 constexpr double calcRedChi2(
const Result_t& result) {
40 return result.nDoF > 0ul ? result.chi2 / result.nDoF : result.chi2;
44 inline unsigned countPrecHits(
const HitVec_t& hits) {
45 return std::ranges::count_if(hits, [](
const Hit_t& hit){
50 inline unsigned countPhiHits(
const HitVec_t& hits) {
51 return std::ranges::count_if(hits, [](
const Hit_t& hit){
52 return isGoodHit(*hit) && hit->measuresPhi();
58 inline void removeBeamSpot(
HitVec_t& hits){
61 return a->type() == xAOD::UncalibMeasType::Other;
65 SegmentLineFitter::Config::RangeArray
68 constexpr double spatRang = 10._m;
69 constexpr double timeTange = 50._ns;
71 rng[toUnderlying(y0)] = std::array{-spatRang, spatRang};
72 rng[toUnderlying(x0)] = std::array{-spatRang, spatRang};
73 rng[toUnderlying(
phi)] = std::array{-179._degree, 179._degree};
74 rng[toUnderlying(
theta)] = std::array{0._degree, 175._degree};
75 rng[toUnderlying(
t0)] = std::array{-timeTange, timeTange};
90 bool appendsBS =
m_cfg.doBeamSpot && countPhiHits(calibHits) > 0;
95 if (
const std::size_t nPars =
m_fitter.config().parsToUse.size(); nPars > 0ul) {
97 if (dOF.bending + dOF.nonBending < nPars) {
101 if (dOF.nonBending == 0ul && nPars == 4ul){
102 bool foundU{
false}, foundV{
false};
103 for (
const HitVec_t::value_type& hit : calibHits) {
107 const auto* mmClust =
dynamic_cast<const xAOD::MMCluster*
>(hit->spacePoint()->primaryMeasurement());
108 assert(mmClust !=
nullptr);
109 const auto& design = mmClust->readoutElement()->stripLayer(mmClust->layerHash()).design();
110 if (!design.hasStereoAngle()) {
113 if (design.stereoAngle() > 0.) {
118 if (foundU && foundV) {
122 if (!foundU || !foundV) {
123 result.measurements = std::move(calibHits);
124 result.parameters = startPars;
127 if (
m_cfg.doBeamSpot) {
137 covariance[toUnderlying(AxisDefs::etaCov)] = square(
m_cfg.beamSpotRadius);
138 covariance[toUnderlying(AxisDefs::phiCov)] = square(
m_cfg.beamSpotLength);
140 auto beamSpotSP = std::make_unique<CalibratedSpacePoint>(
nullptr, std::move(beamSpot));
141 beamSpotSP->setBeamDirection(std::move(beamLine));
142 beamSpotSP->setCovariance(std::move(covariance));
144 <<
Amg::toString(beamSpotSP->localPosition())<<
", "<<beamSpotSP->covariance());
145 calibHits.emplace_back(std::move(beamSpotSP));
147 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__ <<
": Start segment fit with parameters "
149 <<std::endl<<
print(calibHits));
152 fitOpts.calibContext = cctx;
153 fitOpts.calibrator =
m_cfg.calibrator;
156 fitOpts.measurements = std::move(calibHits);
157 fitOpts.localToGlobal = localToGlobal;
158 fitOpts.startParameters = startPars;
160 constexpr auto t0idx = toUnderlying(ParamDefs::t0);
163 result =
m_fitter.fit(std::move(fitOpts));
167 result.covariance(t0idx, t0idx) = Acts::square(
ActsTrk::timeToAthena(1.)) * result.covariance(t0idx, t0idx);
168 for (
ParamDefs p : {ParamDefs::x0, ParamDefs::y0, ParamDefs::phi, ParamDefs::theta}) {
169 auto pidx = toUnderlying(p);
176 const auto[segPos, segDir] =
makeLine(result.parameters);
177 for (
Hit_t& hit : result.measurements) {
178 hit->setChi2Term(SeedingAux::chi2Term(segPos, segDir, *hit));
183 std::unique_ptr<Segment>
191 if (
m_cfg.visionTool) {
193 preFit.parameters = startPars;
194 preFit.measurements = calibHits;
196 m_cfg.visionTool->visualizeSegment(ctx, *seedCopy,
"Prefit");
199 if (
m_cfg.visionTool && segFit.converged) {
201 m_cfg.visionTool->visualizeSegment(ctx, *seedCopy,
"Intermediate fit");
204 segFit.converged? segFit.parameters : startPars,
208 if (!
plugHoles(cctx, *parent, localToGlobal, segFit)) {
212 if (
m_cfg.visionTool) {
213 m_cfg.visionTool->visualizeSegment(ctx, *finalSeg,
"Final fit");
217 std::unique_ptr<Segment>
221 const auto [locPos, locDir] =
makeLine(
data.parameters);
225 std::ranges::sort(
data.measurements, [](
const Hit_t&
a,
const Hit_t& b){
226 return a->localPosition().z() < b->localPosition().z();
230 <<
"built from:\n"<<
print(
data.measurements));
232 auto finalSeg = std::make_unique<Segment>(std::move(globPos), std::move(globDir),
233 patternSeed, std::move(
data.measurements),
235 finalSeg->setCallsToConverge(
data.nIter);
236 finalSeg->setParUncertainties(std::move(
data.covariance));
238 finalSeg->setSegmentT0(
data.parameters[toUnderlying(ParamDefs::t0)]);
250 if (countPrecHits(fitResult.measurements) <
m_cfg.nPrecHitCut || fitResult.nDoF == 0
251 || fitResult.nIter >
m_fitter.config().maxIter) {
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));
258 if (fitResult.converged && calcRedChi2(fitResult) <
m_cfg.outlierRemovalCut) {
260 <<
" is already of good quality "<< calcRedChi2(fitResult)<<
". Don't remove outliers");
263 if (fitResult.nDoF == 0u){
267 <<
toString(fitResult.parameters)<<
", nIter: "<<fitResult.nIter
268 <<
" is of badish quality. "<<
print(fitResult.measurements)
269 <<std::endl<<
"Remove worst hit");
273 if (
m_cfg.doBeamSpot) {
274 removeBeamSpot(fitResult.measurements);
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);
284 return a->chi2Term() < b->chi2Term();
286 fitResult.measurements.back()->setFitState(HitState::Outlier);
287 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
" Mark "<<(*fitResult.measurements.back())<<
" as outlier");
290 Result_t newAttempt = callLineFit(cctx, startPars, localToGlobal,
291 std::move(fitResult.measurements));
292 if (newAttempt.converged) {
293 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
" The outlier removal converged.");
294 newAttempt.nIter+=fitResult.nIter;
295 fitResult = std::move(newAttempt);
296 if (m_cfg.visionTool) {
297 const EventContext& ctx{*cctx.get<
const EventContext*>()};
298 auto seedCopy = convertToSegment(localToGlobal, &seed,
Result_t{fitResult});
299 m_cfg.visionTool->visualizeSegment(ctx, *seedCopy,
"Bad fit recovery");
303 <<
" Outlier removal fit did not converge. Needed iterations: "<<newAttempt.nIter);
304 if (newAttempt.nIter == 0ul) {
307 fitResult.nIter+=newAttempt.nIter;
308 fitResult.measurements = std::move(newAttempt.measurements);
310 return removeOutliers(cctx, seed, localToGlobal,
311 fitResult.converged ? fitResult.parameters : startPars,
316 auto [segPos, segDir] =
makeLine(candidate.parameters);
318 candidate.measurements.erase(
std::remove_if(candidate.measurements.begin(),
319 candidate.measurements.end(),
320 [&](
const HitVec_t::value_type& hit){
321 if (hit->fitState() == HitState::Valid) {
323 }
else if (hit->fitState() == HitState::Duplicate) {
328 const double dist = Amg::lineDistance(segPos, segDir,
329 hit->localPosition(),
330 hit->sensorDirection());
331 const auto* dc = static_cast<const xAOD::MdtDriftCircle*>(hit->spacePoint()->primaryMeasurement());
332 return dist >= dc->readoutElement()->innerTubeRadius();
335 }), candidate.measurements.end());
340 std::ranges::sort(hits, [&](
const Hit_t&
a ,
const Hit_t& b){
341 if (
a->isStraw() || b->isStraw()) {
342 return !
a->isStraw();
348 const unsigned lay_a = sorter.sectorLayerNum(*
a->spacePoint());
349 const unsigned lay_b = sorter.sectorLayerNum(*b->spacePoint());
350 if (lay_a != lay_b) {
351 return lay_a < lay_b;
353 const double chi2a =
a->chi2Term();
354 const double chi2b = b->chi2Term();
359 const auto* sTgcB =
static_cast<const xAOD::sTgcMeasurement*
>(b->spacePoint()->primaryMeasurement());
360 if (sTgcA->channelType() == xAOD::sTgcMeasurement::sTgcChannelTypes::Pad &&
361 sTgcB->channelType() == xAOD::sTgcMeasurement::sTgcChannelTypes::Strip) {
362 return chi2b >
m_cfg.recoveryPull;
363 }
else if (sTgcB->channelType() == xAOD::sTgcMeasurement::sTgcChannelTypes::Pad &&
364 sTgcA->channelType() == xAOD::sTgcMeasurement::sTgcChannelTypes::Strip) {
365 return chi2a <
m_cfg.recoveryPull;
368 return chi2a < chi2b;
371 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__ <<
": Check for duplicate strip hits");
373 for (HitVec_t::iterator itr = hits.begin(); itr != hits.end(); ++itr) {
374 const Hit_t& hit_a{*itr};
375 if (hit_a->isStraw()){
378 if(hit_a->fitState() == HitState::Duplicate ||
382 const unsigned lay_a = sorter.sectorLayerNum(*hit_a->spacePoint());
384 for (HitVec_t::iterator itr2 = itr + 1; itr2 != hits.end(); ++itr2) {
385 const Hit_t& hit_b{*itr2};
387 hit_b->fitState() == HitState::Duplicate) {
390 if (lay_a != sorter.sectorLayerNum(*hit_b->spacePoint())) {
394 if ((hit_a->measuresEta() && hit_b->measuresEta()) ||
395 (hit_a->measuresPhi() && hit_b->measuresPhi())) {
396 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__ <<
": Duplicate hit on same layer"<<std::endl
397 <<
" -- reject: "<<(*hit_b)<<std::endl
398 <<
" -- accept: "<<(*hit_a));
399 hit_b->setFitState(HitState::Duplicate);
406 if (!newResult.converged) {
407 ATH_MSG_VERBOSE(__func__<<
"() "<<__LINE__<<
" The new result did not converge");
410 const double redChi2New = calcRedChi2(newResult);
411 const double redChi2Old = calcRedChi2(oldResult);
412 ATH_MSG_VERBOSE(__func__<<
"() "<<__LINE__<<
" Compare results -- old chi2: "<<redChi2Old<<
", nDoF: "
413 <<oldResult.nDoF<<
" vs. new chi2: "<<redChi2New<<
", nDoF: "<<newResult.nDoF
414 <<
" -- outlier removal: "<<
m_cfg.outlierRemovalCut);
415 if (newResult.nDoF == oldResult.nDoF) {
416 return redChi2New < redChi2Old;
418 return (redChi2New < m_cfg.outlierRemovalCut && newResult.nDoF > oldResult.nDoF) ||
419 (redChi2New >
m_cfg.outlierRemovalCut && redChi2New < redChi2Old);
427 <<
", chi2: "<< calcRedChi2(toRecover) <<
", nDoF: "<<toRecover.nDoF);
431 std::unordered_set<const SpacePoint*> usedSpacePoints{};
432 for (
auto& hit : toRecover.measurements) {
433 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__ <<
": "<<(*hit)<<
" is known");
434 usedSpacePoints.insert(hit->spacePoint());
437 const EventContext& ctx{*cctx.get<
const EventContext*>()};
439 const double timeOff = toRecover.parameters[toUnderlying(ParamDefs::t0)];
441 std::size_t hasCandidate{0};
442 const auto [locPos, locDir] =
makeLine(toRecover.parameters);
445 for (
const auto& hit : *seed.parentBucket()){
447 if (usedSpacePoints.count(hit.get())){
452 if (hit->isStraw()) {
453 using namespace Acts::detail::LineHelper;
454 const double dist = signedDistance(locPos, locDir, hit->localPosition(), hit->sensorDirection());
457 if (Acts::abs(dist) >= dc->readoutElement()->innerTubeRadius()) {
462 if (!hit->measuresEta() &&
463 std::abs(hit->sensorDirection().dot(hit->localPosition() -
464 SeedingAux::extrapolateToPlane(locPos,locDir, *hit))) >
465 1.1*std::sqrt(hit->covariance()[toUnderlying(AxisDefs::etaCov)])){
470 pull = std::sqrt(SeedingAux::chi2Term(locPos, locDir, *hit));
471 if (pull > 1.1 *
m_cfg.recoveryPull) {
476 calibHit->setChi2Term(SeedingAux::chi2Term(locPos, locDir, *calibHit));
477 if (calibHit->chi2Term() <= Acts::square(
m_cfg.recoveryPull)) {
478 hasCandidate += calibHit->fitState() == HitState::Valid;
479 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
": Candidate hit for recovery "
482 calibHit->setFitState(HitState::Outlier);
484 <<(*calibHit)<<
" -> limit: "<<
m_cfg.recoveryPull);
486 candidateHits.push_back(std::move(calibHit));
490 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
": No space point candidates for recovery were found");
491 toRecover.measurements.insert(toRecover.measurements.end(),
492 std::make_move_iterator(candidateHits.begin()),
493 std::make_move_iterator(candidateHits.end()));
495 return toRecover.nDoF > 0;
497 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
": Found "<<hasCandidate<<
" space points for recovery. ");
500 HitVec_t hitsForRecovery = toRecover.measurements;
502 if (
m_cfg.doBeamSpot) {
503 removeBeamSpot(hitsForRecovery);
506 hitsForRecovery.insert(hitsForRecovery.end(),
507 candidateHits.begin(),
508 candidateHits.end());
513 std::move(hitsForRecovery));
518 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
": Accept segment with recovered "
519 <<(recovered.nDoF - toRecover.nDoF)<<
" extra nDoF.");
520 recovered.nIter += toRecover.nIter;
521 toRecover = std::move(recovered);
523 std::vector<const CalibratedSpacePoint*> stripOutliers{};
524 stripOutliers.reserve(toRecover.measurements.size());
527 unsigned recovLoop{(candidateHits.size() != hasCandidate)*
m_cfg.nRecoveryLoops};
528 while (++recovLoop <=
m_cfg.nRecoveryLoops) {
529 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
": Enter recovery loop "<<recovLoop<<
".");
530 hitsForRecovery = toRecover.measurements;
532 if (
m_cfg.doBeamSpot) {
533 removeBeamSpot(hitsForRecovery);
536 for (HitVec_t::value_type& hit : hitsForRecovery) {
537 if (hit->fitState() != HitState::Outlier) {
540 if (hit->chi2Term() < Acts::square(
m_cfg.recoveryPull)) {
541 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
": Try to recover outlier "<<(*hit));
542 hit->setFitState(HitState::Valid);
543 stripOutliers.push_back(hit.get());
547 if (stripOutliers.empty()) {
548 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
": No additional measurement found");
555 return sp->fitState() == HitState::Valid;
557 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
": Outliers turned out to be duplicates.");
560 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
": Start fit without the outliers.");
561 stripOutliers.clear();
562 recovered =
callLineFit(cctx, toRecover.parameters, localToGlobal, std::move(hitsForRecovery));
566 recovered.nIter += toRecover.nIter;
567 toRecover = std::move(recovered);
570 for (HitVec_t::value_type& hit : candidateHits) {
571 hit->setFitState(HitState::Outlier);
572 toRecover.measurements.push_back(std::move(hit));
Scalar phi() const
phi method
Scalar theta() const
theta method
#define ATH_MSG_VERBOSE(x)
char data[hepevt_bytes_allocation_ATLAS]
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.
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.
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(const double 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 double timeToActs(const double athenaT)
Converts a time unit from Athena to Acts units.
This class is not to needed in AthSimulation.
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...
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
sTgcMeasurement_v1 sTgcMeasurement
Full configuration object.
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