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

Histo2DTranslator.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   * Lesser 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 Histo2DTranslator
00024 // 
00025 // Created by: moneta  at Thu Sep 12 10:41:06 2002
00026 // 
00027 // Last update: Thu Sep 12 10:41:06 2002
00028 // 
00029 
00030 #include "Histo2DTranslator.h"
00031 #include "AIDA/IHistogram2D.h"
00032 #include "AIDA_Dev/IDevHistogram2D.h"
00033 #include "AIDA_Dev/IDevHistogramFactory.h"
00034 #include "AIDA/IAxis.h"
00035 
00036 #include "DataXML/DataObject.h"
00037 
00038 static const std::string emptyString = "";
00039 
00041 Anaphe::AIDA_XMLStore::Histo2DTranslator::Histo2DTranslator(const AIDA::IHistogram2D * histo, const std::string & name, const std::string & path ) : 
00042   m_histo(histo), m_name(name), m_path(path)
00043 {
00044 }
00046 Anaphe::AIDA_XMLStore::Histo2DTranslator::Histo2DTranslator(const DataXML::DataObject * xmlObject) : 
00047   m_element(*xmlObject)
00048 {
00049 }
00050 
00051 
00052 Anaphe::AIDA_XMLStore::Histo2DTranslator::~Histo2DTranslator() 
00053 {  // no op
00054 }
00055 
00056 Anaphe::AIDA_XMLStore::Histo2DTranslator::Histo2DTranslator(const Anaphe::AIDA_XMLStore::Histo2DTranslator &)  
00057 {
00058 }
00059 
00060 Anaphe::AIDA_XMLStore::Histo2DTranslator & Anaphe::AIDA_XMLStore::Histo2DTranslator::operator = (const Anaphe::AIDA_XMLStore::Histo2DTranslator &rhs) 
00061 {
00062    if (this == &rhs) return *this;  // time saving self-test
00063 
00064    return *this;
00065 }
00066 
00067 bool Anaphe::AIDA_XMLStore::Histo2DTranslator::toXML() 
00068 { 
00069   // write name and title
00070   appendObjectHeader(m_element,"histogram2d",m_name,m_histo->title(),m_path); 
00071 
00072   appendAnnotation(m_element,m_histo->annotation()); 
00073   if (!setAxes() ) return false;
00074   if (!setStatistics() ) return false;
00075   if (!setData () ) return false;
00076   return true;
00077 
00078 }
00079 
00080 bool Anaphe::AIDA_XMLStore::Histo2DTranslator::setAxes()
00081 { 
00082   appendAxis (m_element, "x", m_histo->xAxis());
00083   appendAxis (m_element, "y", m_histo->yAxis());
00084   return true;
00085 }
00086 
00087 
00090 
00091 bool Anaphe::AIDA_XMLStore::Histo2DTranslator::setStatistics()
00092 { 
00093   DataXML::DataObject allStatisticsElement;
00094   allStatisticsElement.setName("statistics");
00095   allStatisticsElement.setAttribute("entries", toString(m_histo->entries()));
00096 
00097   // append the statistics info
00098   appendStatistics(allStatisticsElement, "x", m_histo->meanX(), m_histo->rmsX());
00099   appendStatistics(allStatisticsElement, "y", m_histo->meanY(), m_histo->rmsY());
00100   m_element.appendChild(allStatisticsElement);
00101   return true;
00102 }
00103 
00104 
00105 bool Anaphe::AIDA_XMLStore::Histo2DTranslator::setData()
00106 { 
00107   // write data element and append to the dataobject
00108 
00109   DataXML::DataObject dataElement;
00110   dataElement.setName("data2d");
00111   const int nbx = (m_histo->xAxis()).bins();
00112   const int nby = (m_histo->yAxis()).bins();
00113   for (int i = -2; i < nbx; ++i) {
00114     for (int j = -2; j < nby; ++j ) { 
00115       int    entries = m_histo->binEntries(i,j);
00116       double height  = m_histo->binHeight (i,j);
00117       double error   = m_histo->binError  (i,j);
00118       double binMeanX = m_histo->binMeanX   (i,j);
00119       double binMeanY = m_histo->binMeanY   (i,j);
00120       appendHisto2DBinData(dataElement, i, j, entries, height, error, binMeanX, binMeanY);
00121     }
00122   }
00123   m_element.appendChild(dataElement);
00124   return true;
00125 }
00126 
00128 
00129 AIDA::Dev::IDevHistogram2D * Anaphe::AIDA_XMLStore::Histo2DTranslator::createFromXML(AIDA::Dev::IDevHistogramFactory& factory)
00130 { 
00131   // read header 
00132   std::string title, options;
00133   getObjectHeader(m_element,m_name,title,m_path,options); 
00134 
00135   // read annotation
00136   AnnotationData annoData; 
00137   getAnnotation(m_element,annoData); 
00138 
00139   // read axis
00140   int numberOfBinsX = 0;
00141   double lowerEdgeX = 0;
00142   double upperEdgeX = 0;
00143   std::vector<double> edgesX; 
00144   getAxis(m_element,"x",numberOfBinsX,lowerEdgeX,upperEdgeX,edgesX); 
00145   int numberOfBinsY = 0;
00146   double lowerEdgeY = 0;
00147   double upperEdgeY = 0;
00148   std::vector<double> edgesY; 
00149   getAxis(m_element,"y",numberOfBinsY,lowerEdgeY,upperEdgeY,edgesY); 
00150 
00151 
00152   // Now I can create the histogram 
00153 
00154   AIDA::Dev::IDevHistogram2D * h = 0; 
00155   if (edgesX.empty() )  
00156     // equal bins histograms
00157     h = factory.createHistogram2D( title, numberOfBinsX, lowerEdgeX, upperEdgeX, numberOfBinsY, lowerEdgeY, upperEdgeY );
00158   else   
00159     // variable bins histograms 
00160     h = factory.createHistogram2D( title, edgesX, edgesY, emptyString );
00161 
00162   //read statistics (rms is needed since no binRMs is stored) 
00163   double rmsX,rmsY = 0; 
00164   const DataXML::DataObject * statElement = m_element.getChild("statistics");
00165   if (statElement) { 
00166     double mean,skew = 0; // not used 
00167     getStatistics(*statElement,"x",mean,rmsX,skew); 
00168     getStatistics(*statElement,"y",mean,rmsY,skew); 
00169   }
00170 
00171   // set annotation
00172   AIDA::IAnnotation & anno = h->annotation(); 
00173   setAnnotation(&anno,annoData); 
00174 
00175   // get the data 
00176  
00177   const DataXML::DataObject * dataElement = m_element.getChild("data2d");
00178   if (dataElement) { 
00179     for (std::vector<DataXML::DataObject>::const_iterator binElement = dataElement->children().begin(); binElement != dataElement->children().end(); ++binElement) {  
00180        if (binElement->name() == "bin2d") { 
00181          int xbin,ybin = -999;    // just a number which is not used 
00182          int entries = 0;
00183          // get info (not all is used..) 
00184          double height, error, wMeanX, wMeanY, wRmsX, wRmsY, error2, rms = 0; 
00185          getHisto2DBinData(*binElement,xbin,ybin,entries,height,error,wMeanX,wMeanY,wRmsX,wRmsY,error2, rms); 
00186          // fill the histogram with the bin contents 
00187          // (assume that binMean(centre) is defined) 
00188          h->setBinContents(xbin,ybin,entries, height, error, wMeanX, wMeanY); 
00189        }
00190     }
00191   }
00192   h->setRms(rmsX,rmsY);
00193 
00194   return h; 
00195 }

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