Belle II Software  release-08-01-10
DisplayDrawingAreaHough.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 display CDC trigger objects.
11 //-----------------------------------------------------------------------------
12 
13 #ifdef TRGCDC_DISPLAY
14 
15 #define TRGCDC_SHORT_NAMES
16 
17 #include <iostream>
18 #include <pangomm/init.h>
19 #include "trg/trg/Utilities.h"
20 #include "trg/cdc/TRGCDC.h"
21 #include "trg/cdc/Wire.h"
22 #include "trg/cdc/WireHit.h"
23 #include "trg/cdc/Segment.h"
24 #include "trg/cdc/FrontEnd.h"
25 #include "trg/cdc/Merger.h"
26 #include "trg/cdc/HoughPlane.h"
27 #include "trg/cdc/HoughPlaneMulti2.h"
28 #include "trg/cdc/Display.h"
29 #include "trg/cdc/DisplayRphi.h"
30 #include "trg/cdc/DisplayDrawingAreaHough.h"
31 
32 using namespace std;
33 
34 namespace Belle2 {
40  TRGCDCDisplayDrawingAreaHough::TRGCDCDisplayDrawingAreaHough(
41  TRGCDCDisplay& w,
42  int size)
43  : TRGCDCDisplayDrawingArea(w, size, 10),
44  _scale(1),
45  _wireName(false),
46  _oldCDC(false),
47  _x(0),
48  _y(0),
49  _scaleX(0), _scaleY(0),
50  _winx(0), _winy(0), _winw(0), _winh(0), _wind(0),
51  _hp(0),
52 
53  _blue(Gdk::Color("blue")),
54  _red(Gdk::Color("red")),
55  _green(Gdk::Color("green")),
56  _black(Gdk::Color("black")),
57  _white(Gdk::Color("white")),
58  _gray(Gdk::Color("gray")),
59  _yellow(Gdk::Color("yellow")),
60 
61  _gray0(Gdk::Color("gray90")),
62  _gray1(Gdk::Color("gray80")),
63  _gray2(Gdk::Color("gray70")),
64  _gray3(Gdk::Color("gray60"))
65  {
66 
67 
68  Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
69  colormap->alloc_color(_blue);
70  colormap->alloc_color(_red);
71  colormap->alloc_color(_green);
72  colormap->alloc_color(_black);
73  colormap->alloc_color(_white);
74  colormap->alloc_color(_gray);
75  colormap->alloc_color(_yellow);
76  colormap->alloc_color(_gray0);
77  colormap->alloc_color(_gray1);
78  colormap->alloc_color(_gray2);
79  colormap->alloc_color(_gray3);
80 
81  add_events(Gdk::EXPOSURE_MASK | Gdk::BUTTON_PRESS_MASK);
82  }
83 
84  TRGCDCDisplayDrawingAreaHough::~TRGCDCDisplayDrawingAreaHough()
85  {
86  }
87 
88  void
89  TRGCDCDisplayDrawingAreaHough::on_realize()
90  {
91  Gtk::DrawingArea::on_realize();
92  _window = get_window();
93  _window->get_geometry(_winx, _winy, _winw, _winh, _wind);
94  if (_hp) {
95  _x = _hp->nX() / 2;
96  _y = _hp->nY() / 2;
97  }
98  _gc = Gdk::GC::create(_window);
99  _window->set_background(_white);
100  _window->clear();
101  }
102 
103  bool
104  TRGCDCDisplayDrawingAreaHough::on_expose_event(GdkEventExpose*)
105  {
106  Glib::RefPtr<Gdk::Window> window = get_window();
107  window->get_geometry(_winx, _winy, _winw, _winh, _wind);
108  window->clear();
109  draw();
110  return true;
111  }
112 
113  bool
114  TRGCDCDisplayDrawingAreaHough::on_button_press_event(GdkEventButton* e)
115  {
116 
117  //...Clear window as a default...
118  on_expose_event((GdkEventExpose*) NULL);
119 
120  //...HP there?...
121  if (! _hp) return true;
122 
123  //...Get cell ID...
124  const int cx0 = int(float(e->x) / _scaleX / _hp->xSize());
125  const int cy0 = _hp->nY() - int(float(e->y) / _scaleY / _hp->ySize());
126  const unsigned sid = _hp->serialId(cx0, cy0);
127 
128  //...Get entries...
129  const unsigned n = _hp->entry(cx0, cy0);
130 
131  //...Information...
132  display().information("Cell(" + TRGUtilities::itostring(cx0) + "," +
133  TRGUtilities::itostring(cy0) + ", sid" +
134  TRGUtilities::itostring(sid) + ") " +
135  TRGUtilities::itostring(n) + "hits");
136 
137  //...Draw cell...
138  _gc->set_foreground(_red);
139 // drawCell(cx0, cy0);
140  vector<unsigned> r;
141  r.push_back(sid);
142  drawRegion(r);
143 
144  //...Draw TSs if rphi display exists...
145  if (display().rphi()) {
146  const TRGCDC& cdc = * TRGCDC::getTRGCDC();
147  const TCHPlaneMulti2& hp =
148  * dynamic_cast<const TCHPlaneMulti2*>(_hp);
149  vector<const TCSegment*> list;
150  string segs;
151  for (unsigned i = 0; i < 5; i++) {
152  const vector<unsigned>& l = hp.patternId(i, sid);
153  for (unsigned j = 0; j < l.size(); j++) {
154  list.push_back(& cdc.segment(i * 2, l[j]));
155  if (list.size() == 1) {
156  segs = "s" + cdc.segment(i * 2, l[j]).name();
157  } else {
158  segs += ",s" + cdc.segment(i * 2, l[j]).name();
159  }
160  }
161  }
162  display().rphi()->information(segs);
163  display().rphi()->area().on_expose_event(0);
164  display().rphi()->area().oneShot(list, _red);
165  }
166 
167  //...Debug...
168  _x = xR(e->x);
169  _y = yR(e->y);
170  cout << "***********************" << endl;
171  cout << "x=" << e->x << ",y=" << e->y << endl;
172  cout << "cx0=" << cx0 << ",cy0=" << cy0 << endl;
173  cout << "************************" << endl;
174 
175  return true;
176  }
177 
178  void
179  TRGCDCDisplayDrawingAreaHough::draw(void)
180  {
181  if (! _hp) return;
182 
183  Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
184  _gc->set_line_attributes(1,
185  Gdk::LINE_SOLID,
186  Gdk::CAP_NOT_LAST,
187  Gdk::JOIN_MITER);
188 
189  //...Cal. scale for y coordinate...
190  const float xMin = _hp->xMin();
191  const float xMax = _hp->xMax();
192  const float yMin = _hp->yMin();
193  const float yMax = _hp->yMax();
194 // const float scaleX = 1 / (xMax - xMin) * _winw;
195 // const float scaleY = 1 / (yMax - yMin) * _winh;
196  _scaleX = _scale / (xMax - xMin) * _winw;
197  _scaleY = _scale / (yMax - yMin) * _winh;
198 
199  //...Search maximum...
200  unsigned nMax = 0;
201  for (unsigned i = 0; i < _hp->nX(); i++) {
202  for (unsigned j = 0; j < _hp->nY(); j++) {
203  const unsigned n = _hp->entry(i, j);
204  if (n > nMax) nMax = n;
205  }
206  }
207 
208  if (nMax == 0)
209  cout << "max entry=" << nMax << endl;
210 
211  //...Draw...
212  for (unsigned i = 0; i < _hp->nX(); i++) {
213  for (unsigned j = 0; j < _hp->nY(); j++) {
214  const unsigned n = _hp->entry(i, j);
215  if (! n) continue;
216 
217  //...Decide color...
218  if (n == 1)
219  _gc->set_foreground(_gray0);
220  else if (n == 2)
221  _gc->set_foreground(_gray1);
222  else if (n == 3)
223  _gc->set_foreground(_gray2);
224  else if (n == 4)
225  _gc->set_foreground(_blue);
226  else
227  _gc->set_foreground(_red);
228 
229  drawCell(i, j);
230  }
231  }
232 
233  //...Draw boundary...
234  const int x0 = 0;
235  const int y0 = 0;
236  const int z0 = toY(y0);
237  const int x1 = int(_hp->xSize() * _scaleX);
238  const int y1 = int(_hp->ySize() * _scaleY);
239  _gc->set_foreground(_gray0);
240  _window->draw_rectangle(_gc, true, x0, z0, x1, y1);
241 
242 // cout << "TWHDArea ... xMin,xMax,yMin,yMax=" << xMin << "," << xMax
243 // << "," << yMin << "," << yMax << endl;
244 // cout << "TWHDArea ... winx,winy,winw,winh,wind=" << _winx << "," << _winy
245 // << "," << _winw << "," << _winh << "," << _wind << endl;
246 
247  //...Draw regions...
248  _gc->set_foreground(_green);
249  _gc->set_line_attributes(2,
250  Gdk::LINE_SOLID,
251  Gdk::CAP_NOT_LAST,
252  Gdk::JOIN_MITER);
253  const vector<vector<unsigned> *>& regions = _hp->regions();
254  for (unsigned i = 0; i < (unsigned) regions.size(); i++)
255  drawRegion(regions[i]);
256 
257  //...Draw text...
258 // _xPositionText = _hp->xMin() + _hp->xSize() * _hp->nX() * 0.1;
259 // _yPositionText = _hp->yMin() + _hp->ySize() * _hp->nY() * 0.9 * scale;
260 // _window.draw_text(_xPositionText, _yPositionText, _text.c_str());
261 // string text = " y_scale=" + dtostring(1/scale);
262 // text += " max.peak=" + itostring(_hp->maxEntry());
263 // text += " #regions=" + itostring(_hp->regions().length());
264 // _yPositionText = _hp->yMin() + _hp->ySize() * _hp->nY() * 0.85 * scale;
265 // _window.draw_text(_xPositionText, _yPositionText, text.c_str());
266 
267  //...Draw reference line(CDC outer wall)...
268 // const float outerWall = log10(88. / 2.);
269 // const float yOuterWall = outerWall * scale;
270 // _window.draw_segment(xMin, yOuterWall, xMax, yOuterWall, leda_gray2);
271 
272 // on_expose_event((GdkEventExpose *) NULL);
273  }
274 
275  void
276  TRGCDCDisplayDrawingAreaHough::resetPosition(void)
277  {
278  _x = _winw / 2;
279  _y = _winh / 2;
280  }
281 
282  int
283  TRGCDCDisplayDrawingAreaHough::xT(double x) const
284  {
285  const double xx = x - _x + double(_hp->nX()) / 2;
286  return int(xx * _hp->xSize() * _scaleX);
287  }
288 
289  int
290  TRGCDCDisplayDrawingAreaHough::yT(double y) const
291  {
292  const double yy = y - _y + double(_hp->nY()) / 2;
293  return - int(yy * _hp->ySize() * _scaleY) + _winh;
294  }
295 
296  int
297  TRGCDCDisplayDrawingAreaHough::xR(double x) const
298  {
299  return int(x / _hp->xSize() * _scaleX);
300  }
301 
302  int
303  TRGCDCDisplayDrawingAreaHough::yR(double y) const
304  {
305  return - int((y - double(_winh)) / _hp->ySize() / _scaleY);
306  }
307 
308  int
309  TRGCDCDisplayDrawingAreaHough::drawCell(unsigned xCell, unsigned yCell)
310  {
311  const float x = float(xCell) * _hp->xSize() * _scaleX;
312  const float y = float(yCell) * _hp->ySize() * _scaleY;
313 
314  const int x0 = int(x);
315  const int y0 = int(y);
316  const int z0 = toY(y0);
317  const int x1 = int(x + _hp->xSize() * _scaleX) - x0;
318  const int y1 = int(y + _hp->ySize() * _scaleY) - y0;
319 
320  _window->draw_rectangle(_gc, true, x0, z0, x1, y1);
321 
322 // cout << "x0,y0,z0,x1,y1=" << x0 << ","
323 // << y0 << "," << z0 << "," << x1 << ","
324 // << y1 << endl;
325 
326  return 0;
327  }
328 
329  int
330  TRGCDCDisplayDrawingAreaHough:: drawRegion(const std::vector<unsigned>& r)
331  {
332  for (unsigned i = 0; i < r.size(); i++) {
333  const unsigned id = r[i];
334  unsigned ix = 0;
335  unsigned iy = 0;
336  _hp->id(id, ix, iy);
337 
338  const float x = float(ix) * _hp->xSize() * _scaleX;
339  const float y = float(iy) * _hp->ySize() * _scaleY;
340 
341  const int x0 = int(x);
342  const int y0 = int(y);
343  const int z0 = toY(y0);
344 
345  const int x1 = int(x + _hp->xSize() * _scaleX);
346  const int y1 = int(y + _hp->ySize() * _scaleY) - y0;
347  const int z1 = z0 + y1;
348 
349  for (unsigned k = 0; k < 8; k++) {
350  if (k % 2) continue;
351  unsigned idx = _hp->neighbor(id, k);
352 
353  if (idx == id) continue;
354  bool found = false;
355  for (unsigned l = 0; l < (unsigned) r.size(); l++) {
356  if (idx == r[l]) {
357  found = true;
358  break;
359  }
360  }
361  if (found) continue;
362  if (k == 0)
363  _window->draw_line(_gc, x0, z0, x1, z0);
364  else if (k == 2)
365  _window->draw_line(_gc, x1, z0, x1, z1);
366  else if (k == 4)
367  _window->draw_line(_gc, x0, z1, x1, z1);
368  else if (k == 6)
369  _window->draw_line(_gc, x0, z0, x0, z1);
370  }
371  }
372 
373  return 0;
374  }
375 
377 } // namespace Belle2
378 
379 #endif
Abstract base class for different kinds of events.