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