CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/CondTools/Ecal/src/EcalLaserAPDPNRatiosXMLTranslator.cc

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