7 #ifndef CALORECGPU_ETAPHIMAP_H
8 #define CALORECGPU_ETAPHIMAP_H
29 template <
int eta_gr
id,
int phi_gr
id,
bool respect_deltas,
bool continuous>
32 template <
int eta_gr
id,
int phi_gr
id,
bool respect_deltas>
35 friend struct EtaPhiMapEntry<eta_grid, phi_grid, respect_deltas, false>;
37 static constexpr
int s_max_overlap_cells = 10;
49 int cells [eta_grid][phi_grid][s_max_overlap_cells];
50 float corner_eta [eta_grid][phi_grid][s_max_overlap_cells];
51 float corner_phi [eta_grid][phi_grid][s_max_overlap_cells];
65 return floorf((
eta - eta_min) / delta_eta);
72 const float frac = (
eta - eta_min) / delta_eta;
73 const float rounded = floorf(
frac);
89 const float rounded = floorf(
frac);
96 for (
int i = 0;
i < s_max_overlap_cells; ++
i)
101 corner_eta [
eta][
phi][
i] = eta_corner;
102 corner_phi [
eta][
phi][
i] = phi_corner;
107 printf(
"Unable to store %d: %d %d %d %d (%d %d %d %d)\n",
cell,
110 eta, eta_grid,
phi, phi_grid );
116 int eta_coord_start = 0, eta_coord_end = 0, phi_coord_start = 0, phi_coord_end = 0;
117 int phi_coord_extra_neg = 0, phi_coord_extra_pos = 0;
118 float eta_fraction_start = 0.f, eta_fraction_end = 0.f, phi_fraction_start = 0.f, phi_fraction_end = 0.f;
119 float phi_fraction_extra_neg = 0.f, phi_fraction_extra_pos = 0.f;
121 eta_coord_start = eta_coordinate(cell_eta - cell_deta / 2, eta_fraction_start);
122 eta_coord_end = eta_coordinate(cell_eta + cell_deta / 2, eta_fraction_end);
123 phi_coord_start = phi_coordinate(cell_phi - cell_dphi / 2, phi_fraction_start);
124 phi_coord_end = phi_coordinate(cell_phi + cell_dphi / 2, phi_fraction_end);
126 phi_coord_extra_neg = phi_coordinate(cell_phi + cell_dphi / 2 - 2 * Helpers::Constants::pi<float>, phi_fraction_extra_neg);
127 phi_coord_extra_pos = phi_coordinate(cell_phi - cell_dphi / 2 + 2 * Helpers::Constants::pi<float>, phi_fraction_extra_pos);
129 if ((eta_coord_end == eta_coord_start && (eta_fraction_start > 0 || eta_fraction_end < 1)) ||
130 (phi_coord_end == phi_coord_start && (phi_fraction_start > 0 || phi_fraction_end < 1)) )
133 printf(
"Something strange going on with cell %d: %f %f %f %f %f %f (%d %d %f %f | %d %d %f %f)\n",
134 cell, cell_eta, cell_deta, delta_eta, cell_phi, cell_dphi,
delta_phi,
135 eta_coord_start, eta_coord_end, eta_fraction_start, eta_fraction_end,
136 phi_coord_start, phi_coord_end, phi_fraction_start, phi_fraction_end);
141 if (eta_coord_start < 0 || eta_coord_end < 0 || phi_coord_start < 0 || phi_coord_end < 0 ||
142 eta_fraction_start < 0 || eta_fraction_end < 0 || phi_fraction_start < 0 || phi_fraction_end < 0)
145 printf(
"Underflowing cell %d: %f %f %f %f %f %f (%d %d %f %f | %d %d %f %f) [%f %f]\n",
146 cell, cell_eta, cell_deta, cell_eta - cell_deta / 2, cell_phi, cell_dphi, cell_phi - cell_dphi / 2,
147 eta_coord_start, eta_coord_end, eta_fraction_start, eta_fraction_end,
148 phi_coord_start, phi_coord_end, phi_fraction_start, phi_fraction_end,
154 if (eta_coord_start >= eta_grid || eta_coord_end >= eta_grid || phi_coord_start >= phi_grid || phi_coord_end >= phi_grid ||
155 eta_fraction_start > 1 || eta_fraction_end > 1 || phi_fraction_start > 1 || phi_fraction_end > 1)
158 printf(
"Overflowing cell %d: %f %f %f %f %f %f (%d %d %f %f | %d %d %f %f) [%f %f]\n",
159 cell, cell_eta, cell_deta, cell_eta + cell_deta / 2, cell_phi, cell_dphi, cell_phi + cell_dphi / 2,
160 eta_coord_start, eta_coord_end, eta_fraction_start, eta_fraction_end,
161 phi_coord_start, phi_coord_end, phi_fraction_start, phi_fraction_end,
167 for (
int eta_c = eta_coord_start; eta_c <= eta_coord_end; ++eta_c)
169 const float eta_corner = ( eta_c == eta_coord_start ?
170 -eta_fraction_start : ( eta_c == eta_coord_end ?
171 eta_fraction_end : 0.f )
173 for (
int phi_c = 0; phi_c <= phi_coord_extra_neg; ++phi_c)
175 const float phi_corner = ( phi_c == phi_coord_extra_neg ?
176 phi_fraction_extra_neg : 0.f );
178 add_cell_to_grid(
cell, eta_corner, phi_corner, eta_c, phi_c);
181 for (
int phi_c = phi_coord_start; phi_c <= phi_coord_end; ++phi_c)
183 const float phi_corner = ( phi_c == phi_coord_start ?
184 -phi_fraction_start : ( phi_c == phi_coord_end ?
185 phi_fraction_end : 0.f )
187 add_cell_to_grid(
cell, eta_corner, phi_corner, eta_c, phi_c);
189 for (
int phi_c = phi_coord_extra_pos; phi_c < phi_grid; ++phi_c)
191 const float phi_corner = ( phi_c == phi_coord_extra_pos ?
192 -phi_fraction_extra_pos : 0.f );
193 add_cell_to_grid(
cell, eta_corner, phi_corner, eta_c, phi_c);
200 const int eta_coord = eta_coordinate(cell_eta);
201 const int phi_coord = phi_coordinate(cell_phi);
203 if (eta_coord < 0 || eta_coord >= eta_grid || phi_coord < 0 || phi_coord >= phi_grid)
206 printf(
"Out of bounds cell: %d | %f %d %f %f %d | %f %d %f %f %d\n",
cell,
207 cell_eta, eta_coord, eta_min, eta_max, eta_grid,
208 cell_phi, phi_coord, phi_min, phi_max, phi_grid );
213 add_cell_to_grid(
cell, cell_eta, cell_phi, eta_coord, phi_coord);
218 const float max_eta,
const float max_phi,
219 const float deta,
const float dphi)
221 for (
int i = 0;
i < eta_grid; ++
i)
223 for (
int j = 0; j < phi_grid; ++j)
225 for (
int k = 0;
k < s_max_overlap_cells; ++
k)
242 constexpr
void register_cell(
const int cell,
const float cell_eta,
const float cell_phi,
const float cell_deta,
const float cell_dphi)
248 register_cell_with_deltas(
cell, cell_eta, cell_phi, cell_deta, cell_dphi);
252 register_cell_center(
cell, cell_eta, cell_phi);
261 constexpr
void initialize(
const float min_eta_neg,
const float min_phi_neg,
262 const float ,
const float ,
263 const float ,
const float ,
264 const float max_eta_pos,
const float max_phi_pos,
265 const float deta,
const float dphi)
267 initialize(min_eta_neg, min_phi_neg, max_eta_pos, max_phi_pos, deta, dphi);
272 return eta_grid * phi_grid * s_max_overlap_cells *
sizeof(
int);
295 int (*casted)[eta_grid][phi_grid][s_max_overlap_cells] = (
int (*)[eta_grid][phi_grid][s_max_overlap_cells])
buffer;
297 int (&cell_arr_dest)[eta_grid][phi_grid][s_max_overlap_cells] = *casted;
301 memcpy(&(cell_arr_dest[0][0][0]), &(
cells[0][0][0]), finish_initializing_buffer_size());
303 bool keep_going =
true;
305 bool second_phase =
false;
314 auto propagate_cell = [&](
const int eta_c,
const int phi_c,
const int input_cell,
315 const float input_eta,
const float input_phi,
const bool only_empty )
317 if (only_empty &&
cells[eta_c][phi_c][0] >= 0)
321 int to_add_index = 0;
322 for (; to_add_index < s_max_overlap_cells; ++to_add_index)
324 if (cell_arr_dest[eta_c][phi_c][to_add_index] == input_cell)
328 else if (cell_arr_dest[eta_c][phi_c][to_add_index] < 0)
333 if (to_add_index == s_max_overlap_cells)
336 printf(
"Unable to store %d: %d %d %d %d (%d %d %d %d)\n", input_cell,
337 cell_arr_dest[eta_c][phi_c][0], cell_arr_dest[eta_c][phi_c][1],
338 cell_arr_dest[eta_c][phi_c][2], cell_arr_dest[eta_c][phi_c][3],
339 eta_c, eta_grid, phi_c, phi_grid );
344 cell_arr_dest [eta_c][phi_c][to_add_index] = input_cell;
345 corner_eta [eta_c][phi_c][to_add_index] = input_eta;
346 corner_phi [eta_c][phi_c][to_add_index] = input_phi;
351 auto process_cell = [&](
const int eta_c,
const int phi_c,
const int cell_index,
const bool only_empty)
353 const int this_cell =
cells [eta_c][phi_c][cell_index];
354 const float this_eta = corner_eta [eta_c][phi_c][cell_index];
355 const float this_phi = corner_phi [eta_c][phi_c][cell_index];
357 for (
int delta_eta = -1; delta_eta <= 1; ++delta_eta)
359 const int target_eta_c = eta_c + delta_eta;
360 if (target_eta_c < 0 || target_eta_c >= eta_grid)
367 if (target_phi_c < 0)
369 target_phi_c = phi_coordinate(phi_min +
delta_phi * target_phi_c + 2 * Helpers::Constants::pi<float>);
370 if (target_phi_c < 0 || target_phi_c >= phi_grid)
375 else if (target_phi_c >= phi_grid)
377 target_phi_c = phi_coordinate(phi_min +
delta_phi * target_phi_c - 2 * Helpers::Constants::pi<float>);
378 if (target_phi_c < 0 || target_phi_c >= phi_grid)
383 propagate_cell(target_eta_c, target_phi_c, this_cell, this_eta, this_phi, only_empty);
391 for (
int eta_c = 0; eta_c < eta_grid; ++eta_c)
393 for (
int phi_c = 0; phi_c < phi_grid; ++phi_c)
395 const auto & this_square_cells =
cells[eta_c][phi_c];
398 if (this_square_cells[0] >= 0 && this_square_cells[1] < 0)
400 process_cell(eta_c, phi_c, 0,
false);
405 for (
int cell_index = 0; cell_index < s_max_overlap_cells; ++cell_index)
407 if (this_square_cells[cell_index] < 0)
411 process_cell(eta_c, phi_c, cell_index,
true);
416 memcpy(&(
cells[0][0][0]), &(cell_arr_dest[0][0][0]), finish_initializing_buffer_size());
417 if (!keep_going && !second_phase)
436 float frac_eta = 0.f, frac_phi = 0.f;
438 const int eta_coord = eta_coordinate(test_eta, frac_eta);
439 const int phi_coord = phi_coordinate(test_phi, frac_phi);
441 if ( test_eta < eta_min || test_eta > eta_max ||
442 test_phi < phi_min || test_phi > phi_max ||
443 eta_coord < 0 || eta_coord >= eta_grid ||
444 phi_coord < 0 || phi_coord >= phi_grid )
458 auto check_coord = [](
const float test,
const float target)
479 for (
int i = 0;
i < s_max_overlap_cells; ++
i)
481 if (
cells[eta_coord][phi_coord][
i] < 0)
485 if ( check_coord(frac_eta, corner_eta[eta_coord][phi_coord][
i]) &&
486 check_coord(frac_phi, corner_phi[eta_coord][phi_coord][
i]) )
488 cell_arr[num_cells] =
cells[eta_coord][phi_coord][
i];
498 for (
int i = 0;
i < s_max_overlap_cells; ++
i)
500 const int this_cell =
cells[eta_coord][phi_coord][
i];
505 const float delta_eta = corner_eta[eta_coord][phi_coord][
i] - test_eta;
506 const float delta_phi = Helpers::angular_difference(corner_phi[eta_coord][phi_coord][
i], test_phi);
531 float frac_eta = 0.f, frac_phi = 0.f;
533 const int eta_coord = eta_coordinate(test_eta, frac_eta);
534 const int phi_coord = phi_coordinate(test_phi, frac_phi);
536 if ( test_eta < eta_min || test_eta > eta_max ||
537 test_phi < phi_min || test_phi > phi_max ||
538 eta_coord < 0 || eta_coord >= eta_grid ||
539 phi_coord < 0 || phi_coord >= phi_grid )
548 auto check_coord = [](
const float test,
const float target)
569 for (
int i = 0;
i < s_max_overlap_cells; ++
i)
571 if (
cells[eta_coord][phi_coord][
i] < 0)
575 if ( check_coord(frac_eta, corner_eta[eta_coord][phi_coord][
i]) &&
576 check_coord(frac_phi, corner_phi[eta_coord][phi_coord][
i]) )
595 template <
int eta_gr
id,
int phi_gr
id,
bool respect_deltas>
602 constexpr
void register_cell(
const int cell,
const float cell_eta,
const float cell_phi,
const float cell_deta,
const float cell_dphi)
606 pos.register_cell(
cell, cell_eta, cell_phi, cell_deta, cell_dphi);
614 constexpr
void initialize(
const float min_eta_neg,
const float min_phi_neg,
615 const float max_eta_neg,
const float max_phi_neg,
616 const float min_eta_pos,
const float min_phi_pos,
617 const float max_eta_pos,
const float max_phi_pos,
618 const float deta,
const float dphi)
620 pos.initialize(min_eta_pos, min_phi_pos, max_eta_pos, max_phi_pos, deta, dphi);
621 neg.
initialize(min_eta_neg, min_phi_neg, max_eta_neg, max_phi_neg, deta, dphi);
648 ret =
pos.get_possible_cells_from_coords(test_eta, test_phi, cell_arr);
661 return pos.has_cell_in_coords(test_eta, test_phi);
707 static_assert(
NumSamplings == 28,
"Written under the assumption there are 28 samplings.");
710 template <
class Func,
class ...
Args>
713 F(sampling_0, std::forward<Args>(
args)...);
714 F(sampling_1, std::forward<Args>(
args)...);
715 F(sampling_2, std::forward<Args>(
args)...);
716 F(sampling_3, std::forward<Args>(
args)...);
717 F(sampling_4, std::forward<Args>(
args)...);
718 F(sampling_5, std::forward<Args>(
args)...);
719 F(sampling_6, std::forward<Args>(
args)...);
720 F(sampling_7, std::forward<Args>(
args)...);
721 F(sampling_8, std::forward<Args>(
args)...);
722 F(sampling_9, std::forward<Args>(
args)...);
723 F(sampling_10, std::forward<Args>(
args)...);
724 F(sampling_11, std::forward<Args>(
args)...);
725 F(sampling_12, std::forward<Args>(
args)...);
726 F(sampling_13, std::forward<Args>(
args)...);
727 F(sampling_14, std::forward<Args>(
args)...);
728 F(sampling_15, std::forward<Args>(
args)...);
729 F(sampling_16, std::forward<Args>(
args)...);
730 F(sampling_17, std::forward<Args>(
args)...);
731 F(sampling_18, std::forward<Args>(
args)...);
732 F(sampling_19, std::forward<Args>(
args)...);
733 F(sampling_20, std::forward<Args>(
args)...);
734 F(sampling_21, std::forward<Args>(
args)...);
735 F(sampling_22, std::forward<Args>(
args)...);
736 F(sampling_23, std::forward<Args>(
args)...);
737 F(sampling_24, std::forward<Args>(
args)...);
738 F(sampling_25, std::forward<Args>(
args)...);
739 F(sampling_26, std::forward<Args>(
args)...);
740 F(sampling_27, std::forward<Args>(
args)...);
744 template <
class Func,
class ...
Args>
747 F(sampling_0, std::forward<Args>(
args)...);
748 F(sampling_1, std::forward<Args>(
args)...);
749 F(sampling_2, std::forward<Args>(
args)...);
750 F(sampling_3, std::forward<Args>(
args)...);
751 F(sampling_4, std::forward<Args>(
args)...);
752 F(sampling_5, std::forward<Args>(
args)...);
753 F(sampling_6, std::forward<Args>(
args)...);
754 F(sampling_7, std::forward<Args>(
args)...);
755 F(sampling_8, std::forward<Args>(
args)...);
756 F(sampling_9, std::forward<Args>(
args)...);
757 F(sampling_10, std::forward<Args>(
args)...);
758 F(sampling_11, std::forward<Args>(
args)...);
759 F(sampling_12, std::forward<Args>(
args)...);
760 F(sampling_13, std::forward<Args>(
args)...);
761 F(sampling_14, std::forward<Args>(
args)...);
762 F(sampling_15, std::forward<Args>(
args)...);
763 F(sampling_16, std::forward<Args>(
args)...);
764 F(sampling_17, std::forward<Args>(
args)...);
765 F(sampling_18, std::forward<Args>(
args)...);
766 F(sampling_19, std::forward<Args>(
args)...);
767 F(sampling_20, std::forward<Args>(
args)...);
768 F(sampling_21, std::forward<Args>(
args)...);
769 F(sampling_22, std::forward<Args>(
args)...);
770 F(sampling_23, std::forward<Args>(
args)...);
771 F(sampling_24, std::forward<Args>(
args)...);
772 F(sampling_25, std::forward<Args>(
args)...);
773 F(sampling_26, std::forward<Args>(
args)...);
774 F(sampling_27, std::forward<Args>(
args)...);
778 template <
class Func,
class ...
Args>
784 F(sampling_0, std::forward<Args>(
args)...);
787 F(sampling_1, std::forward<Args>(
args)...);
790 F(sampling_2, std::forward<Args>(
args)...);
793 F(sampling_3, std::forward<Args>(
args)...);
796 F(sampling_4, std::forward<Args>(
args)...);
799 F(sampling_5, std::forward<Args>(
args)...);
802 F(sampling_6, std::forward<Args>(
args)...);
805 F(sampling_7, std::forward<Args>(
args)...);
808 F(sampling_8, std::forward<Args>(
args)...);
811 F(sampling_9, std::forward<Args>(
args)...);
814 F(sampling_10, std::forward<Args>(
args)...);
817 F(sampling_11, std::forward<Args>(
args)...);
820 F(sampling_12, std::forward<Args>(
args)...);
823 F(sampling_13, std::forward<Args>(
args)...);
826 F(sampling_14, std::forward<Args>(
args)...);
829 F(sampling_15, std::forward<Args>(
args)...);
832 F(sampling_16, std::forward<Args>(
args)...);
835 F(sampling_17, std::forward<Args>(
args)...);
838 F(sampling_18, std::forward<Args>(
args)...);
841 F(sampling_19, std::forward<Args>(
args)...);
844 F(sampling_20, std::forward<Args>(
args)...);
847 F(sampling_21, std::forward<Args>(
args)...);
850 F(sampling_22, std::forward<Args>(
args)...);
853 F(sampling_23, std::forward<Args>(
args)...);
856 F(sampling_24, std::forward<Args>(
args)...);
859 F(sampling_25, std::forward<Args>(
args)...);
862 F(sampling_26, std::forward<Args>(
args)...);
865 F(sampling_27, std::forward<Args>(
args)...);
873 template <
class Func,
class ...
Args>
879 F(sampling_0, std::forward<Args>(
args)...);
882 F(sampling_1, std::forward<Args>(
args)...);
885 F(sampling_2, std::forward<Args>(
args)...);
888 F(sampling_3, std::forward<Args>(
args)...);
891 F(sampling_4, std::forward<Args>(
args)...);
894 F(sampling_5, std::forward<Args>(
args)...);
897 F(sampling_6, std::forward<Args>(
args)...);
900 F(sampling_7, std::forward<Args>(
args)...);
903 F(sampling_8, std::forward<Args>(
args)...);
906 F(sampling_9, std::forward<Args>(
args)...);
909 F(sampling_10, std::forward<Args>(
args)...);
912 F(sampling_11, std::forward<Args>(
args)...);
915 F(sampling_12, std::forward<Args>(
args)...);
918 F(sampling_13, std::forward<Args>(
args)...);
921 F(sampling_14, std::forward<Args>(
args)...);
924 F(sampling_15, std::forward<Args>(
args)...);
927 F(sampling_16, std::forward<Args>(
args)...);
930 F(sampling_17, std::forward<Args>(
args)...);
933 F(sampling_18, std::forward<Args>(
args)...);
936 F(sampling_19, std::forward<Args>(
args)...);
939 F(sampling_20, std::forward<Args>(
args)...);
942 F(sampling_21, std::forward<Args>(
args)...);
945 F(sampling_22, std::forward<Args>(
args)...);
948 F(sampling_23, std::forward<Args>(
args)...);
951 F(sampling_24, std::forward<Args>(
args)...);
954 F(sampling_25, std::forward<Args>(
args)...);
957 F(sampling_26, std::forward<Args>(
args)...);
960 F(sampling_27, std::forward<Args>(
args)...);
975 template <
class Entry>
976 constexpr
void operator() (Entry &
entry)
const
984 template <
class Entry>
985 constexpr
void operator() (Entry &
entry,
986 const float min_eta_neg,
const float min_phi_neg,
987 const float max_eta_neg,
const float max_phi_neg,
988 const float min_eta_pos,
const float min_phi_pos,
989 const float max_eta_pos,
const float max_phi_pos,
990 const float delta_eta,
const float delta_phi )
const
992 entry.initialize(min_eta_neg, min_phi_neg, max_eta_neg, max_phi_neg,
993 min_eta_pos, min_phi_pos, max_eta_pos, max_phi_pos,
1000 template <
class Entry>
1002 const float cell_eta,
const float cell_phi,
1003 const float cell_deta,
const float cell_dphi )
const
1005 entry.register_cell(
cell, cell_eta, cell_phi, cell_deta, cell_dphi);
1011 template <
class Entry>
1012 constexpr
void operator() (Entry &
entry,
size_t & ret)
const
1014 using namespace std;
1015 ret =
max(ret,
entry.finish_initializing_buffer_size());
1021 template <
class Entry>
1032 template <
class Entry>
1033 constexpr
void operator() (Entry &
entry,
int & ret,
const float test_eta,
const float test_phi,
int * cell_arr)
const
1035 ret =
entry.get_possible_cells_from_coords(test_eta, test_phi, cell_arr);
1041 template <
class Entry>
1042 constexpr
void operator() (Entry &
entry,
bool & ret,
const float test_eta,
const float test_phi)
const
1044 ret =
entry.has_cell_in_coords(test_eta, test_phi);
1050 template <
class Entry>
1051 constexpr
void operator() (Entry &
entry,
int & ret,
const float test_eta,
const float test_phi,
int * cell_arr)
const
1053 ret +=
entry.get_possible_cells_from_coords(test_eta, test_phi, cell_arr + ret);
1059 template <
class Entry>
1060 constexpr
void operator() (Entry &
entry,
bool & ret,
const float test_eta,
const float test_phi)
const
1062 ret = ( ret ||
entry.has_cell_in_coords(test_eta, test_phi) );
1077 const float min_eta_neg,
const float min_phi_neg,
1078 const float max_eta_neg,
const float max_phi_neg,
1079 const float min_eta_pos,
const float min_phi_pos,
1080 const float max_eta_pos,
const float max_phi_pos,
1081 const float delta_eta,
const float delta_phi)
1084 min_eta_neg, min_phi_neg,
1085 max_eta_neg, max_phi_neg,
1086 min_eta_pos, min_phi_pos,
1087 max_eta_pos, max_phi_pos,
1091 constexpr
void register_cell(
const int cell,
const int sampling,
const float cell_eta,
const float cell_phi,
const float cell_deta,
const float cell_dphi)
1154 #endif //CALORECGPU_ETAPHIMAP_H