| # Gaudi Framework, version v20r2
Generated: 18 Jul 2008

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- Search for |

- File List
- File Members

# Generic3D.h

Go to the documentation of this file.

    00001 #ifndef GAUDISVC_GENERIC3D_H
    00002 #define GAUDISVC_GENERIC3D_H 1
    00003
    00004 #include "Axis.h"
    00005 #include "TFile.h"
    00006 #include "Annotation.h"
    00007 #include "GaudiKernel/HistogramBase.h"
    00008 #include "AIDA/IHistogram3D.h"
    00009 #include <stdexcept>
    00010
    00011 /*
    00012  *    Gaudi namespace
    00013  */
    00014 namespace Gaudi {
    00015
    00026   template<typename INTERFACE, typename IMPLEMENTATION>
    00027   class Generic3D : virtual public INTERFACE, virtual public HistogramBase  {
    00028   public:
    00029     typedef Generic3D<INTERFACE,IMPLEMENTATION> Base;
    00031     Generic3D() : m_rep(0)  {}
    00033     virtual ~Generic3D()    { delete m_rep; }
    00035     TObject* representation() const                      { return m_rep;                       }
    00037     virtual void adoptRepresentation(TObject* rep);
    00038
    00040     virtual int  dimension() const  { return 3; }
    00042     virtual std::string title() const                    {  return m_annotation.value("Title");}
    00044     virtual bool setTitle(const std::string & title);
    00046     virtual 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 int entries() const;
    00057     virtual int allEntries() const;
    00059     virtual double sumBinHeights() const;
    00061     virtual double sumAllBinHeights() const;
    00063     virtual double  sumExtraBinHeights (  ) const  { return  sumAllBinHeights()-sumBinHeights(); }
    00065     virtual double minBinHeight() const;
    00067     virtual double maxBinHeight() const;
    00068
    00069     int rIndexX(int index) const { return m_xAxis.rIndex(index);}
    00070     int rIndexY(int index) const { return m_yAxis.rIndex(index);}
    00071     int rIndexZ(int index) const { return m_zAxis.rIndex(index);}
    00072
    00074     double binMeanX(int indexX,int ,int ) const
    00075     { return (m_rep->GetXaxis())->GetBinCenter( rIndexX(indexX) ); }
    00077     double binMeanY(int,int indexY,int  ) const
    00078     { return (m_rep->GetYaxis())->GetBinCenter( rIndexY(indexY) ); }
    00080     double binMeanZ(int ,int ,int indexZ) const
    00081     { return (m_rep->GetYaxis())->GetBinCenter( rIndexY(indexZ) ); }
    00083     int binEntries(int indexX,int indexY,int indexZ) const    {
    00084       if (binHeight(indexX, indexY, indexZ)<=0) return 0;
    00085       double xx =  binHeight(indexX, indexY, indexZ)/binError(indexX, indexY, indexZ);
    00086       return int(xx*xx+0.5);
    00087     }
    00089     virtual int binEntriesX(int index) const    {
    00090       int n = 0;
    00091       for (int i = -2; i < yAxis().bins(); ++i)
    00092         for (int j = -2; j < zAxis().bins(); ++j)
    00093           n += binEntries(index,i,j);
    00094       return n;
    00095
    00096     }
    00098     virtual int binEntriesY(int index) const    {
    00099       int n = 0;
    00100       for (int i = -2; i < xAxis().bins(); ++i)
    00101         for (int j = -2; j < zAxis().bins(); ++j)
    00102           n += binEntries(i,index,j);
    00103       return n;
    00104     }
    00105
    00107     virtual int binEntriesZ(int index) const    {
    00108       int n = 0;
    00109       for (int i = -2; i < xAxis().bins(); ++i)
    00110         for (int j = -2; j < yAxis().bins(); ++j)
    00111           n += binEntries(i,j,index);
    00112       return n;
    00113     }
    00114
    00116     double  binHeight ( int indexX,int indexY,int indexZ ) const
    00117     { return m_rep->GetBinContent ( rIndexX(indexX), rIndexY(indexY), rIndexZ(indexZ) ); }
    00118
    00120     virtual double binHeightX(int index) const    {
    00121       double s = 0;
    00122       for (int i = -2; i < yAxis().bins(); ++i)
    00123         for (int j = -2; j < zAxis().bins(); ++j)
    00124           s += binHeight(index,i,j);
    00125       return s;
    00126     }
    00128     virtual double binHeightY(int index) const    {
    00129       double s = 0;
    00130       for (int i = -2; i < xAxis().bins(); ++i)
    00131         for (int j = -2; j < zAxis().bins(); ++j)
    00132           s += binHeight(i,index,j);
    00133       return s;
    00134     }
    00136     virtual double binHeightZ(int index) const    {
    00137       double s = 0;
    00138       for (int i = -2; i < xAxis().bins(); ++i)
    00139         for (int j = -2; j < yAxis().bins(); ++j)
    00140           s += binHeight(i,j,index);
    00141       return s;
    00142     }
    00144     virtual double  binError ( int indexX,int indexY,int indexZ ) const
    00145     { return m_rep->GetBinError ( rIndexX(indexX), rIndexY(indexY ), rIndexZ(indexZ ) ); }
    00147     virtual double  meanX (  ) const  { return m_rep->GetMean ( 1); }
    00148
    00150     virtual double  meanY (  ) const  { return m_rep->GetMean ( 2 ); }
    00152     virtual double  meanZ (  ) const  { return m_rep->GetMean ( 3 ); }
    00154     virtual double  rmsX (  ) const  { return m_rep->GetRMS( 1 ); }
    00156     virtual double  rmsY (  ) const  { return m_rep->GetRMS( 2 ); }
    00158     virtual double  rmsZ (  ) const  { return m_rep->GetRMS( 3 ); }
    00160     virtual const AIDA::IAxis & xAxis (  ) const  { return m_xAxis; }
    00162     virtual const AIDA::IAxis & yAxis (  ) const  { return m_yAxis; }
    00164     virtual const AIDA::IAxis & zAxis (  ) const  { return m_zAxis; }
    00166     virtual int  coordToIndexX ( double coord ) const { return xAxis().coordToIndex(coord);}
    00168     virtual int  coordToIndexY ( double coord ) const { return yAxis().coordToIndex(coord);}
    00170     virtual int  coordToIndexZ ( double coord ) const { return zAxis().coordToIndex(coord);}
    00171
    00173     virtual double equivalentBinEntries (  ) const;
    00175     virtual bool scale( double scaleFactor );
    00177     virtual bool  add ( const INTERFACE & hist ) {
    00178       const Base* p = dynamic_cast<const Base*>(&hist);
    00179       if ( p )  {
    00180         m_rep->Add(p->m_rep);
    00181         return true;
    00182       }
    00183       throw std::runtime_error("Cannot add profile histograms of different implementations.");
    00184     }
    00185
    00186     // overwrite extraentries
    00187     int extraEntries() const {
    00188       return
    00189         binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
    00190         binEntries(AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN)  +
    00191         binEntries(AIDA::IAxis::UNDERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN)  +
    00192         binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN) +
    00193         binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN) +
    00194         binEntries(AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::UNDERFLOW_BIN)  +
    00195         binEntries(AIDA::IAxis::OVERFLOW_BIN, AIDA::IAxis::OVERFLOW_BIN,AIDA::IAxis::OVERFLOW_BIN);
    00196     }
    00198     virtual std::ostream& print( std::ostream& s ) const;
    00200     virtual std::ostream& write( std::ostream& s ) const;
    00202     virtual int write( const char* file_name ) const;
    00203
    00204   protected:
    00205     Gaudi::Axis              m_xAxis;
    00206     Gaudi::Axis              m_yAxis;
    00207     Gaudi::Axis              m_zAxis;
    00209     mutable AIDA::Annotation m_annotation;
    00211     IMPLEMENTATION*          m_rep;
    00212     // class type
    00213     std::string              m_classType;
    00214     // cache sumEntries (allEntries)   when setting contents since Root can't compute by himself
    00215     int                      m_sumEntries;
    00216   }; // end class IHistogram3D
    00217
    00218   template <class INTERFACE, class IMPLEMENTATION>
    00219   bool Generic3D<INTERFACE,IMPLEMENTATION>::setTitle(const std::string & title)  {
    00220     m_rep->SetTitle(title.c_str());
    00221     if ( !annotation().addItem( "Title", title ) )
    00222       m_annotation.setValue( "Title" , title );
    00223     if ( !annotation().addItem( "title", title ) )
    00224       annotation().setValue( "title", title );
    00225     return true;
    00226   }
    00227
    00228   template <class INTERFACE, class IMPLEMENTATION>
    00229   bool Generic3D<INTERFACE,IMPLEMENTATION>::setName( const std::string& newName ) {
    00230     m_rep->SetName(newName.c_str());
    00231     m_annotation.setValue( "Name", newName );
    00232     return true;
    00233   }
    00234   template <class INTERFACE, class IMPLEMENTATION>
    00235   int Generic3D<INTERFACE,IMPLEMENTATION>::entries() const                       {
    00236     return (int)m_rep->GetEntries();
    00237   }
    00238
    00239   template <class INTERFACE, class IMPLEMENTATION>
    00240   int Generic3D<INTERFACE,IMPLEMENTATION>::allEntries (  ) const  {
    00241     return int(m_rep->GetEntries());
    00242   }
    00243
    00244   template <class INTERFACE, class IMPLEMENTATION>
    00245   double  Generic3D<INTERFACE,IMPLEMENTATION>::minBinHeight() const  {
    00246     return m_rep->GetMinimum();
    00247   }
    00248
    00249   template <class INTERFACE, class IMPLEMENTATION>
    00250   double  Generic3D<INTERFACE,IMPLEMENTATION>::maxBinHeight() const  {
    00251     return m_rep->GetMaximum();
    00252   }
    00253
    00254   template <class INTERFACE, class IMPLEMENTATION>
    00255   double Generic3D<INTERFACE,IMPLEMENTATION>::sumBinHeights () const  {
    00256     return m_rep->GetSumOfWeights();
    00257   }
    00258
    00259   template <class INTERFACE, class IMPLEMENTATION>
    00260   double Generic3D<INTERFACE,IMPLEMENTATION>::sumAllBinHeights () const  {
    00261     return m_rep->GetSum();
    00262   }
    00263
    00264   template <class INTERFACE, class IMPLEMENTATION>
    00265   double Generic3D<INTERFACE,IMPLEMENTATION>::equivalentBinEntries() const  {
    00266     if (sumBinHeights() <= 0) return 0;
    00267     Stat_t stats[11];   // cover up to 3D...
    00268     m_rep->GetStats(stats);
    00269     return stats[0]*stats[0]/stats[1];
    00270   }
    00271
    00272   template <class INTERFACE, class IMPLEMENTATION>
    00273   bool Generic3D<INTERFACE,IMPLEMENTATION>::scale(double scaleFactor)   {
    00274     m_rep->Scale ( scaleFactor );
    00275     return true;
    00276   }
    00277
    00278   template <class INTERFACE, class IMPLEMENTATION>
    00279   std::ostream& Generic3D<INTERFACE,IMPLEMENTATION>::print( std::ostream& s ) const
    00280   {
    00282     m_rep->Print("all");
    00283     return s;
    00284   }
    00285
    00286
    00288   template <class INTERFACE, class IMPLEMENTATION>
    00289   std::ostream& Generic3D<INTERFACE,IMPLEMENTATION>::write( std::ostream& s ) const
    00290   {
    00291     s << "\n3D Histogram Table: " << std::endl;
    00292     s << "BinX, BinY, BinZ, Height, Error " << std::endl;
    00293     for ( int i = 0; i < xAxis().bins(); ++i )
    00294       for ( int j = 0; j < yAxis().bins(); ++j )
    00295         for ( int k = 0; k < zAxis().bins(); ++k )
    00296         s << binMeanX( i, j, k )    << ", "
    00297           << binMeanY( i, j, k )    << ", "
    00298           << binMeanZ( i, j, k )    << ", "
    00299           << binHeight( i, j, k ) << ", "
    00300           << binError ( i, j, k ) << std::endl;
    00301     s << std::endl;
    00302     return s;
    00303   }
    00304
    00306   template <class INTERFACE, class IMPLEMENTATION>
    00307   int Generic3D<INTERFACE,IMPLEMENTATION>::write( const char* file_name ) const
    00308   {
    00309     TFile *f = TFile::Open(file_name,"RECREATE");
    00310     Int_t nbytes = m_rep->Write();
    00311     f->Close();
    00312     return nbytes;
    00313   }
    00314 } // end namespace AIDA
    00315 #endif // GAUDIPI_GENERIC3D_H