test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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>
8 #include <xercesc/util/XMLString.hpp>
9 #include <xercesc/sax/SAXException.hpp>
10 #include <xercesc/framework/LocalFileFormatTarget.hpp>
11 
12 
13 
17 
18 
19 using namespace XERCES_CPP_NAMESPACE;
20 using namespace xuti;
21 using namespace std;
22 
23 
24 
26  EcalCondHeader& header,
28 
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 << "EcalWeightSetXMLTranslator::Error parsing document" << std::endl;
41  return -1;
42  }
43 
44  DOMElement* elementRoot = xmlDoc->getDocumentElement();
45 
46  xuti::readHeader(elementRoot, header);
47  // get the first cell node
48  DOMNode * wgtBSnode=getChildNode(elementRoot,wgtBeforeSwitch_tag);
49  DOMNode * wgtASnode=getChildNode(elementRoot,wgtAfterSwitch_tag);
50  DOMNode * wgtChi2BSnode=getChildNode(elementRoot,wgtChi2BeforeSwitch_tag);
51  DOMNode * wgtChi2ASnode=getChildNode(elementRoot,wgtChi2AfterSwitch_tag);
52 
53 
54  DOMNode* rownode = getChildNode(wgtBSnode,row_tag);
55 
56  DOMElement* rowelement;
57 
58  // loop on row nodes
59  while (rownode){
60 
61  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
62 
63  std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
64 
65  std::stringstream rowid_ss(rowid_s);
66  int rowid = 0;
67  rowid_ss >> rowid;
68 
69  std::string weightrow = toNative(rownode->getTextContent());
70 
71  std::stringstream weightrow_s(weightrow);
72  double weight = 0;
73  int i = 0;
74  while(weightrow_s >> weight)
75  {
76  record.getWeightsBeforeGainSwitch()(i,rowid)= weight;
77  i++;
78  }
79 
80 
81  // get next cell
82  rownode = rownode->getNextSibling();
83 
84  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
85  rownode = rownode->getNextSibling();
86 
87 
88  }
89 
90  rownode = getChildNode(wgtASnode,row_tag);
91 
92  // loop on row nodes
93  while (rownode){
94 
95  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
96 
97  std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
98 
99  std::stringstream rowid_ss(rowid_s);
100  int rowid = 0;
101  rowid_ss >> rowid;
102 
103  std::string weightrow = toNative(rownode->getTextContent());
104 
105  std::stringstream weightrow_s(weightrow);
106  double weight = 0;
107  int i = 0;
108  while(weightrow_s >> weight)
109  {
110  record.getWeightsAfterGainSwitch()(i,rowid)= weight;
111  i++;
112  }
113 
114 
115  // get next cell
116  rownode = rownode->getNextSibling();
117 
118  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
119  rownode = rownode->getNextSibling();
120 
121 
122  }
123 
124  rownode = getChildNode(wgtChi2BSnode,row_tag);
125 
126 
127  // loop on row nodes
128  while (rownode){
129 
130  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
131  std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
132 
133  std::stringstream rowid_ss(rowid_s);
134  int rowid = 0;
135  rowid_ss >> rowid;
136 
137  std::string weightrow = toNative(rownode->getTextContent());
138 
139  std::stringstream weightrow_s(weightrow);
140  double weight = 0;
141  int i = 0;
142  while(weightrow_s >> weight)
143  {
144  record.getChi2WeightsBeforeGainSwitch()(i,rowid)= weight;
145  i++;
146  }
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  }
157 
158 
159  rownode = getChildNode(wgtChi2ASnode,row_tag);
160 
161 
162  // loop on row nodes
163  while (rownode){
164 
165  rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
166  std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
167 
168  std::stringstream rowid_ss(rowid_s);
169  int rowid = 0;
170  rowid_ss >> rowid;
171 
172  std::string weightrow = toNative(rownode->getTextContent());
173 
174  std::stringstream weightrow_s(weightrow);
175  double weight = 0;
176  int i = 0;
177  while(weightrow_s >> weight)
178  {
179  record.getChi2WeightsAfterGainSwitch()(i,rowid)= weight;
180  i++;
181  }
182 
183 
184  // get next cell
185  rownode = rownode->getNextSibling();
186 
187  while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
188  rownode = rownode->getNextSibling();
189 
190 
191  }
192 
193  delete parser;
195  return 0;
196 
197 }
198 
199 
200 
201 
202 
204  const EcalCondHeader& header,
205  const EcalWeightSet& record){
206 
207  std::fstream fs(filename.c_str(),ios::out);
208  fs<< dumpXML(header,record);
209  return 0;
210 
211 }
212 
213 
214 
215 
216 void EcalWeightSetXMLTranslator::write10x10(xercesc::DOMElement* node,
217  const EcalWeightSet& record){
218 
219  DOMElement* row[10];
220  DOMAttr* rowid[10];
221  DOMText* rowvalue[10];
223 
224 
225  if(toNative(node->getNodeName()) == wgtChi2BeforeSwitch_tag)
226  {
227  echi2wmatrix = record.getChi2WeightsBeforeGainSwitch();
228 
229  }
230  if(toNative(node->getNodeName()) == wgtChi2AfterSwitch_tag)
231  {
232  echi2wmatrix = record.getChi2WeightsAfterGainSwitch();
233 
234  }
235 
236  for(int i=0;i<10;++i)
237  {
238 
239 
240  row[i] = node->getOwnerDocument()->createElement(fromNative(row_tag).c_str());
241  node->appendChild(row[i]);
242 
243  stringstream value_s;
244  value_s << i;
245 
246  rowid[i] = node->getOwnerDocument()->createAttribute(fromNative(id_tag).c_str());
247  rowid[i]->setValue(fromNative(value_s.str()).c_str());
248  row[i]->setAttributeNode(rowid[i]);
249 
250  stringstream row_s;
251 
252  for(int k=0;k<10;++k)
253  {
254  row_s << " ";
255  row_s << echi2wmatrix(k,i);
256  row_s << " ";
257  }//for loop on element
258 
259 
260  rowvalue[i] = node->getOwnerDocument()->createTextNode(fromNative(row_s.str()).c_str());
261  row[i]->appendChild(rowvalue[i]);
262  }//for loop on row
263 }
264 
265 
266 
267 void EcalWeightSetXMLTranslator::write3x10(xercesc::DOMElement* node,
268  const EcalWeightSet& record){
269 
270  DOMElement* row[10];
271  DOMAttr* rowid[10];
272  DOMText* rowvalue[10];
274 
275 
276  if(toNative(node->getNodeName()) == wgtBeforeSwitch_tag)
277  ewmatrix = record.getWeightsBeforeGainSwitch();
278 
279  if(toNative(node->getNodeName()) == wgtAfterSwitch_tag)
280  ewmatrix = record.getWeightsAfterGainSwitch();
281 
282 
283  for(int i=0;i<10;++i)
284  {
285 
286  row[i] = node->getOwnerDocument()->createElement(fromNative(row_tag).c_str());
287  node->appendChild(row[i]);
288 
289  stringstream value_s;
290  value_s << i;
291 
292  rowid[i] = node->getOwnerDocument()->createAttribute(fromNative(id_tag).c_str());
293 
294  rowid[i]->setValue(fromNative(value_s.str()).c_str());
295 
296  row[i]->setAttributeNode(rowid[i]);
297 
298 
299  stringstream row_s;
300 
301  for(int k=0;k<3;++k)
302  {
303  row_s << " ";
304  row_s << ewmatrix(k,i);
305  row_s << " ";
306  }//for loop on element
307 
308 
309  rowvalue[i] = node->getOwnerDocument()->createTextNode(fromNative(row_s.str()).c_str());
310  row[i]->appendChild(rowvalue[i]);
311  }//for loop on row
312 }
313 
314 
316  const EcalWeightSet& record){
317 
318 
320 
321  DOMImplementation* impl =
322  DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
323 
324  DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
325  writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
326 
327  DOMDocumentType* doctype = impl->createDocumentType( fromNative("XML").c_str(), 0, 0 );
328  DOMDocument * doc =
329  impl->createDocument( 0,fromNative(EcalWeightSet_tag).c_str(), doctype );
330 
331 
332  doc->setEncoding(fromNative("UTF-8").c_str() );
333  doc->setStandalone(true);
334  doc->setVersion(fromNative("1.0").c_str() );
335 
336  DOMElement* root = doc->getDocumentElement();
337 
338  xuti::writeHeader(root, header);
339 
340  DOMElement* wgtBS = doc->createElement(fromNative(wgtBeforeSwitch_tag).c_str());
341  root->appendChild(wgtBS);
342 
343  DOMElement* wgtAS = doc->createElement(fromNative(wgtAfterSwitch_tag).c_str());
344  root->appendChild(wgtAS);
345 
346  DOMElement* wgtChi2BS = doc->createElement(fromNative(wgtChi2BeforeSwitch_tag).c_str());
347  root->appendChild(wgtChi2BS);
348 
349  DOMElement* wgtChi2AS = doc->createElement(fromNative(wgtChi2AfterSwitch_tag).c_str());
350  root->appendChild(wgtChi2AS);
351 
352  write3x10(wgtBS,record);
353  write3x10(wgtAS,record);
354 
355  write10x10(wgtChi2BS,record);
356  write10x10(wgtChi2AS,record);
357 
358  std::string dump= toNative(writer->writeToString(*root));
359  doc->release();
360 
361  return dump;
362 
363 
364 }
EcalChi2WeightMatrix & getChi2WeightsBeforeGainSwitch()
Definition: EcalWeightSet.h:31
int i
Definition: DBlmapReader.cc:9
const std::string EcalWeightSet_tag("EcalWeightSet")
static int readXML(const std::string &filename, EcalCondHeader &header, EcalWeightSet &record)
JetCorrectorParameters::Record record
Definition: classes.h:7
void xercesTerminate()
Definition: Xerces.cc:22
const std::string id_tag("id")
const std::string row_tag("row")
void xercesInitialize()
Definition: Xerces.cc:17
const std::string wgtChi2AfterSwitch_tag("Chi2WeightAfterSwitch")
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
const std::string wgtBeforeSwitch_tag("WeightBeforeSwitch")
EcalWeightMatrix & getWeightsAfterGainSwitch()
Definition: EcalWeightSet.h:30
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)
tuple filename
Definition: lut2db_cfg.py:20
XercesString fromNative(const char *str)
Definition: XercesString.h:31
math::Matrix< 3, 10 >::type EcalWeightMatrix
Definition: EcalWeightSet.h:22
static void write3x10(xercesc::DOMElement *node, const EcalWeightSet &record)
tuple cout
Definition: gather_cfg.py:145
static void write10x10(xercesc::DOMElement *node, const EcalWeightSet &record)
int weight
Definition: histoStyle.py:50
std::string toNative(const XMLCh *str)
Definition: XercesString.h:42