CMS 3D CMS Logo

CaloSubdetectorTopology.cc
Go to the documentation of this file.
2 #include <cassert>
3 
4 std::vector<DetId> CaloSubdetectorTopology::getWindow(const DetId& id, const int& northSouthSize, const int& eastWestSize) const
5 {
6 
7  std::vector<DetId> cellsInWindow;
8  // check pivot
9  if (id.null())
10  return cellsInWindow;
11 
12  //
13  DetId myTmpId(id);
14  std::vector<std::pair<Coordinate,DetId> > fringe;
15  fringe.emplace_back(std::pair<Coordinate,DetId>(Coordinate(0,0),myTmpId));
16 
17  int halfWestEast = eastWestSize/2 ;
18  int halfNorthSouth = northSouthSize/2 ;
19 
20  std::vector<CellInfo> visited_cells;
21  visited_cells.resize(northSouthSize * eastWestSize);
22 
23  while (!fringe.empty())
24  {
25  std::pair<Coordinate,DetId> cur = fringe.back();
26  fringe.pop_back();
27 
28  // check all four neighbours
29  const CaloDirection directions[4] = { NORTH, SOUTH, EAST, WEST };
30 
31  for (auto direction : directions)
32  {
33  Coordinate neighbour = getNeighbourIndex(cur.first,direction);
34  //If outside the window range
35  if ( neighbour.first < -halfWestEast ||
36  neighbour.first > halfWestEast ||
37  neighbour.second < -halfNorthSouth ||
38  neighbour.second > halfNorthSouth )
39  continue;
40 
41 
42  //Found integer index in the matrix
43  unsigned int_index = neighbour.first + halfWestEast +
44  eastWestSize * (neighbour.second + halfNorthSouth );
45  assert(int_index < visited_cells.size());
46 
47  // check whether we have seen this neighbour already
48  if (visited_cells[int_index].visited)
49  // we have seen this one already
50  continue;
51 
52  // a new cell, get the DetId of the neighbour, mark it
53  // as visited and add it to the fringe
54  visited_cells[int_index].visited = true;
55  std::vector<DetId> neighbourCells = getNeighbours(cur.second,direction);
56 
57  if ( neighbourCells.size() == 1 )
58  visited_cells[int_index].cell = neighbourCells[0];
59  else if ( neighbourCells.empty() )
60  visited_cells[int_index].cell = DetId(0);
61  else
62  throw cms::Exception("getWindowError") << "Not supported subdetector for getWindow method";
63 
64  if (!visited_cells[int_index].cell.null())
65  fringe.emplace_back(std::pair<Coordinate,DetId>(neighbour,visited_cells[int_index].cell));
66 
67  } // loop over all possible directions
68  } // while some cells are left on the fringe
69 
70 
71  for (auto & visited_cell : visited_cells)
72  if (!visited_cell.cell.null())
73  cellsInWindow.emplace_back(visited_cell.cell);
74 
75  return cellsInWindow;
76 }
virtual std::vector< DetId > getNeighbours(const DetId &id, const CaloDirection &dir) const
std::pair< int, int > Coordinate
Coordinate getNeighbourIndex(const Coordinate &coord, const CaloDirection &dir) const
Definition: DetId.h:18
virtual std::vector< DetId > getWindow(const DetId &id, const int &northSouthSize, const int &eastWestSize) const
CaloDirection
Codes the local directions in the cell lattice.
Definition: CaloDirection.h:9