CMS 3D CMS Logo

HGCalTriggerGeometryHexImp1.cc
Go to the documentation of this file.
2 
5 
6 #include <vector>
7 #include <iostream>
8 #include <fstream>
9 
11 
13 {
14  public:
16 
17  void initialize(const edm::ESHandle<CaloGeometry>& ) final;
20  const edm::ESHandle<HGCalGeometry>&) final;
21 
22  private:
25 
26  void fillMaps();
28 };
29 
30 
31 /*****************************************************************/
34  l1tCellsMapping_(conf.getParameter<edm::FileInPath>("L1TCellsMapping")),
35  l1tModulesMapping_(conf.getParameter<edm::FileInPath>("L1TModulesMapping"))
36 /*****************************************************************/
37 {
38 }
39 
40 /*****************************************************************/
42 /*****************************************************************/
43 {
44  edm::LogWarning("HGCalTriggerGeometry") << "WARNING: This HGCal trigger geometry is incomplete.\n"\
45  << "WARNING: There is no neighbor information.\n";
46 
47  setCaloGeometry(calo_geometry);
48  fillMaps();
50 
51 }
52 
53 /*****************************************************************/
55  const edm::ESHandle<HGCalGeometry>& hgc_hsi_geometry,
56  const edm::ESHandle<HGCalGeometry>& hgc_hsc_geometry
57  )
58 /*****************************************************************/
59 {
60  throw cms::Exception("BadGeometry")
61  << "HGCalTriggerGeometryHexImp1 geometry cannot be initialized with the V9 HGCAL geometry";
62 }
63 
64 
65 
66 /*****************************************************************/
68 /*****************************************************************/
69 {
70  //
71  // read module mapping file
72  std::unordered_map<short, short> wafer_to_module_ee;
73  std::unordered_map<short, short> wafer_to_module_fh;
74  std::unordered_map<short, std::map<short,short>> module_to_wafers_ee;
75  std::unordered_map<short, std::map<short,short>> module_to_wafers_fh;
76  std::ifstream l1tModulesMappingStream(l1tModulesMapping_.fullPath());
77  if(!l1tModulesMappingStream.is_open()) edm::LogError("HGCalTriggerGeometry") << "Cannot open L1TModulesMapping file\n";
78  short subdet = 0;
79  short wafer = 0;
80  short module = 0;
81  for(; l1tModulesMappingStream>>subdet>>wafer>>module; )
82  {
83  if(subdet==3)
84  {
85  wafer_to_module_ee.emplace(wafer,module);
86  auto itr_insert = module_to_wafers_ee.emplace(module, std::map<short,short>());
87  itr_insert.first->second.emplace(wafer, 0);
88  }
89  else if(subdet==4)
90  {
91  wafer_to_module_fh.emplace(wafer,module);
92  auto itr_insert = module_to_wafers_fh.emplace(module, std::map<short,short>());
93  itr_insert.first->second.emplace(wafer, 0);
94  }
95  else edm::LogWarning("HGCalTriggerGeometry") << "Unsupported subdetector number ("<<subdet<<") in L1TModulesMapping file\n";
96  }
97  if(!l1tModulesMappingStream.eof()) edm::LogWarning("HGCalTriggerGeometry") << "Error reading L1TModulesMapping '"<<wafer<<" "<<module<<"' \n";
98  l1tModulesMappingStream.close();
99  // read trigger cell mapping file
100  std::map<std::pair<short,short>, short> cells_to_trigger_cells;
101  std::unordered_map<short, short> number_trigger_cells_in_wafers; // the map key is the wafer type
102  std::ifstream l1tCellsMappingStream(l1tCellsMapping_.fullPath());
103  if(!l1tCellsMappingStream.is_open()) edm::LogError("HGCalTriggerGeometry") << "Cannot open L1TCellsMapping file\n";
104  short waferType = 0;
105  short cell = 0;
106  short triggerCell = 0;
107  for(; l1tCellsMappingStream>>waferType>>cell>>triggerCell; )
108  {
109  cells_to_trigger_cells.emplace(std::make_pair((waferType?1:-1),cell), triggerCell);
110  auto itr_insert = number_trigger_cells_in_wafers.emplace((waferType?1:-1), 0);
111  if(triggerCell+1 > itr_insert.first->second) itr_insert.first->second = triggerCell+1;
112  }
113  if(!l1tCellsMappingStream.eof()) edm::LogWarning("HGCalTriggerGeometry") << "Error reading L1TCellsMapping'"<<waferType<<" "<<cell<<" "<<triggerCell<<"' \n";
114  l1tCellsMappingStream.close();
115  // For each wafer compute the trigger cell offset according to the wafer position inside
116  // the module. The first wafer will have an offset equal to 0, the second an offset equal to the
117  // number of trigger cells in the first wafer, etc.
118  short offset = 0;
119  for(auto& module_wafers : module_to_wafers_ee)
120  {
121  offset = 0;
122  for(auto& wafer_offset : module_wafers.second)
123  {
124  wafer_offset.second = offset;
125  int wafer_type = (eeTopology().dddConstants().waferTypeT(wafer_offset.first)==1?1:-1);
126  offset += number_trigger_cells_in_wafers.at(wafer_type);
127  }
128  }
129  for(auto& module_wafers : module_to_wafers_fh)
130  {
131  offset = 0;
132  for(auto& wafer_offset : module_wafers.second)
133  {
134  wafer_offset.second = offset;
135  int wafer_type = (fhTopology().dddConstants().waferTypeT(wafer_offset.first)==1?1:-1);
136  offset += number_trigger_cells_in_wafers.at(wafer_type);
137  }
138  }
139  //
140  // Loop over HGC cells
141  // EE
142  for(const auto& id : eeGeometry()->getValidGeomDetIds())
143  {
144  if(id.rawId()==0) continue;
145  HGCalDetId waferDetId(id);
146  short module = wafer_to_module_ee[waferDetId.wafer()];
147  short triggercell_offset = module_to_wafers_ee.at(module).at(waferDetId.wafer());
148  int nCells = eeTopology().dddConstants().numberCellsHexagon(waferDetId.wafer());
149  for(int c=0;c<nCells;c++)
150  {
151  short triggerCellId = cells_to_trigger_cells[std::make_pair(waferDetId.waferType(),c)];
152  // Fill cell -> trigger cell mapping
153  HGCalDetId cellDetId(ForwardSubdetector(waferDetId.subdetId()), waferDetId.zside(), waferDetId.layer(), waferDetId.waferType(), waferDetId.wafer(), c);
154  // The module id is used instead of the wafer id for the trigger cells
155  // Since there are several wafers per module, an offset is applied on the HGCalDetId::cell field
156  if(triggercell_offset+triggerCellId >= HGCalDetId::kHGCalCellMask) edm::LogError("HGCalTriggerGeometry") << "Trigger cell id requested with a cell field larger than available in HGCalDetId (" << triggercell_offset+triggerCellId << " >= " << HGCalDetId::kHGCalCellMask << ")\n";
157  HGCalDetId triggerCellDetId(ForwardSubdetector(waferDetId.subdetId()), waferDetId.zside(), waferDetId.layer(), waferDetId.waferType(), module, triggercell_offset + triggerCellId);
158  cells_to_trigger_cells_.emplace(cellDetId, triggerCellDetId);
159  // Fill trigger cell -> module mapping
160  HGCalDetId moduleDetId(ForwardSubdetector(waferDetId.subdetId()), waferDetId.zside(), waferDetId.layer(), waferDetId.waferType(), module, HGCalDetId::kHGCalCellMask);
161  trigger_cells_to_modules_.emplace(triggerCellDetId, moduleDetId);
162  }
163  }
164  // FH
165  for(const auto& id : fhGeometry()->getValidGeomDetIds())
166  {
167  if(id.rawId()==0) continue;
168  HGCalDetId waferDetId(id);
169  short module = wafer_to_module_fh[waferDetId.wafer()];
170  short triggercell_offset = module_to_wafers_fh.at(module).at(waferDetId.wafer());
171  int nCells = fhTopology().dddConstants().numberCellsHexagon(waferDetId.wafer());
172  for(int c=0;c<nCells;c++)
173  {
174  short triggerCellId = cells_to_trigger_cells[std::make_pair(waferDetId.waferType(),c)];
175  // Fill cell -> trigger cell mapping
176  HGCalDetId cellDetId(ForwardSubdetector(waferDetId.subdetId()), waferDetId.zside(), waferDetId.layer(), waferDetId.waferType(), waferDetId.wafer(), c);
177  // The module id is used instead of the wafer id for the trigger cells
178  // Since there are several wafers per module, an offset is applied on the HGCalDetId::cell field
179  if(triggercell_offset+triggerCellId >= HGCalDetId::kHGCalCellMask) edm::LogError("HGCalTriggerGeometry") << "Trigger cell id requested with a cell field larger than available in HGCalDetId (" << triggercell_offset+triggerCellId << " >= " << HGCalDetId::kHGCalCellMask << ")\n";
180  HGCalDetId triggerCellDetId(ForwardSubdetector(waferDetId.subdetId()), waferDetId.zside(), waferDetId.layer(), waferDetId.waferType(), module, triggercell_offset + triggerCellId);
181  cells_to_trigger_cells_.emplace(cellDetId, triggerCellDetId);
182  // Fill trigger cell -> module mapping
183  HGCalDetId moduleDetId(ForwardSubdetector(waferDetId.subdetId()), waferDetId.zside(), waferDetId.layer(), waferDetId.waferType(), module, HGCalDetId::kHGCalCellMask);
184  trigger_cells_to_modules_.emplace(triggerCellDetId, moduleDetId);
185  }
186  }
187 }
188 
189 
190 
191 /*****************************************************************/
193 /*****************************************************************/
194 {
195  //
196  // Build trigger cells and fill map
198  // make list of cells in trigger cells
199  std::unordered_map<unsigned, list_cells> trigger_cells_to_cells;
200  for(const auto& cell_triggerCell : cells_to_trigger_cells_)
201  {
202  unsigned cell = cell_triggerCell.first;
203  unsigned triggerCell = cell_triggerCell.second;
204  auto itr_exist = trigger_cells_to_cells.emplace(triggerCell, list_cells());
205  itr_exist.first->second.emplace(cell);
206  //trigger_cells_to_cells.at(triggerCell).emplace(cell);
207  }
208  for(const auto& triggerCell_cells : trigger_cells_to_cells)
209  {
210  unsigned triggerCellId = triggerCell_cells.first;
211  list_cells cellIds = triggerCell_cells.second;
212  // Position: barycenter of the trigger cell.
213  Basic3DVector<float> triggerCellVector(0.,0.,0.);
214  for(const auto& cell : cellIds)
215  {
216  HGCalDetId cellDetId(cell);
217  triggerCellVector += (cellDetId.subdetId()==ForwardSubdetector::HGCEE ? eeGeometry()->getPosition(cellDetId) : fhGeometry()->getPosition(cellDetId)).basicVector();
218  }
219  GlobalPoint triggerCellPoint( triggerCellVector/cellIds.size() );
220  const auto& triggerCellToModuleItr = trigger_cells_to_modules_.find(triggerCellId);
221  unsigned moduleId = (triggerCellToModuleItr!=trigger_cells_to_modules_.end() ? triggerCellToModuleItr->second : 0); // 0 if the trigger cell doesn't belong to a module
222  //unsigned moduleId = trigger_cells_to_modules_.at(triggercellId);
223  // FIXME: empty neighbours
224  trigger_cells_.emplace(triggerCellId, std::make_unique<const HGCalTriggerGeometry::TriggerCell>(triggerCellId, moduleId, triggerCellPoint, list_cells(), cellIds));
225  }
226  //
227  // Build modules and fill map
228  typedef HGCalTriggerGeometry::Module::list_type list_triggerCells;
229  typedef HGCalTriggerGeometry::Module::tc_map_type tc_map_to_cells;
230  // make list of trigger cells in modules
231  std::unordered_map<unsigned, list_triggerCells> modules_to_trigger_cells;
232  for(const auto& triggerCell_module : trigger_cells_to_modules_)
233  {
234  unsigned triggerCell = triggerCell_module.first;
235  unsigned module = triggerCell_module.second;
236  auto itr_exist = modules_to_trigger_cells.emplace(module, list_triggerCells());
237  itr_exist.first->second.emplace(triggerCell);
238  //modules_to_trigger_cells.at(module).emplace(triggerCell);
239  }
240  for(const auto& module_triggerCell : modules_to_trigger_cells)
241  {
242  unsigned moduleId = module_triggerCell.first;
243  list_triggerCells triggerCellIds = module_triggerCell.second;
244  tc_map_to_cells cellsInTriggerCells;
245  // Position: barycenter of the module, from trigger cell positions
246  Basic3DVector<float> moduleVector(0.,0.,0.);
247  for(const auto& triggerCell : triggerCellIds)
248  {
249  const auto& cells_in_tc = trigger_cells_to_cells.at(triggerCell);
250  for( const unsigned cell : cells_in_tc )
251  {
252  cellsInTriggerCells.emplace(triggerCell,cell);
253  }
254  moduleVector += trigger_cells_.at(triggerCell)->position().basicVector();
255  }
256  GlobalPoint modulePoint( moduleVector/triggerCellIds.size() );
257  // FIXME: empty neighbours
258  modules_.emplace(moduleId, std::make_unique<const HGCalTriggerGeometry::Module>(moduleId, modulePoint, list_triggerCells(), triggerCellIds, cellsInTriggerCells));
259  }
260 }
261 
262 
265  "HGCalTriggerGeometryHexImp1");
std::unordered_set< unsigned > list_type
const HGCalGeometry * eeGeometry() const
const HGCalTopology & eeTopology() const
GlobalPoint getPosition(const DetId &id) const
ForwardSubdetector
void initialize(const edm::ESHandle< CaloGeometry > &) final
static const int kHGCalCellMask
Definition: HGCalDetId.h:13
int zside() const
get the z-side of the cell (1/-1)
Definition: HGCalDetId.h:51
int numberCellsHexagon(int wafer) const
const HGCalGeometry * fhGeometry() const
std::unordered_multimap< unsigned, unsigned > tc_map_type
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
int wafer() const
get the wafer #
Definition: HGCalDetId.h:42
void setCaloGeometry(const edm::ESHandle< CaloGeometry > &geom)
const HGCalDDDConstants & dddConstants() const
HLT enums.
std::string fullPath() const
Definition: FileInPath.cc:197
int waferType() const
get the wafer type
Definition: HGCalDetId.h:45
#define DEFINE_EDM_PLUGIN(factory, type, name)
HGCalTriggerGeometryHexImp1(const edm::ParameterSet &conf)
int waferTypeT(int wafer) const
Definition: vlib.h:208
const HGCalTopology & fhTopology() const
int layer() const
get the layer #
Definition: HGCalDetId.h:48