CMS 3D CMS Logo

StandaloneTrackerTopology.cc
Go to the documentation of this file.
2 
3 #include <memory>
5 #include <xercesc/parsers/XercesDOMParser.hpp>
6 #include <xercesc/sax/HandlerBase.hpp>
7 #include <xercesc/dom/DOM.hpp>
9 
10 using namespace xercesc;
11 
12 namespace {
13  // copy into a std::string and clean up the C-style string
14  inline std::string xmlc_to_stdstring(char* cstr)
15  {
16  std::string str{cstr};
17  XMLString::release(&cstr);
18  return str;
19  }
20  // transcode, convert into a std::string and clean up the intermediate C-style string
21  inline std::string xmlc_to_stdstring(const XMLCh* xcstr)
22  {
23  char* cstr = XMLString::transcode(xcstr);
24  std::string str{cstr};
25  XMLString::release(&cstr);
26  return str;
27  }
28 
29  // RAII release the XMLCh*
30  class auto_XMLString
31  {
32  public:
33  auto_XMLString(XMLCh* xch) : m_xch(xch) {}
34  XMLCh* get() const { return m_xch; }
35  ~auto_XMLString() { if ( m_xch ) { XMLString::release(&m_xch); } }
36  // avoid double release: make this class move-only
37  auto_XMLString(const auto_XMLString&) = delete;
38  auto_XMLString& operator=(const auto_XMLString&) = delete;
39  auto_XMLString(auto_XMLString&& other) { m_xch = other.m_xch; other.m_xch = nullptr; }
40  auto_XMLString& operator=(auto_XMLString&& other) { m_xch = other.m_xch; other.m_xch = nullptr; return *this; }
41  private:
42  XMLCh* m_xch;
43  };
44 
45  std::string getAttr( const DOMNamedNodeMap* attrMap, const auto_XMLString& attrName )
46  {
47  if ( attrMap->getNamedItem(attrName.get()) ) {
48  return xmlc_to_stdstring(attrMap->getNamedItem(attrName.get())->getTextContent());
49  } else {
50  return std::string{""};
51  }
52  }
53 
54  // split into tokens and convert them to uint32_t
55  inline std::vector<uint32_t> split_string_to_uints(const std::string& str)
56  {
57  std::vector<uint32_t> out{};
58  std::size_t iStart{str.find_first_not_of(" ,\n")}, iEnd{};
59  while ( std::string::npos != iStart ) {
60  iEnd = str.find_first_of(" ,\n", iStart);
61  out.push_back(std::stoul(str.substr(iStart, iEnd), nullptr, 0));
62  iStart = str.find_first_not_of(" ,\n", iEnd);
63  }
64  return out;
65  }
66 }
67 
68 namespace StandaloneTrackerTopology {
70 {
77 
78  try {
80  } catch ( const XMLException& xmlEx ) {
81  throw cms::Exception("StandaloneTrackerTopology",
82  "XML exception at initialization : " + xmlc_to_stdstring(xmlEx.getMessage()));
83  }
84 
85  { // scope for the parser, and all dependent DOM manipulation
86  std::unique_ptr<XercesDOMParser> parser{new XercesDOMParser()};
87  parser->setValidationScheme(XercesDOMParser::Val_Always);
88 
89  std::unique_ptr<ErrorHandler> errHandler{static_cast<ErrorHandler*>(new HandlerBase())};
90  parser->setErrorHandler(errHandler.get());
91 
92  try {
93  parser->parse(xmlFileName.c_str());
94  } catch ( const XMLException& xmlEx ) {
95  throw cms::Exception("StandaloneTrackerTopology",
96  "XML exception when parsing " + xmlFileName + " : " + xmlc_to_stdstring(xmlEx.getMessage()));
97  } catch ( const DOMException& domEx ) {
98  throw cms::Exception("StandaloneTrackerTopology",
99  "DOM exception when parsing " + xmlFileName + " : " + xmlc_to_stdstring(domEx.getMessage()));
100  } catch ( const SAXException& saxEx ) {
101  throw cms::Exception("StandaloneTrackerTopology",
102  "SAX exception when parsing " + xmlFileName + " : " + xmlc_to_stdstring(saxEx.getMessage()));
103  }
104 
105  const std::string subdetName{"Subdetector"};
106  auto_XMLString nm_type{XMLString::transcode("type")};
107  auto_XMLString nm_name{XMLString::transcode("name")};
108  auto_XMLString nm_nEntries{XMLString::transcode("nEntries")};
109 
110  try {
111  DOMDocument* doc{parser->getDocument()};
112  DOMElement* docRootNode{doc->getDocumentElement()};
113  DOMNodeIterator* walker = doc->createNodeIterator(docRootNode, DOMNodeFilter::SHOW_ELEMENT, nullptr, true);
114  for ( DOMNode* currentNode = walker->nextNode(); currentNode; currentNode = walker->nextNode() ) {
115  const auto thisNodeName = xmlc_to_stdstring(currentNode->getNodeName());
116  if ( thisNodeName == "Vector" ) {
117  const auto attrs = currentNode->getAttributes();
118  const auto att_type = getAttr(attrs, nm_type);
119  if ( att_type == "numeric" ) {
120  const auto att_name = getAttr(attrs, nm_name);;
121  if ( 0 == att_name.compare(0, subdetName.size(), subdetName) ) {
122  const auto att_nEntries = getAttr(attrs, nm_nEntries);
123  const std::size_t nEntries = att_nEntries.empty() ? 0 : std::stoul(att_nEntries);
124  const auto vals = split_string_to_uints(xmlc_to_stdstring(currentNode->getTextContent()));
125  if ( nEntries != vals.size() ) {
126  throw cms::Exception("StandaloneTrackerTopology",
127  ("Problem parsing element with name '"+att_name+"' from '"+xmlFileName+"': "+
128  "'nEntries' attribute claims "+std::to_string(nEntries)+" elements, but parsed "+std::to_string(vals.size())));
129  }
130  const auto subDet = std::stoi(att_name.substr(subdetName.size()));
131  switch (subDet) {
132  case PixelSubdetector::PixelBarrel: // layer, ladder module
133  pxbVals.layerStartBit_ = vals[0];
134  pxbVals.ladderStartBit_ = vals[1];
135  pxbVals.moduleStartBit_ = vals[2];
136 
137  pxbVals.layerMask_ = vals[3];
138  pxbVals.ladderMask_ = vals[4];
139  pxbVals.moduleMask_ = vals[5];
140  break;
141 
142  case PixelSubdetector::PixelEndcap: // side, disk, blade, panel, module
143  pxfVals.sideStartBit_ = vals[0];
144  pxfVals.diskStartBit_ = vals[1];
145  pxfVals.bladeStartBit_ = vals[2];
146  pxfVals.panelStartBit_ = vals[3];
147  pxfVals.moduleStartBit_ = vals[4];
148 
149  pxfVals.sideMask_ = vals[5];
150  pxfVals.diskMask_ = vals[6];
151  pxfVals.bladeMask_ = vals[7];
152  pxfVals.panelMask_ = vals[8];
153  pxfVals.moduleMask_ = vals[9];
154  break;
155 
156  case StripSubdetector::TIB: // layer, str_fw_bw, str_int_ext, str, module, ster
157  tibVals.layerStartBit_ = vals[ 0];
158  tibVals.str_fw_bwStartBit_ = vals[ 1];
159  tibVals.str_int_extStartBit_ = vals[ 2];
160  tibVals.strStartBit_ = vals[ 3];
161  tibVals.moduleStartBit_ = vals[ 4];
162  tibVals.sterStartBit_ = vals[ 5];
163 
164  tibVals.layerMask_ = vals[ 6];
165  tibVals.str_fw_bwMask_ = vals[ 7];
166  tibVals.str_int_extMask_ = vals[ 8];
167  tibVals.strMask_ = vals[ 9];
168  tibVals.moduleMask_ = vals[10];
169  tibVals.sterMask_ = vals[11];
170  break;
171 
172  case StripSubdetector::TID: // side, wheel, ring, module_fw_bw, module, ster
173  tidVals.sideStartBit_ = vals[ 0];
174  tidVals.wheelStartBit_ = vals[ 1];
175  tidVals.ringStartBit_ = vals[ 2];
176  tidVals.module_fw_bwStartBit_ = vals[ 3];
177  tidVals.moduleStartBit_ = vals[ 4];
178  tidVals.sterStartBit_ = vals[ 5];
179 
180  tidVals.sideMask_ = vals[ 6];
181  tidVals.wheelMask_ = vals[ 7];
182  tidVals.ringMask_ = vals[ 8];
183  tidVals.module_fw_bwMask_ = vals[ 9];
184  tidVals.moduleMask_ = vals[10];
185  tidVals.sterMask_ = vals[11];
186  break;
187 
188  case StripSubdetector::TOB: // layer, rod_fw_bw, rod, module, ster
189  tobVals.layerStartBit_ = vals[0];
190  tobVals.rod_fw_bwStartBit_ = vals[1];
191  tobVals.rodStartBit_ = vals[2];
192  tobVals.moduleStartBit_ = vals[3];
193  tobVals.sterStartBit_ = vals[4];
194 
195  tobVals.layerMask_ = vals[5];
196  tobVals.rod_fw_bwMask_ = vals[6];
197  tobVals.rodMask_ = vals[7];
198  tobVals.moduleMask_ = vals[8];
199  tobVals.sterMask_ = vals[9];
200  break;
201 
202  case StripSubdetector::TEC: // side, wheel, petal_fw_bw, petal, ring, module, ster
203  tecVals.sideStartBit_ = vals[ 0];
204  tecVals.wheelStartBit_ = vals[ 1];
205  tecVals.petal_fw_bwStartBit_ = vals[ 2];
206  tecVals.petalStartBit_ = vals[ 3];
207  tecVals.ringStartBit_ = vals[ 4];
208  tecVals.moduleStartBit_ = vals[ 5];
209  tecVals.sterStartBit_ = vals[ 6];
210 
211  tecVals.sideMask_ = vals[ 7];
212  tecVals.wheelMask_ = vals[ 8];
213  tecVals.petal_fw_bwMask_ = vals[ 9];
214  tecVals.petalMask_ = vals[10];
215  tecVals.ringMask_ = vals[11];
216  tecVals.moduleMask_ = vals[12];
217  tecVals.sterMask_ = vals[13];
218  break;
219  }
220  }
221  }
222  }
223  }
224  } catch ( const DOMException& domEx ) {
225  throw cms::Exception("StandaloneTrackerTopology",
226  "DOM exception in "+xmlFileName+" : "+xmlc_to_stdstring(domEx.getMessage()));
227  }
228 
229  } // parser and DOM scope
231 
232  return TrackerTopology(pxbVals, pxfVals, tecVals, tibVals, tidVals, tobVals);
233 }
234 }
void xercesTerminate()
Definition: Xerces.cc:23
TrackerTopology fromTrackerParametersXML(const std::string &xmlFileName)
void xercesInitialize()
Definition: Xerces.cc:18
unsigned int module_fw_bwStartBit_