Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-05-10 03:53:08

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   for (unsigned int i = 0; i < linkTagsNum; i++) {
0085     DOMNode* linkNode = doc->getElementsByTagName(_toDOMS("Cell"))->item(i);
0086     ///Get ME name
0087     if (!linkNode) {
0088       std::cout << "Node LINK does not exist, i=" << i << std::endl;
0089       return true;
0090     }
0091     DOMElement* linkElement = static_cast<DOMElement*>(linkNode);
0092     if (!linkElement) {
0093       std::cout << "Element LINK does not exist, i=" << i << std::endl;
0094       return true;
0095     }
0096 
0097     DOMNamedNodeMap* attributes = linkNode->getAttributes();
0098     double scalingfactor = getScalingFactor(attributes);
0099 
0100     DetId cell = parseCellEntry(attributes);
0101 
0102     if (cell != DetId(0)) {
0103       caloMap_.addCell(cell, scalingfactor);
0104     } else {
0105       //        std::cout << "Null received" << std::endl;
0106     }
0107   }
0108 
0109   // The following should be on LogInfo
0110   // std::cout << "Number of good Cells = " << count << std::endl;
0111   return false;
0112 }