Map1Projector.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 "Map1Projector.h"
18 
19 #include "axes/AxisModelBase.h"
20 
22 #include "datasrcs/NTuple.h"
23 
24 #include <algorithm>
25 #include <numeric>
26 
27 #include <cfloat>
28 #include <climits>
29 
30 #include <cassert>
31 
32 using namespace hippodraw;
33 
34 #ifdef ITERATOR_MEMBER_DEFECT
35 using namespace std;
36 #else
37 using std::accumulate;
38 using std::find;
39 using std::max;
40 using std::min;
41 using std::string;
42 using std::vector;
43 #endif
44 
46  : NTupleProjector ( 2 ),
47  m_x_label ( "Item" ),
48  m_y_option ( "Y error (optional)" )
49 {
50  m_binding_options.push_back ( "Y" );
51  m_min_bindings = 1;
52  addPointReps();
53 }
54 
60 Map1Projector ( const Map1Projector & projector )
61  : ProjectorBase ( projector ),
62  NTupleProjector( projector ),
63  m_x_label ( projector.m_x_label )
64 {
65  addPointReps();
66 }
67 
69 {
70  return new Map1Projector( *this );
71 }
72 
73 bool
75 inRange ( int row ) const
76 {
77  bool yes = true;
78  double x = row;
79  const Range & x_range = m_x_axis->getRange ( false );
80  if ( x_range.excludes ( x ) ) {
81  yes = false;
82  }
83  else {
84  const Range & y_range = m_y_axis->getRange ( false );
85  if ( y_range.excludes ( m_ntuple ->
86  valueAt ( row, m_columns[0] ) ) ) yes = false;
87  }
88 
89  return yes;
90 }
91 
92 
93 void Map1Projector::setYErrorOption ( bool enable )
94 {
95  const string name ( "Y error" );
96  vector< string >:: iterator first
97  = find ( m_binding_options.begin (),
98  m_binding_options.end (),
99  name );
100  if ( first != m_binding_options.end () && !enable ) {
101  m_binding_options.erase ( first );
102  m_columns[1] = UINT_MAX;
103  }
104  else if ( enable ) {
105  m_binding_options.push_back ( name );
106  }
107 }
108 
110 {
111  unsigned int cols = m_ntuple->columns () - 1;
112  if ( m_columns[0] > cols ) m_columns[0] = cols;
113  if ( m_columns[1] > cols ) m_columns[1] = UINT_MAX;
114 }
115 
117 {
118  return dataRangeOn ( Axes::Y );
119 }
120 
121 Range
124 {
125  assert ( axis == Axes::X || axis == Axes::Y );
126 
127  if ( axis == Axes::X ) {
128  double pos = getPosOn ( Axes::X );
129  return Range ( 0.0, m_ntuple->rows (), pos );
130  }
131  // It has to be Y.
132  if ( m_columns[1] == UINT_MAX ) {
133  return dataRange ( m_columns[0] );
134  }
135  //It has to be Y with an error.
136  return dataRangeWithError ( m_columns[0], m_columns[1] );
137 }
138 
139 double
142 {
143  assert ( axis == Axes::X || axis == Axes::Y );
144 
145  if ( axis == Axes::X ) {
146  return m_ntuple -> empty () ? DBL_MAX : 1.0;
147  }
148  // It has to be Y.
149  if ( m_columns[1] == UINT_MAX ) {
150  return getPos ( m_columns[0] );
151  }
152  //It has to be Y with an error.
153  return getPosWithError ( m_columns[0], m_columns[1] );
154 }
155 
156 const string & Map1Projector::getXLabel() const
157 {
158  return m_x_label;
159 }
160 
161 const string & Map1Projector::getYLabel ( bool ) const
162 {
163  return m_ntuple->getLabelAt( m_columns[0] );
164 }
165 
166 namespace dp = hippodraw::DataPoint2DTuple;
167 
168 
169 double
172 {
173  Map1Projector * p = const_cast < Map1Projector * > ( this );
174  p -> prepareValues ();
175 
176  unsigned int col = 2; // bad value
177  switch ( axis ) {
178 
179  case Axes::X:
180  col = dp::X;
181  break;
182 
183  case Axes::Y:
184  col = dp::Y;
185  break;
186 
187  default:
188  break;
189  }
190  assert ( col < 2 );
191 
192  const DataSource * ntuple = getProjectedValues ();
193  const vector < double > & data = ntuple -> getColumn ( col );
194 
195  unsigned int size = ntuple -> rows ();
196  double sum = 0.0;
197  sum = accumulate ( data.begin(), data.end(), sum );
198 
199  return sum / size;
200 }
201 
203 {
204  m_pointreps.push_back ( "Symbol" );
205 }
206 
207 DataSource *
209 createNTuple () const
210 {
211  unsigned int columns = 4;
212  NTuple * ntuple = new NTuple ( columns );
213 
214  vector < string > labels;
215  labels.push_back ( "X" );
216  labels.push_back ( "Value" );
217  labels.push_back ( dp::WIDTH );
218  labels.push_back ( dp::ERROR );
219 
220  ntuple->setLabels ( labels );
221 
222  fillProjectedValues ( ntuple );
223 
224  return ntuple;
225 }
226 
227 void
229 fillProjectedValues ( DataSource * ntuple, bool ) const
230 {
231  unsigned int y_col = m_columns[0];
232  unsigned int y_err = m_columns[1];
233  unsigned int size = m_ntuple -> rows ();
234 
235  ntuple -> clear();
236  ntuple -> reserve ( size );
237 
238  vector < double > row ( dp::SIZE );
239  for ( unsigned int i = 0; i < size; i++ ) {
240  if ( acceptRow ( i, m_cut_list ) == false ||
241  inRange ( i ) == false ) continue;
242  row[dp::X] = i;
243  row[dp::Y] = m_ntuple -> valueAt ( i, y_col );
244 
245  double ye
246  = y_err < UINT_MAX ? (m_ntuple -> valueAt ( i, y_err ) ) : 0.0;
247  row[dp::XERR] = 0.0;
248  row[dp::YERR] = ye;
249  ntuple -> addRow ( row );
250  }
251 }
252 
253 void
256 {
257  if ( m_proj_values == 0 ) {
259  } else {
261  }
262 
263  setDirty ( false );
264 }

Generated for HippoDraw Class Library by doxygen