CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DOMHelperFunctions.cc
Go to the documentation of this file.
1 
14 #include <xercesc/dom/DOM.hpp>
15 #include <xercesc/dom/DOMNode.hpp>
16 #include <xercesc/parsers/XercesDOMParser.hpp>
17 #include <xercesc/util/PlatformUtils.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 << toNative(node->getAttribute(fromNative(iEta_tag).c_str()));
46  iphi_str << toNative(node->getAttribute(fromNative(iPhi_tag).c_str()));
47  ix_str << toNative(node->getAttribute(fromNative(ix_tag).c_str()));
48  iy_str << toNative(node->getAttribute(fromNative(iy_tag).c_str()));
49  ixSC_str << toNative(node->getAttribute(fromNative(ixSC_tag).c_str()));
50  iySC_str << toNative(node->getAttribute(fromNative(iySC_tag).c_str()));
51  zside_str<< toNative(node->getAttribute(fromNative(zside_tag).c_str()));
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( fromNative(Cell_tag).c_str());
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(fromNative(iEta_tag).c_str(),
86  fromNative(value_s.str()).c_str());
87  value_s.str("");
88  value_s <<EBDetId(detid).iphi() ;
89 
90  cell_node->setAttribute(fromNative(iPhi_tag).c_str(),
91  fromNative(value_s.str()).c_str());
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(fromNative(ix_tag).c_str(),
102  fromNative(value_s.str()).c_str());
103  value_s.str("");
104  value_s <<EEDetId(detid).iy() ;
105 
106  cell_node->setAttribute(fromNative(iy_tag).c_str(),
107  fromNative(value_s.str()).c_str());
108  value_s.str("");
109  value_s <<EEDetId(detid).zside() ;
110 
111  cell_node->setAttribute(fromNative(zside_tag).c_str(),
112  fromNative(value_s.str()).c_str());
113  }
114  else {
115  stringstream value_s;
116  value_s << EcalScDetId(detid).ix() ;
117 
118  cell_node->setAttribute(fromNative(ixSC_tag).c_str(),
119  fromNative(value_s.str()).c_str());
120  value_s.str("");
121  value_s << EcalScDetId(detid).iy() ;
122 
123  cell_node->setAttribute(fromNative(iySC_tag).c_str(),
124  fromNative(value_s.str()).c_str());
125  value_s.str("");
126  value_s << EcalScDetId(detid).zside() ;
127 
128  cell_node->setAttribute(fromNative(zside_tag).c_str(),
129  fromNative(value_s.str()).c_str());
130  }
131 
132  } else if (detid.subdetId() == EcalTriggerTower ){
133  stringstream value_s;
134  value_s <<EcalTrigTowerDetId(detid).ieta() ;
135 
136  cell_node->setAttribute(fromNative(iEta_tag).c_str(),
137  fromNative(value_s.str()).c_str());
138  value_s.str("");
139  value_s <<EcalTrigTowerDetId(detid).iphi() ;
140 
141  cell_node->setAttribute(fromNative(iPhi_tag).c_str(),
142  fromNative(value_s.str()).c_str());
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 0;
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 = toNative(childNode->getNodeName());
158 
159  if (foundName == nodename) return childNode;
160  }// if element
161  }// for child
162 
163  return 0;
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( fromNative(Header_tag).c_str());
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 
215 int xuti::readHeader(const std::string& filename,EcalCondHeader& header ){
216 
217  XMLPlatformUtils::Initialize();
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;
239  XMLPlatformUtils::Terminate();
240 
241  return 0;
242 }
243 
244 void xuti:: GetNodeStringData(xercesc::DOMNode* node, std::string& value){
245  value= toNative(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:71
std::string date_
const std::string iySC_tag("iySC")
const std::string Header_sincetag("since")
std::string version_
const std::string iEta_tag("iEta")
void WriteNodeWithValue(xercesc::DOMNode *parentNode, const std::string &tag, const T &value)
write a node with
tuple node
Definition: Node.py:50
int ieta() const
get the tower ieta
int iphi() const
get the crystal iphi
Definition: EBDetId.h:46
uint32_t rawId() const
get the raw id
Definition: DetId.h:45
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
const std::string Header_methodtag("method")
int ix() const
Definition: EcalScDetId.h:72
const std::string Cell_tag("cell")
const std::string ix_tag("ix")
const std::string Header_datetag("date")
int zside() const
Definition: EEDetId.h:65
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
int iy() const
Definition: EEDetId.h:77
int ieta() const
get the crystal ieta
Definition: EBDetId.h:44
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:39
int iy() const
Definition: EcalScDetId.h:78
std::string tag_
const std::string iy_tag("iy")
const std::string Header_tagtag("tag")
Definition: DetId.h:20
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:66
const std::string Header_datasourcetag("datasource")
tuple filename
Definition: lut2db_cfg.py:20
XercesString fromNative(const char *str)
Definition: XercesString.h:31
const std::string ixSC_tag("ixSC")
tuple cout
Definition: gather_cfg.py:121
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.
std::string toNative(const XMLCh *str)
Definition: XercesString.h:42