550 {
551
552 std::multimap<int, int>
553 y_idx_quad_map;
554 std::multimap<int, int> x_idx_quad_map;
555
556 std::multimap<int, ldmx::TrigScintTrack> y_quad_map;
557 std::multimap<int, ldmx::TrigScintTrack> x_quad_map;
558
559
560 std::map<ldmx::TrigScintTrack, int> y_track_map;
561 std::map<ldmx::TrigScintTrack, int> x_track_map;
562
563 uint trk_idx = -1;
564 for (auto trk : tracks) {
565 trk_idx++;
566
567 if (trk.getCentroidX() == -1) {
568 if (verbose_)
569 ldmx_log(debug) << " -- In matchXYTracks found y track at "
570 << trk.getCentroidY() << "; mapping to quad "
571 << (int)trk.getCentroidY() / 8 << " with trk index "
572 << trk_idx;
573
574
575 y_quad_map.insert(std::make_pair((int)(trk.getCentroidY() / 8), trk));
576 y_track_map[trk] = trk_idx;
577 y_idx_quad_map.insert(
578 std::make_pair((int)(trk.getCentroidY() / 8), trk_idx));
579
580 } else {
581
582 x_quad_map.insert(std::make_pair((int)(trk.getCentroidY() / 8), trk));
583 x_track_map[trk] = trk_idx;
584 x_idx_quad_map.insert(
585 std::make_pair((int)(trk.getCentroidY() / 8), trk_idx));
586 if (verbose_)
587 ldmx_log(debug) << " -- In matchXYTracks found x track at (x,y) = ("
588 << trk.getCentroidX() << ", " << trk.getCentroidY()
589 << "); mapping to quad " << (int)trk.getCentroidY() / 8
590 << " with trk index " << trk_idx;
591 }
592 }
593
594
595
596
597
598
599
600
601
602 float x0 = 0;
603
604
605 float sx0 = fabs(x_start_);
606
607
608 float sy0 = fabs(y_start_) / 4.;
609
610
611
612 for (auto yitr = y_quad_map.begin(); yitr != y_quad_map.end(); ++yitr) {
613 int n_yin_quad = y_quad_map.count((*yitr).first);
614 int n_xin_quad = x_quad_map.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 = 1. / 2 *
620 x_conv_factor_;
621
622
623
624
625 if (n_xin_quad == 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 (n_xin_quad ==
636 1) {
637
638
639
640 auto xitr = x_quad_map.find((*yitr).first);
641 x = ((*xitr).second).getCentroidX() * x_conv_factor_ + x_start_;
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 (n_xin_quad == 2) {
648
649
650
651
652 auto xitr1 = x_quad_map.lower_bound((*yitr).first);
653 auto xitr2 = x_quad_map.upper_bound((*yitr).first);
654 xitr2--;
655
656 if (xitr1 != xitr2) {
657 x1 = ((*xitr1).second).getCentroidX() * x_conv_factor_ + x_start_;
658 x2 = ((*xitr2).second).getCentroidX() * x_conv_factor_ + x_start_;
659 sx1 = x_conv_factor_ / 2.;
660 sx2 = sx1;
661 x = (x1 + x2) / 2.;
662 sx = fabs(x1 - x2) / 2 *
663 x_conv_factor_;
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 (n_yin_quad == 1) {
673
674 y = ((*yitr).second).getCentroidY() * y_conv_factor_ + y_start_;
675 sy = ((*yitr).second).getResidual() * y_conv_factor_;
676
677
678 if (sy == 0) sy = 1. / 2 * y_conv_factor_;
679
680 if (n_xin_quad <= 1) {
681
682
683
684 auto xidx = x_idx_quad_map.find((*yitr).first);
685 auto yidx = y_idx_quad_map.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: " << n_xin_quad
701 << " of x and " << n_yin_quad << " of y";
702
703 if (n_yin_quad == 2) {
704
705
706 auto yitr1 = y_quad_map.lower_bound((*yitr).first);
707 auto yitr2 = y_quad_map.upper_bound((*yitr).first);
708 yitr2--;
709 y1 = ((*yitr1).second).getCentroidY() * y_conv_factor_ + y_start_;
710 y2 = ((*yitr2).second).getCentroidY() * y_conv_factor_ + y_start_;
711 sy1 = ((*yitr1).second).getResidual() * y_conv_factor_;
712 sy2 = ((*yitr2).second).getResidual() * y_conv_factor_;
713 y = (y1 + y2) / 2.;
714 sy = fabs(y1 - y2) / 2 * y_conv_factor_;
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 (n_yin_quad == 1 &&
721 n_xin_quad == 2) {
722
723
724
725
726 auto yidx = y_idx_quad_map.find((*yitr).first);
727 tracks.at((*yidx).second).setPosition(x, y);
728 tracks.at((*yidx).second).setSigmaXY(sx, sy);
729
730 int min_overlap_pe = 250;
731 if (((*yitr).second).getPE() < min_overlap_pe) {
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() << " > " << min_overlap_pe
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 = x_idx_quad_map.lower_bound((*yitr).first);
759 auto xidx2 = x_idx_quad_map.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 (n_yin_quad == 2 && n_xin_quad == 1) {
768
769
770
771
772 auto xidx = x_idx_quad_map.find((*yitr).first);
773 tracks.at((*xidx).second).setPosition(x, y);
774 tracks.at((*xidx).second).setSigmaXY(sx, sy);
775
776 auto xitr = x_quad_map.lower_bound((*yitr).first);
777 int min_overlap_pe = 300;
778 if (((*xitr).second).getPE() < min_overlap_pe) {
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() << " > " << min_overlap_pe
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 = y_idx_quad_map.lower_bound((*yitr).first);
803 auto yidx2 = y_idx_quad_map.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 (n_yin_quad == 2 && n_xin_quad == 2) {
812
813 auto xidx1 = x_idx_quad_map.lower_bound((*yitr).first);
814 auto xidx2 = x_idx_quad_map.upper_bound((*yitr).first);
815 xidx2--;
816 auto yidx1 = y_idx_quad_map.lower_bound((*yitr).first);
817 auto yidx2 = y_idx_quad_map.upper_bound((*yitr).first);
818 yidx2--;
819
820 if (y_idx_quad_map.find((*yitr).first) == y_idx_quad_map.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 (n_xin_quad > 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 (n_yin_quad > 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 y_quad_map.clear();
861 x_quad_map.clear();
862
863
864}