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 <cassert>
25 #include <climits>
26 
27 using namespace hippodraw;
28 
29 #ifdef ITERATOR_MEMBER_DEFECT
30 using namespace std;
31 #else
32 using std::list;
33 using std::max;
34 using std::string;
35 using std::vector;
36 #endif
37 
39  : Hist1DProjImp ( ),
40  NTupleProjector ( 2 )
41 {
42  m_binding_options.push_back ( "X" );
43  m_binding_options.push_back ( "Weight (optional)" );
44  m_min_bindings = 1;
45 }
46 
52 DyHist1DProjector ( const DyHist1DProjector & projector )
53  : ProjectorBase ( projector ),
54  Hist1DProjImp ( projector ),
55  NTupleProjector ( projector ),
56  m_fixed ( projector.m_fixed )
57 {
58 }
59 
61 {
62  ProjectorBase * pb = new DyHist1DProjector( *this );
63  return pb;
64 }
65 
67 {
68  unsigned int cols = m_ntuple->columns () - 1;
69  if ( m_columns[0] > cols ) {
70  m_columns[0] = cols;
71  }
72  if ( m_columns[1] > cols ) m_columns[1] = UINT_MAX;
73 
74  m_binner->setDirty ( );
75 }
76 
81 {
82  if ( m_ntuple -> isNull () ) return;
83 
84  // Get the data and the optional weight column.
85  unsigned int column = m_columns[0];
86  unsigned int weight = UINT_MAX;
87  bool have_weight = m_columns[1] < UINT_MAX;
88 
89  if ( have_weight ) {
90  weight = m_columns[1];
91  }
92 
93  // Use integer indexing to ensure that it will take everything from the
94  // same row, including the cut values.
95 
96  m_binner->reset ();
97 
98  unsigned int size = m_ntuple -> rows ();
99  for ( unsigned int i = 0; i < size; i++ )
100  {
101  if ( acceptRow ( i, m_cut_list ) == false ) continue;
102  double x = m_ntuple -> valueAt ( i, column );
103  double w = 1.0;
104  if ( have_weight ) {
105  w = m_ntuple -> valueAt ( i, weight );
106  }
107  m_binner->accumulate( x, w );
108  }
109 }
110 
111 double
114 {
115  assert ( axis == Axes::X || axis == Axes::Y );
116 
117  if ( axis == Axes::X ) {
118  return getPos ( m_columns[0] );
119  }
120  // Y
121 
122  return getPosOnValue ();
123 }
124 
125 Range
128 {
129  assert ( axis == Axes::X || axis == Axes::Y );
130 
131  if ( axis == Axes::X ) {
132  return dataRange ( m_columns[0] );
133  }
134  // Y
135  return dataRangeOnValue ();
136 }
137 
138 const string & DyHist1DProjector::getYLabel ( bool density ) const
139 {
140  if ( density == false ) {
141  bool scaling = m_y_axis->isScaling ();
142  if ( scaling ) {
143  return m_y_label_entries;
144  }
145  }
146  // else
147  return m_y_label_density;
148 }
149 
150 namespace dp = hippodraw::DataPoint2DTuple;
151 
157 double
160 {
161  assert ( axis == Axes::X || axis == Axes::Y );
162 
163  double sum = 0.0;
164  double number = 0.0;
165 
166  string label = "";
167 
168  // Get the axis label.
169  switch ( axis ) {
170  case Axes::X:
171  label = getXLabel();
172  break;
173  case Axes::Y:
174  label = getYLabel();
175  break;
176  default:
177  break;
178  }
179 
180  // Get the NTuple.
181  const DataSource * tuple = getNTuple();
182  if ( tuple -> empty () || axis == Axes::Y ) {
183 
184  // The axis label is invalid.
185 
186  // This should not happen for DyHist1DProjector.
187  if ( axis == Axes::X ) return 0.0;
188 
189  // Get the range.
190  const Range & r = m_y_axis->getRange(false);
191 
192  double scale_factor = m_y_axis -> getScaleFactor ();
193  double min = r.low() * scale_factor;
194  double max = r.high() * scale_factor;
195 
196  const vector < double > & values
197  = m_proj_values -> getColumn ( dp::Y );
198 
199  for ( unsigned int i = 0; i < values.size(); i++ ) {
200  double val = values[i] * scale_factor;
201  // Add value to sum if its within the range.
202  if(val >= min && val <= max){
203  sum += val;
204  number ++;
205  }
206  }
207  }
208  else {
209 
210  // The axis label is valid. Reimplementation from NTupleProjector.
211 
212  unsigned int col = tuple -> indexOf ( label );
213  unsigned int size = tuple -> rows ();
214  const Range & range = getRange ( axis );
215 
216  for ( unsigned int i = 0; i < size; i++ ) {
217 
218  if ( ! acceptRow ( i, m_cut_list ) ) continue;
219  double value = tuple -> valueAt ( i, col );
220  if ( range.includes ( value ) ) {
221  sum += value;
222  number ++;
223  }
224 
225  }
226 
227  }
228 
229  return (sum / number);
230 
231 }
232 
233 /* virtual */
234 bool DyHist1DProjector::isAxisBinned ( const std::string & axis ) const
235 {
236  if ( axis == m_binding_options[0] ) {
237  return true;
238  }
239  return false;
240 }
241 
242 void
245  const Range & range,
246  bool const_width )
247 {
248  m_binner -> setRange ( axis, range, const_width );
249  if ( const_width == false ) {
250  checkScaling ();
251  }
252 
253  setDirty ( true );
254 }
255 
256 void
258 update ( const Observable * object )
259 {
260  const DataSource * datasource
261  = dynamic_cast < const DataSource * > ( object );
262 
263  if ( datasource != 0 ) {
264  NTupleProjector::update ( object );
265  }
266  else {
267  BinningProjector::update ( object );
268  }
269 }
270 
271 void
273 willDelete ( const Observable * object )
274 {
275  const DataSource * datasource
276  = dynamic_cast < const DataSource * > ( object );
277 
278  if ( datasource != 0 ) {
279  NTupleProjector::willDelete ( object );
280  }
281  else {
282  BinningProjector::willDelete ( object );
283  }
284 }
285 
286 int
288 getUnderflow () const
289 {
290  int underflow = m_binner->getUnderflow ();
291  return underflow;
292 }
293 
294 int
296 getOverflow () const
297 {
298  int overflow = m_binner->getOverflow ();
299  return overflow;
300 }

Generated for HippoDraw Class Library by doxygen