CMS 3D CMS Logo

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