12 #include "cetlib/cpu_timer.h" 31 #include <unordered_map> 116 using BestNodeMap = std::unordered_map<const reco::ClusterHit3D*, BestNodeTuple>;
153 std::unique_ptr<lar_cluster3d::IClusterParametersBuilder>
187 auto const uWireDir = uWireGeo->
Direction();
188 m_wireDir[0] = {(float)uWireDir.X(), (float)-uWireDir.Z(), (float)uWireDir.Y()};
194 auto const vWireDir = vWireGeo->
Direction();
195 m_wireDir[1] = {(float)vWireDir.X(), (float)-vWireDir.Z(), (float)vWireDir.Y()};
198 m_clusterBuilder = art::make_tool<lar_cluster3d::IClusterParametersBuilder>(
225 cet::cpu_timer theClockBuildClusters;
233 theClockBuildClusters.stop();
239 for (
auto& clusterParams : clusterParametersList)
242 mf::LogDebug(
"MinSpanTreeAlg") <<
">>>>> Cluster3DHits done, found " 243 << clusterParametersList.size() <<
" clusters" << std::endl;
254 if (hitPairList.empty())
return;
257 cet::cpu_timer theClockDBScan;
263 size_t clusterIdx(0);
291 curEdgeItr != curEdgeList.end();) {
293 curEdgeItr = curEdgeList.erase(curEdgeItr);
299 curCluster->push_back(lastAddedHit);
303 float bestDistance(1.5);
311 for (
auto& pair : CandPairList) {
313 double edgeWeight = lastAddedHit->
getHitChiSquare() * pair.second->getHitChiSquare();
315 curEdgeList.push_back(
reco::EdgeTuple(lastAddedHit, pair.second, edgeWeight));
320 if (curEdgeList.empty()) {
321 std::cout <<
"-----------------------------------------------------------------------------" 324 std::cout <<
"**> Cluster idx: " << clusterIdx++ <<
" has " << curCluster->size() <<
" hits" 328 freeHitItr = std::find_if(freeHitItr, hitPairList.end(), [](
const auto&
hit) {
333 if (freeHitItr == hitPairList.end())
break;
335 std::cout <<
"##################################################################>" 336 "Processing another cluster" 342 curClusterItr = --clusterParametersList.end();
344 curEdgeMap = &(*curClusterItr).getHit3DToEdgeMap();
345 curCluster = &(*curClusterItr).getHitPairListPtr();
346 lastAddedHit = &(*freeHitItr++);
351 curEdgeList.sort([](
const auto&
left,
const auto&
right) {
352 return std::get<2>(
left) < std::get<2>(
right);
358 (*curEdgeMap)[std::get<0>(curEdge)].push_back(curEdge);
359 (*curEdgeMap)[std::get<1>(curEdge)].push_back(
360 reco::EdgeTuple(std::get<1>(curEdge), std::get<0>(curEdge), std::get<2>(curEdge)));
363 lastAddedHit = std::get<1>(curEdge);
368 theClockDBScan.stop();
379 float bestQuality(0.);
380 float aveNumEdges(0.);
381 size_t maxNumEdges(0);
382 size_t nIsolatedHits(0);
385 cet::cpu_timer theClockPathFinding;
395 for (
const auto&
hit : hitPairList) {
402 tempList.push_front(std::get<0>(curEdgeMap[
hit].front()));
404 if (quality > bestQuality) {
405 longestCluster = tempList;
406 bestQuality = quality;
412 aveNumEdges += float(curEdgeMap[
hit].
size());
413 maxNumEdges = std::max(maxNumEdges, curEdgeMap[
hit].
size());
416 aveNumEdges /= float(hitPairList.size());
417 std::cout <<
"----> # isolated hits: " << nIsolatedHits
418 <<
", longest branch: " << longestCluster.size()
419 <<
", cluster size: " << hitPairList.size() <<
", ave # edges: " << aveNumEdges
420 <<
", max: " << maxNumEdges << std::endl;
422 if (!longestCluster.empty()) {
423 hitPairList = longestCluster;
424 for (
const auto&
hit : hitPairList) {
425 for (
const auto& edge : curEdgeMap[
hit])
426 bestEdgeList.emplace_back(edge);
429 std::cout <<
" ====> new cluster size: " << hitPairList.size() << std::endl;
433 theClockPathFinding.stop();
445 cet::cpu_timer theClockPathFinding;
466 float alpha = std::min(
float(1.), std::max(
float(0.001), pcaWidth / pcaLen));
472 for (
const auto& hit3D : curCluster) {
474 if (!curEdgeMap[hit3D].
empty() && curEdgeMap[hit3D].
size() == 1) {
475 Eigen::Vector3f pcaToHitVec(hit3D->getPosition()[0] - pcaCenter(0),
476 hit3D->getPosition()[1] - pcaCenter(1),
477 hit3D->getPosition()[2] - pcaCenter(2));
481 isolatedPointList.emplace_back(pcaToHit(2), pcaToHit(1), hit3D);
485 std::cout <<
"************* Finding best path with A* in cluster *****************" 487 std::cout <<
"**> There are " << curCluster.size() <<
" hits, " << isolatedPointList.size()
488 <<
" isolated hits, the alpha parameter is " << alpha << std::endl;
489 std::cout <<
"**> PCA len: " << pcaLen <<
", wid: " << pcaWidth <<
", height: " << pcaHeight
490 <<
", ratio: " << pcaHeight / pcaWidth << std::endl;
493 if (isolatedPointList.size() > 1) {
495 isolatedPointList.sort([](
const auto&
left,
const auto&
right) {
497 std::numeric_limits<float>::epsilon()) ?
506 std::cout <<
"**> Sorted " << isolatedPointList.size()
513 float cost(std::numeric_limits<float>::max());
515 LeastCostPath(curEdgeMap[startHit].front(), stopHit, clusterParams, cost);
520 <<
" hits, " << clusterParams.
getBestEdgeList().size() <<
" edges" << std::endl;
524 std::cout <<
"++++++>>> PCA failure! # hits: " << curCluster.size() << std::endl;
529 theClockPathFinding.stop();
556 bestNodeMap[startNode] =
559 while (!openList.empty()) {
564 if (openList.size() > 1)
565 currentNodeItr = std::min_element(
566 openList.begin(), openList.end(), [bestNodeMap](
const auto& next,
const auto& best) {
567 return std::get<2>(bestNodeMap.at(next)) < std::get<2>(bestNodeMap.at(best));
574 if (currentNode == goalNode) {
583 openList.erase(currentNodeItr);
587 const BestNodeTuple& currentNodeTuple = bestNodeMap.at(currentNode);
588 float currentNodeScore = std::get<1>(currentNodeTuple);
593 for (
const auto& curEdge : curEdgeList) {
598 float tentative_gScore = currentNodeScore + std::get<2>(curEdge);
603 if (candNodeItr == bestNodeMap.end()) { openList.push_back(candHit3D); }
604 else if (tentative_gScore > std::get<1>(candNodeItr->second))
610 bestNodeMap[candHit3D] =
611 BestNodeTuple(currentNode, tentative_gScore, tentative_gScore + guessToTarget);
624 while (std::get<0>(bestNodeMap.at(goalNode)) != goalNode) {
629 pathNodeList.push_front(goalNode);
630 bestEdgeList.push_front(bestEdge);
635 pathNodeList.push_front(goalNode);
643 float& showMeTheMoney)
const 650 showMeTheMoney = std::numeric_limits<float>::max();
652 if (edgeListItr != curEdgeMap.end() && !edgeListItr->second.empty()) {
656 for (
const auto& edge : edgeListItr->second) {
658 if (std::get<1>(edge) == std::get<0>(curEdge))
continue;
661 if (std::get<1>(edge) == goalNode) {
662 bestNodeList.push_back(goalNode);
663 bestEdgeList.push_back(edge);
664 showMeTheMoney = std::get<2>(edge);
669 float currentCost(0.);
673 if (currentCost < std::numeric_limits<float>::max()) {
674 showMeTheMoney = std::get<2>(edge) + currentCost;
680 if (showMeTheMoney < std::numeric_limits<float>::max()) {
691 const Eigen::Vector3f& node1Pos = node1->
getPosition();
692 const Eigen::Vector3f& node2Pos = node2->
getPosition();
693 float deltaNode[] = {
694 node1Pos[0] - node2Pos[0], node1Pos[1] - node2Pos[1], node1Pos[2] - node2Pos[2]};
697 return std::sqrt(deltaNode[0] * deltaNode[0] + deltaNode[1] * deltaNode[1] +
698 deltaNode[2] * deltaNode[2]);
721 float& bestTreeQuality)
const 724 float bestQuality(0.);
725 float curEdgeWeight = std::max(0.3, std::get<2>(curEdge));
726 float curEdgeProj(1. / curEdgeWeight);
730 if (edgeListItr != hitToEdgeMap.end()) {
732 const Eigen::Vector3f& firstHitPos = std::get<0>(curEdge)->getPosition();
733 const Eigen::Vector3f& secondHitPos = std::get<1>(curEdge)->getPosition();
734 float curEdgeVec[] = {secondHitPos[0] - firstHitPos[0],
735 secondHitPos[1] - firstHitPos[1],
736 secondHitPos[2] - firstHitPos[2]};
737 float curEdgeMag = std::sqrt(curEdgeVec[0] * curEdgeVec[0] + curEdgeVec[1] * curEdgeVec[1] +
738 curEdgeVec[2] * curEdgeVec[2]);
740 curEdgeMag = std::max(
float(0.1), curEdgeMag);
742 for (
const auto& edge : edgeListItr->second) {
744 if (std::get<1>(edge) == std::get<0>(curEdge))
continue;
750 if (quality > bestQuality) {
751 hitPairListPtr = tempList;
752 bestQuality = quality;
753 curEdgeProj = 1. / curEdgeMag;
758 hitPairListPtr.push_front(std::get<1>(curEdge));
760 bestTreeQuality += bestQuality + curEdgeProj;
762 return hitPairListPtr;
773 size_t nStartedWith(hitPairVector.size());
774 size_t nRejectedHits(0);
779 for (
const auto& hit3D : hitPairVector) {
781 size_t n2DHitsIn3DHit(0);
782 size_t nThisClusterOnly(0);
783 size_t nOtherCluster(0);
786 const std::set<const reco::ClusterHit3D*>* otherClusterHits = 0;
788 for (
const auto& hit2D : hit3D->getHits()) {
789 if (!hit2D)
continue;
793 if (hit2DToClusterMap[hit2D].
size() < 2)
794 nThisClusterOnly = hit2DToClusterMap[hit2D][&clusterParams].
size();
796 for (
const auto& clusterHitMap : hit2DToClusterMap[hit2D]) {
797 if (clusterHitMap.first == &clusterParams)
continue;
799 if (clusterHitMap.second.size() > nOtherCluster) {
800 nOtherCluster = clusterHitMap.second.size();
802 otherClusterHits = &clusterHitMap.second;
808 if (n2DHitsIn3DHit < 3 && nThisClusterOnly > 1 && nOtherCluster > 0) {
809 bool skip3DHit(
false);
811 for (
const auto& otherHit3D : *otherClusterHits) {
812 size_t nOther2DHits(0);
814 for (
const auto& otherHit2D : otherHit3D->getHits()) {
815 if (!otherHit2D)
continue;
820 if (nOther2DHits > 2) {
827 if (skip3DHit)
continue;
830 goodHits.emplace_back(hit3D);
833 std::cout <<
"###>> Input " << nStartedWith <<
" hits, rejected: " << nRejectedHits
836 hitPairVector.resize(goodHits.size());
837 std::copy(goodHits.begin(), goodHits.end(), hitPairVector.begin());
847 return (*left).getHitPairListPtr().size() > (*right).getHitPairListPtr().size();
860 if (left->
getHits()[m_plane[0]] && right->
getHits()[m_plane[0]] &&
862 return left->
getHits()[m_plane[0]]->getHit()->PeakTime() <
863 right->
getHits()[m_plane[0]]->getHit()->PeakTime();
869 if (left->
getHits()[m_plane[1]] && right->
getHits()[m_plane[1]] &&
871 return left->
getHits()[m_plane[1]]->getHit()->PeakTime() <
872 right->
getHits()[m_plane[1]]->getHit()->PeakTime();
895 if (curCluster.size() > 2) {
903 std::vector<size_t> closestPlane = {0, 0, 0};
904 std::vector<float> bestAngle = {0., 0., 0.};
906 for (
size_t plane = 0; plane < 3; plane++) {
907 const std::vector<float>& wireDir =
m_wireDir[plane];
910 std::fabs(pcaAxis[0] * wireDir[0] + pcaAxis[1] * wireDir[1] + pcaAxis[2] * wireDir[2]);
912 if (dotProd > bestAngle[0]) {
913 bestAngle[2] = bestAngle[1];
914 closestPlane[2] = closestPlane[1];
915 bestAngle[1] = bestAngle[0];
916 closestPlane[1] = closestPlane[0];
917 closestPlane[0] = plane;
918 bestAngle[0] = dotProd;
920 else if (dotProd > bestAngle[1]) {
921 bestAngle[2] = bestAngle[1];
922 closestPlane[2] = closestPlane[1];
923 closestPlane[1] = plane;
924 bestAngle[1] = dotProd;
927 closestPlane[2] = plane;
928 bestAngle[2] = dotProd;
939 std::cout <<
"*****************************************************************************" 942 std::cout <<
"**>>>>> longest axis: " << closestPlane[0] <<
", best angle: " << bestAngle[0]
944 std::cout <<
"**>>>>> second axis: " << closestPlane[1] <<
", best angle: " << bestAngle[1]
946 std::cout <<
" " << std::endl;
951 size_t bestPlane = closestPlane[0];
955 while (firstHitItr != localHitList.end()) {
959 while (lastHitItr != localHitList.end()) {
961 if (currentHit->
getWireIDs()[bestPlane] != (*lastHitItr)->getWireIDs()[bestPlane])
965 if (currentHit->
getHits()[bestPlane] && (*lastHitItr)->getHits()[bestPlane] &&
966 currentHit->
getHits()[bestPlane] != (*lastHitItr)->getHits()[bestPlane])
970 if ((!(currentHit->
getHits()[bestPlane]) && (*lastHitItr)->getHits()[bestPlane]) ||
971 (currentHit->
getHits()[bestPlane] && !((*lastHitItr)->getHits()[bestPlane])))
989 while (firstHitItr != lastHitItr) {
992 currentHit = *++firstHitItr;
995 firstHitItr = lastHitItr;
1018 curCluster = testList;
bool operator()(const reco::ClusterHit3D *left, const reco::ClusterHit3D *right) const
reco::HitPairListPtr & getBestHitPairListPtr()
Geometry description of a TPC wireThe wire is a single straight segment on a wire plane...
std::tuple< const reco::ClusterHit3D *, float, float > BestNodeTuple
void configure(fhicl::ParameterSet const &pset) override
std::list< reco::ClusterHit3D > HitPairList
std::vector< std::vector< float > > m_wireDir
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
void LeastCostPath(const reco::EdgeTuple &, const reco::ClusterHit3D *, reco::ClusterParameters &, float &) const
Find the lowest cost path between two nodes using MST edges.
void PCAAnalysis_3D(const reco::HitPairListPtr &hitPairList, reco::PrincipalComponents &pca, bool skeletonOnly=false) const
float getTimeToExecute(TimeValues index) const override
If monitoring, recover the time to execute a particular function.
std::vector< WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
geo::Geometry const * m_geometry
bool m_enableMonitoring
Data members to follow.
const Eigen::Vector3f getPosition() const
std::list< ProjectedPoint > ProjectedPointList
Declaration of signal hit object.
std::list< KdTreeNode > KdTreeNodeList
constexpr auto abs(T v)
Returns the absolute value of the argument.
Implements a kdTree for use in clustering.
size_t FindNearestNeighbors(const reco::ClusterHit3D *, const KdTreeNode &, CandPairList &, float &) const
const std::vector< size_t > & m_plane
reco::EdgeList & getBestEdgeList()
MinSpanTreeAlg(const fhicl::ParameterSet &)
Constructor.
reco::Hit3DToEdgeMap & getHit3DToEdgeMap()
reco::HitPairListPtr & getHitPairListPtr()
IClusterAlg interface class definiton.
std::vector< size_t > ChannelStatusVec
define data structure for keeping track of channel status
float DistanceBetweenNodes(const reco::ClusterHit3D *, const reco::ClusterHit3D *) const
unsigned int getStatusBits() const
void AStar(const reco::ClusterHit3D *, const reco::ClusterHit3D *, reco::ClusterParameters &) const
Algorithm to find shortest path between two 3D hits.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
std::list< EdgeTuple > EdgeList
void PruneAmbiguousHits(reco::ClusterParameters &, reco::Hit2DToClusterMap &) const
Prune the obvious ambiguous hits.
const EigenValues & getEigenValues() const
std::vector< ChannelStatusVec > ChannelStatusByPlaneVec
std::unordered_map< const reco::ClusterHit3D *, BestNodeTuple > BestNodeMap
Vector_t Direction() const
std::tuple< const reco::ClusterHit3D *, const reco::ClusterHit3D *, double > EdgeTuple
std::unordered_map< const reco::ClusterHit2D *, ClusterToHitPairSetMap > Hit2DToClusterMap
T get(std::string const &key) const
const Eigen::Vector3f & getAvePosition() const
void RunPrimsAlgorithm(reco::HitPairList &, kdTree::KdTreeNode &, reco::ClusterParametersList &) const
Driver for Prim's algorithm.
Path checking algorithm has seen this hit.
TimeValues
enumerate the possible values for time checking if monitoring timing
void CheckHitSorting(reco::ClusterParameters &clusterParams) const
void Cluster3DHits(reco::HitPairList &hitPairList, reco::ClusterParametersList &clusterParametersList) const override
Given a set of recob hits, run DBscan to form 3D clusters.
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
std::list< const reco::ClusterHit3D * > HitPairListPtr
This provides an art tool interface definition for 3D Cluster algorithms.
The geometry of one entire detector, as served by art.
void Cluster3DHits(reco::HitPairListPtr &, reco::ClusterParametersList &) const override
Given a set of recob hits, run DBscan to form 3D clusters.
Definition of data types for geometry description.
std::unique_ptr< lar_cluster3d::IClusterParametersBuilder > m_clusterBuilder
Common cluster builder tool.
std::vector< float > m_timeVector
Detector simulation of raw signals on wires.
std::list< CandPair > CandPairList
This header file defines the interface to a principal components analysis designed to be used within ...
Encapsulate the geometry of a wire.
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
const std::vector< geo::WireID > & getWireIDs() const
~MinSpanTreeAlg()
Destructor.
bool operator()(const reco::ClusterParametersList::iterator &left, const reco::ClusterParametersList::iterator &right)
float getHitChiSquare() const
std::unordered_map< const reco::ClusterHit3D *, reco::EdgeList > Hit3DToEdgeMap
unsigned int ChannelID_t
Type representing the ID of a readout channel.
float getTimeToExecute() const
WireGeo const * WirePtr(WireID const &wireid) const
Returns the specified wire.
KdTreeNode & BuildKdTree(Hit3DVec::iterator, Hit3DVec::iterator, KdTreeNodeList &, int depth=0) const
Given an input set of ClusterHit3D objects, build a kd tree structure.
const ClusterHit2DVec & getHits() const
art framework interface to geometry description
void ReconstructBestPath(const reco::ClusterHit3D *, BestNodeMap &, reco::HitPairListPtr &, reco::EdgeList &) const
std::list< ClusterParameters > ClusterParametersList
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
const EigenVectors & getEigenVectors() const
SetCheckHitOrder(const std::vector< size_t > &plane)
void FindBestPathInCluster(reco::ClusterParameters &) const
Algorithm to find the best path through the given cluster.
PrincipalComponentsAlg m_pcaAlg
void setStatusBit(unsigned bits) const
reco::HitPairListPtr DepthFirstSearch(const reco::EdgeTuple &, const reco::Hit3DToEdgeMap &, float &) const
a depth first search to find longest branches