Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:56

0001 #include <iostream>
0002 #include <iomanip>
0003 #include <sstream>
0004 #include <fstream>
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/EcalDCSTowerStatusXMLTranslator.h"
0015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0016 #include "CondTools/Ecal/interface/XMLTags.h"
0017 
0018 using namespace XERCES_CPP_NAMESPACE;
0019 using namespace xuti;
0020 using namespace std;
0021 
0022 int EcalDCSTowerStatusXMLTranslator::readXML(const std::string& filename,
0023                                              EcalCondHeader& header,
0024                                              EcalDCSTowerStatus& record) {
0025   std::cout << " DCSTowerStatus should not be filled out from an xml file ..." << std::endl;
0026   cms::concurrency::xercesInitialize();
0027 
0028   XercesDOMParser* parser = new XercesDOMParser;
0029   parser->setValidationScheme(XercesDOMParser::Val_Never);
0030   parser->setDoNamespaces(false);
0031   parser->setDoSchema(false);
0032 
0033   parser->parse(filename.c_str());
0034 
0035   DOMDocument* xmlDoc = parser->getDocument();
0036   if (!xmlDoc) {
0037     std::cout << "EcalDCSTowerStatusXMLTranslator::Error parsing document" << std::endl;
0038     return -1;
0039   }
0040 
0041   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0042 
0043   xuti::readHeader(elementRoot, header);
0044 
0045   DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
0046 
0047   while (cellnode) {
0048     int status = -1;
0049     DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
0050 
0051     DOMNode* my_node = getChildNode(cellnode, DCSStatusCode_tag);
0052     GetNodeData(my_node, status);
0053 
0054     record[detid] = status;
0055 
0056     cellnode = cellnode->getNextSibling();
0057 
0058     while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
0059       cellnode = cellnode->getNextSibling();
0060   }
0061 
0062   delete parser;
0063   cms::concurrency::xercesTerminate();
0064   return 0;
0065 }
0066 
0067 int EcalDCSTowerStatusXMLTranslator::writeXML(const std::string& filename,
0068                                               const EcalCondHeader& header,
0069                                               const EcalDCSTowerStatus& record) {
0070   cms::concurrency::xercesInitialize();
0071 
0072   std::fstream fs(filename.c_str(), ios::out);
0073   fs << dumpXML(header, record);
0074 
0075   cms::concurrency::xercesTerminate();
0076 
0077   return 0;
0078 }
0079 
0080 std::string EcalDCSTowerStatusXMLTranslator::dumpXML(const EcalCondHeader& header, const EcalDCSTowerStatus& record) {
0081   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0082 
0083   DOMLSSerializer* writer = impl->createLSSerializer();
0084   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0085     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0086 
0087   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0088   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(DCSTowerStatus_tag.c_str()).ptr(), doctype);
0089   DOMElement* root = doc->getDocumentElement();
0090 
0091   xuti::writeHeader(root, header);
0092   std::cout << " barrel size " << record.barrelItems().size() << std::endl;
0093   if (record.barrelItems().empty())
0094     return std::string();
0095   for (uint cellid = 0; cellid < EcalTrigTowerDetId::kEBTotalTowers; ++cellid) {
0096     uint32_t rawid = EcalTrigTowerDetId::detIdFromDenseIndex(cellid);
0097     if (record.find(rawid) == record.end())
0098       continue;
0099     DOMElement* cellnode = writeCell(root, rawid);
0100 
0101     WriteNodeWithValue(cellnode, DCSStatusCode_tag, record[rawid].getStatusCode());
0102   }
0103 
0104   std::cout << " endcap size " << record.endcapItems().size() << std::endl;
0105   if (record.endcapItems().empty())
0106     return std::string();
0107   for (uint cellid = 0; cellid < EcalTrigTowerDetId::kEETotalTowers; ++cellid) {
0108     if (!EcalScDetId::validHashIndex(cellid))
0109       continue;
0110     uint32_t rawid = EcalScDetId::unhashIndex(cellid);
0111 
0112     if (record.find(rawid) == record.end())
0113       continue;
0114     DOMElement* cellnode = writeCell(root, rawid);
0115 
0116     WriteNodeWithValue(cellnode, DCSStatusCode_tag, record[rawid].getStatusCode());
0117   }
0118 
0119   std::string dump = cms::xerces::toString(writer->writeToString(root));
0120   doc->release();
0121   doctype->release();
0122   writer->release();
0123 
0124   return dump;
0125 }
0126 
0127 void EcalDCSTowerStatusXMLTranslator::plot(std::string fn, const EcalDCSTowerStatus& record) {
0128   std::ofstream fout(fn.c_str());
0129   int valEB[34][72];
0130   std::cout << " barrel size " << record.barrelItems().size() << std::endl;
0131   if (record.barrelItems().empty())
0132     return;
0133   for (uint cellid = 0; cellid < EcalTrigTowerDetId::kEBTotalTowers; ++cellid) {
0134     EcalTrigTowerDetId rawid = EcalTrigTowerDetId::detIdFromDenseIndex(cellid);
0135     if (record.find(rawid) == record.end())
0136       continue;
0137     int ieta = rawid.ieta();
0138     int line = 17 - ieta;
0139     if (ieta < 0)
0140       line--;
0141     int iphi = rawid.iphi() - 1;  // 0 to 71
0142     valEB[line][iphi] = record[rawid].getStatusCode();
0143   }
0144   for (int line = 0; line < 34; line++) {
0145     for (int iphi = 0; iphi < 72; iphi++)
0146       fout << valEB[line][iphi] << " ";
0147     fout << std::endl;
0148     if (line == 16)
0149       fout << std::endl;
0150   }
0151 
0152   std::cout << " endcap size " << record.endcapItems().size() << std::endl;
0153   if (record.endcapItems().empty())
0154     return;
0155   int valEE[2][20][20];
0156   for (int k = 0; k < 2; k++)
0157     for (int ix = 0; ix < 20; ix++)
0158       for (int iy = 0; iy < 20; iy++)
0159         valEE[k][ix][iy] = -1;
0160   for (uint cellid = 0; cellid < EcalTrigTowerDetId::kEETotalTowers; ++cellid) {
0161     if (EcalScDetId::validHashIndex(cellid)) {
0162       EcalScDetId rawid = EcalScDetId::unhashIndex(cellid);
0163       int ix = rawid.ix() - 1;   // 0 to 19
0164       int iy = 20 - rawid.iy();  // 0 to 19
0165       int side = rawid.zside();
0166       int iz = side;
0167       if (side == -1)
0168         iz = 0;
0169       if (ix < 0 || ix > 19)
0170         std::cout << " Pb in ix " << ix << std::endl;
0171       if (iy < 0 || iy > 19)
0172         std::cout << " Pb in iy " << iy << std::endl;
0173       valEE[iz][ix][iy] = record[rawid].getStatusCode();
0174     }
0175   }
0176   for (int k = 0; k < 2; k++) {
0177     int iz = -1;
0178     if (k == 1)
0179       iz = 1;
0180     fout << " Side : " << iz << std::endl;
0181     for (int line = 0; line < 20; line++) {
0182       for (int ix = 0; ix < 20; ix++) {
0183         if (valEE[k][ix][line] < 0)
0184           fout << " . ";
0185         else
0186           fout << setw(2) << valEE[k][ix][line] << " ";
0187       }
0188       fout << std::endl;
0189     }
0190     fout << std::endl;
0191   }
0192 
0193   return;
0194 }