Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:52:04

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