Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:57

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