Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-30 23:47:24

0001 #include "Geometry/CaloTopology/interface/CaloTowerConstituentsMap.h"
0002 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0003 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 #include "FWCore/Utilities/interface/Exception.h"
0006 #include "Geometry/CaloTopology/interface/HcalTopology.h"
0007 #include "Geometry/CaloTopology/interface/CaloTowerTopology.h"
0008 
0009 #include <memory>
0010 
0011 //#define EDM_ML_DEBUG
0012 
0013 CaloTowerConstituentsMap::~CaloTowerConstituentsMap() {
0014   delete m_reverseItems.load();
0015   m_reverseItems = nullptr;
0016 }
0017 CaloTowerConstituentsMap::CaloTowerConstituentsMap(const HcalTopology* hcaltopo, const CaloTowerTopology* cttopo)
0018     : m_hcaltopo(hcaltopo),
0019       m_cttopo(cttopo),
0020       standardHB_(false),
0021       standardHE_(false),
0022       standardHF_(false),
0023       standardHO_(false),
0024       standardEB_(false),
0025       m_reverseItems(nullptr) {}
0026 
0027 CaloTowerDetId CaloTowerConstituentsMap::towerOf(const DetId& id) const {
0028   CaloTowerDetId tid;  // null to start with
0029 
0030   edm::SortedCollection<MapItem>::const_iterator i = m_items.find(id);
0031   if (i != m_items.end())
0032     tid = i->tower;
0033 
0034   //use hcaltopo when dealing with hcal detids
0035   if (tid.null()) {
0036     if (id.det() == DetId::Hcal) {
0037       HcalDetId hid(id);
0038       if ((hid.subdet() == HcalBarrel && standardHB_) || (hid.subdet() == HcalEndcap && standardHE_) ||
0039           (hid.subdet() == HcalOuter && standardHO_) || (hid.subdet() == HcalForward && standardHF_)) {
0040         tid = CaloTowerDetId(m_cttopo->convertHcaltoCT(hid.ietaAbs(), hid.subdet()) * hid.zside(), hid.iphi());
0041       }
0042     } else if (id.det() == DetId::Ecal) {
0043       EcalSubdetector esd = (EcalSubdetector)id.subdetId();
0044       if (esd == EcalBarrel && standardEB_) {
0045         EBDetId ebid(id);
0046         tid = CaloTowerDetId(ebid.tower_ieta(), ebid.tower_iphi());
0047       }
0048     }
0049   }
0050 
0051   return tid;
0052 }
0053 
0054 void CaloTowerConstituentsMap::assign(const DetId& cell, const CaloTowerDetId& tower) {
0055   if (m_items.find(cell) != m_items.end()) {
0056     throw cms::Exception("CaloTowers") << "Cell with id " << std::hex << cell.rawId() << std::dec
0057                                        << " is already mapped to a CaloTower " << m_items.find(cell)->tower
0058                                        << std::endl;
0059   }
0060   m_items.emplace_back(MapItem(cell, tower));
0061 }
0062 
0063 void CaloTowerConstituentsMap::sort() {
0064   m_items.sort();
0065 
0066   //  for (auto const & it : m_items)
0067   //    edm::LogVerbatim("CaloTower") << std::hex << it.cell.rawId() << " " << it.tower.rawId() << std::dec;
0068 }
0069 
0070 std::vector<DetId> CaloTowerConstituentsMap::constituentsOf(const CaloTowerDetId& id) const {
0071 #ifdef EDM_ML_DEBUG
0072   edm::LogVerbatim("CaloTower") << "Get constituent of " << std::hex << id.rawId() << std::dec << " ID " << id
0073                                 << " ieta " << id.ieta();
0074 #endif
0075   std::vector<DetId> items;
0076   if (id.ieta() == 0)
0077     return items;
0078 
0079   // build reverse map if needed
0080   if (!m_reverseItems.load(std::memory_order_acquire)) {
0081     std::unique_ptr<std::multimap<CaloTowerDetId, DetId>> ptr{new std::multimap<CaloTowerDetId, DetId>};
0082     for (auto m_item : m_items)
0083       ptr->insert(std::pair<CaloTowerDetId, DetId>(m_item.tower, m_item.cell));
0084     std::multimap<CaloTowerDetId, DetId>* expected = nullptr;
0085     if (m_reverseItems.compare_exchange_strong(expected, ptr.get(), std::memory_order_acq_rel)) {
0086       ptr.release();
0087     }
0088   }
0089 
0090   /// copy from the items map
0091   std::multimap<CaloTowerDetId, DetId>::const_iterator j;
0092   auto range = (*m_reverseItems.load(std::memory_order_acquire)).equal_range(id);
0093   for (j = range.first; j != range.second; j++)
0094     items.emplace_back(j->second);
0095 
0096   // dealing with topo dependency...
0097   //use cttopo when dealing with calotower detids
0098   int nd, sd;
0099   int hcal_ieta = m_cttopo->convertCTtoHcal(id.ietaAbs());
0100 
0101   if (standardHB_) {
0102     if (id.ietaAbs() <= m_cttopo->lastHBRing()) {
0103       m_hcaltopo->depthBinInformation(HcalBarrel, hcal_ieta, id.iphi(), id.zside(), nd, sd);
0104       for (int i = 0; i < nd; i++) {
0105         if (m_hcaltopo->getMergePositionFlag()) {
0106           HcalDetId hid =
0107               m_hcaltopo->mergedDepthDetId(HcalDetId(HcalBarrel, hcal_ieta * id.zside(), id.iphi(), i + sd));
0108           if (std::find(items.begin(), items.end(), hid) == items.end()) {
0109             items.emplace_back(hid);
0110 #ifdef EDM_ML_DEBUG
0111             edm::LogVerbatim("CaloTower") << id << " Depth " << i << ":" << i + sd << " " << hid;
0112 #endif
0113           }
0114         } else {
0115           HcalDetId hid(HcalBarrel, hcal_ieta * id.zside(), id.iphi(), i + sd);
0116           items.emplace_back(hid);
0117 #ifdef EDM_ML_DEBUG
0118           edm::LogVerbatim("CaloTower") << id << " Depth " << i << ":" << i + sd << " " << hid;
0119 #endif
0120         }
0121       }
0122     }
0123   }
0124   if (standardHO_) {
0125     if (id.ietaAbs() <= m_cttopo->lastHORing()) {
0126       m_hcaltopo->depthBinInformation(HcalOuter, hcal_ieta, id.iphi(), id.zside(), nd, sd);
0127       for (int i = 0; i < nd; i++) {
0128         HcalDetId hid(HcalOuter, hcal_ieta * id.zside(), id.iphi(), i + sd);
0129         items.emplace_back(hid);
0130 #ifdef EDM_ML_DEBUG
0131         edm::LogVerbatim("CaloTower") << id << " Depth " << i << ":" << i + sd << " " << hid;
0132 #endif
0133       }
0134     }
0135   }
0136   if (standardHE_) {
0137     if (id.ietaAbs() >= m_cttopo->firstHERing() && id.ietaAbs() <= m_cttopo->lastHERing()) {
0138       m_hcaltopo->depthBinInformation(HcalEndcap, hcal_ieta, id.iphi(), id.zside(), nd, sd);
0139       for (int i = 0; i < nd; i++) {
0140         if (m_hcaltopo->getMergePositionFlag()) {
0141           HcalDetId hid =
0142               m_hcaltopo->mergedDepthDetId(HcalDetId(HcalEndcap, hcal_ieta * id.zside(), id.iphi(), i + sd));
0143           if (std::find(items.begin(), items.end(), hid) == items.end()) {
0144             items.emplace_back(hid);
0145 #ifdef EDM_ML_DEBUG
0146             edm::LogVerbatim("CaloTower") << id << " Depth " << i << ":" << i + sd << " " << hid;
0147 #endif
0148           }
0149         } else {
0150           HcalDetId hid(HcalEndcap, hcal_ieta * id.zside(), id.iphi(), i + sd);
0151           items.emplace_back(hid);
0152 #ifdef EDM_ML_DEBUG
0153           edm::LogVerbatim("CaloTower") << id << " Depth " << i << ":" << i + sd << " " << hid;
0154 #endif
0155         }
0156       }
0157     }
0158   }
0159   if (standardHF_) {
0160     if (id.ietaAbs() >= m_cttopo->firstHFRing() && id.ietaAbs() <= m_cttopo->lastHFRing()) {
0161       m_hcaltopo->depthBinInformation(HcalForward, hcal_ieta, id.iphi(), id.zside(), nd, sd);
0162       for (int i = 0; i < nd; i++) {
0163         HcalDetId hid(HcalForward, hcal_ieta * id.zside(), id.iphi(), i + sd);
0164         items.emplace_back(hid);
0165 #ifdef EDM_ML_DEBUG
0166         edm::LogVerbatim("CaloTower") << id << " Depth " << i << ":" << i + sd << " " << hid;
0167 #endif
0168       }
0169       // special handling for first HF tower
0170       if (id.ietaAbs() == m_cttopo->firstHFRing()) {
0171         int hcal_ieta2 = hcal_ieta - 1;
0172         m_hcaltopo->depthBinInformation(HcalForward, hcal_ieta2, id.iphi(), id.zside(), nd, sd);
0173         for (int i = 0; i < nd; i++) {
0174           HcalDetId hid(HcalForward, hcal_ieta2 * id.zside(), id.iphi(), i + sd);
0175           items.emplace_back(hid);
0176 #ifdef EDM_ML_DEBUG
0177           edm::LogVerbatim("CaloTower") << id << " Depth " << i << ":" << i + sd << " " << hid;
0178 #endif
0179         }
0180       }
0181     }
0182   }
0183   if (standardEB_ && hcal_ieta <= EBDetId::MAX_IETA / 5) {
0184     HcalDetId hid(HcalBarrel, hcal_ieta * id.zside(), id.iphi(), 1);  // for the limits
0185     int etaMin, etaMax;
0186     if (hid.zside() == -1) {
0187       etaMin = hid.crystal_ieta_high();
0188       etaMax = hid.crystal_ieta_low();
0189     } else {
0190       etaMin = hid.crystal_ieta_low();
0191       etaMax = hid.crystal_ieta_high();
0192     }
0193     for (int ie = etaMin; ie <= etaMax; ie++)
0194       for (int ip = hid.crystal_iphi_low(); ip <= hid.crystal_iphi_high(); ip++)
0195         items.emplace_back(EBDetId(ie, ip));
0196   }
0197   return items;
0198 }
0199 
0200 void CaloTowerConstituentsMap::useStandardHB(bool use) { standardHB_ = use; }
0201 void CaloTowerConstituentsMap::useStandardHE(bool use) { standardHE_ = use; }
0202 void CaloTowerConstituentsMap::useStandardHO(bool use) { standardHO_ = use; }
0203 void CaloTowerConstituentsMap::useStandardHF(bool use) { standardHF_ = use; }
0204 void CaloTowerConstituentsMap::useStandardEB(bool use) { standardEB_ = use; }