CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/CondTools/Ecal/src/EcalTBWeightsXMLTranslator.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalTBWeightsXMLTranslator.h"
00002 #include <xercesc/dom/DOM.hpp>
00003 #include <xercesc/parsers/XercesDOMParser.hpp>
00004 #include <xercesc/util/PlatformUtils.hpp>
00005 #include <xercesc/util/XMLString.hpp>
00006 #include <xercesc/sax/SAXException.hpp>
00007 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00008 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00009 #include "CondTools/Ecal/interface/XMLTags.h"
00010 #include <fstream>
00011 
00012 using namespace xercesc;
00013 using namespace std;
00014 using namespace xuti;
00015 
00016 // ouch ! I have to define here the << and >> operator !
00017 
00018 std::ostream& operator<<( std::ostream& stream_,
00019                           const EcalXtalGroupId& id_ ) {
00020   return stream_ << id_.id();
00021 }  
00022 
00023 
00024 std::istream& operator>>( std::istream& stream_,
00025                           EcalXtalGroupId& id_ ) {
00026   unsigned int id;
00027   stream_ >> id;
00028   id_ =EcalXtalGroupId(id); 
00029   return stream_;
00030 }  
00031 
00032 
00033 
00034 
00035 int EcalTBWeightsXMLTranslator::readXML(const std::string& filename,
00036                                         EcalCondHeader& header,
00037                                         EcalTBWeights&  record){
00038 
00039   XMLPlatformUtils::Initialize();
00040 
00041   XercesDOMParser* parser = new XercesDOMParser;
00042   parser->setValidationScheme( XercesDOMParser::Val_Never );
00043   parser->setDoNamespaces( false );
00044   parser->setDoSchema( false );
00045   
00046   parser->parse(filename.c_str());
00047 
00048   DOMDocument* xmlDoc = parser->getDocument();
00049   if (!xmlDoc) {
00050     std::cout << "EcalTBWeightsXMLTranslator::Error parsing document" << std::endl;
00051     return -1;
00052   }
00053 
00054   DOMElement* elementRoot = xmlDoc->getDocumentElement();
00055   xuti::readHeader(elementRoot, header);
00056 
00057   DOMNode * wnode=getChildNode(elementRoot,EcalTBWeight_tag);
00058 
00059   while (wnode){
00060 
00061     DOMNode * gid_node = getChildNode(wnode,EcalXtalGroupId_tag);
00062     DOMNode * tdc_node = getChildNode(wnode,EcalTDCId_tag);
00063     DOMNode * ws_node =  getChildNode(wnode,EcalWeightSet_tag);
00064     
00065     EcalXtalGroupId          gid;
00066     EcalTBWeights::EcalTDCId tid;
00067     EcalWeightSet            ws;
00068 
00069     GetNodeData(gid_node,gid);
00070     GetNodeData(tdc_node,tid);
00071     
00072     readWeightSet(ws_node,ws);
00073 
00074     record.setValue(gid,tid,ws);
00075 
00076     wnode= wnode->getNextSibling();
00077 
00078     while (wnode&& wnode->getNodeType( ) != DOMNode::ELEMENT_NODE)      
00079       wnode= wnode->getNextSibling();
00080   }
00081 
00082   return 0;
00083 }
00084 
00085 int EcalTBWeightsXMLTranslator::writeXML(const  std::string& filename,
00086                                          const  EcalCondHeader& header,
00087                                          const  EcalTBWeights&  record){
00088   std::fstream fs(filename.c_str(),ios::out);
00089   fs<< dumpXML(header,record);
00090   return 0;  
00091 }
00092 
00093 
00094 void 
00095 EcalTBWeightsXMLTranslator::readWeightSet(xercesc::DOMNode* parentNode, 
00096                                           EcalWeightSet& ws){
00097 
00098 
00099 
00100   // get the first cell node
00101   DOMNode * wgtBSnode=getChildNode(parentNode,wgtBeforeSwitch_tag);
00102   DOMNode * wgtASnode=getChildNode(parentNode,wgtAfterSwitch_tag);
00103   DOMNode * wgtChi2BSnode=getChildNode(parentNode,wgtChi2BeforeSwitch_tag);
00104   DOMNode * wgtChi2ASnode=getChildNode(parentNode,wgtChi2AfterSwitch_tag);
00105 
00106 
00107   DOMNode* rownode = getChildNode(wgtBSnode,row_tag);  
00108 
00109   DOMElement* rowelement=0;
00110 
00111   // loop on row nodes
00112   while  (rownode){
00113 
00114     rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
00115 
00116     std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
00117 
00118     std::stringstream rowid_ss(rowid_s);
00119     int rowid = 0;
00120     rowid_ss >> rowid;
00121 
00122     std::string weightrow = toNative(rownode->getTextContent());
00123 
00124     std::stringstream weightrow_s(weightrow);
00125     double weight = 0;
00126     int i = 0;
00127     while(weightrow_s >> weight){
00128       ws.getWeightsBeforeGainSwitch()(rowid,i)= weight; 
00129       i++;
00130 
00131     }
00132 
00133     
00134     // get next cell
00135     rownode = rownode->getNextSibling();
00136     
00137     while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)      
00138       rownode = rownode->getNextSibling();
00139     
00140     
00141   }
00142 
00143   rownode = getChildNode(wgtASnode,row_tag);  
00144 
00145   // loop on row nodes
00146   while  (rownode){
00147 
00148     rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
00149     
00150     std::string rowid_s = toNative(rowelement->getAttribute( fromNative(id_tag).c_str()));
00151     
00152     std::stringstream rowid_ss(rowid_s);
00153     int rowid = 0;
00154     rowid_ss >> rowid;
00155 
00156     std::string weightrow = toNative(rownode->getTextContent());
00157     
00158     std::stringstream weightrow_s(weightrow);
00159     double weight = 0;
00160     int i = 0;
00161     while(weightrow_s >> weight){
00162       ws.getWeightsAfterGainSwitch()(rowid,i)= weight; 
00163       i++;
00164     }
00165     
00166     
00167     // get next cell
00168     rownode = rownode->getNextSibling();
00169     
00170     while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)      
00171       rownode = rownode->getNextSibling();
00172     
00173     
00174   }
00175   
00176   rownode = getChildNode(wgtChi2BSnode,row_tag);  
00177 
00178 
00179   // loop on row nodes
00180   while  (rownode){
00181 
00182     rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
00183     std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
00184 
00185     std::stringstream rowid_ss(rowid_s);
00186     int rowid = 0;
00187     rowid_ss >> rowid;
00188 
00189     std::string weightrow = toNative(rownode->getTextContent());
00190 
00191     std::stringstream weightrow_s(weightrow);
00192     double weight = 0;
00193     int i = 0;
00194     while(weightrow_s >> weight)
00195       {
00196         ws.getChi2WeightsBeforeGainSwitch()(rowid,i)= weight; 
00197         i++;
00198       }
00199     
00200     
00201     // get next cell
00202     rownode = rownode->getNextSibling();
00203     
00204     while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)      
00205       rownode = rownode->getNextSibling();
00206     
00207     
00208   }
00209 
00210 
00211   rownode = getChildNode(wgtChi2ASnode,row_tag);  
00212 
00213 
00214   // loop on row nodes
00215   while  (rownode){
00216 
00217     rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);  
00218     std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
00219 
00220     std::stringstream rowid_ss(rowid_s);
00221     int rowid = 0;
00222     rowid_ss >> rowid;
00223 
00224     std::string weightrow = toNative(rownode->getTextContent());
00225 
00226     std::stringstream weightrow_s(weightrow);
00227     double weight = 0;
00228     int i = 0;
00229     while(weightrow_s >> weight){
00230       ws.getChi2WeightsAfterGainSwitch()(rowid,i)= weight; 
00231       i++;
00232     }
00233     
00234     
00235     // get next cell
00236     rownode = rownode->getNextSibling();
00237     
00238     while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)      
00239       rownode = rownode->getNextSibling();
00240     
00241     
00242   }
00243 
00244 
00245 }
00246 
00247 void 
00248 EcalTBWeightsXMLTranslator::writeWeightSet(xercesc::DOMNode* parentNode, 
00249                                            const EcalWeightSet& ws){
00250   
00251   
00252   xercesc::DOMDocument* doc = parentNode->getOwnerDocument();
00253 
00254   DOMElement * weightsetel= doc->createElement( fromNative(EcalWeightSet_tag).c_str());
00255   parentNode-> appendChild(weightsetel);
00256 
00257   DOMElement* wgtBS = doc->createElement( fromNative(wgtBeforeSwitch_tag).c_str());
00258   weightsetel->appendChild(wgtBS);
00259   
00260   DOMElement* wgtAS = doc->createElement(fromNative(wgtAfterSwitch_tag).c_str());
00261   weightsetel->appendChild(wgtAS);
00262   
00263   DOMElement* wgtChi2BS = doc->createElement( fromNative(wgtChi2BeforeSwitch_tag).c_str());
00264   weightsetel->appendChild(wgtChi2BS);
00265   
00266   DOMElement* wgtChi2AS = doc->createElement(fromNative(wgtChi2AfterSwitch_tag).c_str());
00267   weightsetel->appendChild(wgtChi2AS);
00268   
00269   writeWeightMatrix(wgtBS,ws.getWeightsBeforeGainSwitch());
00270   writeWeightMatrix(wgtAS,ws.getWeightsAfterGainSwitch());
00271   
00272   writeChi2WeightMatrix(wgtChi2BS,ws.getChi2WeightsBeforeGainSwitch());
00273   writeChi2WeightMatrix(wgtChi2AS,ws.getChi2WeightsBeforeGainSwitch()); 
00274   
00275 }
00276 
00277 void 
00278 EcalTBWeightsXMLTranslator::writeWeightMatrix(xercesc::DOMNode* node,
00279                                               const EcalWeightSet::EcalWeightMatrix& matrix){
00280 
00281   DOMElement* row=0;
00282   DOMAttr* rowid=0;
00283   DOMText* rowvalue=0;
00284  
00285   const int ncols =10;
00286   const int nrows =3;
00287 
00288   for(int i=0;i<nrows;++i)
00289     {
00290 
00291 
00292       row= node->getOwnerDocument()->createElement( fromNative(row_tag).c_str());
00293       node->appendChild(row);
00294 
00295       stringstream value_s;
00296       value_s << i; 
00297 
00298       rowid = node->getOwnerDocument()->createAttribute(fromNative(id_tag).c_str());
00299       rowid ->setValue(fromNative(value_s.str()).c_str());
00300       row ->setAttributeNode(rowid);
00301 
00302       stringstream row_s;
00303 
00304       for(int k=0;k<ncols;++k) row_s <<" " << matrix(i,k)<<" " ;
00305 
00306       rowvalue = 
00307         node->getOwnerDocument()->createTextNode(fromNative(row_s.str()).c_str());
00308       row->appendChild(rowvalue);
00309     }//for loop on col
00310 
00311   
00312 }
00313 
00314 
00315 void 
00316 EcalTBWeightsXMLTranslator::writeChi2WeightMatrix(xercesc::DOMNode* node,
00317                                                   const EcalWeightSet::EcalChi2WeightMatrix& matrix){
00318 
00319   DOMElement* row=0;
00320   DOMAttr* rowid=0;
00321   DOMText* rowvalue=0;
00322  
00323   const int ncols =10;
00324   const int nrows =10;
00325 
00326   for(int i=0;i<nrows;++i)
00327     {
00328 
00329 
00330       row= node->getOwnerDocument()->createElement( fromNative(row_tag).c_str());
00331       node->appendChild(row);
00332 
00333       stringstream value_s;
00334       value_s << i; 
00335 
00336       rowid = node->getOwnerDocument()->createAttribute(fromNative(id_tag).c_str());
00337       rowid ->setValue(fromNative(value_s.str()).c_str());
00338       row ->setAttributeNode(rowid);
00339 
00340       stringstream row_s;
00341 
00342       for(int k=0;k<ncols;++k) row_s << " "<< matrix(i,k)<<" ";
00343       
00344       rowvalue = 
00345         node->getOwnerDocument()->createTextNode(fromNative(row_s.str()).c_str());
00346       row->appendChild(rowvalue);
00347     }//for loop on col
00348 
00349   
00350 }
00351 
00352 
00353 std::string EcalTBWeightsXMLTranslator::dumpXML(const EcalCondHeader& header,
00354                                                 const EcalTBWeights& record){
00355 
00356 
00357  XMLPlatformUtils::Initialize();
00358   
00359   DOMImplementation*  impl =
00360     DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00361   
00362   DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00363   writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00364   
00365   DOMDocumentType* doctype = impl->createDocumentType( fromNative("XML").c_str(), 0, 0 );
00366   DOMDocument *    doc = 
00367     impl->createDocument( 0, fromNative(EcalTBWeights_tag).c_str(), doctype );
00368   
00369   
00370   doc->setEncoding(fromNative("UTF-8").c_str() );
00371   doc->setStandalone(true);
00372   doc->setVersion(fromNative("1.0").c_str() );
00373   
00374   
00375   DOMElement* root = doc->getDocumentElement();
00376   xuti::writeHeader(root, header);
00377 
00378 
00379   const EcalTBWeights::EcalTBWeightMap wmap= record.getMap();
00380 
00381   EcalTBWeights::EcalTBWeightMap::const_iterator it ;
00382   for (it =wmap.begin(); it!=wmap.end(); ++it){
00383    
00384     DOMElement * tbweight= doc->createElement(fromNative(EcalTBWeight_tag).c_str());
00385     root->appendChild(tbweight);
00386 
00387     EcalXtalGroupId          gid = it->first.first;
00388     EcalTBWeights::EcalTDCId tid = it->first.second;
00389     EcalWeightSet            ws  = it->second;
00390 
00391     WriteNodeWithValue(tbweight,EcalXtalGroupId_tag, gid);
00392     WriteNodeWithValue(tbweight,EcalTDCId_tag, tid);
00393     writeWeightSet(tbweight,ws);
00394 
00395      
00396   } //
00397 
00398   std::string dump= toNative(writer->writeToString(*root)); 
00399   doc->release();
00400   
00401   //   XMLPlatformUtils::Terminate();
00402 
00403   return dump;
00404 }