554 {
555
556 std::multimap<int, int>
557 yIdxQuadMap;
558 std::multimap<int, int> xIdxQuadMap;
559
560 std::multimap<int, ldmx::TrigScintTrack> yQuadMap;
561 std::multimap<int, ldmx::TrigScintTrack> xQuadMap;
562
563
564 std::map<ldmx::TrigScintTrack, int> yTrackMap;
565 std::map<ldmx::TrigScintTrack, int> xTrackMap;
566
567 uint trkIdx = -1;
568 for (auto trk : tracks) {
569 trkIdx++;
570
571 if (trk.getCentroidX() == -1) {
572 if (verbose_)
573 ldmx_log(debug) << " -- In matchXYTracks found y track at "
574 << trk.getCentroidY() << "; mapping to quad "
575 << (int)trk.getCentroidY() / 8 << " with trk index "
576 << trkIdx;
577
578
579 yQuadMap.insert(std::make_pair((int)(trk.getCentroidY() / 8), trk));
580 yTrackMap[trk] = trkIdx;
581 yIdxQuadMap.insert(std::make_pair((int)(trk.getCentroidY() / 8), trkIdx));
582
583 } else {
584
585 xQuadMap.insert(std::make_pair((int)(trk.getCentroidY() / 8), trk));
586 xTrackMap[trk] = trkIdx;
587 xIdxQuadMap.insert(std::make_pair((int)(trk.getCentroidY() / 8), trkIdx));
588 if (verbose_)
589 ldmx_log(debug) << " -- In matchXYTracks found x track at (x,y) = ("
590 << trk.getCentroidX() << ", " << trk.getCentroidY()
591 << "); mapping to quad " << (int)trk.getCentroidY() / 8
592 << " with trk index " << trkIdx;
593 }
594 }
595
596
597
598
599
600
601
602
603
604 float x0 = 0;
605 float sx0 = fabs(xStart_);
606
607 float sy0 = fabs(yStart_) /
608 4.;
609
610
611
612 for (auto yitr = yQuadMap.begin(); yitr != yQuadMap.end(); ++yitr) {
613 int nYinQuad = yQuadMap.count((*yitr).first);
614 int nXinQuad = xQuadMap.count((*yitr).first);
615 float y{-9999.}, sy{-9999.}, x{-9999.}, x1{-9999.}, x2{-9999.}, sx1{-9999.},
616 sx2{-9999.}, y1{-9999.}, y2{-9999.}, sy1{-9999.}, sy2{-9999.};
617
618 float y0 = (*yitr).first * 8 + sy0;
619 float sx =
620 1. / 2 * xConvFactor_;
621
622
623
624
625 if (nXinQuad == 0) {
626
627 x = x0;
628 sx = sx0;
629 if (verbose_)
630 ldmx_log(debug) << "\t\t\t no x info in quad " << (*yitr).first
631 << "; will set x to middle of pad, pad half-width as "
632 "precision: set (x, sx)=("
633 << x << ", " << sx << ")";
634 }
635 else if (nXinQuad ==
636 1) {
637
638
639
640 auto xitr = xQuadMap.find((*yitr).first);
641 x = ((*xitr).second).getCentroidX() * xConvFactor_ + xStart_;
642
643 if (verbose_)
644 ldmx_log(debug) << "\t\t\t 1 x in quad " << (*yitr).first
645 << ", getting (x, sx)=(" << x << ", " << sx << ")";
646 }
647 else if (nXinQuad == 2) {
648
649
650
651
652 auto xitr1 = xQuadMap.lower_bound((*yitr).first);
653 auto xitr2 = xQuadMap.upper_bound((*yitr).first);
654 xitr2--;
655
656 if (xitr1 != xitr2) {
657 x1 = ((*xitr1).second).getCentroidX() * xConvFactor_ + xStart_;
658 x2 = ((*xitr2).second).getCentroidX() * xConvFactor_ + xStart_;
659 sx1 = xConvFactor_ / 2.;
660 sx2 = sx1;
661 x = (x1 + x2) / 2.;
662 sx = fabs(x1 - x2) / 2 *
663 xConvFactor_;
664 if (verbose_)
665 ldmx_log(debug) << "\t\t -- 2 x in quad: setting y track x "
666 "coordinate to midpoint";
667 }
668 }
669
670
671
672 if (nYinQuad == 1) {
673
674 y = ((*yitr).second).getCentroidY() * yConvFactor_ + yStart_;
675 sy = ((*yitr).second).getResidual() * yConvFactor_;
676 if (sy == 0)
677 sy = 1. / 2 * yConvFactor_;
678
679
680 if (nXinQuad <= 1) {
681
682
683
684 auto xidx = xIdxQuadMap.find((*yitr).first);
685 auto yidx = yIdxQuadMap.find((*yitr).first);
686 tracks.at((*xidx).second).setPosition(x, y);
687 tracks.at((*xidx).second).setSigmaXY(sx, sy);
688 tracks.at((*yidx).second).setPosition(x, y);
689 tracks.at((*yidx).second).setSigmaXY(sx, sy);
690 if (verbose_)
691 ldmx_log(debug) << "\t\t\t in quad " << (*yitr).first
692 << ", set (x, y) = (" << x << ", " << y
693 << ") and (sx, sy) = " << sx << ", " << sy << ")";
694 continue;
695 }
696 }
697
698 if (verbose_)
699 ldmx_log(debug) << "\t\t in quad " << (*yitr).first
700 << ", not single x,y tracks: " << nXinQuad << " of x and "
701 << nYinQuad << " of y";
702
703 if (nYinQuad == 2) {
704
705
706 auto yitr1 = yQuadMap.lower_bound((*yitr).first);
707 auto yitr2 = yQuadMap.upper_bound((*yitr).first);
708 yitr2--;
709 y1 = ((*yitr1).second).getCentroidY() * yConvFactor_ + yStart_;
710 y2 = ((*yitr2).second).getCentroidY() * yConvFactor_ + yStart_;
711 sy1 = ((*yitr1).second).getResidual() * yConvFactor_;
712 sy2 = ((*yitr2).second).getResidual() * yConvFactor_;
713 y = (y1 + y2) / 2.;
714 sy = fabs(y1 - y2) / 2 * yConvFactor_;
715 if (verbose_)
716 ldmx_log(debug)
717 << "\t\t -- 2 y in quad: setting x track y coordinate to midpoint";
718 }
719
720 if (nYinQuad == 1 &&
721 nXinQuad == 2) {
722
723
724
725
726 auto yidx = yIdxQuadMap.find((*yitr).first);
727 tracks.at((*yidx).second).setPosition(x, y);
728 tracks.at((*yidx).second).setSigmaXY(sx, sy);
729
730 int minOverlapPE_ = 250;
731 if (((*yitr).second).getPE() < minOverlapPE_) {
732
733
734
735 y = y0;
736 sy = sy0;
737 if (verbose_)
738 ldmx_log(debug) << "\t\t -- Can't tell which x track should be "
739 "matched to single y track. Setting both x track "
740 "coordinates to y quadrant value:";
741 }
742 else if (verbose_)
743 ldmx_log(debug) << "\t\t -- Found large PE count ("
744 << ((*yitr).second).getPE() << " > " << minOverlapPE_
745 << "), suggesting overlap! Setting both x track "
746 "coordinates to y track value:";
747
748
749
750
751
752 if (verbose_)
753 ldmx_log(debug) << "\t\t -- (x1, x2, y) = (" << x1 << ", " << x2
754 << ", " << y << ") and (sx1, sx2, sy) = " << sx1 << ", "
755 << sx2 << ", " << sy << ")";
756
757
758 auto xidx1 = xIdxQuadMap.lower_bound((*yitr).first);
759 auto xidx2 = xIdxQuadMap.upper_bound((*yitr).first);
760 xidx2--;
761 tracks.at((*xidx1).second).setPosition(x1, y);
762 tracks.at((*xidx1).second).setSigmaXY(sx1, sy);
763 tracks.at((*xidx2).second).setPosition(x2, y);
764 tracks.at((*xidx2).second).setSigmaXY(sx2, sy);
765
766 }
767 else if (nYinQuad == 2 && nXinQuad == 1) {
768
769
770
771
772 auto xidx = xIdxQuadMap.find((*yitr).first);
773 tracks.at((*xidx).second).setPosition(x, y);
774 tracks.at((*xidx).second).setSigmaXY(sx, sy);
775
776 auto xitr = xQuadMap.lower_bound((*yitr).first);
777 int minOverlapPE_ = 300;
778 if (((*xitr).second).getPE() < minOverlapPE_) {
779 if (verbose_)
780 ldmx_log(debug)
781 << "\t\t just 1 x track with not-unusual PE in the quad -- can't "
782 "match; setting mid-point values for x ";
783 x = x0;
784 sx = sx0;
785 }
786 else {
787
788
789
790
791 if (verbose_)
792 ldmx_log(debug) << "\t\t -- Found large PE count ("
793 << ((*xitr).second).getPE() << " > " << minOverlapPE_
794 << ") in x track, suggesting overlap! Setting both y "
795 "track coordinates to x track value:";
796 }
797 if (verbose_)
798 ldmx_log(debug) << "\t\t -- (x, y1, y2) = (" << x << ", " << y1 << ", "
799 << y2 << ") and (sx, sy1, sy2) = " << sx << ", " << sy1
800 << ", " << sy2 << ")";
801
802 auto yidx1 = yIdxQuadMap.lower_bound((*yitr).first);
803 auto yidx2 = yIdxQuadMap.upper_bound((*yitr).first);
804 yidx2--;
805 tracks.at((*yidx1).second).setPosition(x, y1);
806 tracks.at((*yidx1).second).setSigmaXY(sx, sy1);
807 tracks.at((*yidx2).second).setPosition(x, y2);
808 tracks.at((*yidx2).second).setSigmaXY(sx, sy2);
809
810 }
811 else if (nYinQuad == 2 && nXinQuad == 2) {
812
813 auto xidx1 = xIdxQuadMap.lower_bound((*yitr).first);
814 auto xidx2 = xIdxQuadMap.upper_bound((*yitr).first);
815 xidx2--;
816 auto yidx1 = yIdxQuadMap.lower_bound((*yitr).first);
817 auto yidx2 = yIdxQuadMap.upper_bound((*yitr).first);
818 yidx2--;
819
820 if (yIdxQuadMap.find((*yitr).first) == yIdxQuadMap.end())
821 ldmx_log(error) << "The two y tracks in the same quadrant at "
822 << (*yitr).first
823 << " appear to not be found in the y track map! "
824 "investigate. Note that yidx1.first = "
825 << (*yidx1).first
826 << " and yidx2.first = " << (*yidx2).first;
827 else {
828 tracks.at((*xidx1).second).setPosition(x1, y);
829 tracks.at((*xidx1).second).setSigmaXY(sx1, sy);
830 tracks.at((*xidx2).second).setPosition(x2, y);
831 tracks.at((*xidx2).second).setSigmaXY(sx2, sy);
832
833 tracks.at((*yidx1).second).setPosition(x, y1);
834 tracks.at((*yidx1).second).setSigmaXY(sx, sy1);
835 tracks.at((*yidx2).second).setPosition(x, y2);
836 tracks.at((*yidx2).second).setSigmaXY(sx, sy2);
837
838 if (verbose_)
839 ldmx_log(debug) << "\t\t -- in a 2 x 2 situaiton; midpoint y: " << y
840 << " for both x tracks, midpoint x: " << x
841 << " for both y tracks";
842 }
843 }
844
845 if (nXinQuad > 2) {
846 if (verbose_)
847 ldmx_log(debug) << "\t\t -*-*-*- more than 2 x tracks in the same quad "
848 "-- nothing done about the x,y coordinates in this "
849 "situation -- implement if needed!!";
850 }
851 if (nYinQuad > 2) {
852 if (verbose_)
853 ldmx_log(debug) << "\t\t -*-*-*- more than 2 y tracks in the same quad "
854 "-- nothing done about the x,y coordinates in this "
855 "situation -- implement if needed!!";
856 }
857
858 }
859
860 yQuadMap.clear();
861 xQuadMap.clear();
862
863
864}