Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:07:42

0001 #include "Geometry/CaloTopology/interface/CaloSubdetectorTopology.h"
0002 #include <cassert>
0003 
0004 std::vector<DetId> CaloSubdetectorTopology::getWindow(const DetId& id,
0005                                                       const int& northSouthSize,
0006                                                       const int& eastWestSize) const {
0007   std::vector<DetId> cellsInWindow;
0008   // check pivot
0009   if (id.null())
0010     return cellsInWindow;
0011 
0012   //
0013   DetId myTmpId(id);
0014   std::vector<std::pair<Coordinate, DetId> > fringe;
0015   fringe.emplace_back(std::pair<Coordinate, DetId>(Coordinate(0, 0), myTmpId));
0016 
0017   int halfWestEast = eastWestSize / 2;
0018   int halfNorthSouth = northSouthSize / 2;
0019 
0020   std::vector<CellInfo> visited_cells;
0021   visited_cells.resize(northSouthSize * eastWestSize);
0022 
0023   while (!fringe.empty()) {
0024     std::pair<Coordinate, DetId> cur = fringe.back();
0025     fringe.pop_back();
0026 
0027     // check all four neighbours
0028     const CaloDirection directions[4] = {NORTH, SOUTH, EAST, WEST};
0029 
0030     for (auto direction : directions) {
0031       Coordinate neighbour = getNeighbourIndex(cur.first, direction);
0032       //If outside the window range
0033       if (neighbour.first < -halfWestEast || neighbour.first > halfWestEast || neighbour.second < -halfNorthSouth ||
0034           neighbour.second > halfNorthSouth)
0035         continue;
0036 
0037       //Found integer index in the matrix
0038       unsigned int_index = neighbour.first + halfWestEast + eastWestSize * (neighbour.second + halfNorthSouth);
0039       assert(int_index < visited_cells.size());
0040 
0041       // check whether we have seen this neighbour already
0042       if (visited_cells[int_index].visited)
0043         // we have seen this one already
0044         continue;
0045 
0046       // a new cell, get the DetId of the neighbour, mark it
0047       // as visited and add it to the fringe
0048       visited_cells[int_index].visited = true;
0049       std::vector<DetId> neighbourCells = getNeighbours(cur.second, direction);
0050 
0051       if (neighbourCells.size() == 1)
0052         visited_cells[int_index].cell = neighbourCells[0];
0053       else if (neighbourCells.empty())
0054         visited_cells[int_index].cell = DetId(0);
0055       else
0056         throw cms::Exception("getWindowError") << "Not supported subdetector for getWindow method";
0057 
0058       if (!visited_cells[int_index].cell.null())
0059         fringe.emplace_back(std::pair<Coordinate, DetId>(neighbour, visited_cells[int_index].cell));
0060 
0061     }  // loop over all possible directions
0062   }    // while some cells are left on the fringe
0063 
0064   for (auto& visited_cell : visited_cells)
0065     if (!visited_cell.cell.null())
0066       cellsInWindow.emplace_back(visited_cell.cell);
0067 
0068   return cellsInWindow;
0069 }