MapMatrixProjector.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 // Include max() and min() missing from MicroSoft Visual C++.
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "MapMatrixProjector.h"
18 
19 #include "axes/AxisModelBase.h"
20 
22 #include "datasrcs/RTuple.h"
23 
24 #include <algorithm>
25 #include <numeric>
26 
27 #include <cfloat>
28 #include <climits>
29 #include <cmath>
30 
31 
32 #include <cassert>
33 
34 using namespace hippodraw;
35 
36 #ifdef ITERATOR_MEMBER_DEFECT
37 using namespace std;
38 #else
39 using std::abs;
40 using std::accumulate;
41 using std::find;
42 using std::max;
43 using std::min;
44 using std::sqrt;
45 using std::string;
46 using std::vector;
47 #endif
48 
50  : NTupleProjector ( 2 ),
51  m_x_label ( "x" ),
52  m_y_label ( "y" ),
53  m_cols ( 0 ),
54  m_rows ( 0 ),
55  m_x_step ( 1.0 ),
56  m_y_step ( 1.0 ),
57  m_x_origin ( 0.0 ),
58  m_y_origin ( 0.0 ),
59  m_null_value ( 0.0 ),
60  m_transpose ( false )
61 {
62  m_binding_options.push_back ( "Z" );
63  m_min_bindings = 1;
64  addPointReps();
65 }
66 
73  : ProjectorBase ( projector ),
74  NTupleProjector( projector ),
75  m_x_label ( projector.m_x_label ),
76  m_y_label ( projector.m_y_label ),
77  m_cols ( projector.m_cols ),
78  m_rows ( projector.m_rows ),
79  m_x_step ( projector.m_x_step ),
80  m_y_step ( projector.m_y_step ),
81  m_x_origin ( projector.m_x_origin ),
82  m_y_origin ( projector.m_y_origin ),
83  m_null_value ( projector.m_null_value ),
84  m_transpose ( projector.m_transpose )
85 {
86  addPointReps();
87 }
88 
90 {
91  return new MapMatrixProjector( *this );
92 }
93 
94 void
96 setNumberOfBins ( hippodraw::Axes::Type axis, unsigned int number )
97 {
98  assert ( axis == Axes::X || axis == Axes::Y );
99 
100  if ( axis == Axes::X ) m_cols = number;
101  else m_rows = number;
102 }
103 
104 int
107 {
108  assert ( axis != Axes::Z );
109  int bins = axis == Axes::X ? m_cols : m_rows;
110 
111  return bins;
112 }
113 
114 const Range &
116 setBinWidth ( hippodraw::Axes::Type axis, double step )
117 {
118  if ( axis == Axes::X ) {
119  m_x_step = step;
120  }
121  else if ( axis == Axes::Y ) {
122  m_y_step = step;
123  }
124  else if ( axis == Axes::Z ) {
125  m_scale_factor = step;
126  }
127 
128  return getRange ( axis );
129 }
130 
131 double
134 {
135  if ( axis == Axes::X ) {
136  return m_x_step;
137  }
138  if ( axis == Axes::Y ) {
139  return m_y_step;
140  }
141  if ( axis == Axes::Z ) {
142  return m_scale_factor;
143  }
144  assert ( false );
145  return 0.0;
146 }
147 
148 void
150 setOffset ( hippodraw::Axes::Type axis, double origin )
151 {
152  if ( axis == Axes::X ) {
153  m_x_origin = origin;
154  return;
155  }
156  if ( axis == Axes::Y ) {
157  m_y_origin = origin;
158  return;
159  }
160  assert ( false );
161 }
162 
163 double
166 {
167  if ( axis == Axes::X ) {
168  return m_x_origin;
169  }
170  if ( axis == Axes::Y ) {
171  return m_y_origin;
172  }
173  assert ( false );
174  return 0.0;
175 }
176 
177 bool
179 inRange ( int row ) const
180 {
181  return inRangeWithZ ( row, true );
182 }
183 
184 bool
186 inRangeWithZ ( int row, bool use_z ) const
187 {
188  bool accept = true;
189 
190  std::size_t cindex = calcColumnIndex ( row );
191  double lvalue = m_x_origin + cindex * m_x_step;
192  const Range & x_range = m_x_axis -> getRange ( false );
193  bool in = x_range.includes ( lvalue ) ||
194  x_range.includes ( lvalue + m_x_step );
195  accept &= in;
196 
197  if ( accept ) {
198  std::size_t rindex = calcRowIndex ( row );
199  double bvalue = m_y_origin + rindex * m_y_step;
200  const Range & y_range = m_y_axis -> getRange ( false );
201  in = y_range.includes ( bvalue ) ||
202  y_range.includes ( bvalue + m_y_step );
203  accept &= in;
204  }
205 
206  if ( accept && use_z == true ) {
207  const Range & z_range = m_z_axis->getRange ( false );
208  double value = m_ntuple -> valueAt ( row, m_columns[0] );
209  accept &= z_range.includes ( value );
210  }
211 
212  return accept;
213 }
214 
216 {
217  unsigned int cols = m_ntuple->columns () - 1;
218  if ( m_columns[0] > cols ) m_columns[0] = cols;
219  if ( m_columns[1] > cols ) m_columns[1] = UINT_MAX;
220 }
221 
223 {
224  return dataRangeOn ( Axes::Z );
225 }
226 
227 namespace dp = hippodraw::DataPoint3DTuple;
228 
229 Range
232 {
233  MapMatrixProjector * mmp = const_cast < MapMatrixProjector *> ( this );
234  mmp -> prepareValues ();
235  if ( m_proj_values -> empty () ) {
236  return Range ( 0.0, 1.0, 0.5 );
237  }
238  const vector < double > & values = m_proj_values -> getColumn ( dp::Z );
239 
240  return Range ( values );
241 }
242 
243 Range
246 {
247  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
248 
249  Range range;
250 
251  if ( axis == Axes::X ) {
252  double len = m_x_origin + m_cols * m_x_step;
253  if ( m_x_step < 0. ) {
254  range.setRange ( len, m_x_origin, - m_x_step );
255  } else {
256  range.setRange ( m_x_origin, len, m_x_step );
257  }
258  }
259 
260  if ( axis == Axes::Y ) {
261  double len = m_y_origin + m_rows * m_y_step;
262  if ( m_y_step < 0. ) {
263  range.setRange ( len, m_y_origin, - m_y_step );
264  }
265  else {
266  range.setRange ( m_y_origin, len, m_y_step );
267  }
268  }
269 
270  if ( axis == Axes::Z ) {
271  range = dataRangeOnValue ();
272  }
273 
274  return range;
275 }
276 
277 /* @bug @@@@ This method can create low range > high range from
278  transform log log */
279 Range
282 {
283  Range range;
284  double low = DBL_MAX;
285  double pos = DBL_MAX;
286  double high = -DBL_MIN;
287 
288  if ( axis == Axes::Z ) {
289  std::size_t rows = m_ntuple -> rows ();
290  unsigned int used = 0;
291  for ( unsigned int row = 0; row < rows; row++ ) {
292  bool accept = inRangeWithZ ( row, false );
293  if ( accept ) {
294  double value = m_ntuple -> valueAt ( row, m_columns[0] );
295  low = std::min ( low, value );
296  if ( value > 0 ) {
297  pos = std::min ( pos, value );
298  }
299  high = std::max ( high, value );
300  used++;
301  }
302  }
303  range.setRange ( low, high, pos );
304  }
305  else {
306  range = ProjectorBase::preferredRange ( axis );
307  }
308 
309  return range;
310 }
311 double
314 {
315  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
316 
317  if ( axis == Axes::X ) {
318  return 0.5 * std::abs ( m_x_step );
319  }
320  if ( axis == Axes::Y ) {
321  return 0.5 * std::abs ( m_y_step );
322  }
323  if ( m_columns[1] == UINT_MAX ) {
324  return getPos ( m_columns[0] );
325  }
326  //It has to be Y with an error.
327  return getPosWithError ( m_columns[0], m_columns[1] );
328 }
329 
330 const string & MapMatrixProjector::getXLabel() const
331 {
332  return m_x_label;
333 }
334 
335 const string & MapMatrixProjector::getYLabel ( bool ) const
336 {
337  return m_y_label;
338 }
339 
340 const string &
342 getZLabel () const
343 {
344  return m_ntuple->getLabelAt( m_columns[0] );
345 }
346 
347 
352 double
355 {
356  MapMatrixProjector * p = const_cast < MapMatrixProjector * > ( this );
357  p -> prepareValues ();
358 
359  unsigned int col = 3; // bad value
360  switch ( axis ) {
361 
362  case Axes::X:
363  col = dp::X;
364  break;
365 
366  case Axes::Y:
367  col = dp::Y;
368  break;
369 
370  case Axes::Z:
371  col = dp::Z;
372  break;
373 
374  default:
375  break;
376  }
377  assert ( col < 3 );
378 
379  const DataSource * ntuple = getProjectedValues ();
380  const vector < double > & data = ntuple -> getColumn ( col );
381 
382  unsigned int size = ntuple -> rows ();
383 
384  double sum = 0.0;
385  sum = accumulate ( data.begin(), data.end(), sum );
386 
387  return sum / size;
388 }
389 
391 {
392  m_pointreps.push_back ( "ColorBox" );
393  m_pointreps.push_back ( "Contour" );
394 }
395 
396 void
398 setNTuple ( const DataSource * ntuple )
399 {
400  NTupleProjector::setNTuple ( ntuple );
401 
402  unsigned int size = ntuple->rows ();
403  double s = static_cast < double > ( size );
404  double side = sqrt ( s );
405 
406  m_rows = static_cast < unsigned int > ( side );
407  m_cols = static_cast < unsigned int > ( side );
408 
409  setDirty ();
410 }
411 
412 void
415 {
416  m_transpose = yes;
417 }
418 
423 double
425 getZValue ( double x, double y ) const
426 {
427  double xx = ( x - m_x_origin ) / m_x_step;
428  double yy = ( y - m_y_origin ) / m_y_step;
429 
430  unsigned int i_x = static_cast < unsigned int> ( xx );
431  unsigned int i_y = static_cast < unsigned int> ( yy );
432 
433  unsigned int row;
434  if ( m_transpose ) {
435  row = i_x + m_cols * i_y;
436  }
437  else {
438  row = i_x * m_rows + i_y;
439  }
440 
441  unsigned int size = m_ntuple -> rows ();
442  double value = 0.0;
443 
444  if ( row < size ) {
445  value = m_ntuple -> valueAt ( row, m_columns[0] );
446  }
447  return value;
448 }
449 
450 DataSource *
452 createNTuple () const
453 {
454  unsigned int z_err = m_columns[1];
455 
456  unsigned int columns = 6;
457  RTuple * ntuple = new RTuple ( columns );
458 
459  // using setLabelAt save 2KB compared to setLabels()
460  ntuple -> setLabelAt ( getXLabel (), 0 );
461  ntuple -> setLabelAt ( getYLabel ( false ), 1 );
462  ntuple -> setLabelAt ( getZLabel (), 2 );
463  ntuple -> setLabelAt ( "Width", 3 );
464  ntuple -> setLabelAt ( "Height", 4 );
465  if ( z_err < UINT_MAX ) {
466  ntuple -> setLabelAt ( m_ntuple -> getLabelAt ( z_err ), 5 );
467  }
468  else {
469  ntuple -> setLabelAt ( "Error", 5 );
470  }
471 
472  fillProjectedValues ( ntuple );
473 
474  return ntuple;
475 }
476 
480 void
482 fillProjectedValues ( DataSource * ntuple, bool ) const // in_range ) const
483 {
484  ntuple -> clear();
485 
486  double width_x = m_x_step;
487  double next_x = m_x_origin + 0.5 * width_x;
488 
489  vector < double > row ( dp::SIZE );
490  row[dp::XERR] = abs ( m_x_step );
491  row[dp::YERR] = abs ( m_y_step );
492  row[dp::ZERR] = 1.0;
493 
494  unsigned int l = 0;
495  for ( unsigned int i = 0; i < m_cols; i++ ) {
496 
497  double x = next_x;
498  next_x += width_x;
499 
500  double width_y = m_y_step;
501  double next_y = m_y_origin + 0.5 * width_y;
502 
503  for ( unsigned int j = 0; j < m_rows; j++ ) {
504  double y = next_y;
505  next_y += width_y;
506 
507  // Calculate our own inex to get value because calling
508  // getValueAt takes too long
509  int index;
510  if ( m_transpose ) {
511  index = i + m_cols * j;
512  }
513  else {
514  index = i * m_rows + j;
515  }
516  double value = m_ntuple -> valueAt ( index, m_columns [0] );
517 
518 // if ( acceptRow ( l ) == false ||
519 // ( in_range == true && inRange ( l ) == false ) ) {
520 // l++;o
521 // continue;
522 // }
523  if ( acceptRow ( l, m_cut_list ) == true ) {
524  row[dp::Z] = value;
525  }
526  else {
527  row[dp::Z] = m_null_value;
528  }
529  row[dp::X] = x;
530  row[dp::Y] = y;
531 
532  ntuple -> addRow ( row );
533  l++;
534  }
535  }
536 
537  vector < unsigned int > shape ( 3 );
538  shape[0] = m_cols;
539  shape[1] = m_rows;
540  shape[2] = dp::SIZE;
541 
542  ntuple -> setShape ( shape );
543 }
544 
545 void
548 {
549  if ( m_proj_values == 0 ) {
551  }
552  else {
553  if ( isDirty () ) {
555  }
556  }
557 
558  setDirty ( false );
559 }
560 
561 bool
564 {
565  return true;
566 }
double m_y_step
The step along the Y axis.
virtual void setNumberOfBins(hippodraw::Axes::Type axis, unsigned int number)
Sets the number of steps.
virtual double getAverage(hippodraw::Axes::Type axis) const
Get the Average of all projected values on the specified axis.
unsigned int i
const std::string & getYLabel(bool flag) const
Returns the label for the Y axis.
double getPos(int column) const
Returns the minimum positive value on the specified column.
virtual void setNTuple(const DataSource *ntuple)
Changes the DataSource used for the projections to source.
virtual void setNTuple(const DataSource *source)
Changes the DataSource used for the projections to source.
virtual void setOffset(hippodraw::Axes::Type axis, double origin)
Sets the bin offset.
AxisModelBase * m_x_axis
The AxisModel along the X axis.
Definition: ProjectorBase.h:88
CutList_t m_cut_list
A list of cuts that filter the projection.
virtual double getZValue(double x, double y) const
Returns the value at the coordinate.
virtual const Range & setBinWidth(hippodraw::Axes::Type axis, double step)
Sets the bin width.
bool acceptRow(unsigned int i, const CutList_t &cut_list) const
For row i of the column in the DataSource, returns true if all the cuts accept the row...
double m_x_origin
The origin along the X axis.
std::string m_y_label
The label for the generated y axis.
virtual const Range & getRange(Axes::Type) const
Returns the Range along the specified axis.
std::vector< unsigned int > m_columns
A vector containing indexes to the columns of the DataSource.
double m_null_value
The value of the null value.
double m_y_origin
The origin along the Y axis.
void * data(numeric::array arr)
Definition: num_util.cpp:389
virtual bool isImageConvertable() const
Returns true since the projected values can be converted to an image.
double getPosWithError(int data, int error) const
Returns the minimum positive values considering both data and error.
bool isDirty() const
Returns true if the projector has been marked dirty.
An NTupleProjector is a projector that projects data from an DataSource object.
std::vector< intptr_t > shape(numeric::array arr)
Definition: num_util.cpp:317
virtual double getPosOn(hippodraw::Axes::Type axis) const
Returns the minimum positive value of the data on a specified axis.
virtual void fillProjectedValues(DataSource *ntuple, bool in_range=false) const
bool includes(double value) const
Returns true if the argument value is inside the range.
Definition: Range.cxx:146
virtual void matrixTranspose(bool yes)
Transposes the X and Y axis of the contained matrix.
virtual Range dataRangeOnValue() const
unsigned int m_cols
The number of columns for each row.
hippodraw::MapMatrixProjector class interface
virtual void setDirty(bool value=true)
Sets the dirty flag to value.
return yes
Definition: CanvasView.cxx:883
const DataSource * getProjectedValues() const
Returns DataSource representation of projected values.
AxisModelBase * m_z_axis
The AxisModel along the Z axis.
Definition: ProjectorBase.h:96
virtual double getOffset(hippodraw::Axes::Type axis) const
Returns the origin along the axis axis.
RTuple class interface.
virtual DataSource * createNTuple() const
Creates an NTuple representation of the projected values.
virtual double getBinWidth(hippodraw::Axes::Type axis) const
Returns the step size along the axis axis.
virtual Range dataRangeOn(hippodraw::Axes::Type) const
Returns the range of the data on the specified axis.
error on X or half bin width
virtual Range preferredRange(Axes::Type) const
Returns the preferred Range.
double m_x_step
The step along the X axis.
ProjectorBase * clone()
The clone function returns an object of its own kind which is a copy of this object at this moment...
A DataSource class implemented with std::vector&lt;double&gt; to store the row data.
Definition: RTuple.h:30
intp size(numeric::array arr)
Definition: num_util.cpp:296
A derived class of NTupleProjector that maps 1 DataSource column to a Y axis of two dimensional proje...
const DataSource * m_ntuple
The pointer to the data source being projected.
std::vector< std::string > m_binding_options
The list of binding options for the Projector.
const std::string & getZLabel() const
Returns the label of the z axis.
bool inRangeWithZ(int row, bool flag) const
Returns true if value at row is within range.
virtual bool inRange(int row) const
Checks if the entries in a given row are within the range of the axis model.
bool m_transpose
A flag to indicate if the X-Y matrix should be transposed or not.
void setRange(double low, double high, double pos)
Changes the current Range.
Definition: Range.cxx:126
The base class for the Projector hierarchy.
Definition: ProjectorBase.h:56
virtual void addPointReps()
Function to add the acceptable point reps.
virtual void changedNTuple()
This function is called when the NTuple has been changed to a new one.
hippodraw::AxisModelBase class interface
std::string m_x_label
The label for the generated x axis.
virtual unsigned int rows() const =0
Returns the number of rows.
DataSource * m_proj_values
The NTuple representing the result of the projection.
Definition: ProjectorBase.h:80
virtual void prepareValues()
Informs the projector to prepare its projected values for plotting.
const Range & getRange(bool scaled) const
Returns the range represented by this AxisModel.
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
unsigned int m_rows
The number of rows for each column.
const std::string & getXLabel() const
Returns the label (title) of the x axis.
Expresses a range of values.
Definition: Range.h:33
return index
Definition: PickTable.cxx:182
std::vector< std::string > m_pointreps
Vector of acceptable PointReps.
hippodraw::DataPointTuple namespace interface
MapMatrixProjector()
This default constructor binds to the first two columns.
virtual Range valueRange() const
Finds the range of the projected values.
AxisModelBase * m_y_axis
The AxisModel along the Y axis.
Definition: ProjectorBase.h:92
unsigned int m_min_bindings
The minimum number of columns that must be bound.
unsigned int calcRowIndex(unsigned int row) const
Calculates the index of the row of the matrix, given the row of the NTuple.
virtual const std::string & getLabelAt(unsigned int index) const
Returns the label for the column at index index.
Definition: DataSource.cxx:179
double m_scale_factor
The scale factor.
unsigned int calcColumnIndex(unsigned int row) const
Calculates the index of the column of the matrix, given the row of the NTuple.
Type
Axes constants.
Definition: AxesType.h:31
virtual Range preferredRange(hippodraw::Axes::Type axis) const
Returns the preferred Range.
virtual int getNumberOfBins(hippodraw::Axes::Type axis) const
Returns the number of bins.
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen