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 206 207 208 209 210 211 212 213
#include "Geometry/MTDGeometryBuilder/interface/MTDGeomUtil.h"

#include "Geometry/MTDCommonData/interface/MTDTopologyMode.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"

using namespace mtd;

void MTDGeomUtil::setGeometry(const MTDGeometry* geom) { geom_ = geom; }

void MTDGeomUtil::setTopology(const MTDTopology* topo) { topology_ = topo; }

bool MTDGeomUtil::isETL(const DetId& id) const {
  MTDDetId hid{id};
  const auto& subDet = hid.mtdSubDetector();
  if (subDet == 0)
    throw cms::Exception("mtd::MTDGeomUtil") << "DetId " << hid.rawId() << " not in MTD!" << std::endl;
  if (subDet == MTDDetId::MTDType::ETL)
    return true;
  return false;
}

bool MTDGeomUtil::isBTL(const DetId& id) const { return !(isETL(id)); }

// row and column set to 0 by default since they are not needed for BTL
std::pair<LocalPoint, GlobalPoint> MTDGeomUtil::position(const DetId& id, int row, int column) const {
  LocalPoint local_point(0., 0., 0.);
  GlobalPoint global_point(0., 0., 0.);
  if (isBTL(id)) {
    BTLDetId detId{id};
    DetId geoId = detId.geographicalId(MTDTopologyMode::crysLayoutFromTopoMode(topology_->getMTDTopologyMode()));
    const MTDGeomDet* thedet = geom_->idToDet(geoId);
    if (thedet == nullptr)
      throw cms::Exception("mtd::MTDGeomUtil") << "GeographicalID: " << std::hex << geoId.rawId() << " ("
                                               << detId.rawId() << ") is invalid!" << std::dec << std::endl;
    const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(thedet->topology());
    const RectangularMTDTopology& topo = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());

    local_point = topo.pixelToModuleLocalPoint(local_point, detId.row(topo.nrows()), detId.column(topo.nrows()));
    global_point = thedet->toGlobal(local_point);
  } else {
    ETLDetId detId{id};
    DetId geoId = detId.geographicalId();
    const MTDGeomDet* thedet = geom_->idToDet(geoId);
    if (thedet == nullptr)
      throw cms::Exception("mtd::MTDGeomUtil") << "GeographicalID: " << std::hex << geoId.rawId() << " ("
                                               << detId.rawId() << ") is invalid!" << std::dec << std::endl;
    const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(thedet->topology());
    const RectangularMTDTopology& topo = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());

    local_point = LocalPoint(topo.localX(row), topo.localY(column), 0.);
    global_point = thedet->toGlobal(local_point);
  }
  return {local_point, global_point};
}

GlobalPoint MTDGeomUtil::globalPosition(const DetId& id, const LocalPoint& local_point) const {
  auto global_point = GlobalPoint(0., 0., 0.);
  if (isBTL(id)) {
    BTLDetId detId{id};
    DetId geoId = detId.geographicalId(MTDTopologyMode::crysLayoutFromTopoMode(topology_->getMTDTopologyMode()));
    const MTDGeomDet* thedet = geom_->idToDet(geoId);
    if (thedet == nullptr)
      throw cms::Exception("mtd::MTDGeomUtil") << "GeographicalID: " << std::hex << geoId.rawId() << " ("
                                               << detId.rawId() << ") is invalid!" << std::dec << std::endl;
    const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(thedet->topology());
    const RectangularMTDTopology& topo = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());
    auto local_point_sim =
        topo.pixelToModuleLocalPoint(local_point, detId.row(topo.nrows()), detId.column(topo.nrows()));
    global_point = thedet->toGlobal(local_point_sim);
  } else {
    ETLDetId detId{id};
    DetId geoId = detId.geographicalId();
    const MTDGeomDet* thedet = geom_->idToDet(geoId);
    if (thedet == nullptr)
      throw cms::Exception("mtd::MTDGeomUtil") << "GeographicalID: " << std::hex << geoId.rawId() << " ("
                                               << detId.rawId() << ") is invalid!" << std::dec << std::endl;
    global_point = thedet->toGlobal(local_point);
  }
  return global_point;
}

int MTDGeomUtil::zside(const DetId& id) const {
  const MTDDetId hid(id);
  return hid.zside();
}

unsigned int MTDGeomUtil::layer(const DetId& id) const {
  unsigned int layer(0);
  if (isETL(id)) {
    ETLDetId hid(id);
    layer = hid.nDisc();
  }
  return layer;
}

int MTDGeomUtil::module(const DetId& id) const {
  int module = -1;
  if (isETL(id)) {
    ETLDetId hid(id);
    module = hid.module();
  } else {
    BTLDetId hid(id);
    module = hid.module();
  }
  return module;
}

