CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/CondTools/Ecal/src/EcalLaserAPDPNRatiosXMLTranslator.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 "CondFormats/EcalObjects/interface/EcalLaserAPDPNRatios.h"
00013 #include "CondTools/Ecal/interface/EcalLaserAPDPNRatiosXMLTranslator.h"
00014 
00015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00016 #include "CondTools/Ecal/interface/XMLTags.h"
00017 
00018 using namespace XERCES_CPP_NAMESPACE;
00019 using namespace xuti;
00020 using namespace std;
00021 
00022 
00023 //
00024 // TODO: write and read time map
00025 //
00026 //
00027 
00028 
00029 
00030 int  EcalLaserAPDPNRatiosXMLTranslator::readXML(const std::string& filename, 
00031                                              EcalCondHeader& header,
00032                                              EcalLaserAPDPNRatios& record){
00033 
00034 
00035 
00036   XMLPlatformUtils::Initialize();
00037 
00038   XercesDOMParser* parser = new XercesDOMParser;
00039   parser->setValidationScheme( XercesDOMParser::Val_Never );
00040   parser->setDoNamespaces( false );
00041   parser->setDoSchema( false );
00042   
00043   parser->parse(filename.c_str());
00044 
00045   DOMDocument* xmlDoc = parser->getDocument();
00046   if (!xmlDoc) {
00047     std::cout << "EcalLaserAPDPNRatiosXMLTranslator::Error parsing document" << std::endl;
00048     return -1;
00049   }
00050 
00051   DOMElement* elementRoot = xmlDoc->getDocumentElement();
00052 
00053   xuti::readHeader(elementRoot,header);
00054 
00055   DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
00056 
00057   while(cellnode)
00058     {
00059       float p1 = 0;
00060       float p2 = 0;
00061       float p3 = 0;
00062 //       edm::TimeStamp t1=0;
00063 //       edm::TimeStamp t2=0;
00064 //       edm::TimeStamp t3=0;
00065 
00066 
00067       DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
00068 
00069       DOMNode* p1_node = getChildNode(cellnode,Laser_p1_tag);
00070       GetNodeData(p1_node,p1);
00071 
00072       DOMNode* p2_node = getChildNode(cellnode,Laser_p2_tag);
00073       GetNodeData(p2_node,p2);
00074 
00075       DOMNode* p3_node = getChildNode(cellnode,Laser_p3_tag);
00076       GetNodeData(p3_node,p3);
00077 
00078 //       DOMNode* t1_node = getChildNode(cellnode,Laser_t1_tag);
00079 //       GetNodeData(t1_node,t1);
00080 
00081 //       DOMNode* t2_node = getChildNode(cellnode,Laser_t2_tag);
00082 //       GetNodeData(t3_node,t2);
00083 
00084 //       DOMNode* p1_node = getChildNode(cellnode,Laser_t3_tag);
00085 //       GetNodeData(t3_node,t3);
00086 
00087  
00088       EcalLaserAPDPNRatios::EcalLaserAPDPNpair pair;
00089       pair.p1 =p1;
00090       pair.p2 =p2;
00091       pair.p3 =p3;
00092 
00093       record.setValue(detid,pair);
00094    
00095       cellnode = cellnode->getNextSibling();
00096 
00097       while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
00098         cellnode = cellnode->getNextSibling();
00099 
00100  
00101     }  
00102 
00103   delete parser;
00104   XMLPlatformUtils::Terminate();
00105   return 0;
00106   
00107   
00108 }
00109   
00110 
00111 
00112 
00113 
00114   int EcalLaserAPDPNRatiosXMLTranslator::writeXML(const std::string& filename, 
00115                                                const EcalCondHeader& header,
00116                                                const EcalLaserAPDPNRatios& record){
00117     std::fstream fs(filename.c_str(),ios::out);
00118     fs<< dumpXML(header,record);
00119     return 0;
00120   }
00121 
00122 
00123 std::string EcalLaserAPDPNRatiosXMLTranslator::dumpXML(
00124                                              const EcalCondHeader& header,
00125                                              const EcalLaserAPDPNRatios& record){
00126  
00127 
00128    XMLPlatformUtils::Initialize();
00129 
00130     DOMImplementation*  impl =
00131       DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00132 
00133     DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00134     writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00135 
00136     DOMDocumentType* doctype = impl->createDocumentType( fromNative("XML").c_str(), 0, 0 );
00137     DOMDocument *    doc = 
00138          impl->createDocument( 0, fromNative(Laser_tag).c_str(), doctype );
00139 
00140 
00141     doc->setEncoding(fromNative("UTF-8").c_str() );
00142     doc->setStandalone(true);
00143     doc->setVersion(fromNative("1.0").c_str() );
00144 
00145     
00146     DOMElement* root = doc->getDocumentElement();
00147  
00148     xuti::writeHeader(root,header);
00149 
00150     string Lasertag = "Laser", LMtag = "LM";
00151     for(int cellid = 0; cellid < (int)record.getTimeMap().size(); cellid++) {
00152 
00153       DOMElement* cellnode = doc->createElement( fromNative(Lasertag).c_str());
00154       root->appendChild(cellnode); 
00155       stringstream value_s;
00156       value_s << cellid;
00157       cellnode->setAttribute(fromNative(LMtag).c_str(),
00158                             fromNative(value_s.str()).c_str());
00159 
00160       long int t123[3];
00161       t123[0]=(record.getTimeMap())[cellid].t1.value();
00162       t123[1]=(record.getTimeMap())[cellid].t2.value();
00163       t123[2]=(record.getTimeMap())[cellid].t3.value();
00164       string Laser_t_tag[3] = {"t1","t2","t3"};
00165       for(int i = 0; i < 3; i++) {
00166       time_t t = t123[i] >> 32;
00167       char buf[256];
00168       struct tm lt;
00169       localtime_r(&t, &lt);
00170       strftime(buf, sizeof(buf), "%F %R:%S", &lt);
00171       buf[sizeof(buf)-1] = 0;
00172       DOMDocument* subdoc = cellnode->getOwnerDocument();
00173       DOMElement* new_node = subdoc->createElement(fromNative(Laser_t_tag[i]).c_str());
00174       cellnode->appendChild(new_node);
00175       std::stringstream value_ss;
00176       value_ss << t123[i];
00177       string newstr = value_ss.str() + " [" + string(buf) +"]";
00178       DOMText* tvalue = 
00179         subdoc->createTextNode(fromNative(newstr).c_str());
00180       new_node->appendChild(tvalue);
00181       }
00182     }
00183  
00184     for(int cellid = EBDetId::MIN_HASH;
00185         cellid < EBDetId::kSizeForDenseIndexing;
00186         ++cellid)
00187       {
00188 
00189         uint32_t rawid = EBDetId::unhashIndex(cellid);
00190 
00191         DOMElement* cellnode= writeCell(root,rawid);      
00192 
00193         float p1=(record.getLaserMap())[rawid].p1;
00194         float p2=(record.getLaserMap())[rawid].p2;
00195         float p3=(record.getLaserMap())[rawid].p3;
00196          
00197         WriteNodeWithValue(cellnode,Laser_p1_tag,p1);
00198         WriteNodeWithValue(cellnode,Laser_p2_tag,p2);
00199         WriteNodeWithValue(cellnode,Laser_p3_tag,p3);
00200                           
00201       }
00202 
00203 
00204 
00205    
00206     for(int cellid = 0;
00207         cellid < EEDetId::kSizeForDenseIndexing;
00208         ++cellid)
00209       {
00210           
00211         if(!EEDetId::validHashIndex(cellid)) continue;
00212           
00213         uint32_t rawid = EEDetId::unhashIndex(cellid); 
00214 
00215 
00216         DOMElement* cellnode=writeCell(root,rawid);
00217           
00218         float p1=(record.getLaserMap())[rawid].p1;
00219         float p2=(record.getLaserMap())[rawid].p2;
00220         float p3=(record.getLaserMap())[rawid].p3;
00221          
00222         WriteNodeWithValue(cellnode,Laser_p1_tag,p1);
00223         WriteNodeWithValue(cellnode,Laser_p2_tag,p2);
00224         WriteNodeWithValue(cellnode,Laser_p3_tag,p3);
00225       }
00226     
00227 
00228     std::string dump= toNative(writer->writeToString(*root)); 
00229     doc->release();
00230     //   XMLPlatformUtils::Terminate();
00231 
00232     return dump;
00233 
00234 }