Commit f6618bc2 authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Merge branch 'perf_implants' into 'master'

Performance: Do not Transform Coordinates without Necessity

See merge request allpix-squared/allpix-squared!1009
parents 4a815706 2500e2ce
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -18,15 +18,20 @@ namespace allpix {
    template <typename T, size_t N>
    T DetectorField<T, N>::get(const ROOT::Math::XYZPoint& pos, const bool extrapolate_z) const {

        // Return empty field if outside the matrix or no field is set
        auto [px, py] = model_->getPixelIndex(pos);
        if(type_ == FieldType::NONE || !model_->isWithinMatrix(px, py)) {
        // Return empty field if no field is set
        if(type_ == FieldType::NONE) {
            return {};
        }

        // Return empty field if outside the matrix
        if(!model_->isWithinMatrix(pos)) {
            return {};
        }

        // For per-pixel fields, resort to getRelativeTo with current pixel as reference:
        if(mapping_ != FieldMapping::SENSOR) {
            // Calculate center of current pixel from index as reference point:
            auto [px, py] = model_->getPixelIndex(pos);
            auto ref = static_cast<ROOT::Math::XYPoint>(model_->getPixelCenter(px, py));

            // Get field relative to pixel center:
+7 −1
Original line number Diff line number Diff line
@@ -288,10 +288,16 @@ std::vector<SupportLayer> DetectorModel::getSupportLayers() const {

std::optional<DetectorModel::Implant> DetectorModel::isWithinImplant(const ROOT::Math::XYZPoint& local_pos) const {

    // Bail out if we have no implants - no need to transform coordinates:
    auto implants = getImplants();
    if(implants.empty()) {
        return std::nullopt;
    }

    auto [xpixel, ypixel] = getPixelIndex(local_pos);
    auto inPixelPos = local_pos - getPixelCenter(xpixel, ypixel);

    for(const auto& implant : getImplants()) {
    for(const auto& implant : implants) {
        if(implant.contains(inPixelPos)) {
            return implant;
        }
+3 −1
Original line number Diff line number Diff line
@@ -399,8 +399,10 @@ namespace allpix {
         * @brief Returns if a position is within the grid of pixels defined for the device
         * @param position Position in local coordinates of the detector model
         * @return True if position within the pixel grid, false otherwise
         *
         * @note This method is virtual and can be implemented by detector models for faster calculations
         */
        bool isWithinMatrix(const ROOT::Math::XYZPoint& position) const {
        virtual bool isWithinMatrix(const ROOT::Math::XYZPoint& position) const {
            auto [index_x, index_y] = getPixelIndex(position);
            return isWithinMatrix(index_x, index_y);
        }
+14 −0
Original line number Diff line number Diff line
@@ -78,6 +78,20 @@ bool PixelDetectorModel::isWithinMatrix(const int x, const int y) const {
    return !(x < 0 || x >= static_cast<int>(number_of_pixels_.x()) || y < 0 || y >= static_cast<int>(number_of_pixels_.y()));
}

/**
 * Faster implementation of matrix lookup for local coordinate positions than going through the pixel index
 * This is quite easy for rectangular pixels and matrices.
 */
bool PixelDetectorModel::isWithinMatrix(const ROOT::Math::XYZPoint& position) const {
    if(position.x() < -0.5 * pixel_size_.x() || position.x() > (number_of_pixels_.x() - 0.5) * pixel_size_.x()) {
        return false;
    }
    if(position.y() < -0.5 * pixel_size_.y() || position.y() > (number_of_pixels_.y() - 0.5) * pixel_size_.y()) {
        return false;
    }
    return true;
}

ROOT::Math::XYZPoint PixelDetectorModel::getPixelCenter(const int x, const int y) const {
    auto size = getPixelSize();
    auto local_x = size.x() * x;
+7 −0
Original line number Diff line number Diff line
@@ -80,6 +80,13 @@ namespace allpix {
         */
        bool isWithinMatrix(const int x, const int y) const override;

        /**
         * @brief Returns if a position is within the grid of pixels defined for the device
         * @param position Position in local coordinates of the detector model
         * @return True if position within the pixel grid, false otherwise
         */
        bool isWithinMatrix(const ROOT::Math::XYZPoint& position) const override;

        /**
         * @brief Returns a pixel center in local coordinates
         * @param x X- (or column-) coordinate of the pixel