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