00001
00002 #ifndef GAUDISVC_AXIS_H
00003 #define GAUDISVC_AXIS_H 1
00004
00005
00006
00007 #include "GaudiKernel/IHistogram.h"
00008 #include "GaudiKernel/IAxis.h"
00009
00010 #include "HTL/Histograms.h"
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class Axis : virtual public IAxis {
00030
00031 public:
00032
00034 Axis( I_Histo* h, int axisNumber, int nBins, double low, double high ) {
00035 m_histogram = h;
00036 m_axisNumber = axisNumber;
00037 setEdgesAndBins( nBins, low, high );
00038 }
00039
00040 Axis( I_Histo* h, int axisNumber, const std::vector<double>& edges ) {
00041 m_histogram = h;
00042 m_axisNumber = axisNumber;
00043 setEdgesAndBins( edges );
00044 }
00045
00046 virtual ~Axis() {}
00047
00051
00053 virtual const std::vector<double>& edges() const {
00054 return m_edges;
00055 }
00056
00058 virtual double lowerEdge() const {
00059 return m_edges[0];
00060 }
00062 virtual double upperEdge() const {
00063 return m_edges[m_bins];
00064 }
00065
00066
00070
00073
00075 virtual int bins() const {
00076 return m_bins;
00077 }
00078
00081
00083 virtual double binLowerEdge( int index ) const {
00084 int i = checkIndex( index );
00085 if( IHistogram::UNDERFLOW_BIN == i ) {
00086
00087 return -999999.999999;
00088 }
00089 else if ( IHistogram::OVERFLOW_BIN == i ) {
00090 return m_edges[m_bins];
00091 }
00092
00093 return m_edges[i];
00094 }
00095
00097 virtual double binUpperEdge( int index ) const {
00098 int i = checkIndex( index );
00099 if( IHistogram::UNDERFLOW_BIN == i ) {
00100 return m_edges[0];
00101 }
00102 else if ( IHistogram::OVERFLOW_BIN == i ) {
00103
00104 return +999999.999999;
00105 }
00106
00107 return m_edges[i+1];
00108 }
00109
00112
00114 virtual double binWidth( int index ) const {
00115 int i = checkIndex( index );
00116 return m_edges[i+1] - m_edges[i];
00117 }
00118
00121
00123 virtual double binCentre( int index ) const {
00124 int i = checkIndex( index );
00125
00126 if( 1 == m_histogram->dim() ) {
00127
00128 return HInfo::bin_center( *m_histogram, i );
00129 }
00130 else if( 2 == m_histogram->dim() ) {
00131
00132 if( 0 == m_axisNumber ) {
00133
00134 return HInfo::bin_center( *m_histogram, m_axisNumber, i, 0 );
00135 }
00136 else if( 1 == m_axisNumber ) {
00137
00138 return HInfo::bin_center( *m_histogram, m_axisNumber, 0, i );
00139 }
00140 std::cout << "Other axes than X or Y are not supported" << std::endl;
00141 }
00142 std::cout << "Other histograms than 1D or 2D are not supported" << std::endl;
00143 return 0.;
00144
00145
00146 }
00147
00148
00152
00155 virtual int coordToIndex( double coord ) const {
00156
00157
00158 if( lowerEdge() > coord ) {
00159 return IHistogram::UNDERFLOW_BIN;
00160 }
00161 if( upperEdge() <= coord ) {
00162 return IHistogram::OVERFLOW_BIN;
00163 }
00164
00165 int begin = 0;
00166 int end = bins() - 1;
00167 int centre = 0;
00168 while( begin != end ) {
00169 centre = ( begin + end ) / 2;
00170 if( binUpperEdge( centre ) <= coord ) {
00171 begin = centre + 1;
00172 }
00173 else {
00174 end = centre;
00175 }
00176 }
00177 return begin;
00178 }
00179
00180
00184
00187 virtual int checkIndex( int index ) const {
00188 if( (index >= 0) && (index < m_bins) ) {
00189 return index;
00190 }
00191 else if( (IHistogram::OVERFLOW_BIN != index) && ((index < 0) || (index >= m_bins)) ) {
00192 return IHistogram::UNDERFLOW_BIN;
00193 }
00194
00195 return IHistogram::OVERFLOW_BIN;
00196 }
00197
00198
00199 protected:
00200
00204
00206 void setEdgesAndBins( const std::vector<double>& value ) {
00207 m_edges = value;
00208 m_bins = m_edges.size() - 1;
00209 }
00211 void setEdgesAndBins( int nBins, double low, double high ) {
00212 double siz = ( high - low ) / nBins;
00213 double edge = low;
00214 for( int i=0; i<nBins; i++ ) {
00215 m_edges.push_back( edge );
00216 edge += siz;
00217 }
00218 m_edges.push_back( high );
00219 m_bins = nBins;
00220 }
00221
00222 protected:
00223
00225 int m_bins;
00226
00228 std::vector<double> m_edges;
00229
00231 I_Histo* m_histogram;
00232
00234 int m_axisNumber;
00235
00236 };
00237
00238
00239 #endif // GAUDISVC_AXIS_H
00240
00241
00242
00243
00244
00245