-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustering.py
316 lines (257 loc) · 10.5 KB
/
clustering.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
'''
This file does clustering to the hits in calorimeter.
'''
import numpy as np
import os.path
from itertools import product
from ROOT import TCanvas, TH2F, gStyle, TColor
class Tower:
'''
Each tower object has "seed" for the number of the cluster
it is assigned to.
'''
def __init__(self, tower_hits):
self.hits = tower_hits
self.sector = tower_hits[0].sector
self.pad = tower_hits[0].pad
self.energy = sum(hit.energy for hit in tower_hits)
self.n_pads = len(tower_hits)
self.seed = -1
def make_towers_list(hits_list):
"""Return list of towers objects out of hits list"""
towers_pos = set((hit.sector, hit.pad) for hit in hits_list)
towers = []
for pos in towers_pos:
tower_hits = [hit for hit in hits_list if (hit.sector, hit.pad) == pos]
towers.append(Tower(tower_hits))
# Sort towers by energy
towers.sort(key=lambda x: x.energy, reverse=True)
return towers
def set_tower_seeds(towers_list):
"""Assign cluster indices to the towers. Make towers seeds"""
seed_idx = 0
for tower in towers_list:
if tower.n_pads == 1:
continue
sector = tower.sector
pad = tower.pad
# Check if it is local maximum
for tower_neighbor in towers_list:
if (tower_neighbor.sector in range(sector - 1, sector + 2)
and tower_neighbor.pad in range(pad - 1, pad + 2)
and tower_neighbor.energy > tower.energy):
break
else:
# This is local maximum with more than 1 pad.
tower.seed = seed_idx
seed_idx += 1
# Assign others not seed towers towers to clusters
def find_neighbor_assign_cluster(towers_list):
if not any(tower.seed != -1 for tower in towers_list):
return
r = 1
while any(tower.seed == -1 for tower in towers_list):
changing = True
while changing:
changing = False
for tower in towers_list:
# plot_sensor_clusters(towers_list)
if tower.seed != -1:
continue
sector = tower.sector
pad = tower.pad
neighbors = []
for tower_neighbor in towers_list:
if (tower_neighbor.sector in range(sector - r, sector + r + 1)
and tower_neighbor.pad in range(pad - r, pad + r + 1)
and tower_neighbor.seed != -1):
neighbors.append(tower_neighbor)
# It has neighbors assigned to the cluster
if len(neighbors) > 0:
neighbors.sort(key=lambda x: x.energy, reverse=True)
tower.seed = neighbors[0].seed
changing = True
r += 1
class CalCluster:
def __init__(self, cluster_hits, n_towers):
self.hits = cluster_hits
self.energy = sum(hit.energy for hit in cluster_hits)
self.n_towers = n_towers
self.n_pads = len(cluster_hits)
weights = [max(0, 3.4 + np.log(hit.energy / self.energy)) for hit in cluster_hits]
sum_of_weights = sum(weights)
if sum_of_weights != 0:
self.sector = sum(getattr(hit, "sector") * weight for hit, weight in zip(cluster_hits, weights)) / sum_of_weights
self.pad = sum(getattr(hit, "pad") * weight for hit, weight in zip(cluster_hits, weights)) / sum_of_weights
self.layer = sum(getattr(hit, "layer") * weight for hit, weight in zip(cluster_hits, weights)) / sum_of_weights
self.x = sum(getattr(hit, "x") * weight for hit, weight in zip(cluster_hits, weights)) / sum_of_weights
self.y = sum(getattr(hit, "y") * weight for hit, weight in zip(cluster_hits, weights)) / sum_of_weights
else:
self.sector = -999
self.pad = -999
self.layer = -999
self.x = -999
self.y = -999
def merge(self, cluster2):
self.hits.extend(cluster2.hits)
self.energy = sum(hit.energy for hit in self.hits)
self.n_towers += cluster2.n_towers
self.n_pads = len(self.hits)
weights = [max(0, 3.4 + np.log(hit.energy / self.energy)) for hit in self.hits]
sum_of_weights = sum(weights)
if sum_of_weights != 0:
self.sector = sum(getattr(hit, "sector") * weight for hit, weight in zip(self.hits, weights)) / sum_of_weights
self.pad = sum(getattr(hit, "pad") * weight for hit, weight in zip(self.hits, weights)) / sum_of_weights
self.layer = sum(getattr(hit, "layer") * weight for hit, weight in zip(self.hits, weights)) / sum_of_weights
self.x = sum(getattr(hit, "x") * weight for hit, weight in zip(self.hits, weights)) / sum_of_weights
self.y = sum(getattr(hit, "y") * weight for hit, weight in zip(self.hits, weights)) / sum_of_weights
else:
self.sector = -999
self.pad = -999
self.layer = -999
self.x = -999
self.y = -999
def merge_clusters(clusters_list):
"""Merge pair of clusters if they meet following condition
This is OPTIONAL. Working with TB20. First do the clustering without it!
"""
while True:
for clst1, clst2 in product(clusters_list, clusters_list):
if clst1 == clst2:
continue
distance = abs(clst1.y - clst2.y)
ratio = clst2.energy / clst1.energy
if distance < 7.5 or (ratio < 0.032 * (20. - distance)):
clst1.merge(clst2)
clusters_list.remove(clst2)
break
else:
break
# This is the main function which does everything.
def make_cal_clusters(hits_list):
"""Return cluster list out of towers list"""
towers_list = make_towers_list(hits_list)
# This part manages clustering for the calorimeter
set_tower_seeds(towers_list)
find_neighbor_assign_cluster(towers_list)
clusters = []
if not towers_list:
return clusters
n_clusters = max(tower.seed for tower in towers_list) + 1
for i in range(n_clusters):
cluster_hits = []
n_towers = 0
for tower in towers_list:
if tower.seed == i:
n_towers += 1
cluster_hits.extend(tower.hits)
clusters.append(CalCluster(cluster_hits, n_towers))
clusters.sort(key=lambda x: x.energy, reverse=True)
# Comment here to exclude merging.
merge_clusters(clusters)
clusters.sort(key=lambda x: x.energy, reverse=True)
return clusters
class TrCluster:
def __init__(self, cluster_hits):
self.hits = cluster_hits
self.n_pads = len(cluster_hits)
weights = [hit.energy for hit in cluster_hits]
self.energy = sum(weights)
if self.energy != 0:
self.sector = sum(getattr(hit, "sector") * weight for hit, weight in zip(cluster_hits, weights)) / self.energy
self.pad = sum(getattr(hit, "pad") * weight for hit, weight in zip(cluster_hits, weights)) / self.energy
self.x = sum(getattr(hit, "x") * weight for hit, weight in zip(cluster_hits, weights)) / self.energy
self.y = sum(getattr(hit, "y") * weight for hit, weight in zip(cluster_hits, weights)) / self.energy
else:
self.sector = -999
self.pad = -999
self.x = -999
self.y = -999
def make_tr_clusters(hits_list):
seed_idx = 0
for hit in hits_list:
sector = hit.sector
pad = hit.pad
# Check if it is local maximum
for hit_neighbor in hits_list:
if (hit_neighbor.sector in range(sector - 1, sector + 2)
and hit_neighbor.pad in range(pad - 1, pad + 2)
and hit_neighbor.energy > hit.energy):
break
else:
# This is local maximum.
hit.seed = seed_idx
seed_idx += 1
while any(hit.seed == -1 for hit in hits_list):
for hit in hits_list:
if hit.seed != -1:
continue
sector = hit.sector
pad = hit.pad
neighbors = []
for hit_neighbor in hits_list:
if (hit_neighbor.sector in range(sector - 1, sector + 2)
and hit_neighbor.pad in range(pad - 1, pad + 2)
and hit_neighbor.seed != -1):
neighbors.append(hit_neighbor)
# It has neighbors, check most energetic seed and assign
if len(neighbors) > 0:
neighbors.sort(key=lambda x: x.energy, reverse=True)
hit.seed = neighbors[0].seed
clusters = []
if not hits_list:
return clusters
n_clusters = max(hit.seed for hit in hits_list) + 1
for i in range(n_clusters):
cluster_hits = []
for hit in hits_list:
if hit.seed == i:
cluster_hits.append(hit)
clusters.append(TrCluster(cluster_hits))
clusters.sort(key=lambda x: x.energy, reverse=True)
return clusters
def make_clusters_lists(hits_tr1, hits_tr2, hits_cal):
clusters_tr1 = make_tr_clusters(hits_tr1)
clusters_tr2 = make_tr_clusters(hits_tr2)
clusters_cal = make_cal_clusters(hits_cal)
return clusters_tr1, clusters_tr2, clusters_cal
###########################################################
# These functions might be useful once to see how the event looks like.
# You can ignore or remove them.
def plot_sensor_energies(towers_list):
c = TCanvas("c", "c", 800, 1200)
# gStyle.SetPalette(0)
c.SetGridx(1)
c.SetGridy(1)
gStyle.SetOptStat(0)
h = TH2F("h", "title", 4, 0., 4., 44, 20., 64.)
h.GetXaxis().SetTitle("sector number")
h.GetYaxis().SetTitle("pad number")
for t in towers_list:
h.Fill(t.sector, t.pad, t.energy)
h.SetTitle("")
h.DrawCopy("colztext")
# input("wait plot sensor")
c.Print("./energies.png")
TColor.InvertPalette()
def plot_sensor_clusters(towers_list):
pic_number = 0
c = TCanvas("c", "c", 800, 1200)
c.SetGridx(1)
c.SetGridy(1)
gStyle.SetOptStat(0)
h = TH2F("h", "title", 4, 0., 4., 44, 20., 64.)
h.GetXaxis().SetTitle("sector number")
h.GetYaxis().SetTitle("pad number")
for t in towers_list:
if t.seed != -1:
h.Fill(t.sector, t.pad, t.seed + 1)
else:
h.Fill(t.sector, t.pad, t.seed)
h.SetTitle("")
h.DrawCopy("colz text")
# input("wait plot sensor")
while os.path.exists("./clustering{}.png".format(pic_number)):
pic_number += 1
c.Print("./clustering{}.png".format(pic_number))
pic_number += 1