CMS 3D CMS Logo

HGCalTriggerTowerGeometryHelper.cc
Go to the documentation of this file.
1 
7 
8 #include <cmath>
9 #include <iostream>
10 #include <fstream>
11 #include <algorithm>
12 
13 
14 HGCalTriggerTowerGeometryHelper::HGCalTriggerTowerGeometryHelper(const edm::ParameterSet& conf) : minEta_(conf.getParameter<double>("minEta")),
15  maxEta_(conf.getParameter<double>("maxEta")),
16  minPhi_(conf.getParameter<double>("minPhi")),
17  maxPhi_(conf.getParameter<double>("maxPhi")),
18  nBinsEta_(conf.getParameter<int>("nBinsEta")),
19  nBinsPhi_(conf.getParameter<int>("nBinsPhi")),
20  binsEta_(conf.getParameter<std::vector<double> >("binsEta")),
21  binsPhi_(conf.getParameter<std::vector<double> >("binsPhi")) {
22 
23 
24  if(!binsEta_.empty() && ((unsigned int)(binsEta_.size()) != nBinsEta_+1)) {
25  throw edm::Exception(edm::errors::Configuration, "Configuration")
26  << "HGCalTriggerTowerGeometryHelper nBinsEta for the tower map not consistent with binsEta size"<<std::endl;
27  }
28 
29  if(!binsPhi_.empty() && ((unsigned int)(binsPhi_.size()) != nBinsPhi_+1)) {
30  throw edm::Exception(edm::errors::Configuration, "Configuration")
31  << "HGCalTriggerTowerGeometryHelper nBinsPhi for the tower map not consistent with binsPhi size"<<std::endl;
32  }
33 
34  // if the bin vecctor is empty we assume the bins to be regularly spaced
35  if(binsEta_.empty()) {
36  for(unsigned int bin1 = 0; bin1 != nBinsEta_+1; bin1++) {
37  binsEta_.push_back(minEta_+bin1*((maxEta_-minEta_)/nBinsEta_));
38  }
39  }
40 
41  // if the bin vecctor is empty we assume the bins to be regularly spaced
42  if(binsPhi_.empty()) {
43  for(unsigned int bin2 = 0; bin2 != nBinsPhi_+1; bin2++) {
44  binsPhi_.push_back(minPhi_+bin2*((maxPhi_-minPhi_)/nBinsPhi_));
45  }
46  }
47 
48  for(int zside = -1; zside <= 1; zside+=2) {
49  for(unsigned int bin1 = 0; bin1 != nBinsEta_; bin1++) {
50  for(unsigned int bin2 = 0; bin2 != nBinsPhi_; bin2++) {
51  l1t::HGCalTowerID towerId(zside, bin1, bin2);
52  tower_coords_.emplace_back(towerId.rawId(),
53  (binsEta_[bin1+1] + binsEta_[bin1])/2,
54  (binsPhi_[bin2+1] + binsPhi_[bin2])/2);
55  }
56  }
57  }
58 
59  if(conf.getParameter<bool>("readMappingFile")) {
60  // We read the TC to TT mapping from file,
61  // otherwise we derive the TC to TT mapping on the fly from eta-phi coord. of the TCs
62  std::ifstream l1tTriggerTowerMappingStream(conf.getParameter<edm::FileInPath>("L1TTriggerTowerMapping").fullPath());
63  if(!l1tTriggerTowerMappingStream.is_open()) {
64  throw cms::Exception("MissingDataFile")
65  << "Cannot open HGCalTriggerGeometry L1TTriggerTowerMapping file\n";
66  }
67 
68  unsigned trigger_cell_id = 0;
69  unsigned short iEta = 0;
70  unsigned short iPhi = 0;
71 
72  for(; l1tTriggerTowerMappingStream >> trigger_cell_id >> iEta >> iPhi;) {
73  if(iEta >= nBinsEta_ || iPhi >= nBinsPhi_) {
74  throw edm::Exception(edm::errors::Configuration, "Configuration")
75  << "HGCalTriggerTowerGeometryHelper warning inconsistent mapping TC : " << trigger_cell_id
76  << " to TT iEta: " << iEta << " iPhi: " << iPhi
77  << " when max #bins eta: " << nBinsEta_ << " phi: " << nBinsPhi_ << std::endl;
78  }
79  l1t::HGCalTowerID towerId(HGCalDetId(trigger_cell_id).zside(), iEta, iPhi);
80  cells_to_trigger_towers_[trigger_cell_id] = towerId.rawId();
81  }
82  l1tTriggerTowerMappingStream.close();
83 
84  }
85 }
86 
87 
88 const std::vector<l1t::HGCalTowerCoord>& HGCalTriggerTowerGeometryHelper::getTowerCoordinates() const {
89  return tower_coords_;
90 }
91 
92 
93 unsigned short HGCalTriggerTowerGeometryHelper::getTriggerTowerFromTriggerCell(const unsigned trigger_cell_id, const float& eta, const float& phi) const {
94  // NOTE: if the TC is not found in the map than it is mapped via eta-phi coords.
95  // this can be considered dangerous (silent failure of the map) but it actually allows to save
96  // memory mapping explicitly only what is actually needed
97  auto tower_id_itr = cells_to_trigger_towers_.find(trigger_cell_id);
98  if(tower_id_itr != cells_to_trigger_towers_.end()) return tower_id_itr->second;
99 
100  auto bin_eta_l = std::lower_bound(binsEta_.begin(), binsEta_.end(), fabs(eta));
101  unsigned int bin_eta = 0;
102  // we add a protection for TCs in Hadron part which are outside the boundaries and possible rounding effects
103  if(bin_eta_l == binsEta_.end()) {
104  if(fabs(eta) < minEta_) {
105  bin_eta = 0;
106  } else if(fabs(eta) >= maxEta_) {
107  bin_eta = nBinsEta_;
108  } else {
109  edm::LogError("HGCalTriggerTowerGeometryHelper") << " did not manage to map TC " << trigger_cell_id << " (eta: " << eta << ") to any Trigger Tower\n";
110  }
111  } else {
112  bin_eta = bin_eta_l - binsEta_.begin();
113  }
114 
115 
116  auto bin_phi_l = std::lower_bound(binsPhi_.begin(), binsPhi_.end(), phi);
117  unsigned int bin_phi = 0;
118  if(bin_phi_l == binsPhi_.end()) {
119  if(phi < minPhi_) {
120  bin_phi = nBinsPhi_;
121  } else if(phi >= maxPhi_) {
122  bin_phi = 0;
123  } else {
124  edm::LogError("HGCalTriggerTowerGeometryHelper") << " did not manage to map TC " << trigger_cell_id << " (phi: " << phi << ") to any Trigger Tower\n";
125  }
126  } else {
127  bin_phi = bin_phi_l - binsPhi_.begin();
128  }
129  int zside = eta < 0 ? -1 : 1;
130  return l1t::HGCalTowerID(zside, bin_eta, bin_phi).rawId();
131 }
T getParameter(std::string const &) const
int zside(DetId const &)
const std::vector< l1t::HGCalTowerCoord > & getTowerCoordinates() const
unsigned short getTriggerTowerFromTriggerCell(const unsigned tcId, const float &eta, const float &phi) const
std::vector< l1t::HGCalTowerCoord > tower_coords_
unsigned short rawId() const
Definition: HGCalTowerID.h:24
l1t::HGCalTowerID towerId
Definition: classes.h:34
std::string fullPath() const
Definition: FileInPath.cc:197
std::unordered_map< unsigned, short > cells_to_trigger_towers_
HGCalTriggerTowerGeometryHelper(const edm::ParameterSet &conf)