Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/****************************************************************************
*
* This is a part of TOTEM offline software.
* Authors:
*  Jan Kaspar (jan.kaspar@gmail.com)
*
****************************************************************************/

#include "Geometry/VeryForwardGeometryBuilder/interface/CTPPSGeometry.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include <regex>

//----------------------------------------------------------------------------------------------------

void CTPPSGeometry::build(const DetGeomDesc* gD, unsigned int verbosity) {
  // reset
  sensors_map_.clear();
  rps_map_.clear();
  stations_in_arm_.clear();
  rps_in_station_.clear();
  dets_in_rp_.clear();

  // propagate through the GeometricalDet structure and add all detectors to 'sensors_map_'
  std::deque<const DetGeomDesc*> buffer;
  buffer.emplace_back(gD);
  while (!buffer.empty()) {
    const DetGeomDesc* d = buffer.front();
    buffer.pop_front();

    // verbosity printout
    if (verbosity == 2) {
      d->print();
    }

    // check if it is a sensor
    if (d->name() == DDD_TOTEM_RP_SENSOR_NAME ||
        std::regex_match(d->name(), std::regex(DDD_TOTEM_TIMING_SENSOR_TMPL)) ||
        d->name() == DDD_CTPPS_DIAMONDS_SEGMENT_NAME || d->name() == DDD_CTPPS_UFSD_SEGMENT_NAME ||
        d->name() == DDD_CTPPS_PIXELS_SENSOR_NAME || d->name() == DDD_CTPPS_PIXELS_SENSOR_NAME_2x2)
      addSensor(d->geographicalID(), d);

    // check if it is a RP
    if (d->name() == DDD_TOTEM_RP_RP_NAME || d->name() == DDD_TOTEM_TIMING_RP_NAME ||
        d->name() == DDD_CTPPS_DIAMONDS_RP_NAME || d->name() == DDD_CTPPS_PIXELS_RP_NAME)
      addRP(d->geographicalID(), d);

    for (const auto& comp : d->components())
      buffer.emplace_back(comp);
  }

  // verbosity printout
  if (verbosity) {
    edm::LogVerbatim("CTPPSGeometry::build")
        << "sensors_map_.size() = " << sensors_map_.size() << ", rps_map_.size() = " << rps_map_.size() << std::endl;
  }

  // build sets
  for (const auto& it : sensors_map_) {
    const CTPPSDetId detId(it.first);
    const CTPPSDetId rpId = detId.rpId();
    const CTPPSDetId stId = detId.stationId();
    const CTPPSDetId armId = detId.armId();

    stations_in_arm_[armId].insert(armId);
    rps_in_station_[stId].insert(rpId);
    dets_in_rp_[rpId].insert(detId);
  }
}

//----------------------------------------------------------------------------------------------------

bool CTPPSGeometry::addSensor(unsigned int id, const DetGeomDesc*& gD) {
  if (sensors_map_.find(id) != sensors_map_.end())
    return false;

  sensors_map_[id] = gD;
  return true;
}

//----------------------------------------------------------------------------------------------------

bool CTPPSGeometry::addRP(unsigned int id, const DetGeomDesc*& gD) {
  if (rps_map_.find(id) != rps_map_.end())
    return false;

  rps_map_[id] = gD;
  return true;
}

//----------------------------------------------------------------------------------------------------

const DetGeomDesc* CTPPSGeometry::sensor(unsigned int id) const {
  auto g = sensorNoThrow(id);
  if (nullptr == g) {
    throw cms::Exception("CTPPSGeometry") << "Not found detector with ID " << id << ", i.e. " << CTPPSDetId(id);
  }
  return g;
}

//----------------------------------------------------------------------------------------------------

const DetGeomDesc* CTPPSGeometry::sensorNoThrow(unsigned int id) const noexcept {
  auto it = sensors_map_.find(id);
  if (it == sensors_map_.end()) {
    return nullptr;
  }
  return it->second;
}

//----------------------------------------------------------------------------------------------------

const DetGeomDesc* CTPPSGeometry::rp(unsigned int id) const {
  auto rp = rpNoThrow(id);
  if (nullptr == rp) {
    throw cms::Exception("CTPPSGeometry") << "Not found RP device with ID " << id << ", i.e. " << CTPPSDetId(id);
  }
  return rp;
}

//----------------------------------------------------------------------------------------------------

const DetGeomDesc* CTPPSGeometry::rpNoThrow(unsigned int id) const noexcept {
  auto it = rps_map_.find(id);
  if (it == rps_map_.end()) {
    return nullptr;
  }

  return it->second;
}

//----------------------------------------------------------------------------------------------------
const std::set<unsigned int>& CTPPSGeometry::stationsInArm(unsigned int id) const {
  auto it = stations_in_arm_.find(id);
  if (it == stations_in_arm_.end())
    throw cms::Exception("CTPPSGeometry") << "Arm with ID " << id << " not found.";
  return it->second;
}

//----------------------------------------------------------------------------------------------------

const std::set<unsigned int>& CTPPSGeometry::rpsInStation(unsigned int id) const {
  auto it = rps_in_station_.find(id);
  if (it == rps_in_station_.end())
    throw cms::Exception("CTPPSGeometry") << "Station with ID " << id << " not found.";
  return it->second;
}

//----------------------------------------------------------------------------------------------------

const std::set<unsigned int>& CTPPSGeometry::sensorsInRP(unsigned int id) const {
  auto it = dets_in_rp_.find(id);
  if (it == dets_in_rp_.end())
    throw cms::Exception("CTPPSGeometry") << "RP with ID " << id << " not found.";
  return it->second;
}

//----------------------------------------------------------------------------------------------------

CTPPSGeometry::Vector CTPPSGeometry::localToGlobal(const DetGeomDesc* gd, const CTPPSGeometry::Vector& r) const {
  return gd->rotation() * r + gd->translation();
}

//----------------------------------------------------------------------------------------------------

CTPPSGeometry::Vector CTPPSGeometry::localToGlobal(unsigned int id, const CTPPSGeometry::Vector& r) const {
  return localToGlobal(sensor(id), r);
}

//----------------------------------------------------------------------------------------------------

CTPPSGeometry::Vector CTPPSGeometry::globalToLocal(const DetGeomDesc* gd, const CTPPSGeometry::Vector& r) const {
  return gd->rotation().Inverse() * (r - gd->translation());
}

//----------------------------------------------------------------------------------------------------

CTPPSGeometry::Vector CTPPSGeometry::globalToLocal(unsigned int id, const CTPPSGeometry::Vector& r) const {
  return globalToLocal(sensor(id), r);
}

//----------------------------------------------------------------------------------------------------

CTPPSGeometry::Vector CTPPSGeometry::localToGlobalDirection(unsigned int id, const CTPPSGeometry::Vector& dir) const {
  return sensor(id)->rotation() * dir;
}

//----------------------------------------------------------------------------------------------------

CTPPSGeometry::Vector CTPPSGeometry::globalToLocalDirection(unsigned int id, const CTPPSGeometry::Vector& dir) const {
  return sensor(id)->rotation().Inverse() * dir;
}

//----------------------------------------------------------------------------------------------------

CTPPSGeometry::Vector CTPPSGeometry::sensorTranslation(unsigned int id) const {
  auto gd = sensor(id);
  return gd->translation();
}

//----------------------------------------------------------------------------------------------------

CTPPSGeometry::Vector CTPPSGeometry::rpTranslation(unsigned int id) const {
  auto gd = rp(id);
  return gd->translation();
}