Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
#include <iostream>
#include <sstream>
#include <fstream>

#include <xercesc/dom/DOMNode.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include "Utilities/Xerces/interface/Xerces.h"
#include "Utilities/Xerces/interface/XercesStrUtils.h"
#include <xercesc/util/XMLString.hpp>
#include <xercesc/sax/SAXException.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>

#include "CondTools/Ecal/interface/EcalFloatCondObjectContainerXMLTranslator.h"
#include "CondTools/Ecal/interface/DOMHelperFunctions.h"

using namespace XERCES_CPP_NAMESPACE;
using namespace xuti;
using namespace std;

const int kEBChannels = 61200, kEEChannels = 14648;

int EcalFloatCondObjectContainerXMLTranslator::readXML(const string& filename,
                                                       EcalCondHeader& header,
                                                       EcalFloatCondObjectContainer& record) {
  std::cout << "EcalFloatCondObjectContainerXMLTranslatorr::readXML filename " << filename << std::endl;
  /*    old method for DBv1
  cms::concurrency::xercesInitialize();

  XercesDOMParser* parser = new XercesDOMParser;
  parser->setValidationScheme( XercesDOMParser::Val_Never );
  parser->setDoNamespaces( false );
  parser->setDoSchema( false );
  
  parser->parse(filename.c_str());

  DOMDocument* xmlDoc = parser->getDocument();
  

  if (!xmlDoc) {
    std::cout << "EcalFloatCondObjectContainerXMLTranslator::Error parsing document" << std::endl;
    return -1;
  }

  DOMElement* elementRoot = xmlDoc->getDocumentElement();

  xuti::readHeader(elementRoot, header);

  // get the first cell node
  DOMNode * cellnode=getChildNode(elementRoot,Cell_tag);
  
  // loop on cell nodes
  while  (cellnode){

    float val=0;

    // read id
    DetId detid= readCellId(dynamic_cast<DOMElement*>(cellnode));
       
    // read value
    DOMNode * c_node = getChildNode(cellnode,Value_tag);
    GetNodeData(c_node,val);

    // fill record
    record[detid]=val;

    // get next cell
    cellnode= cellnode->getNextSibling();
    
    while (cellnode&& cellnode->getNodeType( ) != DOMNode::ELEMENT_NODE)      
      cellnode= cellnode->getNextSibling();
    
    
  }


  delete parser;
  cms::concurrency::xercesTerminate();
  */
  // new method for DBv2
  std::string dummyLine, bid;
  std::ifstream fxml;
  fxml.open(filename);
  if (!fxml.is_open()) {
    std::cout << "ERROR : cannot open file " << filename << std::endl;
    exit(1);
  }
  // header
  for (int i = 0; i < 6; i++) {
    getline(fxml, dummyLine);  // skip first lines
    //	std::cout << dummyLine << std::endl;
  }
  fxml >> bid;
  //  std::cout << bid << std::endl;
  std::string stt = bid.substr(7, 5);
  std::istringstream iEB(stt);
  int nEB;
  iEB >> nEB;
  if (nEB != kEBChannels) {
    std::cout << " strange number of EB channels " << nEB << std::endl;
    exit(-1);
  }
  fxml >> bid;  // <item_version>0</item_version>
  for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
    EBDetId myEBDetId = EBDetId::unhashIndex(iChannel);
    fxml >> bid;
    std::size_t found = bid.find("</");
    stt = bid.substr(6, found - 6);
    float val = std::stof(stt);
    record[myEBDetId] = val;
  }
  for (int i = 0; i < 5; i++) {
    getline(fxml, dummyLine);  // skip first lines
    //	std::cout << dummyLine << std::endl;
  }
  fxml >> bid;
  //    cout << bid << endl;
  stt = bid.substr(7, 5);
  std::istringstream iEE(stt);
  int nEE;
  iEE >> nEE;
  if (nEE != kEEChannels) {
    std::cout << " strange number of EE channels " << nEE << std::endl;
    exit(-1);
  }
  fxml >> bid;  // <item_version>0</item_version>
  // now endcaps
  for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
    EEDetId myEEDetId = EEDetId::unhashIndex(iChannel);
    fxml >> bid;
    std::size_t found = bid.find("</");
    stt = bid.substr(6, found - 6);
    float val = std::stof(stt);
    record[myEEDetId] = val;
  }

  return 0;
}

