Map2Projector.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 "Map2Projector.h"
18 
19 #include "axes/Range.h"
20 
22 #include "datasrcs/NTuple.h"
23 
24 #include <algorithm>
25 
26 #include <cfloat>
27 #include <climits>
28 
29 #include <cassert>
30 
31 using namespace hippodraw;
32 
33 #ifdef ITERATOR_MEMBER_DEFECT
34 using namespace std;
35 #else
36 using std::find;
37 using std::max;
38 using std::min;
39 using std::string;
40 using std::vector;
41 #endif
42 
44  : NTupleProjector ( 4 ),
45  m_x_option ( "X error (optional)" ),
46  m_y_option ( "Y error (optional)" )
47 {
48  m_binding_options.push_back ( "X" );
49  m_binding_options.push_back ( "Y" );
50  m_min_bindings = 2;
51  addPointReps();
52 }
53 
59 Map2Projector ( const Map2Projector & projector )
60  : ProjectorBase ( projector ),
61  NTupleProjector ( projector )
62 {
63  addPointReps();
64 }
65 
66 // For some reason, implementing empty destructor decrease code size
67 // by 5 kbytes.
69 {
70 }
71 
73 {
74  return new Map2Projector( *this );
75 }
76 
77 void Map2Projector::setXErrorOption ( bool enable )
78 {
79  const string name ( m_x_option );
80  vector< string >:: iterator first
81  = find ( m_binding_options.begin (),
82  m_binding_options.end (),
83  name );
84 
85  if ( first != m_binding_options.end () && !enable ) {
86  m_binding_options.erase ( first );
87  m_columns[2] = UINT_MAX;
88  }
89  else if ( enable ) {
90  m_binding_options.push_back ( name );
91  }
92 }
93 
96 void Map2Projector::setYErrorOption ( bool enable )
97 {
98  const string name ( m_y_option );
99  vector< string >:: iterator first
100  = find ( m_binding_options.begin (),
101  m_binding_options.end (),
102  name );
103  if ( first != m_binding_options.end () && !enable ) {
104  m_binding_options.erase ( first );
105  m_columns[3] = UINT_MAX;
106  }
107  else if ( enable ) {
108  m_binding_options.push_back ( name );
109  }
110 }
111 
113 {
114  unsigned int cols = m_ntuple->columns () - 1;
115  if ( m_columns[0] > cols ) m_columns[0] = cols;
116  if ( m_columns[1] > cols ) m_columns[1] = cols;
117  if ( m_columns[2] > cols ) m_columns[2] = UINT_MAX;
118  if ( m_columns[3] > cols ) m_columns[3] = UINT_MAX;
119 }
120 
122 {
123  return dataRangeOn ( Axes::Y );
124 }
125 
126 Range
129 {
130  assert ( axis == Axes::X || axis == Axes::Y );
131 
132  if ( axis == Axes::X ) {
133  if ( m_columns[2] == UINT_MAX ) {
134  return dataRange ( m_columns[0] );
135  } else {
136  return dataRangeWithError ( m_columns[0], m_columns[2] );
137  }
138  }
139  // It has to be Y.
140  if ( m_columns[3] == UINT_MAX ) {
141  return dataRange ( m_columns[1] );
142  }
143  // It has to be Y with an error.
144  return dataRangeWithError ( m_columns[1], m_columns[3] );
145 }
146 
147 double
150 {
151  assert ( axis == Axes::X || axis == Axes::Y );
152 
153  if ( axis == Axes::X ) {
154  if ( m_columns[2] == UINT_MAX ) {
155  return getPos ( m_columns[0] );
156  } else {
157  return getPosWithError ( m_columns[0], m_columns[2] );
158  }
159  }
160  // It has to be Y.
161  if ( m_columns[3] == UINT_MAX ) {
162  return getPos ( m_columns[1] );
163  }
164  // It has to be Y with an error.
165  return getPosWithError ( m_columns[1], m_columns[3] );
166 }
167 
169 {
170  m_pointreps.push_back ( "Symbol" );
171  m_pointreps.push_back ( "Line" );
172  m_pointreps.push_back ( "Column" );
173 }
174 
175 namespace dp = hippodraw::DataPoint2DTuple;
176 
177 DataSource *
179 createNTuple () const
180 {
181 
182  unsigned int x_col = m_columns[0];
183  unsigned int y_col = m_columns[1];
184 
185  unsigned int x_err = m_columns[2];
186  unsigned int y_err = m_columns[3];
187 
188  unsigned int columns = 4;
189  NTuple * ntuple = new NTuple ( columns );
190 
191  vector < string > labels;
192  labels.push_back ( m_ntuple -> getLabelAt ( x_col ) );
193  labels.push_back ( m_ntuple -> getLabelAt ( y_col ) );
194 
195  if ( x_err < UINT_MAX ) {
196  labels.push_back ( m_ntuple -> getLabelAt ( x_err ) );
197  } else {
198  labels.push_back ( dp::WIDTH );
199  }
200 
201  if ( y_err < UINT_MAX ) {
202  labels.push_back ( m_ntuple -> getLabelAt ( y_err ) );
203  } else {
204  labels.push_back ( dp::ERROR );
205  }
206  ntuple->setLabels ( labels );
207 
208  unsigned int size = m_ntuple -> rows ();
209  ntuple -> reserve ( size );
210 
211  fillProjectedValues ( ntuple );
212 
213  return ntuple;
214 }
215 
223 void
225 fillProjectedValues ( DataSource * ntuple, bool in_range ) const
226 {
227  ntuple -> clear ();
228 
229  unsigned int x_col = m_columns[0];
230  unsigned int y_col = m_columns[1];
231 
232  unsigned int x_err = m_columns[2];
233  unsigned int y_err = m_columns[3];
234 
235  const vector < string > & labels = m_ntuple -> getLabels ();
236  unsigned int size = labels.size();
237  if ( size > 2 ) {
238  if ( x_err == UINT_MAX &&
239  labels [ dp::XERR ] == dp::WIDTH ) x_err = dp::XERR;
240  if ( size > 3 ) {
241  if ( y_err == UINT_MAX &&
242  labels [ dp::YERR ] == dp::ERROR ) y_err = dp::YERR;
243  }
244  }
245  size = m_ntuple -> rows ();
246  vector < double > row ( dp::SIZE );
247  for ( unsigned int i = 0; i < size; i++ ) {
248  if ( acceptRow ( i, m_cut_list ) == false ||
249  ( in_range == true && inRange ( i ) == false ) ) continue;
250 
251  row[dp::X] = m_ntuple -> valueAt ( i, x_col );
252  row[dp::Y] = m_ntuple -> valueAt ( i, y_col );
253 
254 
255  double xe
256  = x_err < UINT_MAX ? m_ntuple -> valueAt ( i, x_err ) : 0.0;
257  double ye
258  = y_err < UINT_MAX ? m_ntuple -> valueAt( i, y_err ) : 0.0;
259 
260  row[dp::XERR] = xe;
261  row[dp::YERR] = ye;
262 
263  ntuple -> addRow ( row );
264  }
265 }
266 
267 void
270 {
271  if ( m_proj_values == 0 ) {
273  }
274  else if ( isDirty () ) {
276  }
277 
278  setDirty ( false );
279 }

Generated for HippoDraw Class Library by doxygen