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