CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/CondTools/Ecal/src/EcalTPGTowerStatusXMLTranslator.cc

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