BinnerAxisLog.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "msdevstudio/MSconfig.h"
14 #endif
15 
16 #include "binners/BinnerAxisLog.h"
17 
18 #include <algorithm>
19 #include <stdexcept>
20 
21 #include <cmath>
22 #include <cassert>
23 
24 
25 #ifdef ITERATOR_MEMBER_DEFECT
26 using namespace std;
27 #else
28 using std::max;
29 using std::pow;
30 using std::upper_bound;
31 using std::vector;
32 #endif
33 
34 using namespace hippodraw;
35 
36 BinnerAxisLog::
37 BinnerAxisLog ()
38  : BinnerAxis ( "BinnerLog" )
39 {
40 }
41 
43  : BinnerAxis ( binner ),
44  bins ( binner.bins )
45 {
46 }
47 
49 {
50 }
51 
52 BinnerAxis *
54 {
55  return new BinnerAxisLog ( *this );
56 }
57 
58 void
60 {
61  m_num_bins = nb;
62 }
63 
64 int
65 BinnerAxisLog::axisBinNumber ( double x ) const
66 {
67  vector<double>::const_iterator it
68  = upper_bound ( bins.begin(), bins.end(), x );
69  int i = it - bins.begin();
70  if( i < 1 ) i = 0;
71  if ( i > m_num_bins ) i = m_num_bins + 1;
72 
73  return i;
74 }
75 
76 double
78 getCoordinate ( int i ) const
79 {
80  assert ( i < m_num_bins && !( i < 0 ) );
81 
82  double low = bins[i];
83  double high = bins[i+1];
84 
85  return sqrt(low*high);
86 }
87 
88 double
89 BinnerAxisLog::axisBinWidth ( int bins_number) const
90 {
91  bins_number = std::min ( bins_number, m_num_bins -1 );
92  bins_number = std::max ( bins_number, 0 );
93 
94  double width = bins[bins_number+1] - bins[bins_number];
95  assert ( width >= 0.0 );
96 
97  return width;
98 }
99 
100 /* virtual */
101 double
103 {
104  return m_width;
105 }
106 
107 double
109 getBinWidth ( ) const
110 {
111  return m_width;
112 }
113 
114 void
116 {
118 }
119 
120 double
121 BinnerAxisLog::calcWidthParm ( int num_bins ) const
122 {
123  double width_parm = 0;
124  double low = m_range.low();
125  double high = m_range.high();
126  width_parm = log10 ( high / low ) / num_bins;
127 
128  return width_parm;
129 }
130 
131 void
133 {
134  double low = m_range.low ();
135  assert ( low > 0.0 );
136 
137  vector<double>::iterator it = bins.begin();
138  *it++ = low;
139 
140  for ( int i = 1; it != bins.end(); ++it, i++ ) {
141  *it = low * pow ( 10.0, i * m_width );
142  }
143 }
144 
145 const Range &
147 setRange ( const Range & range, bool hold_width )
148 {
149  m_range = range;
150 
151  if ( hold_width ) {
152  m_num_bins = getNob ( range );
153  }
154  else {
156  }
157 
158  double low = m_range.low ();
159  double high = 1.0;
160  if (low <= 0) {
161  low = m_range.high()/pow(10., m_num_bins*m_width);
162  m_range.setLow(low);
163  } else {
164  high = low * pow ( 10.0, m_num_bins * m_width );
165  m_range.setHigh(high);
166  }
167 
168  if (high <= 0) { //sloppy, using high as a flag
169  throw std::runtime_error("BinnerAxisLog::Range: attempt to set range "
170  "to negative values.");
171  }
172 
173  bins.resize( m_num_bins + 1, 0.0 );
174  setBins();
175 
176  return m_range;
177 }
178 
179 const Range &
181 {
182  assert ( wid > 0.0 );
183 
184  m_width = wid;
185  m_num_bins = getNob ( m_width );
186 
187  double low = m_range.low ();
188  double high = low * pow ( 10.0, m_num_bins * wid );
189  m_range.setHigh ( high );
190  bins.resize( m_num_bins + 1, 0.0 );
191  setBins();
192 
193  return m_range;
194 }
195 
196 double
197 BinnerAxisLog::calcOffset ( int parm, bool dragging ) const
198 {
199  setStartRange ( dragging );
200 
201  return ( parm - 50 ) / 50.0;
202 }
203 
206 double
208 {
209  return m_offset;
210 }
211 
212 const void
213 BinnerAxisLog::setOffset ( double offset )
214 {
215  double oldoff = m_offset;
216  m_offset = offset;
217  double change = m_offset - oldoff;
218 
219  if( offset == 0.0 ) return; // resetting...
220  double low = m_range_start.low ();
221  double width = low * pow ( 10.0, m_width ); // first bin width
222  double woffset = width * change;
223  double new_low = low * pow ( 10.0, woffset * m_width );
224  double new_high = new_low * pow ( 10.0, ( bins.size() -1 ) * m_width );
225 
226  Range r( new_low, new_high );
227  setRange( r );
228 }
229 
230 double
232 {
233  return 1.0;
234 }
235 
236 int BinnerAxisLog::getNob ( const Range & range ) const
237 {
238  double low = range.low ();
239  double high = range.high ();
240  int tmp = static_cast< int > ( ceil ( log10 ( high/low ) /
241  m_width ) );
242 
243  return tmp ? tmp : 1;
244 }
245 
246 int BinnerAxisLog::getNob ( double wid ) const
247 {
248  double low = m_range.low ();
249  double high = m_range.high ();
250  int tmp = static_cast < int > ( ceil ( log10 ( high/low ) /
251  wid ) );
252  return tmp ? tmp : 1;
253 }
BinnerAxisLog()
The default constructor.
unsigned int i
void setBins()
Sets the bins structure by calculating the lower edge of each bin.
virtual double axisBinWidth(int bins_number) const
Returns the width of the bin designated by its place number in the bins structure.
double high() const
Returns the maximum of the range object.
Definition: Range.cxx:100
hippodraw::BinnerAxisLog class interface
virtual double getCoordinate(int i) const
Returns the coordinate.
std::vector< double > bins
Has the lower edge of each bin, starting with [0] = the first one.
Definition: BinnerAxisLog.h:41
Range m_range_start
The Range at the start of dragging.
Definition: BinnerAxis.h:83
virtual void setConstWid()
Sets the constant width parameter.
virtual const Range & setBinWidth(double width)
Sets the bin width and adjusts the range and number of bins accordingly.
virtual void axisSetNumberOfBins(int nb)
Sets m_num_bins.
virtual double calcOffset(int parm, bool dragging) const
Calculates and returns a double corresponding to a bin offset when dragging a slider control...
void setLow(double x)
Sets the minimum of the range object.
Definition: Range.cxx:93
void setStartRange(bool dragging) const
Sets the starting Range.
Definition: BinnerAxis.cxx:90
virtual int getNob(double wid) const
Returns the number of bins given the width parameter width.
virtual double getOffset() const
Returns the offset.
Range m_range
The range of the bins.
Definition: BinnerAxis.h:68
void setHigh(double x)
Sets the maximum of the range object.
Definition: Range.cxx:106
double m_width
The width parameter for the binning.
Definition: BinnerAxis.h:75
virtual int axisBinNumber(double x) const
Returns the number of the bin in which the x value and y value are situated.
The base class for the BinnerAxis hierarchy.
Definition: BinnerAxis.h:35
BinnerAxis * clone()
The clone function returns an object of its own kind which is a copy of this object at this moment...
The class for bins of logarithmically increasing widths.
Definition: BinnerAxisLog.h:32
virtual double getBinWidth() const
Returns the bin width parameter.
virtual const void setOffset(double offset)
Sets the offset and adjusts the range accordingly.
double calcWidthParm(int number) const
Calculates the bin width parameter given the number of bins.
double low() const
Returns the minimum of the range object.
Definition: Range.cxx:87
double m_offset
The offset.
Definition: BinnerAxis.h:71
Expresses a range of values.
Definition: Range.h:33
virtual ~BinnerAxisLog()
The destructor.
int m_num_bins
The number of bins.
Definition: BinnerAxis.h:64
virtual double scaleFactorWid()
Returns the bin width associated with the scale factor - in the case of log binning, this is always 1.0.
virtual const Range & setRange(const Range &, bool hold_width=true)
Sets the range.
list< QAction * >::iterator it
virtual double getConstWid() const
Returns the constant width parameter.

Generated for HippoDraw Class Library by doxygen