Bins2DHist.cxx
Go to the documentation of this file.
1 
16 #ifdef _MSC_VER
17 // Include max() and min() missing from MicroSoft Visual C++.
18 #include "msdevstudio/MSconfig.h"
19 #endif
20 
21 #include "Bins2DHist.h"
22 
23 #include "BinnerAxis.h"
24 
26 #include "datasrcs/NTuple.h"
27 
28 #include <cmath>
29 
30 #include <cassert>
31 
32 #ifdef ITERATOR_MEMBER_DEFECT
33 using namespace std;
34 #else
35 using std::fill;
36 using std::list;
37 using std::sqrt;
38 using std::vector;
39 #endif
40 
41 using namespace hippodraw;
42 
43 Bins2DHist::Bins2DHist ( )
44  : Bins2DBase ( "Bins2DHist" )
45 {
46 }
47 
49  : Bins2DBase( binner ),
50  m_variance( binner.m_variance )
51 {
52  m_num_bins = binner.m_num_bins;
53 
54  for ( int i = 0; i < 3; i++ ) {
55  m_x_moments[i] = binner.m_x_moments[i];
56  m_y_moments[i] = binner.m_y_moments[i];
57  }
58 }
59 
60 BinsBase *
62 {
63  return new Bins2DHist ( *this );
64 }
65 
67 {
68 }
69 
70 void
73 {
74  assert ( axis == Axes::X || axis == Axes::Y );
75 
76  if ( axis == Axes::X ) {
77  m_data.resize ( nb + 2 ); //GB
78  m_variance.resize ( nb ); //GB
79  Bins2DBase::setNumberOfBins ( axis, nb );
80  }
81  else { // Y
82  Bins2DBase::setNumberOfBins ( axis, nb );
83 
84  int number_y = numberOfBins ( Axes::Y );
85 
86  unsigned int i = 0; // For Visual Studio C++ for scope bug
87  for( ; i < m_data.size(); i++ ) {
88  m_data[i].resize( number_y + 2 );
89  }
90 
91  for( i = 0; i < m_variance.size(); i++ ) {
92  m_variance[i].resize( number_y );
93  }
94  }
95 
96  reset ();
97 }
98 
100 {
101  unsigned int i = 0; // For Visual Studio for scope bug.
102  for ( ; i < m_data.size(); i++ ) {
103  fill ( m_data[i].begin(), m_data[i].end(), 0.0 );
104  }
105 
106  for ( i = 0; i < m_variance.size(); i++ ) {
107  fill ( m_variance[i].begin(), m_variance[i].end(), 0.0 );
108  }
109 
110  fill ( m_x_moments, m_x_moments + 3, 0.0 );
111  fill ( m_y_moments, m_y_moments + 3, 0.0 );
112 
113  m_empty = true;
114 }
115 
116 void Bins2DHist::accumulate( double x, double y, double wt, double )
117 {
118  int i = binNumberX ( x );
119  int j = binNumberY ( y );
120 
121  if( i > 0 && i <= numberOfBins ( Axes::X ) &&
122  j > 0 && j <= numberOfBins ( Axes::Y ) ) {
123  m_variance[i-1][j-1] += wt * wt;
124  m_x_moments[0] += wt;
125  m_x_moments[1] += wt * x;
126  m_x_moments[2] += wt * x * x;
127  m_y_moments[0] += wt;
128  m_y_moments[1] += wt * y;
129  m_y_moments[2] += wt * y * y;
130  }
131  m_data[i][j] += wt;
132 
133  m_empty = false;
134 }
135 
136 double Bins2DHist::getZValue ( double x, double y ) const
137 {
138  int i = binNumberX( x );
139  int j = binNumberY( y );
140  double widthX = binWidthX ( i-1 );
141  double widthY = binWidthY ( j-1 );
142  double v = m_data[i][j] / ( widthX * widthY );
143  return v;
144 }
145 
146 NTuple *
148 createNTuple () const
149 {
150  unsigned int size = numberOfBins ( Axes::X ) * numberOfBins ( Axes::Y );
151  NTuple * ntuple = prepareNTuple ( size );
152 
153  fillDataSource ( ntuple );
154 
155  return ntuple;
156 }
157 
158 namespace dp = hippodraw::DataPoint3DTuple;
159 
160 void
162 fillDataSource ( DataSource * ntuple ) const
163 {
164  ntuple -> clear ();
165 
166  double total = getNumberOfEntries ();
167  double factor = m_is_scaling ? m_scale_factor / total : 1.0;
168 
169  vector < double > row ( dp::SIZE );
170 
171  double x = getLow ( Axes::X );
172 
173  unsigned int num_x = numberOfBins ( Axes::X );
174  unsigned int num_y = numberOfBins ( Axes::Y );
175 
176  for ( unsigned int i = 0; i < num_x; i++ ) {
177 
178  double widthX = binWidthX ( i );
179  double half_widthX = 0.5 * widthX;
180  x += half_widthX;
181 
182  double y = getLow ( Axes::Y );
183 
184  for ( unsigned int j = 0; j < num_y; j++ ) {
185  double widthY = binWidthY ( j );
186  double half_widthY = 0.5 * widthY;
187  y += half_widthY;
188 
189  double v = factor * ( m_data[i+1][j+1] / ( widthX * widthY ) );
190  double verr = factor * ( sqrt ( m_variance[i][j] ) );
191 
192  row[dp::X] = x;
193  row[dp::Y] = y;
194  row[dp::Z] = v;
195  row [dp::XERR] = half_widthX;
196  row [dp::YERR] = half_widthY;
197  row [dp::ZERR] = verr;
198 
199  ntuple -> addRow ( row );
200 
201  y += half_widthY;
202  }
203  x += half_widthX;
204  }
205 
206  vector < unsigned int > shape ( 3 );
207  shape[0] = num_x;
208  shape[1] = num_y;
209  shape[2] = dp::SIZE;
210 
211  ntuple -> setShape ( shape );
212 }
213 
214 void
216 setBinContents ( const DataSource * ntuple )
217 {
218  unsigned int num_x = numberOfBins ( Axes::X );
219  unsigned int num_y = numberOfBins ( Axes::Y );
220  unsigned int r = 0;
221  for ( unsigned int i = 0; i < num_x; i++ ) {
222  double widthX = binWidthX ( i );
223  for ( unsigned int j = 0; j < num_y; j++ ) {
224  double widthY = binWidthY ( j );
225  const vector < double > & row = ntuple -> getRow ( r++ );
226  double value = row [ dp::Z ];
227  double verr = row [ dp::ZERR ];
228  m_data[i+1][j+1] = value * ( widthX * widthY );
229  m_variance[i][j] = verr * verr;
230  }
231  }
232 }

Generated for HippoDraw Class Library by doxygen