MeshProjector.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 "MeshProjector.h"
18 
19 #include "axes/AxisModelBase.h"
20 #include "axes/Range.h"
21 
23 #include "datasrcs/NTuple.h"
24 
25 #include <algorithm>
26 #include <cfloat>
27 #include <climits>
28 #include <cassert>
29 
30 using namespace hippodraw;
31 
32 #ifdef ITERATOR_MEMBER_DEFECT
33 using namespace std;
34 #else
35 using std::find;
36 using std::max;
37 using std::min;
38 using std::string;
39 using std::vector;
40 #endif
41 
43  : NTupleProjector ( 5 ),
44  m_x_option ( "X width (optional)" ),
45  m_y_option ( "Y width (optional)" )
46 {
47  m_binding_options.push_back ( "X" );
48  m_binding_options.push_back ( "Y" );
49  m_binding_options.push_back ( "Z" );
50  m_binding_options.push_back ( "X width" );
51  m_binding_options.push_back ( "Y width" );
52  m_min_bindings = 5;
53  addPointReps();
54 }
55 
61 MeshProjector ( const MeshProjector & projector )
62  : ProjectorBase ( projector ),
63  NTupleProjector ( projector )
64 {
65  addPointReps();
66 }
67 
68 // For some reason, implementing empty destructor decrease code size
69 // by 5 kbytes.
71 {
72 }
73 
75 {
76  return new MeshProjector( *this );
77 }
78 
79 void MeshProjector::setXErrorOption ( bool enable )
80 {
81  const string name ( m_x_option );
82  vector< string >:: iterator first
83  = find ( m_binding_options.begin (),
84  m_binding_options.end (),
85  name );
86 
87  if ( first != m_binding_options.end () && !enable ) {
88  m_binding_options.erase ( first );
89  m_columns[3] = UINT_MAX;
90  }
91  else if ( enable ) {
92  m_binding_options.push_back ( name );
93  }
94 }
95 
98 void MeshProjector::setYErrorOption ( bool enable )
99 {
100  const string name ( m_y_option );
101  vector< string >:: iterator first
102  = find ( m_binding_options.begin (),
103  m_binding_options.end (),
104  name );
105  if ( first != m_binding_options.end () && !enable ) {
106  m_binding_options.erase ( first );
107  m_columns[4] = UINT_MAX;
108  }
109  else if ( enable ) {
110  m_binding_options.push_back ( name );
111  }
112 }
113 
115 {
116  unsigned int cols = m_ntuple->columns () - 1;
117  if ( m_columns[0] > cols ) m_columns[0] = cols;
118  if ( m_columns[1] > cols ) m_columns[1] = cols;
119  if ( m_columns[2] > cols ) m_columns[2] = cols;
120  if ( m_columns[3] > cols ) m_columns[3] = cols;
121  if ( m_columns[4] > cols ) m_columns[4] = cols;
122 }
123 
125 {
126  return dataRangeOn ( Axes::Z );
127 }
128 
129 Range
132 {
133  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
134 
135  if ( axis == Axes::X ) {
136  if ( m_columns[3] == UINT_MAX ) {
137  return dataRange ( m_columns[0] );
138  } else {
139  return dataRangeWithError ( m_columns[0], m_columns[3] );
140  }
141  }
142  if ( axis == Axes::Y ) {
143  if ( m_columns[4] == UINT_MAX ) {
144  return dataRange ( m_columns[1] );
145  }
146  else {
147  return dataRangeWithError ( m_columns[1], m_columns[4] );
148  }
149  }
150  // has to be Z
151  return dataRangeOnValue ( );
152 }
153 
154 namespace dp = hippodraw::DataPoint3DTuple;
155 
156 Range
159 {
160  MeshProjector * mp = const_cast < MeshProjector * > ( this );
161  mp -> prepareValues ();
162  if ( m_proj_values -> empty () ) {
163  return Range ( 0.0, 1.0, 0.5 );
164  }
165  const vector < double > & values = m_proj_values -> getColumn ( dp::Z );
166 
167  return Range ( values );
168 }
169 
170 double
173 {
174  assert ( axis == Axes::X || axis == Axes::Y || axis == Axes::Z );
175 
176  if ( axis == Axes::X ) {
177  if ( m_columns[3] == UINT_MAX ) { // Was m_columns[2], should be a bug
178  return getPos ( m_columns[0] );
179  } else {
180  return getPosWithError ( m_columns[0], m_columns[3] );
181  }
182  }
183  if ( axis == Axes::Y ) {
184  if ( m_columns[4] == UINT_MAX ) { // Was m_coloumns[3], should be a bug
185  return getPos ( m_columns[1] );
186  }
187  else {
188  return getPosWithError ( m_columns[1], m_columns[4] );
189  }
190  }
191  // has to be Z
192  return getPos ( m_columns[2] );
193 }
194 
196 {
197  m_pointreps.push_back ( "ColorBox" );
198 }
199 
200 DataSource *
202 createNTuple () const
203 {
204 
205  unsigned int x_col = m_columns[0];
206  unsigned int y_col = m_columns[1];
207  unsigned int z_col = m_columns[2]; // Was 3, should be a bug
208 
209  unsigned int x_err = m_columns[3];
210  unsigned int y_err = m_columns[4];
211 
212  unsigned int columns = dp::SIZE;
213  NTuple * ntuple = new NTuple ( columns );
214 
215  vector < string > labels;
216  labels.push_back ( m_ntuple -> getLabelAt ( x_col ) );
217  labels.push_back ( m_ntuple -> getLabelAt ( y_col ) );
218  labels.push_back ( m_ntuple -> getLabelAt ( z_col ) );
219 
220  if ( x_err < UINT_MAX ) {
221  labels.push_back ( m_ntuple -> getLabelAt ( x_err ) );
222  } else {
223  labels.push_back ( dp::XWIDTH );
224  }
225 
226  if ( y_err < UINT_MAX ) {
227  labels.push_back ( m_ntuple -> getLabelAt ( y_err ) );
228  } else {
229  labels.push_back ( dp::YWIDTH );
230  }
231  labels.push_back ( " z error" ); // for z error
232 
233  ntuple->setLabels ( labels );
234 
235  unsigned int size = m_ntuple -> rows ();
236  ntuple -> reserve ( size );
237 
238  fillProjectedValues ( ntuple );
239 
240  return ntuple;
241 }
242 
250 void
252 fillProjectedValues ( DataSource * ntuple, bool in_range ) const
253 {
254  ntuple -> clear ();
255 
256  unsigned int x_col = m_columns[0];
257  unsigned int y_col = m_columns[1];
258  unsigned int z_col = m_columns[2];
259 
260  unsigned int x_err = m_columns[3];
261  unsigned int y_err = m_columns[4];
262 
263  const vector < string > & labels = m_ntuple -> getLabels ();
264  unsigned int size = labels.size();
265  if ( size > 3 ) {
266  if ( x_err == UINT_MAX &&
267  labels [ dp::XERR ] == dp::XWIDTH ) x_err = dp::XERR;
268  if ( size > 3 ) {
269  if ( y_err == UINT_MAX &&
270  labels [ dp::YERR ] == dp::YWIDTH ) y_err = dp::YERR;
271  }
272  }
273  size = m_ntuple -> rows ();
274  vector < double > row ( dp::SIZE );
275  for ( unsigned int i = 0; i < size; i++ ) {
276  if ( acceptRow ( i, m_cut_list ) == false ||
277  ( in_range == true && inRange ( i ) == false ) ) continue;
278 
279  row[dp::X] = m_ntuple -> valueAt ( i, x_col );
280  row[dp::Y] = m_ntuple -> valueAt ( i, y_col );
281  row[dp::Z] = m_ntuple -> valueAt ( i, z_col );
282 
283 
284  double xe
285  = x_err < UINT_MAX ? m_ntuple -> valueAt ( i, x_err ) : 0.0;
286  double ye
287  = y_err < UINT_MAX ? m_ntuple -> valueAt( i, y_err ) : 0.0;
288 
289  row[dp::XERR] = xe;
290  row[dp::YERR] = ye;
291 
292  ntuple -> addRow ( row );
293  }
294 }
295 
296 void
299 {
300  if ( m_proj_values == 0 ) {
302  }
303  else if ( isDirty () ) {
305  }
306 
307  setDirty ( false );
308 }
309 
310 bool
312 inRangeWithZ ( int row, bool with_z ) const
313 {
314  bool accept = true;
315 
316  for ( unsigned int i = 0; i < 2; i++ ) {
317  AxisModelBase * model = i == 0 ? m_x_axis : m_y_axis;
318  const Range & range = model -> getRange ( false );
319  unsigned int vcolumn = m_columns[i];
320  unsigned int wcolumn = m_columns[i+3];
321  double value = m_ntuple -> valueAt ( row, vcolumn );
322  double width = m_ntuple -> valueAt ( row, wcolumn );
323  bool in = range.includes ( value + width ) ||
324  range.includes ( value - width );
325  accept &= in;
326  }
327  if ( with_z ) {
328  const Range & range = m_z_axis -> getRange ( false );
329  double value = m_ntuple -> valueAt ( row, m_columns[2] );
330  bool in = range.includes ( value );
331  accept &= in;
332  }
333 
334  return accept;
335 }
336 
337 bool
339 inRange ( int row ) const
340 {
341  return inRangeWithZ ( row, true );
342 }
343 
344 Range
347 {
348  Range range;
349  double low = DBL_MAX;
350  double pos = DBL_MAX;
351  double high = -DBL_MIN;
352  if ( axis == Axes::Z ) {
353  std::size_t rows = m_ntuple -> rows ();
354  for ( unsigned int row = 0; row < rows; row++ ) {
355  bool accept = inRangeWithZ ( row, false );
356  if ( accept ) {
357  double value = m_ntuple -> valueAt ( row, m_columns[2] );
358  low = std::min ( low, value );
359  if ( value > 0 ) {
360  pos = std::min ( pos, value );
361  }
362  high = std::max ( high, value );
363  }
364  }
365  range.setRange ( low, high, pos );
366  }
367  else {
368  range = ProjectorBase::preferredRange ( axis );
369  }
370 
371  return range;
372 }
373 
374 
375 const string & MeshProjector::getZLabel () const
376 {
377  return m_proj_values->getLabelAt ( dp::Z );
378 }
379 
380 
385 double MeshProjector::getZValue ( double x, double y ) const
386 {
387 
388  double retval = 0;
389 
390  const vector < double > & xs = m_proj_values -> getColumn ( dp::X );
391  const vector < double > & ys = m_proj_values -> getColumn ( dp::Y );
392  const vector < double > & zs = m_proj_values -> getColumn ( dp::Z );
393  const vector < double > & xerr = m_proj_values -> getColumn ( dp::XERR );
394  const vector < double > & yerr = m_proj_values -> getColumn ( dp::YERR );
395 
396  unsigned int size = xs.size();
397  for ( unsigned int i = 0; i < size; i++ ) {
398  if ( x>xs[i]-xerr[i] && x<xs[i]+xerr[i] &&
399  y>ys[i]-yerr[i] && y<ys[i]+yerr[i] ) {
400  retval = zs[i];
401 
402  // Assume (x,y) can not be in another box
403  break;
404  }
405  }
406 
407  return retval;
408 
409 
410  /* TO REMOVE: Old algorithm, search for the nestest data point.
411 
412  const Range & xr = m_x_axis->getRange ( true );
413  const Range & yr = m_y_axis->getRange ( true );
414 
415  double xe = 0.1 * xr.length();
416  double ye = 0.1 * yr.length();
417  double distanceSquare = xe*xe+ye*ye;
418 
419  if ( (x-xs[i])*(x-xs[i])+(y-ys[i])*(y-ys[i]) < distanceSquare) {
420  // Update the nearest point info.
421  distanceSquare = (x-xs[i])*(x-xs[i])+(y-ys[i])*(y-ys[i]);
422  retval = zs[i];
423  }
424  */
425 }
Range dataRangeOnValue() const
Returns the Range of the projected values.
unsigned int i
double getPos(int column) const
Returns the minimum positive value on the specified column.
AxisModelBase * m_x_axis
The AxisModel along the X axis.
Definition: ProjectorBase.h:88
virtual void changedNTuple()
This function is called when the ntuple has been changed to a new one.
CutList_t m_cut_list
A list of cuts that filter the projection.
virtual void prepareValues()
Informs the projector to prepare its projected values for plotting.
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...
ProjectorBase * clone()
The clone function returns an object of its own kind which is a copy of this object at this moment...
virtual Range valueRange() const
Finds the range of the projected values.
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.
const std::string & getZLabel() const
Returns the label (title) of the z axis.
bool inRangeWithZ(int row, bool flag) const
Returns true if value at row is within range.
std::string XWIDTH
X width label.
double getPosWithError(int data, int error) const
Returns the minimum positive values considering both data and error.
Range dataRange(int column) const
Returns the range of data on the specified column.
virtual bool inRange(int row) const
Returns true if value at row is within range on all axis.
std::string m_y_option
The label for the Y width binding option.
Definition: MeshProjector.h:35
void setLabels(const std::vector< std::string > &v)
Assigns the label to each column from the vector of strings.
Definition: NTuple.cxx:471
bool isDirty() const
Returns true if the projector has been marked dirty.
An NTupleProjector is a projector that projects data from an DataSource object.
virtual void addPointReps()
Function to add the acceptable point reps.
bool includes(double value) const
Returns true if the argument value is inside the range.
Definition: Range.cxx:146
std::string YWIDTH
Y width label.
virtual void setDirty(bool value=true)
Sets the dirty flag to value.
virtual ~MeshProjector()
The destructor.
A derived class of NTupleProjector that maps 3 DataSource columns to a two dimensional mesh projectio...
Definition: MeshProjector.h:28
AxisModelBase * m_z_axis
The AxisModel along the Z axis.
Definition: ProjectorBase.h:96
hippodraw::NTuple class interface.
virtual void setXErrorOption(bool enable)
Sets whether the X width options are to be enabled or not.
virtual double getZValue(double x, double y) const
Get the z value at the specified point (x,y).
error on X or half bin width
virtual Range preferredRange(Axes::Type) const
Returns the preferred Range.
hippodraw::MeshProjector class interface
intp size(numeric::array arr)
Definition: num_util.cpp:296
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.
The AxisModelBase class maintains the Range and scaling of an axis.
Definition: AxisModelBase.h:33
MeshProjector()
This default constructor binds to the first two columns.
virtual Range preferredRange(hippodraw::Axes::Type axis) const
Returns the preferred Range on the given axis.
void setRange(double low, double high, double pos)
Changes the current Range.
Definition: Range.cxx:126
A DataSource class implemented with std::vector&lt;double&gt; to store the column data. ...
Definition: NTuple.h:33
The base class for the Projector hierarchy.
Definition: ProjectorBase.h:56
virtual double getPosOn(hippodraw::Axes::Type axis) const
Returns the minimum positive value of the data on a specified axis.
hippodraw::AxisModelBase class interface
DataSource * m_proj_values
The NTuple representing the result of the projection.
Definition: ProjectorBase.h:80
hippodraw::Range class interface
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
Range dataRangeWithError(int data, int error) const
Returns a range considering both data and error.
virtual DataSource * createNTuple() const
Creates an NTuple representation of the projected values.
Expresses a range of values.
Definition: Range.h:33
std::vector< std::string > m_pointreps
Vector of acceptable PointReps.
virtual Range dataRangeOn(hippodraw::Axes::Type) const
Returns the range of the data on the specified axis.
hippodraw::DataPointTuple namespace interface
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.
virtual const std::string & getLabelAt(unsigned int index) const
Returns the label for the column at index index.
Definition: DataSource.cxx:179
virtual void setYErrorOption(bool enable)
Sets whether the Y width options are to be enabled or not.
std::string m_x_option
The label for the X width binding option.
Definition: MeshProjector.h:32
Type
Axes constants.
Definition: AxesType.h:31
virtual void fillProjectedValues(DataSource *ntuple, bool in_range=false) const
Fills the DataSource the projected values.
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen