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