Belle II Software development
DisplayDrawingAreaRphi.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 <cstdlib>
18#include <iostream>
19#include <pangomm/init.h>
20#include "trg/trg/Constants.h"
21#include "trg/cdc/TRGCDC.h"
22#include "trg/cdc/Link.h"
23#include "trg/cdc/Wire.h"
24#include "trg/cdc/WireHit.h"
25#include "trg/cdc/Segment.h"
26#include "trg/cdc/SegmentHit.h"
27#include "trg/cdc/Circle.h"
28#include "trg/cdc/TRGCDCTrack.h"
29#include "trg/cdc/FrontEnd.h"
30#include "trg/cdc/Merger.h"
31#include "trg/cdc/DisplayDrawingAreaRphi.h"
32#include "cdc/dataobjects/CDCSimHit.h"
33
34using namespace std;
35
36namespace Belle2 {
42 TRGCDCDisplayDrawingAreaRphi::TRGCDCDisplayDrawingAreaRphi(TRGCDCDisplay& w,
43 int size,
44 double innerR,
45 double outerR)
46 : TRGCDCDisplayDrawingArea(w, size, outerR),
47 _scale(double(size) / outerR / 2),
48 _axial(true),
49 _stereo(false),
50 _wireName(false),
51 _oldCDC(false),
52 _x(0),
53 _y(0),
54 _winx(0), _winy(0), _winw(0), _winh(0), _wind(0),
55 _innerR(innerR),
56 _outerR(outerR),
57
58 _blue(Gdk::Color("blue")),
59 _red(Gdk::Color("red")),
60 _green(Gdk::Color("green")),
61 _black(Gdk::Color("black")),
62 _white(Gdk::Color("white")),
63 _grey(Gdk::Color("grey")),
64 _yellow(Gdk::Color("yellow")),
65 _grey0(Gdk::Color("gray90"))
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(_grey);
75 colormap->alloc_color(_yellow);
76
77 Pango::init();
78 Glib::ustring wn = "unknown";
79 _pl = create_pango_layout(wn);
80
81 add_events(Gdk::EXPOSURE_MASK | Gdk::BUTTON_PRESS_MASK);
82 }
83
84 TRGCDCDisplayDrawingAreaRphi::~TRGCDCDisplayDrawingAreaRphi()
85 {
86 }
87
88 void
89 TRGCDCDisplayDrawingAreaRphi::on_realize()
90 {
91 Gtk::DrawingArea::on_realize();
92 _window = get_window();
93 _gc = Gdk::GC::create(_window);
94 _window->set_background(_white);
95 _window->clear();
96 }
97
98 bool
99 TRGCDCDisplayDrawingAreaRphi::on_expose_event(GdkEventExpose*)
100 {
101 Glib::RefPtr<Gdk::Window> window = get_window();
102 window->get_geometry(_winx, _winy, _winw, _winh, _wind);
103 window->clear();
104 drawCDC();
105 draw();
106 return true;
107 }
108
109 bool
110 TRGCDCDisplayDrawingAreaRphi::on_button_press_event(GdkEventButton* e)
111 {
112 _x = xR(e->x);
113 _y = yR(- e->y);
114 on_expose_event((GdkEventExpose*) NULL);
115 return true;
116 }
117
118 void
119 TRGCDCDisplayDrawingAreaRphi::drawCDC(void)
120 {
121
122 //...Axis...
123 _gc->set_foreground(_grey);
124 _gc->set_line_attributes(1,
125 Gdk::LINE_SOLID,
126 Gdk::CAP_NOT_LAST,
127 Gdk::JOIN_MITER);
128 _window->draw_line(_gc,
129 x(- _outerR),
130 y(0),
131 x(_outerR),
132 y(0));
133 _window->draw_line(_gc,
134 x(0),
135 y(- _outerR),
136 x(0),
137 y(_outerR));
138
139 //...CDC...
140 _gc->set_foreground(_grey);
141 _gc->set_line_attributes(1,
142 Gdk::LINE_SOLID,
143 Gdk::CAP_NOT_LAST,
144 Gdk::JOIN_MITER);
145 _window->draw_arc(_gc,
146 0,
147 x(- _innerR),
148 y(_innerR),
149 int(2 * _innerR * _scale),
150 int(2 * _innerR * _scale),
151 0,
152 360 * 64);
153 _window->draw_arc(_gc,
154 0,
155 x(- _outerR),
156 y(_outerR),
157 int(2 * _outerR * _scale),
158 int(2 * _outerR * _scale),
159 0,
160 360 * 64);
161
162 //...Belle CDC...
163 if (_oldCDC) {
164 const double innerR = 118;
165 const double outerR = 880;
166 _gc->set_foreground(_grey);
167 _gc->set_line_attributes(1,
168 Gdk::LINE_ON_OFF_DASH,
169 Gdk::CAP_NOT_LAST,
170 Gdk::JOIN_MITER);
171 _window->draw_arc(_gc,
172 0,
173 x(- innerR),
174 y(innerR),
175 int(2 * innerR * _scale),
176 int(2 * innerR * _scale),
177 0,
178 360 * 64);
179 _window->draw_arc(_gc,
180 0,
181 x(- outerR),
182 y(outerR),
183 int(2 * outerR * _scale),
184 int(2 * outerR * _scale),
185 0,
186 360 * 64);
187 }
188 }
189
190 void
191 TRGCDCDisplayDrawingAreaRphi::draw(void)
192 {
193 drawHits();
194 for (unsigned i = 0; i < _segments.size(); i++)
195 drawSegment(* _segments[i],
196 1,
197 _segmentsColor[i],
198 Gdk::LINE_SOLID);
199 for (unsigned i = 0; i < _segmentHits.size(); i++)
200 drawSegment(_segmentHits[i]->segment(),
201 1,
202 _segmentHitsColor[i],
203 Gdk::LINE_SOLID);
204 for (unsigned i = 0; i < _circles.size(); i++)
205 drawCircle(* _circles[i], 1, _circlesColor[i], Gdk::LINE_SOLID);
206 for (unsigned i = 0; i < _tracks.size(); i++)
207 drawTrack(* _tracks[i], 1, _tracksColor[i], Gdk::LINE_SOLID);
208 for (unsigned i = 0; i < _fronts.size(); i++)
209 drawFrontEnd(* _fronts[i],
210 1,
211 _frontColors[i],
212 Gdk::LINE_SOLID);
213
214 for (unsigned i = 0; i < _mergers.size(); i++)
215 drawMerger(* _mergers[i],
216 1,
217 _mergerColors[i],
218 Gdk::LINE_SOLID);
219 drawWires();
220 }
221
222 void
223 TRGCDCDisplayDrawingAreaRphi::drawWires(void)
224 {
225 for (unsigned i = 0; i < _wires.size(); i++)
226 drawWire(* _wires[i], 1, _wiresColor[i], Gdk::LINE_SOLID);
227 }
228
229 void
230 TRGCDCDisplayDrawingAreaRphi::drawHits(void)
231 {
232 Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
233 const unsigned n = _hits.size();
234 for (unsigned i = 0; i < n; i++) {
235 if (! _stereo)
236 if (_hits[i]->wire().stereo())
237 continue;
238 if (! _axial)
239 if (_hits[i]->wire().axial())
240 continue;
241
242 //...Points...
243 const TCWire& w = _hits[i]->wire();
244 const HepGeom::Point3D<double>& p = w.backwardPosition();
245 // In ns scale. Actually in length scale. cm
246 double t_driftTime = _hits[i]->drift();
247 // To show in display when driftTime is 0.
248 if (t_driftTime == 0) t_driftTime = 0.3;
249 if (t_driftTime < 0) {
250 t_driftTime = 0.3;
251 cout << "[DisplayDrawingAreaRphi] Error. DriftTime is minus." << endl;
252 }
253 if (t_driftTime > 2) {
254 t_driftTime = 0.3;
255 cout << "[DisplayDrawingAreaRphi] Error. DriftTime is larger than 800 ns." << endl;
256 }
257 //const double radius = _hits[i]->drift();
258 //t_driftTime = 0.3;
259 const double radius = t_driftTime;
260
261 colormap->alloc_color(_hitsColor[i]);
262 _gc->set_foreground(_hitsColor[i]);
263 _gc->set_line_attributes(1,
264 Gdk::LINE_SOLID,
265 Gdk::CAP_NOT_LAST,
266 Gdk::JOIN_MITER);
267// _window->draw_arc(_gc,
268// 0,
269// x((p.x() - radiusMC) * 10),
270// y((p.y() + radiusMC) * 10),
271// int(2 * radiusMC * 10 * _scale),
272// int(2 * radiusMC * 10 * _scale),
273// 0,
274// 360 * 64);
275 _window->draw_arc(_gc,
276 0,
277 x((p.x() - radius) * 10),
278 y((p.y() + radius) * 10),
279 int(2 * radius * 10 * _scale),
280 int(2 * radius * 10 * _scale),
281 0,
282 360 * 64);
283 if (_wireName) {
284 Glib::ustring wn = _hits[i]->wire().name().c_str();
285 _pl->set_text(wn);
286 _window->draw_layout(_gc, x(p.x() * 10.), y(p.y() * 10.), _pl);
287 }
288 colormap->free_color(_hitsColor[i]);
289 }
290 }
291
292 void
293 TRGCDCDisplayDrawingAreaRphi::drawWire(const TCWire& w,
294 int lineWidth,
295 Gdk::Color& c,
296 Gdk::LineStyle s)
297 {
298
299 if (! _stereo)
300 if (w.stereo())
301 return;
302 if (! _axial)
303 if (w.axial())
304 return;
305
306 Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
307 colormap->alloc_color(c);
308 _gc->set_foreground(c);
309 _gc->set_line_attributes(lineWidth,
310 s,
311 Gdk::CAP_NOT_LAST,
312 Gdk::JOIN_MITER);
313
314 //...Cell shape...
315 Gdk::Point p0;
316 vector<Gdk::Point> points;
317 const unsigned nDivisions = 5;
318 const float ri = w.layer().innerRadius();
319 const float ro = w.layer().outerRadius();
320 const float cPhi = w.backwardPosition().phi();
321 const float dPhi = M_PI / w.layer().nCells();
322 for (unsigned j = 0; j < nDivisions + 1; j++) { // inner
323 const float phi = cPhi - dPhi
324 + (2 * dPhi / float(nDivisions)) * float(j);
325 const float xx = ri * cos(phi) * 10;
326 const float yy = ri * sin(phi) * 10;
327 if (j == 0) {
328 p0.set_x(x(xx));
329 p0.set_y(y(yy));
330 }
331 points.push_back(Gdk::Point(x(xx), y(yy)));
332 }
333 for (unsigned j = 0; j < nDivisions + 1; j++) { // outer
334 const float phi = cPhi + dPhi
335 - (2 * dPhi / float(nDivisions)) * float(j);
336 const float xx = ro * cos(phi) * 10;
337 const float yy = ro * sin(phi) * 10;
338 points.push_back(Gdk::Point(x(xx), y(yy)));
339 }
340 points.push_back(p0);
341
342 _gc->set_foreground(c);
343 _window->draw_lines(_gc, points);
344 }
345
346 void
347 TRGCDCDisplayDrawingAreaRphi::drawSegment(const TCSegment& w,
348 int lineWidth,
349 Gdk::Color& c,
350 Gdk::LineStyle s)
351 {
352 const vector<const TCWire*> wires = w.wires();
353 const unsigned n = wires.size();
354 for (unsigned i = 0; i < n; i++) {
355 drawWire(* wires[i], lineWidth, c, s);
356 }
357
358// Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
359// const double radiusTDC = w.timing().time() *
360 }
361
362 void
363 TRGCDCDisplayDrawingAreaRphi::drawCircle(const TCCircle& t,
364 int,
365 Gdk::Color& c,
366 Gdk::LineStyle)
367 {
368
369 Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
370 colormap->alloc_color(c);
371 _gc->set_foreground(c);
372 _gc->set_line_attributes(1,
373 Gdk::LINE_SOLID,
374 Gdk::CAP_NOT_LAST,
375 Gdk::JOIN_MITER);
376
377 //...Draw a circle...
378 const TRGPoint2D& h = t.center();
379 double radius = fabs(t.radius());
380 _window->draw_arc(_gc,
381 0,
382 x((h.x() - radius) * 10),
383 y((h.y() + radius) * 10),
384 int(2 * radius * 10 * _scale),
385 int(2 * radius * 10 * _scale),
386 0,
387 360 * 64);
388 colormap->free_color(c);
389
390 //...Draw text...
391 //_window.draw_text(_xPositionText, _yPositionText, _text.c_str());
392 }
393
394 void
395 TRGCDCDisplayDrawingAreaRphi::drawTrack(const TCTrack& t,
396 int lineWidth,
397 Gdk::Color& c,
398 Gdk::LineStyle s)
399 {
400
401 Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
402 colormap->alloc_color(c);
403 _gc->set_foreground(c);
404 _gc->set_line_attributes(1,
405 Gdk::LINE_SOLID,
406 Gdk::CAP_NOT_LAST,
407 Gdk::JOIN_MITER);
408
409 const TRGCDC& cdc = * TRGCDC::getTRGCDC();
410
411 //...Draw segments first...
412 for (unsigned i = 0; i < cdc.nSuperLayers(); i++) {
413 const vector<TCLink*>& links = t.links(i);
414 for (unsigned j = 0; j < links.size(); j++) {
415 const TCLink& l = * links[j];
416 const TCSegment* seg = dynamic_cast<const TCSegment*>(l.cell());
417
418 if (seg)
419 drawSegment(* seg,
420 lineWidth,
421 c,
422 s);
423 _window->draw_line(_gc,
424 x(l.positionOnTrack().x() * 10),
425 y(l.positionOnTrack().y() * 10),
426 x(l.positionOnWire().x() * 10),
427 y(l.positionOnWire().y() * 10));
428 }
429 }
430
431 //...Draw a track...
432 TCHelix hIp = t.helix();
433 hIp.pivot(ORIGIN);
434 const HepGeom::Point3D<double>& h = hIp.center();
435 const double radius = fabs(hIp.radius());
436 const HepGeom::Point3D<double> pIn =
437 TCLink::innerMost(t.links())->positionOnTrack() - h;
438 const HepGeom::Point3D<double> pOut =
439 TCLink::outerMost(t.links())->positionOnTrack() - h;
440 double a0 = atan2(pIn.y(), pIn.x()) / M_PI * 180;
441 double a1 = atan2(pOut.y(), pOut.x()) / M_PI * 180;
442 double d = a1 - a0;
443 if (d > 180) d -= 360;
444 else if (d < -180) d += 360;
445 _window->draw_arc(_gc,
446 0,
447 x((h.x() - radius) * 10),
448 y((h.y() + radius) * 10),
449 int(2 * radius * 10 * _scale),
450 int(2 * radius * 10 * _scale),
451 int(a0 * 64),
452 int(d * 64));
453
454 cout << "a0,d=" << a0 << "," << d << endl;
455
456 colormap->free_color(c);
457 }
458
459 void
460 TRGCDCDisplayDrawingAreaRphi::drawFrontEnd(const TCFrontEnd& w,
461 int lineWidth,
462 Gdk::Color& c,
463 Gdk::LineStyle s)
464 {
465 const unsigned n = w.size();
466 for (unsigned i = 0; i < n; i++) {
467 drawWire(* w[i], lineWidth, c, s);
468 }
469 }
470
471 void
472 TRGCDCDisplayDrawingAreaRphi::drawMerger(const TCMerger& w,
473 int lineWidth,
474 Gdk::Color& c,
475 Gdk::LineStyle s)
476 {
477 const unsigned n = w.size();
478 for (unsigned i = 0; i < n; i++) {
479 drawFrontEnd(* w[i], lineWidth, c, s);
480 }
481 }
482
483 void
484 TRGCDCDisplayDrawingAreaRphi::resetPosition(void)
485 {
486 if (_winw < _winh)
487 _scale = double(_winw) / _outerR / 2;
488 else
489 _scale = double(_winh) / _outerR / 2;
490
491 _x = _y = 0;
492 on_expose_event((GdkEventExpose*) NULL);
493 }
494
495 void
496 TRGCDCDisplayDrawingAreaRphi::append(const vector<const TCWire*>& l,
497 Gdk::Color c)
498 {
499 const unsigned n = l.size();
500 for (unsigned i = 0; i < n; i++) {
501 _wires.push_back(l[i]);
502 _wiresColor.push_back(c);
503 }
504 on_expose_event((GdkEventExpose*) NULL);
505 }
506
507 void
508 TRGCDCDisplayDrawingAreaRphi::append(const vector<const TCWHit*>& l,
509 Gdk::Color c)
510 {
511 const unsigned n = l.size();
512 for (unsigned i = 0; i < n; i++) {
513 _hits.push_back(l[i]);
514 _hitsColor.push_back(c);
515 }
516 on_expose_event((GdkEventExpose*) NULL);
517 }
518
519 void
520 TRGCDCDisplayDrawingAreaRphi::append(const TCSegment& s,
521 Gdk::Color c)
522 {
523 _segments.push_back(& s);
524 _segmentsColor.push_back(c);
525 on_expose_event((GdkEventExpose*) NULL);
526 }
527
528 void
529 TRGCDCDisplayDrawingAreaRphi::append(const vector<const TCSegment*>& l,
530 Gdk::Color c)
531 {
532 const unsigned n = l.size();
533 for (unsigned i = 0; i < n; i++) {
534 _segments.push_back(l[i]);
535 _segmentsColor.push_back(c);
536 }
537 on_expose_event((GdkEventExpose*) NULL);
538 }
539
540 void
541 TRGCDCDisplayDrawingAreaRphi::append(
542 const vector<const TCSHit*>& l,
543 Gdk::Color c)
544 {
545 const unsigned n = l.size();
546 for (unsigned i = 0; i < n; i++) {
547 _segmentHits.push_back(l[i]);
548 _segmentHitsColor.push_back(c);
549 }
550 on_expose_event((GdkEventExpose*) NULL);
551 }
552
553 void
554 TRGCDCDisplayDrawingAreaRphi::append(const TCCircle& s,
555 Gdk::Color c)
556 {
557 _circles.push_back(& s);
558 _circlesColor.push_back(c);
559 on_expose_event((GdkEventExpose*) NULL);
560 }
561
562 void
563 TRGCDCDisplayDrawingAreaRphi::append(const vector<const TCCircle*>& l,
564 Gdk::Color c)
565 {
566 const unsigned n = l.size();
567 for (unsigned i = 0; i < n; i++) {
568 _circles.push_back(l[i]);
569 _circlesColor.push_back(c);
570 }
571 on_expose_event((GdkEventExpose*) NULL);
572 }
573
574 void
575 TRGCDCDisplayDrawingAreaRphi::append(const TCTrack& s,
576 Gdk::Color c)
577 {
578 _tracks.push_back(& s);
579 _tracksColor.push_back(c);
580 on_expose_event((GdkEventExpose*) NULL);
581 }
582
583 void
584 TRGCDCDisplayDrawingAreaRphi::append(const vector<const TCTrack*>& l,
585 Gdk::Color c)
586 {
587 const unsigned n = l.size();
588 for (unsigned i = 0; i < n; i++) {
589 _tracks.push_back(l[i]);
590 _tracksColor.push_back(c);
591 }
592 on_expose_event((GdkEventExpose*) NULL);
593 }
594
595 void
596 TRGCDCDisplayDrawingAreaRphi::append(const TCFrontEnd& s,
597 Gdk::Color c)
598 {
599 _fronts.push_back(& s);
600 _frontColors.push_back(c);
601 on_expose_event((GdkEventExpose*) NULL);
602 }
603
604 void
605 TRGCDCDisplayDrawingAreaRphi::append(const TCMerger& s,
606 Gdk::Color c)
607 {
608 _mergers.push_back(& s);
609 _mergerColors.push_back(c);
610 on_expose_event((GdkEventExpose*) NULL);
611 }
612
613 void
614 TRGCDCDisplayDrawingAreaRphi::clear(void)
615 {
616 _wires.clear();
617 _wiresColor.clear();
618 _hits.clear();
619 _hitsColor.clear();
620 _segments.clear();
621 _segmentsColor.clear();
622 _segmentHits.clear();
623 _segmentHitsColor.clear();
624 _circles.clear();
625 _circlesColor.clear();
626 _tracks.clear();
627 _tracksColor.clear();
628 _fronts.clear();
629 _frontColors.clear();
630 _mergers.clear();
631 _mergerColors.clear();
632 }
633
634 void
635 TRGCDCDisplayDrawingAreaRphi::oneShot(const vector<const TCSegment*>& l,
636 Gdk::Color color)
637 {
638 for (unsigned i = 0; i < l.size(); i++)
639 drawSegment(* l[i], 1, color, Gdk::LINE_SOLID);
640 }
641
643} // namespace Belle2
644
645#endif
Abstract base class for different kinds of events.
STL namespace.