Belle II Software  release-08-01-10
HoughPlaneBoolean.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 class to represent a Hough parameter plane with simple
11 // counter.
12 //-----------------------------------------------------------------------------
13 
14 #define TRGCDC_SHORT_NAMES
15 
16 #include <iostream>
17 #include "trg/cdc/HoughPlaneBoolean.h"
18 
19 using namespace std;
20 
21 namespace Belle2 {
27  TRGCDCHoughPlaneBoolean::TRGCDCHoughPlaneBoolean(const std::string& name,
28  const TRGCDCHoughTransformation& trans,
29  unsigned nX,
30  float xMin,
31  float xMax,
32  unsigned nY,
33  float yMin,
34  float yMax)
35  : TRGCDCHoughPlaneBase(name, trans, nX, xMin, xMax, nY, yMin, yMax),
36  _n(nX * nY / 32 + 1),
37  _cell(new unsigned[_n]),
38  _nPatterns(0),
39  _patterns(0),
40  _nActive(0),
41  _reverse(0)
42  {
43  clear();
44  }
45 
47  {
48  for (unsigned i = 0; i < _nPatterns; i++)
49  delete [] _patterns[i];
50  delete [] _patterns;
51  delete [] _nActive;
52  delete [] _cell;
53  delete [] _reverse;
54  }
55 
56  void
58  float ry,
59  float targetCharge,
60  int weight)
61  {
62 
63  const HepGeom::Point3D<double> r(rx, ry, 0);
64 
65 //
66 // cout << "yMax=" << yMax() << endl;
67 //
68 
69  //...phi loop...
70  for (unsigned i = 0; i < nX(); i++) {
71  const float x0 = xSize() * float(i);
72  const HepGeom::Point3D<double> center(cos(x0), sin(x0), 0);
73  float charge = r.cross(center).z();
74  if (targetCharge != 0)
75  if (targetCharge * charge > 0)
76  continue;
77 
78  float y0 = transformation().y(rx, ry, x0);
79  const float x1 = xSize() * float(i + 1);
80  float y1 = transformation().y(rx, ry, x1);
81 
82 //
83 // cout << "x0,x1,y0,y1=" << x0 << "," << x1 << "," << y0 << "," << y1
84 // << endl;
85 //
86 
87  //...Check y position...
88  if ((y0 == 0) && (y1 == 0))
89  continue;
90  else if ((y0 > yMax()) && (y1 > yMax()))
91  continue;
92  else if ((y0 < yMin()) && (y1 < yMin()))
93  continue;
94 
95  //...Divergence here...
96  if ((y0 == 0) && (y1 != 0))
97  y0 = yMax();
98  else if ((y0 != 0) && (y1 == 0))
99  y1 = yMax();
100 
101  //...Adjust location...
102  if (y0 < yMin())
103  y0 = yMin();
104  if (y1 < yMin())
105  y1 = yMin();
106  if (y0 > yMax())
107  y0 = yMax();
108  if (y1 > yMax())
109  y1 = yMax();
110 
111  //...Location in the plane...
112  int iY0 = int((y0 - yMin()) / ySize());
113  int iY1 = int((y1 - yMin()) / ySize());
114 
115 //
116 // cout << "x0,x1,y0,y1=" << x0 << "," << x1 << "," << y0 << "," << y1
117 // << "," << iY0 << "," << iY1 << endl;
118 //
119 
120  //...Sorting...
121  if (iY0 > iY1) {
122  const int tmp = iY0;
123  iY0 = iY1;
124  iY1 = tmp;
125  }
126 
127  //...Voting...
128  for (unsigned j = (unsigned) iY0; j < (unsigned)(iY1 + 1); j++) {
129  const unsigned k = i * nY() + j;
130  const unsigned b0 = k / 32;
131  const unsigned b1 = k % 32;
132  if (weight > 0)
133  _cell[b0] |= (1 << b1);
134  else
135  _cell[b0] &= (~(1 << b1));
136  }
137  }
138  }
139 
140  void
142  float ry,
143  float targetCharge,
144  int weight)
145  {
146 
147  const HepGeom::Point3D<double> r(rx, ry, 0);
148 
149  //...phi loop...
150  for (unsigned i = 0; i < nX(); i++) {
151  const float x0 = xSize() * float(i);
152  const HepGeom::Point3D<double> phi(cos(x0), sin(x0), 0);
153  float charge = r.cross(phi).z();
154  if (targetCharge != 0)
155  if (targetCharge * charge > 0)
156  continue;
157 
158  const float y0 = transformation().y(rx, ry, x0);
159  const float x1 = xSize() * float(i + 1);
160  const float y1 = transformation().y(rx, ry, x1);
161 
162  //...Location in the plane...
163  int iY0 = int((y0 - yMin()) / ySize());
164  int iY1 = int((y1 - yMin()) / ySize());
165 
166  //...This is special implementation for Circle Hough...
167  if (transformation().diverge(rx, ry, x0, x1)) {
168  if (iY0 > 0) {
169  if (iY0 >= (int) nY()) continue;
170  iY1 = nY() - 1;
171  } else {
172  if (iY1 >= (int) nY()) continue;
173  iY0 = iY1;
174  iY1 = nY() - 1;
175  }
176  }
177 
178  //...Sorting...
179  if (iY0 > iY1) {
180  const int tmp = iY0;
181  iY0 = iY1;
182  iY1 = tmp;
183  }
184 
185  //...Both out of region ?...
186  if (iY1 < 0) continue;
187  if (iY0 >= (int) nY()) continue;
188 
189  //...In region ?...
190  if (iY0 < 0) iY0 = 0;
191  if (iY0 >= (int) nY()) iY0 = nY() - 1;
192  // if (iY1 < 0) iY1 = 0; //redundant
193  if (iY1 >= (int) nY()) iY1 = nY() - 1;
194 
195  //...Voting...
196  for (unsigned j = (unsigned) iY0; j < (unsigned)(iY1 + 1); j++) {
197  const unsigned k = i * nY() + j;
198  const unsigned b0 = k / 32;
199  const unsigned b1 = k % 32;
200  if (weight > 0)
201  _cell[b0] |= (1 << b1);
202  else
203  _cell[b0] &= (~(1 << b1));
204  }
205  }
206  }
207 
208  void
210  {
211 
212  //...Check status...
213  if (_patterns[id]) {
214  std::cout << "TRGCDCHoughPlaneBoolean::registerPattern !!! "
215  << "a pattern(id=" << id << ") was already registered"
216  << std::endl;
217  return;
218  }
219  if (id >= _nPatterns) {
220  std::cout << "TRGCDCHoughPlaneBoolean::registerPattern !!! "
221  << "given id(" << id << ") is over range(" << _nPatterns << ")"
222  << std::endl;
223  return;
224  }
225  const unsigned n = nX() * nY();
226 
227  //...Check # of active cells...
228  unsigned nActive = 0;
229  for (unsigned i = 0; i < n; i++) {
230  const unsigned j = i / 32;
231  const unsigned k = i % 32;
232  if ((_cell[j] >> k) & 1) {
233  ++nActive;
234  _reverse[i].push_back(id);
235  }
236  }
237  _nActive[id] = nActive;
238 
239  //...Create an array...
240  _patterns[id] = new unsigned[nActive];
241 
242  //...Store them...
243  unsigned l = 0;
244  for (unsigned i = 0; i < n; i++) {
245  const unsigned j = i / 32;
246  const unsigned k = i % 32;
247  if ((_cell[j] >> k) & 1) {
248  _patterns[id][l] = i;
249  l++;
250  }
251  }
252  }
253 
254  void
256  {
257  if (_nPatterns) {
258  std::cout << "TRGCDCHoughPlaneBoolean::preparePatterns !!! "
259  << "patterns were already allocated" << std::endl;
260  return;
261  }
262 
263  _nPatterns = nPatterns;
264  _patterns = new unsigned * [_nPatterns];
265  _nActive = new unsigned[_nPatterns];
266  memset(_patterns, 0, _nPatterns * sizeof(unsigned*));
267  _reverse = new vector<unsigned>[nX() * nY()];
268  }
269 
270  void
271  TRGCDCHoughPlaneBoolean::vote(unsigned patternId, int weight)
272  {
273  const unsigned n = _nActive[patternId];
274  for (unsigned i = 0; i < n; i++) {
275  add(_patterns[patternId][i], weight);
276  }
277  }
278 
280 } // namespace Belle2
A class to represent a Hough parameter plane.
unsigned _nPatterns
number of patterns
std::vector< unsigned > * _reverse
Pattern ID's for each cell.
unsigned * _nActive
number of active cells
An abstract class to represent a Hough transformation.
virtual float y(float xReal, float yReal, float x) const =0
returns Y coordinate in a Hough parameter plane.
void registerPattern(unsigned id) override
registers a pattern..
float ySize(void) const
returns size of y bin.
void voteUsedInTrasan(float rx, float ry, float charge, int weight=1)
Votes with charge decision.
unsigned nY(void) const
return # of y bins.
void add(unsigned cellId, int weight) override
Add to a cell.
void preparePatterns(unsigned nPatterns)
allocate memory for patterns.
void vote(float rx, float ry, int weight=1) override
Votes.
float charge(void) const
returns charge for this plane.
void id(unsigned serialId, unsigned &x, unsigned &y) const
returns x and y for serialID.
float yMax(void) const
returns max. of y.
const std::vector< unsigned > & patternId(unsigned cellId) const
returns pattern ID which activates specified cell.
void clear(void) override
clear all entries.
const TRGCDCHoughTransformation & transformation(void) const
returns Hough transformation object.
float xSize(void) const
returns size of x bin.
unsigned nX(void) const
returns # of x bins.
float yMin(void) const
returns min. of y.
virtual ~TRGCDCHoughPlaneBoolean()
Destructor.
Abstract base class for different kinds of events.