Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:58

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   /*    old method for DBv1
0028   cms::concurrency::xercesInitialize();
0029 
0030   XercesDOMParser* parser = new XercesDOMParser;
0031   parser->setValidationScheme( XercesDOMParser::Val_Never );
0032   parser->setDoNamespaces( false );
0033   parser->setDoSchema( false );
0034   
0035   parser->parse(filename.c_str());
0036 
0037   DOMDocument* xmlDoc = parser->getDocument();
0038   
0039 
0040   if (!xmlDoc) {
0041     std::cout << "EcalFloatCondObjectContainerXMLTranslator::Error parsing document" << std::endl;
0042     return -1;
0043   }
0044 
0045   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0046 
0047   xuti::readHeader(elementRoot, header);
0048 
0049   // get the first cell node
0050   DOMNode * cellnode=getChildNode(elementRoot,Cell_tag);
0051   
0052   // loop on cell nodes
0053   while  (cellnode){
0054 
0055     float val=0;
0056 
0057     // read id
0058     DetId detid= readCellId(dynamic_cast<DOMElement*>(cellnode));
0059        
0060     // read value
0061     DOMNode * c_node = getChildNode(cellnode,Value_tag);
0062     GetNodeData(c_node,val);
0063 
0064     // fill record
0065     record[detid]=val;
0066 
0067     // get next cell
0068     cellnode= cellnode->getNextSibling();
0069     
0070     while (cellnode&& cellnode->getNodeType( ) != DOMNode::ELEMENT_NODE)      
0071       cellnode= cellnode->getNextSibling();
0072     
0073     
0074   }
0075 
0076 
0077   delete parser;
0078   cms::concurrency::xercesTerminate();
0079   */
0080   // new method for DBv2
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   // header
0089   for (int i = 0; i < 6; i++) {
0090     getline(fxml, dummyLine);  // skip first lines
0091     //  std::cout << dummyLine << std::endl;
0092   }
0093   fxml >> bid;
0094   //  std::cout << bid << std::endl;
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;  // <item_version>0</item_version>
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);  // skip first lines
0114     //  std::cout << dummyLine << std::endl;
0115   }
0116   fxml >> bid;
0117   //    cout << bid << endl;
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;  // <item_version>0</item_version>
0127   // now endcaps
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) {  // loop on EB cells
0174 
0175     uint32_t rawid = EBDetId::unhashIndex(cellid);
0176     record[rawid] = eb[cellid];
0177   }
0178 
0179   for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {  // loop on EE cells
0180 
0181     if (EEDetId::validHashIndex(cellid)) {
0182       uint32_t rawid = EEDetId::unhashIndex(cellid);
0183 
0184       record[rawid] = ee[cellid];
0185     }  // if
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) {  // loop on EB cells
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;  // cell absent from original record
0214 
0215     DOMElement* cellnode = writeCell(root, rawid);
0216 
0217     WriteNodeWithValue(cellnode, Value_tag, *value_ptr);
0218 
0219   }  // loop on EB cells
0220 
0221   for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {  // loop on EE cells
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;  // cell absent from original record
0230 
0231     DOMElement* cellnode = writeCell(root, rawid);
0232     WriteNodeWithValue(cellnode, Value_tag, *value_ptr);
0233 
0234   }  // loop on EE cells
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 }