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 }
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
CutList_t m_cut_list
A list of cuts that filter the projection.
std::string ERROR
YERR column label.
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::vector< unsigned int > m_columns
A vector containing indexes to the columns of the DataSource.
std::string m_x_label
The label for the generated x axis.
Definition: Map1Projector.h:40
void * data(numeric::array arr)
Definition: num_util.cpp:389
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
An NTupleProjector is a projector that projects data from an DataSource object.
virtual Range valueRange() const
Finds the range of the projected values.
const std::string & getYLabel(bool density=false) const
Returns the label for the Y axis.
virtual double getPosOn(hippodraw::Axes::Type axis) const
Returns the minimum positive value of the data on a specified axis.
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.
hippodraw::NTuple class interface.
virtual void addPointReps()
Function to add the acceptable point reps.
error on X or half bin width
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.
const std::string & getXLabel() const
Returns the label (title) of the x axis.
virtual bool inRange(int row) const
Checks if the entries in a given row are within the range of the axis model.
virtual void setYErrorOption(bool enable)
Sets whether the Y error options are to be enabled or not.
virtual void prepareValues()
Informs the projector to prepare its projected values for plotting.
ProjectorBase * clone()
The clone function returns an object of its own kind which is a copy of this object at this moment...
virtual void fillProjectedValues(DataSource *ntuple, bool in_range=false) const
A DataSource class implemented with std::vector&lt;double&gt; to store the column data. ...
Definition: NTuple.h:33
bool excludes(double value) const
Returns true if the argument value is outside the range.
Definition: Range.cxx:153
The base class for the Projector hierarchy.
Definition: ProjectorBase.h:56
virtual double getAverage(hippodraw::Axes::Type axis) const
Get the Average of all projected values on the specified axis.
virtual DataSource * createNTuple() const
Creates an NTuple representation of the projected values.
Map1Projector()
This default constructor binds to the first two columns.
hippodraw::Map1Projector class interface
hippodraw::AxisModelBase class interface
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 changedNTuple()
This function is called when the NTuple has been changed to a new one.
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
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.
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 Range dataRangeOn(hippodraw::Axes::Type) const
Returns the range of the data on the specified axis.
std::string WIDTH
XERR column label.
virtual const std::string & getLabelAt(unsigned int index) const
Returns the label for the column at index index.
Definition: DataSource.cxx:179
A derived class of NTupleProjector that maps 1 DataSource column to a Y axis of two dimensional proje...
Definition: Map1Projector.h:33
Type
Axes constants.
Definition: AxesType.h:31
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen