Gaudi Framework, version v20r4

Generated: 8 Jan 2009

Generic2D.h

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

Generated at Thu Jan 8 17:44:23 2009 for Gaudi Framework, version v20r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004