Belle II Software  release-08-01-10
HoughPlane.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 //-----------------------------------------------------------------------------
10 // Description : A base class to represent a Hough parameter plane
11 //-----------------------------------------------------------------------------
12 
13 #define TRGCDC_SHORT_NAMES
14 
15 #include "trg/cdc/HoughPlane.h"
16 
17 namespace Belle2 {
23  TRGCDCHoughPlane::TRGCDCHoughPlane(const std::string& name,
24  const TRGCDCHoughTransformation& trans,
25  unsigned nX,
26  float xMin,
27  float xMax,
28  unsigned nY,
29  float yMin,
30  float yMax)
31  : TRGCDCHoughPlaneBase(name, trans, nX, xMin, xMax, nY, yMin, yMax),
32 // _cell(new unsigned[nX * nY * sizeof(unsigned)]),
33  _cell(new int[nX * nY]),
34  _patterns(0),
35  _nPatterns(0)
36  {
37  clear();
38  }
39 
41  {
42  delete [] _cell;
43  if (_patterns)
44  delete [] _patterns;
45  }
46 
47 // void
48 // TRGCDCHoughPlane::vote(float x, float y) {
49 // if (x < xMin()) return;
50 // if (x > xMax()) return;
51 // if (y < yMin()) return;
52 // if (y > yMax()) return;
53 
54 // const unsigned iX = unsigned((x - xMin()) / xSize());
55 // const unsigned iY = unsigned((y - yMin()) / ySize());
56 // const unsigned id = iX * nY() + iY;
57 // ++_cell[id];
58 // }
59 
60 // void
61 // TRGCDCHoughPlane::vote(float x0, float y0, float x1, float y1) {
62 
63 // //...Cell positions...
64 // unsigned iX0, iY0, iX1, iY1;
65 // unsigned nFound;
66 // locationInPlane(x0, y0, x1, y1, nFound, iX0, iY0, iX1, iY1);
67 // #ifdef TRASAN_DEBUG_DETAIL
68 // std::cout << "(x0,y0)=(" << x0 << "," << y0 << "),(iX0,iY0)=(" << iX0
69 // << "," << iY0 << ")" << std::endl;
70 // std::cout << "(x1,y1)=(" << x1 << "," << y1
71 // << "),(iX1,iY1)=(" << iX1 << "," << iY1 << ")" << std::endl;
72 // #endif
73 
74 // //...No cell...
75 // if (nFound < 2) return;
76 
77 // //...Same cell...
78 // if ((iX0 == iX1) && (iY0 == iY1)) {
79 // ++_cell[iX0 * nY() + iY0];
80 // return;
81 // }
82 
83 // //...X...
84 // else if (iX0 == iX1) {
85 // int iMin = iY0;
86 // int iMax = iY1;
87 // if (iY0 > iY1) {
88 // iMin = iY1;
89 // iMax = iY0;
90 // }
91 // for (int i = iMin; i < (iMax + 1); i++)
92 // ++_cell[iX0 * nY() + i];
93 // return;
94 // }
95 
96 // //...Y...
97 // else if (iY0 == iY1) {
98 // int iMin = iX0;
99 // int iMax = iX1;
100 // if (iX0 > iX1) {
101 // iMin = iX1;
102 // iMax = iX0;
103 // }
104 // for (int i = iMin; i < (iMax + 1); i++)
105 // ++_cell[i * nY() + iY0];
106 // return;
107 // }
108 
109 // //...Line...
110 // const float xDiff = x1 - x0;
111 // const float yDiff = y1 - y0;
112 // const float slope = yDiff / xDiff;
113 // const float offset = y0 - slope * x0;
114 
115 // //...Direction to move : (x0, y0) -> (x1, y1)...
116 // unsigned direction = 0;
117 // if ((xDiff < 0) && (yDiff > 0)) direction = 1;
118 // else if ((xDiff < 0) && (yDiff < 0)) direction = 2;
119 // else if ((xDiff > 0) && (yDiff < 0)) direction = 3;
120 
121 // //...Grid offset to search a cross-point...
122 // int iXGrid = 1;
123 // int iYGrid = 1;
124 // if (direction == 1) {
125 // iXGrid = 0;
126 // iYGrid = 1;
127 // }
128 // else if (direction == 2) {
129 // iXGrid = 0;
130 // iYGrid = 0;
131 // }
132 // else if (direction == 3) {
133 // iXGrid = 1;
134 // iYGrid = 0;
135 // }
136 
137 // std::cout << "direction=" << direction << std::endl;
138 // std::cout << "iXGrid,iYGrid=" << iXGrid << "," << iYGrid << std::endl;
139 
140 // //...Steps...
141 // const int xStep = iX1 - iX0;
142 // const int yStep = iY1 - iY0;
143 // const unsigned maxStep = abs(xStep) + abs(yStep);
144 
145 // unsigned iX = iX0;
146 // unsigned iY = iY0;
147 // unsigned steps = 0;
148 // while (1) {
149 
150 // //...Vote this cell...
151 // ++_cell[iX * nY() + iY];
152 // if ((iX == iX1) && (iY == iY1))
153 // break;
154 
155 // //...Center of the cell...
156 // TPoint2D center((float(iX) + 0.5) * xSize() + xMin(),
157 // (float(iY) + 0.5) * ySize() + yMin());
158 
159 // //...Set grids...
160 // const float xGrid = (iX + iXGrid) * xSize() + xMin();
161 // const float yGrid = (iY + iYGrid) * ySize() + yMin();
162 
163 // //...Cal. position of cross-points with grids...
164 // TPoint2D crs[2];
165 // crs[0] = TPoint2D((yGrid - offset) / slope, yGrid);
166 // crs[1] = TPoint2D(xGrid, slope * xGrid + offset);
167 
168 // //...Select points on a cell...
169 // unsigned best = 0;
170 // if ((crs[1] - center).mag2() < (crs[0] - center).mag2()) best = 1;
171 
172 // #ifdef TRASAN_DEBUG_DETAIL
173 // std::cout << " step " << steps << ":(iX,iY)=" << iX << "," << iY
174 // << "):center=(" << center.x() << "," << center.y()
175 // << "):best=" << best << std::endl;
176 // std::cout << " crs0=(" << crs[0].x() << "," << crs[0].y()
177 // << "),crs1=(" << crs[1].x() << "," << crs[1].y() << "),"
178 // << std::endl;
179 // #endif
180 
181 // //...Move...
182 // if (best == 0) {
183 // if (iYGrid == 1)
184 // ++iY;
185 // else
186 // --iY;
187 // }
188 // else {
189 // if (iXGrid == 1)
190 // ++iX;
191 // else
192 // --iX;
193 // }
194 
195 // //...End ?...
196 // ++steps;
197 // if (steps > maxStep) break;
198 // }
199 // }
200 
201  void
203  {
204  if (_patterns) {
205  delete [] _patterns;
206  _nPatterns = 0;
207  }
208  const unsigned n = nX() * nY();
209 
210  //...Check # of active cells...
211  for (unsigned i = 0; i < n; i++)
212  if (_cell[i]) ++_nPatterns;
213 
214  //...Create array...
215  _patterns = new unsigned[_nPatterns];
216 
217  //...Store them...
218  unsigned j = 0;
219  for (unsigned i = 0; i < n; i++)
220  if (_cell[i]) _patterns[j++] = i;
221  }
222 
223  void
224  TRGCDCHoughPlane::voteByPattern(float xOffset, int weight)
225  {
226 #ifdef TRASAN_DEBUG
227  if (_patterns == 0)
228  std::cout << "TRGCDCHoughPlane::vote !!! pattern is note defined" << std::endl;
229  if ((xOffset < 0) || (xOffset > 1))
230  std::cout << "TRGCDCHoughPlane::vote !!! xOffset should be (0 - 1). xOffset="
231  << xOffset << std::endl;
232 #endif
233 
234  const unsigned x = unsigned(nX() * xOffset);
235  const unsigned p = x ? (x - 1) * nY() : 0;
236  const unsigned n = nX() * nY();
237 
238  for (unsigned i = 0; i < _nPatterns; i++) {
239  unsigned id = _patterns[i] + p;
240  if (id > n) id -= n;
241  _cell[id] += weight;
242  }
243 
244 // const unsigned x = unsigned(nX() * xOffset);
245 // const unsigned p = x ? (x - 1) * nY() : 0;
246 // const unsigned n = nX() * nY();
247 // unsigned j = 0;
248 // for (unsigned i = p; i < n; i++)
249 // _cell[i] += _pattern[j++] * weight;
250 // for (unsigned i = 0; i < p; i++)
251 // _cell[i] += _pattern[j++] * weight;
252 
253 // std::cout << "--------------------------------" << std::endl;
254  }
255 
257 } // namespace Belle2
258 
A class to represent a Hough parameter plane.
unsigned * _patterns
Curve patterns.
Definition: HoughPlane.h:96
int *const _cell
Counters.
Definition: HoughPlane.h:93
unsigned _nPatterns
# of curve patterns.
Definition: HoughPlane.h:99
An abstract class to represent a Hough transformation.
void registerPattern(unsigned id) override
Registers a pattern on a Hough plane with (r, phi=0).
Definition: HoughPlane.cc:202
TRGCDCHoughPlane(const std::string &name, const TRGCDCHoughTransformation &transformation, unsigned nX, float xMin, float xMax, unsigned nY, float yMin, float yMax)
Contructor.
Definition: HoughPlane.cc:23
unsigned nY(void) const
return # of y bins.
void id(unsigned serialId, unsigned &x, unsigned &y) const
returns x and y for serialID.
void clear(void) override
clear all entries.
Definition: HoughPlane.h:115
virtual void voteByPattern(float xOffset, int weight=1)
Votes using a pattern.
Definition: HoughPlane.cc:224
unsigned nX(void) const
returns # of x bins.
virtual ~TRGCDCHoughPlane()
Destructor.
Definition: HoughPlane.cc:40
Abstract base class for different kinds of events.