CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DOMHelperFunctions.cc
Go to the documentation of this file.
1 
12 #include <xercesc/dom/DOM.hpp>
13 #include <xercesc/dom/DOMNode.hpp>
14 #include <xercesc/parsers/XercesDOMParser.hpp>
17 #include <xercesc/framework/LocalFileFormatTarget.hpp>
18 #include <sstream>
19 
20 using namespace std;
21 using namespace xuti;
22 using namespace xercesc;
23 
24 const DetId xuti::readCellId(xercesc::DOMElement* node) {
25  int ieta = 0;
26  int iphi = 0;
27  int ix = 0;
28  int iy = 0;
29  int ixSC = 0;
30  int iySC = 0;
31  int zside = 0;
32 
33  stringstream ieta_str;
34  stringstream iphi_str;
35  stringstream ix_str;
36  stringstream iy_str;
37  stringstream ixSC_str;
38  stringstream iySC_str;
39  stringstream zside_str;
40 
41  ieta_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr()));
42  iphi_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr()));
43  ix_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(ix_tag.c_str()).ptr()));
44  iy_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iy_tag.c_str()).ptr()));
45  ixSC_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(ixSC_tag.c_str()).ptr()));
46  iySC_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iySC_tag.c_str()).ptr()));
47  zside_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr()));
48 
49  ieta_str >> ieta;
50  iphi_str >> iphi;
51  ix_str >> ix;
52  iy_str >> iy;
53  ixSC_str >> ixSC;
54  iySC_str >> iySC;
55  zside_str >> zside;
56 
57  if (ieta && iphi) {
58  return EBDetId(ieta, iphi);
59  }
60  if (ix && iy && zside) {
61  return EEDetId(ix, iy, zside);
62  }
63  if (ixSC && iySC && zside) {
64  return EcalScDetId(ixSC, iySC, zside);
65  }
66 
67  cerr << "XMLCell: error reading cell, missing field ?" << std::endl;
68  return 0;
69 }
70 
71 DOMElement* xuti::writeCell(xercesc::DOMNode* node, const DetId& detid) {
72  DOMElement* cell_node = node->getOwnerDocument()->createElement(cms::xerces::uStr(Cell_tag.c_str()).ptr());
73 
74  node->appendChild(cell_node);
75 
76  if (detid.subdetId() == EcalBarrel) {
77  stringstream value_s;
78  value_s << EBDetId(detid).ieta();
79 
80  cell_node->setAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
81  value_s.str("");
82  value_s << EBDetId(detid).iphi();
83 
84  cell_node->setAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
85 
86  } else if (detid.subdetId() == EcalEndcap) {
87  // is it a EcalScDetId ?
88  unsigned int ScIdCheck = detid.rawId() & 0x00008000;
89  if (ScIdCheck == 0) {
90  stringstream value_s;
91  value_s << EEDetId(detid).ix();
92 
93  cell_node->setAttribute(cms::xerces::uStr(ix_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
94  value_s.str("");
95  value_s << EEDetId(detid).iy();
96 
97  cell_node->setAttribute(cms::xerces::uStr(iy_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
98  value_s.str("");
99  value_s << EEDetId(detid).zside();
100 
101  cell_node->setAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr(),
102  cms::xerces::uStr(value_s.str().c_str()).ptr());
103  } else {
104  stringstream value_s;
105  value_s << EcalScDetId(detid).ix();
106 
107  cell_node->setAttribute(cms::xerces::uStr(ixSC_tag.c_str()).ptr(),
108  cms::xerces::uStr(value_s.str().c_str()).ptr());
109  value_s.str("");
110  value_s << EcalScDetId(detid).iy();
111 
112  cell_node->setAttribute(cms::xerces::uStr(iySC_tag.c_str()).ptr(),
113  cms::xerces::uStr(value_s.str().c_str()).ptr());
114  value_s.str("");
115  value_s << EcalScDetId(detid).zside();
116 
117  cell_node->setAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr(),
118  cms::xerces::uStr(value_s.str().c_str()).ptr());
119  }
120 
121  } else if (detid.subdetId() == EcalTriggerTower) {
122  stringstream value_s;
123  value_s << EcalTrigTowerDetId(detid).ieta();
124 
125  cell_node->setAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
126  value_s.str("");
127  value_s << EcalTrigTowerDetId(detid).iphi();
128 
129  cell_node->setAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
130  }
131  return cell_node;
132 }
133 
134 // return 0 if not found
135 DOMNode* xuti::getChildNode(DOMNode* node, const std::string& nodename) {
136  if (!node)
137  return nullptr;
138 
139  for (DOMNode* childNode = node->getFirstChild(); childNode; childNode = childNode->getNextSibling()) {
140  if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
141  const string foundName = cms::xerces::toString(childNode->getNodeName());
142 
143  if (foundName == nodename)
144  return childNode;
145  } // if element
146  } // for child
147 
148  return nullptr;
149 }
150 
151 void xuti::writeHeader(xercesc::DOMNode* parentNode, const EcalCondHeader& header) {
152  DOMElement* headernode = parentNode->getOwnerDocument()->createElement(cms::xerces::uStr(Header_tag.c_str()).ptr());
153  parentNode->appendChild(headernode);
154 
155  // write the actual header
156  WriteNodeWithValue(headernode, Header_methodtag, header.method_);
157  WriteNodeWithValue(headernode, Header_versiontag, header.version_);
159  WriteNodeWithValue(headernode, Header_sincetag, header.since_);
160  WriteNodeWithValue(headernode, Header_tagtag, header.tag_);
161  WriteNodeWithValue(headernode, Header_datetag, header.date_);
162 }
163 
164 void xuti::readHeader(xercesc::DOMNode* parentNode, EcalCondHeader& header) {
165  DOMNode* hnode = getChildNode(parentNode, Header_tag);
166 
167  DOMNode* node = getChildNode(hnode, Header_methodtag);
168  GetNodeStringData(node, header.method_);
169 
170  node = getChildNode(hnode, Header_versiontag);
171  GetNodeStringData(node, header.version_);
172 
173  node = getChildNode(hnode, Header_datasourcetag);
174  GetNodeStringData(node, header.datasource_);
175 
176  node = getChildNode(hnode, Header_sincetag);
177  GetNodeData(node, header.since_);
178 
179  node = getChildNode(hnode, Header_tagtag);
180  GetNodeStringData(node, header.tag_);
181 
182  node = getChildNode(hnode, Header_datetag);
183  GetNodeStringData(node, header.date_);
184 }
185 
188 
189  XercesDOMParser* parser = new XercesDOMParser;
190  parser->setValidationScheme(XercesDOMParser::Val_Never);
191  parser->setDoNamespaces(false);
192  parser->setDoSchema(false);
193 
194  parser->parse(filename.c_str());
195 
196  DOMDocument* xmlDoc = parser->getDocument();
197 
198  if (!xmlDoc) {
199  std::cout << "Error parsing document" << std::endl;
200  return -1;
201  }
202 
203  DOMElement* elementRoot = xmlDoc->getDocumentElement();
204 
205  xuti::readHeader(elementRoot, header);
206 
207  delete parser;
209 
210  return 0;
211 }
212 
213 void xuti::GetNodeStringData(xercesc::DOMNode* node, std::string& value) {
214  value = cms::xerces::toString(node->getTextContent());
215 }
void GetNodeStringData(xercesc::DOMNode *node, std::string &value)
get the node data as string. Needs to be used to avoid splitting
std::string datasource_
cond::Time_t since_
int ix() const
Definition: EEDetId.h:77
std::string date_
const std::string iySC_tag("iySC")
void xercesTerminate()
Definition: Xerces.cc:23
const std::string Header_sincetag("since")
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
std::string version_
const std::string iEta_tag("iEta")
int zside(DetId const &)
void xercesInitialize()
Definition: Xerces.cc:18
void WriteNodeWithValue(xercesc::DOMNode *parentNode, const std::string &tag, const T &value)
write a node with
int ieta() const
get the tower ieta
int iphi() const
get the crystal iphi
Definition: EBDetId.h:51
std::string toString(XMLCh const *toTranscode)
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
const std::string Header_methodtag("method")
int ix() const
Definition: EcalScDetId.h:70
const std::string Cell_tag("cell")
const std::string ix_tag("ix")
const std::string Header_datetag("date")
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
int zside() const
Definition: EEDetId.h:71
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
ZStr< XMLCh > uStr(char const *str)
int iy() const
Definition: EEDetId.h:83
int ieta() const
get the crystal ieta
Definition: EBDetId.h:49
int iy() const
Definition: EcalScDetId.h:76
std::string tag_
const std::string iy_tag("iy")
const std::string Header_tagtag("tag")
Definition: DetId.h:17
const std::string Header_versiontag("version")
int iphi() const
get the tower iphi
const std::string Header_tag("EcalCondHeader")
std::string method_
void GetNodeData(xercesc::DOMNode *node, T &value)
get the node data
xercesc::DOMNode * getChildNode(xercesc::DOMNode *node, const std::string &nodename)
get the child of
const std::string zside_tag("zside")
int zside() const
Definition: EcalScDetId.h:64
const std::string Header_datasourcetag("datasource")
tuple filename
Definition: lut2db_cfg.py:20
const std::string ixSC_tag("ixSC")
tuple cout
Definition: gather_cfg.py:144
xercesc::DOMElement * writeCell(xercesc::DOMNode *node, const DetId &detid)
Append a Cell node with attributes to.
const std::string iPhi_tag("iPhi")
const DetId readCellId(xercesc::DOMElement *node)
Assuming.