CMS 3D CMS Logo

DOMHelperFunctions.cc
Go to the documentation of this file.
1 
13 #include <xercesc/dom/DOM.hpp>
14 #include <xercesc/dom/DOMNode.hpp>
15 #include <xercesc/parsers/XercesDOMParser.hpp>
18 #include <xercesc/framework/LocalFileFormatTarget.hpp>
19 #include <sstream>
20 
21 using namespace std;
22 using namespace xuti;
23 using namespace xercesc;
24 
25 const DetId xuti::readCellId(xercesc::DOMElement* node){
26 
27 
28  int ieta =0;
29  int iphi =0;
30  int ix =0;
31  int iy =0;
32  int ixSC =0;
33  int iySC =0;
34  int zside=0;
35 
36  stringstream ieta_str ;
37  stringstream iphi_str;
38  stringstream ix_str;
39  stringstream iy_str ;
40  stringstream ixSC_str;
41  stringstream iySC_str ;
42  stringstream zside_str ;
43 
44 
45  ieta_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr()));
46  iphi_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr()));
47  ix_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(ix_tag.c_str()).ptr()));
48  iy_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iy_tag.c_str()).ptr()));
49  ixSC_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(ixSC_tag.c_str()).ptr()));
50  iySC_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iySC_tag.c_str()).ptr()));
51  zside_str<< cms::xerces::toString(node->getAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr()));
52 
53  ieta_str>> ieta;
54  iphi_str>> iphi;
55  ix_str >> ix;
56  iy_str >> iy;
57  ixSC_str >> ixSC;
58  iySC_str >> iySC;
59  zside_str >> zside;
60 
61  if (ieta && iphi) {return EBDetId(ieta,iphi);}
62  if (ix && iy && zside){return EEDetId(ix,iy,zside);}
63  if (ixSC && iySC && zside){return EcalScDetId(ixSC, iySC, zside);}
64 
65  cerr<<"XMLCell: error reading cell, missing field ?"<<std::endl;
66  return 0;
67 
68 }
69 
70 
71 
72 DOMElement* xuti::writeCell(xercesc::DOMNode* node,
73  const DetId& detid){
74 
75  DOMElement* cell_node =
76  node->getOwnerDocument()->createElement( cms::xerces::uStr(Cell_tag.c_str()).ptr());
77 
78  node->appendChild(cell_node);
79 
80  if (detid.subdetId() == EcalBarrel ){
81 
82  stringstream value_s;
83  value_s <<EBDetId(detid).ieta() ;
84 
85  cell_node->setAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr(),
86  cms::xerces::uStr(value_s.str().c_str()).ptr());
87  value_s.str("");
88  value_s <<EBDetId(detid).iphi() ;
89 
90  cell_node->setAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr(),
91  cms::xerces::uStr(value_s.str().c_str()).ptr());
92 
93  } else if (detid.subdetId() == EcalEndcap){
94 
95  // is it a EcalScDetId ?
96  unsigned int ScIdCheck = detid.rawId() & 0x00008000;
97  if (ScIdCheck == 0) {
98  stringstream value_s;
99  value_s <<EEDetId(detid).ix() ;
100 
101  cell_node->setAttribute(cms::xerces::uStr(ix_tag.c_str()).ptr(),
102  cms::xerces::uStr(value_s.str().c_str()).ptr());
103  value_s.str("");
104  value_s <<EEDetId(detid).iy() ;
105 
106  cell_node->setAttribute(cms::xerces::uStr(iy_tag.c_str()).ptr(),
107  cms::xerces::uStr(value_s.str().c_str()).ptr());
108  value_s.str("");
109  value_s <<EEDetId(detid).zside() ;
110 
111  cell_node->setAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr(),
112  cms::xerces::uStr(value_s.str().c_str()).ptr());
113  }
114  else {
115  stringstream value_s;
116  value_s << EcalScDetId(detid).ix() ;
117 
118  cell_node->setAttribute(cms::xerces::uStr(ixSC_tag.c_str()).ptr(),
119  cms::xerces::uStr(value_s.str().c_str()).ptr());
120  value_s.str("");
121  value_s << EcalScDetId(detid).iy() ;
122 
123  cell_node->setAttribute(cms::xerces::uStr(iySC_tag.c_str()).ptr(),
124  cms::xerces::uStr(value_s.str().c_str()).ptr());
125  value_s.str("");
126  value_s << EcalScDetId(detid).zside() ;
127 
128  cell_node->setAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr(),
129  cms::xerces::uStr(value_s.str().c_str()).ptr());
130  }
131 
132  } else if (detid.subdetId() == EcalTriggerTower ){
133  stringstream value_s;
134  value_s <<EcalTrigTowerDetId(detid).ieta() ;
135 
136  cell_node->setAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr(),
137  cms::xerces::uStr(value_s.str().c_str()).ptr());
138  value_s.str("");
139  value_s <<EcalTrigTowerDetId(detid).iphi() ;
140 
141  cell_node->setAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr(),
142  cms::xerces::uStr(value_s.str().c_str()).ptr());
143  }
144  return cell_node;
145 }
146 
147 // return 0 if not found
148 DOMNode * xuti::getChildNode(DOMNode * node, const std::string& nodename ){
149 
150  if (!node) return nullptr;
151 
152  for (DOMNode* childNode = node->getFirstChild();
153  childNode; childNode = childNode->getNextSibling()) {
154 
155  if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
156 
157  const string foundName = cms::xerces::toString(childNode->getNodeName());
158 
159  if (foundName == nodename) return childNode;
160  }// if element
161  }// for child
162 
163  return nullptr;
164 
165 }
166 
167 
168 
169 void xuti::writeHeader (xercesc::DOMNode* parentNode,
170  const EcalCondHeader& header ){
171 
172 
173 
174  DOMElement* headernode =
175  parentNode->getOwnerDocument()->createElement( cms::xerces::uStr(Header_tag.c_str()).ptr());
176  parentNode->appendChild(headernode);
177 
178  // write the actual header
179  WriteNodeWithValue(headernode, Header_methodtag, header.method_);
180  WriteNodeWithValue(headernode, Header_versiontag, header.version_);
182  WriteNodeWithValue(headernode, Header_sincetag, header.since_);
183  WriteNodeWithValue(headernode, Header_tagtag, header.tag_);
184  WriteNodeWithValue(headernode, Header_datetag, header.date_);
185 
186 }
187 
188 
189 void xuti::readHeader(xercesc::DOMNode* parentNode,
191 
192  DOMNode * hnode = getChildNode(parentNode,Header_tag);
193 
194  DOMNode * node = getChildNode(hnode,Header_methodtag);
195  GetNodeStringData(node,header.method_);
196 
197  node = getChildNode(hnode,Header_versiontag);
198  GetNodeStringData(node,header.version_);
199 
200  node = getChildNode(hnode,Header_datasourcetag);
201  GetNodeStringData(node,header.datasource_);
202 
203  node = getChildNode(hnode,Header_sincetag);
204  GetNodeData(node,header.since_);
205 
206  node = getChildNode(hnode,Header_tagtag);
207  GetNodeStringData(node,header.tag_);
208 
209 
210  node = getChildNode(hnode,Header_datetag);
211  GetNodeStringData(node,header.date_);
212 
213 }
214 
216 
218 
219  XercesDOMParser* parser = new XercesDOMParser;
220  parser->setValidationScheme( XercesDOMParser::Val_Never );
221  parser->setDoNamespaces( false );
222  parser->setDoSchema( false );
223 
224  parser->parse(filename.c_str());
225 
226  DOMDocument* xmlDoc = parser->getDocument();
227 
228 
229  if (!xmlDoc) {
230  std::cout << "Error parsing document" << std::endl;
231  return -1;
232  }
233 
234  DOMElement* elementRoot = xmlDoc->getDocumentElement();
235 
236  xuti::readHeader(elementRoot, header);
237 
238  delete parser;
240 
241  return 0;
242 }
243 
244 void xuti:: GetNodeStringData(xercesc::DOMNode* node, std::string& value){
245  value= cms::xerces::toString(node->getTextContent());
246 }
247 
248 
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:50
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:41
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
Definition: value.py:1
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:18
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")
const std::string ixSC_tag("ixSC")
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.