@@ -465,12 +465,41 @@ void TrackPlotter::fillHistograms(const o2::globaltracking::RecoContainer& recoC
465465 }
466466 if (mSrc == GID::MFTMCH || mSrc == GID::MFTMCHMID) {
467467 auto tracksFwd = recoCont.getGlobalFwdTracks ();
468- for (auto & t : tracksFwd) {
469- MuonTrack mt (&t, recoCont, mFirstTForbit );
468+ std::map<int64_t , std::vector<std::pair<int64_t , double >>> matchingCandidates;
469+ // loop over the global forward tracks and collect the matching candidates
470+ for (size_t ti = 0 ; ti < tracksFwd.size (); ti++) {
471+ auto & t = tracksFwd[ti];
470472 // skip tracks without MID if full matching is requested
471- if (mSrc == GID::MFTMCHMID && !mt. hasMID () ) {
473+ if (mSrc == GID::MFTMCHMID && t. getMIDTrackID () < 0 ) {
472474 continue ;
473475 }
476+
477+ // associate the matching candidate to the corresponding MCH standalone track, and store the matching chi2
478+ int64_t mchTrackIndex = t.getMCHTrackID ();
479+ double matchingChi2 = t.getMFTMCHMatchingChi2 ();
480+ auto matchingCandidateIterator = matchingCandidates.find (mchTrackIndex);
481+ if (matchingCandidateIterator != matchingCandidates.end ()) {
482+ matchingCandidateIterator->second .push_back (std::make_pair (ti, matchingChi2));
483+ } else {
484+ matchingCandidates[mchTrackIndex].push_back (std::make_pair (ti, matchingChi2));
485+ }
486+ }
487+
488+ // loop over the matching candidate for each standalone MCH track, sort the candidates according to the matching chi2,
489+ // and for each MCH track pick the leading candidate for further processing
490+ for (auto & [mchTrackIndex, matchingCandidatesVector] : matchingCandidates) {
491+ if (matchingCandidates.empty ()) {
492+ continue ;
493+ }
494+
495+ // sort the vector of matching candidates in ascending order of the matching chi2
496+ std::sort (matchingCandidatesVector.begin (), matchingCandidatesVector.end (),
497+ [](const std::pair<int64_t , double >& t1, const std::pair<int64_t , double >& t2) -> bool {
498+ return (t1.second < t2.second );
499+ });
500+
501+ // store the leading candidate
502+ auto & t = tracksFwd[matchingCandidatesVector[0 ].first ];
474503 mMuonTracks .emplace_back (std::make_pair<MuonTrack, bool >({ &t, recoCont, mFirstTForbit }, true ));
475504 }
476505 }
0 commit comments