7#ifndef CALORECGPU_ETAPHIMAP_H
8#define CALORECGPU_ETAPHIMAP_H
33 template <
int eta_gr
id,
int phi_gr
id,
bool respect_deltas,
bool continuous,
int sampling_number>
74 constexpr float start_eta(
const bool positive =
true)
const
95 constexpr float end_eta(
const bool positive =
true)
const
120 const float rounded = floorf(frac);
121 interval = frac - rounded;
122 const int casted =
static_cast<int>(rounded);
123 if ((casted == eta_grid || casted == 2 * eta_grid) && interval == 0.f)
126 return casted - 1 + (
eta > 0 && !continuous) * eta_grid;
130 return casted + (
eta > 0 && !continuous) * eta_grid;
144 const float rounded = floorf(frac);
145 interval = frac - rounded;
146 const int to_return =
static_cast<int>(rounded) % phi_grid;
147 return to_return + (to_return < 0) * phi_grid;
159 const bool phi_in_range =
true;
160 return eta_in_range && phi_in_range;
165 if (!continuous && eta_coord >= eta_grid)
182 constexpr void add_cell_to_grid(
const int cell,
const float eta_fraction,
const float phi_fraction,
const int eta,
const int phi)
186#if CALORECGPU_ETA_PHI_MAP_DEBUG
187 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: Attempt out of bounds store %d (%d): %f %f (%d / %d, %d / %d)\n",
188 cell, sampling_number, eta_fraction, phi_fraction,
eta, eta_grid,
phi, phi_grid);
204#if CALORECGPU_ETA_PHI_MAP_DEBUG
205 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: Unable to store %d (%d): ", cell, sampling_number);
221 const float d_eta_1 = fabsf(this_eta - cell_eta);
222 const float d_eta_2 = fabsf(this_eta +
delta_eta() - cell_eta);
227 printf(
" %f %f %f %f | %f %f %f %f (%f %f | %f %f %f %f)\n",
249 constexpr void register_cell(
const int cell,
const float cell_eta,
const float cell_phi,
const float cell_deta,
const float cell_dphi)
251 int start_eta_coord = -1, end_eta_coord = -1;
252 float start_eta_frac = 0.f, end_eta_frac = 0.f;
254 start_eta_coord =
eta_coordinate(cell_eta - cell_deta / 2, start_eta_frac);
255 end_eta_coord =
eta_coordinate(cell_eta + cell_deta / 2, end_eta_frac);
257 int start_phi_coord = -1, end_phi_coord = -1;
258 float start_phi_frac = 0.f, end_phi_frac = 0.f;
259 int start_phi_extra = -1, end_phi_extra = -1;
260 float start_phi_extra_frac = 0.f, end_phi_extra_frac = 0.f;
267 start_phi_frac = 0.f;
269 end_phi_coord = phi_grid - 1;
272 else if (cell_phi - cell_dphi / 2 <
s_phi_min)
275 start_phi_frac = 0.f;
277 end_phi_coord =
phi_coordinate(cell_phi + cell_dphi / 2, end_phi_frac);
281 end_phi_extra = phi_grid - 1;
282 end_phi_extra_frac = 1.f;
284 else if (cell_phi + cell_dphi / 2 >
s_phi_max)
286 start_phi_coord =
phi_coordinate(cell_phi - cell_dphi / 2, start_phi_frac);
288 end_phi_coord = phi_grid - 1;
294 start_phi_extra_frac = 1.f;
300 start_phi_coord =
phi_coordinate(cell_phi - cell_dphi / 2, start_phi_frac);
301 end_phi_coord =
phi_coordinate(cell_phi + cell_dphi / 2, end_phi_frac);
304 for (
int i = start_eta_coord; i <= end_eta_coord; ++i)
306 const float eta_frac = (i == start_eta_coord ? -start_eta_frac : i == end_eta_coord ? end_eta_frac : 0.f);
308 for (
int j = start_phi_coord; j <= end_phi_coord; ++j)
310 const float phi_frac = (j == start_phi_coord ? -start_phi_frac : j == end_phi_coord ? end_phi_frac : 0.f);
312 add_cell_to_grid(cell, (respect_deltas ? eta_frac : cell_eta), (respect_deltas ? phi_frac : cell_phi), i, j);
315 if (start_phi_extra >= 0)
317 for (
int j = start_phi_extra; j <= end_phi_extra; ++j)
319 const float phi_frac = (j == start_phi_extra ? -start_phi_extra_frac : j == end_phi_extra ? end_phi_extra_frac : 0.f);
321 add_cell_to_grid(cell, (respect_deltas ? eta_frac : cell_eta), (respect_deltas ? phi_frac : cell_phi), i, j);
331 for (
int j = 0; j < phi_grid; ++j)
341 constexpr void initialize(
const float min_eta,
const float max_eta)
356 m_delta_eta = ((max_eta - min_eta) / eta_grid) * 2;
359#if CALORECGPU_ETA_PHI_MAP_DEBUG
360 if (sampling_number < 24)
362 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: %d | %d | %f %f | %f %f | %f %f | %f %f\n",
363 sampling_number,
static_cast<int>(continuous),
438#if CALORECGPU_ETA_PHI_MAP_DEBUG
439 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: cannot add more cells! (%d)\n", sampling_number);
455 const int value =
eta * phi_grid +
phi;
494 auto calculate_minima = [&](
float & min_dist_eta,
float & min_dist_phi,
495 const float this_eta,
const float this_phi,
496 const float gridcell_eta,
const float gridcell_phi)
498 if (this_eta >= gridcell_eta && this_eta <= gridcell_eta +
delta_eta())
500 min_dist_eta = this_eta;
504 if (this_eta < gridcell_eta)
506 min_dist_eta = gridcell_eta;
508#if CALORECGPU_ETA_PHI_MAP_DEBUG
509 else if (this_eta <= gridcell_eta +
delta_eta())
511 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: Strange things going on with eta distance (%d): %f | %f %f\n",
512 sampling_number, this_eta, gridcell_eta, gridcell_eta +
delta_eta());
517 min_dist_eta = gridcell_eta +
delta_eta();
521 if (this_phi >= gridcell_phi && this_phi <= gridcell_phi +
delta_phi())
523 min_dist_phi = this_phi;
531 min_dist_phi = gridcell_phi;
533#if CALORECGPU_ETA_PHI_MAP_DEBUG
536 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: Strange things going on with phi distance (%d): %f | %f %f\n",
537 sampling_number, this_phi, gridcell_phi, gridcell_phi +
delta_phi());
542 min_dist_phi = gridcell_phi +
delta_phi();
547 auto calculate_dist = [](
const float e_1,
const float p_1,
548 const float e_2,
const float p_2)
559 const float this_eta,
560 const float this_phi,
561 const float min_dist_eta,
562 const float min_dist_phi)
566 int replace_count = 0;
568 const float new_dist_at_new_minimum = calculate_dist(this_eta, this_phi, min_dist_eta, min_dist_phi);
577 if (cells[i] == this_cell)
583 const float old_dist_at_new_minimum = calculate_dist(etas[i], phis[i], min_dist_eta, min_dist_phi);
585 if (old_dist_at_new_minimum <= new_dist_at_new_minimum)
591 const float new_dist_at_old_minimum = calculate_dist(this_eta, this_phi, min_etas[i], min_phis[i]);
592 const float old_dist_at_old_minimum = calculate_dist(etas[i], phis[i], min_etas[i], min_phis[i]);
594 if (new_dist_at_old_minimum < old_dist_at_old_minimum)
597 to_replace[replace_count] = i;
602 while (replace_count > 0 && i > 0)
604 const int orig = to_replace[replace_count - 1];
607 cells [orig] = cells [i - 1];
608 etas [orig] = etas [i - 1];
609 phis [orig] = phis [i - 1];
610 min_etas [orig] = min_etas [i - 1];
611 min_phis [orig] = min_phis [i - 1];
616 if (i < 0 || (i == 0 && replace_count > 0))
618#if CALORECGPU_ETA_PHI_MAP_DEBUG
619 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: Negative count on cell list update, somehow... (%d)\n", sampling_number);
627 cells [i] = this_cell;
630 min_etas [i] = min_dist_eta;
631 min_phis [i] = min_dist_phi;
634#if CALORECGPU_ETA_PHI_MAP_DEBUG
637 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: Warning: overfull list of overlapping cells: %d (%d)\n", i, sampling_number);
648 const float gridcell_eta,
649 const float gridcell_phi,
653 bool updated =
false;
656 const int this_cell =
m_cells[orig_eta][orig_phi][i];
666 float min_dist_eta = 0.f, min_dist_phi = 0.f;
668 calculate_minima(min_dist_eta, min_dist_phi, this_eta, this_phi, gridcell_eta, gridcell_phi);
670 updated = updated || update_cell_list(cells, etas, phis, min_etas, min_phis,
671 this_cell, this_eta, this_phi, min_dist_eta, min_dist_phi);
678 auto process_cell = [&](
const int eta,
const int phi)
680 const int phi_before = (
phi == 0 ? phi_grid - 1 :
phi - 1);
681 const int phi_after = (
phi == phi_grid - 1 ? 0 :
phi + 1);
682 const int eta_before = (
eta == 0 ||
eta == eta_grid ? -1 :
eta - 1);
683 const int eta_after = (
eta == eta_grid - 1 ||
eta == 2 * eta_grid - 1 ? -1 :
eta + 1);
700 calculate_minima(min_etas[i], min_phis[i], this_eta, this_phi, this_grid_eta, this_grid_phi);
705 added = added || add_possible_cells(temps.
cells[
eta][
phi],
708 this_grid_eta, this_grid_phi,
710 added = added || add_possible_cells(temps.
cells[
eta][
phi],
713 this_grid_eta, this_grid_phi,
718 added = added || add_possible_cells(temps.
cells[
eta][
phi],
721 this_grid_eta, this_grid_phi,
726 added = added || add_possible_cells(temps.
cells[
eta][
phi],
729 this_grid_eta, this_grid_phi,
769 const int phi_before = (
phi == 0 ? phi_grid - 1 :
phi - 1);
770 const int phi_after = (
phi == phi_grid - 1 ? 0 :
phi + 1);
771 const int eta_before = (
eta == 0 ||
eta == eta_grid ? -1 :
eta - 1);
793#if CALORECGPU_ETA_PHI_MAP_DEBUG
795 int equal_iter_counter = 0;
807#if CALORECGPU_ETA_PHI_MAP_DEBUG
810 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: Init %d: %d | %d %d (%d)\n",
823 ++equal_iter_counter;
824 if (equal_iter_counter >= 4)
835 printf(
" %d %d",
eta,
phi);
850 equal_iter_counter = 0;
855 equal_iter_counter = 0;
874#if CALORECGPU_ETA_PHI_MAP_DEBUG
875 printf(
"CALORECGPU ETA PHI MAP DEBUG OUTPUT: Finished %d: %d (%d %d)\n",
896 float frac_eta = 0.f, frac_phi = 0.f;
901 if (eta_coord < 0 || eta_coord >=
s_eta_grid_size || phi_coord < 0 || phi_coord >= phi_grid)
908 auto check_coord = [](
const float test,
const float target)
911 if (target > 0 && fabsf(test) <= fabsf(target))
915 else if (target < 0 && fabsf(test) >= fabsf(target))
919 else if (target == 0)
931 const int this_cell =
m_cells[eta_coord][phi_coord][i];
941 cell_arr[num_cells] = this_cell;
948 float distance = 1e38f;
953 const int this_cell =
m_cells[eta_coord][phi_coord][i];
960 const float this_delta_eta =
m_eta_coordinates[eta_coord][phi_coord][i] - test_eta;
966 const float this_dist = fabsf(this_delta_eta) + fabsf(this_delta_phi);
968 if (this_dist < distance || (this_dist == distance && this_cell > ret))
970 distance = this_dist;
992 float frac_eta = 0.f, frac_phi = 0.f;
997 if (eta_coord < 0 || eta_coord >=
s_eta_grid_size || phi_coord < 0 || phi_coord >= phi_grid)
1004 auto check_coord = [](
const float test,
const float target)
1006 using namespace std;
1007 if (target > 0 && fabsf(test) <= fabsf(target))
1011 else if (target < 0 && fabsf(test) >= fabsf(target))
1015 else if (target == 0)
1027 if (
m_cells[eta_coord][phi_coord][i] < 0)
1055 for (
int phi = 0;
phi < phi_grid; ++
phi)
1071 if (this_count > ret)
1120 static_assert(
NumSamplings == 28,
"Written under the assumption there are 28 samplings.");
1123 template <
class Func,
class ...
Args>
1157 template <
class Func,
class ...
Args>
1191 template <
class Func,
class ...
Args>
1286 template <
class Func,
class ...
Args>
1388 template <
class Entry>
1397 template <
class Entry>
1398 constexpr void operator() (Entry & entry,
const float min_eta,
const float max_eta)
const
1400 entry.initialize(min_eta, max_eta);
1406 template <
class Entry>
1408 const float cell_eta,
const float cell_phi,
1409 const float cell_deta,
const float cell_dphi )
const
1411 entry.register_cell(cell, cell_eta, cell_phi, cell_deta, cell_dphi);
1417 template <
class Entry>
1420 using namespace std;
1421 ret =
max(ret, entry.finish_initializing_buffer_size());
1427 template <
class Entry>
1430 entry.finish_initializing(buffer);
1438 template <
class Entry>
1439 constexpr void operator() (Entry & entry,
int & ret,
const float test_eta,
const float test_phi,
int * cell_arr)
const
1441 ret = entry.get_possible_cells_from_coords(test_eta, test_phi, cell_arr);
1447 template <
class Entry>
1448 constexpr void operator() (Entry & entry,
bool & ret,
const float test_eta,
const float test_phi)
const
1450 ret = entry.has_cell_in_coords(test_eta, test_phi);
1456 template <
class Entry>
1457 constexpr void operator() (Entry & entry,
int & ret,
const float test_eta,
const float test_phi,
int * cell_arr)
const
1459 ret += entry.get_possible_cells_from_coords(test_eta, test_phi, cell_arr + ret);
1465 template <
class Entry>
1466 constexpr void operator() (Entry & entry,
bool & ret,
const float test_eta,
const float test_phi)
const
1468 ret = ( ret || entry.has_cell_in_coords(test_eta, test_phi) );
1475 template <
class Entry>
1478 using namespace std;
1479 ret =
max(ret, entry.get_max_real_overlap());
1492 constexpr void initialize(
const int sampling,
const float min_eta,
const float max_eta)
1497 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)
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
static CUDA_HOS_DEV float regularize_angle(const float b, const float a=0.f)
static CUDA_HOS_DEV T angular_difference(const T x, const T y)
Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
constexpr int NumSamplings
int grid_list[2][s_max_cells]
constexpr bool try_add_next_gridcell(const int eta, const int phi)
float phis[s_eta_grid_size][phi_grid][s_max_overlap_cells]
constexpr int * get_gridcells()
constexpr const int * get_gridcells() const
constexpr int * get_next_gridcells()
constexpr const int * get_next_gridcells() const
float etas[s_eta_grid_size][phi_grid][s_max_overlap_cells]
static constexpr int s_max_cells
constexpr int & get_counter()
constexpr void clear_next_gridcells()
constexpr void get_gridcell(const int i, int &eta, int &phi)
constexpr bool add_next_gridcell(const int eta, const int phi)
int cells[s_eta_grid_size][phi_grid][s_max_overlap_cells]
constexpr const int & get_next_counter() const
constexpr const int & get_counter() const
constexpr int & get_next_counter()
constexpr bool add_next_gridcell(const int value)
Holds an (eta, phi) to cell map for a given sampling.
float m_phi_coordinates[s_eta_grid_size][phi_grid][s_max_overlap_cells]
static constexpr float s_phi_max
constexpr void add_cell_to_grid(const int cell, const float eta_fraction, const float phi_fraction, const int eta, const int phi)
static constexpr int s_eta_grid_size
constexpr void register_cell(const int cell, const float cell_eta, const float cell_phi, const float cell_deta, const float cell_dphi)
int m_cells[s_eta_grid_size][phi_grid][s_max_overlap_cells]
constexpr float delta_phi() const
float m_eta_limits[1+!continuous]
constexpr int phi_coordinate(const float phi) const
static constexpr size_t finish_initializing_buffer_size()
constexpr float end_eta(const bool positive=true) const
constexpr bool has_cell_in_coords(const float test_eta, const float test_phi) const
constexpr void initialize(const float min_eta, const float max_eta)
constexpr int get_possible_cells_from_coords(const float test_eta, const float test_phi, int *cell_arr) const
We assume cell_arr is large enough.
constexpr float start_phi() const
static constexpr float s_delta_phi
constexpr int get_max_real_overlap() const
constexpr float end_phi() const
CUDA_HOS_DEV void finish_initializing(void *buffer)
!
static constexpr int s_max_overlap_cells
float m_eta_coordinates[s_eta_grid_size][phi_grid][s_max_overlap_cells]
constexpr float phi_value(const int phi_coord) const
constexpr int eta_coordinate(const float eta, float &interval) const
constexpr float start_eta(const bool positive=true) const
constexpr float eta_value(const int eta_coord) const
static constexpr float s_phi_min
constexpr int phi_coordinate(const float phi, float &interval) const
constexpr float delta_eta() const
constexpr bool coordinates_in_range(const float eta, const float) const
constexpr void initialize()
constexpr int eta_coordinate(const float eta) const
constexpr void operator()(Entry &entry, size_t &ret) const
constexpr void operator()(Entry &entry, bool &ret, const float test_eta, const float test_phi) const
constexpr void operator()(Entry &entry, bool &ret, const float test_eta, const float test_phi) const
constexpr void operator()(Entry &entry, void *buffer) const
constexpr void operator()(Entry &entry, int &ret, const float test_eta, const float test_phi, int *cell_arr) const
constexpr void operator()(Entry &entry, int &ret, const float test_eta, const float test_phi, int *cell_arr) const
constexpr void operator()(Entry &entry) const
constexpr void operator()(Entry &entry, const float min_eta, const float max_eta) const
constexpr void operator()(const Entry &entry, int &ret) const
constexpr void operator()(Entry &entry, const int cell, const float cell_eta, const float cell_phi, const float cell_deta, const float cell_dphi) const
CUDA_HOS_DEV void finish_initializing(void *buffer)
!
EtaPhiMapEntry< 34, 66, false, false, 11 > sampling_11
static constexpr int s_max_overlap_cells
EtaPhiMapEntry< 148, 232, false, false, 21 > sampling_21
EtaPhiMapEntry< 150, 260, false, false, 6 > sampling_6
constexpr bool has_cell_in_coords(const float test_eta, const float test_phi) const
constexpr int get_possible_cells_from_coords(const int sampling, const float test_eta, const float test_phi, int *cell_arr) const
We assume cell_arr is large enough.
EtaPhiMapEntry< 1, 1, true, true, 25 > sampling_25
EtaPhiMapEntry< 12, 66, true, false, 19 > sampling_19
EtaPhiMapEntry< 12, 66, true, false, 18 > sampling_18
EtaPhiMapEntry< 6, 66, true, false, 16 > sampling_16
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)
constexpr void initialize()
Initialize all cells of all samplings.
EtaPhiMapEntry< 10, 66, true, true, 14 > sampling_14
constexpr size_t finish_initializing_buffer_size() const
EtaPhiMapEntry< 6, 66, true, false, 15 > sampling_15
EtaPhiMapEntry< 958, 262, false, false, 1 > sampling_1
EtaPhiMapEntry< 732, 68, false, false, 5 > sampling_5
constexpr void apply_to_all_samplings(Func &&F, Args &&... args) const
F must be prepared to receive as first argument a EtaPhiMapEntry<N, M>, as well as any arguments.
constexpr int get_possible_cells_from_coords(const float test_eta, const float test_phi, int *cell_arr) const
We assume cell_arr is large enough.
EtaPhiMapEntry< 1, 1, true, true, 26 > sampling_26
EtaPhiMapEntry< 98, 164, false, false, 22 > sampling_22
EtaPhiMapEntry< 6, 66, true, false, 20 > sampling_20
EtaPhiMapEntry< 24, 66, true, true, 12 > sampling_12
EtaPhiMapEntry< 25, 68, false, false, 4 > sampling_4
EtaPhiMapEntry< 64, 130, false, false, 23 > sampling_23
EtaPhiMapEntry< 58, 262, false, false, 3 > sampling_3
constexpr void apply_to_all_samplings(Func &&F, Args &&... args)
F must be prepared to receive as first argument a EtaPhiMapEntry<N, M>, as well as any arguments.
constexpr int get_max_real_overlap() const
EtaPhiMapEntry< 124, 262, false, false, 2 > sampling_2
EtaPhiMapEntry< 1, 1, true, true, 27 > sampling_27
constexpr void apply_to_sampling(const int sampling, Func &&F, Args &&... args)
F must be prepared to receive as first argument a EtaPhiMapEntry<N, M>, as well as any arguments.
constexpr bool has_cell_in_coords(const int sampling, const float test_eta, const float test_phi) const
EtaPhiMapEntry< 34, 66, false, false, 9 > sampling_9
EtaPhiMapEntry< 1, 1, true, true, 24 > sampling_24
constexpr void apply_to_sampling(const int sampling, Func &&F, Args &&... args) const
F must be prepared to receive as first argument a EtaPhiMapEntry<N, M>, as well as any arguments.
EtaPhiMapEntry< 72, 260, false, false, 7 > sampling_7
EtaPhiMapEntry< 40, 66, false, false, 8 > sampling_8
constexpr void initialize(const int sampling, const float min_eta, const float max_eta)
Initialize a specific sampling with known eta and phi ranges.
EtaPhiMapEntry< 20, 66, true, true, 13 > sampling_13
EtaPhiMapEntry< 16, 66, true, false, 17 > sampling_17
EtaPhiMapEntry< 32, 66, false, false, 10 > sampling_10
EtaPhiMapEntry< 126, 66, false, true, 0 > sampling_0