CMS 3D CMS Logo

HGCalTriggerGeometryHexLayerBasedImp1.cc
Go to the documentation of this file.
2 
7 
8 #include <vector>
9 #include <iostream>
10 #include <fstream>
11 #include <regex>
12 
14 public:
16 
17  void initialize(const edm::ESHandle<CaloGeometry>&) final;
20  const edm::ESHandle<HGCalGeometry>&) final;
21  void reset() final;
22 
23  unsigned getTriggerCellFromCell(const unsigned) const final;
24  unsigned getModuleFromCell(const unsigned) const final;
25  unsigned getModuleFromTriggerCell(const unsigned) const final;
26 
27  geom_set getCellsFromTriggerCell(const unsigned) const final;
28  geom_set getCellsFromModule(const unsigned) const final;
29  geom_set getTriggerCellsFromModule(const unsigned) const final;
30 
31  geom_ordered_set getOrderedCellsFromModule(const unsigned) const final;
32  geom_ordered_set getOrderedTriggerCellsFromModule(const unsigned) const final;
33 
34  geom_set getNeighborsFromTriggerCell(const unsigned) const final;
35 
36  GlobalPoint getTriggerCellPosition(const unsigned) const final;
37  GlobalPoint getModulePosition(const unsigned) const final;
38 
39  bool validTriggerCell(const unsigned) const final;
40  bool disconnectedModule(const unsigned) const final;
41  unsigned triggerLayer(const unsigned) const final;
42 
43 private:
49 
50  // module related maps
51  std::unordered_map<unsigned, unsigned> wafer_to_module_;
52  std::unordered_multimap<unsigned, unsigned> module_to_wafers_;
53 
54  // trigger cell related maps
55  std::unordered_map<unsigned, unsigned> cells_to_trigger_cells_;
56  std::unordered_multimap<unsigned, unsigned> trigger_cells_to_cells_;
57  std::unordered_map<unsigned, unsigned> cells_to_trigger_cells_bh_;
58  std::unordered_multimap<unsigned, unsigned> trigger_cells_to_cells_bh_;
59  std::unordered_map<unsigned, unsigned short> number_trigger_cells_in_wafers_;
60  std::unordered_map<unsigned, unsigned short> number_trigger_cells_in_wafers_bh_;
61  std::unordered_set<unsigned> invalid_triggercells_;
62 
63  // neighbor related maps
64  // trigger cell neighbors:
65  // - The key includes the module and trigger cell id
66  // - The value is a set of (module_id, trigger_cell_id)
67  std::unordered_map<int, std::set<std::pair<short, short>>> trigger_cell_neighbors_;
68  std::unordered_map<int, std::set<std::pair<short, short>>> trigger_cell_neighbors_bh_;
69 
70  // Disconnected modules and layers
71  std::unordered_set<unsigned> disconnected_modules_;
72  std::unordered_set<unsigned> disconnected_layers_;
73  std::vector<unsigned> trigger_layers_;
74 
75  // layer offsets
76  unsigned fhOffset_;
77  unsigned bhOffset_;
78  unsigned totalLayers_;
79 
80  void fillMaps();
81  void fillNeighborMaps(const edm::FileInPath&, std::unordered_map<int, std::set<std::pair<short, short>>>&);
83  unsigned packTriggerCell(unsigned, unsigned) const;
84  bool validCellId(unsigned subdet, unsigned cell_id) const;
85  bool validTriggerCellFromCells(const unsigned) const;
86 
87  // returns transverse wafer type: -1=coarse, 1=fine, 0=undefined
88  int detIdWaferType(unsigned subdet, short wafer) const;
89  unsigned packWaferCellId(unsigned subdet, unsigned wafer, unsigned cell) const;
90  unsigned packIetaIphi(unsigned ieta, unsigned iphi) const;
91  void unpackWaferCellId(unsigned wafer_cell, unsigned& wafer, unsigned& cell) const;
92  void unpackIetaIphi(unsigned ieta_iphi, unsigned& ieta, unsigned& iphi) const;
93 
94  unsigned layerWithOffset(unsigned) const;
95 };
96 
99  l1tCellsMapping_(conf.getParameter<edm::FileInPath>("L1TCellsMapping")),
100  l1tCellsBHMapping_(conf.getParameter<edm::FileInPath>("L1TCellsBHMapping")),
101  l1tModulesMapping_(conf.getParameter<edm::FileInPath>("L1TModulesMapping")),
102  l1tCellNeighborsMapping_(conf.getParameter<edm::FileInPath>("L1TCellNeighborsMapping")),
103  l1tCellNeighborsBHMapping_(conf.getParameter<edm::FileInPath>("L1TCellNeighborsBHMapping")) {
104  std::vector<unsigned> tmp_vector = conf.getParameter<std::vector<unsigned>>("DisconnectedModules");
105  std::move(tmp_vector.begin(), tmp_vector.end(), std::inserter(disconnected_modules_, disconnected_modules_.end()));
106  tmp_vector = conf.getParameter<std::vector<unsigned>>("DisconnectedLayers");
107  std::move(tmp_vector.begin(), tmp_vector.end(), std::inserter(disconnected_layers_, disconnected_layers_.end()));
108 }
109 
111  cells_to_trigger_cells_.clear();
112  trigger_cells_to_cells_.clear();
115  wafer_to_module_.clear();
116  module_to_wafers_.clear();
119  trigger_cell_neighbors_.clear();
121 }
122 
124  setCaloGeometry(calo_geometry);
128  trigger_layers_.resize(totalLayers_ + 1);
129  trigger_layers_[0] = 0; // layer number 0 doesn't exist
130  unsigned trigger_layer = 1;
131  for (unsigned layer = 1; layer < trigger_layers_.size(); layer++) {
132  if (disconnected_layers_.find(layer) == disconnected_layers_.end()) {
133  // Increase trigger layer number if the layer is not disconnected
134  trigger_layers_[layer] = trigger_layer;
135  trigger_layer++;
136  } else {
137  trigger_layers_[layer] = 0;
138  }
139  }
140  fillMaps();
144 }
145 
147  const edm::ESHandle<HGCalGeometry>& hgc_hsi_geometry,
148  const edm::ESHandle<HGCalGeometry>& hgc_hsc_geometry) {
149  throw cms::Exception("BadGeometry")
150  << "HGCalTriggerGeometryHexLayerBasedImp1 geometry cannot be initialized with the V9 HGCAL geometry";
151 }
152 
154  unsigned subdet = 0;
155  int zside = 0;
156  unsigned layer = 0;
157  unsigned wafer_trigger_cell = 0;
158  unsigned trigger_cell = 0;
159  // BH
160  if (DetId(cell_id).det() == DetId::Hcal) {
161  HcalDetId cell_det_id(cell_id);
162  if (cell_det_id.subdetId() != HcalEndcap)
163  return 0;
164  unsigned ieta = cell_det_id.ietaAbs();
165  unsigned iphi = cell_det_id.iphi();
166  layer = cell_det_id.depth();
168  zside = cell_det_id.zside();
169  auto trigger_cell_itr = cells_to_trigger_cells_bh_.find(packIetaIphi(ieta, iphi));
170  if (trigger_cell_itr == cells_to_trigger_cells_bh_.end()) {
171  throw cms::Exception("BadGeometry")
172  << "HGCalTriggerGeometry: Hcal cell ieta=" << ieta << ", iphi=" << iphi
173  << " is not mapped to any trigger cell. The trigger cell mapping should be modified.\n";
174  }
175  trigger_cell = 0;
176  wafer_trigger_cell = 0;
177  unpackWaferCellId(trigger_cell_itr->second, wafer_trigger_cell, trigger_cell);
178  }
179  // EE or FH
180  else if (DetId(cell_id).det() == DetId::Forward) {
181  HGCalDetId cell_det_id(cell_id);
182  subdet = cell_det_id.subdetId();
183  layer = cell_det_id.layer();
184  zside = cell_det_id.zside();
185  unsigned wafer = cell_det_id.wafer();
186  unsigned cell = cell_det_id.cell();
187  auto trigger_cell_itr = cells_to_trigger_cells_.find(packWaferCellId(subdet, wafer, cell));
188  if (trigger_cell_itr == cells_to_trigger_cells_.end()) {
189  throw cms::Exception("BadGeometry")
190  << "HGCalTriggerGeometry: HGCal cell " << cell << " in wafer " << wafer
191  << " is not mapped to any trigger cell. The trigger cell mapping should be modified.\n";
192  }
193  trigger_cell = 0;
194  wafer_trigger_cell = 0;
195  unpackWaferCellId(trigger_cell_itr->second, wafer_trigger_cell, trigger_cell);
196  }
197  return HGCalDetId((ForwardSubdetector)subdet, zside, layer, 1, wafer_trigger_cell, trigger_cell).rawId();
198 }
199 
200 unsigned HGCalTriggerGeometryHexLayerBasedImp1::getModuleFromCell(const unsigned cell_id) const {
202 }
203 
204 unsigned HGCalTriggerGeometryHexLayerBasedImp1::getModuleFromTriggerCell(const unsigned trigger_cell_id) const {
205  HGCalDetId trigger_cell_det_id(trigger_cell_id);
206  unsigned module = 0;
207  // BH
208  if (trigger_cell_det_id.subdetId() == ForwardSubdetector::HGCHEB) {
209  // For BH, the module ID is currently encoded as the wafer in HGCalDetId
210  module = trigger_cell_det_id.wafer();
211  }
212  // EE or FH
213  else {
214  auto module_itr = wafer_to_module_.find(trigger_cell_det_id.wafer());
215  if (module_itr == wafer_to_module_.end()) {
216  throw cms::Exception("BadGeometry")
217  << trigger_cell_det_id << "HGCalTriggerGeometry: Wafer " << trigger_cell_det_id.wafer()
218  << " is not mapped to any trigger module. The module mapping should be modified. See "
219  "https://twiki.cern.ch/twiki/bin/viewauth/CMS/HGCALTriggerPrimitivesSimulation#Trigger_geometry for "
220  "details.\n";
221  }
222  module = module_itr->second;
223  }
224  return HGCalDetId((ForwardSubdetector)trigger_cell_det_id.subdetId(),
225  trigger_cell_det_id.zside(),
226  trigger_cell_det_id.layer(),
227  (trigger_cell_det_id.waferType() == 1 ? 1 : 0),
228  module,
230  .rawId();
231 }
232 
234  const unsigned trigger_cell_id) const {
235  HGCalDetId trigger_cell_det_id(trigger_cell_id);
236  geom_set cell_det_ids;
237  // BH
238  if (trigger_cell_det_id.subdetId() == ForwardSubdetector::HGCHEB) {
239  unsigned subdet = trigger_cell_det_id.subdetId();
240  unsigned trigger_wafer = trigger_cell_det_id.wafer();
241  unsigned trigger_cell = trigger_cell_det_id.cell();
242  const auto& cell_range =
243  trigger_cells_to_cells_bh_.equal_range(packWaferCellId(subdet, trigger_wafer, trigger_cell));
244  for (auto tc_c_itr = cell_range.first; tc_c_itr != cell_range.second; tc_c_itr++) {
245  unsigned ieta = 0;
246  unsigned iphi = 0;
247  unpackIetaIphi(tc_c_itr->second, ieta, iphi);
248  unsigned cell_det_id =
249  HcalDetId(HcalEndcap, trigger_cell_det_id.zside() * ieta, iphi, trigger_cell_det_id.layer()).rawId();
250  if (validCellId(subdet, cell_det_id))
251  cell_det_ids.emplace(cell_det_id);
252  }
253  }
254  // EE or FH
255  else {
256  unsigned subdet = trigger_cell_det_id.subdetId();
257  unsigned trigger_wafer = trigger_cell_det_id.wafer();
258  unsigned trigger_cell = trigger_cell_det_id.cell();
259  const auto& cell_range = trigger_cells_to_cells_.equal_range(packWaferCellId(subdet, trigger_wafer, trigger_cell));
260  for (auto tc_c_itr = cell_range.first; tc_c_itr != cell_range.second; tc_c_itr++) {
261  unsigned wafer = 0;
262  unsigned cell = 0;
263  unpackWaferCellId(tc_c_itr->second, wafer, cell);
264  unsigned wafer_type = (detIdWaferType(subdet, wafer) == 1 ? 1 : 0);
265  unsigned cell_det_id = HGCalDetId((ForwardSubdetector)trigger_cell_det_id.subdetId(),
266  trigger_cell_det_id.zside(),
267  trigger_cell_det_id.layer(),
268  wafer_type,
269  wafer,
270  cell)
271  .rawId();
272  if (validCellId(subdet, cell_det_id))
273  cell_det_ids.emplace(cell_det_id);
274  }
275  }
276  return cell_det_ids;
277 }
278 
280  const unsigned module_id) const {
281  geom_set cell_det_ids;
282  geom_set trigger_cells = getTriggerCellsFromModule(module_id);
283  for (auto trigger_cell_id : trigger_cells) {
284  geom_set cells = getCellsFromTriggerCell(trigger_cell_id);
285  cell_det_ids.insert(cells.begin(), cells.end());
286  }
287  return cell_det_ids;
288 }
289 
291  const unsigned module_id) const {
292  geom_ordered_set cell_det_ids;
293  geom_ordered_set trigger_cells = getOrderedTriggerCellsFromModule(module_id);
294  for (auto trigger_cell_id : trigger_cells) {
295  geom_set cells = getCellsFromTriggerCell(trigger_cell_id);
296  cell_det_ids.insert(cells.begin(), cells.end());
297  }
298  return cell_det_ids;
299 }
300 
302  const unsigned module_id) const {
303  HGCalDetId module_det_id(module_id);
304  geom_set trigger_cell_det_ids;
305  // BH
306  if (module_det_id.subdetId() == ForwardSubdetector::HGCHEB) {
307  unsigned module = module_det_id.wafer();
308  // loop on the trigger cells in each module
309  for (int trigger_cell = 0; trigger_cell < number_trigger_cells_in_wafers_bh_.at(module); trigger_cell++) {
310  HGCalDetId trigger_cell_id((ForwardSubdetector)module_det_id.subdetId(),
311  module_det_id.zside(),
312  module_det_id.layer(),
313  1,
314  module,
315  trigger_cell);
316  if (validTriggerCell(trigger_cell_id))
317  trigger_cell_det_ids.emplace(trigger_cell_id.rawId());
318  }
319  }
320  // EE or FH
321  else {
322  unsigned module = module_det_id.wafer();
323  auto wafer_itrs = module_to_wafers_.equal_range(module);
324  // loop on the wafers included in the module
325  for (auto wafer_itr = wafer_itrs.first; wafer_itr != wafer_itrs.second; wafer_itr++) {
326  unsigned wafer = wafer_itr->second;
327  // loop on the trigger cells in each wafer
328  for (int trigger_cell = 0; trigger_cell < number_trigger_cells_in_wafers_.at(wafer); trigger_cell++) {
329  HGCalDetId trigger_cell_id((ForwardSubdetector)module_det_id.subdetId(),
330  module_det_id.zside(),
331  module_det_id.layer(),
332  1,
333  wafer,
334  trigger_cell);
335  if (validTriggerCell(trigger_cell_id))
336  trigger_cell_det_ids.emplace(trigger_cell_id.rawId());
337  }
338  }
339  }
340  return trigger_cell_det_ids;
341 }
342 
344  const unsigned module_id) const {
345  HGCalDetId module_det_id(module_id);
346  geom_ordered_set trigger_cell_det_ids;
347  // BH
348  if (module_det_id.subdetId() == ForwardSubdetector::HGCHEB) {
349  unsigned module = module_det_id.wafer();
350  // loop on the trigger cells in each module
351  for (int trigger_cell = 0; trigger_cell < number_trigger_cells_in_wafers_bh_.at(module); trigger_cell++) {
352  HGCalDetId trigger_cell_id((ForwardSubdetector)module_det_id.subdetId(),
353  module_det_id.zside(),
354  module_det_id.layer(),
355  1,
356  module,
357  trigger_cell);
358  if (validTriggerCell(trigger_cell_id))
359  trigger_cell_det_ids.emplace(trigger_cell_id.rawId());
360  }
361  }
362  // EE or FH
363  else {
364  unsigned module = module_det_id.wafer();
365  auto wafer_itrs = module_to_wafers_.equal_range(module);
366  // loop on the wafers included in the module
367  for (auto wafer_itr = wafer_itrs.first; wafer_itr != wafer_itrs.second; wafer_itr++) {
368  unsigned wafer = wafer_itr->second;
369  // loop on the trigger cells in each wafer
370  for (int trigger_cell = 0; trigger_cell < number_trigger_cells_in_wafers_.at(wafer); trigger_cell++) {
371  HGCalDetId trigger_cell_id((ForwardSubdetector)module_det_id.subdetId(),
372  module_det_id.zside(),
373  module_det_id.layer(),
374  1,
375  wafer,
376  trigger_cell);
377  if (validTriggerCell(trigger_cell_id))
378  trigger_cell_det_ids.emplace(trigger_cell_id.rawId());
379  }
380  }
381  }
382  return trigger_cell_det_ids;
383 }
384 
386  const unsigned trigger_cell_id) const {
387  HGCalDetId trigger_cell_det_id(trigger_cell_id);
388  // Choose scintillator or silicon map
389  const auto& neighbors_map = (trigger_cell_det_id.subdetId() == ForwardSubdetector::HGCHEB ? trigger_cell_neighbors_bh_
391  unsigned module = trigger_cell_det_id.wafer();
392  unsigned trigger_cell = trigger_cell_det_id.cell();
393  // retrieve neighbors
394  unsigned trigger_cell_key = packTriggerCell(module, trigger_cell);
395  geom_set neighbor_detids;
396  auto neighbors_itr = neighbors_map.find(trigger_cell_key);
397  if (neighbors_itr == neighbors_map.end()) {
398  throw cms::Exception("BadGeometry") << "HGCalTriggerGeometry: Neighbors are not defined for trigger cell "
399  << trigger_cell << " in module " << module
400  << ". The trigger cell neighbor mapping should be modified. \n";
401  }
402  const auto& neighbors = neighbors_itr->second;
403  // create HGCalDetId of neighbors and check their validity
404  neighbor_detids.reserve(neighbors.size());
405  for (const auto& module_tc : neighbors) {
406  HGCalDetId neighbor_det_id((ForwardSubdetector)trigger_cell_det_id.subdetId(),
407  trigger_cell_det_id.zside(),
408  trigger_cell_det_id.layer(),
409  1,
410  module_tc.first,
411  module_tc.second);
412  if (validTriggerCell(neighbor_det_id.rawId())) {
413  neighbor_detids.emplace(neighbor_det_id.rawId());
414  }
415  }
416  return neighbor_detids;
417 }
418 
420  unsigned subdet = HGCalDetId(trigger_cell_det_id).subdetId();
421  // Position: barycenter of the trigger cell.
422  Basic3DVector<float> triggerCellVector(0., 0., 0.);
423  const auto cell_ids = getCellsFromTriggerCell(trigger_cell_det_id);
424  // BH
425  if (subdet == ForwardSubdetector::HGCHEB) {
426  for (const auto& cell : cell_ids) {
427  HcalDetId cellDetId(cell);
428  triggerCellVector += bhGeometry()->getPosition(cellDetId).basicVector();
429  }
430  }
431  // EE or FH
432  else {
433  for (const auto& cell : cell_ids) {
434  HGCalDetId cellDetId(cell);
435  triggerCellVector += (cellDetId.subdetId() == ForwardSubdetector::HGCEE ? eeGeometry()->getPosition(cellDetId)
436  : fhGeometry()->getPosition(cellDetId))
437  .basicVector();
438  }
439  }
440  return GlobalPoint(triggerCellVector / cell_ids.size());
441 }
442 
444  unsigned subdet = HGCalDetId(module_det_id).subdetId();
445  // Position: barycenter of the module.
446  Basic3DVector<float> moduleVector(0., 0., 0.);
447  const auto cell_ids = getCellsFromModule(module_det_id);
448  // BH
449  if (subdet == ForwardSubdetector::HGCHEB) {
450  for (const auto& cell : cell_ids) {
451  HcalDetId cellDetId(cell);
452  moduleVector += bhGeometry()->getPosition(cellDetId).basicVector();
453  }
454  }
455  // EE or FH
456  else {
457  for (const auto& cell : cell_ids) {
458  HGCalDetId cellDetId(cell);
459  moduleVector += (cellDetId.subdetId() == ForwardSubdetector::HGCEE ? eeGeometry()->getPosition(cellDetId)
460  : fhGeometry()->getPosition(cellDetId))
461  .basicVector();
462  }
463  }
464  return GlobalPoint(moduleVector / cell_ids.size());
465 }
466 
468  //
469  // read module mapping file
470  std::ifstream l1tModulesMappingStream(l1tModulesMapping_.fullPath());
471  if (!l1tModulesMappingStream.is_open()) {
472  throw cms::Exception("MissingDataFile") << "Cannot open HGCalTriggerGeometry L1TModulesMapping file\n";
473  }
474  short trigger_wafer = 0;
475  short module = 0;
476  for (; l1tModulesMappingStream >> trigger_wafer >> module;) {
477  wafer_to_module_.emplace(trigger_wafer, module);
478  module_to_wafers_.emplace(module, trigger_wafer);
479  // Default number of trigger cell in wafer is 0
480  number_trigger_cells_in_wafers_.emplace(trigger_wafer, 0);
481  }
482  if (!l1tModulesMappingStream.eof())
483  edm::LogWarning("HGCalTriggerGeometry")
484  << "Error reading L1TModulesMapping '" << trigger_wafer << " " << module << "' \n";
485  l1tModulesMappingStream.close();
486  // read trigger cell mapping file
487  std::ifstream l1tCellsMappingStream(l1tCellsMapping_.fullPath());
488  if (!l1tCellsMappingStream.is_open()) {
489  throw cms::Exception("MissingDataFile") << "Cannot open HGCalTriggerGeometry L1TCellsMapping file\n";
490  }
491  short subdet = 0;
492  short wafer = 0;
493  short cell = 0;
494  trigger_wafer = 0;
495  short trigger_cell = 0;
496  for (; l1tCellsMappingStream >> subdet >> wafer >> cell >> trigger_wafer >> trigger_cell;) {
497  unsigned cell_key = packWaferCellId(subdet, wafer, cell);
498  unsigned trigger_cell_key = packWaferCellId(subdet, trigger_wafer, trigger_cell);
499  // fill cell <-> trigger cell mappings
500  cells_to_trigger_cells_.emplace(cell_key, trigger_cell_key);
501  trigger_cells_to_cells_.emplace(trigger_cell_key, cell_key);
502  // fill number of trigger cells in wafers
503  auto itr_insert = number_trigger_cells_in_wafers_.emplace(trigger_wafer, 0);
504  if (trigger_cell + 1 > itr_insert.first->second)
505  itr_insert.first->second = trigger_cell + 1;
506  }
507  if (!l1tCellsMappingStream.eof())
508  edm::LogWarning("HGCalTriggerGeometry") << "Error reading L1TCellsMapping '" << subdet << " " << wafer << " "
509  << cell << " " << trigger_wafer << " " << trigger_cell << "' \n";
510  l1tCellsMappingStream.close();
511  // read BH trigger cell mapping file
512  std::ifstream l1tCellsBHMappingStream(l1tCellsBHMapping_.fullPath());
513  if (!l1tCellsBHMappingStream.is_open()) {
514  throw cms::Exception("MissingDataFile") << "Cannot open HGCalTriggerGeometry L1TCellsBHMapping file\n";
515  }
516  short ieta = 0;
517  short iphi = 0;
518  trigger_wafer = 0;
519  trigger_cell = 0;
520  for (; l1tCellsBHMappingStream >> ieta >> iphi >> trigger_wafer >> trigger_cell;) {
521  unsigned cell_key = packIetaIphi(ieta, iphi);
522  unsigned trigger_cell_key = packWaferCellId(ForwardSubdetector::HGCHEB, trigger_wafer, trigger_cell);
523  // fill cell <-> trigger cell mappings
524  cells_to_trigger_cells_bh_.emplace(cell_key, trigger_cell_key);
525  trigger_cells_to_cells_bh_.emplace(trigger_cell_key, cell_key);
526  // fill number of trigger cells in wafers
527  auto itr_insert = number_trigger_cells_in_wafers_bh_.emplace(trigger_wafer, 0);
528  if (trigger_cell + 1 > itr_insert.first->second)
529  itr_insert.first->second = trigger_cell + 1;
530  }
531  if (!l1tCellsBHMappingStream.eof())
532  edm::LogWarning("HGCalTriggerGeometry") << "Error reading L1TCellsBHMapping '" << ieta << " " << iphi << " "
533  << trigger_wafer << " " << trigger_cell << "' \n";
534  l1tCellsBHMappingStream.close();
535 }
536 
538  const edm::FileInPath& file, std::unordered_map<int, std::set<std::pair<short, short>>>& neighbors_map) {
539  // Fill trigger neighbor map
540  std::ifstream l1tCellNeighborsMappingStream(file.fullPath());
541  if (!l1tCellNeighborsMappingStream.is_open()) {
542  throw cms::Exception("MissingDataFile") << "Cannot open HGCalTriggerGeometry L1TCellNeighborsMapping file\n";
543  }
544  for (std::array<char, 512> buffer; l1tCellNeighborsMappingStream.getline(&buffer[0], 512);) {
545  std::string line(&buffer[0]);
546  // Extract keys consisting of the module id
547  // and of the trigger cell id
548  // Match patterns (X,Y)
549  // where X is a number with less than 4 digis
550  // and Y is a number with less than 4 digits
551  std::regex key_regex("\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*\\)");
552  std::vector<std::string> key_tokens{std::sregex_token_iterator(line.begin(), line.end(), key_regex), {}};
553  if (key_tokens.empty()) {
554  throw cms::Exception("BadGeometry") << "Syntax error in the L1TCellNeighborsMapping:\n"
555  << " Cannot find the trigger cell key in line:\n"
556  << " '" << &buffer[0] << "'\n";
557  }
558  std::regex digits_regex("\\d{1,3}");
559  std::vector<std::string> module_tc{
560  std::sregex_token_iterator(key_tokens[0].begin(), key_tokens[0].end(), digits_regex), {}};
561  // get module and cell id
562  int module = std::stoi(module_tc[0]);
563  int trigger_cell = std::stoi(module_tc[1]);
564  unsigned map_key = packTriggerCell(module, trigger_cell);
565  // Extract neighbors
566  // Match patterns (X,Y)
567  // where X is a number with less than 4 digits
568  // and Y is a number with less than 4 digits
569  std::regex neighbors_regex("\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*\\)");
570  std::vector<std::string> neighbors_tokens{std::sregex_token_iterator(line.begin(), line.end(), neighbors_regex),
571  {}};
572  if (neighbors_tokens.size() < 2) {
573  throw cms::Exception("BadGeometry") << "Syntax error in the L1TCellNeighborsMapping:\n"
574  << " Cannot find any neighbor in line:\n"
575  << " '" << &buffer[0] << "'\n";
576  }
577  auto itr_insert = neighbors_map.emplace(map_key, std::set<std::pair<short, short>>());
578  // The first element is the key, so start at index 1
579  for (unsigned i = 1; i < neighbors_tokens.size(); i++) {
580  const auto& neighbor = neighbors_tokens[i];
581  std::vector<std::string> pair_neighbor{std::sregex_token_iterator(neighbor.begin(), neighbor.end(), digits_regex),
582  {}};
583  short neighbor_module(std::stoi(pair_neighbor[0]));
584  short neighbor_cell(std::stoi(pair_neighbor[1]));
585  itr_insert.first->second.emplace(neighbor_module, neighbor_cell);
586  }
587  }
588  if (!l1tCellNeighborsMappingStream.eof())
589  edm::LogWarning("HGCalTriggerGeometry") << "Error reading L1TCellNeighborsMapping'\n";
590  l1tCellNeighborsMappingStream.close();
591 }
592 
594  unsigned n_layers_ee = eeTopology().dddConstants().layers(true);
595  for (unsigned layer = 1; layer <= n_layers_ee; layer++) {
596  for (const auto& wafer_module : wafer_to_module_) {
597  unsigned wafer = wafer_module.first;
598  // loop on the trigger cells in each wafer
599  for (int trigger_cell = 0; trigger_cell < number_trigger_cells_in_wafers_.at(wafer); trigger_cell++) {
600  std::set<unsigned> trigger_cell_ids;
601  trigger_cell_ids.emplace(HGCalDetId(ForwardSubdetector::HGCEE, -1, layer, 1, wafer, trigger_cell));
602  trigger_cell_ids.emplace(HGCalDetId(ForwardSubdetector::HGCEE, 1, layer, 1, wafer, trigger_cell));
603  trigger_cell_ids.emplace(HGCalDetId(ForwardSubdetector::HGCHEF, -1, layer, 1, wafer, trigger_cell));
604  trigger_cell_ids.emplace(HGCalDetId(ForwardSubdetector::HGCHEF, 1, layer, 1, wafer, trigger_cell));
605  for (unsigned trigger_cell : trigger_cell_ids) {
606  if (!validTriggerCellFromCells(trigger_cell))
607  invalid_triggercells_.emplace(trigger_cell);
608  for (unsigned neighbor : getNeighborsFromTriggerCell(trigger_cell)) {
609  if (!validTriggerCellFromCells(neighbor))
610  invalid_triggercells_.emplace(neighbor);
611  }
612  }
613  }
614  }
615  }
616 }
617 
618 unsigned HGCalTriggerGeometryHexLayerBasedImp1::packWaferCellId(unsigned subdet, unsigned wafer, unsigned cell) const {
619  unsigned packed_value = 0;
620  const int kSubdetMask = 0x7;
621  packed_value |= ((cell & HGCalDetId::kHGCalCellMask) << HGCalDetId::kHGCalCellOffset);
622  packed_value |= ((wafer & HGCalDetId::kHGCalWaferMask) << HGCalDetId::kHGCalWaferOffset);
623  packed_value |= ((subdet & kSubdetMask) << (HGCalDetId::kHGCalWaferTypeOffset));
624  return packed_value;
625 }
626 
627 unsigned HGCalTriggerGeometryHexLayerBasedImp1::packIetaIphi(unsigned ieta, unsigned iphi) const {
628  unsigned packed_value = 0;
629  packed_value |= (iphi & HcalDetId::kHcalPhiMask2);
630  packed_value |= ((ieta & HcalDetId::kHcalEtaMask2) << HcalDetId::kHcalEtaOffset2);
631  return packed_value;
632 }
633 
635  unsigned& wafer,
636  unsigned& cell) const {
637  cell = wafer_cell & HGCalDetId::kHGCalCellMask;
639 }
640 
641 void HGCalTriggerGeometryHexLayerBasedImp1::unpackIetaIphi(unsigned ieta_iphi, unsigned& ieta, unsigned& iphi) const {
642  iphi = ieta_iphi & HcalDetId::kHcalPhiMask2;
644 }
645 
646 bool HGCalTriggerGeometryHexLayerBasedImp1::validTriggerCell(const unsigned trigger_cell_id) const {
647  return invalid_triggercells_.find(trigger_cell_id) == invalid_triggercells_.end();
648 }
649 
650 bool HGCalTriggerGeometryHexLayerBasedImp1::disconnectedModule(const unsigned module_id) const {
651  bool disconnected = false;
652  if (disconnected_modules_.find(HGCalDetId(module_id).wafer()) != disconnected_modules_.end())
653  disconnected = true;
654  if (disconnected_layers_.find(layerWithOffset(module_id)) != disconnected_layers_.end())
655  disconnected = true;
656  return disconnected;
657 }
658 
659 unsigned HGCalTriggerGeometryHexLayerBasedImp1::triggerLayer(const unsigned id) const {
660  unsigned layer = layerWithOffset(id);
661  if (layer >= trigger_layers_.size())
662  return 0;
663  return trigger_layers_[layer];
664 }
665 
666 bool HGCalTriggerGeometryHexLayerBasedImp1::validTriggerCellFromCells(const unsigned trigger_cell_id) const {
667  // Check the validity of a trigger cell with the
668  // validity of the cells. One valid cell in the
669  // trigger cell is enough to make the trigger cell
670  // valid.
671  HGCalDetId trigger_cell_det_id(trigger_cell_id);
672  unsigned subdet = trigger_cell_det_id.subdetId();
673  const geom_set cells = getCellsFromTriggerCell(trigger_cell_id);
674  bool is_valid = false;
675  for (const auto cell_id : cells) {
676  is_valid |= validCellId(subdet, cell_id);
677  if (is_valid)
678  break;
679  }
680  return is_valid;
681 }
682 
683 bool HGCalTriggerGeometryHexLayerBasedImp1::validCellId(unsigned subdet, unsigned cell_id) const {
684  bool is_valid = false;
685  switch (subdet) {
687  is_valid = eeTopology().valid(cell_id);
688  break;
690  is_valid = fhTopology().valid(cell_id);
691  break;
693  is_valid = bhTopology().valid(cell_id);
694  break;
695  default:
696  is_valid = false;
697  break;
698  }
699  return is_valid;
700 }
701 
702 unsigned HGCalTriggerGeometryHexLayerBasedImp1::packTriggerCell(unsigned module, unsigned trigger_cell) const {
703  unsigned packed_value = 0;
704  packed_value |= ((trigger_cell & HGCalDetId::kHGCalCellMask) << HGCalDetId::kHGCalCellOffset);
705  packed_value |= ((module & HGCalDetId::kHGCalWaferMask) << HGCalDetId::kHGCalWaferOffset);
706  return packed_value;
707 }
708 
709 int HGCalTriggerGeometryHexLayerBasedImp1::detIdWaferType(unsigned subdet, short wafer) const {
710  int wafer_type = 0;
711  switch (subdet) {
712  // HGCalDDDConstants::waferTypeT() returns 2=coarse, 1=fine
713  // HGCalDetId::waferType() returns -1=coarse, 1=fine
714  // Convert to HGCalDetId waferType
716  wafer_type = (eeTopology().dddConstants().waferTypeT(wafer) == 2 ? -1 : 1);
717  break;
719  wafer_type = (fhTopology().dddConstants().waferTypeT(wafer) == 2 ? -1 : 1);
720  break;
721  default:
722  break;
723  };
724  return wafer_type;
725 }
726 
728  HGCalDetId detid(id);
729  unsigned layer = 0;
730  switch (detid.subdetId()) {
732  layer = detid.layer();
733  break;
735  layer = fhOffset_ + detid.layer();
736  break;
738  layer = bhOffset_ + detid.layer();
739  break;
740  };
741  return layer;
742 }
743 
746  "HGCalTriggerGeometryHexLayerBasedImp1");
T getParameter(std::string const &) const
HGCalTriggerGeometryHexLayerBasedImp1(const edm::ParameterSet &conf)
std::unordered_multimap< unsigned, unsigned > module_to_wafers_
const HcalDDDRecConstants * dddConstants() const
Definition: HcalTopology.h:168
void initialize(const edm::ESHandle< CaloGeometry > &) final
bool valid(const DetId &id) const override
static uint32_t kHcalEtaMask2
Definition: HcalDetId.h:21
bool is_valid(const Digi &d)
Definition: GenericDigi.h:64
int zside() const
get the z-side of the cell (1/-1)
Definition: HcalDetId.h:149
std::unordered_multimap< unsigned, unsigned > trigger_cells_to_cells_
std::unordered_map< unsigned, unsigned short > number_trigger_cells_in_wafers_bh_
const HGCalGeometry * eeGeometry() const
const HGCalTopology & eeTopology() const
int detIdWaferType(unsigned subdet, short wafer) const
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
static const int kHGCalWaferTypeOffset
Definition: HGCalDetId.h:16
GlobalPoint getPosition(const DetId &id) const
unsigned packIetaIphi(unsigned ieta, unsigned iphi) const
int zside(DetId const &)
std::unordered_map< unsigned, unsigned > cells_to_trigger_cells_
ForwardSubdetector
bool validCellId(unsigned subdet, unsigned cell_id) const
unsigned getModuleFromTriggerCell(const unsigned) const final
void fillNeighborMaps(const edm::FileInPath &, std::unordered_map< int, std::set< std::pair< short, short >>> &)
std::unordered_map< unsigned, unsigned short > number_trigger_cells_in_wafers_
void unpackWaferCellId(unsigned wafer_cell, unsigned &wafer, unsigned &cell) const
static const int kHGCalCellOffset
Definition: HGCalDetId.h:12
static const int kHGCalCellMask
Definition: HGCalDetId.h:13
int depth() const
get the tower depth
Definition: HcalDetId.h:166
unsigned getTriggerCellFromCell(const unsigned) const final
unsigned int layers(bool reco) const
int zside() const
get the z-side of the cell (1/-1)
Definition: HGCalDetId.h:51
const HGCalGeometry * fhGeometry() const
const HcalTopology & bhTopology() const
void unpackIetaIphi(unsigned ieta_iphi, unsigned &ieta, unsigned &iphi) const
unsigned packWaferCellId(unsigned subdet, unsigned wafer, unsigned cell) const
std::unordered_map< unsigned, unsigned > cells_to_trigger_cells_bh_
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
GlobalPoint getTriggerCellPosition(const unsigned) const final
#define end
Definition: vmac.h:39
int wafer() const
get the wafer #
Definition: HGCalDetId.h:42
GlobalPoint getPosition(const DetId &id) const
void setCaloGeometry(const edm::ESHandle< CaloGeometry > &geom)
const HcalGeometry * bhGeometry() const
int ietaAbs() const
get the absolute value of the cell ieta
Definition: HcalDetId.h:154
int iphi() const
get the cell iphi
Definition: HcalDetId.h:161
Definition: DetId.h:18
std::unordered_map< unsigned, unsigned > wafer_to_module_
int cell() const
get the absolute value of the cell #&#39;s in x and y
Definition: HGCalDetId.h:39
const HGCalDDDConstants & dddConstants() const
static uint32_t kHcalPhiMask2
Definition: HcalDetId.h:17
std::unordered_map< int, std::set< std::pair< short, short > > > trigger_cell_neighbors_bh_
int getMaxDepth(const int &type) const
static uint32_t kHcalEtaOffset2
Definition: HcalDetId.h:19
geom_set getNeighborsFromTriggerCell(const unsigned) const final
static const int kHGCalWaferOffset
Definition: HGCalDetId.h:14
std::unordered_multimap< unsigned, unsigned > trigger_cells_to_cells_bh_
std::set< unsigned > geom_ordered_set
geom_ordered_set getOrderedCellsFromModule(const unsigned) const final
GlobalPoint getModulePosition(const unsigned) const final
#define begin
Definition: vmac.h:32
HLT enums.
geom_set getCellsFromTriggerCell(const unsigned) const final
std::unordered_set< unsigned > geom_set
std::string fullPath() const
Definition: FileInPath.cc:163
int waferType() const
get the wafer type
Definition: HGCalDetId.h:45
#define DEFINE_EDM_PLUGIN(factory, type, name)
bool valid(const DetId &id) const override
Is this a valid cell id.
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:56
int waferTypeT(int wafer) const
Definition: vlib.h:208
geom_ordered_set getOrderedTriggerCellsFromModule(const unsigned) const final
std::unordered_map< int, std::set< std::pair< short, short > > > trigger_cell_neighbors_
def move(src, dest)
Definition: eostools.py:511
const HGCalTopology & fhTopology() const
int layer() const
get the layer #
Definition: HGCalDetId.h:48
geom_set getTriggerCellsFromModule(const unsigned) const final
static const int kHGCalWaferMask
Definition: HGCalDetId.h:15