-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.py
More file actions
414 lines (324 loc) · 10.7 KB
/
draw.py
File metadata and controls
414 lines (324 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# External Dependencies
import ROOT as root
import math
import numpy as np
import os
# Internal Dependencies
from dataTypes import *
import utilities
import geometryReco
def makeCenterLines(plane, wire):
"""Make Lines corresponding to the center of every primitive wire in merged wire
Parameters
----------
plane : PlaneInfo
Plane information for the plane the wire is on
wire : tuple[2] of int
A merged wire
Returns
-------
list of Line
List of lines that represent the merged wire
"""
lines = []
for wireNo in range(wire[0], wire[1] + 1):
dist = plane.pitch * wireNo + plane.pitch / 2
if plane.originTranslation > 0:
point = Point(plane.originTranslation -
(dist * (-plane.cos)), dist * plane.sin)
else:
point = Point(dist * plane.cos, dist * plane.sin)
if plane.gradient == "INF":
line = Line(point, Point(point.x, point.y + 1))
else:
line = Line(point, Point(point.x + 1, point.y + plane.gradient))
lines.append(line)
return lines
def makeEventLines(planes, event, useCenterLines):
"""Create all lines in an event
Parameters
----------
planes : list of PlaneInfo
A list containing information for all the planes in the detector
event : list of list of tuple[2] of ints
An event with all merged wires
useCenterLines : boolean or str
Make center lines if True, edges if False and both if "both"
Returns
-------
list of lines
List of lines in event
"""
lines = []
for planeNo, plane in enumerate(event):
for wire in plane:
if (useCenterLines == "both"):
centerLines = makeCenterLines(planes[planeNo], wire)
centerLines = list(map(lambda line: (line, True), centerLines))
lines.extend(centerLines)
borderLines = geometryReco.makeLines(planes[planeNo], wire)
borderLines = list(
map(lambda line: (line, False), borderLines))
lines.extend(borderLines)
elif useCenterLines:
centerLines = makeCenterLines(planes[planeNo], wire)
centerLines = list(map(lambda line: (line, True), centerLines))
lines.extend(centerLines)
else:
borderLines = geometryReco.makeLines(planes[planeNo], wire)
borderLines = list(
map(lambda line: (line, False), borderLines))
lines.extend(borderLines)
return lines
def drawEventLines(lines, volume, centerColor=root.kBlue, centerStyle=2,edgeColor=root.kBlack,edgeStyle=1):
"""Create the TLines that will be drawn
Parameters
----------
lines : list of Line
List of lines in event
volume : DetectorVolume
Volume of Detector
centerColor : TColor
Color of Center Wires
centerStyle : TStyle
Style of Center Wires
edgeColor : TColor
Color of Edge Wirees
edgeStyle : TStyle
Style of edge Wires
Returns
-------
list of tuple[2] (TLine,boolean)
The actual Tlines to be drawn and whether they're center line
"""
drawLines = []
x0 = 0
x1 = volume.width
for lineNo, line in enumerate(lines):
if(math.isclose(line[0][0].x, line[0][1].x, rel_tol=1e-5)):
drawLine = root.TLine(line[0][0].x, 0, line[0][0].x, volume.height)
else:
gradient = (line[0][1].y - line[0][0].y) / \
(line[0][1].x - line[0][0].x)
y0 = gradient * x0 - gradient * line[0][0].x + line[0][0].y
y1 = gradient * x1 - gradient * line[0][0].x + line[0][0].y
drawLine = root.TLine(x0, y0, x1, y1)
if line[1]:
drawLine.SetLineColor(centerColor)
drawLine.SetLineStyle(centerStyle)
else:
drawLine.SetLineColor(edgeColor)
drawLine.SetLineStyle(edgeStyle)
drawLine.Draw()
drawLines.append((drawLine,line[1]))
return drawLines
def drawBlobs(blobs, color = root.kGreen, width = 4):
"""Draw True Blobs
Parameters
----------
blobs : list of Blob
List of True Blobs
color : TColor
Color of Blobs
width : int
Width of lines that draw Blobs
Returns
-------
list of TLine
list of lines to be drawn that represent blobs
"""
drawnBlobs = []
for blob in blobs:
for i in range(1, len(blob.points)):
drawnBlobs.append(root.TLine(
blob.points[i - 1].x, blob.points[i - 1].y, blob.points[i].x, blob.points[i].y))
drawnBlobs.append(root.TLine(
blob.points[-1].x, blob.points[-1].y, blob.points[0].x, blob.points[0].y))
for line in drawnBlobs:
line.SetLineWidth(width)
line.SetLineColor(color)
line.Draw()
return drawnBlobs
def drawCells(cells, asMarker, trueColor = root.kGreen, recoColor = root.kRed, width = 4, trueCells=[], drawFake=True):
"""Draw the Cells
Parameters
----------
cells : list of Cell
List of reconstructed cells
asMarker : boolen
If true draw markers denoting vertices else draw polygon
trueColor : TColor
Color of True Cells
recoColor : TColor
Color of Cells
width : int
Width of lines that draw Cells
trueCells : list of Boolean
Truth information for the cells
drawFake : Boolean
True if draw fake cells false otherwise
Returns
-------
list of list of TLine or TMarker
Drawn Cells
"""
drawnCells = []
if trueCells == []:
trueCells = [False] * len(cells)
if asMarker:
for cellNo, cell in enumerate(cells):
drawnCell = list(map(lambda p: root.TMarker(p.x, p.y, 21), cell.points))
for marker in drawnCell:
if trueCells[cellNo]:
marker.SetMarkerColor(trueColor)
else:
marker.SetMarkerColor(recoColor)
marker.Draw()
drawnCells.append(drawnCell)
else:
for cellNo, cell in enumerate(cells):
drawnCell = []
for i in range(1, len(cell.points)):
drawnCell.append(root.TLine(
cell.points[i - 1].x, cell.points[i - 1].y, cell.points[i].x, cell.points[i].y))
drawnCell.append(root.TLine(
cell.points[-1].x, cell.points[-1].y, cell.points[0].x, cell.points[0].y))
for line in drawnCell:
line.SetLineWidth(width)
if drawFake:
if trueCells[cellNo]:
line.SetLineColor(trueColor)
else:
line.SetLineColor(recoColor)
line.Draw()
else:
if trueCells[cellNo]:
line.SetLineColor(trueColor)
line.Draw()
drawnCells.append(drawnCell)
return drawnCells
def drawCellNumbers(cells, color = root.kBlack, trueCells=[], drawFake=True):
"""Draw Numbering for the cells
Parameters
----------
cells : list of Cell
List of reconstructed cells
color : TColor
Color of text
trueCells : list of Boolean
Truth information for the cells
drawFake : Boolean
True if draw fake cells false otherwise
Returns
-------
list of TText
The number associated with each cell
"""
numbers = []
if trueCells == []:
trueCells = [False] * len(cells)
counter = 0
for cellNo, cell in enumerate(cells):
center = np.mean(cell.points,axis=0)
number = root.TText(center[0],center[1],str(counter))
number.SetTextColor(color)
if drawFake:
counter += 1
number.Draw()
numbers.append(number)
else:
if trueCells[cellNo]:
counter += 1
number.Draw()
numbers.append(number)
return numbers
def drawCellPointNumbers(cell, color = root.kBlack):
"""Draw Numbering for the points in a cell
Parameters
----------
cell : Cell
Cell in question
color : TColor
Color of text
Returns
-------
list of TText
The number associated with each point in cell
"""
numbers = []
if cell[0] != False:
for pointNo, point in enumerate(cell.points):
number = root.TText(point.x,point.y,str(pointNo))
number.SetTextColor(color)
number.Draw()
numbers.append(number)
return numbers
def zoomCell(cell,side,volume):
"""Zoom in on a particular cell
Parameters
----------
cell : Cell
The cell in question
side : int
Width of the zoom window in mm
volume : DetectorVolume
Volume of detector
Returns
-------
tuple[2] of point
(Bottom Left Point, Top Right Point)
"""
width = side
height = int(side * (volume.height/volume.width))
center = Point(*np.mean(cell.points,axis=0))
x1 = int(center.x-width/2)
y1 = int(center.y-height/2)
if x1 < 0:
x1 = 0
if y1 < 0:
y1 = 0
x2 = x1 + width
y2 = y1 + height
if x2 > volume.width:
x2 = volume.width
x1 = x2 - width
if y2 > volume.height:
y2 = volume.height
y1 = y2 - height
#get canvas
c1 = root.gPad.GetCanvas()
c1.Range(x1,y1,x2,y2)
return (Point(x1,y1),Point(x2,y2))
def saveImage(fileName,directory=""):
#check if directories exist make if not
if len(directory)>0 and not (os.path.isdir(directory)):
os.mkdir(directory)
if not (os.path.isdir(directory+"/png")):
os.mkdir(directory+"/png")
if not (os.path.isdir(directory+"/pdf")):
os.mkdir(directory+"/pdf")
#get canvas
c1 = root.gPad.GetCanvas()
#save file and convert png to transparent background
c1.SaveAs(directory + "/png/" + fileName+".png")
c1.SaveAs(directory + "/pdf/" + fileName+".pdf")
#Convert image to have transparent background requires imagemagick
os.system("convert " + directory + "/png/" + fileName + ".png -fuzz 20% -transparent white " + directory + "/png/" + fileName+".png")
return True
def zoom():
mouseLocation = Point(root.gPad.GetEventX(),root.gPad.GetEventY())
#get canvas
c1 = root.gPad.GetCanvas()
#get canvas range
x0 = root.Double()
y0 = root.Double()
x1 = root.Double()
y1 = root.Double()
c1.GetRange(x0,y0,x1,y1)
root.gPad.HandleInput()
ev = root.gPad.GetEvent()
# print(ev)
# if ev == 11:
# continue
# elif ev == 12:
# continue