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 #include "axes/AxisModelBase.h"
21 
23 #include "datasrcs/NTuple.h"
24 
25 #include <algorithm>
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::find;
38 using std::max;
39 using std::min;
40 using std::string;
41 using std::vector;
42 #endif
43 
45  : NTupleProjector ( 4 ),
46  m_x_option ( "X error (optional)" ),
47  m_y_option ( "Y error (optional)" )
48 {
49  m_binding_options.push_back ( "X" );
50  m_binding_options.push_back ( "Y" );
51  m_min_bindings = 2;
52  addPointReps();
53 }
54 
60 Map2Projector ( const Map2Projector & projector )
61  : ProjectorBase ( projector ),
62  NTupleProjector ( projector )
63 {
64  addPointReps();
65 }
66 
67 // For some reason, implementing empty destructor decrease code size
68 // by 5 kbytes.
70 {
71 }
72 
74 {
75  return new Map2Projector( *this );
76 }
77 
78 void Map2Projector::setXErrorOption ( bool enable )
79 {
80  const string name ( m_x_option );
81  vector< string >:: iterator first
82  = find ( m_binding_options.begin (),
83  m_binding_options.end (),
84  name );
85 
86  if ( first != m_binding_options.end () && !enable ) {
87  m_binding_options.erase ( first );
88  m_columns[2] = UINT_MAX;
89  }
90  else if ( enable ) {
91  m_binding_options.push_back ( name );
92  }
93 }
94 
97 void Map2Projector::setYErrorOption ( bool enable )
98 {
99  const string name ( m_y_option );
100  vector< string >:: iterator first
101  = find ( m_binding_options.begin (),
102  m_binding_options.end (),
103  name );
104  if ( first != m_binding_options.end () && !enable ) {
105  m_binding_options.erase ( first );
106  m_columns[3] = UINT_MAX;
107  }
108  else if ( enable ) {
109  m_binding_options.push_back ( name );
110  }
111 }
112 
114 {
115  unsigned int cols = m_ntuple->columns () - 1;
116  if ( m_columns[0] > cols ) m_columns[0] = cols;
117  if ( m_columns[1] > cols ) m_columns[1] = cols;
118  if ( m_columns[2] > cols ) m_columns[2] = UINT_MAX;
119  if ( m_columns[3] > cols ) m_columns[3] = UINT_MAX;
120 }
121 
123 {
124  return dataRangeOn ( Axes::Y );
125 }
126 
127 Range
130 {
131  assert ( axis == Axes::X || axis == Axes::Y );
132 
133  if ( axis == Axes::X ) {
134  if ( m_columns[2] == UINT_MAX ) {
135  return dataRange ( m_columns[0] );
136  } else {
137  return dataRangeWithError ( m_columns[0], m_columns[2] );
138  }
139  }
140  // It has to be Y.
141  if ( m_columns[3] == UINT_MAX ) {
142  return dataRange ( m_columns[1] );
143  }
144  // It has to be Y with an error.
145  return dataRangeWithError ( m_columns[1], m_columns[3] );
146 }
147 
148 double
151 {
152  assert ( axis == Axes::X || axis == Axes::Y );
153 
154  if ( axis == Axes::X ) {
155  if ( m_columns[2] == UINT_MAX ) {
156  return getPos ( m_columns[0] );
157  } else {
158  return getPosWithError ( m_columns[0], m_columns[2] );
159  }
160  }
161  // It has to be Y.
162  if ( m_columns[3] == UINT_MAX ) {
163  return getPos ( m_columns[1] );
164  }
165  // It has to be Y with an error.
166  return getPosWithError ( m_columns[1], m_columns[3] );
167 }
168 
170 {
171  m_pointreps.push_back ( "Symbol" );
172  m_pointreps.push_back ( "Line" );
173  m_pointreps.push_back ( "Column" );
174 }
175 
176 namespace dp = hippodraw::DataPoint2DTuple;
177 
178 DataSource *
180 createNTuple () const
181 {
182 
183  unsigned int x_col = m_columns[0];
184  unsigned int y_col = m_columns[1];
185 
186  unsigned int x_err = m_columns[2];
187  unsigned int y_err = m_columns[3];
188 
189  unsigned int columns = 4;
190  NTuple * ntuple = new NTuple ( columns );
191 
192  vector < string > labels;
193  labels.push_back ( m_ntuple -> getLabelAt ( x_col ) );
194  labels.push_back ( m_ntuple -> getLabelAt ( y_col ) );
195 
196  if ( x_err < UINT_MAX ) {
197  labels.push_back ( m_ntuple -> getLabelAt ( x_err ) );
198  } else {
199  labels.push_back ( dp::WIDTH );
200  }
201 
202  if ( y_err < UINT_MAX ) {
203  labels.push_back ( m_ntuple -> getLabelAt ( y_err ) );
204  } else {
205  labels.push_back ( dp::ERROR );
206  }
207  ntuple->setLabels ( labels );
208 
209  unsigned int size = m_ntuple -> rows ();
210  ntuple -> reserve ( size );
211 
212  fillProjectedValues ( ntuple );
213 
214  return ntuple;
215 }
216 
224 void
226 fillProjectedValues ( DataSource * ntuple, bool in_range ) const
227 {
228  ntuple -> clear ();
229 
230  unsigned int x_col = m_columns[0];
231  unsigned int y_col = m_columns[1];
232 
233  unsigned int x_err = m_columns[2];
234  unsigned int y_err = m_columns[3];
235 
236  const vector < string > & labels = m_ntuple -> getLabels ();
237  unsigned int size = labels.size();
238  if ( size > 2 ) {
239  if ( x_err == UINT_MAX &&
240  labels [ dp::XERR ] == dp::WIDTH ) x_err = dp::XERR;
241  if ( size > 3 ) {
242  if ( y_err == UINT_MAX &&
243  labels [ dp::YERR ] == dp::ERROR ) y_err = dp::YERR;
244  }
245  }
246  size = m_ntuple -> rows ();
247  vector < double > row ( dp::SIZE );
248  for ( unsigned int i = 0; i < size; i++ ) {
249  if ( acceptRow ( i, m_cut_list ) == false ||
250  ( in_range == true && inRange ( i ) == false ) ) continue;
251 
252  row[dp::X] = m_ntuple -> valueAt ( i, x_col );
253  row[dp::Y] = m_ntuple -> valueAt ( i, y_col );
254 
255 
256  double xe
257  = x_err < UINT_MAX ? m_ntuple -> valueAt ( i, x_err ) : 0.0;
258  double ye
259  = y_err < UINT_MAX ? m_ntuple -> valueAt( i, y_err ) : 0.0;
260 
261  row[dp::XERR] = xe;
262  row[dp::YERR] = ye;
263 
264  ntuple -> addRow ( row );
265  }
266 }
267 
268 void
271 {
272  if ( m_proj_values == 0 ) {
274  }
275  else if ( isDirty () ) {
277  }
278 
279  setDirty ( false );
280 }
unsigned int i
double getPos(int column) const
Returns the minimum positive value on the specified column.
CutList_t m_cut_list
A list of cuts that filter the projection.
std::string ERROR
YERR column label.
virtual bool inRange(int row) const
Checks if the entries in a given row are within the range of the axis model.
A derived class of NTupleProjector that maps 2 ntuple columns to a two dimensional projection...
Definition: Map2Projector.h:29
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...
std::string m_x_option
The label for the X error binding option.
Definition: Map2Projector.h:33
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
Fills the NTuple with the projected values.
std::vector< unsigned int > m_columns
A vector containing indexes to the columns of the DataSource.
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.
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.
ProjectorBase * clone()
The clone function returns an object of its own kind which is a copy of this object at this moment...
virtual ~Map2Projector()
The destructor.
virtual void setYErrorOption(bool enable)
Sets whether the Y error options are to be enabled or not.
virtual void setDirty(bool value=true)
Sets the dirty flag to value.
hippodraw::NTuple class interface.
error on X or half bin width
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.
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.
virtual void prepareValues()
Informs the projector to prepare its projected values for plotting.
virtual DataSource * createNTuple() const
Creates an NTuple representation of the projected values.
virtual void setXErrorOption(bool enable)
Sets whether the X error options are to be enabled or not.
virtual Range dataRangeOn(hippodraw::Axes::Type) const
Returns the range of the data on the specified axis.
virtual Range valueRange() const
Finds the range of the projected values.
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
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.
Expresses a range of values.
Definition: Range.h:33
std::vector< std::string > m_pointreps
Vector of acceptable PointReps.
std::string m_y_option
The label for the Y error binding option.
Definition: Map2Projector.h:36
hippodraw::DataPointTuple namespace interface
unsigned int m_min_bindings
The minimum number of columns that must be bound.
std::string WIDTH
XERR column label.
Type
Axes constants.
Definition: AxesType.h:31
Map2Projector()
This default constructor binds to the first two columns.
Base class for DataSource.
Definition: DataSource.h:55
hippodraw::Map2Projector class interface

Generated for HippoDraw Class Library by doxygen