CBAlgoCenterOfMassSmall.cxx
Go to the documentation of this file.
2 
3 #include "TString.h"
4 
5 namespace cmtool {
6 
7  //----------------------------------------
9  //----------------------------------------
10  {
11 
12  SetDebug(false);
14  SetMaxDistance(20.);
15  SetMaxCOMDistance(25.);
16  UseCOMInPoly(true);
17  UseCOMClose(true);
18  UseCOMNearClus(true);
19 
20  }
21 
22  //---------------------------------------------------------------------------
23  bool CBAlgoCenterOfMassSmall::Bool(const ::cluster::ClusterParamsAlg &cluster1,
24  const ::cluster::ClusterParamsAlg &cluster2)
25  //---------------------------------------------------------------------------
26  {
27 
28  int Nhits1 = 0;
29  int Nhits2 = 0;
30 
31  //Both clusters should have less hits than some threshold
32  if ( (cluster1.GetHitVector().size() > _maxHits) or (cluster2.GetHitVector().size() > _maxHits) )
33  return false;
34 
35  //Define COM values on w & t
36  double COM_t_1 = 0;
37  double COM_w_1 = 0;
38  double Q_1 = 0;
39  double start_w_1;
40  double start_t_1;
41  double end_w_1;
42  double end_t_1;
43  Polygon2D poly1;
44  double COM_t_2 = 0;
45  double COM_w_2 = 0;
46  double Q_2 = 0;
47  double start_w_2;
48  double start_t_2;
49  double end_w_2;
50  double end_t_2;
51  Polygon2D poly2;
52 
53  //Get Hit vector for cluster 1
54  //std::vector<util::PxHit> hits1;
55  std::vector<util::PxHit> hits1;
56  hits1 = cluster1.GetHitVector();
57  Nhits1 = hits1.size();
58  poly1 = cluster1.GetParams().PolyObject;
59  start_w_1 = cluster1.GetParams().start_point.w;
60  start_t_1 = cluster1.GetParams().start_point.t;
61  end_w_1 = cluster1.GetParams().end_point.w;
62  end_t_1 = cluster1.GetParams().end_point.t;
63  //Get Hit vector for cluster 2
64  //std::vector<util::PxHit> hits2;
65  std::vector<util::PxHit> hits2;
66  hits2 = cluster2.GetHitVector();
67  Nhits2 = hits2.size();
68  poly2 = cluster2.GetParams().PolyObject;
69  start_w_2 = cluster2.GetParams().start_point.w;
70  start_t_2 = cluster2.GetParams().start_point.t;
71  end_w_2 = cluster2.GetParams().end_point.w;
72  end_t_2 = cluster2.GetParams().end_point.t;
73 
74  //Find COM for cluster 1
75  for (auto& hit: hits1){
76  COM_t_1 += hit.t * hit.charge;
77  COM_w_1 += hit.w * hit.charge;
78  Q_1 += hit.charge;
79  }
80  COM_t_1 /= Q_1;
81  COM_w_1 /= Q_1;
82 
83  //Find COM for cluster 2
84  for (auto& hit: hits2){
85  COM_t_2 += hit.t * hit.charge;
86  COM_w_2 += hit.w * hit.charge;
87  Q_2 += hit.charge;
88  }
89  COM_t_2 /= Q_2;
90  COM_w_2 /= Q_2;
91 
92  if (_debug) {
93  std::cout << "Cluster 1: " << std::endl;
94  std::cout << "N Hits: " << Nhits1 << std::endl;
95  std::cout << "COM: (w,t) -> (" << COM_w_1 << ", " << COM_t_1 << ")" << std::endl;
96  std::cout << "Cluster 2: " << std::endl;
97  std::cout << "N Hits: " << Nhits2 << std::endl;
98  std::cout << "COM: (w,t) -> (" << COM_w_2 << ", " << COM_t_2 << ")" << std::endl;
99  std::cout << std::endl;
100  }
101 
102  //Get COM
103  std::pair<float,float> COM_1;
104  COM_1 = std::make_pair( COM_w_1, COM_t_1 );
105  std::pair<float,float> COM_2;
106  COM_2 = std::make_pair( COM_w_2, COM_t_2 );
107 
108  //look for polygon overlap
109  if ( ( ( poly2.PointInside(COM_1) ) and _COMinPolyAlg ) or
110  ( ( poly1.PointInside(COM_2) ) and _COMinPolyAlg ) ){
111  if (_verbose) { std::cout << "Polygon Overlap -> Merge!" << std::endl << std::endl;}
112  return true;
113  }
114 
115  //look for COM of 1 close to COM of 2
116  double distCOMs = ( COM_w_1-COM_w_2 )*( COM_w_1-COM_w_2 ) +
117  ( COM_t_1-COM_t_2 )*( COM_t_1-COM_t_2 );
118  if ( _COMsClose and ( distCOMs < _MaxCOMDistSquared ) ){
119  if (_verbose) { std::cout << "COMs close to each other -> Merge!" << std::endl << std::endl;}
120  return true;
121  }
122 
123  //look for COM close to start-end of other cluster
124  if ( _COMNearClus and
125  ( ( ShortestDistanceSquared( COM_w_1, COM_t_1, start_w_2, start_t_2, end_w_2, end_t_2 ) < _MaxDist ) or
126  ( ShortestDistanceSquared( COM_w_2, COM_t_2, start_w_1, start_t_1, end_w_1, end_t_1 ) < _MaxDist ) ) ) {
127  if (_verbose) { std::cout << "COM close to start-end -> Merge!" << std::endl; }
128  return true;
129  }
130 
131  return false;
132  }
133 
134  //-----------------------
136  //-----------------------
137  {
138 
139  }
140 
141  double CBAlgoCenterOfMassSmall::ShortestDistanceSquared(double point_x, double point_y,
142  double start_x, double start_y,
143  double end_x, double end_y ) const {
144 
145  //This code finds the shortest distance between a point and a line segment.
146  //code based off sample from
147  //http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
148  //note to self: rewrite this with TVector2 and compare time differences...
149  //TVector2 code might be more understandable
150 
151  double distance_squared = -1;
152 
153  // Line segment: from ("V") = (start_x, start_y) to ("W")=(end_x, end_y)
154  double length_squared = pow((end_x - start_x), 2) + pow((end_y - start_y), 2);
155 
156  // Treat the case start & end point overlaps
157  if(length_squared < 0.1) {
158  if(_verbose){
159  std::cout << std::endl;
160  std::cout << Form(" Provided very short line segment: (%g,%g) => (%g,%g)",
161  start_x,start_y,end_x,end_y) << std::endl;
162  std::cout << " Likely this means one of two clusters have start & end point identical." << std::endl;
163  std::cout << " Check the cluster output!" << std::endl;
164  std::cout << std::endl;
165  std::cout << Form(" At this time, the algorithm uses a point (%g,%g)",start_x,start_y) << std::endl;
166  std::cout << " to represent this cluster's location." << std::endl;
167  std::cout << std::endl;
168  }
169 
170  return (pow((point_x - start_x),2) + pow((point_y - start_y),2));
171  }
172 
173  //Find shortest distance between point ("P")=(point_x,point_y) to this line segment
174  double t = ( (point_x - start_x)*(end_x - start_x) + (point_y - start_y)*(end_y - start_y) ) / length_squared;
175 
176  if(t<0.0) distance_squared = pow((point_x - start_x), 2) + pow((point_y - start_y), 2);
177 
178  else if (t>1.0) distance_squared = pow((point_x - end_x), 2) + pow(point_y - end_y, 2);
179 
180  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);
181 
182  return distance_squared;
183 
184  }//end ShortestDistanceSquared function
185 
186 
187 
188 
189 }
bool _COMinPolyAlg
How four out - as percent of cluster length - cone will extend from start point.
virtual bool Bool(const ::cluster::ClusterParamsAlg &cluster1, const ::cluster::ClusterParamsAlg &cluster2)
void SetMaxCOMDistance(double d)
Function to set Max Distance between COMs.
constexpr T pow(T x)
Definition: pow.h:72
virtual void Report()
Function to report what&#39;s going on per merging iteration.
double ShortestDistanceSquared(double point_x, double point_y, double start_x, double start_y, double end_x, double end_y) const
void UseCOMClose(bool on)
Use COM in Poly algo to decide merging.
CBAlgoCenterOfMassSmall()
Default constructor.
Detector simulation of raw signals on wires.
Class def header for a class CBAlgoCenterOfMassSmall.
void SetMaxDistance(double d)
Function to set Max Distance for COM to be from start-end.
void UseCOMInPoly(bool on)
Use COM in Poly algo to decide merging.
void SetDebug(bool on)
Function to set Debug mode of output.
void UseCOMNearClus(bool on)
Use COM in Poly algo to decide merging.
bool PointInside(const std::pair< float, float > &point) const
Definition: Polygon2D.cxx:252
void SetMaxHitsSmallClus(size_t n)
Function to set Max hits for small clsuters.
bool _verbose
Boolean to choose verbose mode. Turned on if CMergeManager/CMatchManager&#39;s verbosity level is >= kPer...
Definition: CMAlgoBase.h:102
QTextStream & endl(QTextStream &s)