File indexing completed on 2024-10-29 06:08:19
0001 #include "FastSimulation/CTPPSFastGeometry/interface/CTPPSToFDetector.h"
0002 #include <algorithm>
0003 #include <cmath>
0004
0005 CTPPSToFDetector::CTPPSToFDetector(
0006 int ncellx, int ncelly, std::vector<double>& cellw, double cellh, double pitchx, double pitchy, double pos, int res)
0007 : nCellX_(ncellx),
0008 nCellY_(ncelly),
0009 cellW_(cellw),
0010 cellH_(cellh),
0011 pitchX_(pitchx),
0012 pitchY_(pitchy),
0013 fToFResolution_(res),
0014 detPosition_(pos) {
0015
0016
0017 cellRow_.push_back(std::pair<double, double>(-cellH_ * 0.5, cellH_ * 0.5));
0018 cellColumn_.reserve(nCellX_);
0019 for (int i = 0; i < nCellX_; i++) {
0020 double x1 = 0., x2 = 0.;
0021 if (i == 0) {
0022 detW_ = pitchX_;
0023 x1 = -(detPosition_ + detW_);
0024 } else
0025 x1 = -detPosition_ + detW_;
0026 x2 = x1 - cellW_.at(i);
0027 detW_ += (x2 - x1) - pitchX_;
0028 cellColumn_.push_back(std::pair<double, double>(x1, x2));
0029 }
0030
0031 detH_ = nCellY_ * cellH_;
0032 detW_ = -detW_ - 2 * pitchX_;
0033 };
0034
0035 CTPPSToFDetector::CTPPSToFDetector(
0036 int ncellx, int ncelly, double cellwq, double cellh, double pitchx, double pitchy, double pos, int res)
0037 : nCellX_(ncellx),
0038 nCellY_(ncelly),
0039 cellWq_(cellwq),
0040 cellH_(cellh),
0041 pitchX_(pitchx),
0042 pitchY_(pitchy),
0043 fToFResolution_(res),
0044 detPosition_(pos) {
0045
0046 detW_ = nCellX_ * cellWq_ + (nCellX_ - 1) * pitchX_;
0047 detH_ = nCellY_ * cellH_ + (nCellY_ - 1) * pitchY_;
0048
0049 cellRow_.reserve(nCellY_);
0050 for (int i = 0; i < nCellY_; i++) {
0051 double y1 = cellH_ * (i - nCellY_ * 0.5) + pitchY_ * (i - (nCellY_ - 1) * 0.5);
0052 double y2 = y1 + cellH_;
0053 cellRow_.push_back(std::pair<double, double>(y1, y2));
0054 }
0055 cellColumn_.reserve(nCellX_);
0056 for (int i = 0; i < nCellX_; i++) {
0057 double x1 = -(cellWq_ * i + pitchX_ * i);
0058 x1 -= detPosition_;
0059 double x2 = x1 - cellWq_;
0060 cellColumn_.push_back(std::pair<double, double>(x1, x2));
0061 }
0062 };
0063 void CTPPSToFDetector::AddHit(double x, double y, double tof) {
0064 int cellid = findCellId(x, y);
0065 if (cellid == 0)
0066 return;
0067 if (theToFInfo.find(cellid) == theToFInfo.end())
0068 theToFInfo[cellid];
0069 std::vector<double>* tofs = &(theToFInfo.find(cellid)->second);
0070 int ntof = tofs->size();
0071 int i = 0;
0072 double oneOverRes = 1.0 / fToFResolution_;
0073 for (; i < ntof; i++) {
0074 if (fabs(tofs->at(i) - tof) * oneOverRes < 3) {
0075 tofs->at(i) = (tofs->at(i) + tof) / 2.;
0076 nADC_.at(cellid).at(i)++;
0077 return;
0078 }
0079 }
0080 tofs->push_back(tof);
0081 nHits_++;
0082 nADC_[cellid].push_back(1);
0083 }
0084 int CTPPSToFDetector::findCellId(double x, double y) {
0085 auto it = std::find_if(
0086 cellRow_.begin(), cellRow_.end(), [y](const auto& cell) { return y >= cell.first && y <= cell.second; });
0087
0088 if (it == cellRow_.end())
0089 return 0;
0090
0091 unsigned int y_idx = std::distance(cellRow_.begin(), it) + 1;
0092
0093 it = std::find_if(
0094 cellColumn_.begin(), cellColumn_.end(), [x](const auto& cell) { return x <= cell.first && x > cell.second; });
0095
0096 if (it == cellColumn_.end())
0097 return 0;
0098
0099 unsigned int x_idx = std::distance(cellColumn_.begin(), it) + 1;
0100 return 100 * y_idx + x_idx;
0101 }
0102 bool CTPPSToFDetector::get_CellCenter(int cell_id, double& x, double& y) {
0103 if (cell_id == 0)
0104 return false;
0105
0106 unsigned int y_idx = int(cell_id * 0.01);
0107 unsigned int x_idx = cell_id - y_idx * 100;
0108 x = (cellColumn_.at(x_idx - 1).first + cellColumn_.at(x_idx - 1).second) / 2.0;
0109 y = (cellRow_.at(y_idx - 1).first + cellRow_.at(y_idx - 1).second) / 2.0;
0110 return true;
0111 }