Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-23 03:27:32

0001 
0002 #include "L1Trigger/L1THGCal/interface/HGCalTriggerTowerGeometryHelper.h"
0003 #include "FWCore/Utilities/interface/Exception.h"
0004 #include "FWCore/Utilities/interface/EDMException.h"
0005 #include "DataFormats/DetId/interface/DetId.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 
0008 #include <cmath>
0009 #include <iostream>
0010 #include <fstream>
0011 #include <algorithm>
0012 
0013 HGCalTriggerTowerGeometryHelper::HGCalTriggerTowerGeometryHelper(const edm::ParameterSet& conf)
0014     : doNose_(conf.getParameter<bool>("doNose")),
0015       minEta_(conf.getParameter<double>("minEta")),
0016       maxEta_(conf.getParameter<double>("maxEta")),
0017       minPhi_(conf.getParameter<double>("minPhi")),
0018       maxPhi_(conf.getParameter<double>("maxPhi")),
0019       nBinsEta_(conf.getParameter<int>("nBinsEta")),
0020       nBinsPhi_(conf.getParameter<int>("nBinsPhi")),
0021       binsEta_(conf.getParameter<std::vector<double> >("binsEta")),
0022       binsPhi_(conf.getParameter<std::vector<double> >("binsPhi")) {
0023   if (!binsEta_.empty() && ((unsigned int)(binsEta_.size()) != nBinsEta_ + 1)) {
0024     throw edm::Exception(edm::errors::Configuration, "Configuration")
0025         << "HGCalTriggerTowerGeometryHelper nBinsEta for the tower map not consistent with binsEta size" << std::endl;
0026   }
0027 
0028   if (!binsPhi_.empty() && ((unsigned int)(binsPhi_.size()) != nBinsPhi_ + 1)) {
0029     throw edm::Exception(edm::errors::Configuration, "Configuration")
0030         << "HGCalTriggerTowerGeometryHelper nBinsPhi for the tower map not consistent with binsPhi size" << std::endl;
0031   }
0032 
0033   // if the bin vecctor is empty we assume the bins to be regularly spaced
0034   if (binsEta_.empty()) {
0035     for (unsigned int bin1 = 0; bin1 != nBinsEta_ + 1; bin1++) {
0036       binsEta_.push_back(minEta_ + bin1 * ((maxEta_ - minEta_) / nBinsEta_));
0037     }
0038   }
0039 
0040   // if the bin vecctor is empty we assume the bins to be regularly spaced
0041   if (binsPhi_.empty()) {
0042     for (unsigned int bin2 = 0; bin2 != nBinsPhi_ + 1; bin2++) {
0043       binsPhi_.push_back(minPhi_ + bin2 * ((maxPhi_ - minPhi_) / nBinsPhi_));
0044     }
0045   }
0046 
0047   for (int zside = -1; zside <= 1; zside += 2) {
0048     for (unsigned int bin1 = 0; bin1 != nBinsEta_; bin1++) {
0049       for (unsigned int bin2 = 0; bin2 != nBinsPhi_; bin2++) {
0050         l1t::HGCalTowerID towerId(doNose_, zside, bin1, bin2);
0051         tower_coords_.emplace_back(towerId.rawId(),
0052                                    zside * ((binsEta_[bin1 + 1] + binsEta_[bin1]) / 2),
0053                                    (binsPhi_[bin2 + 1] + binsPhi_[bin2]) / 2);
0054       }
0055     }
0056   }
0057 
0058   if (conf.getParameter<bool>("readMappingFile")) {
0059     // We read the TC to TT mapping from file,
0060     // otherwise we derive the TC to TT mapping on the fly from eta-phi coord. of the TCs
0061     std::ifstream l1tTriggerTowerMappingStream(conf.getParameter<edm::FileInPath>("L1TTriggerTowerMapping").fullPath());
0062     if (!l1tTriggerTowerMappingStream.is_open()) {
0063       throw cms::Exception("MissingDataFile") << "Cannot open HGCalTriggerGeometry L1TTriggerTowerMapping file\n";
0064     }
0065 
0066     unsigned trigger_cell_id = 0;
0067     unsigned short iEta = 0;
0068     unsigned short iPhi = 0;
0069 
0070     for (; l1tTriggerTowerMappingStream >> trigger_cell_id >> iEta >> iPhi;) {
0071       if (iEta >= nBinsEta_ || iPhi >= nBinsPhi_) {
0072         throw edm::Exception(edm::errors::Configuration, "Configuration")
0073             << "HGCalTriggerTowerGeometryHelper warning inconsistent mapping TC : " << trigger_cell_id
0074             << " to TT iEta: " << iEta << " iPhi: " << iPhi << " when max #bins eta: " << nBinsEta_
0075             << " phi: " << nBinsPhi_ << std::endl;
0076       }
0077       l1t::HGCalTowerID towerId(doNose_, triggerTools_.zside(DetId(trigger_cell_id)), iEta, iPhi);
0078       cells_to_trigger_towers_[trigger_cell_id] = towerId.rawId();
0079     }
0080     l1tTriggerTowerMappingStream.close();
0081   }
0082 }
0083 
0084 const std::vector<l1t::HGCalTowerCoord>& HGCalTriggerTowerGeometryHelper::getTowerCoordinates() const {
0085   return tower_coords_;
0086 }
0087 
0088 unsigned short HGCalTriggerTowerGeometryHelper::getTriggerTowerFromEtaPhi(const float& eta, const float& phi) const {
0089   auto bin_eta_l = std::lower_bound(binsEta_.begin(), binsEta_.end(), fabs(eta));
0090   unsigned int bin_eta = 0;
0091   // we add a protection for TCs in Hadron part which are outside the boundaries and possible rounding effects
0092   if (bin_eta_l == binsEta_.end()) {
0093     if (fabs(eta) < minEta_) {
0094       bin_eta = 0;
0095     } else if (fabs(eta) >= maxEta_) {
0096       bin_eta = nBinsEta_;
0097     } else {
0098       edm::LogError("HGCalTriggerTowerGeometryHelper")
0099           << " did not manage to map eta " << eta << " to any Trigger Tower\n";
0100     }
0101   } else {
0102     bin_eta = bin_eta_l - binsEta_.begin() - 1;
0103   }
0104 
0105   auto bin_phi_l = std::lower_bound(binsPhi_.begin(), binsPhi_.end(), phi);
0106   unsigned int bin_phi = 0;
0107   if (bin_phi_l == binsPhi_.end()) {
0108     if (phi < minPhi_) {
0109       bin_phi = nBinsPhi_;
0110     } else if (phi >= maxPhi_) {
0111       bin_phi = 0;
0112     } else {
0113       edm::LogError("HGCalTriggerTowerGeometryHelper")
0114           << " did not manage to map phi " << phi << " to any Trigger Tower\n";
0115     }
0116   } else {
0117     bin_phi = bin_phi_l - binsPhi_.begin() - 1;
0118   }
0119   int zside = eta < 0 ? -1 : 1;
0120   return l1t::HGCalTowerID(doNose_, zside, bin_eta, bin_phi).rawId();
0121 }
0122 
0123 unsigned short HGCalTriggerTowerGeometryHelper::getTriggerTower(const l1t::HGCalTriggerCell& thecell) const {
0124   unsigned int trigger_cell_id = thecell.detId();
0125   // NOTE: if the TC is not found in the map than it is mapped via eta-phi coords.
0126   // this can be considered dangerous (silent failure of the map) but it actually allows to save
0127   // memory mapping explicitly only what is actually needed
0128   auto tower_id_itr = cells_to_trigger_towers_.find(trigger_cell_id);
0129   if (tower_id_itr != cells_to_trigger_towers_.end())
0130     return tower_id_itr->second;
0131 
0132   return getTriggerTowerFromEtaPhi(thecell.position().eta(), thecell.position().phi());
0133 }
0134 
0135 unsigned short HGCalTriggerTowerGeometryHelper::getTriggerTower(const l1t::HGCalTriggerSums& thesum) const {
0136   return getTriggerTowerFromEtaPhi(thesum.position().eta(), thesum.position().phi());
0137 }