Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Translator.cpp

Go to the documentation of this file.
00001  /**********************************************************************
00002   *                                                                    *
00003   * Copyright (c) 2002 Lorenzo Moneta, CERN/IT                       *
00004   *                   <Lorenzo.Moneta.cern.ch>                       *
00005   *                                                                    *
00006   * This library is free software; you can redistribute it and/or      *
00007   * modify it under the terms of the GNU Lesser General Public         *
00008   * License as published by the Free Software Foundation; either       *
00009   * version 2.1 of the License, or (at your option) any later version. *
00010   *                                                                    *
00011   * This library is distributed in the hope that it will be useful,    *
00012   * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
00013   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00014   * Le ser General Public License for more details.                    *
00015   *                                                                    *
00016   * You should have received a copy of the GNU Lesser General Public   *
00017   * License along with this library (see file COPYING); if not, write  *
00018   * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00019   * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00020   *                                                                    *
00021   **********************************************************************/
00022 
00023 // Implementation file for class Translator
00024 // 
00025 // Created by: moneta  at Fri Sep 13 10:43:55 2002
00026 // 
00027 // Last update: Fri Sep 13 10:43:55 2002
00028 // 
00029 
00030 #include "Translator.h"
00031 
00032 #include "AIDA/IAxis.h"
00033 #include "AIDA/IAnnotation.h"
00034 #include "AIDA/IConstants.h"
00035 #include "DataXML/DataObject.h"
00036 
00037 #ifdef OLDSTREAMS
00038 # include <strstream>
00039 # define ostringstream ostrstream
00040 # define istringstream istrstream
00041 #else
00042 # include <sstream>
00043 #endif
00044 
00045 #include <algorithm>
00046 
00047 namespace Anaphe {
00048 
00049   namespace AIDA_XMLStore { 
00050 
00052 //                              //
00053 // static members of Translator //
00054 //                              //
00056 
00057     const int     Translator::SCI_PRECISION          = 8;
00058     const std::string Translator::XML_VERSION        = "1.0";
00059     const std::string Translator::ENCODING_TYPE      = "ISO-8859-1";
00060     const std::string Translator::PACKAGE            = "Anaphe";
00061     const std::string Translator::PACKAGE_VERSION    = "5.0.0";  // set by hand 
00062 
00063   }
00064 }
00065 
00066 
00067 Anaphe::AIDA_XMLStore::Translator::Translator() 
00068 {
00069 }
00070 
00071 Anaphe::AIDA_XMLStore::Translator::~Translator() 
00072 {
00073 }
00074 
00075 Anaphe::AIDA_XMLStore::Translator::Translator(const Anaphe::AIDA_XMLStore::Translator &) 
00076 {
00077 }
00078 
00079 Anaphe::AIDA_XMLStore::Translator & Anaphe::AIDA_XMLStore::Translator::operator = (const Anaphe::AIDA_XMLStore::Translator &rhs) 
00080 {
00081    if (this == &rhs) return *this;  // time saving self-test
00082 
00083    return *this;
00084 }
00085 
00087 
00088 bool Anaphe::AIDA_XMLStore::Translator::appendAidaHeader(DataXML::DataObject & aidaElement) 
00089 { 
00090   aidaElement.setName("aida"); 
00091   aidaElement.setAttribute("version",VERSION);
00092   DataXML::DataObject implementationElement; 
00093   implementationElement.setName("implementation");
00094   implementationElement.setAttribute("package",Anaphe::AIDA_XMLStore::Translator::PACKAGE);
00095   implementationElement.setAttribute("version",Anaphe::AIDA_XMLStore::Translator::PACKAGE_VERSION);
00096   aidaElement.appendChild(implementationElement);   
00097   return true; 
00098 }
00099 
00101 
00102 bool Anaphe::AIDA_XMLStore::Translator::appendObjectHeader(DataXML::DataObject & objElement, const std::string & objType, const std::string & name, const std::string & title, const std::string & path, const std::string & options)
00103 { 
00104   // type identifies the object (e.g. histogram1d..)
00105   objElement.setName(objType);
00106   objElement.setAttribute("name", name);
00107   // optional arguments 
00108   if (!title.empty())
00109     objElement.setAttribute("title", title);
00110   if (!path.empty())
00111     objElement.setAttribute("path", path);
00112   if (!options.empty())
00113     objElement.setAttribute("options", options);
00114   return true;  
00115 }
00116 
00118 
00119 bool Anaphe::AIDA_XMLStore::Translator::appendAnnotation(DataXML::DataObject & objElement, const AIDA::IAnnotation& anno) 
00120 { 
00121   // add annotation child
00122   DataXML::DataObject annoElement;
00123   annoElement.setName("annotation");
00124   for (int i = 0; i < anno.size() ; ++i) {  
00125     DataXML::DataObject itemElement;
00126     itemElement.setName("item");     
00127     itemElement.setAttribute("key", anno.key(i));
00128     itemElement.setAttribute("value", anno.value(i));
00129     // retieve stckness from dev level 
00130     // to do.....
00131     annoElement.appendChild(itemElement); 
00132   }
00133   objElement.appendChild(annoElement);
00134   return true;
00135 }
00136 
00138 
00139 bool Anaphe::AIDA_XMLStore::Translator::appendAxis(DataXML::DataObject & objElement, const std::string& axisName, const AIDA::IAxis& axis) 
00140 { 
00141 
00142     // add axis child 
00143   DataXML::DataObject axisElement;
00144   axisElement.setName("axis");
00145   axisElement.setAttribute("direction", axisName);
00146   axisElement.setAttribute("min",          toString(axis.lowerEdge()));
00147   axisElement.setAttribute("max",          toString(axis.upperEdge()));
00148   axisElement.setAttribute("numberOfBins", toString(axis.bins()));
00149 
00150   // for variable Bins axis
00151   if (!axis.isFixedBinning() ) {
00152     DataXML::DataObject borderElement;
00153     borderElement.setName("binBorder");
00154     const int nEdges = axis.bins() - 1;
00155     for (int i = 0; i < nEdges; ++i) {
00156       borderElement.setAttribute("value", toString(axis.binUpperEdge(i)));
00157       axisElement.appendChild(borderElement);
00158     }
00159   }
00160   objElement.appendChild(axisElement);
00161   return true;
00162 }
00163 
00165 
00166 bool Anaphe::AIDA_XMLStore::Translator::appendStatistics(DataXML::DataObject & statisticsElement, const std::string& axisName, 
00167                         double mean, double rms, double skew)
00168 {
00169 
00170   DataXML::DataObject statElement;
00171   statElement.setName("statistic");
00172   statElement.setAttribute("direction", axisName);
00173   statElement.setAttribute("mean",toString(mean));
00174   statElement.setAttribute("rms", toString(rms));
00175   if (skew != 0) statElement.setAttribute("skewness", toString(skew));
00176   statisticsElement.appendChild(statElement);
00177   return true;
00178   
00179 }
00180 
00182 
00183 bool Anaphe::AIDA_XMLStore::Translator::appendHisto1DBinData(DataXML::DataObject & dataElement, int xbin, int entries, double height, double error, double wMean, double rms, double wRms, double error2)
00184 
00185 {
00186   // skip bins with sero entries
00187   if (entries == 0) return false; 
00188   DataXML::DataObject binElement;
00189   binElement.setName(std::string("bin1d"));
00190   binElement.setAttribute("binNum",binNumToString(xbin));
00191   binElement.setAttribute("height",  toString(height) );
00192   binElement.setAttribute("error",   toString(error)  );
00193   binElement.setAttribute("entries", toString(entries));
00194   if (wMean != 0) 
00195     binElement.setAttribute("weightedMean", toString(wMean));
00196   if (rms != 0) 
00197     binElement.setAttribute("rms", toString(rms));
00198   if (wRms != 0) 
00199     binElement.setAttribute("weightedRms", toString(wRms));
00200   if (error2 != 0) 
00201     binElement.setAttribute("error2", toString(error2));
00202 
00203   dataElement.appendChild(binElement);  
00204   return true;
00205 }
00206 
00207 
00209 
00210 bool Anaphe::AIDA_XMLStore::Translator::appendHisto2DBinData(DataXML::DataObject & dataElement, int xbin, int ybin, int entries, double height, double error, double wMeanX, double wMeanY, double rms)
00211 
00212 {
00213 
00214   // skip bins with sero entries
00215   if (entries == 0) return false; 
00216   DataXML::DataObject binElement;
00217   binElement.setName(std::string("bin2d") );
00218   binElement.setAttribute("binNumX",binNumToString(xbin));
00219   binElement.setAttribute("binNumY",binNumToString(ybin));
00220   binElement.setAttribute("height",  toString(height) );
00221   binElement.setAttribute("error",   toString(error)  );
00222   binElement.setAttribute("entries", toString(entries));
00223   if (wMeanX != 0 || wMeanY != 0 ) {
00224       binElement.setAttribute("weightedMeanX", toString(wMeanX));
00225       binElement.setAttribute("weightedMeanY", toString(wMeanY));
00226   }
00227   if (rms != 0) 
00228     binElement.setAttribute("rms", toString(rms));
00229 
00230   dataElement.appendChild(binElement);  
00231   return true;
00232 }
00233 
00235 
00236 bool Anaphe::AIDA_XMLStore::Translator::appendHisto3DBinData(DataXML::DataObject & dataElement, int xbin, int ybin, int zbin, int entries, double height, double error, double wMeanX, double wMeanY, double wMeanZ, double rms)
00237 
00238 {
00239 
00240   // skip bins with sero entries
00241   if (entries == 0) return false; 
00242   DataXML::DataObject binElement;
00243   binElement.setName(std::string("bin3d") );
00244   binElement.setAttribute("binNumX",binNumToString(xbin));
00245   binElement.setAttribute("binNumY",binNumToString(ybin));
00246   binElement.setAttribute("binNumZ",binNumToString(zbin));
00247   binElement.setAttribute("height",  toString(height) );
00248   binElement.setAttribute("error",   toString(error)  );
00249   binElement.setAttribute("entries", toString(entries));
00250   if (wMeanX != 0 || wMeanY != 0  || wMeanZ != 0 ) {
00251       binElement.setAttribute("weightedMeanX", toString(wMeanX));
00252       binElement.setAttribute("weightedMeanY", toString(wMeanY));
00253       binElement.setAttribute("weightedMeanZ", toString(wMeanZ));
00254   }
00255   if (rms != 0) 
00256     binElement.setAttribute("rms", toString(rms));
00257 
00258   dataElement.appendChild(binElement);  
00259   return true;
00260 }
00261   
00262 bool Anaphe::AIDA_XMLStore::Translator::appendCloudEntry(DataXML::DataObject & dataElement, int dim, double xval, double yval, double zval, double weight ) 
00263 { 
00264 
00265   DataXML::DataObject entryElement;
00266   entryElement.setName(std::string("entry")+toString(dim)+std::string("d"));
00267 
00268   if (dim >= 1) entryElement.setAttribute("valueX", toString(xval));
00269   if (dim >= 2) entryElement.setAttribute("valueY", toString(yval));
00270   if (dim >= 3) entryElement.setAttribute("valueZ", toString(zval));
00271   if (weight != 1.0) 
00272     entryElement.setAttribute("weight",  toString(weight) );
00273 
00274   dataElement.appendChild(entryElement);  
00275   return true;
00276 }
00277 
00278 bool Anaphe::AIDA_XMLStore::Translator::appendDataPointMeasurement(DataXML::DataObject & dataElement,  double value, double eminus, double eplus) 
00279 { 
00280 
00281   DataXML::DataObject measurementElement;
00282   measurementElement.setName("measurement");
00283   measurementElement.setAttribute("value",  toString(value) );
00284   if (eplus != 0)
00285     measurementElement.setAttribute("errorPlus",  toString(eplus) );
00286   if (eminus != 0)
00287     measurementElement.setAttribute("errorMinus",  toString(eminus) );
00288 
00289   dataElement.appendChild(measurementElement);  
00290 
00291   return true;
00292 }
00293 
00295 
00296 void Anaphe::AIDA_XMLStore::Translator::getObjectHeader(const DataXML::DataObject & obj, std::string & name, std::string & title, std::string & path, std::string & options) 
00297 { 
00298   name = obj.getAttributeValue("name"); 
00299   title = obj.getAttributeValue("title");
00300   // try for label in case title does not exist 
00301   if (title.empty()) 
00302     title = obj.getAttributeValue("label");
00303     
00304   path = obj.getAttributeValue("path");
00305   options = obj.getAttributeValue("options");
00306 }
00307 
00309 void Anaphe::AIDA_XMLStore::Translator::getAnnotation(const DataXML::DataObject & obj,AnnotationData & annoData) { 
00310   const DataXML::DataObject * anno_obj = obj.getChild("annotation"); 
00311   if (!anno_obj) return;   
00312   for (ElementIterator i = anno_obj->children().begin(); i != anno_obj->children().end(); ++i) {
00313     if (i->name() == "item") { 
00314       bool stickness = (i->getAttributeValue("sticky") == "true"); 
00315       annoData.push_back(std::make_pair(i->getAttributeValue("key"),std::make_pair(i->getAttributeValue("value"),stickness) ) );
00316     }
00317   }
00318 }
00319 // set Annotation on histograms
00320 
00321     void Anaphe::AIDA_XMLStore::Translator::setAnnotation(AIDA::IAnnotation * anno, const AnnotationData  & annoData) { 
00322       int nItem = annoData.size(); 
00323       for (int i = 0; i < nItem; ++i ) {
00324         bool ret = anno->addItem(annoData[i].first,(annoData[i].second).first,(annoData[i].second).second); 
00325         if (!ret) { // item exists just set them 
00326           anno->setValue(annoData[i].first, (annoData[i].second).first ); 
00327           anno->setSticky(annoData[i].first,(annoData[i].second).second );
00328         }
00329       }
00330     }
00331 
00333 
00334 void Anaphe::AIDA_XMLStore::Translator::getAxis(const DataXML::DataObject & obj, const std::string & axisName, int& nBins, double& low, double& high, std::vector<double> & edges) 
00335 { 
00336   // extract the parameters of the histogram axis, including bin edges if necessary
00337   for (ElementIterator i = obj.children().begin(); i != obj.children().end(); ++i) {
00338     if (i->name() == "axis" && i->getAttributeValue("direction") == axisName) {
00339       // get common parameters
00340       toValue(i->getAttributeValue("min"),          low);
00341       toValue(i->getAttributeValue("max"),          high);
00342       toValue(i->getAttributeValue("numberOfBins"), nBins);
00343 
00344       // and then get variable bin edges (if available)
00345 
00346       edges.clear(); 
00347       for (ElementIterator j = i->children().begin(); j != i->children().end(); ++j) { 
00348         if (j->name() == "binBorder") {
00349           double binEdge = 0.0;
00350           if (toValue(i->getAttributeValue("value"), binEdge)) 
00351             edges.push_back(binEdge); 
00352         }
00353       }
00354       // check if edges vector has the right size otherwise used equal bins (empty vector) 
00355       if ( nBins != static_cast<int>(edges.size())-1)
00356         edges.clear(); 
00357       // if OK sort vector in correct order of the edges 
00358       else 
00359         std::sort(edges.begin(), edges.end() ); 
00360 
00361     }
00362   }
00363 }
00364 
00366 
00367 void Anaphe::AIDA_XMLStore::Translator::getStatistics(const DataXML::DataObject & statisticsElement, const std::string & axisName, double & mean, double & rms, double & skew) 
00368 { 
00369   for (ElementIterator i = statisticsElement.children().begin();
00370        i != statisticsElement.children().end(); ++i) {
00371     if (i->name() == "statistic" && i->getAttributeValue("direction") == axisName) {
00372       toValue(i->getAttributeValue("mean"),     mean);
00373       toValue(i->getAttributeValue("rms"),      rms);
00374       toValue(i->getAttributeValue("skewness"), skew);
00375       return;
00376     }
00377   }
00378   //  missingElementError("statistic ("+axisName+" axis)");
00379 }
00380 
00382 
00383 void Anaphe::AIDA_XMLStore::Translator::getHisto1DBinData(const DataXML::DataObject &  binElement, int & xbin, int & entries, double & height, double & error, double & wMean, double & wRms, double & error2, double & rms)
00384 { 
00385 
00386   xbin = stringToBinNum(binElement.getAttributeValue("binNum"));
00387 
00388   toValue(binElement.getAttributeValue("entries"),entries);
00389   toValue(binElement.getAttributeValue("height"),height);
00390   toValue(binElement.getAttributeValue("error"),error);
00391   toValue(binElement.getAttributeValue("weightedMean"),wMean);
00392   toValue(binElement.getAttributeValue("weightedRms"),wRms);
00393   toValue(binElement.getAttributeValue("error2"),error2);
00394   toValue(binElement.getAttributeValue("rms"),rms);
00395 
00396 }
00397 
00398 
00399 void Anaphe::AIDA_XMLStore::Translator::getHisto2DBinData(const DataXML::DataObject &  binElement, int & xbin, int & ybin, int & entries, double & height, double & error, double & wMeanX, double & wMeanY, double & wRmsX, double & wRmsY, double & error2, double & rms)
00400 { 
00401 
00402   xbin = stringToBinNum(binElement.getAttributeValue("binNumX"));
00403   ybin = stringToBinNum(binElement.getAttributeValue("binNumY"));
00404 
00405   toValue(binElement.getAttributeValue("entries"),entries);
00406   toValue(binElement.getAttributeValue("height"),height);
00407   toValue(binElement.getAttributeValue("error"),error);
00408   toValue(binElement.getAttributeValue("weightedMeanX"),wMeanX);
00409   toValue(binElement.getAttributeValue("weightedMeanY"),wMeanY);
00410   toValue(binElement.getAttributeValue("weightedRmsX"),wRmsX);
00411   toValue(binElement.getAttributeValue("weightedRmsY"),wRmsY);
00412   toValue(binElement.getAttributeValue("error2"),error2);
00413   toValue(binElement.getAttributeValue("rms"),rms);
00414 
00415 }
00416 
00417 
00418 void Anaphe::AIDA_XMLStore::Translator::getHisto3DBinData(const DataXML::DataObject &  binElement, int & xbin, int & ybin, int & zbin, int & entries, double & height, double & error, double & wMeanX, double & wMeanY, double & wMeanZ, double & wRmsX, double & wRmsY, double & wRmsZ, double & error2, double & rms)
00419 { 
00420 
00421   xbin = stringToBinNum(binElement.getAttributeValue("binNumX"));
00422   ybin = stringToBinNum(binElement.getAttributeValue("binNumY"));
00423   zbin = stringToBinNum(binElement.getAttributeValue("binNumZ"));
00424 
00425   toValue(binElement.getAttributeValue("entries"),entries);
00426   toValue(binElement.getAttributeValue("height"),height);
00427   toValue(binElement.getAttributeValue("error"),error);
00428   toValue(binElement.getAttributeValue("weightedMeanX"),wMeanX);
00429   toValue(binElement.getAttributeValue("weightedMeanY"),wMeanY);
00430   toValue(binElement.getAttributeValue("weightedMeanZ"),wMeanZ);
00431   toValue(binElement.getAttributeValue("weightedRmsX"),wRmsX);
00432   toValue(binElement.getAttributeValue("weightedRmsY"),wRmsY);
00433   toValue(binElement.getAttributeValue("weightedRmsZ"),wRmsZ);
00434   toValue(binElement.getAttributeValue("error2"),error2);
00435   toValue(binElement.getAttributeValue("rms"),rms);
00436 
00437 }
00438 
00440 
00441 void Anaphe::AIDA_XMLStore::Translator::getCloudEntryData(const DataXML::DataObject &  entryElement, const int dim, double & xval, double & yval, double & zval, double & weight )
00442 { 
00443   if (dim >= 1)   toValue(entryElement.getAttributeValue("valueX"),xval);
00444   if (dim >= 2)   toValue(entryElement.getAttributeValue("valueY"),yval);
00445   if (dim >= 3)   toValue(entryElement.getAttributeValue("valueZ"),zval);
00446 
00447   toValue(entryElement.getAttributeValue("weight"),weight);
00448 }
00449 
00451 
00452 void Anaphe::AIDA_XMLStore::Translator::getDataPointMeasurement(const DataXML::DataObject & dataElement,  double & value, double & eminus, double & eplus) 
00453 { 
00454   toValue(dataElement.getAttributeValue("value"),value);
00455   toValue(dataElement.getAttributeValue("errorPlus"),eminus);
00456   toValue(dataElement.getAttributeValue("errorMinus"),eplus);
00457 }
00458 
00459 
00461 
00462 std::string Anaphe::AIDA_XMLStore::Translator::toString(int number) const
00463 {
00464   // specialisation for integers
00465   std::ostringstream buf;
00466   buf << number;
00467 #ifndef BADENDS
00468   buf << std::ends;
00469 #endif
00470   std::string ret = buf.str();
00471   return ret;
00472 }
00473 
00479 template<class T>
00480 bool Anaphe::AIDA_XMLStore::Translator::toValue(std::string s, T& val)
00481 {
00482   std::istringstream buf(s.c_str());
00483   buf >> std::ws;
00484   if ( buf.eof() || (!buf.good() ) ) return false;
00485   buf >> val;
00486   return true;
00487 }
00488 
00490 
00491 std::string Anaphe::AIDA_XMLStore::Translator::binNumToString(int ibin) 
00492 {
00493   std::string id;
00494   if (ibin == AIDA::IAxis::UNDERFLOW_BIN)  id = "UNDERFLOW";
00495   else if (ibin == AIDA::IAxis::OVERFLOW_BIN)  id = "OVERFLOW";
00496   else                      id = toString(ibin);
00497   return id; 
00498 }
00499 
00500 int Anaphe::AIDA_XMLStore::Translator::stringToBinNum(const std::string & sbin) 
00501 {
00502   int ibin = -9999;
00503   if (sbin == "UNDERFLOW") 
00504     ibin =   static_cast<int>( AIDA::IAxis::UNDERFLOW_BIN);
00505   else if (sbin == "OVERFLOW") 
00506     ibin =  static_cast<int> ( AIDA::IAxis::OVERFLOW_BIN);
00507   else
00508     toValue(sbin,ibin); 
00509 
00510   return ibin; 
00511 }
00512 
00513 
00514 

Generated on Tue Nov 19 12:32:57 2002 for AIDA_XMLStore by doxygen1.2.16