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

Histo1DTranslator.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 Histo1DTranslator
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 "Histo1DTranslator.h"
00031 #include "AIDA/IHistogram1D.h"
00032 #include "AIDA_Dev/IDevHistogram1D.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::Histo1DTranslator::Histo1DTranslator(const AIDA::IHistogram1D * 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::Histo1DTranslator::Histo1DTranslator(const DataXML::DataObject * xmlObject) : 
00047   m_element(*xmlObject)
00048 {
00049 }
00050 
00051 
00052 Anaphe::AIDA_XMLStore::Histo1DTranslator::~Histo1DTranslator() 
00053 {  // no op
00054 }
00055 
00056 Anaphe::AIDA_XMLStore::Histo1DTranslator::Histo1DTranslator(const Anaphe::AIDA_XMLStore::Histo1DTranslator &)  
00057 {
00058 }
00059 
00060 Anaphe::AIDA_XMLStore::Histo1DTranslator & Anaphe::AIDA_XMLStore::Histo1DTranslator::operator = (const Anaphe::AIDA_XMLStore::Histo1DTranslator &rhs) 
00061 {
00062    if (this == &rhs) return *this;  // time saving self-test
00063 
00064    return *this;
00065 }
00066 
00067 bool Anaphe::AIDA_XMLStore::Histo1DTranslator::toXML() 
00068 { 
00069   // write name and title
00070   appendObjectHeader(m_element,"histogram1d",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::Histo1DTranslator::setAxes()
00081 { 
00082   appendAxis (m_element, "x", m_histo->axis());
00083   return true;
00084 }
00085 
00086 
00089 
00090 bool Anaphe::AIDA_XMLStore::Histo1DTranslator::setStatistics()
00091 { 
00092   DataXML::DataObject allStatisticsElement;
00093   allStatisticsElement.setName("statistics");
00094   allStatisticsElement.setAttribute("entries", toString(m_histo->entries()));
00095 
00096   // append the statistics info
00097   appendStatistics(allStatisticsElement, "x", m_histo->mean(), m_histo->rms());
00098   m_element.appendChild(allStatisticsElement);
00099   return true;
00100 }
00101 
00102 
00103 bool Anaphe::AIDA_XMLStore::Histo1DTranslator::setData()
00104 { 
00105   // write data element and append to the dataobject
00106 
00107   DataXML::DataObject dataElement;
00108   dataElement.setName("data1d");
00109   const int nbx = (m_histo->axis()).bins();
00110   for (int i = -2; i < nbx; ++i) {
00111     int    entries = m_histo->binEntries(i);
00112     double height  = m_histo->binHeight (i);
00113     double error   = m_histo->binError  (i);
00114     double binMean = m_histo->binMean   (i);
00115     appendHisto1DBinData(dataElement, i, entries, height, error, binMean);
00116   }
00117   m_element.appendChild(dataElement);
00118   return true;
00119 }
00120 
00122 
00123 AIDA::Dev::IDevHistogram1D * Anaphe::AIDA_XMLStore::Histo1DTranslator::createFromXML(AIDA::Dev::IDevHistogramFactory& factory)
00124 { 
00125   // read header 
00126   std::string title, options;
00127   getObjectHeader(m_element,m_name,title,m_path,options); 
00128 
00129   // read annotation
00130   AnnotationData annoData; 
00131   getAnnotation(m_element,annoData); 
00132 
00133   // read axis
00134   int numberOfBins = 0;
00135   double lowerEdge = 0;
00136   double upperEdge = 0;
00137   std::vector<double> edges; 
00138   getAxis(m_element,"x",numberOfBins,lowerEdge,upperEdge,edges); 
00139 
00140 
00141   // Now I can create the histogram 
00142 
00143   AIDA::Dev::IDevHistogram1D * h = 0; 
00144   if (edges.empty())  
00145     // equal bins histograms
00146     h = factory.createHistogram1D( title, numberOfBins, lowerEdge, upperEdge );
00147   else   
00148     // variable bins histograms 
00149     h = factory.createHistogram1D( title, edges, emptyString );
00150 
00151   //read statistics (rms is needed since no binRMs is stored) 
00152   double mean,rms,skew = 0; 
00153   const DataXML::DataObject * statElement = m_element.getChild("statistics");
00154   if (statElement)
00155     getStatistics(*statElement,"x",mean,rms,skew); 
00156   
00157 
00158   // set annotation
00159   AIDA::IAnnotation & anno = h->annotation(); 
00160   setAnnotation(&anno,annoData); 
00161 
00162   // get the data 
00163  
00164   const DataXML::DataObject * dataElement = m_element.getChild("data1d");
00165   if (dataElement) { 
00166     for (std::vector<DataXML::DataObject>::const_iterator binElement = dataElement->children().begin(); binElement != dataElement->children().end(); ++binElement) {  
00167       if (binElement->name() == "bin1d") { 
00168         int xbin = -999;    // just a number which is not used 
00169         int entries = 0;
00170         // get info (not all is used..) 
00171         double height, error, wRms, wMean, error2, rms = 0; 
00172         getHisto1DBinData(*binElement,xbin,entries,height,error,wMean,wRms,error2, rms); 
00173         // fill the histogram with the bin contents 
00174         // (if binMean(centre) is not defined calculate from binWidth 
00175         if (wMean == 0 && xbin >=0 ) { 
00176           wMean = h->binMean(xbin);  // if histo is empty should return 1/2 value
00177         }
00178         h->setBinContents(xbin,entries, height, error, wMean); 
00179       }
00180     }
00181   }
00182 
00183   h->setRms(rms); 
00184 
00185   return h; 
00186 }

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