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_,
20  const EcalXtalGroupId& id_ ) {
21  return stream_ << id_.id();
22 }
23 
24 
25 std::istream& operator>>( std::istream& stream_,
26  EcalXtalGroupId& id_ ) {
27  unsigned int id;
28  stream_ >> id;
29  id_ =EcalXtalGroupId(id);
30  return stream_;
31 }
32 
36 
38 
39  XercesDOMParser* parser = new XercesDOMParser;
40  parser->setValidationScheme( XercesDOMParser::Val_Never );
41  parser->setDoNamespaces( false );
42  parser->setDoSchema( false );
43 
44  parser->parse(filename.c_str());
45 
46  DOMDocument* xmlDoc = parser->getDocument();
47  if (!xmlDoc) {
48  std::cout << "EcalTBWeightsXMLTranslator::Error parsing document" << std::endl;
49  return -1;
50  }
51 
52  DOMElement* elementRoot = xmlDoc->getDocumentElement();
53  xuti::readHeader(elementRoot, header);
54 
55  DOMNode * wnode=getChildNode(elementRoot,EcalTBWeight_tag);
56 
57  while (wnode){
58 
59  DOMNode * gid_node = getChildNode(wnode,EcalXtalGroupId_tag);
60  DOMNode * tdc_node = getChildNode(wnode,EcalTDCId_tag);
61  DOMNode * ws_node = getChildNode(wnode,EcalWeightSet_tag);
62 
63  EcalXtalGroupId gid;
65  EcalWeightSet ws;
66 
67  GetNodeData(gid_node,gid);
68  GetNodeData(tdc_node,tid);
69 
70  readWeightSet(ws_node,ws);
71 
72  record.setValue(gid,tid,ws);
73 
74  wnode= wnode->getNextSibling();
75 
76  while (wnode&& wnode->getNodeType( ) != DOMNode::ELEMENT_NODE)
77  wnode= wnode->getNextSibling();
78  }
79 
81 
82  return 0;
83 }
84 
86  const EcalCondHeader& header,
87  const EcalTBWeights& record){
89 
90  std::fstream fs(filename.c_str(),ios::out);
91  fs<< dumpXML(header,record);
92 
94 
95  return 0;
96 }
97 
98 
99 void
100 EcalTBWeightsXMLTranslator::readWeightSet(xercesc::DOMNode* parentNode,
101  EcalWeightSet& ws){
102 
103 
104 
105  // get the first cell node
106  DOMNode * wgtBSnode=getChildNode(parentNode,wgtBeforeSwitch_tag);
107  DOMNode * wgtASnode=getChildNode(parentNode,wgtAfterSwitch_tag);
108  DOMNode * wgtChi2BSnode=getChildNode(parentNode,wgtChi2BeforeSwitch_tag);
109  DOMNode * wgtChi2ASnode=getChildNode(parentNode,wgtChi2AfterSwitch_tag);
110 
111 
112  DOMNode* rownode = getChildNode(wgtBSnode,row_tag);
113 
114  DOMElement* rowelement=nullptr;
115 
116  // loop on row nodes
117  while (rownode){
118 
119  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
120 
121  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
122 
123  std::stringstream rowid_ss(rowid_s);
124  int rowid = 0;
125  rowid_ss >> rowid;
126 
127  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
128 
129  std::stringstream weightrow_s(weightrow);
130  double weight = 0;
131  int i = 0;
132  while(weightrow_s >> weight){
133  ws.getWeightsBeforeGainSwitch()(rowid,i)= weight;
134  i++;
135 
136  }
137 
138 
139  // get next cell
140  rownode = rownode->getNextSibling();
141 
142  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
143  rownode = rownode->getNextSibling();
144 
145 
146  }
147 
148  rownode = getChildNode(wgtASnode,row_tag);
149 
150  // loop on row nodes
151  while (rownode){
152 
153  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
154 
155  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute( cms::xerces::uStr(id_tag.c_str()).ptr()));
156 
157  std::stringstream rowid_ss(rowid_s);
158  int rowid = 0;
159  rowid_ss >> rowid;
160 
161  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
162 
163  std::stringstream weightrow_s(weightrow);
164  double weight = 0;
165  int i = 0;
166  while(weightrow_s >> weight){
167  ws.getWeightsAfterGainSwitch()(rowid,i)= weight;
168  i++;
169  }
170 
171 
172  // get next cell
173  rownode = rownode->getNextSibling();
174 
175  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
176  rownode = rownode->getNextSibling();
177 
178 
179  }
180 
181  rownode = getChildNode(wgtChi2BSnode,row_tag);
182 
183 
184  // loop on row nodes
185  while (rownode){
186 
187  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
188  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
189 
190  std::stringstream rowid_ss(rowid_s);
191  int rowid = 0;
192  rowid_ss >> rowid;
193 
194  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
195 
196  std::stringstream weightrow_s(weightrow);
197  double weight = 0;
198  int i = 0;
199  while(weightrow_s >> weight)
200  {
201  ws.getChi2WeightsBeforeGainSwitch()(rowid,i)= weight;
202  i++;
203  }
204 
205 
206  // get next cell
207  rownode = rownode->getNextSibling();
208 
209  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
210  rownode = rownode->getNextSibling();
211 
212 
213  }
214 
215 
216  rownode = getChildNode(wgtChi2ASnode,row_tag);
217 
218 
219  // loop on row nodes
220  while (rownode){
221 
222  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
223  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
224 
225  std::stringstream rowid_ss(rowid_s);
226  int rowid = 0;
227  rowid_ss >> rowid;
228 
229  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
230 
231  std::stringstream weightrow_s(weightrow);
232  double weight = 0;
233  int i = 0;
234  while(weightrow_s >> weight){
235  ws.getChi2WeightsAfterGainSwitch()(rowid,i)= weight;
236  i++;
237  }
238 
239 
240  // get next cell
241  rownode = rownode->getNextSibling();
242 
243  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
244  rownode = rownode->getNextSibling();
245 
246 
247  }
248 
249 
250 }
251 
252 void
253 EcalTBWeightsXMLTranslator::writeWeightSet(xercesc::DOMNode* parentNode,
254  const EcalWeightSet& ws){
255 
256 
257  xercesc::DOMDocument* doc = parentNode->getOwnerDocument();
258 
259  DOMElement * weightsetel= doc->createElement( cms::xerces::uStr(EcalWeightSet_tag.c_str()).ptr());
260  parentNode-> appendChild(weightsetel);
261 
262  DOMElement* wgtBS = doc->createElement( cms::xerces::uStr(wgtBeforeSwitch_tag.c_str()).ptr());
263  weightsetel->appendChild(wgtBS);
264 
265  DOMElement* wgtAS = doc->createElement(cms::xerces::uStr(wgtAfterSwitch_tag.c_str()).ptr());
266  weightsetel->appendChild(wgtAS);
267 
268  DOMElement* wgtChi2BS = doc->createElement( cms::xerces::uStr(wgtChi2BeforeSwitch_tag.c_str()).ptr());
269  weightsetel->appendChild(wgtChi2BS);
270 
271  DOMElement* wgtChi2AS = doc->createElement(cms::xerces::uStr(wgtChi2AfterSwitch_tag.c_str()).ptr());
272  weightsetel->appendChild(wgtChi2AS);
273 
274  writeWeightMatrix(wgtBS,ws.getWeightsBeforeGainSwitch());
275  writeWeightMatrix(wgtAS,ws.getWeightsAfterGainSwitch());
276 
277  writeChi2WeightMatrix(wgtChi2BS,ws.getChi2WeightsBeforeGainSwitch());
278  writeChi2WeightMatrix(wgtChi2AS,ws.getChi2WeightsBeforeGainSwitch());
279 
280 }
281 
282 void
285 
286  DOMElement* row=nullptr;
287  DOMAttr* rowid=nullptr;
288  DOMText* rowvalue=nullptr;
289 
290  const int ncols =10;
291  const int nrows =3;
292 
293  for(int i=0;i<nrows;++i)
294  {
295 
296 
297  row= node->getOwnerDocument()->createElement( cms::xerces::uStr(row_tag.c_str()).ptr());
298  node->appendChild(row);
299 
300  stringstream value_s;
301  value_s << i;
302 
303  rowid = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
304  rowid ->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
305  row ->setAttributeNode(rowid);
306 
307  stringstream row_s;
308 
309  for(int k=0;k<ncols;++k) row_s <<" " << matrix(i,k)<<" " ;
310 
311  rowvalue =
312  node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
313  row->appendChild(rowvalue);
314  }//for loop on col
315 
316 
317 }
318 
319 
320 void
323 
324  DOMElement* row=nullptr;
325  DOMAttr* rowid=nullptr;
326  DOMText* rowvalue=nullptr;
327 
328  const int ncols =10;
329  const int nrows =10;
330 
331  for(int i=0;i<nrows;++i)
332  {
333 
334 
335  row= node->getOwnerDocument()->createElement( cms::xerces::uStr(row_tag.c_str()).ptr());
336  node->appendChild(row);
337 
338  stringstream value_s;
339  value_s << i;
340 
341  rowid = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
342  rowid ->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
343  row ->setAttributeNode(rowid);
344 
345  stringstream row_s;
346 
347  for(int k=0;k<ncols;++k) row_s << " "<< matrix(i,k)<<" ";
348 
349  rowvalue =
350  node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
351  row->appendChild(rowvalue);
352  }//for loop on col
353 
354 
355 }
356 
357 
359  const EcalTBWeights& record){
360 
361 
363 
364  unique_ptr<DOMImplementation> impl( DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
365 
366  DOMLSSerializer* writer = impl->createLSSerializer();
367  if( writer->getDomConfig()->canSetParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true ))
368  writer->getDomConfig()->setParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true );
369 
370  DOMDocumentType* doctype = impl->createDocumentType( cms::xerces::uStr("XML").ptr(), nullptr, nullptr );
371  DOMDocument* doc =
372  impl->createDocument( nullptr, cms::xerces::uStr(EcalTBWeights_tag.c_str()).ptr(), doctype );
373  DOMElement* root = doc->getDocumentElement();
374 
375  xuti::writeHeader(root, header);
376 
377  const EcalTBWeights::EcalTBWeightMap& wmap= record.getMap();
378 
379  EcalTBWeights::EcalTBWeightMap::const_iterator it ;
380  for (it =wmap.begin(); it!=wmap.end(); ++it){
381 
382  DOMElement * tbweight= doc->createElement(cms::xerces::uStr(EcalTBWeight_tag.c_str()).ptr());
383  root->appendChild(tbweight);
384 
385  EcalXtalGroupId gid = it->first.first;
386  EcalTBWeights::EcalTDCId tid = it->first.second;
387  EcalWeightSet ws = it->second;
388 
390  WriteNodeWithValue(tbweight,EcalTDCId_tag, tid);
391  writeWeightSet(tbweight,ws);
392 
393 
394  } //
395 
396  std::string dump = cms::xerces::toString( writer->writeToString( root ));
397  doc->release();
398  doctype->release();
399  writer->release();
400 
401  return dump;
402 }
EcalChi2WeightMatrix & getChi2WeightsBeforeGainSwitch()
Definition: EcalWeightSet.h:31
std::map< std::pair< EcalXtalGroupId, EcalTDCId >, EcalWeightSet > EcalTBWeightMap
Definition: EcalTBWeights.h:21
const std::string EcalWeightSet_tag("EcalWeightSet")
static std::string dumpXML(const EcalCondHeader &header, const EcalTBWeights &record)
def dumpXML(db, tag, since, filename='dump.xml')
JetCorrectorParameters::Record record
Definition: classes.h:7
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::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
const std::string row_tag("row")
void setValue(const EcalXtalGroupId &groupId, const EcalTDCId &tdcId, const EcalWeightSet &weight)
void xercesInitialize()
Definition: Xerces.cc:18
void WriteNodeWithValue(xercesc::DOMNode *parentNode, const std::string &tag, const T &value)
write a node with
const unsigned int id() const
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
EcalChi2WeightMatrix & getChi2WeightsAfterGainSwitch()
Definition: EcalWeightSet.h:32
ZStr< XMLCh > uStr(char const *str)
const std::string wgtBeforeSwitch_tag("WeightBeforeSwitch")
static void writeWeightSet(xercesc::DOMNode *parentNode, const EcalWeightSet &ws)
EcalWeightMatrix & getWeightsAfterGainSwitch()
Definition: EcalWeightSet.h:30
int k[5][pyjets_maxn]
static void readWeightSet(xercesc::DOMNode *parentNode, EcalWeightSet &ws)
math::Matrix< 10, 10 >::type EcalChi2WeightMatrix
Definition: EcalWeightSet.h:23
Definition: dump.py:1
EcalWeightMatrix & getWeightsBeforeGainSwitch()
Definition: EcalWeightSet.h:29
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 EcalTBWeightMap & getMap() const
Definition: EcalTBWeights.h:31
math::Matrix< 3, 10 >::type EcalWeightMatrix
Definition: EcalWeightSet.h:22
const std::string EcalTDCId_tag("EcalTDCId")