File indexing completed on 2023-03-17 10:48:03
0001 #include <iostream>
0002 #include <sstream>
0003 #include <fstream>
0004
0005 #include <xercesc/dom/DOMNode.hpp>
0006 #include <xercesc/dom/DOM.hpp>
0007 #include <xercesc/parsers/XercesDOMParser.hpp>
0008 #include "Utilities/Xerces/interface/Xerces.h"
0009 #include "Utilities/Xerces/interface/XercesStrUtils.h"
0010 #include <xercesc/util/XMLString.hpp>
0011 #include <xercesc/sax/SAXException.hpp>
0012 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0013
0014 #include "CondTools/Ecal/interface/EcalFloatCondObjectContainerXMLTranslator.h"
0015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0016
0017 using namespace XERCES_CPP_NAMESPACE;
0018 using namespace xuti;
0019 using namespace std;
0020
0021 const int kEBChannels = 61200, kEEChannels = 14648;
0022
0023 int EcalFloatCondObjectContainerXMLTranslator::readXML(const string& filename,
0024 EcalCondHeader& header,
0025 EcalFloatCondObjectContainer& record) {
0026 std::cout << "EcalFloatCondObjectContainerXMLTranslatorr::readXML filename " << filename << std::endl;
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081 std::string dummyLine, bid;
0082 std::ifstream fxml;
0083 fxml.open(filename);
0084 if (!fxml.is_open()) {
0085 std::cout << "ERROR : cannot open file " << filename << std::endl;
0086 exit(1);
0087 }
0088
0089 for (int i = 0; i < 6; i++) {
0090 getline(fxml, dummyLine);
0091
0092 }
0093 fxml >> bid;
0094
0095 std::string stt = bid.substr(7, 5);
0096 std::istringstream iEB(stt);
0097 int nEB;
0098 iEB >> nEB;
0099 if (nEB != kEBChannels) {
0100 std::cout << " strange number of EB channels " << nEB << std::endl;
0101 exit(-1);
0102 }
0103 fxml >> bid;
0104 for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
0105 EBDetId myEBDetId = EBDetId::unhashIndex(iChannel);
0106 fxml >> bid;
0107 std::size_t found = bid.find("</");
0108 stt = bid.substr(6, found - 6);
0109 float val = std::stof(stt);
0110 record[myEBDetId] = val;
0111 }
0112 for (int i = 0; i < 5; i++) {
0113 getline(fxml, dummyLine);
0114
0115 }
0116 fxml >> bid;
0117
0118 stt = bid.substr(7, 5);
0119 std::istringstream iEE(stt);
0120 int nEE;
0121 iEE >> nEE;
0122 if (nEE != kEEChannels) {
0123 std::cout << " strange number of EE channels " << nEE << std::endl;
0124 exit(-1);
0125 }
0126 fxml >> bid;
0127
0128 for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
0129 EEDetId myEEDetId = EEDetId::unhashIndex(iChannel);
0130 fxml >> bid;
0131 std::size_t found = bid.find("</");
0132 stt = bid.substr(6, found - 6);
0133 float val = std::stof(stt);
0134 record[myEEDetId] = val;
0135 }
0136
0137 return 0;
0138 }
0139
0140 std::vector<float> EcalFloatCondObjectContainerXMLTranslator::barrelfromXML(const string& filename) {
0141 EcalCondHeader header;
0142 EcalFloatCondObjectContainer record;
0143 readXML(filename, header, record);
0144
0145 return record.barrelItems();
0146 }
0147
0148 std::vector<float> EcalFloatCondObjectContainerXMLTranslator::endcapfromXML(const string& filename) {
0149 EcalCondHeader header;
0150 EcalFloatCondObjectContainer record;
0151 readXML(filename, header, record);
0152
0153 return record.endcapItems();
0154 }
0155
0156 std::string EcalFloatCondObjectContainerXMLTranslator::dumpXML(const EcalCondHeader& header,
0157 const std::vector<float>& eb,
0158 const std::vector<float>& ee) {
0159 if (eb.size() != EBDetId::kSizeForDenseIndexing) {
0160 std::cerr << "Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Barrel array size: " << eb.size()
0161 << " should be " << EBDetId::kSizeForDenseIndexing << std::endl;
0162 return std::string("");
0163 }
0164
0165 if (ee.size() != EEDetId::kSizeForDenseIndexing) {
0166 std::cerr << "Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Endcap array size: " << ee.size()
0167 << " should be " << EEDetId::kSizeForDenseIndexing << std::endl;
0168 return std::string("");
0169 }
0170
0171 EcalFloatCondObjectContainer record;
0172
0173 for (int cellid = 0; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
0174
0175 uint32_t rawid = EBDetId::unhashIndex(cellid);
0176 record[rawid] = eb[cellid];
0177 }
0178
0179 for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
0180
0181 if (EEDetId::validHashIndex(cellid)) {
0182 uint32_t rawid = EEDetId::unhashIndex(cellid);
0183
0184 record[rawid] = ee[cellid];
0185 }
0186 }
0187
0188 return dumpXML(header, record);
0189 }
0190
0191 std::string EcalFloatCondObjectContainerXMLTranslator::dumpXML(const EcalCondHeader& header,
0192 const EcalFloatCondObjectContainer& record) {
0193 cms::concurrency::xercesInitialize();
0194
0195 unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0196
0197 DOMLSSerializer* writer = impl->createLSSerializer();
0198 if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0199 writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0200
0201 DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0202 DOMDocument* doc =
0203 impl->createDocument(nullptr, cms::xerces::uStr(EcalFloatCondObjectContainer_tag.c_str()).ptr(), doctype);
0204 DOMElement* root = doc->getDocumentElement();
0205
0206 xuti::writeHeader(root, header);
0207
0208 for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
0209
0210 uint32_t rawid = EBDetId::unhashIndex(cellid);
0211 EcalFloatCondObjectContainer::const_iterator value_ptr = record.find(rawid);
0212 if (value_ptr == record.end())
0213 continue;
0214
0215 DOMElement* cellnode = writeCell(root, rawid);
0216
0217 WriteNodeWithValue(cellnode, Value_tag, *value_ptr);
0218
0219 }
0220
0221 for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
0222
0223 if (!EEDetId::validHashIndex(cellid))
0224 continue;
0225
0226 uint32_t rawid = EEDetId::unhashIndex(cellid);
0227 EcalFloatCondObjectContainer::const_iterator value_ptr = record.find(rawid);
0228 if (value_ptr == record.end())
0229 continue;
0230
0231 DOMElement* cellnode = writeCell(root, rawid);
0232 WriteNodeWithValue(cellnode, Value_tag, *value_ptr);
0233
0234 }
0235
0236 std::string dump = cms::xerces::toString(writer->writeToString(root));
0237 doc->release();
0238 doctype->release();
0239 writer->release();
0240
0241 cms::concurrency::xercesTerminate();
0242
0243 return dump;
0244 }
0245
0246 int EcalFloatCondObjectContainerXMLTranslator::writeXML(const std::string& filename,
0247 const EcalCondHeader& header,
0248 const EcalFloatCondObjectContainer& record) {
0249 cms::concurrency::xercesInitialize();
0250
0251 std::fstream fs(filename.c_str(), ios::out);
0252 fs << dumpXML(header, record);
0253
0254 cms::concurrency::xercesTerminate();
0255
0256 return 0;
0257 }