| # Gaudi Framework, version v21r11 |

Home |  | Generated: 30 Sep 2010

# Generic3D.h

Go to the documentation of this file.

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