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 #ifdef tools_data_axis //g4tools backcomp :
0032 bool is_log(bool a_v) {if(m_is_log==a_v) return false;m_is_log = a_v;return true;}
0033 bool min_value(float a_v) {if(m_min_value==a_v) return false;m_min_value = a_v;return true;}
0034 bool max_value(float a_v) {if(m_max_value==a_v) return false;m_max_value = a_v;return true;}
0035 #endif
0036 float min_value() const {return m_min_value;}
0037 float max_value() const {return m_max_value;}
0038 bool is_log() const {return m_is_log;}
0039
0040 void adjust() { //from hippodraw.
0041 int _axis = 0;
0042 float step;
0043 float mylow, myhigh;
0044 int N_NICE = 4;
0045 static const float nice[/*N_NICE*/4] = { 1.0,2.0,2.5,5.0 };
0046
0047 if (m_min_value > m_max_value) {
0048 float low = m_min_value;
0049 m_min_value = m_max_value;
0050 m_max_value = low;
0051 } else if (m_min_value == m_max_value) {
0052 float value = m_min_value;
0053 m_min_value = value - 1;
0054 m_max_value = value + 1;
0055 return;
0056 }
0057
0058 //if (m_steps <= 0) { //if letting the if and m_steps as a field, twice this function do not give the same result.
0059 _axis = 1;
0060 unsigned int m_steps = 10;
0061 //}
0062
0063 // Round the "bin width" to a nice number.
0064 // If this is being done for an axis (ie m_steps was 0 , then
0065 // we don't have to go > *m_max_value.
0066 //
0067 float w = (m_max_value - m_min_value)/((float)m_steps);
0068 float mag = ffloor(flog10(w));
0069 int i = 0;
0070 do {
0071 step = nice[i] * fpow(10.0,mag);
0072 mylow = ffloor(m_min_value/step) * step;
0073 //myhigh = _axis==1 ? fceil(m_max_value/step) * step : mylow + step * m_steps;
0074 myhigh = fceil(m_max_value/step) * step; //quiet Coverity.
0075 i++;
0076 if (i>=N_NICE) {i = 0;mag++;}
0077 }
0078 while ( ( (_axis==1) && (myhigh < m_max_value)) ||
0079 ( (_axis==0) && (myhigh <= m_max_value)) );
0080
0081 float range = myhigh - mylow;
0082
0083 // we now have decided on a range. Try to move
0084 // m_min_value/m_max_value a little
0085 // to end up on a nice number.
0086 //
0087 // first check if either end is near 0.0
0088 if ( !m_is_log && (m_min_value >= 0.0) &&
0089 (( (_axis==1) && (range>=m_max_value) ) ||
0090 ( (_axis==0) && (range>m_max_value) )) ) {
0091 m_min_value = 0.0;
0092 m_max_value = range;
0093 return;
0094 }
0095
0096 if ( (( (_axis==1) && (m_max_value<=0.0) ) ||
0097 ( (_axis==0) && (m_max_value<0.0) ))
0098 && (-range<=m_min_value)) {
0099 m_max_value = 0.0;
0100 m_min_value = -range;
0101 return;
0102 }
0103
0104 // try to round *m_min_value.
0105 // correction
0106 if( m_is_log && (m_min_value<=0.0)) m_min_value = 1.0;
0107
0108 i = N_NICE-1;
0109 mag = myhigh != 0.0 ? fceil(flog10(ffabs(myhigh))) : fceil(flog10(ffabs(mylow)));
0110
0111 do {
0112 step = nice[i] * fpow(10.0,mag);
0113 mylow = ffloor(m_min_value/step) * step;
0114 myhigh = mylow + range;
0115 i--;
0116 if (i<0) {
0117 i = N_NICE-1;
0118 mag--;
0119 }
0120 }
0121 while (( m_is_log && (mylow <= 0.0) ) ||
0122 ( (_axis==1) && (myhigh < m_max_value) ) ||
0123 ( (_axis==0) && (myhigh <= m_max_value) ) );
0124
0125 m_min_value = mylow;
0126 m_max_value = myhigh;
0127 }
0128 protected:
0129 float m_min_value;
0130 float m_max_value;
0131 //int m_steps;
0132 bool m_is_log;
0133 };
0134
0135 }
0136
0137 #endif