Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:46:25

0001 #include "CalibCalorimetry/CaloMiscalibTools/interface/MiscalibReaderFromXML.h"
0002 #include "CalibCalorimetry/CaloMiscalibTools/interface/MiscalibReaderFromXMLDomUtils.h"
0003 #include "CalibCalorimetry/CaloMiscalibTools/interface/CaloMiscalibMap.h"
0004 #include "Utilities/Xerces/interface/Xerces.h"
0005 #include <cstdio>
0006 #include <cstdlib>
0007 #include <string>
0008 #include <vector>
0009 
0010 using namespace xercesc;
0011 
0012 int MiscalibReaderFromXML::s_numberOfInstances = 0;  //to check that there is only 1 instance
0013 
0014 inline std::string _toString(const XMLCh* toTranscode) {
0015   std::string tmp(XMLString::transcode(toTranscode));
0016   return tmp;
0017 }
0018 
0019 inline XMLCh* _toDOMS(std::string temp) {
0020   XMLCh* buff = XMLString::transcode(temp.c_str());
0021   return buff;
0022 }
0023 
0024 MiscalibReaderFromXML::MiscalibReaderFromXML(CaloMiscalibMap& caloMap) : caloMap_(caloMap) {
0025   try {
0026     //std::cout << "Xerces-c initialization Number "
0027     //<< s_numberOfInstances<<std::endl;
0028     if (s_numberOfInstances == 0)
0029       cms::concurrency::xercesInitialize();
0030   } catch (const XMLException& e) {
0031     std::cout << "Xerces-c error in initialization \n"
0032               << "Exception message is:  \n"
0033               << _toString(e.getMessage()) << std::endl;
0034     // throw an exception here
0035   }
0036 
0037   ++s_numberOfInstances;
0038 }
0039 //////////////////////////////////////////////////////////////////////////////////
0040 
0041 int MiscalibReaderFromXML::getIntAttribute(DOMNamedNodeMap* attribute, const std::string& attribute_name) {
0042   bool well_formed_string;
0043   int retval = MiscalibReaderFromXMLDomUtils::getIntAttribute(attribute, attribute_name, well_formed_string);
0044   if (!well_formed_string)
0045     std::cout << "MiscalibReaderFromXML::getIntAttribute PROBLEMS ...!!!" << std::endl;
0046 
0047   return retval;
0048 }
0049 
0050 //////////////////////////////////////////////////////////////////////////////////
0051 
0052 double MiscalibReaderFromXML::getScalingFactor(XERCES_CPP_NAMESPACE::DOMNamedNodeMap* attribute) {
0053   return MiscalibReaderFromXML::getFloatAttribute(attribute, "scale_factor");
0054 }
0055 
0056 //////////////////////////////////////////////////////////////////////////////////
0057 
0058 double MiscalibReaderFromXML::getFloatAttribute(DOMNamedNodeMap* attribute, const std::string& attribute_name) {
0059   bool well_formed_string;
0060   double retval = MiscalibReaderFromXMLDomUtils::getFloatAttribute(attribute, attribute_name, well_formed_string);
0061   if (!well_formed_string)
0062     std::cout << "MiscalibReaderFromXML::getFloatAttribute PROBLEMS ...!!!" << std::endl;
0063 
0064   return retval;
0065 }
0066 
0067 //////////////////////////////////////////////////////////////////////////////////
0068 
0069 bool MiscalibReaderFromXML::parseXMLMiscalibFile(std::string configFile) {
0070   XercesDOMParser* parser = new XercesDOMParser;
0071   parser->setValidationScheme(XercesDOMParser::Val_Auto);
0072   parser->setDoNamespaces(false);
0073   parser->parse(configFile.c_str());
0074   DOMDocument* doc = parser->getDocument();
0075   assert(doc);
0076 
0077   unsigned int linkTagsNum = doc->getElementsByTagName(_toDOMS("Cell"))->getLength();
0078   // The following should be on LogInfo
0079   //std::cout << "Read number of Cells = " << linkTagsNum << std::endl;
0080 
0081   if (linkTagsNum == 0)
0082     std::cout << "Number of Cells in file is 0 - probably bad file format" << std::endl;
0083 
0084   int count = 0;
0085   for (unsigned int i = 0; i < linkTagsNum; i++) {
0086     DOMNode* linkNode = doc->getElementsByTagName(_toDOMS("Cell"))->item(i);
0087     ///Get ME name
0088     if (!linkNode) {
0089       std::cout << "Node LINK does not exist, i=" << i << std::endl;
0090       return true;
0091     }
0092     DOMElement* linkElement = static_cast<DOMElement*>(linkNode);
0093     if (!linkElement) {
0094       std::cout << "Element LINK does not exist, i=" << i << std::endl;
0095       return true;
0096     }
0097 
0098     DOMNamedNodeMap* attributes = linkNode->getAttributes();
0099     double scalingfactor = getScalingFactor(attributes);
0100 
0101     DetId cell = parseCellEntry(attributes);
0102 
0103     if (cell != DetId(0)) {
0104       count++;
0105       caloMap_.addCell(cell, scalingfactor);
0106     } else {
0107       //        std::cout << "Null received" << std::endl;
0108     }
0109   }
0110 
0111   // The following should be on LogInfo
0112   // std::cout << "Number of good Cells = " << count << std::endl;
0113   return false;
0114 }