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