32 return StatusCode::FAILURE;
46 return StatusCode::FAILURE;
49 return StatusCode::SUCCESS;
56 std::vector<int> cluster_order(clusters->number);
59 std::vector<int> reverse_cluster_order(clusters->number, -1);
61 std::iota(cluster_order.begin(), cluster_order.end(), 0);
65 std::sort(cluster_order.begin(), cluster_order.end(), [&](
const int a,
const int b)
67 if (clusters->seedCellIndex[a] < 0)
74 else if (clusters->seedCellIndex[b] < 0)
78 return clusters->clusterEt[
a] > clusters->clusterEt[b];
81 unsigned int real_cluster_number = 0;
83 for (real_cluster_number = 0; real_cluster_number < cluster_order.size(); ++real_cluster_number)
85 if (clusters->seedCellIndex[real_cluster_number] < 0)
91 reverse_cluster_order[cluster_order[real_cluster_number]] = real_cluster_number;
95 cluster_order.resize(real_cluster_number);
97 final_clusters->number = real_cluster_number;
98 final_clusters->has_deleted_clusters =
false;
103 if (clusters->has_cells_per_cluster())
105 for (
int i = 0; i < cell_info->number; ++i)
107 final_clusters->cells.tags[i] = 0;
109 for (
int i = 0;
i <
clusters->number_cells; ++
i)
111 const int real_cluster_index = reverse_cluster_order[
clusters->clusterIndices[
i]];
113 if (real_cluster_index < 0)
118 const int this_cell_index =
clusters->cells.indices[
i];
123 std::memcpy(&weight_as_int, &weight,
sizeof(
float));
128 if (weight_as_int == 0)
136 const ClusterTag this_tag = final_clusters->cells.tags[this_cell_index];
144 final_clusters->cells.tags[this_cell_index] =
ClusterTag::make_tag(real_cluster_index, weight_as_int, 0);
151 else if (weight > 0.5f)
155 else if (weight == 0.5f)
158 const int max_cluster = real_cluster_index > other_index ? real_cluster_index : other_index;
159 const int min_cluster = real_cluster_index > other_index ? other_index : real_cluster_index;
160 final_clusters->cells.tags[this_cell_index] =
ClusterTag::make_tag(max_cluster, weight_as_int, min_cluster);
164 final_clusters->cells.tags[this_cell_index] =
ClusterTag::make_tag(other_index, weight_as_int, real_cluster_index);
175 for (
int i = 0;
i <
clusters->number_cells; ++
i)
190 if (primary_index >= 0 && secondary_index >= 0)
194 else if (primary_index >= 0)
198 else if (secondary_index >= 0)
204 final_clusters->cells.tags[
i] = 0;
209 final_clusters->number_cells = cell_info->number;
214 auto update_prefix_sum = [&]()
216 int running_count = 0;
217 for (
int i = 1;
i <= final_clusters->number; ++
i)
219 running_count += final_clusters->cellsPrefixSum[
i];
220 final_clusters->cellsPrefixSum[
i] = running_count;
222 final_clusters->number_cells = running_count;
225 for (
int i = 0;
i <= final_clusters->number; ++
i)
227 final_clusters->cellsPrefixSum[
i] = 0;
228 if (i < final_clusters->
number)
235 if (!
clusters->has_cells_per_cluster())
237 for (
int i = 0;
i <
clusters->number_cells; ++
i)
250 if (primary_index >= 0)
252 final_clusters->cellsPrefixSum[primary_index + 1] += 1;
255 if (secondary_index >= 0)
257 final_clusters->cellsPrefixSum[secondary_index + 1] += 1;
263 for (
int i = 0;
i <
clusters->number_cells; ++
i)
280 std::memcpy(&weight, &weight_as_int,
sizeof(
float));
282 if (primary_index >= 0)
284 const int this_cell_start = final_clusters->cellsPrefixSum[primary_index];
286 int & this_cell_count =
clusters->clusterIndices[primary_index];
292 const int this_index_in_cluster = this_cell_start + this_cell_count * is_seed_cell;
294 final_clusters->cells.indices[this_index_in_cluster] =
i;
295 final_clusters->cellWeights[this_index_in_cluster] = 1.0f -
weight;
296 final_clusters->clusterIndices[this_index_in_cluster] = primary_index;
299 if (secondary_index >= 0)
301 const int this_cell_start = final_clusters->cellsPrefixSum[secondary_index];
303 int & this_cell_count =
clusters->clusterIndices[secondary_index];
309 const int this_index_in_cluster = this_cell_start + this_cell_count * is_seed_cell;
311 final_clusters->cells.indices[this_index_in_cluster] =
i;
312 final_clusters->cellWeights[this_index_in_cluster] =
weight;
313 final_clusters->clusterIndices[this_index_in_cluster] = primary_index;
319 final_clusters->cellsPrefixSum[0] = 0;
321 for (
unsigned int i = 0;
i < cluster_order.size(); ++
i)
323 const int original_cluster = cluster_order[
i];
324 final_clusters->cellsPrefixSum[
i + 1] =
clusters->cellsPrefixSum[original_cluster + 1] -
clusters->cellsPrefixSum[original_cluster];
329 for (
unsigned int i = 0;
i < cluster_order.size(); ++
i)
331 const int original_cluster = cluster_order[
i];
333 int new_index = final_clusters->cellsPrefixSum[
i];
335 for (
int j =
clusters->cellsPrefixSum[original_cluster]; j < clusters->cellsPrefixSum[original_cluster + 1]; ++j, ++new_index)
337 final_clusters->cells.indices[new_index] =
clusters->cells.indices[j];
338 final_clusters->cellWeights[new_index] =
clusters->cellWeights[j];
339 final_clusters->clusterIndices[new_index] =
clusters->clusterIndices[j];
348 for (
unsigned int i = 0;
i < cluster_order.size(); ++
i)
350 final_clusters->clusterEnergy[
i] =
clusters->clusterEnergy[cluster_order[
i]];
351 final_clusters->clusterEt[
i] =
clusters->clusterEt[cluster_order[
i]];
352 final_clusters->clusterEta[
i] =
clusters->clusterEta[cluster_order[
i]];
353 final_clusters->clusterPhi[
i] =
clusters->clusterPhi[cluster_order[
i]];
354 final_clusters->seedCellIndex[
i] =
clusters->seedCellIndex[cluster_order[
i]];
358 m_filePrefix, m_fileSuffix, m_numWidth);
362 return StatusCode::FAILURE;
365 return StatusCode::SUCCESS;