File indexing completed on 2024-04-06 12:02:58
0001 #include "CondTools/Ecal/interface/EcalTBWeightsXMLTranslator.h"
0002 #include <xercesc/dom/DOM.hpp>
0003 #include <xercesc/parsers/XercesDOMParser.hpp>
0004 #include "Utilities/Xerces/interface/Xerces.h"
0005 #include "Utilities/Xerces/interface/XercesStrUtils.h"
0006 #include <xercesc/util/XMLString.hpp>
0007 #include <xercesc/sax/SAXException.hpp>
0008 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0009 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0010 #include "CondTools/Ecal/interface/XMLTags.h"
0011 #include <fstream>
0012
0013 using namespace xercesc;
0014 using namespace std;
0015 using namespace xuti;
0016
0017
0018
0019 std::ostream& operator<<(std::ostream& stream_, const EcalXtalGroupId& id_) { return stream_ << id_.id(); }
0020
0021 std::istream& operator>>(std::istream& stream_, EcalXtalGroupId& id_) {
0022 unsigned int id;
0023 stream_ >> id;
0024 id_ = EcalXtalGroupId(id);
0025 return stream_;
0026 }
0027
0028 int EcalTBWeightsXMLTranslator::readXML(const std::string& filename, EcalCondHeader& header, EcalTBWeights& record) {
0029 cms::concurrency::xercesInitialize();
0030
0031 XercesDOMParser* parser = new XercesDOMParser;
0032 parser->setValidationScheme(XercesDOMParser::Val_Never);
0033 parser->setDoNamespaces(false);
0034 parser->setDoSchema(false);
0035
0036 parser->parse(filename.c_str());
0037
0038 DOMDocument* xmlDoc = parser->getDocument();
0039 if (!xmlDoc) {
0040 std::cout << "EcalTBWeightsXMLTranslator::Error parsing document" << std::endl;
0041 return -1;
0042 }
0043
0044 DOMElement* elementRoot = xmlDoc->getDocumentElement();
0045 xuti::readHeader(elementRoot, header);
0046
0047 DOMNode* wnode = getChildNode(elementRoot, EcalTBWeight_tag);
0048
0049 while (wnode) {
0050 DOMNode* gid_node = getChildNode(wnode, EcalXtalGroupId_tag);
0051 DOMNode* tdc_node = getChildNode(wnode, EcalTDCId_tag);
0052 DOMNode* ws_node = getChildNode(wnode, EcalWeightSet_tag);
0053
0054 EcalXtalGroupId gid;
0055 EcalTBWeights::EcalTDCId tid;
0056 EcalWeightSet ws;
0057
0058 GetNodeData(gid_node, gid);
0059 GetNodeData(tdc_node, tid);
0060
0061 readWeightSet(ws_node, ws);
0062
0063 record.setValue(gid, tid, ws);
0064
0065 wnode = wnode->getNextSibling();
0066
0067 while (wnode && wnode->getNodeType() != DOMNode::ELEMENT_NODE)
0068 wnode = wnode->getNextSibling();
0069 }
0070
0071 cms::concurrency::xercesTerminate();
0072
0073 return 0;
0074 }
0075
0076 int EcalTBWeightsXMLTranslator::writeXML(const std::string& filename,
0077 const EcalCondHeader& header,
0078 const EcalTBWeights& record) {
0079 cms::concurrency::xercesInitialize();
0080
0081 std::fstream fs(filename.c_str(), ios::out);
0082 fs << dumpXML(header, record);
0083
0084 cms::concurrency::xercesTerminate();
0085
0086 return 0;
0087 }
0088
0089 void EcalTBWeightsXMLTranslator::readWeightSet(xercesc::DOMNode* parentNode, EcalWeightSet& ws) {
0090
0091 DOMNode* wgtBSnode = getChildNode(parentNode, wgtBeforeSwitch_tag);
0092 DOMNode* wgtASnode = getChildNode(parentNode, wgtAfterSwitch_tag);
0093 DOMNode* wgtChi2BSnode = getChildNode(parentNode, wgtChi2BeforeSwitch_tag);
0094 DOMNode* wgtChi2ASnode = getChildNode(parentNode, wgtChi2AfterSwitch_tag);
0095
0096 DOMNode* rownode = getChildNode(wgtBSnode, row_tag);
0097
0098 DOMElement* rowelement = nullptr;
0099
0100
0101 while (rownode) {
0102 rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
0103
0104 std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
0105
0106 std::stringstream rowid_ss(rowid_s);
0107 int rowid = 0;
0108 rowid_ss >> rowid;
0109
0110 std::string weightrow = cms::xerces::toString(rownode->getTextContent());
0111
0112 std::stringstream weightrow_s(weightrow);
0113 double weight = 0;
0114 int i = 0;
0115 while (weightrow_s >> weight) {
0116 ws.getWeightsBeforeGainSwitch()(rowid, i) = weight;
0117 i++;
0118 }
0119
0120
0121 rownode = rownode->getNextSibling();
0122
0123 while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
0124 rownode = rownode->getNextSibling();
0125 }
0126
0127 rownode = getChildNode(wgtASnode, row_tag);
0128
0129
0130 while (rownode) {
0131 rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
0132
0133 std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
0134
0135 std::stringstream rowid_ss(rowid_s);
0136 int rowid = 0;
0137 rowid_ss >> rowid;
0138
0139 std::string weightrow = cms::xerces::toString(rownode->getTextContent());
0140
0141 std::stringstream weightrow_s(weightrow);
0142 double weight = 0;
0143 int i = 0;
0144 while (weightrow_s >> weight) {
0145 ws.getWeightsAfterGainSwitch()(rowid, i) = weight;
0146 i++;
0147 }
0148
0149
0150 rownode = rownode->getNextSibling();
0151
0152 while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
0153 rownode = rownode->getNextSibling();
0154 }
0155
0156 rownode = getChildNode(wgtChi2BSnode, row_tag);
0157
0158
0159 while (rownode) {
0160 rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
0161 std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
0162
0163 std::stringstream rowid_ss(rowid_s);
0164 int rowid = 0;
0165 rowid_ss >> rowid;
0166
0167 std::string weightrow = cms::xerces::toString(rownode->getTextContent());
0168
0169 std::stringstream weightrow_s(weightrow);
0170 double weight = 0;
0171 int i = 0;
0172 while (weightrow_s >> weight) {
0173 ws.getChi2WeightsBeforeGainSwitch()(rowid, i) = weight;
0174 i++;
0175 }
0176
0177
0178 rownode = rownode->getNextSibling();
0179
0180 while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
0181 rownode = rownode->getNextSibling();
0182 }
0183
0184 rownode = getChildNode(wgtChi2ASnode, row_tag);
0185
0186
0187 while (rownode) {
0188 rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
0189 std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
0190
0191 std::stringstream rowid_ss(rowid_s);
0192 int rowid = 0;
0193 rowid_ss >> rowid;
0194
0195 std::string weightrow = cms::xerces::toString(rownode->getTextContent());
0196
0197 std::stringstream weightrow_s(weightrow);
0198 double weight = 0;
0199 int i = 0;
0200 while (weightrow_s >> weight) {
0201 ws.getChi2WeightsAfterGainSwitch()(rowid, i) = weight;
0202 i++;
0203 }
0204
0205
0206 rownode = rownode->getNextSibling();
0207
0208 while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
0209 rownode = rownode->getNextSibling();
0210 }
0211 }
0212
0213 void EcalTBWeightsXMLTranslator::writeWeightSet(xercesc::DOMNode* parentNode, const EcalWeightSet& ws) {
0214 xercesc::DOMDocument* doc = parentNode->getOwnerDocument();
0215
0216 DOMElement* weightsetel = doc->createElement(cms::xerces::uStr(EcalWeightSet_tag.c_str()).ptr());
0217 parentNode->appendChild(weightsetel);
0218
0219 DOMElement* wgtBS = doc->createElement(cms::xerces::uStr(wgtBeforeSwitch_tag.c_str()).ptr());
0220 weightsetel->appendChild(wgtBS);
0221
0222 DOMElement* wgtAS = doc->createElement(cms::xerces::uStr(wgtAfterSwitch_tag.c_str()).ptr());
0223 weightsetel->appendChild(wgtAS);
0224
0225 DOMElement* wgtChi2BS = doc->createElement(cms::xerces::uStr(wgtChi2BeforeSwitch_tag.c_str()).ptr());
0226 weightsetel->appendChild(wgtChi2BS);
0227
0228 DOMElement* wgtChi2AS = doc->createElement(cms::xerces::uStr(wgtChi2AfterSwitch_tag.c_str()).ptr());
0229 weightsetel->appendChild(wgtChi2AS);
0230
0231 writeWeightMatrix(wgtBS, ws.getWeightsBeforeGainSwitch());
0232 writeWeightMatrix(wgtAS, ws.getWeightsAfterGainSwitch());
0233
0234 writeChi2WeightMatrix(wgtChi2BS, ws.getChi2WeightsBeforeGainSwitch());
0235 writeChi2WeightMatrix(wgtChi2AS, ws.getChi2WeightsBeforeGainSwitch());
0236 }
0237
0238 void EcalTBWeightsXMLTranslator::writeWeightMatrix(xercesc::DOMNode* node,
0239 const EcalWeightSet::EcalWeightMatrix& matrix) {
0240 DOMElement* row = nullptr;
0241 DOMAttr* rowid = nullptr;
0242 DOMText* rowvalue = nullptr;
0243
0244 const int ncols = 10;
0245 const int nrows = 3;
0246
0247 for (int i = 0; i < nrows; ++i) {
0248 row = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
0249 node->appendChild(row);
0250
0251 stringstream value_s;
0252 value_s << i;
0253
0254 rowid = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
0255 rowid->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
0256 row->setAttributeNode(rowid);
0257
0258 stringstream row_s;
0259
0260 for (int k = 0; k < ncols; ++k)
0261 row_s << " " << matrix(i, k) << " ";
0262
0263 rowvalue = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
0264 row->appendChild(rowvalue);
0265 }
0266 }
0267
0268 void EcalTBWeightsXMLTranslator::writeChi2WeightMatrix(xercesc::DOMNode* node,
0269 const EcalWeightSet::EcalChi2WeightMatrix& matrix) {
0270 DOMElement* row = nullptr;
0271 DOMAttr* rowid = nullptr;
0272 DOMText* rowvalue = nullptr;
0273
0274 const int ncols = 10;
0275 const int nrows = 10;
0276
0277 for (int i = 0; i < nrows; ++i) {
0278 row = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
0279 node->appendChild(row);
0280
0281 stringstream value_s;
0282 value_s << i;
0283
0284 rowid = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
0285 rowid->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
0286 row->setAttributeNode(rowid);
0287
0288 stringstream row_s;
0289
0290 for (int k = 0; k < ncols; ++k)
0291 row_s << " " << matrix(i, k) << " ";
0292
0293 rowvalue = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
0294 row->appendChild(rowvalue);
0295 }
0296 }
0297
0298 std::string EcalTBWeightsXMLTranslator::dumpXML(const EcalCondHeader& header, const EcalTBWeights& record) {
0299 cms::concurrency::xercesInitialize();
0300
0301 unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0302
0303 DOMLSSerializer* writer = impl->createLSSerializer();
0304 if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0305 writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0306
0307 DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0308 DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(EcalTBWeights_tag.c_str()).ptr(), doctype);
0309 DOMElement* root = doc->getDocumentElement();
0310
0311 xuti::writeHeader(root, header);
0312
0313 const EcalTBWeights::EcalTBWeightMap& wmap = record.getMap();
0314
0315 EcalTBWeights::EcalTBWeightMap::const_iterator it;
0316 for (it = wmap.begin(); it != wmap.end(); ++it) {
0317 DOMElement* tbweight = doc->createElement(cms::xerces::uStr(EcalTBWeight_tag.c_str()).ptr());
0318 root->appendChild(tbweight);
0319
0320 EcalXtalGroupId gid = it->first.first;
0321 EcalTBWeights::EcalTDCId tid = it->first.second;
0322 EcalWeightSet ws = it->second;
0323
0324 WriteNodeWithValue(tbweight, EcalXtalGroupId_tag, gid);
0325 WriteNodeWithValue(tbweight, EcalTDCId_tag, tid);
0326 writeWeightSet(tbweight, ws);
0327
0328 }
0329
0330 std::string dump = cms::xerces::toString(writer->writeToString(root));
0331 doc->release();
0332 doctype->release();
0333 writer->release();
0334
0335 return dump;
0336 }