CMS 3D CMS Logo

EcalTBWeightsXMLTranslator.cc
Go to the documentation of this file.
2 #include <xercesc/dom/DOM.hpp>
3 #include <xercesc/parsers/XercesDOMParser.hpp>
6 #include <xercesc/util/XMLString.hpp>
7 #include <xercesc/sax/SAXException.hpp>
8 #include <xercesc/framework/LocalFileFormatTarget.hpp>
11 #include <fstream>
12 
13 using namespace xercesc;
14 using namespace std;
15 using namespace xuti;
16 
17 // ouch ! I have to define here the << and >> operator !
18 
19 std::ostream& operator<<(std::ostream& stream_, const EcalXtalGroupId& id_) { return stream_ << id_.id(); }
20 
21 std::istream& operator>>(std::istream& stream_, EcalXtalGroupId& id_) {
22  unsigned int id;
23  stream_ >> id;
24  id_ = EcalXtalGroupId(id);
25  return stream_;
26 }
27 
30 
31  XercesDOMParser* parser = new XercesDOMParser;
32  parser->setValidationScheme(XercesDOMParser::Val_Never);
33  parser->setDoNamespaces(false);
34  parser->setDoSchema(false);
35 
36  parser->parse(filename.c_str());
37 
38  DOMDocument* xmlDoc = parser->getDocument();
39  if (!xmlDoc) {
40  std::cout << "EcalTBWeightsXMLTranslator::Error parsing document" << std::endl;
41  return -1;
42  }
43 
44  DOMElement* elementRoot = xmlDoc->getDocumentElement();
45  xuti::readHeader(elementRoot, header);
46 
47  DOMNode* wnode = getChildNode(elementRoot, EcalTBWeight_tag);
48 
49  while (wnode) {
50  DOMNode* gid_node = getChildNode(wnode, EcalXtalGroupId_tag);
51  DOMNode* tdc_node = getChildNode(wnode, EcalTDCId_tag);
52  DOMNode* ws_node = getChildNode(wnode, EcalWeightSet_tag);
53 
54  EcalXtalGroupId gid;
57 
58  GetNodeData(gid_node, gid);
59  GetNodeData(tdc_node, tid);
60 
61  readWeightSet(ws_node, ws);
62 
63  record.setValue(gid, tid, ws);
64 
65  wnode = wnode->getNextSibling();
66 
67  while (wnode && wnode->getNodeType() != DOMNode::ELEMENT_NODE)
68  wnode = wnode->getNextSibling();
69  }
70 
72 
73  return 0;
74 }
75 
77  const EcalCondHeader& header,
78  const EcalTBWeights& record) {
80 
81  std::fstream fs(filename.c_str(), ios::out);
82  fs << dumpXML(header, record);
83 
85 
86  return 0;
87 }
88 
89 void EcalTBWeightsXMLTranslator::readWeightSet(xercesc::DOMNode* parentNode, EcalWeightSet& ws) {
90  // get the first cell node
91  DOMNode* wgtBSnode = getChildNode(parentNode, wgtBeforeSwitch_tag);
92  DOMNode* wgtASnode = getChildNode(parentNode, wgtAfterSwitch_tag);
93  DOMNode* wgtChi2BSnode = getChildNode(parentNode, wgtChi2BeforeSwitch_tag);
94  DOMNode* wgtChi2ASnode = getChildNode(parentNode, wgtChi2AfterSwitch_tag);
95 
96  DOMNode* rownode = getChildNode(wgtBSnode, row_tag);
97 
98  DOMElement* rowelement = nullptr;
99 
100  // loop on row nodes
101  while (rownode) {
102  rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
103 
104  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
105 
106  std::stringstream rowid_ss(rowid_s);
107  int rowid = 0;
108  rowid_ss >> rowid;
109 
110  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
111 
112  std::stringstream weightrow_s(weightrow);
113  double weight = 0;
114  int i = 0;
115  while (weightrow_s >> weight) {
116  ws.getWeightsBeforeGainSwitch()(rowid, i) = weight;
117  i++;
118  }
119 
120  // get next cell
121  rownode = rownode->getNextSibling();
122 
123  while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
124  rownode = rownode->getNextSibling();
125  }
126 
127  rownode = getChildNode(wgtASnode, row_tag);
128 
129  // loop on row nodes
130  while (rownode) {
131  rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
132 
133  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
134 
135  std::stringstream rowid_ss(rowid_s);
136  int rowid = 0;
137  rowid_ss >> rowid;
138 
139  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
140 
141  std::stringstream weightrow_s(weightrow);
142  double weight = 0;
143  int i = 0;
144  while (weightrow_s >> weight) {
145  ws.getWeightsAfterGainSwitch()(rowid, i) = weight;
146  i++;
147  }
148 
149  // get next cell
150  rownode = rownode->getNextSibling();
151 
152  while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
153  rownode = rownode->getNextSibling();
154  }
155 
156  rownode = getChildNode(wgtChi2BSnode, row_tag);
157 
158  // loop on row nodes
159  while (rownode) {
160  rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
161  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
162 
163  std::stringstream rowid_ss(rowid_s);
164  int rowid = 0;
165  rowid_ss >> rowid;
166 
167  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
168 
169  std::stringstream weightrow_s(weightrow);
170  double weight = 0;
171  int i = 0;
172  while (weightrow_s >> weight) {
173  ws.getChi2WeightsBeforeGainSwitch()(rowid, i) = weight;
174  i++;
175  }
176 
177  // get next cell
178  rownode = rownode->getNextSibling();
179 
180  while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
181  rownode = rownode->getNextSibling();
182  }
183 
184  rownode = getChildNode(wgtChi2ASnode, row_tag);
185 
186  // loop on row nodes
187  while (rownode) {
188  rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
189  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
190 
191  std::stringstream rowid_ss(rowid_s);
192  int rowid = 0;
193  rowid_ss >> rowid;
194 
195  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
196 
197  std::stringstream weightrow_s(weightrow);
198  double weight = 0;
199  int i = 0;
200  while (weightrow_s >> weight) {
201  ws.getChi2WeightsAfterGainSwitch()(rowid, i) = weight;
202  i++;
203  }
204 
205  // get next cell
206  rownode = rownode->getNextSibling();
207 
208  while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
209  rownode = rownode->getNextSibling();
210  }
211 }
212 
213 void EcalTBWeightsXMLTranslator::writeWeightSet(xercesc::DOMNode* parentNode, const EcalWeightSet& ws) {
214  xercesc::DOMDocument* doc = parentNode->getOwnerDocument();
215 
216  DOMElement* weightsetel = doc->createElement(cms::xerces::uStr(EcalWeightSet_tag.c_str()).ptr());
217  parentNode->appendChild(weightsetel);
218 
219  DOMElement* wgtBS = doc->createElement(cms::xerces::uStr(wgtBeforeSwitch_tag.c_str()).ptr());
220  weightsetel->appendChild(wgtBS);
221 
222  DOMElement* wgtAS = doc->createElement(cms::xerces::uStr(wgtAfterSwitch_tag.c_str()).ptr());
223  weightsetel->appendChild(wgtAS);
224 
225  DOMElement* wgtChi2BS = doc->createElement(cms::xerces::uStr(wgtChi2BeforeSwitch_tag.c_str()).ptr());
226  weightsetel->appendChild(wgtChi2BS);
227 
228  DOMElement* wgtChi2AS = doc->createElement(cms::xerces::uStr(wgtChi2AfterSwitch_tag.c_str()).ptr());
229  weightsetel->appendChild(wgtChi2AS);
230 
231  writeWeightMatrix(wgtBS, ws.getWeightsBeforeGainSwitch());
232  writeWeightMatrix(wgtAS, ws.getWeightsAfterGainSwitch());
233 
234  writeChi2WeightMatrix(wgtChi2BS, ws.getChi2WeightsBeforeGainSwitch());
235  writeChi2WeightMatrix(wgtChi2AS, ws.getChi2WeightsBeforeGainSwitch());
236 }
237 
240  DOMElement* row = nullptr;
241  DOMAttr* rowid = nullptr;
242  DOMText* rowvalue = nullptr;
243 
244  const int ncols = 10;
245  const int nrows = 3;
246 
247  for (int i = 0; i < nrows; ++i) {
248  row = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
249  node->appendChild(row);
250 
251  stringstream value_s;
252  value_s << i;
253 
254  rowid = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
255  rowid->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
256  row->setAttributeNode(rowid);
257 
258  stringstream row_s;
259 
260  for (int k = 0; k < ncols; ++k)
261  row_s << " " << matrix(i, k) << " ";
262 
263  rowvalue = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
264  row->appendChild(rowvalue);
265  } //for loop on col
266 }
267 
270  DOMElement* row = nullptr;
271  DOMAttr* rowid = nullptr;
272  DOMText* rowvalue = nullptr;
273 
274  const int ncols = 10;
275  const int nrows = 10;
276 
277  for (int i = 0; i < nrows; ++i) {
278  row = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
279  node->appendChild(row);
280 
281  stringstream value_s;
282  value_s << i;
283 
284  rowid = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
285  rowid->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
286  row->setAttributeNode(rowid);
287 
288  stringstream row_s;
289 
290  for (int k = 0; k < ncols; ++k)
291  row_s << " " << matrix(i, k) << " ";
292 
293  rowvalue = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
294  row->appendChild(rowvalue);
295  } //for loop on col
296 }
297 
300 
301  unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
302 
303  DOMLSSerializer* writer = impl->createLSSerializer();
304  if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
305  writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
306 
307  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
308  DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(EcalTBWeights_tag.c_str()).ptr(), doctype);
309  DOMElement* root = doc->getDocumentElement();
310 
312 
313  const EcalTBWeights::EcalTBWeightMap& wmap = record.getMap();
314 
315  EcalTBWeights::EcalTBWeightMap::const_iterator it;
316  for (it = wmap.begin(); it != wmap.end(); ++it) {
317  DOMElement* tbweight = doc->createElement(cms::xerces::uStr(EcalTBWeight_tag.c_str()).ptr());
318  root->appendChild(tbweight);
319 
320  EcalXtalGroupId gid = it->first.first;
321  EcalTBWeights::EcalTDCId tid = it->first.second;
322  EcalWeightSet ws = it->second;
323 
324  WriteNodeWithValue(tbweight, EcalXtalGroupId_tag, gid);
325  WriteNodeWithValue(tbweight, EcalTDCId_tag, tid);
326  writeWeightSet(tbweight, ws);
327 
328  } //
329 
330  std::string dump = cms::xerces::toString(writer->writeToString(root));
331  doc->release();
332  doctype->release();
333  writer->release();
334 
335  return dump;
336 }
const std::string EcalWeightSet_tag("EcalWeightSet")
static std::string dumpXML(const EcalCondHeader &header, const EcalTBWeights &record)
def dumpXML(db, tag, since, filename='dump.xml')
std::istream & operator>>(std::istream &stream_, EcalXtalGroupId &id_)
void xercesTerminate()
Definition: Xerces.cc:23
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalTBWeights &record)
std::map< std::pair< EcalXtalGroupId, EcalTDCId >, EcalWeightSet > EcalTBWeightMap
Definition: EcalTBWeights.h:18
std::ostream & operator<<(std::ostream &stream_, const EcalXtalGroupId &id_)
static void writeWeightMatrix(xercesc::DOMNode *node, const EcalWeightSet::EcalWeightMatrix &matrix)
const std::string id_tag("id")
Definition: weight.py:1
math::Matrix< 10, 10 >::type EcalChi2WeightMatrix
Definition: EcalWeightSet.h:20
math::Matrix< 3, 10 >::type EcalWeightMatrix
Definition: EcalWeightSet.h:19
const std::string row_tag("row")
void xercesInitialize()
Definition: Xerces.cc:18
void WriteNodeWithValue(xercesc::DOMNode *parentNode, const std::string &tag, const T &value)
write a node with
auto &__restrict__ ws
const std::string wgtChi2AfterSwitch_tag("Chi2WeightAfterSwitch")
std::string toString(XMLCh const *toTranscode)
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
const std::string EcalTBWeights_tag("EcalTBWeights")
const std::string EcalTBWeight_tag("EcalTBWeight")
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
ZStr< XMLCh > uStr(char const *str)
unsigned int id() const
const std::string wgtBeforeSwitch_tag("WeightBeforeSwitch")
static void writeWeightSet(xercesc::DOMNode *parentNode, const EcalWeightSet &ws)
static void readWeightSet(xercesc::DOMNode *parentNode, EcalWeightSet &ws)
const std::string wgtChi2BeforeSwitch_tag("Chi2WeightBeforeSwitch")
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 wgtAfterSwitch_tag("WeightAfterSwitch")
static int readXML(const std::string &filename, EcalCondHeader &header, EcalTBWeights &record)
const std::string EcalXtalGroupId_tag("EcalXtalGroupId")
static void writeChi2WeightMatrix(xercesc::DOMNode *node, const EcalWeightSet::EcalChi2WeightMatrix &matrix)
const std::string EcalTDCId_tag("EcalTDCId")