Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/data_axis is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_data_axis
0005 #define tools_data_axis
0006 
0007 #include "mathf"
0008 
0009 namespace tools {
0010 
0011 class data_axis {
0012 public:
0013   data_axis():m_min_value(0),m_max_value(0),m_is_log(false){}
0014   virtual ~data_axis(){}
0015 public:
0016   data_axis(const data_axis& a_from)
0017   :m_min_value(a_from.m_min_value)
0018   ,m_max_value(a_from.m_max_value)
0019   ,m_is_log(a_from.m_is_log)
0020   {}
0021   data_axis& operator=(const data_axis& a_from){
0022     m_min_value = a_from.m_min_value;
0023     m_max_value = a_from.m_max_value;
0024     m_is_log = a_from.m_is_log;
0025     return *this;
0026   }
0027 public:
0028   bool set_is_log(bool a_v) {if(m_is_log==a_v) return false;m_is_log = a_v;return true;}
0029   bool set_min_value(float a_v) {if(m_min_value==a_v) return false;m_min_value = a_v;return true;}
0030   bool set_max_value(float a_v) {if(m_max_value==a_v) return false;m_max_value = a_v;return true;}
0031   float min_value() const {return m_min_value;}
0032   float max_value() const {return m_max_value;}
0033   bool is_log() const {return m_is_log;}
0034 
0035   void adjust() { //from hippodraw.
0036     int _axis = 0;
0037     float step;
0038     float mylow, myhigh;
0039     int N_NICE = 4;
0040     static const float nice[/*N_NICE*/4] = { 1.0,2.0,2.5,5.0 };
0041 
0042     if (m_min_value > m_max_value) {
0043       float low = m_min_value;
0044       m_min_value = m_max_value;
0045       m_max_value = low;
0046     } else if (m_min_value == m_max_value) {
0047       float value = m_min_value;
0048       m_min_value = value - 1;
0049       m_max_value = value + 1;
0050       return;
0051     }
0052 
0053     //if (m_steps <= 0) { //if letting the if and m_steps as a field, twice this function do not give the same result.
0054       _axis    = 1;
0055     unsigned int m_steps = 10;
0056     //}
0057 
0058     // Round the "bin width" to a nice number.
0059     // If this is being done for an axis (ie m_steps was 0 , then
0060     // we don't have to go > *m_max_value.
0061     //
0062     float w = (m_max_value - m_min_value)/((float)m_steps);
0063     float mag = ffloor(flog10(w));
0064     int i = 0;
0065     do {
0066       step   = nice[i] * fpow(10.0,mag);
0067       mylow  = ffloor(m_min_value/step) * step;
0068     //myhigh = _axis==1 ? fceil(m_max_value/step) * step : mylow + step * m_steps;
0069       myhigh = fceil(m_max_value/step) * step; //quiet Coverity.
0070       i++;
0071       if (i>=N_NICE) {i = 0;mag++;}
0072     }
0073     while ( ( (_axis==1) && (myhigh < m_max_value)) ||
0074             ( (_axis==0) && (myhigh <= m_max_value)) );
0075 
0076     float range = myhigh - mylow;
0077 
0078     // we now have decided on a range. Try to move
0079     // m_min_value/m_max_value a little
0080     // to end up on a nice number.
0081     //
0082     // first check if either end is near 0.0
0083     if ( !m_is_log && (m_min_value >= 0.0) &&
0084         (( (_axis==1) && (range>=m_max_value) ) ||
0085          ( (_axis==0) && (range>m_max_value) )) ) {
0086       m_min_value = 0.0;
0087       m_max_value = range;
0088       return;
0089     }
0090 
0091     if ( (( (_axis==1) && (m_max_value<=0.0) ) ||
0092           ( (_axis==0) && (m_max_value<0.0) ))
0093          && (-range<=m_min_value)) {
0094       m_max_value = 0.0;
0095       m_min_value = -range;
0096       return;
0097     }
0098 
0099     // try to round *m_min_value.
0100     // correction
0101     if( m_is_log && (m_min_value<=0.0)) m_min_value = 1.0;
0102 
0103     i   = N_NICE-1;
0104     mag = myhigh != 0.0 ? fceil(flog10(ffabs(myhigh))) : fceil(flog10(ffabs(mylow)));
0105 
0106     do {
0107       step   = nice[i] * fpow(10.0,mag);
0108       mylow  = ffloor(m_min_value/step) * step;
0109       myhigh = mylow + range;
0110       i--;
0111       if (i<0) {
0112         i = N_NICE-1;
0113         mag--;
0114       }
0115     }
0116     while (( m_is_log && (mylow  <= 0.0)     ) ||
0117            ( (_axis==1)  && (myhigh < m_max_value)  ) ||
0118            ( (_axis==0)  && (myhigh <= m_max_value) ) );
0119 
0120     m_min_value = mylow;
0121     m_max_value = myhigh;
0122   }
0123 protected:
0124   float m_min_value;
0125   float m_max_value;
0126   //int m_steps;
0127   bool m_is_log;
0128 };
0129 
0130 }
0131 
0132 #endif