File indexing completed on 2025-08-06 08:18:06
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "TpcSpaceChargeMatrixContainerv2.h"
0010
0011
0012 TpcSpaceChargeMatrixContainerv2::TpcSpaceChargeMatrixContainerv2()
0013 {
0014
0015 TpcSpaceChargeMatrixContainerv2::Reset();
0016 }
0017
0018
0019 void TpcSpaceChargeMatrixContainerv2::identify(std::ostream& out) const
0020 {
0021 out << "TpcSpaceChargeMatrixContainerv2" << std::endl;
0022 out << " phibins: " << m_phibins << std::endl;
0023 out << " rbins: " << m_rbins << std::endl;
0024 out << " zbins: " << m_zbins << std::endl;
0025 }
0026
0027
0028 void TpcSpaceChargeMatrixContainerv2::get_grid_dimensions(int& phibins, int& rbins, int& zbins) const
0029 {
0030 phibins = m_phibins;
0031 rbins = m_rbins;
0032 zbins = m_zbins;
0033 }
0034
0035
0036 int TpcSpaceChargeMatrixContainerv2::get_grid_size() const
0037 {
0038 return m_phibins * m_rbins * m_zbins;
0039 }
0040
0041
0042 int TpcSpaceChargeMatrixContainerv2::get_cell_index(int iphi, int ir, int iz) const
0043 {
0044 if (iphi < 0 || iphi >= m_phibins)
0045 {
0046 return -1;
0047 }
0048 if (ir < 0 || ir >= m_rbins)
0049 {
0050 return -1;
0051 }
0052 if (iz < 0 || iz >= m_zbins)
0053 {
0054 return -1;
0055 }
0056 return iz + m_zbins * (ir + m_rbins * iphi);
0057 }
0058
0059
0060 int TpcSpaceChargeMatrixContainerv2::get_entries(int cell_index) const
0061 {
0062
0063 if (!bound_check(cell_index))
0064 {
0065 return 0;
0066 }
0067 return m_entries[cell_index];
0068 }
0069
0070
0071 float TpcSpaceChargeMatrixContainerv2::get_lhs(int cell_index, int i, int j) const
0072 {
0073
0074 if (!bound_check(cell_index, i, j))
0075 {
0076 return 0;
0077 }
0078 return m_lhs[cell_index][get_flat_index(i, j)];
0079 }
0080
0081
0082 float TpcSpaceChargeMatrixContainerv2::get_rhs(int cell_index, int i) const
0083 {
0084
0085 if (!bound_check(cell_index, i))
0086 {
0087 return 0;
0088 }
0089 return m_rhs[cell_index][i];
0090 }
0091
0092
0093 float TpcSpaceChargeMatrixContainerv2::get_lhs_rphi(int cell_index, int i, int j) const
0094 {
0095
0096 if (!bound_check_reduced(cell_index, i, j))
0097 {
0098 return 0;
0099 }
0100 return m_lhs_rphi[cell_index][get_flat_index_reduced(i, j)];
0101 }
0102
0103
0104 float TpcSpaceChargeMatrixContainerv2::get_rhs_rphi(int cell_index, int i) const
0105 {
0106
0107 if (!bound_check_reduced(cell_index, i))
0108 {
0109 return 0;
0110 }
0111 return m_rhs_rphi[cell_index][i];
0112 }
0113
0114
0115 float TpcSpaceChargeMatrixContainerv2::get_lhs_z(int cell_index, int i, int j) const
0116 {
0117
0118 if (!bound_check_reduced(cell_index, i, j))
0119 {
0120 return 0;
0121 }
0122 return m_lhs_z[cell_index][get_flat_index_reduced(i, j)];
0123 }
0124
0125
0126 float TpcSpaceChargeMatrixContainerv2::get_rhs_z(int cell_index, int i) const
0127 {
0128
0129 if (!bound_check_reduced(cell_index, i))
0130 {
0131 return 0;
0132 }
0133 return m_rhs_z[cell_index][i];
0134 }
0135
0136
0137 void TpcSpaceChargeMatrixContainerv2::Reset()
0138 {
0139
0140 const int totalbins = m_phibins * m_rbins * m_zbins;
0141
0142
0143 m_entries = std::vector<int>(totalbins, 0);
0144 m_lhs = std::vector<matrix_t>(totalbins, {{}});
0145 m_rhs = std::vector<column_t>(totalbins, {{}});
0146
0147
0148 m_lhs_rphi = std::vector<reduced_matrix_t>(totalbins, {{}});
0149 m_rhs_rphi = std::vector<reduced_column_t>(totalbins, {{}});
0150
0151
0152 m_lhs_z = std::vector<reduced_matrix_t>(totalbins, {{}});
0153 m_rhs_z = std::vector<reduced_column_t>(totalbins, {{}});
0154 }
0155
0156
0157 void TpcSpaceChargeMatrixContainerv2::set_grid_dimensions(int phibins, int rbins, int zbins)
0158 {
0159 m_phibins = phibins;
0160 m_rbins = rbins;
0161 m_zbins = zbins;
0162 Reset();
0163 }
0164
0165
0166 void TpcSpaceChargeMatrixContainerv2::add_to_entries(int cell_index, int value)
0167 {
0168 if (bound_check(cell_index))
0169 {
0170 m_entries[cell_index] += value;
0171 }
0172 }
0173
0174
0175 void TpcSpaceChargeMatrixContainerv2::add_to_lhs(int cell_index, int i, int j, float value)
0176 {
0177 if (bound_check(cell_index, i, j))
0178 {
0179 m_lhs[cell_index][get_flat_index(i, j)] += value;
0180 }
0181 }
0182
0183
0184 void TpcSpaceChargeMatrixContainerv2::add_to_rhs(int cell_index, int i, float value)
0185 {
0186 if (bound_check(cell_index, i))
0187 {
0188 m_rhs[cell_index][i] += value;
0189 }
0190 }
0191
0192
0193 void TpcSpaceChargeMatrixContainerv2::add_to_lhs_rphi(int cell_index, int i, int j, float value)
0194 {
0195 if (bound_check_reduced(cell_index, i, j))
0196 {
0197 m_lhs_rphi[cell_index][get_flat_index_reduced(i, j)] += value;
0198 }
0199 }
0200
0201
0202 void TpcSpaceChargeMatrixContainerv2::add_to_rhs_rphi(int cell_index, int i, float value)
0203 {
0204 if (bound_check_reduced(cell_index, i))
0205 {
0206 m_rhs_rphi[cell_index][i] += value;
0207 }
0208 }
0209
0210
0211 void TpcSpaceChargeMatrixContainerv2::add_to_lhs_z(int cell_index, int i, int j, float value)
0212 {
0213 if (bound_check_reduced(cell_index, i, j))
0214 {
0215 m_lhs_z[cell_index][get_flat_index_reduced(i, j)] += value;
0216 }
0217 }
0218
0219
0220 void TpcSpaceChargeMatrixContainerv2::add_to_rhs_z(int cell_index, int i, float value)
0221 {
0222 if (bound_check_reduced(cell_index, i))
0223 {
0224 m_rhs_z[cell_index][i] += value;
0225 }
0226 }
0227
0228
0229 bool TpcSpaceChargeMatrixContainerv2::add(const TpcSpaceChargeMatrixContainer& other)
0230 {
0231
0232 int phibins = 0;
0233 int rbins = 0;
0234 int zbins = 0;
0235 other.get_grid_dimensions(phibins, rbins, zbins);
0236 if ((m_phibins != phibins) || (m_rbins != rbins) || (m_zbins != zbins))
0237 {
0238 std::cout << "TpcSpaceChargeMatrixContainerv2::add - inconsistent grid sizes" << std::endl;
0239 return false;
0240 }
0241
0242
0243 for (size_t cell_index = 0; cell_index < m_lhs.size(); ++cell_index)
0244 {
0245 add_to_entries(cell_index, other.get_entries(cell_index));
0246 }
0247
0248
0249 for (size_t cell_index = 0; cell_index < m_lhs.size(); ++cell_index)
0250 {
0251 for (int i = 0; i < m_ncoord; ++i)
0252 {
0253 for (int j = 0; j < m_ncoord; ++j)
0254 {
0255 add_to_lhs(cell_index, i, j, other.get_lhs(cell_index, i, j));
0256 }
0257 }
0258 }
0259
0260
0261 for (size_t cell_index = 0; cell_index < m_rhs.size(); ++cell_index)
0262 {
0263 for (int i = 0; i < m_ncoord; ++i)
0264 {
0265 add_to_rhs(cell_index, i, other.get_rhs(cell_index, i));
0266 }
0267 }
0268
0269
0270 for (size_t cell_index = 0; cell_index < m_lhs_rphi.size(); ++cell_index)
0271 {
0272 for (int i = 0; i < m_ncoord_reduced; ++i)
0273 {
0274 for (int j = 0; j < m_ncoord_reduced; ++j)
0275 {
0276 add_to_lhs_rphi(cell_index, i, j, other.get_lhs_rphi(cell_index, i, j));
0277 }
0278 }
0279 }
0280
0281
0282 for (size_t cell_index = 0; cell_index < m_rhs_rphi.size(); ++cell_index)
0283 {
0284 for (int i = 0; i < m_ncoord_reduced; ++i)
0285 {
0286 add_to_rhs_rphi(cell_index, i, other.get_rhs_rphi(cell_index, i));
0287 }
0288 }
0289
0290
0291 for (size_t cell_index = 0; cell_index < m_lhs_z.size(); ++cell_index)
0292 {
0293 for (int i = 0; i < m_ncoord_reduced; ++i)
0294 {
0295 for (int j = 0; j < m_ncoord_reduced; ++j)
0296 {
0297 add_to_lhs_z(cell_index, i, j, other.get_lhs_z(cell_index, i, j));
0298 }
0299 }
0300 }
0301
0302
0303 for (size_t cell_index = 0; cell_index < m_rhs_z.size(); ++cell_index)
0304 {
0305 for (int i = 0; i < m_ncoord_reduced; ++i)
0306 {
0307 add_to_rhs_z(cell_index, i, other.get_rhs_z(cell_index, i));
0308 }
0309 }
0310
0311 return true;
0312 }
0313
0314
0315 bool TpcSpaceChargeMatrixContainerv2::bound_check(int cell_index) const
0316 {
0317 if (cell_index < 0 || cell_index >= (int) m_rhs.size())
0318 {
0319 return false;
0320 }
0321 return true;
0322 }
0323
0324
0325 bool TpcSpaceChargeMatrixContainerv2::bound_check(int cell_index, int i) const
0326 {
0327 if (cell_index < 0 || cell_index >= (int) m_rhs.size())
0328 {
0329 return false;
0330 }
0331 if (i < 0 || i >= m_ncoord)
0332 {
0333 return false;
0334 }
0335 return true;
0336 }
0337
0338
0339 bool TpcSpaceChargeMatrixContainerv2::bound_check(int cell_index, int i, int j) const
0340 {
0341 if (cell_index < 0 || cell_index >= (int) m_lhs.size())
0342 {
0343 return false;
0344 }
0345 if (i < 0 || i >= m_ncoord)
0346 {
0347 return false;
0348 }
0349 if (j < 0 || j >= m_ncoord)
0350 {
0351 return false;
0352 }
0353 return true;
0354 }
0355
0356
0357 bool TpcSpaceChargeMatrixContainerv2::bound_check_reduced(int cell_index, int i) const
0358 {
0359 if (cell_index < 0 || cell_index >= (int) m_rhs_rphi.size())
0360 {
0361 return false;
0362 }
0363 if (i < 0 || i >= m_ncoord_reduced)
0364 {
0365 return false;
0366 }
0367 return true;
0368 }
0369
0370
0371 bool TpcSpaceChargeMatrixContainerv2::bound_check_reduced(int cell_index, int i, int j) const
0372 {
0373 if (cell_index < 0 || cell_index >= (int) m_lhs_rphi.size())
0374 {
0375 return false;
0376 }
0377 if (i < 0 || i >= m_ncoord_reduced)
0378 {
0379 return false;
0380 }
0381 if (j < 0 || j >= m_ncoord_reduced)
0382 {
0383 return false;
0384 }
0385 return true;
0386 }
0387
0388
0389 int TpcSpaceChargeMatrixContainerv2::get_flat_index(int i, int j) const
0390 {
0391 return j + i * m_ncoord;
0392 }
0393
0394
0395 int TpcSpaceChargeMatrixContainerv2::get_flat_index_reduced(int i, int j) const
0396 {
0397 return j + i * m_ncoord_reduced;
0398 }