raygrid_draw.h
Go to the documentation of this file.
1 // This is NOT a real header. It's code shared between a few tests in
2 // an ugly way. It should be #include'd before main().
3 
4 #include "TCanvas.h"
5 #include "TMarker.h"
6 #include "TText.h"
7 #include "TLatex.h"
8 #include "TLine.h"
9 #include "TPolyLine.h"
10 #include "TArrow.h"
11 #include "TH1F.h"
12 #include "TStyle.h"
13 
14 void draw_text(const Point& pt, const std::string text, int color=1, int align=22);
15 
16 void dump(const blobs_t& blobs)
17 {
18  info("-----dumping {} blobs:", blobs.size());
19  for (const auto& b : blobs) {
20  info("\t{}",b.as_string());
21  }
22  info("------");
23 }
24 
25 void draw_point(const Point& p, float size=1, int style=20, int color=1);
26 void draw_point(const Point& p, float size, int style, int color)
27 {
28  TMarker m;
29  m.SetMarkerColor(color);
30  m.SetMarkerSize(size);
31  m.SetMarkerStyle(style);
32  m.DrawMarker(p.z(), p.y());
33 }
34 void draw_ray(const Ray& ray, int color=1, float width=1.0)
35 {
36  TArrow l;
37  l.SetLineColor(color);
38  l.SetLineWidth(width);
39  l.DrawLine(ray.first.z(), ray.first.y(),
40  ray.second.z(), ray.second.y());
41 }
42 void draw_arrow(const Ray& ray, int color=1, float width=1.0, float asize=0, const char* opt="")
43 {
44  TArrow l;
45  l.SetLineColor(color);
46  l.SetLineWidth(width);
47  l.DrawArrow(ray.first.z(), ray.first.y(),
48  ray.second.z(), ray.second.y(),
49  asize, opt);
50 }
51 
52 TH1F* draw_frame(TCanvas& canvas, std::string title)
53 {
54  auto* frame = canvas.DrawFrame(-1.0*border, -1.0*border, width+border, height+border);
55  frame->SetTitle(title.c_str());
56  return frame;
57 }
58 
59 const std::vector<int> layer_colors{1,1,2,3,4};
60 
61 void draw_strip(const Point& head, const Point& tail,
62  const Vector& raydir, int color, bool outline=true);
63 void draw_strip(const Point& head, const Point& tail,
64  const Vector& raydir, int color, bool outline)
65 {
66  const double shoot = 2*std::max(width,height);
67  std::vector<Point> points {
68  tail+raydir*shoot,
69  tail-raydir*shoot,
70  head-raydir*shoot,
71  head+raydir*shoot
72  };
73 
74  TPolyLine* pl = new TPolyLine; // like a sieve
75  pl->SetLineColor(color);
76  pl->SetFillColorAlpha(color, 0.1);
77  for (const auto& p : points) {
78  pl->SetNextPoint(p.z(), p.y());
79  }
80  pl->SetNextPoint(points.front().z(), points.front().y());
81  pl->Draw("f");
82  if (outline) {
83  pl->Draw("");
84  }
85 }
86 
87 void draw_layer(Coordinates& coords, int ilayer,
88  double pitch_mag,
89  const Point& pitch,
90  const Point& center,
91  const std::vector<double>& measure)
92 {
93  const Vector ecks(1,0,0);
94  const auto raydir = ecks.cross(pitch);
95 
96 
97  for (size_t ind=0; ind<measure.size(); ++ind) {
98  int color = layer_colors[ilayer];
99  if (measure[ind] <= 0.0) { continue; }
100  const auto tail = center + ind*pitch_mag*pitch;
101  const auto head = center + (ind+1)*pitch_mag*pitch;
102  draw_strip(tail, head, raydir, color);
103  }
104 }
105 
106 
107 void draw_strips(Coordinates& coords, const strips_t& strips, bool outline=true);
108 void draw_strips(Coordinates& coords, const strips_t& strips, bool outline)
109 {
110  const Vector ecks(1,0,0);
111 
112  for (const auto& strip : strips) {
113  int color = layer_colors[strip.layer];
114  const auto& pitch = coords.pitch_dirs()[strip.layer];
115  const auto raydir = ecks.cross(pitch);
116  const auto& center = coords.centers()[strip.layer];
117 
118  const double pitch_mag = coords.pitch_mags()[strip.layer];
119 
120  const auto pind1 = strip.bounds.first;
121  const auto pind2 = strip.bounds.second;
122  const double pitch_dist = std::abs(pind1-pind2)*pitch_mag;
123 
124  const auto tail = center + pind1*pitch_mag*pitch;
125  const auto head = tail + pitch_dist*pitch;
126  draw_strip(tail, head, raydir, color, outline);
127  }
128 }
129 
130 
131 Point draw_blob(Coordinates& coords, const Blob& blob, int color=1)
132 {
133  const auto& corners = blob.corners();
134  if (corners.empty()) {
135  return Point();
136  }
137 
138  std::vector<Point> points;
139  Point center;
140  for (const auto& corn : corners) {
141  const auto p = coords.ray_crossing(corn.first, corn.second);
142  center += p;
143  points.push_back(p);
144  }
145  center = center * (1.0/points.size());
146  sort(points.begin(), points.end(),
147  [&](const Point& a, const Point&b) {
148  const Point ac = a-center;
149  const Point bc = b-center;
150  const double anga = atan2(ac.y(), ac.z());
151  const double angb = atan2(bc.y(), bc.z());
152  return anga > angb;
153  });
154 
155  TPolyLine* pl = new TPolyLine; // like a sieve
156  pl->SetLineColor(color);
157  pl->SetLineWidth(2);
158 
159  for (const auto& p : points) {
160  pl->SetNextPoint(p.z(), p.y());
161  }
162  pl->SetNextPoint(points.front().z(), points.front().y());
163  pl->Draw();
164  return center;
165 }
166 
167 
168 
169 struct Printer {
170  TCanvas canvas;
172  int count;
174  : canvas("test_raytiling", "Ray Tiling", 500, 500)
175  , fname(fn)
176  , count(0)
177  {
178  canvas.Print((fname + ".pdf[").c_str(), "pdf");
179  }
180  ~Printer() { canvas.Print((fname+".pdf]").c_str(), "pdf"); }
181  void operator()() {
182  canvas.Print((fname+".pdf").c_str(), "pdf");
183  canvas.Print(Form("%s-%02d.png", fname.c_str(), count), "png");
184  canvas.Print(Form("%s-%02d.svg", fname.c_str(), count), "svg");
185  ++count;
186  }
187 };
188 
189 
190 void draw_points_blobs(Coordinates& coords, Printer& print,
191  const std::vector<Point>& points,
192  const blobs_t& blobs)
193 {
194  int nstrips = 0;
195  for (const auto& b : blobs) {
196  nstrips += b.strips().size();
197  }
198 
199  draw_frame(print.canvas, Form("%d points, %d blobs, %d strips",
200  (int)points.size(), (int)blobs.size(), nstrips));
201  for (size_t ipt=0; ipt<points.size(); ++ipt ) {
202  const auto& p = points[ipt];
203  draw_point(p, 1, 24, ipt+1);
204  }
205  for (size_t ib = 0; ib<blobs.size(); ++ib) {
206  draw_blob(coords, blobs[ib],1);
207  }
208 }
209 
210 void draw_points_blobs_solved(Coordinates& coords, Printer& print,
211  const std::vector<Point>& points,
212  const blobs_t& blobs,
213  const std::unordered_map<size_t, float>& blob_charge)
214 {
215  int nstrips = 0;
216  for (const auto& b : blobs) {
217  nstrips += b.strips().size();
218  }
219 
220 
221  draw_frame(print.canvas, Form("%d points, %d blobs, %d strips",
222  (int)points.size(), (int)blobs.size(), nstrips));
223 
224  for (size_t ipt=0; ipt<points.size(); ++ipt ) {
225  const auto& p = points[ipt];
226  draw_point(p, 1, 24, ipt+1);
227  }
228 
229  for (auto it : blob_charge) {
230  size_t ind = it.first;
231  const auto& blob = blobs[ind];
232  float q = it.second;
233  int color = 1;
234  if (q<1.0) {
235  color = 2;
236  }
237  auto center = draw_blob(coords, blob, color);
238  if (q<1.0 ) {
239  draw_text(center, Form("s%d", (int)ind), color, 22);
240  }
241  else {
242  draw_text(center, Form("s%d:%.1f", (int)ind, q), color, 22);
243  }
244  info("center:{} ind:{} q:[]", center, ind, q);
245  }
246 }
247 
248 void draw_text(const Point& pt, const std::string text, int color, int align)
249 {
250  TLatex l;
251  l.SetTextAlign(align);
252  l.SetTextFont(52);
253  l.SetTextSize(0.03);
254  l.SetTextColor(color);
255  l.DrawLatex(pt.z(), pt.y(), text.c_str());
256 }
257 void draw_raygrid(Printer& print, const Coordinates& coords, const ray_pair_vector_t& raypairs)
258 {
259  auto* frame = print.canvas.DrawFrame(-110, 50-110, 110, 50+110);
260  frame->SetTitle("Ray Grid");
261 
262  info("got {} ray pairs", raypairs.size());
263  std:: vector<Point> centers0;
264  std:: vector<Point> centers1;
265  const int ui = 2, vi = 3, wi = 4;
266  int index[] = {2, 3, 4};
267  int colors[] = {2,4,1};
268  for (int ind=0; ind<3; ++ind) {
269  int ri = index[ind];
270 
271  auto r0 = raypairs[ri].first;
272  auto r1 = raypairs[ri].second;
273 
274  draw_ray(r0, colors[ind], 2.0);
275  draw_ray(r1, colors[ind], 2.0);
276  //auto dir = ray_direction(raypairs[ind].first).norm();
277  auto pit = ray_vector(ray_pitch(r0, r1));
278  auto cen = 0.5*(r0.first + r0.second);
279  centers0.push_back(cen);
280  centers1.push_back(0.5*(r1.first + r1.second));
281  draw_arrow(Ray(cen, cen+pit), colors[ind], 1, 0.01, ">");
282  }
283 
284  const auto r00 = coords.zero_crossing(ui, vi);
285  const auto r0n = coords.ray_crossing({ui,-1}, {vi,1});
286  const auto r1n = coords.ray_crossing({ui,1}, {vi,-1});
287 
288  const auto r01 = coords.ray_crossing({ui,0}, {vi,1});
289  const auto r10 = coords.ray_crossing({ui,1}, {vi,0});
290 
291  const int i=10, j=16;
292 
293  const auto rij = coords.ray_crossing({ui,i}, {vi,j});
294  const auto r0j = coords.ray_crossing({ui,0}, {vi,j});
295  const auto ri0 = coords.ray_crossing({ui,i}, {vi,0});
296 
297  const double wpij = coords.pitch_location({ui,i}, {vi,j}, wi);
298  const auto wray0 = raypairs[wi].first;
299  const auto wray1 = raypairs[wi].second;
300  const auto wtail = wray0.first;
301  const auto whead = wray0.second;
302  const auto wpit = ray_vector(ray_pitch(wray0, wray1));
303  const double wpind = wpij / wpit.magnitude();
304 
305 
306  draw_point(r00, 1, 24, 1);
307  draw_text(r00+Point(0,0,-15), "r^{01}_{00}");
308  draw_text(r00+Point(0,5,5), "w^{01}",2);
309  draw_text(r00+Point(0,-15,0), "w^{10}",4);
310 
311  //draw_point(r01, 1, 24, 2);
312  //draw_point(r10, 1, 24, 4);
313  draw_point(rij, 1, 20, 1);
314  draw_text(rij+Point(0,0,5), "r^{01}_{ij}");
315 
316  draw_ray(Ray(r00, centers0[0]), colors[0], 1.0);
317  draw_ray(Ray(r00, centers0[1]), colors[1], 1.0);
318 
319  draw_ray(Ray(r1n, centers1[0]), colors[0], 1.0);
320  draw_ray(Ray(r0n, centers1[1]), colors[1], 1.0);
321 
322  draw_text(centers0[0]+Point(0,10,0), "p^{0}", colors[0]);
323 
324  draw_arrow(Ray(r00, r01), colors[0], 2.0, 0.01, ">");
325  draw_arrow(Ray(r00, r10), colors[1], 2.0, 0.01, ">");
326 
327 
328 
329  draw_arrow(Ray(r00, rij), 1, 1.0, 0.01, ">");
330  //draw_arrow(Ray(r00, r0j), colors[0], 2.0, 0.01, ">");
331  draw_arrow(Ray(r00, ri0), colors[1], 2.0, 0.01, ">");
332  draw_arrow(Ray(ri0, rij), colors[0], 2.0, 0.01, ">");
333 
334  draw_text(ri0+Point(0,-10,-20), "iw^{10}", 4);
335  draw_text(0.5*(ri0+rij)+Point(0,-10,0), "jw^{01}", 2);
336 
337  draw_ray(Ray(wtail + wpind*wpit, whead + wpind*wpit), colors[2], 1.0);
338 
339  print();
340 }
341 
std::pair< Point, Point > Ray
A line segment running from a first (tail) to a second (head) point.
Definition: Point.h:21
static const double m
Definition: Units.h:79
void draw_points_blobs_solved(Coordinates &coords, Printer &print, const std::vector< Point > &points, const blobs_t &blobs, const std::unordered_map< size_t, float > &blob_charge)
Definition: raygrid_draw.h:210
void draw_strip(const Point &head, const Point &tail, const Vector &raydir, int color, bool outline=true)
Definition: raygrid_draw.h:63
std::string string
Definition: nybbler.cc:12
opt
Definition: train.py:196
std::vector< Strip > strips_t
Definition: RayTiling.h:42
const std::vector< int > layer_colors
Definition: raygrid_draw.h:59
static QStrList * l
Definition: config.cpp:1044
def blobs(cm, hist)
Definition: plots.py:79
const double width
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:87
void draw_raygrid(Printer &print, const Coordinates &coords, const ray_pair_vector_t &raypairs)
Definition: raygrid_draw.h:257
void draw_text(const Point &pt, const std::string text, int color=1, int align=22)
Definition: raygrid_draw.h:248
T abs(T value)
const double border
QAsciiDict< Entry > fn
void draw_strips(Coordinates &coords, const strips_t &strips, bool outline=true)
Definition: raygrid_draw.h:108
Point draw_blob(Coordinates &coords, const Blob &blob, int color=1)
Definition: raygrid_draw.h:131
std::enable_if< internal::is_string< String >::value >::type print(std::FILE *f, const text_style &ts, const String &format_str, const Args &...args)
Definition: color.h:549
Printer(std::string fn)
Definition: raygrid_draw.h:173
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
Definition: DCEL.h:34
TCanvas canvas
Definition: raygrid_draw.h:170
const double height
static int max(int a, int b)
void draw_points_blobs(Coordinates &coords, Printer &print, const std::vector< Point > &points, const blobs_t &blobs)
Definition: raygrid_draw.h:190
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
std::vector< ray_pair_t > ray_pair_vector_t
Definition: Point.h:27
TH1F * draw_frame(TCanvas &canvas, std::string title)
Definition: raygrid_draw.h:52
p
Definition: test.py:223
std::vector< float > Vector
color
Definition: color.h:50
std::vector< Blob > blobs_t
Definition: RayTiling.h:134
Ray ray_pitch(const Ray &ray1, const Ray &ray2)
Definition: Point.cxx:76
void draw_layer(Coordinates &coords, int ilayer, double pitch_mag, const Point &pitch, const Point &center, const std::vector< double > &measure)
Definition: raygrid_draw.h:87
void operator()()
Definition: raygrid_draw.h:181
def center(depos, point)
Definition: depos.py:117
static bool * b
Definition: config.cpp:1043
std::string fname
Definition: raygrid_draw.h:171
void dump(const blobs_t &blobs)
Definition: raygrid_draw.h:16
static QCString align(DocHtmlCell *cell)
void draw_point(const Point &p, float size=1, int style=20, int color=1)
Definition: raygrid_draw.h:26
void draw_ray(const Ray &ray, int color=1, float width=1.0)
Definition: raygrid_draw.h:34
Vector ray_vector(const Ray &ray)
Definition: Point.cxx:67
void draw_arrow(const Ray &ray, int color=1, float width=1.0, float asize=0, const char *opt="")
Definition: raygrid_draw.h:42
def points(jdat)
Definition: rendertvtk.py:105