-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolLibrary.py
479 lines (388 loc) · 18.6 KB
/
PolLibrary.py
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import numpy as np
import PolMath
import warnings
__all__ = ['singleComptonEvent', 'comptonEventChain', 'squareScatteringMatrix', 'squareFilterMatrix', 'squareDetector']
#Class describing a single compton events. The attributes of the class are:
# -coordinates: x, y, and z coordinate of the compotn interaction
# -energy: the energy of the interaction
class singleComptonEvent:
coordinates = np.array([])
energy = 0
#Constructor for the class
def __init__(self, coordinates, Energy):
self.coordinates = np.array(coordinates)
self.energy = Energy
#Return the x coordinate of the interaction
@property
def x(self):
return self.coordinates[0]
#Return the y coordinate of the interaction
@property
def y(self):
return self.coordinates[1]
#Return the z coordinate of the interaction
@property
def z(self):
return self.coordinates[2]
#Assign a new coordinate vector to the event
def change_coordinates(self, new_coord):
self.coordinates = np.copy(new_coord)
def __str__(self):
return "Compton Event - x: {0}, y: {1}, z: {2} ; Energy: {3}".format(self.coordinates[0], self.coordinates[1], self.coordinates[2], self.energy)
def __repr__(self):
return "{0};{1};{2};{3}".format(self.coordinates[0], self.coordinates[1], self.coordinates[2], self.energy)
#Class describing a chain of compont events. The attributes of the class are:
# -events: A dictionary containing all the events in the chain. The keys of the
# dictionary are the ordinal number of the interaction, the values are
# the single compton events, represented by singleComptonEvent objects
# -event_id: a numerical identifier for the event.
class comptonEventChain:
events = {}
event_id = 0
#Constructor of the class
def __init__(self, event_id):
self.event_id = event_id
self.events = {}
#Returns the total energy deposited by the event chain
@property
def totalEnergy(self):
return sum([self.events[key].energy for key in self.events])
#Returns the geometrical angle associated to the first interaction
@property
def geometricalAngleFirstInt(self):
x = self.events[2].x - self.events[1].x
y = self.events[2].y - self.events[1].y
z = self.events[2].z - self.events[1].z
#theta = np.arctan(np.sqrt(x**2 + y**2)/z)*180/np.pi
#if theta < 0: theta += 180
theta = np.arctan2(round(np.sqrt(x**2 + y**2), 10),round(-z,10))*PolMath.RAD_TO_DEG
return theta
#Returns the total number of Compton events in the cahin
@property
def multiplicity(self):
return len(self.events.keys())
#Function which adds a new event to the chain. The inputs are:
# -order: the ordinal number of the event
# -event: a singleComptonEvent object representing one single Compton interaction
def addEvent(self, order, event):
self.events[order] = event
#Left to be eventually updated and used in the future
#def vectorTranslationFirstInt(self):
# translation_vector = self.events[1].coordinates
# new_event_1 = singleComptonEvent([self.events[1].coordinates[i] - translation_vector[i] for i in (0,1,2)], self.events[1].energy )
# new_event_2 = singleComptonEvent([self.events[2].coordinates[i] - translation_vector[i] for i in (0,1,2)], self.events[2].energy )
# new_double_event = comptonEventChain(self.event_id)
# new_double_event.addEvent(1, new_event_1 )
# new_double_event.addEvent(2, new_event_2 )
# return new_double_event
def __str__(self):
return str(self.events)
def __repr__(self):
return str(self.events)
def __getitem__(self, order):
return self.events[order]
def __setitem__(self, order, val):
self.events[order] = val
#Internal class representing the structure of a xy pixelized scattering map for
#a square voxel detector.
#The attributes of the class are:
# -matrix: a 2n-1 x 2n-1 matrix, where n is the number of pixels in the detector.
# Each entry of the matrix represents a single square pixel and the origin
# of the reference frame is placed in the central pixel of the map.
# -detector_pixels: the number n of pixels of the detector
# -x_min, x_max: the minimum and the maximum value of the x coordinate of the map
# -y_min, y_max: the minimum and the maximum value of the y coordinate of the map
# -x_range, y_range: the range of values for the x and y coordinates
class _squareDetectorMatrix:
matrix = []
detector_pixels = 0
x_min, x_max, y_min, y_max = 0, 0, 0, 0
x_range, y_range = '', ''
#Constructor of the class
def __init__(self, detector_pixels, np_type):
self.matrix = np.zeros((2*detector_pixels-1, 2*detector_pixels-1), dtype = np_type)
self.detector_pixels = detector_pixels
self.x_min = - detector_pixels
self.y_min = - detector_pixels
self.x_max = detector_pixels
self.y_max = detector_pixels
self.x_range = range(self.x_min, self.x_max )
self.y_range = range(self.y_min, self.y_max )
#Gets one element from the matrix using the scattering map coordinate system
def __getitem__(self, coordinates):
x, y = coordinates
return self.matrix[ y + (self.detector_pixels-1) , x + (self.detector_pixels-1)]
#Sets one element from the matrix using the scattering map coordinate system
def __setitem__(self, coordinates, val):
x, y = coordinates
self.matrix[ y + (self.detector_pixels-1) , x + (self.detector_pixels-1)] = val
def __str__(self):
return str(self.matrix)
def __repr__(self):
return self.matrix
#Class representing a 2D scattering map for a square voxel detector.
class squareScatteringMatrix(_squareDetectorMatrix):
def __init__(self, detector_pixels):
super().__init__(detector_pixels, "int32")
#Checks if the scattering map is empty
def isEmpty(self):
return sum(map(sum, self.matrix)) == 0
#Class representing a 2D geometrical filter which can be applied on a square voxel
#scattering map. The filter is 2D matrix of boolean value which can be used to flag
#which pixels have to be included in the data analysis (True) and which not (False)
class squareFilterMatrix(_squareDetectorMatrix):
def __init__(self, detector_pixels):
super().__init__(detector_pixels, "bool")
#A square shaped filter, centered on the (0,0) pixel of the scattering map and
#with a given edge length. All the pixels INSIDE the square are flagged False
def squareFilter(self, edge):
for x in self.x_range:
for y in self.y_range:
self[x,y] = not (abs(x) < edge and abs(y) < edge)
#A square shaped filter, centered on the (0,0) pixel of the scattering map and
#with a given edge length. All the pixels OUTSIDE the square are flagged False
def inverseSquareFilter(self, edge):
for x in self.x_range:
for y in self.y_range:
self[x,y] = abs(x) < edge and abs(y) < edge
#A rectangle shaped filter, centered on the (0,0) pixel of the scattering map and
#with a given x and y edge length. All the pixels OUTSIDE the square are flagged False
def rectangleFilter(self, x_edge, y_edge):
for x in self.x_range:
for y in self.y_range:
self[x,y] = not (abs(x) < x_edge and abs(y) < y_edge)
#A circular shaped filter, centered on the (0,0) pixel of the scattering map and
#with a chosen radius. All the pixels INSIDE the square are flagged False
def circularFilter(self, radius):
for x in self.x_range:
for y in self.y_range:
self[x,y] = np.sqrt(x**2 + y**2) >= radius
#A circular shaped filter, centered on the (0,0) pixel of the scattering map and
#with a chosen radius. All the pixels OUTSIDE the square are flagged False
def inverseCircularFilter(self, radius):
for x in self.x_range:
for y in self.y_range:
self[x,y] = np.sqrt(x**2 + y**2) < radius
#An angular sector shaped filter, centered on the (0,0) pixel of the scattering map.
#All the pixels OUTSIDE the minimum and the maximum angle of the sector are flagged as
#False
def sectorFilter(self, min_angle, max_angle):
for x in self.x_range:
for y in self.y_range:
self[x,y] = min_angle <= PolMath.convertAngle(np.arctan2(y,x))*180/np.pi < max_angle
#Flags as False a list of single points to be excluded
def singlePointsFilter(self, point_list):
self.matrix = np.full((2*self.detector_pixels, 2*self.detector_pixels), True)
for point in point_list:
self[point] = False
#Removes all the filters
def noFilter(self):
self.matrix = np.full((2*self.detector_pixels, 2*self.detector_pixels), True)
#Makes the entry by entry boolean multiplication of a list of masks and return
#the final filter matrix
@staticmethod
def multiplyMask(mask_list):
detector_pixels = mask_list[0].detector_pixels
new_mask = squareFilterMatrix(detector_pixels)
new_mask.noFilter()
for x in new_mask.x_range:
for y in new_mask.y_range:
for mask in mask_list:
new_mask[x,y] = new_mask[x,y] and mask[x,y]
return new_mask
#Makes the entry by entry boolean sum of a list of masks and return
#the final filter matrix
@staticmethod
def sumMask(mask_list):
detector_pixels = mask_list[0].detector_pixels
new_mask = squareFilterMatrix(detector_pixels)
for x in new_mask.x_range:
for y in new_mask.y_range:
for mask in mask_list:
new_mask[x,y] = new_mask[x,y] or mask[x,y]
return new_mask
#Class defining a detector based on square shaped voxels. The class is strongly
#based on the current design of NFT. To be expanded and updated
class squareDetector:
pcb_size = 0
sensitive_layer_size = 0
total_pcb_number = 0
total_sensitive_number = 0
space_between_voxels = 0
top_surface_coordinate = 0
voxel_size = 0
voxel_layers = {}
sensitive_layers_start_centroid = []
voxels_per_layer = {}
detector_xy_size = 0
energy_res = {}
def __init__(self, detector_xy_size = 0, pcb_size = 0, sensitive_layer_size = 0, total_pcb_number = 0, total_sensitive_number = 0, space_between_voxels = 0, top_surface_coordinate = 0, voxel_size = 0 ):
self.detector_xy_size = detector_xy_size
self.pcb_size = pcb_size
self.sensitive_layer_size = sensitive_layer_size
self.total_pcb_number = total_pcb_number
self.total_sensitive_number = total_sensitive_number
self.space_between_voxels = space_between_voxels
self.top_surface_coordinate = top_surface_coordinate
self.voxel_size = voxel_size
first_centroid = round(top_surface_coordinate - voxel_size/2 ,4)
self.sensitive_layers_start_centroid = [round(first_centroid - (i-1) * (self.sensitive_layer_size + self.pcb_size),4) for i in range(1, self.total_sensitive_number + 1) ]
#print(self.sensitive_layers_start_centroid)
total_voxels_per_layer = int(self.sensitive_layer_size/voxel_size)
for i in range(len(self.sensitive_layers_start_centroid)):
for j in range(total_voxels_per_layer):
self.voxel_layers[i *total_voxels_per_layer + j ] = round(self.sensitive_layers_start_centroid[i] - j * voxel_size, 4)
for i in range(self.total_sensitive_number):
self.voxels_per_layer[i+1] = []
for j in range(total_voxels_per_layer):
self.voxels_per_layer[i+1].append(round(self.sensitive_layers_start_centroid[i] - j * self.voxel_size,4))
#print(self.voxel_layers)
def findSensLayer(self, z):
res = 0
for key in self.voxels_per_layer:
#print(z in self.voxels_per_layer[key])
if z in self.voxels_per_layer[key]:
res = key
return res
def setEnergyResolution(self, infile):
for line in infile:
if "CZTDetector_1" in line:
tmp = line.split(' ')
self.energy_res[float(tmp[2])] = float(tmp[4])
################## HEX DETECTORS WIP #####################################
# class hexScatteringMatrix:
# hex_map = {}
# row_num, col_num = 0, 0
# x_min, x_max, y_min, y_max = 0, 0, 0, 0
# x_range, y_range = '', ''
# def __init__(self, col_num, row_num):
# self.row_num = row_num
# self.col_num = col_num
# def addEvent(self, coord):
# #coord = (x, y)
# #print(coord)
# #print(list(self.hex_map.keys()))
# if not coord in self.hex_map.keys():
# self.hex_map[coord] = 1
# else:
# self.hex_map[coord] += 1
# def isEmpty(self):
# return len(hex_map) == 0
class _hexDetectorMatrix:
matrix = []
row_num, col_num = 0, 0
x_min, x_max, y_min, y_max = 0, 0, 0, 0
x_range, y_range = '', ''
#Constructor of the class
def __init__(self, col_num, row_num, np_type):
self.matrix = np.zeros((2*col_num, 2*row_num-1), dtype = np_type)
self.row_num = row_num
self.col_num = col_num
self.x_min = - row_num
self.y_min = - col_num
self.x_max = row_num
self.y_max = col_num
self.x_range = range(self.x_min+1, self.x_max)
self.y_range = range(self.y_min+1, self.y_max)
#print(self.x_range)
#print(self.y_range)
#Gets one element from the matrix using the scattering map coordinate system
def __getitem__(self, coordinates):
x, y = coordinates
return self.matrix[ y + (self.col_num-1) , x + (self.row_num-1)]
#Sets one element from the matrix using the scattering map coordinate system
def __setitem__(self, coordinates, val):
x, y = coordinates
#print(x,y)
if self[x,y] != -1:
self.matrix[y + (self.col_num-1) , x + (self.row_num-1)] = val
else:
warnings.warn('Warning: Attempted access to an invalid entry')
def __str__(self):
return str(self.matrix)
def __repr__(self):
return self.matrix
#Class representing a 2D scattering map for a square voxel detector.
class hexScatteringMatrix(_hexDetectorMatrix):
def __init__(self, col_num, row_num):
super().__init__(col_num, row_num, "int32")
for i in self.x_range:
if i % 2 == 0:
self[i, self.y_max] = -1
#Checks if the scattering map is empty
def isEmpty(self):
return sum(map(sum, self.matrix)) == 0
#Class representing a 2D geometrical filter which can be applied on a square voxel
#scattering map. The filter is 2D matrix of boolean value which can be used to flag
#which pixels have to be included in the data analysis (True) and which not (False)
class hexFilterMatrix(_hexDetectorMatrix):
def __init__(self, col_num, row_num):
super().__init__(col_num, row_num, "bool")
# #A square shaped filter, centered on the (0,0) pixel of the scattering map and
# #with a given edge length. All the pixels INSIDE the square are flagged False
# def squareFilter(self, edge):
# for x in self.x_range:
# for y in self.y_range:
# self[x,y] = not (abs(x) < edge and abs(y) < edge)
# #A square shaped filter, centered on the (0,0) pixel of the scattering map and
# #with a given edge length. All the pixels OUTSIDE the square are flagged False
# def inverseSquareFilter(self, edge):
# for x in self.x_range:
# for y in self.y_range:
# self[x,y] = abs(x) < edge and abs(y) < edge
# #A rectangle shaped filter, centered on the (0,0) pixel of the scattering map and
# #with a given x and y edge length. All the pixels OUTSIDE the square are flagged False
# def rectangleFilter(self, x_edge, y_edge):
# for x in self.x_range:
# for y in self.y_range:
# self[x,y] = not (abs(x) < x_edge and abs(y) < y_edge)
# #A circular shaped filter, centered on the (0,0) pixel of the scattering map and
# #with a chosen radius. All the pixels INSIDE the square are flagged False
# def circularFilter(self, radius):
# for x in self.x_range:
# for y in self.y_range:
# self[x,y] = np.sqrt(x**2 + y**2) >= radius
# #A circular shaped filter, centered on the (0,0) pixel of the scattering map and
# #with a chosen radius. All the pixels OUTSIDE the square are flagged False
# def inverseCircularFilter(self, radius):
# for x in self.x_range:
# for y in self.y_range:
# self[x,y] = np.sqrt(x**2 + y**2) < radius
# #An angular sector shaped filter, centered on the (0,0) pixel of the scattering map.
# #All the pixels OUTSIDE the minimum and the maximum angle of the sector are flagged as
# #False
# def sectorFilter(self, min_angle, max_angle):
# for x in self.x_range:
# for y in self.y_range:
# self[x,y] = min_angle <= convertAngle(np.arctan2(y,x))*180/np.pi < max_angle
#Flags as False a list of single points to be excluded
def singlePointsFilter(self, point_list):
self.matrix = np.full((2*self.col_num, 2*self.row_num-1), True)
for point in point_list:
self[point] = False
#Removes all the filters
def noFilter(self):
self.matrix = np.full((2*self.detector_pixels, 2*self.detector_pixels), True)
#Makes the entry by entry boolean multiplication of a list of masks and return
#the final filter matrix
@staticmethod
def multiplyMask(mask_list):
detector_pixels = mask_list[0].detector_pixels
new_mask = squareFilterMatrix(detector_pixels)
new_mask.noFilter()
for x in new_mask.x_range:
for y in new_mask.y_range:
for mask in mask_list:
new_mask[x,y] = new_mask[x,y] and mask[x,y]
return new_mask
#Makes the entry by entry boolean sum of a list of masks and return
#the final filter matrix
@staticmethod
def sumMask(mask_list):
detector_pixels = mask_list[0].detector_pixels
new_mask = squareFilterMatrix(detector_pixels)
for x in new_mask.x_range:
for y in new_mask.y_range:
for mask in mask_list:
new_mask[x,y] = new_mask[x,y] or mask[x,y]
return new_mask