CMS 3D CMS Logo

EcalWeightSetXMLTranslator.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <fstream>
4 #include <xercesc/dom/DOMNode.hpp>
5 #include <xercesc/dom/DOM.hpp>
6 #include <xercesc/parsers/XercesDOMParser.hpp>
9 #include <xercesc/util/XMLString.hpp>
10 #include <xercesc/sax/SAXException.hpp>
11 #include <xercesc/framework/LocalFileFormatTarget.hpp>
12 
16 
17 using namespace XERCES_CPP_NAMESPACE;
18 using namespace xuti;
19 using namespace std;
20 
24 
26 
27  XercesDOMParser* parser = new XercesDOMParser;
28  parser->setValidationScheme( XercesDOMParser::Val_Never );
29  parser->setDoNamespaces( false );
30  parser->setDoSchema( false );
31 
32  parser->parse(filename.c_str());
33 
34  DOMDocument* xmlDoc = parser->getDocument();
35  if (!xmlDoc) {
36  std::cout << "EcalWeightSetXMLTranslator::Error parsing document" << std::endl;
37  return -1;
38  }
39 
40  DOMElement* elementRoot = xmlDoc->getDocumentElement();
41 
42  xuti::readHeader(elementRoot, header);
43  // get the first cell node
44  DOMNode * wgtBSnode=getChildNode(elementRoot,wgtBeforeSwitch_tag);
45  DOMNode * wgtASnode=getChildNode(elementRoot,wgtAfterSwitch_tag);
46  DOMNode * wgtChi2BSnode=getChildNode(elementRoot,wgtChi2BeforeSwitch_tag);
47  DOMNode * wgtChi2ASnode=getChildNode(elementRoot,wgtChi2AfterSwitch_tag);
48 
49  DOMNode* rownode = getChildNode(wgtBSnode,row_tag);
50 
51  DOMElement* rowelement;
52 
53  // loop on row nodes
54  while (rownode){
55 
56  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
57 
58  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
59 
60  std::stringstream rowid_ss(rowid_s);
61  int rowid = 0;
62  rowid_ss >> rowid;
63 
64  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
65 
66  std::stringstream weightrow_s(weightrow);
67  double weight = 0;
68  int i = 0;
69  while(weightrow_s >> weight)
70  {
71  record.getWeightsBeforeGainSwitch()(i,rowid)= weight;
72  i++;
73  }
74 
75 
76  // get next cell
77  rownode = rownode->getNextSibling();
78 
79  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
80  rownode = rownode->getNextSibling();
81  }
82 
83  rownode = getChildNode(wgtASnode,row_tag);
84 
85  // loop on row nodes
86  while (rownode){
87 
88  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
89 
90  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
91 
92  std::stringstream rowid_ss(rowid_s);
93  int rowid = 0;
94  rowid_ss >> rowid;
95 
96  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
97 
98  std::stringstream weightrow_s(weightrow);
99  double weight = 0;
100  int i = 0;
101  while(weightrow_s >> weight)
102  {
103  record.getWeightsAfterGainSwitch()(i,rowid)= weight;
104  i++;
105  }
106 
107 
108  // get next cell
109  rownode = rownode->getNextSibling();
110 
111  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
112  rownode = rownode->getNextSibling();
113 
114 
115  }
116 
117  rownode = getChildNode(wgtChi2BSnode,row_tag);
118 
119 
120  // loop on row nodes
121  while (rownode){
122 
123  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
124  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
125 
126  std::stringstream rowid_ss(rowid_s);
127  int rowid = 0;
128  rowid_ss >> rowid;
129 
130  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
131 
132  std::stringstream weightrow_s(weightrow);
133  double weight = 0;
134  int i = 0;
135  while(weightrow_s >> weight)
136  {
137  record.getChi2WeightsBeforeGainSwitch()(i,rowid)= weight;
138  i++;
139  }
140 
141 
142  // get next cell
143  rownode = rownode->getNextSibling();
144 
145  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
146  rownode = rownode->getNextSibling();
147 
148 
149  }
150 
151 
152  rownode = getChildNode(wgtChi2ASnode,row_tag);
153 
154 
155  // loop on row nodes
156  while (rownode){
157 
158  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
159  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
160 
161  std::stringstream rowid_ss(rowid_s);
162  int rowid = 0;
163  rowid_ss >> rowid;
164 
165  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
166 
167  std::stringstream weightrow_s(weightrow);
168  double weight = 0;
169  int i = 0;
170  while(weightrow_s >> weight)
171  {
172  record.getChi2WeightsAfterGainSwitch()(i,rowid)= weight;
173  i++;
174  }
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  }
185 
186  delete parser;
188  return 0;
189 }
190 
192  const EcalCondHeader& header,
193  const EcalWeightSet& record){
194 
196 
197  std::fstream fs(filename.c_str(),ios::out);
198  fs<< dumpXML(header,record);
199 
201 
202  return 0;
203 }
204 
205 void EcalWeightSetXMLTranslator::write10x10(xercesc::DOMElement* node,
206  const EcalWeightSet& record){
207 
208  DOMElement* row[10];
209  DOMAttr* rowid[10];
210  DOMText* rowvalue[10];
212 
213 
214  if(cms::xerces::toString(node->getNodeName()) == wgtChi2BeforeSwitch_tag)
215  {
216  echi2wmatrix = record.getChi2WeightsBeforeGainSwitch();
217 
218  }
219  if(cms::xerces::toString(node->getNodeName()) == wgtChi2AfterSwitch_tag)
220  {
221  echi2wmatrix = record.getChi2WeightsAfterGainSwitch();
222 
223  }
224 
225  for(int i=0;i<10;++i)
226  {
227 
228 
229  row[i] = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
230  node->appendChild(row[i]);
231 
232  stringstream value_s;
233  value_s << i;
234 
235  rowid[i] = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
236  rowid[i]->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
237  row[i]->setAttributeNode(rowid[i]);
238 
239  stringstream row_s;
240 
241  for(int k=0;k<10;++k)
242  {
243  row_s << " ";
244  row_s << echi2wmatrix(k,i);
245  row_s << " ";
246  }//for loop on element
247 
248 
249  rowvalue[i] = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
250  row[i]->appendChild(rowvalue[i]);
251  }//for loop on row
252 }
253 
254 
255 
256 void EcalWeightSetXMLTranslator::write3x10(xercesc::DOMElement* node,
257  const EcalWeightSet& record){
258 
259  DOMElement* row[10];
260  DOMAttr* rowid[10];
261  DOMText* rowvalue[10];
263 
264 
265  if(cms::xerces::toString(node->getNodeName()) == wgtBeforeSwitch_tag)
266  ewmatrix = record.getWeightsBeforeGainSwitch();
267 
268  if(cms::xerces::toString(node->getNodeName()) == wgtAfterSwitch_tag)
269  ewmatrix = record.getWeightsAfterGainSwitch();
270 
271 
272  for(int i=0;i<10;++i)
273  {
274 
275  row[i] = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
276  node->appendChild(row[i]);
277 
278  stringstream value_s;
279  value_s << i;
280 
281  rowid[i] = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
282 
283  rowid[i]->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
284 
285  row[i]->setAttributeNode(rowid[i]);
286 
287 
288  stringstream row_s;
289 
290  for(int k=0;k<3;++k)
291  {
292  row_s << " ";
293  row_s << ewmatrix(k,i);
294  row_s << " ";
295  }//for loop on element
296 
297 
298  rowvalue[i] = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
299  row[i]->appendChild(rowvalue[i]);
300  }//for loop on row
301 }
302 
303 
305  const EcalWeightSet& record){
306 
307  unique_ptr<DOMImplementation> impl( DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
308 
309  DOMLSSerializer* writer = impl->createLSSerializer();
310  if( writer->getDomConfig()->canSetParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true ))
311  writer->getDomConfig()->setParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true );
312 
313  DOMDocumentType* doctype = impl->createDocumentType( cms::xerces::uStr("XML").ptr(), 0, 0 );
314  DOMDocument * doc =
315  impl->createDocument( 0,cms::xerces::uStr(EcalWeightSet_tag.c_str()).ptr(), doctype );
316 
317  DOMElement* root = doc->getDocumentElement();
318 
319  xuti::writeHeader(root, header);
320 
321  DOMElement* wgtBS = doc->createElement(cms::xerces::uStr(wgtBeforeSwitch_tag.c_str()).ptr());
322  root->appendChild(wgtBS);
323 
324  DOMElement* wgtAS = doc->createElement(cms::xerces::uStr(wgtAfterSwitch_tag.c_str()).ptr());
325  root->appendChild(wgtAS);
326 
327  DOMElement* wgtChi2BS = doc->createElement(cms::xerces::uStr(wgtChi2BeforeSwitch_tag.c_str()).ptr());
328  root->appendChild(wgtChi2BS);
329 
330  DOMElement* wgtChi2AS = doc->createElement(cms::xerces::uStr(wgtChi2AfterSwitch_tag.c_str()).ptr());
331  root->appendChild(wgtChi2AS);
332 
333  write3x10(wgtBS,record);
334  write3x10(wgtAS,record);
335 
336  write10x10(wgtChi2BS,record);
337  write10x10(wgtChi2AS,record);
338 
339  std::string dump = cms::xerces::toString(writer->writeToString( root ));
340  doc->release();
341  doctype->release();
342  writer->release();
343 
344  return dump;
345 }
EcalChi2WeightMatrix & getChi2WeightsBeforeGainSwitch()
Definition: EcalWeightSet.h:31
const std::string EcalWeightSet_tag("EcalWeightSet")
static int readXML(const std::string &filename, EcalCondHeader &header, EcalWeightSet &record)
def dumpXML(db, tag, since, filename='dump.xml')
JetCorrectorParameters::Record record
Definition: classes.h:7
void xercesTerminate()
Definition: Xerces.cc:23
const std::string id_tag("id")
Definition: weight.py:1
const std::string row_tag("row")
void xercesInitialize()
Definition: Xerces.cc:18
const std::string wgtChi2AfterSwitch_tag("Chi2WeightAfterSwitch")
std::string toString(XMLCh const *toTranscode)
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
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")
EcalWeightMatrix & getWeightsAfterGainSwitch()
Definition: EcalWeightSet.h:30
int k[5][pyjets_maxn]
math::Matrix< 10, 10 >::type EcalChi2WeightMatrix
Definition: EcalWeightSet.h:23
EcalWeightMatrix & getWeightsBeforeGainSwitch()
Definition: EcalWeightSet.h:29
const std::string wgtChi2BeforeSwitch_tag("Chi2WeightBeforeSwitch")
xercesc::DOMNode * getChildNode(xercesc::DOMNode *node, const std::string &nodename)
get the child of
const std::string wgtAfterSwitch_tag("WeightAfterSwitch")
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalWeightSet &record)
static std::string dumpXML(const EcalCondHeader &header, const EcalWeightSet &record)
math::Matrix< 3, 10 >::type EcalWeightMatrix
Definition: EcalWeightSet.h:22
static void write3x10(xercesc::DOMElement *node, const EcalWeightSet &record)
static void write10x10(xercesc::DOMElement *node, const EcalWeightSet &record)