| # Gaudi Framework, version v21r10p1 |

Home |  | Generated: 29 Jul 2010

# Generic2D.h

Go to the documentation of this file.

    00001 #ifndef GAUDISVC_GENERIC2D_H
    00002 #define GAUDISVC_GENERIC2D_H 1
    00004 #include "AIDA_visibility_hack.h"
    00005
    00006 #include <stdexcept>
    00007 #include "AIDA/IProfile2D.h"
    00008 #include "GaudiKernel/HistogramBase.h"
    00009 #include "Annotation.h"
    00010 #include "Axis.h"
    00011 #include "TFile.h"
    00012
    00013
    00014 /*
    00015  *    Gaudi namespace
    00016  */
    00017 namespace Gaudi {
    00018
    00029   template <class INTERFACE, class IMPLEMENTATION>
    00030   class GAUDI_API Generic2D : virtual public INTERFACE, virtual public HistogramBase  {
    00031   public:
    00032     typedef Generic2D<INTERFACE,IMPLEMENTATION> Base;
    00033
    00034     Generic2D() : m_rep(0) {}
    00035
    00037     virtual ~Generic2D()   {  delete m_rep;    }
    00038
    00040     TObject* representation() const                      { return m_rep;                       }
    00042     virtual void adoptRepresentation(TObject* rep);
    00044     virtual std::string title() const                    {  return m_annotation.value( "Title" );    }
    00046     virtual bool setTitle(const std::string & title);
    00048     std::string name() const                             { return m_annotation.value("Name"); }
    00050     bool setName( const std::string& newName );
    00052     virtual AIDA::IAnnotation & annotation()             { return m_annotation;           }
    00054     virtual const AIDA::IAnnotation & annotation() const { return m_annotation;           }
    00055
    00057     virtual const AIDA::IAxis & xAxis() const            { return m_xAxis;                }
    00059     virtual const AIDA::IAxis & yAxis() const            { return m_yAxis;                }
    00061     virtual int rIndexX(int index) const                 { return m_xAxis.rIndex(index);  }
    00063     virtual int rIndexY(int index) const                 { return m_yAxis.rIndex(index);  }
    00064
    00066     virtual int entries() const;
    00068     virtual int allEntries() const;
    00070     virtual int extraEntries() const;
    00072     virtual double sumBinHeights() const;
    00074     virtual double sumAllBinHeights() const;
    00076     virtual double  sumExtraBinHeights (  ) const  { return  sumAllBinHeights()-sumBinHeights(); }
    00078     virtual double minBinHeight() const;
    00080     virtual double maxBinHeight() const;
    00081
    00083     virtual double binMeanX(int indexX,int indexY) const;
    00085     virtual double binMeanY(int indexX,int indexY) const;
    00087     virtual int  binEntries ( int indexX,int indexY ) const;
    00089     virtual int binEntriesX(int indexX) const;
    00091     virtual int binEntriesY(int indexY) const;
    00093     virtual double binHeight(int indexX,int indexY) const;
    00095     virtual double binHeightX(int indexX) const;
    00097     virtual double binHeightY(int indexY) const;
    00099     virtual double binError(int indexX,int indexY) const;
    00101     virtual double binRms(int indexX,int indexY) const;
    00103     virtual double meanX() const;
    00105     virtual double meanY() const;
    00107     virtual double rmsX() const;
    00109     virtual double rmsY() const;
    00111     virtual int coordToIndexX(double coordX) const;
    00113     virtual int coordToIndexY(double coordY) const;
    00115     virtual double equivalentBinEntries (  ) const;
    00117     virtual bool scale( double scaleFactor );
    00119     virtual bool add(const INTERFACE & h);
    00120     // overwrite reset
    00121     bool  reset (  );
    00123     void * cast(const std::string & className) const;
    00125     const std::string& userLevelClassType() const { return m_classType; }
    00127     virtual int  dimension() const  { return 2; }
    00129     virtual std::ostream& print( std::ostream& s ) const;
    00131     virtual std::ostream& write( std::ostream& s ) const;
    00133     virtual int write( const char* file_name ) const;
    00134
    00135   protected:
    00137     Axis                     m_xAxis;
    00139     Axis                     m_yAxis;
    00141     mutable AIDA::Annotation m_annotation;
    00143     IMPLEMENTATION*          m_rep;
    00145     std::string              m_classType;
    00147     int                      m_sumEntries;
    00148   };
    00149
    00150   template <class INTERFACE, class IMPLEMENTATION>
    00151   bool Generic2D<INTERFACE,IMPLEMENTATION>::setTitle(const std::string & title)  {
    00152     m_rep->SetTitle(title.c_str());
    00153     if ( !annotation().addItem( "Title", title ) )
    00154       m_annotation.setValue( "Title" , title );
    00155     if ( !annotation().addItem( "title", title ) )
    00156       annotation().setValue( "title", title );
    00157     return true;
    00158   }
    00159
    00160   template <class INTERFACE, class IMPLEMENTATION>
    00161   bool Generic2D<INTERFACE,IMPLEMENTATION>::setName( const std::string& newName ) {
    00162     m_rep->SetName(newName.c_str());
    00163     m_annotation.setValue( "Name", newName );
    00164     return true;
    00165   }
    00166
    00167   template <class INTERFACE, class IMPLEMENTATION>
    00168   int Generic2D<INTERFACE,IMPLEMENTATION>::entries() const                       {
    00169     return (int)m_rep->GetEntries();
    00170   }
    00171
    00172   template <class INTERFACE, class IMPLEMENTATION>
    00173   int Generic2D<INTERFACE,IMPLEMENTATION>::allEntries (  ) const  {
    00174     return int(m_rep->GetEntries());
    00175   }
    00176
    00177   template <class INTERFACE, class IMPLEMENTATION>
    00178   double  Generic2D<INTERFACE,IMPLEMENTATION>::minBinHeight() const  {
    00179     return m_rep->GetMinimum();
    00180   }
    00181
    00182   template <class INTERFACE, class IMPLEMENTATION>
    00183   double  Generic2D<INTERFACE,IMPLEMENTATION>::maxBinHeight() const  {
    00184     return m_rep->GetMaximum();
    00185   }
    00186
    00187   template <class INTERFACE, class IMPLEMENTATION>
    00188   double Generic2D<INTERFACE,IMPLEMENTATION>::sumBinHeights () const  {
    00189     return m_rep->GetSumOfWeights();
    00190   }
    00191
    00192   template <class INTERFACE, class IMPLEMENTATION>
    00193   double Generic2D<INTERFACE,IMPLEMENTATION>::sumAllBinHeights () const  {
    00194     return m_rep->GetSum();
    00195   }
    00196
    00197   template <class INTERFACE, class IMPLEMENTATION>
    00198   double Generic2D<INTERFACE,IMPLEMENTATION>::binRms(int indexX,int indexY) const {
    00199     return m_rep->GetBinError ( rIndexX(indexX), rIndexY(indexY) );
    00200   }
    00201
    00202   template <class INTERFACE, class IMPLEMENTATION>
    00203   double Generic2D<INTERFACE,IMPLEMENTATION>::binMeanX(int indexX,int ) const {
    00204     return (m_rep->GetXaxis())->GetBinCenter( rIndexX(indexX) );
    00205   }
    00206
    00207   template <class INTERFACE, class IMPLEMENTATION>
    00208   double Generic2D<INTERFACE,IMPLEMENTATION>::binMeanY(int,int indexY) const  {
    00209     return (m_rep->GetYaxis())->GetBinCenter( rIndexY(indexY) );
    00210   }
    00211
    00212   template <class INTERFACE, class IMPLEMENTATION>
    00213   int Generic2D<INTERFACE,IMPLEMENTATION>::binEntriesX(int index) const   {
    00214     int n = 0;
    00215     for (int iY = -2; iY < yAxis().bins(); ++iY)
    00216       n += binEntries(index,iY);
    00217     return n;
    00218   }
    00219
    00220   template <class INTERFACE, class IMPLEMENTATION>
    00221   int Generic2D<INTERFACE,IMPLEMENTATION>::binEntriesY(int index) const    {
    00222     int n = 0;
    00223     for (int iX = -2; iX < xAxis().bins(); ++iX)
    00224       n += binEntries(iX,index);
    00225     return n;
    00226   }
    00227
    00228   template <class INTERFACE, class IMPLEMENTATION>
    00229   double Generic2D<INTERFACE,IMPLEMENTATION>::binHeight ( int indexX,int indexY ) const  {
    00230     return m_rep->GetBinContent ( rIndexX(indexX), rIndexY(indexY) );
    00231   }
    00232
    00233   template <class INTERFACE, class IMPLEMENTATION>
    00234   double Generic2D<INTERFACE,IMPLEMENTATION>::binHeightX(int index) const  {
    00235     double s = 0;
    00236     for (int iY = -2; iY < yAxis().bins(); ++iY) {
    00237       s += binHeight(index,iY);
    00238     }
    00239     return s;
    00240   }
    00241
    00242   template <class INTERFACE, class IMPLEMENTATION>
    00243   double Generic2D<INTERFACE,IMPLEMENTATION>::binHeightY(int index) const  {
    00244     double s = 0;
    00245     for (int iX = -2; iX < xAxis().bins(); ++iX)
    00246       s += binHeight(iX,index);
    00247     return s;
    00248   }
    00249
    00250   template <class INTERFACE, class IMPLEMENTATION>
    00251   double Generic2D<INTERFACE,IMPLEMENTATION>::binError(int indexX,int indexY) const  {
    00252     return m_rep->GetBinError ( rIndexX(indexX), rIndexY(indexY ) );
    00253   }
    00254
    00255   template <class INTERFACE, class IMPLEMENTATION>
    00256   double Generic2D<INTERFACE,IMPLEMENTATION>::meanX() const  {
    00257     return m_rep->GetMean(1);
    00258   }
    00259
    00260   template <class INTERFACE, class IMPLEMENTATION>
    00261   double Generic2D<INTERFACE,IMPLEMENTATION>::meanY() const  {
    00262     return m_rep->GetMean(2);
    00263   }
    00264
    00265   template <class INTERFACE, class IMPLEMENTATION>
    00266   double Generic2D<INTERFACE,IMPLEMENTATION>::rmsX() const  {
    00267     return m_rep->GetRMS(1);
    00268   }
    00269
    00270   template <class INTERFACE, class IMPLEMENTATION>
    00271   double Generic2D<INTERFACE,IMPLEMENTATION>::rmsY() const  {
    00272     return m_rep->GetRMS(2);
    00273   }
    00274
    00275   template <class INTERFACE, class IMPLEMENTATION>
    00276   int Generic2D<INTERFACE,IMPLEMENTATION>::coordToIndexX ( double coord ) const {
    00277     return xAxis().coordToIndex(coord);
    00278   }
    00279
    00280   template <class INTERFACE, class IMPLEMENTATION>
    00281   int Generic2D<INTERFACE,IMPLEMENTATION>::coordToIndexY ( double coord ) const {
    00282     return yAxis().coordToIndex(coord);
    00283   }
    00284
    00285   template <class INTERFACE, class IMPLEMENTATION>
    00286   bool Generic2D<INTERFACE,IMPLEMENTATION>::add ( const INTERFACE & hist ) {
    00287     const Base* p = dynamic_cast<const Base*>(&hist);
    00288     if ( p )  {
    00289       m_rep->Add(p->m_rep);
    00290       return true;
    00291     }
    00292     throw std::runtime_error("Cannot add profile histograms of different implementations.");
    00293   }
    00294
    00295   template <class INTERFACE, class IMPLEMENTATION>
    00296   int Generic2D<INTERFACE,IMPLEMENTATION>::extraEntries() const {
    00297     return
    00298       binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
    00299       binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN)  +
    00300       binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN)  +
    00301       binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN);
    00302   }
    00303
    00304   template <class INTERFACE, class IMPLEMENTATION>
    00305   double Generic2D<INTERFACE,IMPLEMENTATION>::equivalentBinEntries() const  {
    00306     if (sumBinHeights() <= 0) return 0;
    00307     Stat_t stats[11];   // cover up to 3D...
    00308     m_rep->GetStats(stats);
    00309     return stats[0]*stats[0]/stats[1];
    00310   }
    00311
    00312   template <class INTERFACE, class IMPLEMENTATION>
    00313   bool Generic2D<INTERFACE,IMPLEMENTATION>::scale(double scaleFactor)   {
    00314     m_rep->Scale ( scaleFactor );
    00315     return true;
    00316   }
    00317
    00318   template <class INTERFACE, class IMPLEMENTATION>
    00319   bool Generic2D<INTERFACE,IMPLEMENTATION>::reset (  )   {
    00320     m_sumEntries = 0;
    00321     m_rep->Reset ( );
    00322     return true;
    00323   }
    00324
    00325   template <class INTERFACE, class IMPLEMENTATION>
    00326   std::ostream& Generic2D<INTERFACE,IMPLEMENTATION>::print( std::ostream& s ) const
    00327   {
    00329     m_rep->Print("all");
    00330     return s;
    00331   }
    00332
    00334   template <class INTERFACE, class IMPLEMENTATION>
    00335   std::ostream& Generic2D<INTERFACE,IMPLEMENTATION>::write( std::ostream& s ) const
    00336   {
    00337     s << std::endl << "2D Histogram Table: " << std::endl;
    00338     s << "BinX, BinY, Height, Error " << std::endl;
    00339     for ( int i = 0; i < xAxis().bins(); ++i )  {
    00340       for ( int j = 0; j < yAxis().bins(); ++j )  {
    00341         s << binMeanX( i, j )    << ", "
    00342                 << binMeanY( i, j )    << ", "
    00343                 << binHeight( i, j ) << ", "
    00344                 << binError ( i, j ) << std::endl;
    00345       }
    00346     }
    00347     s << std::endl;
    00348     return s;
    00349   }
    00350
    00352   template <class INTERFACE, class IMPLEMENTATION>
    00353   int Generic2D<INTERFACE,IMPLEMENTATION>::write( const char* file_name ) const
    00354   {
    00355     TFile *f = TFile::Open(file_name,"RECREATE");
    00356     Int_t nbytes = m_rep->Write();
    00357     f->Close();
    00358     return nbytes;
    00359   }
    00360 } // end namespace AIDA
    00361 #endif // GAUDIPI_GENERIC2D_H