CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CondTools/Ecal/src/DOMHelperFunctions.cc

Go to the documentation of this file.
00001 
00008 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00009 #include "CondTools/Ecal/interface/XMLTags.h"
00010 #include "CondTools/Ecal/interface/XercesString.h"
00011 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00012 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00013 #include "DataFormats/EcalDetId/interface/EcalTrigTowerDetId.h"
00014 #include <xercesc/dom/DOM.hpp>
00015 #include <xercesc/dom/DOMNode.hpp>
00016 #include <xercesc/parsers/XercesDOMParser.hpp>
00017 #include <xercesc/util/PlatformUtils.hpp>
00018 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00019 #include <sstream>
00020 
00021 using namespace std;
00022 using namespace xuti;
00023 using namespace xercesc;
00024 
00025 const DetId xuti::readCellId(xercesc::DOMElement* node){
00026   
00027 
00028   int ieta =0;
00029   int iphi =0;
00030   int ix   =0;
00031   int iy   =0;
00032   int ixSC =0;
00033   int iySC =0;
00034   int zside=0;
00035   
00036   stringstream ieta_str ;
00037   stringstream iphi_str;
00038   stringstream ix_str;
00039   stringstream iy_str ;
00040   stringstream ixSC_str;
00041   stringstream iySC_str ;
00042   stringstream zside_str ;
00043 
00044  
00045   ieta_str << toNative(node->getAttribute(fromNative(iEta_tag).c_str()));
00046   iphi_str << toNative(node->getAttribute(fromNative(iPhi_tag).c_str()));
00047   ix_str   << toNative(node->getAttribute(fromNative(ix_tag).c_str()));
00048   iy_str   << toNative(node->getAttribute(fromNative(iy_tag).c_str()));
00049   ixSC_str << toNative(node->getAttribute(fromNative(ixSC_tag).c_str()));
00050   iySC_str << toNative(node->getAttribute(fromNative(iySC_tag).c_str()));
00051   zside_str<< toNative(node->getAttribute(fromNative(zside_tag).c_str()));
00052   
00053   ieta_str>> ieta;
00054   iphi_str>> iphi;
00055   ix_str  >> ix;
00056   iy_str  >> iy;
00057   ixSC_str >> ixSC;
00058   iySC_str >> iySC;
00059   zside_str >> zside;
00060 
00061   if (ieta && iphi)        {return EBDetId(ieta,iphi);}
00062   if (ix   && iy  && zside){return EEDetId(ix,iy,zside);}
00063   if (ixSC && iySC && zside){return EcalScDetId(ixSC, iySC, zside);}
00064   
00065   cerr<<"XMLCell: error reading cell, missing field ?"<<std::endl;
00066   return 0;
00067  
00068 }
00069 
00070 
00071 
00072 DOMElement*  xuti::writeCell(xercesc::DOMNode* node, 
00073                   const DetId& detid){
00074   
00075   DOMElement* cell_node = 
00076       node->getOwnerDocument()->createElement( fromNative(Cell_tag).c_str());
00077 
00078   node->appendChild(cell_node);
00079   
00080   if (detid.subdetId() == EcalBarrel ){ 
00081    
00082     stringstream value_s;
00083     value_s <<EBDetId(detid).ieta() ;
00084 
00085     cell_node->setAttribute(fromNative(iEta_tag).c_str(),
00086                             fromNative(value_s.str()).c_str());
00087     value_s.str("");
00088     value_s <<EBDetId(detid).iphi() ;
00089 
00090     cell_node->setAttribute(fromNative(iPhi_tag).c_str(),
00091                             fromNative(value_s.str()).c_str());
00092 
00093   } else if (detid.subdetId() == EcalEndcap){
00094     
00095     // is it a EcalScDetId ?
00096     unsigned int ScIdCheck = detid.rawId() & 0x00008000;
00097     if (ScIdCheck == 0) {
00098       stringstream value_s;
00099       value_s <<EEDetId(detid).ix() ;
00100   
00101       cell_node->setAttribute(fromNative(ix_tag).c_str(),
00102                               fromNative(value_s.str()).c_str());
00103       value_s.str("");
00104       value_s <<EEDetId(detid).iy() ;
00105 
00106       cell_node->setAttribute(fromNative(iy_tag).c_str(),
00107                               fromNative(value_s.str()).c_str());
00108       value_s.str("");
00109       value_s <<EEDetId(detid).zside() ;
00110 
00111       cell_node->setAttribute(fromNative(zside_tag).c_str(),
00112                               fromNative(value_s.str()).c_str());
00113     }
00114     else {
00115       stringstream value_s;
00116       value_s << EcalScDetId(detid).ix() ;
00117   
00118       cell_node->setAttribute(fromNative(ixSC_tag).c_str(),
00119                               fromNative(value_s.str()).c_str());
00120       value_s.str("");
00121       value_s << EcalScDetId(detid).iy() ;
00122 
00123       cell_node->setAttribute(fromNative(iySC_tag).c_str(),
00124                               fromNative(value_s.str()).c_str());
00125       value_s.str("");
00126       value_s << EcalScDetId(detid).zside() ;
00127 
00128       cell_node->setAttribute(fromNative(zside_tag).c_str(),
00129                               fromNative(value_s.str()).c_str());
00130     }
00131 
00132   } else if (detid.subdetId() == EcalTriggerTower ){ 
00133     stringstream value_s;
00134     value_s <<EcalTrigTowerDetId(detid).ieta() ;
00135   
00136     cell_node->setAttribute(fromNative(iEta_tag).c_str(),
00137                             fromNative(value_s.str()).c_str());
00138     value_s.str("");
00139     value_s <<EcalTrigTowerDetId(detid).iphi() ;
00140 
00141     cell_node->setAttribute(fromNative(iPhi_tag).c_str(),
00142                             fromNative(value_s.str()).c_str());
00143   }
00144   return cell_node;
00145 }
00146   
00147 // return 0 if not found
00148 DOMNode * xuti::getChildNode(DOMNode * node,  const std::string& nodename ){
00149 
00150    if (!node) return 0;
00151 
00152   for (DOMNode* childNode = node->getFirstChild(); 
00153        childNode; childNode = childNode->getNextSibling()) {
00154     
00155     if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
00156       
00157       const string foundName = toNative(childNode->getNodeName());
00158       
00159       if (foundName == nodename) return childNode;
00160     }// if element 
00161   }// for child 
00162   
00163   return 0;
00164 
00165 }
00166 
00167 
00168 
00169 void xuti::writeHeader (xercesc::DOMNode* parentNode, 
00170                         const EcalCondHeader& header ){
00171 
00172 
00173   
00174   DOMElement* headernode =
00175     parentNode->getOwnerDocument()->createElement( fromNative(Header_tag).c_str());
00176   parentNode->appendChild(headernode);
00177 
00178   // write the actual header
00179   WriteNodeWithValue(headernode, Header_methodtag, header.method_);
00180   WriteNodeWithValue(headernode, Header_versiontag, header.version_);
00181   WriteNodeWithValue(headernode, Header_datasourcetag, header.datasource_);
00182   WriteNodeWithValue(headernode, Header_sincetag, header.since_);
00183   WriteNodeWithValue(headernode, Header_tagtag, header.tag_);
00184   WriteNodeWithValue(headernode, Header_datetag, header.date_);
00185 
00186 }
00187 
00188 
00189 void xuti::readHeader(xercesc::DOMNode* parentNode, 
00190                       EcalCondHeader& header){
00191 
00192   DOMNode * hnode = getChildNode(parentNode,Header_tag);
00193 
00194   DOMNode * node  = getChildNode(hnode,Header_methodtag);
00195   GetNodeStringData(node,header.method_);
00196 
00197   node  = getChildNode(hnode,Header_versiontag);
00198   GetNodeStringData(node,header.version_);
00199 
00200   node  = getChildNode(hnode,Header_datasourcetag);
00201   GetNodeStringData(node,header.datasource_);
00202 
00203   node  = getChildNode(hnode,Header_sincetag);
00204   GetNodeData(node,header.since_);
00205 
00206   node  = getChildNode(hnode,Header_tagtag);
00207   GetNodeStringData(node,header.tag_);
00208 
00209   
00210   node  = getChildNode(hnode,Header_datetag);
00211   GetNodeStringData(node,header.date_);
00212 
00213 }
00214 
00215 int xuti::readHeader(const std::string& filename,EcalCondHeader& header ){
00216   
00217   XMLPlatformUtils::Initialize();
00218 
00219   XercesDOMParser* parser = new XercesDOMParser;
00220   parser->setValidationScheme( XercesDOMParser::Val_Never );
00221   parser->setDoNamespaces( false );
00222   parser->setDoSchema( false );
00223   
00224   parser->parse(filename.c_str());
00225 
00226   DOMDocument* xmlDoc = parser->getDocument();
00227   
00228 
00229   if (!xmlDoc) {
00230     std::cout << "Error parsing document" << std::endl;
00231     return -1;
00232   }
00233 
00234   DOMElement* elementRoot = xmlDoc->getDocumentElement();
00235 
00236   xuti::readHeader(elementRoot, header);
00237   
00238   delete parser;
00239   XMLPlatformUtils::Terminate();
00240 
00241   return 0;
00242 }
00243 
00244 void xuti:: GetNodeStringData(xercesc::DOMNode* node, std::string& value){
00245   value=  toNative(node->getTextContent());       
00246 }
00247   
00248