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
#include "Geometry/CaloTopology/interface/CaloSubdetectorTopology.h"
#include <cassert>

std::vector<DetId> CaloSubdetectorTopology::getWindow(const DetId& id,
                                                      const int& northSouthSize,
                                                      const int& eastWestSize) const {
  std::vector<DetId> cellsInWindow;
  // check pivot
  if (id.null())
    return cellsInWindow;

  //
  DetId myTmpId(id);
  std::vector<std::pair<Coordinate, DetId> > fringe;
  fringe.emplace_back(std::pair<Coordinate, DetId>(Coordinate(0, 0), myTmpId));

  int halfWestEast = eastWestSize / 2;
  int halfNorthSouth = northSouthSize / 2;

  std::vector<CellInfo> visited_cells;
  visited_cells.resize(northSouthSize * eastWestSize);

  while (!fringe.empty()) {
    std::pair<Coordinate, DetId> cur = fringe.back();
    fringe.pop_back();

    // check all four neighbours
    const CaloDirection directions[4] = {NORTH, SOUTH, EAST, WEST};

    for (auto direction : directions) {
      Coordinate neighbour = getNeighbourIndex(cur.first, direction);
      //If outside the window range
      if (neighbour.first < -halfWestEast || neighbour.first > halfWestEast || neighbour.second < -halfNorthSouth ||
          neighbour.second > halfNorthSouth)
        continue;

      //Found integer index in the matrix
      unsigned int_index = neighbour.first + halfWestEast + eastWestSize * (neighbour.second + halfNorthSouth);
      assert(int_index < visited_cells.size());

      // check whether we have seen this neighbour already
      if (visited_cells[int_index].visited)
        // we have seen this one already
        continue;

      // a new cell, get the DetId of the neighbour, mark it
      // as visited and add it to the fringe
      visited_cells[int_index].visited = true;
      std::vector<DetId> neighbourCells = getNeighbours(cur.second, direction);

      if (neighbourCells.size() == 1)
        visited_cells[int_index].cell = neighbourCells[0];
      else if (neighbourCells.empty())
        visited_cells[int_index].cell = DetId(0);
      else
        throw cms::Exception("getWindowError") << "Not supported subdetector for getWindow method";

      if (!visited_cells[int_index].cell.null())
        fringe.emplace_back(std::pair<Coordinate, DetId>(neighbour, visited_cells[int_index].cell));

    }  // loop over all possible directions
  }  // while some cells are left on the fringe

  for (auto& visited_cell : visited_cells)
    if (!visited_cell.cell.null())
      cellsInWindow.emplace_back(visited_cell.cell);

  return cellsInWindow;
}