CBAlgoShortestDistNonEndPoint.cxx
Go to the documentation of this file.
2 
3 #include "TString.h"
4 
5 namespace cmtool {
6 
7 
9 
10  //this just sets default values
11  SetVerbose(true);
12  SetDebug(false);
13  SetMinHits(0);
14 
15  //1e9 is huge; everything will be merged
17 
18  _min_distance_unit = 1.e-3;
19  } //end constructor
20 
21  bool CBAlgoShortestDistNonEndpoint::Bool(const ::cluster::ClusterParamsAlg &cluster1,
22  const ::cluster::ClusterParamsAlg &cluster2)
23  {
24 
25  //if number of hits not large enough skip
26  if ( (_minHits > 0) and ((cluster1.GetParams().N_Hits < _minHits) or (cluster2.GetParams().N_Hits < _minHits)) ) {
27  if (_debug) { std::cout << "Num of Hits below threshold..." << std::endl; }
28  return false;
29  }
30 
31  double w_start1 = cluster1.GetParams().start_point.w;
32  double t_start1 = cluster1.GetParams().start_point.t;
33  double w_end1 = cluster1.GetParams().end_point.w;
34  double t_end1 = cluster1.GetParams().end_point.t;
35 
36  double w_start2 = cluster2.GetParams().start_point.w;
37  double t_start2 = cluster2.GetParams().start_point.t;
38  double w_end2 = cluster2.GetParams().end_point.w;
39  double t_end2 = cluster2.GetParams().end_point.t;
40 
41  if (_debug){
42  std::cout << "Start point Cluster 1: (" << w_start1 << ", " << t_start1 << ")" << std::endl;
43  std::cout << "End point Cluster 2: (" << w_end1 << ", " << t_end1 << ")" << std::endl;
44  std::cout << "Start point Cluster 1: (" << w_start2 << ", " << t_start2 << ")" << std::endl;
45  std::cout << "End point Cluster 2: (" << w_end2 << ", " << t_end2 << ")" << std::endl;
46  }
47 
48  //First, pretend the first cluster is a 2D line segment, from its start point to end point
49  //Find the shortest distance between start point of the second cluster to this line segment.
50  //Repeat for end point of second cluster to this line segment.
51  //Then, pretend second cluster is a 2D line segment, from its start point to end point.
52  //Find the shortest distance between start point of the first cluster to this line segment.
53  //Repeat for end point of first cluster to this line segment.
54  //If the shortest of these four distances is less than the cutoff,
55  //return true (the clusters are merge-compatible). else, return false.
56 
57  // Step 1: inspect (w_start1, t_start1) vs. line (w_start2, t_start2) => (w_end2, t_end2)
58  double shortest_distance2 = ShortestDistanceSquared(w_start1, t_start1,
59  w_start2, t_start2,
60  w_end2, t_end2);
61 
62  // Step 2: inspect (w_end1, t_end1) vs. line (w_start2, t_start2) => (w_end2, t_end2)
63  double shortest_distance2_tmp = ShortestDistanceSquared(w_end1, t_end1,
64  w_start2, t_start2,
65  w_end2, t_end2);
66 
67  shortest_distance2 = (shortest_distance2_tmp < shortest_distance2) ?
68  shortest_distance2_tmp : shortest_distance2;
69 
70  // Step 3: inspect (w_start2, t_start2) vs. line (w_start1, t_start1) => (w_end1, t_end1)
71  shortest_distance2_tmp = ShortestDistanceSquared(w_start2, t_start2,
72  w_start1, t_start1,
73  w_end1, t_end1);
74 
75  shortest_distance2 = (shortest_distance2_tmp < shortest_distance2) ?
76  shortest_distance2_tmp : shortest_distance2;
77 
78  // Step 4: inspect (w_end2, t_end2) vs. line (w_start1, t_start1) => (w_end1, t_end1)
79  shortest_distance2_tmp = ShortestDistanceSquared(w_end2, t_end2,
80  w_start1, t_start1,
81  w_end1, t_end1);
82 
83  shortest_distance2 = (shortest_distance2_tmp < shortest_distance2) ?
84  shortest_distance2_tmp : shortest_distance2;
85 
86  bool compatible = shortest_distance2 < _max_2D_dist2;
87 
88  if(_verbose) {
89 
90  if(compatible) std::cout<<Form(" Compatible in distance (%g).\n",shortest_distance2);
91  else std::cout<<Form(" NOT compatible in distance (%g).\n",shortest_distance2);
92 
93  }
94 
95  return compatible;
96 
97 
98  }//end Bool function
99 
100  double CBAlgoShortestDistNonEndpoint::ShortestDistanceSquared(double point_x, double point_y,
101  double start_x, double start_y,
102  double end_x, double end_y ) const {
103 
104  //This code finds the shortest distance between a point and a line segment.
105  //code based off sample from
106  //http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
107  //note to self: rewrite this with TVector2 and compare time differences...
108  //TVector2 code might be more understandable
109 
110  double distance_squared = 999999;
111 
112  // Line segment: from ("V") = (start_x, start_y) to ("W")=(end_x, end_y)
113  double length_squared = pow((end_x - start_x), 2) + pow((end_y - start_y), 2);
114 
115  // Treat the case start & end point overlaps
116  if(length_squared < _min_distance_unit) {
117  if(_verbose){
118  std::cout << std::endl;
119  std::cout << Form(" Provided very short line segment: (%g,%g) => (%g,%g)",
120  start_x,start_y,end_x,end_y) << std::endl;
121  std::cout << " Likely this means one of two clusters have start & end point identical." << std::endl;
122  std::cout << " Check the cluster output!" << std::endl;
123  std::cout << std::endl;
124  std::cout << Form(" At this time, the algorithm uses a point (%g,%g)",start_x,start_y) << std::endl;
125  std::cout << " to represent this cluster's location." << std::endl;
126  std::cout << std::endl;
127  }
128 
129  return (pow((point_x - start_x),2) + pow((point_y - start_y),2));
130  }
131 
132  //Find shortest distance between point ("P")=(point_x,point_y) to this line segment
133  double t = ( (point_x - start_x)*(end_x - start_x) + (point_y - start_y)*(end_y - start_y) ) / length_squared;
134 
135  //if(t<0.0) distance_squared = pow((point_x - start_x), 2) + pow((point_y - start_y), 2);
136 
137  //else if (t>1.0) distance_squared = pow((point_x - end_x), 2) + pow(point_y - end_y, 2);
138 
139  //else distance_squared = pow((point_x - (start_x + t*(end_x - start_x))), 2) + pow((point_y - (start_y + t*(end_y - start_y))),2);
140 
141 
142  if(t>0.1 && t < 0.9) distance_squared = pow((point_x - (start_x + t*(end_x - start_x))), 2) + pow((point_y - (start_y + t*(end_y - start_y))),2);
143 
144 
145  return distance_squared;
146 
147  }//end ShortestDistanceSquared function
148 
149 }
double ShortestDistanceSquared(double point_x, double point_y, double start_x, double start_y, double end_x, double end_y) const
virtual bool Bool(const ::cluster::ClusterParamsAlg &cluster1, const ::cluster::ClusterParamsAlg &cluster2)
Overloaded (from CBoolAlgoBase) Bool function.
constexpr T pow(T x)
Definition: pow.h:72
size_t _minHits
bool to suppress lots of output if you want
void SetMinHits(int n)
Set Minimum Number of Hits to consider Cluster.
void SetVerbose(bool on)
Method to set verbose mode.
double _min_distance_unit
Min Number of hits for cluster to be considered.
void SetSquaredDistanceCut(double d)
Method to set cut value in cm^2 for distance compatibility test.
double _max_2D_dist2
minimum distance b/t start and end point of cluster to use it
bool _debug
bool to suppress lots of output if you want
Class def header for a class CBAlgoShortestDistNonEndpoint.
Definition: cfalgo.cc:3
void SetDebug(bool on)
Method to set debug mode.
QTextStream & endl(QTextStream &s)