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