Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:49:07

0001 #include <typeinfo>
0002 
0003 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0004 #include "Geometry/TrackerNumberingBuilder/interface/GeometricDet.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/SiStripDetId/interface/StripSubdetector.h"
0010 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0011 #include "DataFormats/DetId/interface/DetId.h"
0012 #include "FWCore/Utilities/interface/Exception.h"
0013 
0014 #include <algorithm>
0015 #include <iostream>
0016 #include <map>
0017 
0018 namespace {
0019   GeomDetEnumerators::SubDetector geometricDetToGeomDet(GeometricDet::GDEnumType gdenum) {
0020     // provide a map between the GeometricDet enumerators and the GeomDet enumerators of the possible tracker subdetectors
0021     if (gdenum == GeometricDet::GDEnumType::PixelBarrel)
0022       return GeomDetEnumerators::SubDetector::PixelBarrel;
0023     if (gdenum == GeometricDet::GDEnumType::PixelEndCap)
0024       return GeomDetEnumerators::SubDetector::PixelEndcap;
0025     if (gdenum == GeometricDet::GDEnumType::TIB)
0026       return GeomDetEnumerators::SubDetector::TIB;
0027     if (gdenum == GeometricDet::GDEnumType::TID)
0028       return GeomDetEnumerators::SubDetector::TID;
0029     if (gdenum == GeometricDet::GDEnumType::TOB)
0030       return GeomDetEnumerators::SubDetector::TOB;
0031     if (gdenum == GeometricDet::GDEnumType::TEC)
0032       return GeomDetEnumerators::SubDetector::TEC;
0033     if (gdenum == GeometricDet::GDEnumType::PixelPhase1Barrel)
0034       return GeomDetEnumerators::SubDetector::P1PXB;
0035     if (gdenum == GeometricDet::GDEnumType::PixelPhase1EndCap)
0036       return GeomDetEnumerators::SubDetector::P1PXEC;
0037     if (gdenum == GeometricDet::GDEnumType::PixelPhase2Barrel)
0038       return GeomDetEnumerators::SubDetector::P2PXB;
0039     if (gdenum == GeometricDet::GDEnumType::PixelPhase2EndCap)
0040       return GeomDetEnumerators::SubDetector::P2PXEC;
0041     if (gdenum == GeometricDet::GDEnumType::OTPhase2Barrel)
0042       return GeomDetEnumerators::SubDetector::P2OTB;
0043     if (gdenum == GeometricDet::GDEnumType::OTPhase2EndCap)
0044       return GeomDetEnumerators::SubDetector::P2OTEC;
0045     return GeomDetEnumerators::SubDetector::invalidDet;
0046   }
0047 
0048   class DetIdComparator {
0049   public:
0050     bool operator()(GeometricDet const* gd1, GeometricDet const* gd2) const {
0051       uint32_t det1 = gd1->geographicalId();
0052       uint32_t det2 = gd2->geographicalId();
0053       return det1 < det2;
0054     }
0055   };
0056 }  // namespace
0057 
0058 TrackerGeometry::TrackerGeometry(GeometricDet const* gd) : theTrackerDet(gd) {
0059   for (unsigned int i = 0; i < 6; ++i) {
0060     theSubDetTypeMap[i] = GeomDetEnumerators::invalidDet;
0061     theNumberOfLayers[i] = 0;
0062   }
0063   GeometricDet::ConstGeometricDetContainer subdetgd = gd->components();
0064 
0065   LogDebug("BuildingSubDetTypeMap") << "GeometriDet and GeomDetEnumerators enumerator values of the subdetectors";
0066   for (unsigned int i = 0; i < subdetgd.size(); ++i) {
0067     assert(subdetgd[i]->geographicalId().subdetId() > 0 && subdetgd[i]->geographicalId().subdetId() < 7);
0068     theSubDetTypeMap[subdetgd[i]->geographicalId().subdetId() - 1] = geometricDetToGeomDet(subdetgd[i]->type());
0069     theNumberOfLayers[subdetgd[i]->geographicalId().subdetId() - 1] = subdetgd[i]->components().size();
0070     LogTrace("BuildingSubDetTypeMap") << "subdet " << i << " Geometric Det type " << subdetgd[i]->type()
0071                                       << " Geom Det type "
0072                                       << theSubDetTypeMap[subdetgd[i]->geographicalId().subdetId() - 1] << " detid "
0073                                       << subdetgd[i]->geographicalId() << " subdetid "
0074                                       << subdetgd[i]->geographicalId().subdetId() << " number of layers "
0075                                       << subdetgd[i]->components().size();
0076   }
0077   LogDebug("SubDetTypeMapContent") << "Content of theSubDetTypeMap";
0078   for (unsigned int i = 1; i < 7; ++i) {
0079     LogTrace("SubDetTypeMapContent") << " detid subdet " << i << " Geom Det type " << geomDetSubDetector(i);
0080   }
0081   LogDebug("NumberOfLayers") << "Content of theNumberOfLayers";
0082   for (unsigned int i = 1; i < 7; ++i) {
0083     LogTrace("NumberOfLayers") << " detid subdet " << i << " number of layers " << numberOfLayers(i);
0084   }
0085   std::vector<const GeometricDet*> deepcomp;
0086   gd->deepComponents(deepcomp);
0087 
0088   sort(deepcomp.begin(), deepcomp.end(), DetIdComparator());
0089 
0090   LogDebug("ThicknessAndType") << " Total Number of Detectors " << deepcomp.size();
0091   LogDebug("ThicknessAndType") << "Dump of sensors names and bounds";
0092   for (auto det : deepcomp) {
0093     fillTestMap(det);
0094     LogDebug("ThicknessAndType") << det->geographicalId() << " " << det->name() << " " << det->bounds()->thickness();
0095   }
0096   LogDebug("DetTypeList") << " Content of DetTypetList : size " << theDetTypetList.size();
0097   for (const auto& iVal : theDetTypetList) {
0098     LogDebug("DetTypeList") << " DetId " << std::get<0>(iVal) << " Type "
0099                             << static_cast<std::underlying_type<TrackerGeometry::ModuleType>::type>(std::get<1>(iVal))
0100                             << " Thickness " << std::get<2>(iVal);
0101   }
0102 }
0103 
0104 TrackerGeometry::~TrackerGeometry() {
0105   for (auto d : theDets)
0106     delete const_cast<GeomDet*>(d);
0107   for (auto d : theDetTypes)
0108     delete const_cast<GeomDetType*>(d);
0109 }
0110 
0111 void TrackerGeometry::finalize() {
0112   theDetTypes.shrink_to_fit();  // owns the DetTypes
0113   theDetUnits.shrink_to_fit();  // they're all also into 'theDets', so we assume 'theDets' owns them
0114   theDets.shrink_to_fit();      // owns *ONLY* the GeomDet * corresponding to GluedDets.
0115   theDetUnitIds.shrink_to_fit();
0116   theDetIds.shrink_to_fit();
0117 
0118   thePXBDets.shrink_to_fit();  // not owned: they're also in 'theDets'
0119   thePXFDets.shrink_to_fit();  // not owned: they're also in 'theDets'
0120   theTIBDets.shrink_to_fit();  // not owned: they're also in 'theDets'
0121   theTIDDets.shrink_to_fit();  // not owned: they're also in 'theDets'
0122   theTOBDets.shrink_to_fit();  // not owned: they're also in 'theDets'
0123   theTECDets.shrink_to_fit();  // not owned: they're also in 'theDets'
0124 }
0125 
0126 void TrackerGeometry::addType(GeomDetType const* p) {
0127   theDetTypes.emplace_back(p);  // add to vector
0128 }
0129 
0130 void TrackerGeometry::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.insert(std::make_pair(p->geographicalId().rawId(), p));
0135 }
0136 
0137 void TrackerGeometry::addDetUnitId(DetId p) { theDetUnitIds.emplace_back(p); }
0138 
0139 void TrackerGeometry::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   DetId id(p->geographicalId());
0145   switch (id.subdetId()) {
0146     case PixelSubdetector::PixelBarrel:
0147       thePXBDets.emplace_back(p);
0148       break;
0149     case PixelSubdetector::PixelEndcap:
0150       thePXFDets.emplace_back(p);
0151       break;
0152     case StripSubdetector::TIB:
0153       theTIBDets.emplace_back(p);
0154       break;
0155     case StripSubdetector::TID:
0156       theTIDDets.emplace_back(p);
0157       break;
0158     case StripSubdetector::TOB:
0159       theTOBDets.emplace_back(p);
0160       break;
0161     case StripSubdetector::TEC:
0162       theTECDets.emplace_back(p);
0163       break;
0164     default:
0165       edm::LogError("TrackerGeometry") << "ERROR - I was expecting a Tracker Subdetector, I got a " << id.subdetId();
0166   }
0167 }
0168 
0169 void TrackerGeometry::addDetId(DetId p) { theDetIds.emplace_back(p); }
0170 
0171 const TrackerGeometry::DetContainer& TrackerGeometry::detsPXB() const { return thePXBDets; }
0172 
0173 const TrackerGeometry::DetContainer& TrackerGeometry::detsPXF() const { return thePXFDets; }
0174 
0175 const TrackerGeometry::DetContainer& TrackerGeometry::detsTIB() const { return theTIBDets; }
0176 
0177 const TrackerGeometry::DetContainer& TrackerGeometry::detsTID() const { return theTIDDets; }
0178 
0179 const TrackerGeometry::DetContainer& TrackerGeometry::detsTOB() const { return theTOBDets; }
0180 
0181 const TrackerGeometry::DetContainer& TrackerGeometry::detsTEC() const { return theTECDets; }
0182 
0183 const TrackerGeomDet* TrackerGeometry::idToDetUnit(DetId s) const {
0184   mapIdToDetUnit::const_iterator p = theMapUnit.find(s.rawId());
0185   if (p != theMapUnit.end()) {
0186     return static_cast<const TrackerGeomDet*>(p->second);
0187   } else {
0188     throw cms::Exception("WrongTrackerSubDet")
0189         << "Invalid DetID: no GeomDetUnit associated with raw ID " << s.rawId() << " of subdet ID " << s.subdetId();
0190   }
0191 }
0192 
0193 const TrackerGeomDet* TrackerGeometry::idToDet(DetId s) const {
0194   mapIdToDet::const_iterator p = theMap.find(s.rawId());
0195   if (p != theMap.end()) {
0196     return static_cast<const TrackerGeomDet*>(p->second);
0197   } else {
0198     throw cms::Exception("WrongTrackerSubDet")
0199         << "Invalid DetID: no GeomDetUnit associated with raw ID " << s.rawId() << " of subdet ID " << s.subdetId();
0200   }
0201 }
0202 
0203 const GeomDetEnumerators::SubDetector TrackerGeometry::geomDetSubDetector(int subdet) const {
0204   if (subdet >= 1 && subdet <= 6) {
0205     return theSubDetTypeMap[subdet - 1];
0206   } else {
0207     throw cms::Exception("WrongTrackerSubDet") << "Subdetector " << subdet;
0208   }
0209 }
0210 
0211 unsigned int TrackerGeometry::numberOfLayers(int subdet) const {
0212   if (subdet >= 1 && subdet <= 6) {
0213     return theNumberOfLayers[subdet - 1];
0214   } else {
0215     throw cms::Exception("WrongTrackerSubDet") << "Subdetector " << subdet;
0216   }
0217 }
0218 
0219 bool TrackerGeometry::isThere(GeomDetEnumerators::SubDetector subdet) const {
0220   for (unsigned int i = 1; i < 7; ++i) {
0221     if (subdet == geomDetSubDetector(i))
0222       return true;
0223   }
0224   return false;
0225 }
0226 
0227 void TrackerGeometry::fillTestMap(const GeometricDet* gd) {
0228   const std::string& temp = gd->name();
0229   std::string name = temp.substr(temp.find(':') + 1);
0230   DetId detid = gd->geographicalId();
0231   float thickness = gd->bounds()->thickness();
0232   std::string nameTag;
0233   TrackerGeometry::ModuleType mtype = moduleType(name);
0234   if (theDetTypetList.empty()) {
0235     theDetTypetList.emplace_back(detid, mtype, thickness);
0236   } else {
0237     auto& t = (*(theDetTypetList.end() - 1));
0238     if (std::get<1>(t) != mtype)
0239       theDetTypetList.emplace_back(detid, mtype, thickness);
0240     else {
0241       if (detid > std::get<0>(t))
0242         std::get<0>(t) = detid;
0243     }
0244   }
0245 }
0246 
0247 TrackerGeometry::ModuleType TrackerGeometry::getDetectorType(DetId detid) const {
0248   for (const auto& iVal : theDetTypetList) {
0249     DetId detid_max = std::get<0>(iVal);
0250     if (detid.rawId() <= detid_max.rawId())
0251       return std::get<1>(iVal);
0252   }
0253   return TrackerGeometry::ModuleType::UNKNOWN;
0254 }
0255 
0256 float TrackerGeometry::getDetectorThickness(DetId detid) const {
0257   for (const auto& iVal : theDetTypetList) {
0258     DetId detid_max = std::get<0>(iVal);
0259     if (detid.rawId() <= detid_max.rawId())
0260       return std::get<2>(iVal);
0261   }
0262   return -1.0;
0263 }
0264 
0265 TrackerGeometry::ModuleType TrackerGeometry::moduleType(const std::string& name) const {
0266   // IT
0267   if (name.find("Pixel") != std::string::npos) {
0268     // Phase 1
0269     if (name.find("BarrelActive") != std::string::npos)
0270       return ModuleType::Ph1PXB;
0271     else if (name.find("ForwardSensor") != std::string::npos)
0272       return ModuleType::Ph1PXF;
0273 
0274     // Phase 2
0275     // barrel
0276     else if (name.find("BModule") != std::string::npos) {
0277       if (name.find("InnerPixelActive") != std::string::npos) {
0278         return ModuleType::Ph2PXB;
0279       } else if (name.find("InnerPixel3DActive") != std::string::npos) {
0280         return ModuleType::Ph2PXB3D;
0281       }
0282     }
0283     // forward
0284     else if (name.find("EModule") != std::string::npos) {
0285       if (name.find("InnerPixelActive") != std::string::npos) {
0286         return ModuleType::Ph2PXF;
0287       } else if (name.find("InnerPixel3DActive") != std::string::npos) {
0288         return ModuleType::Ph2PXF3D;
0289       }
0290     }
0291   }
0292 
0293   // TIB
0294   else if (name.find("TIB") != std::string::npos) {
0295     if (name.find('0') != std::string::npos)
0296       return ModuleType::IB1;
0297     else
0298       return ModuleType::IB2;
0299   }
0300 
0301   // TOB
0302   else if (name.find("TOB") != std::string::npos) {
0303     if (name.find('0') != std::string::npos)
0304       return ModuleType::OB1;
0305     else
0306       return ModuleType::OB2;
0307   }
0308 
0309   // TID
0310   else if (name.find("TID") != std::string::npos) {
0311     if (name.find('0') != std::string::npos)
0312       return ModuleType::W1A;
0313     else if (name.find('1') != std::string::npos)
0314       return ModuleType::W2A;
0315     else if (name.find('2') != std::string::npos)
0316       return ModuleType::W3A;
0317   }
0318 
0319   // TEC
0320   else if (name.find("TEC") != std::string::npos) {
0321     if (name.find('0') != std::string::npos)
0322       return ModuleType::W1B;
0323     else if (name.find('1') != std::string::npos)
0324       return ModuleType::W2B;
0325     else if (name.find('2') != std::string::npos)
0326       return ModuleType::W3B;
0327     else if (name.find('3') != std::string::npos)
0328       return ModuleType::W4;
0329     else if (name.find('4') != std::string::npos)
0330       return ModuleType::W5;
0331     else if (name.find('5') != std::string::npos)
0332       return ModuleType::W6;
0333     else if (name.find('6') != std::string::npos)
0334       return ModuleType::W7;
0335   }
0336 
0337   // Phase 2 OT
0338   if (name.find("BModule") != std::string::npos || name.find("EModule") != std::string::npos) {
0339     if (name.find("PSMacroPixel") != std::string::npos)
0340       return ModuleType::Ph2PSP;
0341     else if (name.find("PSStrip") != std::string::npos)
0342       return ModuleType::Ph2PSS;
0343     else if (name.find("2S") != std::string::npos)
0344       return ModuleType::Ph2SS;
0345   }
0346 
0347   return ModuleType::UNKNOWN;
0348 }