Bins2DBase.cxx
Go to the documentation of this file.
1 
13 #ifdef _MSC_VER
14 // Include max() and min() missing from MicroSoft Visual C++.
15 #include "msdevstudio/MSconfig.h"
16 #endif
17 
18 #include "Bins2DBase.h"
19 
20 #include "BinnerAxis.h"
21 
22 #include "datasrcs/NTuple.h"
23 
24 #include <numeric>
25 
26 #include <cassert>
27 
28 using std::string;
29 using std::vector;
30 
31 
32 using namespace hippodraw;
33 
34 Bins2DBase::Bins2DBase ( const char * name )
35  : BinsBase ( name ),
36  binner_axisX ( 0 ),
37  binner_axisY ( 0 )
38 {
39 }
40 
42  : BinsBase( binner ),
43  binner_axisX ( 0 ),
44  binner_axisY ( 0 ),
45  m_data ( binner.m_data )
46 {
47  if ( binner.binner_axisX != 0 ) {
48  binner_axisX = binner.binner_axisX->clone ();
49  }
50 
51  if ( binner.binner_axisY != 0 ) {
52  binner_axisY = binner.binner_axisY->clone ();
53  }
54 
55  m_values_dirty = true;
56 }
57 
59 {
60  if ( binner_axisX ) delete binner_axisX;
61  if ( binner_axisY ) delete binner_axisY;
62 }
63 
64 int
67 {
68  return 2;
69 }
70 
72 {
73  int nbx = numberOfBins ( Axes::X );
74  int nby = numberOfBins ( Axes::Y );
75 
76  if ( nbx == 0 ||
77  nby == 0 ) return;
78 
79  resize ( nbx, nby );
80 }
81 
82 const BinnerAxis *
85 {
86  assert ( axis == Axes::X ||
87  axis == Axes::Y );
88 
89  if ( axis == Axes::X ) return binner_axisX;
90  // else Y
91  return binner_axisY;
92 }
93 
94 
95 void
98 {
99  assert ( axis == Axes::X || axis == Axes::Y );
100 
101  if ( axis == Axes::X ) {
102  if ( binner_axisX ) delete binner_axisX;
103  binner_axisX = binner;
104  }
105  else { // else Y
106  if ( binner_axisY ) delete binner_axisY;
107  binner_axisY = binner;
108  }
109  resize ();
110 
111  m_values_dirty = true;
112 }
113 
114 double
117 {
118  if ( axis == Axes::X ) return binner_axisX->axisGetLow();
119  if ( axis == Axes::Y ) return binner_axisY->axisGetLow();
120 
121  assert ( false );
122  return 0.0;
123 }
124 
125 void
128 {
129  assert ( axis == Axes::X || axis == Axes::Y );
130 
131  if ( axis == Axes::X ) {
133 
134  if ( numberOfBins ( Axes::Y ) > 0 ) {
136  }
137  }
138  else { // Y
140  }
141 
143 }
144 
146 {
147  return m_num_bins;
148 }
149 
150 int
153 {
154  assert ( axis == Axes::X || axis == Axes::Y );
155 
156  if ( axis == Axes::X &&
157  binner_axisX != 0 ) return binner_axisX->axisNumberOfBins();
158  // else Y
159  if ( binner_axisY != 0 ) return binner_axisY->axisNumberOfBins();
160 
161  return 0;
162 }
163 
164 int Bins2DBase::binNumberX( double x ) const
165 {
166  return binner_axisX->axisBinNumber( x );
167 }
168 
169 int Bins2DBase::binNumberY( double y ) const
170 {
171  return binner_axisY->axisBinNumber( y );
172 }
173 
174 double
177 {
178  assert ( axis == Axes::X || axis == Axes::Y );
179 
180  if ( axis == Axes::X ) return binner_axisX->getConstWid();
181  // else Y
182  return binner_axisY->getConstWid ();
183 }
184 
185 double Bins2DBase::binWidthX ( int i ) const
186 {
187  return binner_axisX->axisBinWidth( i );
188 }
189 
190 double Bins2DBase::binWidthY ( int i ) const
191 {
192  return binner_axisY->axisBinWidth( i );
193 }
194 
195 bool
197 {
199 }
200 
201 double
203 {
204  if ( binner_axisX->hasEqualWidths () == false ||
205  binner_axisY->hasEqualWidths () == false ) {
206  return 1.0;
207  }
208 
209  double width_x = binner_axisX->axisBinWidth ( 1 );
210  double width_y = binner_axisY->axisBinWidth ( 1 );
211 
212  return width_x * width_y;
213 }
214 
215 const Range &
217 setBinWidth ( hippodraw::Axes::Type axis, double width )
218 {
219  assert ( axis == Axes::X || axis == Axes::Y );
220  assert ( width > 0.0 );
221 
222  BinnerAxis * binner = 0;
223  if ( axis == Axes::X ) binner = binner_axisX;
224  else binner = binner_axisY;
225 
226  const Range & range = binner->setBinWidth ( width );
227 
228  resize ();
229  m_values_dirty = true;
230 
231  return range;
232 }
233 
234 double
236 calcBinWidth ( Axes::Type axis, int parm, bool dragging ) const
237 {
238  double new_width = -1.0;
239 
240  if ( axis == Axes::X ) {
241  new_width = binner_axisX->calcBinWidth ( parm, dragging );
242  }
243  else { // "Y"
244  new_width = binner_axisY->calcBinWidth ( parm, dragging );
245  }
246 
247  return new_width;
248 }
249 
250 double
251 Bins2DBase::calcOffset ( const std::string & axis,
252  int parm,
253  bool dragging ) const
254 {
255  if ( axis == "X" ) {
256  return binner_axisX->calcOffset ( parm, dragging );
257  }
258  //else "Y"
259  return binner_axisY->calcOffset ( parm, dragging );
260 }
261 
262 double
265 {
266  assert ( axis == Axes::X || axis == Axes::Y );
267 
268  if( axis == Axes::X ) return binner_axisX->getOffset();
269  // else Y
270  return binner_axisY->getOffset();
271 }
272 
273 void
275 setOffset ( hippodraw::Axes::Type axis, double offset )
276 {
277  assert ( axis == Axes::X || axis == Axes::Y );
278 
279  BinnerAxis * binner = 0;
280  if ( axis == Axes::X ) binner = binner_axisX;
281  else binner = binner_axisY;
282 
283  binner->setOffset( offset );
284 
285  m_values_dirty = true;
286 }
287 
288 const Range &
290 setRange ( hippodraw::Axes::Type axis, const Range & range, bool hold_width )
291 {
292  assert ( axis == Axes::X || axis == Axes::Y );
293 
294  BinnerAxis * binner = 0;
295  if ( axis == Axes::X ) {
296  binner = binner_axisX;
297  } else {
298  binner = binner_axisY;
299  }
300  const Range & new_range = binner->setRange ( range, hold_width );
301  resize ();
302 
303  return new_range;
304 }
305 
306 /* virtual */
307 const Range &
310 {
311  assert ( axis == Axes::X || axis == Axes::Y );
312 
313  BinnerAxis * binner = 0;
314  if ( axis == Axes::X ) {
315  binner = binner_axisX;
316  } else {
317  binner = binner_axisY;
318  }
319 
320  return binner->getRange();
321 }
322 
323 
329 void
330 Bins2DBase::resize ( int nx, int ny )
331 {
332  // The following is necessary in order to prevent a memory leak
334 
335  setNumberOfBins ( Axes::X, nx );
336  setNumberOfBins ( Axes::Y, ny );
337 
338  m_values_dirty = true;
339 }
340 
342 {
343  double sum = 0.0;
344  for ( unsigned int i = 1; i < m_data.size () -1; i++ ) {
345  sum += std::accumulate ( m_data[i].begin()+1, m_data[i].end()-1, 0.0 );
346  }
347 
348  return static_cast < int > ( sum );
349 }
350 
352 {
353  return -1;
354 }
355 
357 {
358  return -1;
359 }
360 
361 
362 NTuple *
364 prepareNTuple ( unsigned int rows ) const
365 {
366  unsigned int columns = 6;
367  NTuple * ntuple = new NTuple ( columns );
368  ntuple -> reserve ( rows );
369 
370  // note: using setLabelAt instead of setLables(...) save 10KB of code
371  ntuple -> setLabelAt ( "X", 0 );
372  ntuple -> setLabelAt ( "Y", 0 );
373  ntuple -> setLabelAt ( "Value", 0 );
374  ntuple -> setLabelAt ( "Width", 0 );
375  ntuple -> setLabelAt ( "Height", 0 );
376  ntuple -> setLabelAt ( "Error", 0 );
377 
378  return ntuple;
379 }
unsigned int i
virtual bool hasEqualWidths() const
Returns true if all the bins have the same width.
Definition: BinnerAxis.cxx:59
virtual int getNumberOfAxes() const
Returns the number of axes handled by the BinsBase derived class.
Definition: Bins2DBase.cxx:66
virtual int getNumberOfEntries() const
Returns the true number of entries.
Definition: Bins2DBase.cxx:341
int m_num_bins
Total number of bins not including overflow and underflow.
Definition: Bins2DBase.h:57
const Range & getRange() const
Returns the range.
Definition: BinnerAxis.cxx:78
virtual const Range & setBinWidth(hippodraw::Axes::Type axis, double value)
Sets the bin width parameter on the specified axis.
Definition: Bins2DBase.cxx:217
virtual double scaleFactor() const
Returns the scale factor.
Definition: Bins2DBase.cxx:202
void resize()
Resizes the internal arrays.
Definition: Bins2DBase.cxx:71
void setBinnerOn(BinnerAxis *, hippodraw::Axes::Type axis)
Sets a new BinnerAxis on the axis axis for the binner to use.
Definition: Bins2DBase.cxx:97
virtual double getOffset() const =0
Returns the offset.
virtual double axisBinWidth(int i) const =0
Returns the width of each bin.
virtual void setNumberOfBins(hippodraw::Axes::Type axis, int number)
Sets the number of bins on the the specified axis.
Definition: Bins2DBase.cxx:127
virtual const Range & setRange(hippodraw::Axes::Type axis, const Range &, bool hold_width=true)
Sets the Range on the specified axis.
Definition: Bins2DBase.cxx:290
Bins2DBase class interface.
virtual const Range & setBinWidth(double width)=0
Sets the bin width and adjusts the range and number of bins accordingly.
virtual BinnerAxis * clone()=0
The clone function returns an object of its own kind which is a copy of this object at this moment...
int axisNumberOfBins() const
Returns the number of bins.
Definition: BinnerAxis.cxx:84
virtual const Range & getRange(hippodraw::Axes::Type axis)
Returns range from binner axis.
Definition: Bins2DBase.cxx:309
virtual int axisBinNumber(double x) const =0
Returns the number of the bin in which the x value is situated.
int binNumberX(double x) const
Returns the number of the bin in which the x value is situated.
Definition: Bins2DBase.cxx:164
virtual const Range & setRange(const Range &, bool hold_width=true)=0
Sets the range.
double axisGetLow() const
Returns the lower edge of the first bin.
Definition: BinnerAxis.cxx:65
hippodraw::NTuple class interface.
std::vector< std::vector< double > > m_data
The accumulated sum of weights.
Definition: Bins2DBase.h:48
int numberOfBins() const
Returns the number of bins.
Definition: Bins2DBase.cxx:145
bool m_values_dirty
A flag to indicate that the objects in m_values are not correct.
Definition: BinsBase.h:53
virtual double getOffset(hippodraw::Axes::Type axis) const
Returns the offset parameter on specific axis.
Definition: Bins2DBase.cxx:264
The base class for the BinnerAxis hierarchy.
Definition: BinnerAxis.h:35
Bins2DBase(const char *name)
A constructor that takes bins container name as argument.
Definition: Bins2DBase.cxx:34
virtual const BinnerAxis * getBinnerOn(hippodraw::Axes::Type axis) const
Returns the BinnerAxis object used by this object.
Definition: Bins2DBase.cxx:84
virtual double calcOffset(const std::string &, int parm, bool dragging) const
Calculates and returns a new range from dragging slider.
Definition: Bins2DBase.cxx:251
double calcBinWidth(hippodraw::Axes::Type axis, int parm, bool dragging) const
Calculates the bin width parameter from dragging slider.
Definition: Bins2DBase.cxx:236
virtual void axisSetNumberOfBins(int nb)=0
Sets m_num_bins.
A DataSource class implemented with std::vector&lt;double&gt; to store the column data. ...
Definition: NTuple.h:33
virtual bool hasEqualWidths() const
Returns true if all bins have the same width.
Definition: Bins2DBase.cxx:196
virtual double getLow(hippodraw::Axes::Type axis) const
Returns the low value of the bins on the specified axis.
Definition: Bins2DBase.cxx:116
The base class for the 2D binner hierarchy.
Definition: Bins2DBase.h:30
virtual NTuple * prepareNTuple(unsigned int rows) const
Prepares the NTuple.
Definition: Bins2DBase.cxx:364
virtual void setOffset(hippodraw::Axes::Type axis, double value)
Sets the offset parameter on the specified axis.
Definition: Bins2DBase.cxx:275
virtual int getOverflow() const
Returns the overflow.
Definition: Bins2DBase.cxx:356
BinnerAxis * binner_axisY
The binner axis object for the Y axis.
Definition: Bins2DBase.h:39
virtual const void setOffset(double offset)=0
Sets the offset and adjusts the range accordingly.
BinnerAxis * binner_axisX
The binner axis object for the X axis.
Definition: Bins2DBase.h:36
Expresses a range of values.
Definition: Range.h:33
int binNumberY(double y) const
Returns the number of the bin in which the y value is situated.
Definition: Bins2DBase.cxx:169
virtual double calcOffset(int parm, bool dragging) const =0
Calculates and returns a double corresponding to a bin offset when dragging a slider control...
virtual double getConstWid() const =0
Returns the constant width parameter.
virtual double binWidth(hippodraw::Axes::Type axis) const
Returns the bin width parameter on the specified axis.
Definition: Bins2DBase.cxx:176
~Bins2DBase()
The destructor.
Definition: Bins2DBase.cxx:58
virtual int getUnderflow() const
Returns the underflow.
Definition: Bins2DBase.cxx:351
hippodraw::BinnerAxis class interface
Type
Axes constants.
Definition: AxesType.h:31
The base class for the binner hierarchy.
Definition: BinsBase.h:33
double calcBinWidth(int parm, bool dragging) const
Calculates the bin width parameter when dragging a slider control.
Definition: BinnerAxis.cxx:130
double binWidthY(int i) const
Gets the bin width of the Y bin for the i bin.
Definition: Bins2DBase.cxx:190
double binWidthX(int i) const
Gets the bin width of the X bin for the i bin.
Definition: Bins2DBase.cxx:185

Generated for HippoDraw Class Library by doxygen