std::vector<float> EcalFloatCondObjectContainerXMLTranslator::barrelfromXML(const string& filename) {
  EcalCondHeader header;
  EcalFloatCondObjectContainer record;
  readXML(filename, header, record);

  return record.barrelItems();
}

std::vector<float> EcalFloatCondObjectContainerXMLTranslator::endcapfromXML(const string& filename) {
  EcalCondHeader header;
  EcalFloatCondObjectContainer record;
  readXML(filename, header, record);

  return record.endcapItems();
}

std::string EcalFloatCondObjectContainerXMLTranslator::dumpXML(const EcalCondHeader& header,
                                                               const std::vector<float>& eb,
                                                               const std::vector<float>& ee) {
  if (eb.size() != EBDetId::kSizeForDenseIndexing) {
    std::cerr << "Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Barrel array size: " << eb.size()
              << " should be " << EBDetId::kSizeForDenseIndexing << std::endl;
    return std::string("");
  }

  if (ee.size() != EEDetId::kSizeForDenseIndexing) {
    std::cerr << "Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Endcap array size: " << ee.size()
              << " should be " << EEDetId::kSizeForDenseIndexing << std::endl;
    return std::string("");
  }

  EcalFloatCondObjectContainer record;

  for (int cellid = 0; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {  // loop on EB cells

    uint32_t rawid = EBDetId::unhashIndex(cellid);
    record[rawid] = eb[cellid];
  }

  for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {  // loop on EE cells

    if (EEDetId::validHashIndex(cellid)) {
      uint32_t rawid = EEDetId::unhashIndex(cellid);

      record[rawid] = ee[cellid];
    }  // if
  }

  return dumpXML(header, record);
}

std::string EcalFloatCondObjectContainerXMLTranslator::dumpXML(const EcalCondHeader& header,
                                                               const EcalFloatCondObjectContainer& record) {
  cms::concurrency::xercesInitialize();

  unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));

  DOMLSSerializer* writer = impl->createLSSerializer();
  if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
    writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);

  DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
  DOMDocument* doc =
      impl->createDocument(nullptr, cms::xerces::uStr(EcalFloatCondObjectContainer_tag.c_str()).ptr(), doctype);
  DOMElement* root = doc->getDocumentElement();

  xuti::writeHeader(root, header);

  for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {  // loop on EB cells

    uint32_t rawid = EBDetId::unhashIndex(cellid);
    EcalFloatCondObjectContainer::const_iterator value_ptr = record.find(rawid);
    if (value_ptr == record.end())
      continue;  // cell absent from original record

    DOMElement* cellnode = writeCell(root, rawid);

    WriteNodeWithValue(cellnode, Value_tag, *value_ptr);

  }  // loop on EB cells

  for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {  // loop on EE cells

    if (!EEDetId::validHashIndex(cellid))
      continue;

    uint32_t rawid = EEDetId::unhashIndex(cellid);
    EcalFloatCondObjectContainer::const_iterator value_ptr = record.find(rawid);
    if (value_ptr == record.end())
      continue;  // cell absent from original record

    DOMElement* cellnode = writeCell(root, rawid);
    WriteNodeWithValue(cellnode, Value_tag, *value_ptr);

  }  // loop on EE cells

  std::string dump = cms::xerces::toString(writer->writeToString(root));
  doc->release();
  doctype->release();
  writer->release();

  cms::concurrency::xercesTerminate();

  return dump;
}

int EcalFloatCondObjectContainerXMLTranslator::writeXML(const std::string& filename,
                                                        const EcalCondHeader& header,
                                                        const EcalFloatCondObjectContainer& record) {
  cms::concurrency::xercesInitialize();

  std::fstream fs(filename.c_str(), ios::out);
  fs << dumpXML(header, record);

  cms::concurrency::xercesTerminate();

  return 0;
}