Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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   while (cellnode) {
0047     int status = -1;
0048     DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
0049 
0050     DOMNode* my_node = getChildNode(cellnode, DAQStatusCode_tag);
0051     GetNodeData(my_node, status);
0052 
0053     record[detid] = status;
0054 
0055     cellnode = cellnode->getNextSibling();
0056 
0057     while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
0058       cellnode = cellnode->getNextSibling();
0059   }
0060 
0061   delete parser;
0062   cms::concurrency::xercesTerminate();
0063   return 0;
0064 }
0065 
0066 int EcalDAQTowerStatusXMLTranslator::writeXML(const std::string& filename,
0067                                               const EcalCondHeader& header,
0068                                               const EcalDAQTowerStatus& record) {
0069   cms::concurrency::xercesInitialize();
0070 
0071   std::fstream fs(filename.c_str(), ios::out);
0072   fs << dumpXML(header, record);
0073 
0074   cms::concurrency::xercesTerminate();
0075 
0076   return 0;
0077 }
0078 
0079 std::string EcalDAQTowerStatusXMLTranslator::dumpXML(const EcalCondHeader& header, const EcalDAQTowerStatus& record) {
0080   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0081 
0082   DOMLSSerializer* writer = impl->createLSSerializer();
0083   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0084     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0085 
0086   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0087   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(DAQTowerStatus_tag.c_str()).ptr(), doctype);
0088 
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, DAQStatusCode_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, DAQStatusCode_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 EcalDAQTowerStatusXMLTranslator::plot(std::string fn, const EcalDAQTowerStatus& 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 << valEE[k][ix][line] << " ";
0187       }
0188       fout << std::endl;
0189     }
0190     fout << std::endl;
0191   }
0192 
0193   return;
0194 }