Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:42

0001 /**
0002  *  \file Implementation of helper functions
0003  *
0004  *  $Id: DOMHelperFunctions.cc,v 1.5 2010/12/16 08:54:06 fay Exp $
0005  */
0006 
0007 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0008 #include "CondTools/Ecal/interface/XMLTags.h"
0009 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0010 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0011 #include "DataFormats/EcalDetId/interface/EcalTrigTowerDetId.h"
0012 #include <xercesc/dom/DOM.hpp>
0013 #include <xercesc/dom/DOMNode.hpp>
0014 #include <xercesc/parsers/XercesDOMParser.hpp>
0015 #include "Utilities/Xerces/interface/Xerces.h"
0016 #include "Utilities/Xerces/interface/XercesStrUtils.h"
0017 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0018 #include <sstream>
0019 
0020 using namespace std;
0021 using namespace xuti;
0022 using namespace xercesc;
0023 
0024 const DetId xuti::readCellId(xercesc::DOMElement* node) {
0025   int ieta = 0;
0026   int iphi = 0;
0027   int ix = 0;
0028   int iy = 0;
0029   int ixSC = 0;
0030   int iySC = 0;
0031   int zside = 0;
0032 
0033   stringstream ieta_str;
0034   stringstream iphi_str;
0035   stringstream ix_str;
0036   stringstream iy_str;
0037   stringstream ixSC_str;
0038   stringstream iySC_str;
0039   stringstream zside_str;
0040 
0041   ieta_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr()));
0042   iphi_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr()));
0043   ix_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(ix_tag.c_str()).ptr()));
0044   iy_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iy_tag.c_str()).ptr()));
0045   ixSC_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(ixSC_tag.c_str()).ptr()));
0046   iySC_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(iySC_tag.c_str()).ptr()));
0047   zside_str << cms::xerces::toString(node->getAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr()));
0048 
0049   ieta_str >> ieta;
0050   iphi_str >> iphi;
0051   ix_str >> ix;
0052   iy_str >> iy;
0053   ixSC_str >> ixSC;
0054   iySC_str >> iySC;
0055   zside_str >> zside;
0056 
0057   if (ieta && iphi) {
0058     return EBDetId(ieta, iphi);
0059   }
0060   if (ix && iy && zside) {
0061     return EEDetId(ix, iy, zside);
0062   }
0063   if (ixSC && iySC && zside) {
0064     return EcalScDetId(ixSC, iySC, zside);
0065   }
0066 
0067   cerr << "XMLCell: error reading cell, missing field ?" << std::endl;
0068   return 0;
0069 }
0070 
0071 DOMElement* xuti::writeCell(xercesc::DOMNode* node, const DetId& detid) {
0072   DOMElement* cell_node = node->getOwnerDocument()->createElement(cms::xerces::uStr(Cell_tag.c_str()).ptr());
0073 
0074   node->appendChild(cell_node);
0075 
0076   if (detid.subdetId() == EcalBarrel) {
0077     stringstream value_s;
0078     value_s << EBDetId(detid).ieta();
0079 
0080     cell_node->setAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
0081     value_s.str("");
0082     value_s << EBDetId(detid).iphi();
0083 
0084     cell_node->setAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
0085 
0086   } else if (detid.subdetId() == EcalEndcap) {
0087     // is it a EcalScDetId ?
0088     unsigned int ScIdCheck = detid.rawId() & 0x00008000;
0089     if (ScIdCheck == 0) {
0090       stringstream value_s;
0091       value_s << EEDetId(detid).ix();
0092 
0093       cell_node->setAttribute(cms::xerces::uStr(ix_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
0094       value_s.str("");
0095       value_s << EEDetId(detid).iy();
0096 
0097       cell_node->setAttribute(cms::xerces::uStr(iy_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
0098       value_s.str("");
0099       value_s << EEDetId(detid).zside();
0100 
0101       cell_node->setAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr(),
0102                               cms::xerces::uStr(value_s.str().c_str()).ptr());
0103     } else {
0104       stringstream value_s;
0105       value_s << EcalScDetId(detid).ix();
0106 
0107       cell_node->setAttribute(cms::xerces::uStr(ixSC_tag.c_str()).ptr(),
0108                               cms::xerces::uStr(value_s.str().c_str()).ptr());
0109       value_s.str("");
0110       value_s << EcalScDetId(detid).iy();
0111 
0112       cell_node->setAttribute(cms::xerces::uStr(iySC_tag.c_str()).ptr(),
0113                               cms::xerces::uStr(value_s.str().c_str()).ptr());
0114       value_s.str("");
0115       value_s << EcalScDetId(detid).zside();
0116 
0117       cell_node->setAttribute(cms::xerces::uStr(zside_tag.c_str()).ptr(),
0118                               cms::xerces::uStr(value_s.str().c_str()).ptr());
0119     }
0120 
0121   } else if (detid.subdetId() == EcalTriggerTower) {
0122     stringstream value_s;
0123     value_s << EcalTrigTowerDetId(detid).ieta();
0124 
0125     cell_node->setAttribute(cms::xerces::uStr(iEta_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
0126     value_s.str("");
0127     value_s << EcalTrigTowerDetId(detid).iphi();
0128 
0129     cell_node->setAttribute(cms::xerces::uStr(iPhi_tag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
0130   }
0131   return cell_node;
0132 }
0133 
0134 // return 0 if not found
0135 DOMNode* xuti::getChildNode(DOMNode* node, const std::string& nodename) {
0136   if (!node)
0137     return nullptr;
0138 
0139   for (DOMNode* childNode = node->getFirstChild(); childNode; childNode = childNode->getNextSibling()) {
0140     if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
0141       const string foundName = cms::xerces::toString(childNode->getNodeName());
0142 
0143       if (foundName == nodename)
0144         return childNode;
0145     }  // if element
0146   }  // for child
0147 
0148   return nullptr;
0149 }
0150 
0151 void xuti::writeHeader(xercesc::DOMNode* parentNode, const EcalCondHeader& header) {
0152   DOMElement* headernode = parentNode->getOwnerDocument()->createElement(cms::xerces::uStr(Header_tag.c_str()).ptr());
0153   parentNode->appendChild(headernode);
0154 
0155   // write the actual header
0156   WriteNodeWithValue(headernode, Header_methodtag, header.method_);
0157   WriteNodeWithValue(headernode, Header_versiontag, header.version_);
0158   WriteNodeWithValue(headernode, Header_datasourcetag, header.datasource_);
0159   WriteNodeWithValue(headernode, Header_sincetag, header.since_);
0160   WriteNodeWithValue(headernode, Header_tagtag, header.tag_);
0161   WriteNodeWithValue(headernode, Header_datetag, header.date_);
0162 }
0163 
0164 void xuti::readHeader(xercesc::DOMNode* parentNode, EcalCondHeader& header) {
0165   DOMNode* hnode = getChildNode(parentNode, Header_tag);
0166 
0167   DOMNode* node = getChildNode(hnode, Header_methodtag);
0168   GetNodeStringData(node, header.method_);
0169 
0170   node = getChildNode(hnode, Header_versiontag);
0171   GetNodeStringData(node, header.version_);
0172 
0173   node = getChildNode(hnode, Header_datasourcetag);
0174   GetNodeStringData(node, header.datasource_);
0175 
0176   node = getChildNode(hnode, Header_sincetag);
0177   GetNodeData(node, header.since_);
0178 
0179   node = getChildNode(hnode, Header_tagtag);
0180   GetNodeStringData(node, header.tag_);
0181 
0182   node = getChildNode(hnode, Header_datetag);
0183   GetNodeStringData(node, header.date_);
0184 }
0185 
0186 int xuti::readHeader(const std::string& filename, EcalCondHeader& header) {
0187   cms::concurrency::xercesInitialize();
0188 
0189   XercesDOMParser* parser = new XercesDOMParser;
0190   parser->setValidationScheme(XercesDOMParser::Val_Never);
0191   parser->setDoNamespaces(false);
0192   parser->setDoSchema(false);
0193 
0194   parser->parse(filename.c_str());
0195 
0196   DOMDocument* xmlDoc = parser->getDocument();
0197 
0198   if (!xmlDoc) {
0199     std::cout << "Error parsing document" << std::endl;
0200     return -1;
0201   }
0202 
0203   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0204 
0205   xuti::readHeader(elementRoot, header);
0206 
0207   delete parser;
0208   cms::concurrency::xercesTerminate();
0209 
0210   return 0;
0211 }
0212 
0213 void xuti::GetNodeStringData(xercesc::DOMNode* node, std::string& value) {
0214   value = cms::xerces::toString(node->getTextContent());
0215 }