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