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
00013 #include "CondFormats/EcalObjects/interface/EcalPedestals.h"
00014 #include "CondTools/Ecal/interface/EcalPedestalsXMLTranslator.h"
00015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00016 #include "CondTools/Ecal/interface/XMLTags.h"
00017
00018
00019 using namespace XERCES_CPP_NAMESPACE;
00020 using namespace xuti;
00021 using namespace std;
00022
00023 int EcalPedestalsXMLTranslator::readXML(const std::string& filename,
00024 EcalCondHeader& header,
00025 EcalPedestals& record){
00026
00027 XMLPlatformUtils::Initialize();
00028
00029 XercesDOMParser* parser = new XercesDOMParser;
00030 parser->setValidationScheme( XercesDOMParser::Val_Never );
00031 parser->setDoNamespaces( false );
00032 parser->setDoSchema( false );
00033
00034 parser->parse(filename.c_str());
00035
00036 DOMDocument* xmlDoc = parser->getDocument();
00037 if (!xmlDoc) {
00038 std::cout << "EcalPedestalsXMLTranslator::Error parsing document" << std::endl;
00039 return -1;
00040 }
00041
00042 DOMElement* elementRoot = xmlDoc->getDocumentElement();
00043
00044 xuti::readHeader(elementRoot,header);
00045
00046 DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
00047
00048 int chan = 0;
00049 while(cellnode) {
00050
00051 float mean12 = 0;
00052 float mean6 = 0;
00053 float mean1 = 0;
00054 float rms12 = 0;
00055 float rms6 = 0;
00056 float rms1 = 0;
00057 DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
00058
00059
00060 DOMNode* mean12_node = getChildNode(cellnode,mean12_tag);
00061 GetNodeData(mean12_node,mean12);
00062
00063
00064 DOMNode* mean6_node = getChildNode(cellnode,mean6_tag);
00065 GetNodeData(mean6_node,mean6);
00066
00067
00068 DOMNode* mean1_node = getChildNode(cellnode,mean1_tag);
00069 GetNodeData(mean1_node,mean1);
00070
00071
00072 DOMNode* rms12_node = getChildNode(cellnode,rms12_tag);
00073 GetNodeData(rms12_node,rms12);
00074
00075
00076 DOMNode* rms6_node = getChildNode(cellnode,rms6_tag);
00077 GetNodeData(rms6_node,rms6);
00078
00079
00080 DOMNode* rms1_node = getChildNode(cellnode,rms1_tag);
00081
00082
00083 GetNodeData(rms1_node,rms1);
00084
00085 record[detid].mean_x12 = mean12;
00086 record[detid].mean_x6 = mean6;
00087 record[detid].mean_x1 = mean1;
00088 record[detid].rms_x12 = rms12;
00089 record[detid].rms_x6 = rms6;
00090 record[detid].rms_x1 = rms1;
00091
00092 cellnode = cellnode->getNextSibling();
00093
00094 while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
00095 cellnode = cellnode->getNextSibling();
00096 chan++;
00097 }
00098
00099 delete parser;
00100 XMLPlatformUtils::Terminate();
00101 std::cout << " nb of channels found in xml file " << chan << std::endl;
00102 return 0;
00103 }
00104
00105 int EcalPedestalsXMLTranslator::writeXML(const std::string& filename,
00106 const EcalCondHeader& header,
00107 const EcalPedestals& record){
00108 std::fstream fs(filename.c_str(),ios::out);
00109 fs<< dumpXML(header,record);
00110 return 0;
00111 }
00112
00113
00114 std::string EcalPedestalsXMLTranslator::dumpXML(const EcalCondHeader& header,const EcalPedestals& record){
00115
00116 XMLPlatformUtils::Initialize();
00117 DOMImplementation* impl =
00118 DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00119
00120 DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00121 writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00122
00123 DOMDocumentType* doctype = impl->createDocumentType(fromNative("XML").c_str(), 0, 0 );
00124 DOMDocument * doc =
00125 impl->createDocument( 0, fromNative(Pedestals_tag).c_str(), doctype );
00126
00127 doc->setEncoding(fromNative("UTF-8").c_str() );
00128 doc->setStandalone(true);
00129 doc->setVersion(fromNative("1.0").c_str() );
00130
00131 DOMElement* root = doc->getDocumentElement();
00132
00133 xuti::writeHeader(root,header);
00134 if (!record.barrelItems().size()) return std::string();
00135 for(int cellid = EBDetId::MIN_HASH;
00136 cellid < EBDetId::kSizeForDenseIndexing;
00137 ++cellid) {
00138 uint32_t rawid = EBDetId::unhashIndex(cellid);
00139
00140 if (record.find(rawid) == record.end()) continue;
00141 if(!record[rawid].mean_x12 && !record[rawid].rms_x12) continue;
00142
00143 DOMElement* cellnode=writeCell(root,rawid);
00144
00145 WriteNodeWithValue(cellnode,mean12_tag,record[rawid].mean_x12);
00146 WriteNodeWithValue(cellnode,mean6_tag,record[rawid].mean_x6);
00147 WriteNodeWithValue(cellnode,mean1_tag,record[rawid].mean_x1);
00148 WriteNodeWithValue(cellnode,rms12_tag,record[rawid].rms_x12);
00149 WriteNodeWithValue(cellnode,rms6_tag,record[rawid].rms_x6);
00150 WriteNodeWithValue(cellnode,rms1_tag,record[rawid].rms_x1);
00151 }
00152
00153 if (!record.endcapItems().size()) return std::string();
00154 for(int cellid = 0;
00155 cellid < EEDetId::kSizeForDenseIndexing;
00156 ++cellid) {
00157 if(!EEDetId::validHashIndex(cellid)) continue;
00158
00159 uint32_t rawid = EEDetId::unhashIndex(cellid);
00160
00161 if (record.find(rawid) == record.end()) continue;
00162 if(!record[rawid].mean_x12 && !record[rawid].rms_x12) continue;
00163
00164 DOMElement* cellnode=writeCell(root,rawid);
00165
00166 WriteNodeWithValue(cellnode,mean12_tag,record[rawid].mean_x12);
00167 WriteNodeWithValue(cellnode,mean6_tag,record[rawid].mean_x6);
00168 WriteNodeWithValue(cellnode,mean1_tag,record[rawid].mean_x1);
00169 WriteNodeWithValue(cellnode,rms12_tag,record[rawid].rms_x12);
00170 WriteNodeWithValue(cellnode,rms6_tag,record[rawid].rms_x6);
00171 WriteNodeWithValue(cellnode,rms1_tag,record[rawid].rms_x1);
00172 }
00173
00174 std::string dump= toNative(writer->writeToString(*root));
00175 doc->release();
00176 return dump;
00177 }