Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:15:00

0001 #include "CalibTracker/StandaloneTrackerTopology/interface/StandaloneTrackerTopology.h"
0002 
0003 #include "tinyxml2.h"
0004 #include "FWCore/Utilities/interface/Exception.h"
0005 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
0006 
0007 namespace {
0008   // split into tokens and convert them to uint32_t
0009   inline std::vector<uint32_t> split_string_to_uints(const std::string& str) {
0010     std::vector<uint32_t> out{};
0011     std::size_t iStart{str.find_first_not_of(" ,\n")}, iEnd{};
0012     while (std::string::npos != iStart) {
0013       iEnd = str.find_first_of(" ,\n", iStart);
0014       out.push_back(std::stoul(str.substr(iStart, iEnd), nullptr, 0));
0015       iStart = str.find_first_not_of(" ,\n", iEnd);
0016     }
0017     return out;
0018   }
0019 
0020   class TrackerTopologyExtractor : public tinyxml2::XMLVisitor {
0021   public:
0022     bool VisitEnter(const tinyxml2::XMLElement& elem, const tinyxml2::XMLAttribute*) override {
0023       if (std::strcmp(elem.Value(), "Vector") == 0) {
0024         const std::string att_type{elem.Attribute("type")};
0025         if (att_type == "numeric") {
0026           const std::string att_name{elem.Attribute("name")};
0027           if (0 == att_name.compare(0, SubdetName.size(), SubdetName)) {  // starts with
0028             const std::string att_nEntries{elem.Attribute("nEntries")};
0029             const std::size_t nEntries = att_nEntries.empty() ? 0 : std::stoul(att_nEntries);
0030             const auto vals = split_string_to_uints(elem.GetText());
0031 
0032             if (nEntries != vals.size()) {
0033               throw cms::Exception(
0034                   "StandaloneTrackerTopology",
0035                   ("Problem parsing element with name '" + att_name + "': " + "'nEntries' attribute claims " +
0036                    std::to_string(nEntries) + " elements, but parsed " + std::to_string(vals.size())));
0037             }
0038             const auto subDet = std::stoi(att_name.substr(SubdetName.size()));
0039             switch (subDet) {
0040               case PixelSubdetector::PixelBarrel:  // layer, ladder module
0041                 pxbVals_.layerStartBit_ = vals[0];
0042                 pxbVals_.ladderStartBit_ = vals[1];
0043                 pxbVals_.moduleStartBit_ = vals[2];
0044 
0045                 pxbVals_.layerMask_ = vals[3];
0046                 pxbVals_.ladderMask_ = vals[4];
0047                 pxbVals_.moduleMask_ = vals[5];
0048 
0049                 foundPXB = true;
0050                 break;
0051 
0052               case PixelSubdetector::PixelEndcap:  // side, disk, blade, panel, module
0053                 pxfVals_.sideStartBit_ = vals[0];
0054                 pxfVals_.diskStartBit_ = vals[1];
0055                 pxfVals_.bladeStartBit_ = vals[2];
0056                 pxfVals_.panelStartBit_ = vals[3];
0057                 pxfVals_.moduleStartBit_ = vals[4];
0058 
0059                 pxfVals_.sideMask_ = vals[5];
0060                 pxfVals_.diskMask_ = vals[6];
0061                 pxfVals_.bladeMask_ = vals[7];
0062                 pxfVals_.panelMask_ = vals[8];
0063                 pxfVals_.moduleMask_ = vals[9];
0064 
0065                 foundPXF = true;
0066                 break;
0067 
0068               case StripSubdetector::TIB:  // layer, str_fw_bw, str_int_ext, str, module, ster
0069                 tibVals_.layerStartBit_ = vals[0];
0070                 tibVals_.str_fw_bwStartBit_ = vals[1];
0071                 tibVals_.str_int_extStartBit_ = vals[2];
0072                 tibVals_.strStartBit_ = vals[3];
0073                 tibVals_.moduleStartBit_ = vals[4];
0074                 tibVals_.sterStartBit_ = vals[5];
0075 
0076                 tibVals_.layerMask_ = vals[6];
0077                 tibVals_.str_fw_bwMask_ = vals[7];
0078                 tibVals_.str_int_extMask_ = vals[8];
0079                 tibVals_.strMask_ = vals[9];
0080                 tibVals_.moduleMask_ = vals[10];
0081                 tibVals_.sterMask_ = vals[11];
0082 
0083                 foundTIB = true;
0084                 break;
0085 
0086               case StripSubdetector::TID:  // side, wheel, ring, module_fw_bw, module, ster
0087                 tidVals_.sideStartBit_ = vals[0];
0088                 tidVals_.wheelStartBit_ = vals[1];
0089                 tidVals_.ringStartBit_ = vals[2];
0090                 tidVals_.module_fw_bwStartBit_ = vals[3];
0091                 tidVals_.moduleStartBit_ = vals[4];
0092                 tidVals_.sterStartBit_ = vals[5];
0093 
0094                 tidVals_.sideMask_ = vals[6];
0095                 tidVals_.wheelMask_ = vals[7];
0096                 tidVals_.ringMask_ = vals[8];
0097                 tidVals_.module_fw_bwMask_ = vals[9];
0098                 tidVals_.moduleMask_ = vals[10];
0099                 tidVals_.sterMask_ = vals[11];
0100 
0101                 foundTID = true;
0102                 break;
0103 
0104               case StripSubdetector::TOB:  // layer, rod_fw_bw, rod, module, ster
0105                 tobVals_.layerStartBit_ = vals[0];
0106                 tobVals_.rod_fw_bwStartBit_ = vals[1];
0107                 tobVals_.rodStartBit_ = vals[2];
0108                 tobVals_.moduleStartBit_ = vals[3];
0109                 tobVals_.sterStartBit_ = vals[4];
0110 
0111                 tobVals_.layerMask_ = vals[5];
0112                 tobVals_.rod_fw_bwMask_ = vals[6];
0113                 tobVals_.rodMask_ = vals[7];
0114                 tobVals_.moduleMask_ = vals[8];
0115                 tobVals_.sterMask_ = vals[9];
0116 
0117                 foundTOB = true;
0118                 break;
0119 
0120               case StripSubdetector::TEC:  // side, wheel, petal_fw_bw, petal, ring, module, ster
0121                 tecVals_.sideStartBit_ = vals[0];
0122                 tecVals_.wheelStartBit_ = vals[1];
0123                 tecVals_.petal_fw_bwStartBit_ = vals[2];
0124                 tecVals_.petalStartBit_ = vals[3];
0125                 tecVals_.ringStartBit_ = vals[4];
0126                 tecVals_.moduleStartBit_ = vals[5];
0127                 tecVals_.sterStartBit_ = vals[6];
0128 
0129                 tecVals_.sideMask_ = vals[7];
0130                 tecVals_.wheelMask_ = vals[8];
0131                 tecVals_.petal_fw_bwMask_ = vals[9];
0132                 tecVals_.petalMask_ = vals[10];
0133                 tecVals_.ringMask_ = vals[11];
0134                 tecVals_.moduleMask_ = vals[12];
0135                 tecVals_.sterMask_ = vals[13];
0136 
0137                 foundTEC = true;
0138                 break;
0139             }
0140           }
0141         }
0142       }
0143       return true;
0144     }
0145 
0146     TrackerTopology getTrackerTopology() const {
0147       if (!(foundPXB && foundPXF && foundTIB && foundTID && foundTOB && foundTEC)) {
0148         throw cms::Exception("StandaloneTrackerTopology", "Could not find parameters for all tracker subdetectors");
0149       }
0150       return TrackerTopology(pxbVals_, pxfVals_, tecVals_, tibVals_, tidVals_, tobVals_);
0151     }
0152 
0153   private:
0154     TrackerTopology::PixelBarrelValues pxbVals_;
0155     TrackerTopology::PixelEndcapValues pxfVals_;
0156     TrackerTopology::TIBValues tibVals_;
0157     TrackerTopology::TIDValues tidVals_;
0158     TrackerTopology::TOBValues tobVals_;
0159     TrackerTopology::TECValues tecVals_;
0160 
0161     bool foundPXB = false, foundPXF = false, foundTIB = false, foundTID = false, foundTOB = false, foundTEC = false;
0162 
0163     const std::string SubdetName = "Subdetector";
0164   };
0165 }  // namespace
0166 
0167 namespace StandaloneTrackerTopology {
0168   TrackerTopology fromTrackerParametersXMLFile(const std::string& xmlFileName) {
0169     tinyxml2::XMLDocument xmlDoc;
0170     xmlDoc.LoadFile(xmlFileName.c_str());
0171     if (!xmlDoc.Error()) {
0172       TrackerTopologyExtractor extr{};
0173       xmlDoc.Accept(&extr);
0174       return extr.getTrackerTopology();
0175     } else {
0176       throw cms::Exception("StandaloneTrackerTopology",
0177                            std::string{"Failed to parse file "} + xmlFileName + ": " + xmlDoc.ErrorStr());
0178     }
0179   }
0180   TrackerTopology fromTrackerParametersXMLString(const std::string& xmlContent) {
0181     tinyxml2::XMLDocument xmlDoc;
0182     xmlDoc.Parse(xmlContent.c_str());
0183     if (!xmlDoc.Error()) {
0184       TrackerTopologyExtractor extr{};
0185       xmlDoc.Accept(&extr);
0186       return extr.getTrackerTopology();
0187     } else {
0188       throw cms::Exception("StandaloneTrackerTopology", std::string{"Error while parsing XML: "} + xmlDoc.ErrorStr());
0189     }
0190   }
0191 }  // namespace StandaloneTrackerTopology