// returns the local position as a pair (x, y) - for ETL
std::pair<float, float> MTDGeomUtil::pixelInModule(const DetId& id, const int row, const int column) const {
  if (isBTL(id))
    throw cms::Exception("mtd::MTDGeomUtil")
        << "ID: " << std::hex << id.rawId() << " from BTL. This method is for ETL only." << std::endl;
  ETLDetId detId(id);
  DetId geoId = detId.geographicalId();
  const MTDGeomDet* thedet = geom_->idToDet(geoId);
  if (thedet == nullptr)
    throw cms::Exception("mtd::MTDGeomUtil") << "GeographicalID: " << std::hex << geoId.rawId() << " (" << detId.rawId()
                                             << ") is invalid!" << std::dec << std::endl;
  const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(thedet->topology());
  const RectangularMTDTopology& topo = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());
  const Local3DPoint local_point(topo.localX(row), topo.localY(column), 0.);
  return topo.pixel(local_point);
}

// returns row and column as a pair (row, col)
std::pair<uint8_t, uint8_t> MTDGeomUtil::pixelInModule(const DetId& id, const LocalPoint& local_point) const {
  if (isETL(id)) {
    ETLDetId detId(id);
    DetId geoId = detId.geographicalId();
    const MTDGeomDet* thedet = geom_->idToDet(geoId);
    if (thedet == nullptr)
      throw cms::Exception("mtd::MTDGeomUtil") << "GeographicalID: " << std::hex << geoId.rawId() << " ("
                                               << detId.rawId() << ") is invalid!" << std::dec << std::endl;
    const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(thedet->topology());
    const RectangularMTDTopology& topo = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());
    const auto& thepixel = topo.pixelIndex(local_point);
    uint8_t row = static_cast<uint8_t>(thepixel.first);
    uint8_t col = static_cast<uint8_t>(thepixel.second);
    return std::pair<uint8_t, uint8_t>(row, col);
  } else {
    BTLDetId detId(id);
    DetId geoId = detId.geographicalId(MTDTopologyMode::crysLayoutFromTopoMode(topology_->getMTDTopologyMode()));
    const MTDGeomDet* thedet = geom_->idToDet(geoId);
    if (thedet == nullptr)
      throw cms::Exception("mtd::MTDGeomUtil") << "GeographicalID: " << std::hex << geoId.rawId() << " ("
                                               << detId.rawId() << ") is invalid!" << std::dec << std::endl;
    const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(thedet->topology());
    const RectangularMTDTopology& topo = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());
    auto topo_point = topo.pixelToModuleLocalPoint(local_point, detId.row(topo.nrows()), detId.column(topo.nrows()));
    const auto& thepixel = topo.pixelIndex(topo_point);
    uint8_t row = static_cast<uint8_t>(thepixel.first);
    uint8_t col = static_cast<uint8_t>(thepixel.second);
    return std::pair<uint8_t, uint8_t>(row, col);
  }
}

int MTDGeomUtil::crystalInModule(const DetId& id) const {
  BTLDetId hid(id);
  return hid.crystal();
}

// returns the sensor module id
uint32_t MTDGeomUtil::sensorModuleId(const DetId& id) const {
  if (isBTL(id)) {
    BTLDetId detId(id);
    DetId geoId = detId.geographicalId(MTDTopologyMode::crysLayoutFromTopoMode(topology_->getMTDTopologyMode()));
    return (geoId.rawId());
  } else {
    ETLDetId detId(id);
    DetId geoId = detId.geographicalId();
    return (geoId.rawId());
  }
}

float MTDGeomUtil::eta(const GlobalPoint& position, const float& vertex_z) const {
  GlobalPoint corrected_position = GlobalPoint(position.x(), position.y(), position.z() - vertex_z);
  return corrected_position.eta();
}

float MTDGeomUtil::eta(const DetId& id, const LocalPoint& local_point, const float& vertex_z) const {
  GlobalPoint position = globalPosition(id, local_point);
  float Eta = eta(position, vertex_z);
  return Eta;
}

float MTDGeomUtil::phi(const GlobalPoint& position) const {
  float phi = (position.x() == 0 && position.y() == 0) ? 0 : atan2(position.y(), position.x());
  return phi;
}

float MTDGeomUtil::phi(const DetId& id, const LocalPoint& local_point) const {
  GlobalPoint position = globalPosition(id, local_point);
  float phi = (position.x() == 0 && position.y() == 0) ? 0 : atan2(position.y(), position.x());
  return phi;
}

float MTDGeomUtil::pt(const GlobalPoint& position, const float& hitEnergy, const float& vertex_z) const {
  float Eta = eta(position, vertex_z);
  float pt = hitEnergy / cosh(Eta);
  return pt;
}

float MTDGeomUtil::pt(const DetId& id,
                      const LocalPoint& local_point,
                      const float& hitEnergy,
                      const float& vertex_z) const {
  GlobalPoint position = globalPosition(id, local_point);
  float Eta = eta(position, vertex_z);
  float pt = hitEnergy / cosh(Eta);
  return pt;
}