DyHist1DProjector.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 "DyHist1DProjector.h"
18 
19 #include "axes/AxisModelBase.h"
20 #include "binners/BinsBase.h"
22 #include "datasrcs/NTuple.h"
23 
24 #include <climits>
25 #include <cassert>
26 
27 
28 using namespace hippodraw;
29 
30 #ifdef ITERATOR_MEMBER_DEFECT
31 using namespace std;
32 #else
33 using std::list;
34 using std::max;
35 using std::string;
36 using std::vector;
37 #endif
38 
40  : Hist1DProjImp ( ),
41  NTupleProjector ( 2 )
42 {
43  m_binding_options.push_back ( "X" );
44  m_binding_options.push_back ( "Weight (optional)" );
45  m_min_bindings = 1;
46 }
47 
53 DyHist1DProjector ( const DyHist1DProjector & projector )
54  : ProjectorBase ( projector ),
55  Hist1DProjImp ( projector ),
56  NTupleProjector ( projector ),
57  m_fixed ( projector.m_fixed )
58 {
59 }
60 
62 {
63  ProjectorBase * pb = new DyHist1DProjector( *this );
64  return pb;
65 }
66 
68 {
69  unsigned int cols = m_ntuple->columns () - 1;
70  if ( m_columns[0] > cols ) {
71  m_columns[0] = cols;
72  }
73  if ( m_columns[1] > cols ) m_columns[1] = UINT_MAX;
74 
75  m_binner->setDirty ( );
76 }
77 
82 {
83  if ( m_ntuple -> isNull () ) return;
84 
85  // Get the data and the optional weight column.
86  unsigned int column = m_columns[0];
87  unsigned int weight = UINT_MAX;
88  bool have_weight = m_columns[1] < UINT_MAX;
89 
90  if ( have_weight ) {
91  weight = m_columns[1];
92  }
93 
94  // Use integer indexing to ensure that it will take everything from the
95  // same row, including the cut values.
96 
97  m_binner->reset ();
98 
99  unsigned int size = m_ntuple -> rows ();
100  for ( unsigned int i = 0; i < size; i++ )
101  {
102  if ( acceptRow ( i, m_cut_list ) == false ) continue;
103  double x = m_ntuple -> valueAt ( i, column );
104  double w = 1.0;
105  if ( have_weight ) {
106  w = m_ntuple -> valueAt ( i, weight );
107  }
108  m_binner->accumulate( x, w );
109  }
110 }
111 
112 double
115 {
116  assert ( axis == Axes::X || axis == Axes::Y );
117 
118  if ( axis == Axes::X ) {
119  return getPos ( m_columns[0] );
120  }
121  // Y
122 
123  return getPosOnValue ();
124 }
125 
126 Range
129 {
130  assert ( axis == Axes::X || axis == Axes::Y );
131 
132  if ( axis == Axes::X ) {
133  return dataRange ( m_columns[0] );
134  }
135  // Y
136  return dataRangeOnValue ();
137 }
138 
139 const string & DyHist1DProjector::getYLabel ( bool density ) const
140 {
141  if ( density == false ) {
142  bool scaling = m_y_axis->isScaling ();
143  if ( scaling ) {
144  return m_y_label_entries;
145  }
146  }
147  // else
148  return m_y_label_density;
149 }
150 
151 namespace dp = hippodraw::DataPoint2DTuple;
152 
158 double
161 {
162  assert ( axis == Axes::X || axis == Axes::Y );
163 
164  double sum = 0.0;
165  double number = 0.0;
166 
167  string label = "";
168 
169  // Get the axis label.
170  switch ( axis ) {
171  case Axes::X:
172  label = getXLabel();
173  break;
174  case Axes::Y:
175  label = getYLabel();
176  break;
177  default:
178  break;
179  }
180 
181  // Get the NTuple.
182  const DataSource * tuple = getNTuple();
183  if ( tuple -> empty () || axis == Axes::Y ) {
184 
185  // The axis label is invalid.
186 
187  // This should not happen for DyHist1DProjector.
188  if ( axis == Axes::X ) return 0.0;
189 
190  // Get the range.
191  const Range & r = m_y_axis->getRange(false);
192 
193  double scale_factor = m_y_axis -> getScaleFactor ();
194  double min = r.low() * scale_factor;
195  double max = r.high() * scale_factor;
196 
197  const vector < double > & values
198  = m_proj_values -> getColumn ( dp::Y );
199 
200  for ( unsigned int i = 0; i < values.size(); i++ ) {
201  double val = values[i] * scale_factor;
202  // Add value to sum if its within the range.
203  if(val >= min && val <= max){
204  sum += val;
205  number ++;
206  }
207  }
208  }
209  else {
210 
211  // The axis label is valid. Reimplementation from NTupleProjector.
212 
213  unsigned int col = tuple -> indexOf ( label );
214  unsigned int size = tuple -> rows ();
215  const Range & range = getRange ( axis );
216 
217  for ( unsigned int i = 0; i < size; i++ ) {
218 
219  if ( ! acceptRow ( i, m_cut_list ) ) continue;
220  double value = tuple -> valueAt ( i, col );
221  if ( range.includes ( value ) ) {
222  sum += value;
223  number ++;
224  }
225 
226  }
227 
228  }
229 
230  return (sum / number);
231 
232 }
233 
234 /* virtual */
235 bool DyHist1DProjector::isAxisBinned ( const std::string & axis ) const
236 {
237  if ( axis == m_binding_options[0] ) {
238  return true;
239  }
240  return false;
241 }
242 
243 void
246  const Range & range,
247  bool const_width )
248 {
249  m_binner -> setRange ( axis, range, const_width );
250  if ( const_width == false ) {
251  checkScaling ();
252  }
253 
254  setDirty ( true );
255 }
256 
257 void
259 update ( const Observable * object )
260 {
261  const DataSource * datasource
262  = dynamic_cast < const DataSource * > ( object );
263 
264  if ( datasource != 0 ) {
265  NTupleProjector::update ( object );
266  }
267  else {
268  BinningProjector::update ( object );
269  }
270 }
271 
272 void
274 willDelete ( const Observable * object )
275 {
276  const DataSource * datasource
277  = dynamic_cast < const DataSource * > ( object );
278 
279  if ( datasource != 0 ) {
280  NTupleProjector::willDelete ( object );
281  }
282  else {
283  BinningProjector::willDelete ( object );
284  }
285 }
286 
287 int
289 getUnderflow () const
290 {
291  int underflow = m_binner->getUnderflow ();
292  return underflow;
293 }
294 
295 int
297 getOverflow () const
298 {
299  int overflow = m_binner->getOverflow ();
300  return overflow;
301 }
A namespace to set the standard for indexing into 2 dimension data point tuple.
virtual double getAverage(hippodraw::Axes::Type axis) const
Get the Average of all projected values on the specified axis.
virtual const Range & getRange(Axes::Type) const
Returns the Range along the specified axis.
virtual const std::string & getXLabel() const
Finds the X axis label of the plot.
bool m_fixed
Sets true if the bins are disconnected from the data source and are, thus, fixed. ...
virtual void willDelete(const Observable *object)
Implements Observer pattern.
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...
unsigned int m_min_bindings
The minimum number of columns that must be bound.
Part of an implementation of the Observable-Observer pattern based on the example in the GOF Patterns...
Definition: Observable.h:39
virtual void checkScaling()
Checks the axis scaling.
bool isScaling() const
Returns true if the axis is being scaled.
hippodraw::AxisModelBase class interface
virtual void willDelete(const Observable *object)
If object is the target of normalization, removes the target and sets normalization off...
std::vector< std::string > m_binding_options
The list of binding options for the Projector.
Type
Axes constants.
Definition: AxesType.h:31
DataSource * m_proj_values
The NTuple representing the result of the projection.
Definition: ProjectorBase.h:80
BinsBase * m_binner
The binner object.
virtual void willDelete(const Observable *)
Responds to willDelete message from the observed data source.
virtual void setBinnerRange(hippodraw::Axes::Type axis, const Range &range, bool const_width)
Sets the Range of the binner.
double high() const
Returns the maximum of the range object.
Definition: Range.cxx:100
virtual ProjectorBase * clone()
The clone function returns an object of its own kind which is a copy of this object at this moment...
virtual int getOverflow() const
Returns overflow.
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
virtual const DataSource * getNTuple() const
Returns the DataSource used by the projector.
A derived class of BinningProjector that projects a column of data into a one dimensional histogram...
virtual Range dataRangeOn(hippodraw::Axes::Type axis) const
Returns the range of the data on a specified axis.
virtual const std::string & getYLabel(bool density=false) const
Returns the label of the axis.
Namespace for HippoDraw.
Definition: AxesType.cxx:21
const DataSource * m_ntuple
The pointer to the data source being projected.
double getPos(int column) const
Returns the minimum positive value on the specified column.
virtual void accumulate(double x, double w_or_y=1.0, double z=1.0, double w=1.0)=0
Accumulates the data point with weight w.
AxisModelBase * m_y_axis
The AxisModel along the Y axis.
Definition: ProjectorBase.h:92
virtual void changedNTuple()
This function is called when the NTuple has been changed to a new one.
hippodraw::NTuple class interface.
Base class for DataSource.
Definition: DataSource.h:55
virtual void update(const Observable *)
Responds to update message from the data source.
intp size(numeric::array arr)
Definition: num_util.cpp:296
double low() const
Returns the minimum of the range object.
Definition: Range.cxx:87
DyHist1DProjector()
The default constructor.
STL namespace.
double getPosOnValue() const
Returns the smallest positive value on the Y axis.
Expresses a range of values.
Definition: Range.h:33
The base class for the Projector hierarchy.
Definition: ProjectorBase.h:56
An NTupleProjector is a projector that projects data from an DataSource object.
virtual int indexOf(const std::string &label) const
Returns the index of the label.
virtual void reset()=0
Resets the accumulation to zero.
CutList_t m_cut_list
A list of cuts that filter the projection.
virtual bool isAxisBinned(const std::string &axis) const
Returns true if specified axis is binned.
bool includes(double value) const
Returns true if the argument value is inside the range.
Definition: Range.cxx:146
hippodraw::DataPointTuple namespace interface
std::string m_y_label_density
The label of the y axis when axis is not scaled.
Definition: Hist1DProjImp.h:44
A derived class of BinningProjector that implements the member function common to classes implementin...
Definition: Hist1DProjImp.h:32
virtual void setRange(hippodraw::Axes::Type, bool)
virtual int getUnderflow() const =0
Returns the underflow.
virtual void update(const Observable *object)
Updates the receiving projector.
virtual void execute()
Projects the data source into bins.
Range dataRange(int column) const
Returns the range of data on the specified column.
virtual void update(const Observable *object)
Implements Observer pattern.
void setDirty()
Sets a flag to indicate that re-binning needs to be done.
Definition: BinsBase.cxx:64
virtual double getPosOn(hippodraw::Axes::Type axis) const
Returns the minimum positive value of the data on a specified axis.
DyHist1Projector class interface.
virtual int getOverflow() const =0
Returns the overflow.
std::string m_y_label_entries
The label of the y axis when axis is scaled.
Definition: Hist1DProjImp.h:40
column
The column indices for 2 dimension data point tuple.
hippodraw::BinsBase class interface
const Range & getRange(bool scaled) const
Returns the range represented by this AxisModel.
virtual void setDirty(bool value=true)
Sets the dirty flag to value.
virtual int getUnderflow() const
Returns underflow.
std::vector< unsigned int > m_columns
A vector containing indexes to the columns of the DataSource.
Range dataRangeOnValue() const
Returns the data range on the Y axis.

Generated for HippoDraw Class Library by doxygen