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 
23 
24  XercesDOMParser* parser = new XercesDOMParser;
25  parser->setValidationScheme(XercesDOMParser::Val_Never);
26  parser->setDoNamespaces(false);
27  parser->setDoSchema(false);
28 
29  parser->parse(filename.c_str());
30 
31  DOMDocument* xmlDoc = parser->getDocument();
32  if (!xmlDoc) {
33  std::cout << "EcalWeightSetXMLTranslator::Error parsing document" << std::endl;
34  return -1;
35  }
36 
37  DOMElement* elementRoot = xmlDoc->getDocumentElement();
38 
39  xuti::readHeader(elementRoot, header);
40  // get the first cell node
41  DOMNode* wgtBSnode = getChildNode(elementRoot, wgtBeforeSwitch_tag);
42  DOMNode* wgtASnode = getChildNode(elementRoot, wgtAfterSwitch_tag);
43  DOMNode* wgtChi2BSnode = getChildNode(elementRoot, wgtChi2BeforeSwitch_tag);
44  DOMNode* wgtChi2ASnode = getChildNode(elementRoot, wgtChi2AfterSwitch_tag);
45 
46  DOMNode* rownode = getChildNode(wgtBSnode, row_tag);
47 
48  DOMElement* rowelement;
49 
50  // loop on row nodes
51  while (rownode) {
52  rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
53 
54  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
55 
56  std::stringstream rowid_ss(rowid_s);
57  int rowid = 0;
58  rowid_ss >> rowid;
59 
60  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
61 
62  std::stringstream weightrow_s(weightrow);
63  double weight = 0;
64  int i = 0;
65  while (weightrow_s >> weight) {
66  record.getWeightsBeforeGainSwitch()(i, rowid) = weight;
67  i++;
68  }
69 
70  // get next cell
71  rownode = rownode->getNextSibling();
72 
73  while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
74  rownode = rownode->getNextSibling();
75  }
76 
77  rownode = getChildNode(wgtASnode, row_tag);
78 
79  // loop on row nodes
80  while (rownode) {
81  rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
82 
83  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
84 
85  std::stringstream rowid_ss(rowid_s);
86  int rowid = 0;
87  rowid_ss >> rowid;
88 
89  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
90 
91  std::stringstream weightrow_s(weightrow);
92  double weight = 0;
93  int i = 0;
94  while (weightrow_s >> weight) {
95  record.getWeightsAfterGainSwitch()(i, rowid) = weight;
96  i++;
97  }
98 
99  // get next cell
100  rownode = rownode->getNextSibling();
101 
102  while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
103  rownode = rownode->getNextSibling();
104  }
105 
106  rownode = getChildNode(wgtChi2BSnode, row_tag);
107 
108  // loop on row nodes
109  while (rownode) {
110  rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
111  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
112 
113  std::stringstream rowid_ss(rowid_s);
114  int rowid = 0;
115  rowid_ss >> rowid;
116 
117  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
118 
119  std::stringstream weightrow_s(weightrow);
120  double weight = 0;
121  int i = 0;
122  while (weightrow_s >> weight) {
123  record.getChi2WeightsBeforeGainSwitch()(i, rowid) = weight;
124  i++;
125  }
126 
127  // get next cell
128  rownode = rownode->getNextSibling();
129 
130  while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
131  rownode = rownode->getNextSibling();
132  }
133 
134  rownode = getChildNode(wgtChi2ASnode, row_tag);
135 
136  // loop on row nodes
137  while (rownode) {
138  rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
139  std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
140 
141  std::stringstream rowid_ss(rowid_s);
142  int rowid = 0;
143  rowid_ss >> rowid;
144 
145  std::string weightrow = cms::xerces::toString(rownode->getTextContent());
146 
147  std::stringstream weightrow_s(weightrow);
148  double weight = 0;
149  int i = 0;
150  while (weightrow_s >> weight) {
151  record.getChi2WeightsAfterGainSwitch()(i, rowid) = weight;
152  i++;
153  }
154 
155  // get next cell
156  rownode = rownode->getNextSibling();
157 
158  while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
159  rownode = rownode->getNextSibling();
160  }
161 
162  delete parser;
164  return 0;
165 }
166 
168  const EcalCondHeader& header,
169  const EcalWeightSet& record) {
171 
172  std::fstream fs(filename.c_str(), ios::out);
173  fs << dumpXML(header, record);
174 
176 
177  return 0;
178 }
179 
180 void EcalWeightSetXMLTranslator::write10x10(xercesc::DOMElement* node, const EcalWeightSet& record) {
181  DOMElement* row[10];
182  DOMAttr* rowid[10];
183  DOMText* rowvalue[10];
185 
186  if (cms::xerces::toString(node->getNodeName()) == wgtChi2BeforeSwitch_tag) {
187  echi2wmatrix = record.getChi2WeightsBeforeGainSwitch();
188  }
189  if (cms::xerces::toString(node->getNodeName()) == wgtChi2AfterSwitch_tag) {
190  echi2wmatrix = record.getChi2WeightsAfterGainSwitch();
191  }
192 
193  for (int i = 0; i < 10; ++i) {
194  row[i] = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
195  node->appendChild(row[i]);
196 
197  stringstream value_s;
198  value_s << i;
199 
200  rowid[i] = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
201  rowid[i]->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
202  row[i]->setAttributeNode(rowid[i]);
203 
204  stringstream row_s;
205 
206  for (int k = 0; k < 10; ++k) {
207  row_s << " ";
208  row_s << echi2wmatrix(k, i);
209  row_s << " ";
210  } //for loop on element
211 
212  rowvalue[i] = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
213  row[i]->appendChild(rowvalue[i]);
214  } //for loop on row
215 }
216 
217 void EcalWeightSetXMLTranslator::write3x10(xercesc::DOMElement* node, const EcalWeightSet& record) {
218  DOMElement* row[10];
219  DOMAttr* rowid[10];
220  DOMText* rowvalue[10];
222 
223  if (cms::xerces::toString(node->getNodeName()) == wgtBeforeSwitch_tag)
224  ewmatrix = record.getWeightsBeforeGainSwitch();
225 
226  if (cms::xerces::toString(node->getNodeName()) == wgtAfterSwitch_tag)
227  ewmatrix = record.getWeightsAfterGainSwitch();
228 
229  for (int i = 0; i < 10; ++i) {
230  row[i] = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
231  node->appendChild(row[i]);
232 
233  stringstream value_s;
234  value_s << i;
235 
236  rowid[i] = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
237 
238  rowid[i]->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
239 
240  row[i]->setAttributeNode(rowid[i]);
241 
242  stringstream row_s;
243 
244  for (int k = 0; k < 3; ++k) {
245  row_s << " ";
246  row_s << ewmatrix(k, i);
247  row_s << " ";
248  } //for loop on element
249 
250  rowvalue[i] = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
251  row[i]->appendChild(rowvalue[i]);
252  } //for loop on row
253 }
254 
256  unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
257 
258  DOMLSSerializer* writer = impl->createLSSerializer();
259  if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
260  writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
261 
262  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
263  DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(EcalWeightSet_tag.c_str()).ptr(), doctype);
264 
265  DOMElement* root = doc->getDocumentElement();
266 
268 
269  DOMElement* wgtBS = doc->createElement(cms::xerces::uStr(wgtBeforeSwitch_tag.c_str()).ptr());
270  root->appendChild(wgtBS);
271 
272  DOMElement* wgtAS = doc->createElement(cms::xerces::uStr(wgtAfterSwitch_tag.c_str()).ptr());
273  root->appendChild(wgtAS);
274 
275  DOMElement* wgtChi2BS = doc->createElement(cms::xerces::uStr(wgtChi2BeforeSwitch_tag.c_str()).ptr());
276  root->appendChild(wgtChi2BS);
277 
278  DOMElement* wgtChi2AS = doc->createElement(cms::xerces::uStr(wgtChi2AfterSwitch_tag.c_str()).ptr());
279  root->appendChild(wgtChi2AS);
280 
281  write3x10(wgtBS, record);
282  write3x10(wgtAS, record);
283 
284  write10x10(wgtChi2BS, record);
285  write10x10(wgtChi2AS, record);
286 
287  std::string dump = cms::xerces::toString(writer->writeToString(root));
288  doc->release();
289  doctype->release();
290  writer->release();
291 
292  return dump;
293 }
xuti::getChildNode
xercesc::DOMNode * getChildNode(xercesc::DOMNode *node, const std::string &nodename)
get the child of
XercesStrUtils.h
EcalWeightSetXMLTranslator::write10x10
static void write10x10(xercesc::DOMElement *node, const EcalWeightSet &record)
Definition: EcalWeightSetXMLTranslator.cc:180
common_cff.doc
doc
Definition: common_cff.py:54
mps_fire.i
i
Definition: mps_fire.py:355
EcalCondTools.dumpXML
def dumpXML(db, tag, since, filename='dump.xml')
Definition: EcalCondTools.py:44
xuti::writeHeader
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
Definition: DOMHelperFunctions.cc:151
xuti::wgtChi2BeforeSwitch_tag
const std::string wgtChi2BeforeSwitch_tag("Chi2WeightBeforeSwitch")
EcalWeightSetXMLTranslator.h
gather_cfg.cout
cout
Definition: gather_cfg.py:144
GlobalPosition_Frontier_DevDB_cff.record
record
Definition: GlobalPosition_Frontier_DevDB_cff.py:10
writedatasetfile.parser
parser
Definition: writedatasetfile.py:7
cscNeutronWriter_cfi.writer
writer
Definition: cscNeutronWriter_cfi.py:6
xuti::wgtAfterSwitch_tag
const std::string wgtAfterSwitch_tag("WeightAfterSwitch")
EcalWeightSet::EcalWeightMatrix
math::Matrix< 3, 10 >::type EcalWeightMatrix
Definition: EcalWeightSet.h:19
cms::xerces::toString
std::string toString(XMLCh const *toTranscode)
Definition: XercesStrUtils.h:34
xuti::id_tag
const std::string id_tag("id")
xuti::wgtBeforeSwitch_tag
const std::string wgtBeforeSwitch_tag("WeightBeforeSwitch")
XERCES_CPP_NAMESPACE
Definition: XmlConfigParser.h:12
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
dqmdumpme.k
k
Definition: dqmdumpme.py:60
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
DOMHelperFunctions.h
EcalCondHeader
Definition: EcalCondHeader.h:16
EcalWeightSet::EcalChi2WeightMatrix
math::Matrix< 10, 10 >::type EcalChi2WeightMatrix
Definition: EcalWeightSet.h:20
EcalWeightSetXMLTranslator::writeXML
static int writeXML(const std::string &filename, const EcalCondHeader &header, const EcalWeightSet &record)
Definition: EcalWeightSetXMLTranslator.cc:167
FrontierConditions_GlobalTag_cff.dump
dump
Definition: FrontierConditions_GlobalTag_cff.py:12
Xerces.h
cms::concurrency::xercesTerminate
void xercesTerminate()
Definition: Xerces.cc:23
xuti
Definition: DOMHelperFunctions.h:21
root
Definition: RooFitFunction.h:10
xuti::EcalWeightSet_tag
const std::string EcalWeightSet_tag("EcalWeightSet")
cms::xerces::uStr
ZStr< XMLCh > uStr(char const *str)
Definition: XercesStrUtils.h:30
EcalWeightSet
Definition: EcalWeightSet.h:17
impl
Definition: trackAlgoPriorityOrder.h:18
std
Definition: JetResolutionObject.h:76
EcalWeightSetXMLTranslator::dumpXML
static std::string dumpXML(const EcalCondHeader &header, const EcalWeightSet &record)
Definition: EcalWeightSetXMLTranslator.cc:255
RecoTauValidation_cfi.header
header
Definition: RecoTauValidation_cfi.py:292
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
EcalWeightSetXMLTranslator::write3x10
static void write3x10(xercesc::DOMElement *node, const EcalWeightSet &record)
Definition: EcalWeightSetXMLTranslator.cc:217
XMLTags.h
cms::concurrency::xercesInitialize
void xercesInitialize()
Definition: Xerces.cc:18
xuti::readHeader
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
Definition: DOMHelperFunctions.cc:164
xuti::wgtChi2AfterSwitch_tag
const std::string wgtChi2AfterSwitch_tag("Chi2WeightAfterSwitch")
EcalWeightSetXMLTranslator::readXML
static int readXML(const std::string &filename, EcalCondHeader &header, EcalWeightSet &record)
Definition: EcalWeightSetXMLTranslator.cc:21
weight
Definition: weight.py:1
xuti::row_tag
const std::string row_tag("row")