Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:48:54

0001 #include <typeinfo>
0002 
0003 #include "Geometry/MTDGeometryBuilder/interface/MTDGeometry.h"
0004 #include "Geometry/MTDNumberingBuilder/interface/GeometricTimingDet.h"
0005 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0006 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 
0009 #include "DataFormats/ForwardDetId/interface/MTDDetId.h"
0010 #include "DataFormats/ForwardDetId/interface/BTLDetId.h"
0011 #include "DataFormats/ForwardDetId/interface/ETLDetId.h"
0012 
0013 #include "DataFormats/DetId/interface/DetId.h"
0014 #include "FWCore/Utilities/interface/Exception.h"
0015 
0016 #include <algorithm>
0017 #include <iostream>
0018 #include <map>
0019 
0020 //#define EDM_ML_DEBUG
0021 
0022 namespace {
0023   GeomDetEnumerators::SubDetector geometricDetToGeomDet(GeometricTimingDet::GTDEnumType gdenum) {
0024     // provide a map between the GeometricTimingDet enumerators and the GeomDet enumerators of the possible tracker subdetectors
0025     if (gdenum == GeometricTimingDet::GTDEnumType::BTL)
0026       return GeomDetEnumerators::SubDetector::TimingBarrel;
0027     if (gdenum == GeometricTimingDet::GTDEnumType::ETL)
0028       return GeomDetEnumerators::SubDetector::TimingEndcap;
0029     return GeomDetEnumerators::SubDetector::invalidDet;
0030   }
0031 
0032   class DetIdComparator {
0033   public:
0034     bool operator()(GeometricTimingDet const* gd1, GeometricTimingDet const* gd2) const {
0035       uint32_t det1 = gd1->geographicalId();
0036       uint32_t det2 = gd2->geographicalId();
0037       return det1 < det2;
0038     }
0039   };
0040 }  // namespace
0041 
0042 MTDGeometry::MTDGeometry(GeometricTimingDet const* gd) : theTrackerDet(gd) {
0043   for (unsigned int i = 0; i < 2; ++i) {
0044     theSubDetTypeMap[i] = GeomDetEnumerators::invalidDet;
0045     theNumberOfLayers[i] = 0;
0046   }
0047   GeometricTimingDet::ConstGeometricTimingDetContainer subdetgd = gd->components();
0048 
0049 #ifdef EDM_ML_DEBUG
0050   edm::LogInfo("BuildingSubDetTypeMap")
0051       << "MTD GeometricTimingDet and GeomDetEnumerators enumerator values of the subdetectors" << std::endl;
0052 #endif
0053   for (unsigned int i = 0; i < subdetgd.size(); ++i) {
0054     MTDDetId mtdid(subdetgd[i]->geographicalId());
0055     assert(mtdid.mtdSubDetector() > 0 && mtdid.mtdSubDetector() < 3);
0056     theSubDetTypeMap[mtdid.mtdSubDetector() - 1] = geometricDetToGeomDet(subdetgd[i]->type());
0057     theNumberOfLayers[mtdid.mtdSubDetector() - 1] = subdetgd[i]->components().size();
0058 #ifdef EDM_ML_DEBUG
0059     LogTrace("BuildingSubDetTypeMap") << "MTD subdet " << i << " Geometric Det type " << subdetgd[i]->type()
0060                                       << " Geom Det type " << theSubDetTypeMap[mtdid.mtdSubDetector() - 1] << " detid "
0061                                       << std::hex << subdetgd[i]->geographicalId().rawId() << std::dec << " subdetid "
0062                                       << mtdid.mtdSubDetector() << " number of layers "
0063                                       << subdetgd[i]->components().size();
0064 #endif
0065   }
0066 #ifdef EDM_ML_DEBUG
0067   edm::LogInfo("SubDetTypeMapContent").log([&](auto& debugstr) {
0068     debugstr << "MTD Content of theSubDetTypeMap"
0069              << "\n";
0070     for (unsigned int i = 1; i <= 2; ++i) {
0071       debugstr << " detid subdet " << i << " Geom Det type " << geomDetSubDetector(i) << "\n";
0072     }
0073   });
0074   edm::LogInfo("NumberOfLayers").log([&](auto& debugstr) {
0075     debugstr << "MTD Content of theNumberOfLayers"
0076              << "\n";
0077     for (unsigned int i = 1; i <= 2; ++i) {
0078       debugstr << " detid subdet " << i << " number of layers " << numberOfLayers(i) << "\n";
0079     }
0080   });
0081 #endif
0082   std::vector<const GeometricTimingDet*> deepcomp;
0083   gd->deepComponents(deepcomp);
0084 
0085   sort(deepcomp.begin(), deepcomp.end(), DetIdComparator());
0086 
0087 #ifdef EDM_ML_DEBUG
0088   edm::LogInfo("ThicknessAndType") << "MTD Total Number of Detectors " << deepcomp.size() << std::endl;
0089   edm::LogInfo("ThicknessAndType") << "MTD Dump of sensors names and bounds" << std::endl;
0090   edm::LogInfo("ThicknessAndType").log([&](auto& debugstr) {
0091     for (const auto& det : deepcomp) {
0092       fillTestMap(det);
0093       debugstr << std::hex << det->geographicalId().rawId() << std::dec << " " << det->name() << " "
0094                << det->bounds()->thickness() << "\n";
0095     }
0096   });
0097   edm::LogInfo("DetTypeList").log([&](auto& debugstr) {
0098     debugstr << "MTD Content of DetTypeList : size " << theDetTypetList.size() << "\n";
0099     for (const auto& iVal : theDetTypetList) {
0100       debugstr << " DetId " << std::get<0>(iVal).rawId() << " Type "
0101                << static_cast<std::underlying_type<MTDGeometry::ModuleType>::type>(std::get<1>(iVal)) << " Thickness "
0102                << std::get<2>(iVal) << "\n";
0103     }
0104   });
0105 #endif
0106 }
0107 
0108 MTDGeometry::~MTDGeometry() {
0109   for (auto d : theDets)
0110     delete const_cast<GeomDet*>(d);
0111   for (auto d : theDetTypes)
0112     delete const_cast<GeomDetType*>(d);
0113 }
0114 
0115 void MTDGeometry::finalize() {
0116   theDetTypes.shrink_to_fit();  // owns the DetTypes
0117   theDetUnits.shrink_to_fit();  // they're all also into 'theDets', so we assume 'theDets' owns them
0118   theDets.shrink_to_fit();      // owns *ONLY* the GeomDet * corresponding to GluedDets.
0119   theDetUnitIds.shrink_to_fit();
0120   theDetIds.shrink_to_fit();
0121 
0122   theBTLDets.shrink_to_fit();  // not owned: they're also in 'theDets'
0123   theETLDets.shrink_to_fit();  // not owned: they're also in 'theDets'
0124 }
0125 
0126 void MTDGeometry::addType(GeomDetType const* p) {
0127   theDetTypes.emplace_back(p);  // add to vector
0128 }
0129 
0130 void MTDGeometry::addDetUnit(GeomDet const* p) {
0131   // set index
0132   const_cast<GeomDet*>(p)->setIndex(theDetUnits.size());
0133   theDetUnits.emplace_back(p);  // add to vector
0134   theMapUnit.emplace(p->geographicalId().rawId(), p);
0135 }
0136 
0137 void MTDGeometry::addDetUnitId(DetId p) { theDetUnitIds.emplace_back(p); }
0138 
0139 void MTDGeometry::addDet(GeomDet const* p) {
0140   // set index
0141   const_cast<GeomDet*>(p)->setGdetIndex(theDets.size());
0142   theDets.emplace_back(p);  // add to vector
0143   theMap.insert(std::make_pair(p->geographicalId().rawId(), p));
0144   MTDDetId id(p->geographicalId());
0145   switch (id.mtdSubDetector()) {
0146     case MTDDetId::BTL:
0147       theBTLDets.emplace_back(p);
0148       break;
0149     case MTDDetId::ETL:
0150       theETLDets.emplace_back(p);
0151       break;
0152     default:
0153       edm::LogError("MTDGeometry") << "ERROR - I was expecting a MTD Subdetector, I got a " << id.mtdSubDetector();
0154   }
0155 }
0156 
0157 void MTDGeometry::addDetId(DetId p) { theDetIds.emplace_back(p); }
0158 
0159 const MTDGeometry::DetContainer& MTDGeometry::detsBTL() const { return theBTLDets; }
0160 
0161 const MTDGeometry::DetContainer& MTDGeometry::detsETL() const { return theETLDets; }
0162 
0163 const MTDGeomDet* MTDGeometry::idToDetUnit(DetId s) const {
0164   mapIdToDetUnit::const_iterator p = theMapUnit.find(s.rawId());
0165   if (p != theMapUnit.end()) {
0166     return static_cast<const MTDGeomDet*>(p->second);
0167   }
0168   return nullptr;
0169 }
0170 
0171 const MTDGeomDet* MTDGeometry::idToDet(DetId s) const {
0172   mapIdToDet::const_iterator p = theMap.find(s.rawId());
0173   if (p != theMap.end()) {
0174     return static_cast<const MTDGeomDet*>(p->second);
0175   }
0176   return nullptr;
0177 }
0178 
0179 const GeomDetEnumerators::SubDetector MTDGeometry::geomDetSubDetector(int subdet) const {
0180   if (subdet >= 1 && subdet <= 2) {
0181     return theSubDetTypeMap[subdet - 1];
0182   } else {
0183     throw cms::Exception("WrongTrackerSubDet") << "Subdetector " << subdet;
0184   }
0185 }
0186 
0187 unsigned int MTDGeometry::numberOfLayers(int subdet) const {
0188   if (subdet >= 1 && subdet <= 2) {
0189     return theNumberOfLayers[subdet - 1];
0190   } else {
0191     throw cms::Exception("WrongTrackerSubDet") << "Subdetector " << subdet;
0192   }
0193 }
0194 
0195 bool MTDGeometry::isThere(GeomDetEnumerators::SubDetector subdet) const {
0196   for (unsigned int i = 1; i <= 2; ++i) {
0197     if (subdet == geomDetSubDetector(i))
0198       return true;
0199   }
0200   return false;
0201 }
0202 
0203 void MTDGeometry::fillTestMap(const GeometricTimingDet* gd) {
0204   const std::string& temp = gd->name();
0205   std::string name = temp.substr(temp.find(':') + 1);
0206   DetId detid = gd->geographicalId();
0207   float thickness = gd->bounds()->thickness();
0208   std::string nameTag;
0209   MTDGeometry::ModuleType mtype = moduleType(name);
0210   if (theDetTypetList.empty()) {
0211     theDetTypetList.emplace_back(detid, mtype, thickness);
0212   } else {
0213     auto& t = (*(theDetTypetList.end() - 1));
0214     if (std::get<1>(t) != mtype)
0215       theDetTypetList.emplace_back(detid, mtype, thickness);
0216     else {
0217       if (detid > std::get<0>(t))
0218         std::get<0>(t) = detid;
0219     }
0220   }
0221 }
0222 
0223 MTDGeometry::ModuleType MTDGeometry::getDetectorType(DetId detid) const {
0224   for (const auto& iVal : theDetTypetList) {
0225     DetId detid_max = std::get<0>(iVal);
0226     MTDGeometry::ModuleType mtype = std::get<1>(iVal);
0227     if (detid.rawId() <= detid_max.rawId())
0228       return mtype;
0229   }
0230   return MTDGeometry::ModuleType::UNKNOWN;
0231 }
0232 
0233 float MTDGeometry::getDetectorThickness(DetId detid) const {
0234   for (const auto& iVal : theDetTypetList) {
0235     DetId detid_max = std::get<0>(iVal);
0236     if (detid.rawId() <= detid_max.rawId())
0237       return std::get<2>(iVal);
0238   }
0239   return -1.0;
0240 }
0241 
0242 MTDGeometry::ModuleType MTDGeometry::moduleType(const std::string& name) const {
0243   if (name.find("Timing") != std::string::npos) {
0244     if (name.find("BModule") != std::string::npos)
0245       return ModuleType::BTL;
0246     else if (name.find("EModule") != std::string::npos)
0247       return ModuleType::ETL;
0248   }
0249   return ModuleType::UNKNOWN;
0250 }