CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/CondTools/Ecal/src/EcalDCSTowerStatusXMLTranslator.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <iomanip>
00003 #include <sstream>
00004 #include <fstream>
00005 #include <xercesc/dom/DOMNode.hpp>
00006 #include <xercesc/dom/DOM.hpp>
00007 #include <xercesc/parsers/XercesDOMParser.hpp>
00008 #include <xercesc/util/PlatformUtils.hpp>
00009 #include <xercesc/util/XMLString.hpp>
00010 #include <xercesc/sax/SAXException.hpp>
00011 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00012 
00013 #include "CondTools/Ecal/interface/EcalDCSTowerStatusXMLTranslator.h"
00014 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00015 #include "CondTools/Ecal/interface/XMLTags.h"
00016 
00017 using namespace XERCES_CPP_NAMESPACE;
00018 using namespace xuti;
00019 using namespace std;
00020 
00021 int  EcalDCSTowerStatusXMLTranslator::readXML(const std::string& filename, 
00022                                           EcalCondHeader& header,
00023                                           EcalDCSTowerStatus& record){
00024 
00025   std::cout << " DCSTowerStatus should not be filled out from an xml file ..." << std::endl;
00026   XMLPlatformUtils::Initialize();
00027 
00028   XercesDOMParser* parser = new XercesDOMParser;
00029   parser->setValidationScheme( XercesDOMParser::Val_Never );
00030   parser->setDoNamespaces( false );
00031   parser->setDoSchema( false );
00032   
00033   parser->parse(filename.c_str());
00034 
00035   DOMDocument* xmlDoc = parser->getDocument();
00036   if (!xmlDoc) {
00037     std::cout << "EcalDCSTowerStatusXMLTranslator::Error parsing document" << std::endl;
00038     return -1;
00039   }
00040 
00041   DOMElement* elementRoot = xmlDoc->getDocumentElement();
00042 
00043   xuti::readHeader(elementRoot,header);
00044 
00045   DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
00046 
00047   int chan = 0;
00048   while(cellnode) {
00049     int status = -1;
00050     DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
00051 
00052     DOMNode* my_node = getChildNode(cellnode,DCSStatusCode_tag);
00053     GetNodeData(my_node, status);
00054 
00055     record[detid] = status;
00056 
00057     cellnode = cellnode->getNextSibling();
00058 
00059     while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
00060       cellnode = cellnode->getNextSibling();
00061     chan++;
00062   } 
00063 
00064   delete parser;
00065   XMLPlatformUtils::Terminate();
00066   return 0;
00067  }
00068 
00069 int EcalDCSTowerStatusXMLTranslator::writeXML(const std::string& filename, 
00070                                           const EcalCondHeader& header,
00071                                           const EcalDCSTowerStatus& record){
00072   std::fstream fs(filename.c_str(),ios::out);
00073   fs<< dumpXML(header,record);
00074   return 0;  
00075 }
00076 
00077 
00078 std::string EcalDCSTowerStatusXMLTranslator::dumpXML(const EcalCondHeader& header,const EcalDCSTowerStatus& record){
00079 
00080   XMLPlatformUtils::Initialize();
00081   DOMImplementation*  impl =
00082     DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00083 
00084   DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00085   writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00086 
00087   DOMDocumentType* doctype = impl->createDocumentType(fromNative("XML").c_str(), 0, 0 );
00088   DOMDocument *    doc = 
00089     impl->createDocument( 0, fromNative(DCSTowerStatus_tag).c_str(), doctype );
00090 
00091   doc->setEncoding(fromNative("UTF-8").c_str() );
00092   doc->setStandalone(true);
00093   doc->setVersion(fromNative("1.0").c_str() );
00094 
00095   DOMElement* root = doc->getDocumentElement();
00096 
00097   xuti::writeHeader(root,header);
00098   std::cout << " barrel size " << record.barrelItems().size() << std::endl;
00099   if (!record.barrelItems().size()) return std::string();
00100   for(uint cellid = 0;
00101       cellid < EcalTrigTowerDetId::kEBTotalTowers;
00102       ++cellid) {
00103     uint32_t rawid = EcalTrigTowerDetId::detIdFromDenseIndex(cellid);
00104     if (record.find(rawid) == record.end()) continue;
00105     DOMElement* cellnode=writeCell(root,rawid);
00106 
00107     WriteNodeWithValue(cellnode, DCSStatusCode_tag, record[rawid].getStatusCode());
00108   } 
00109 
00110   std::cout << " endcap size " << record.endcapItems().size() << std::endl;
00111   if (!record.endcapItems().size()) return std::string();
00112   for(uint cellid = 0;
00113       cellid < EcalTrigTowerDetId::kEETotalTowers;
00114       ++cellid) {
00115     if(!EcalScDetId::validHashIndex(cellid)) continue;
00116     uint32_t rawid = EcalScDetId::unhashIndex(cellid); 
00117 
00118     if (record.find(rawid) == record.end()) continue;
00119     DOMElement* cellnode=writeCell(root,rawid);
00120 
00121     WriteNodeWithValue(cellnode, DCSStatusCode_tag, record[rawid].getStatusCode());
00122   }
00123 
00124   std::string dump = toNative(writer->writeToString(*root)); 
00125   doc->release(); 
00126   return dump;
00127 }
00128 
00129 void EcalDCSTowerStatusXMLTranslator::plot(std::string fn, const EcalDCSTowerStatus& record){
00130   std::ofstream fout(fn.c_str());
00131   int valEB[34][72];
00132   std::cout << " barrel size " << record.barrelItems().size() << std::endl;
00133   if (!record.barrelItems().size()) return;
00134   for(uint cellid = 0;
00135       cellid < EcalTrigTowerDetId::kEBTotalTowers;
00136       ++cellid) {
00137     EcalTrigTowerDetId rawid = EcalTrigTowerDetId::detIdFromDenseIndex(cellid);
00138     if (record.find(rawid) == record.end()) continue;
00139     int ieta = rawid.ieta();
00140     int line = 17 - ieta;
00141     if(ieta < 0) line--;
00142     int iphi = rawid.iphi() - 1;  // 0 to 71
00143     valEB[line][iphi] = record[rawid].getStatusCode();
00144   }
00145   for(int line = 0; line < 34; line++) {
00146     for(int iphi = 0; iphi < 72; iphi++)
00147       fout << valEB[line][iphi] << " ";
00148     fout << std::endl;
00149     if(line == 16) fout << std::endl;
00150   }
00151 
00152   std::cout << " endcap size " << record.endcapItems().size() << std::endl;
00153   if (!record.endcapItems().size()) return;
00154   int valEE[2][20][20];
00155   for(int k = 0 ; k < 2; k++ ) 
00156     for(int ix = 0 ; ix < 20; ix++) 
00157       for(int iy = 0 ; iy < 20; iy++) 
00158         valEE[k][ix][iy] = -1;
00159   for(uint cellid = 0;
00160       cellid < EcalTrigTowerDetId::kEETotalTowers;
00161       ++cellid) {
00162     if(EcalScDetId::validHashIndex(cellid)) {
00163       EcalScDetId rawid = EcalScDetId::unhashIndex(cellid); 
00164       int ix = rawid.ix() - 1;  // 0 to 19
00165       int iy = 20 -  rawid.iy();  // 0 to 19
00166       int side = rawid.zside();
00167       int iz = side;
00168       if(side == -1) iz = 0;
00169       if(ix < 0 || ix > 19) std::cout << " Pb in ix " << ix << std::endl;
00170       if(iy < 0 || iy > 19) std::cout << " Pb in iy " << iy << std::endl;
00171       valEE[iz][ix][iy] = record[rawid].getStatusCode();
00172     }
00173   }
00174   for(int k = 0 ; k < 2; k++ ) {
00175     int iz = -1;
00176     if(k == 1) iz = 1;
00177     fout << " Side : " << iz << std::endl;
00178     for(int line = 0; line < 20; line++) {
00179       for(int ix = 0 ; ix < 20; ix++) {
00180         if(valEE[k][ix][line] < 0) fout << " . ";
00181         else fout << setw(2) << valEE[k][ix][line] << " ";
00182       }
00183       fout << std::endl;
00184     }
00185     fout << std::endl;
00186   }
00187 
00188   return;
00189 }