CMS 3D CMS Logo

StandaloneTrackerTopology.cc
Go to the documentation of this file.
2 
3 #include "tinyxml2.h"
5 
6 namespace {
7  // split into tokens and convert them to uint32_t
8  inline std::vector<uint32_t> split_string_to_uints(const std::string& str) {
9  std::vector<uint32_t> out{};
10  std::size_t iStart{str.find_first_not_of(" ,\n")}, iEnd{};
11  while (std::string::npos != iStart) {
12  iEnd = str.find_first_of(" ,\n", iStart);
13  out.push_back(std::stoul(str.substr(iStart, iEnd), nullptr, 0));
14  iStart = str.find_first_not_of(" ,\n", iEnd);
15  }
16  return out;
17  }
18 
19  class TrackerTopologyExtractor : public tinyxml2::XMLVisitor {
20  public:
21  bool VisitEnter(const tinyxml2::XMLElement& elem, const tinyxml2::XMLAttribute*) override {
22  if (std::strcmp(elem.Value(), "Vector") == 0) {
23  const std::string att_type{elem.Attribute("type")};
24  if (att_type == "numeric") {
25  const std::string att_name{elem.Attribute("name")};
26  if (0 == att_name.compare(0, SubdetName.size(), SubdetName)) { // starts with
27  const std::string att_nEntries{elem.Attribute("nEntries")};
28  const std::size_t nEntries = att_nEntries.empty() ? 0 : std::stoul(att_nEntries);
29  const auto vals = split_string_to_uints(elem.GetText());
30 
31  if (nEntries != vals.size()) {
32  throw cms::Exception(
33  "StandaloneTrackerTopology",
34  ("Problem parsing element with name '" + att_name + "': " + "'nEntries' attribute claims " +
35  std::to_string(nEntries) + " elements, but parsed " + std::to_string(vals.size())));
36  }
37  const auto subDet = std::stoi(att_name.substr(SubdetName.size()));
38  switch (subDet) {
39  case PixelSubdetector::PixelBarrel: // layer, ladder module
40  pxbVals_.layerStartBit_ = vals[0];
41  pxbVals_.ladderStartBit_ = vals[1];
42  pxbVals_.moduleStartBit_ = vals[2];
43 
44  pxbVals_.layerMask_ = vals[3];
45  pxbVals_.ladderMask_ = vals[4];
46  pxbVals_.moduleMask_ = vals[5];
47 
48  foundPXB = true;
49  break;
50 
51  case PixelSubdetector::PixelEndcap: // side, disk, blade, panel, module
52  pxfVals_.sideStartBit_ = vals[0];
53  pxfVals_.diskStartBit_ = vals[1];
54  pxfVals_.bladeStartBit_ = vals[2];
55  pxfVals_.panelStartBit_ = vals[3];
56  pxfVals_.moduleStartBit_ = vals[4];
57 
58  pxfVals_.sideMask_ = vals[5];
59  pxfVals_.diskMask_ = vals[6];
60  pxfVals_.bladeMask_ = vals[7];
61  pxfVals_.panelMask_ = vals[8];
62  pxfVals_.moduleMask_ = vals[9];
63 
64  foundPXF = true;
65  break;
66 
67  case StripSubdetector::TIB: // layer, str_fw_bw, str_int_ext, str, module, ster
68  tibVals_.layerStartBit_ = vals[0];
69  tibVals_.str_fw_bwStartBit_ = vals[1];
70  tibVals_.str_int_extStartBit_ = vals[2];
71  tibVals_.strStartBit_ = vals[3];
72  tibVals_.moduleStartBit_ = vals[4];
73  tibVals_.sterStartBit_ = vals[5];
74 
75  tibVals_.layerMask_ = vals[6];
76  tibVals_.str_fw_bwMask_ = vals[7];
77  tibVals_.str_int_extMask_ = vals[8];
78  tibVals_.strMask_ = vals[9];
79  tibVals_.moduleMask_ = vals[10];
80  tibVals_.sterMask_ = vals[11];
81 
82  foundTIB = true;
83  break;
84 
85  case StripSubdetector::TID: // side, wheel, ring, module_fw_bw, module, ster
86  tidVals_.sideStartBit_ = vals[0];
87  tidVals_.wheelStartBit_ = vals[1];
88  tidVals_.ringStartBit_ = vals[2];
89  tidVals_.module_fw_bwStartBit_ = vals[3];
90  tidVals_.moduleStartBit_ = vals[4];
91  tidVals_.sterStartBit_ = vals[5];
92 
93  tidVals_.sideMask_ = vals[6];
94  tidVals_.wheelMask_ = vals[7];
95  tidVals_.ringMask_ = vals[8];
96  tidVals_.module_fw_bwMask_ = vals[9];
97  tidVals_.moduleMask_ = vals[10];
98  tidVals_.sterMask_ = vals[11];
99 
100  foundTID = true;
101  break;
102 
103  case StripSubdetector::TOB: // layer, rod_fw_bw, rod, module, ster
104  tobVals_.layerStartBit_ = vals[0];
105  tobVals_.rod_fw_bwStartBit_ = vals[1];
106  tobVals_.rodStartBit_ = vals[2];
107  tobVals_.moduleStartBit_ = vals[3];
108  tobVals_.sterStartBit_ = vals[4];
109 
110  tobVals_.layerMask_ = vals[5];
111  tobVals_.rod_fw_bwMask_ = vals[6];
112  tobVals_.rodMask_ = vals[7];
113  tobVals_.moduleMask_ = vals[8];
114  tobVals_.sterMask_ = vals[9];
115 
116  foundTOB = true;
117  break;
118 
119  case StripSubdetector::TEC: // side, wheel, petal_fw_bw, petal, ring, module, ster
120  tecVals_.sideStartBit_ = vals[0];
121  tecVals_.wheelStartBit_ = vals[1];
122  tecVals_.petal_fw_bwStartBit_ = vals[2];
123  tecVals_.petalStartBit_ = vals[3];
124  tecVals_.ringStartBit_ = vals[4];
125  tecVals_.moduleStartBit_ = vals[5];
126  tecVals_.sterStartBit_ = vals[6];
127 
128  tecVals_.sideMask_ = vals[7];
129  tecVals_.wheelMask_ = vals[8];
130  tecVals_.petal_fw_bwMask_ = vals[9];
131  tecVals_.petalMask_ = vals[10];
132  tecVals_.ringMask_ = vals[11];
133  tecVals_.moduleMask_ = vals[12];
134  tecVals_.sterMask_ = vals[13];
135 
136  foundTEC = true;
137  break;
138  }
139  }
140  }
141  }
142  return true;
143  }
144 
145  TrackerTopology getTrackerTopology() const {
146  if (!(foundPXB && foundPXF && foundTIB && foundTID && foundTOB && foundTEC)) {
147  throw cms::Exception("StandaloneTrackerTopology", "Could not find parameters for all tracker subdetectors");
148  }
149  return TrackerTopology(pxbVals_, pxfVals_, tecVals_, tibVals_, tidVals_, tobVals_);
150  }
151 
152  private:
159 
160  bool foundPXB = false, foundPXF = false, foundTIB = false, foundTID = false, foundTOB = false, foundTEC = false;
161 
162  const std::string SubdetName = "Subdetector";
163  };
164 } // namespace
165 
166 namespace StandaloneTrackerTopology {
168  tinyxml2::XMLDocument xmlDoc;
169  xmlDoc.LoadFile(xmlFileName.c_str());
170  if (!xmlDoc.Error()) {
171  TrackerTopologyExtractor extr{};
172  xmlDoc.Accept(&extr);
173  return extr.getTrackerTopology();
174  } else {
175  throw cms::Exception("StandaloneTrackerTopology",
176  std::string{"Failed to parse file "} + xmlFileName + ": " + xmlDoc.ErrorStr());
177  }
178  }
180  tinyxml2::XMLDocument xmlDoc;
181  xmlDoc.Parse(xmlContent.c_str());
182  if (!xmlDoc.Error()) {
183  TrackerTopologyExtractor extr{};
184  xmlDoc.Accept(&extr);
185  return extr.getTrackerTopology();
186  } else {
187  throw cms::Exception("StandaloneTrackerTopology", std::string{"Error while parsing XML: "} + xmlDoc.ErrorStr());
188  }
189  }
190 } // namespace StandaloneTrackerTopology
def elem(elemtype, innerHTML='', html_class='', kwargs)
Definition: HTMLExport.py:19
TrackerTopology fromTrackerParametersXMLFile(const std::string &xmlFileName)
TrackerTopology fromTrackerParametersXMLString(const std::string &xmlContent)
#define str(s)