Bins2DProfile.cxx
Go to the documentation of this file.
1 
18 #ifdef _MSC_VER
19 // Include max() and min() missing from MicroSoft Visual C++.
20 #include "msdevstudio/MSconfig.h"
21 #endif
22 
23 #include "Bins2DProfile.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 Bins2DProfile::Bins2DProfile ( )
44  : Bins2DBase ( "Bins2DProfile" )
45 {
46 }
47 
49  : Bins2DBase( binner ),
50  m_sumZ( binner.m_sumZ ),
51  m_variance( binner.m_variance ),
52  m_z_range ( binner.m_z_range )
53 {
54  m_num_bins = binner.m_num_bins;
55 }
56 
58 {
59 }
60 
61 BinsBase *
63 {
64  return new Bins2DProfile ( *this );
65 }
66 
67 void
70 {
71  assert ( axis == Axes::X || axis == Axes::Y );
72 
73  Bins2DBase::setNumberOfBins ( axis, nb );
74  if ( axis == Axes::X ) {
75  int number = numberOfBins ( Axes::X );
76  m_data.resize( number + 2 );
77  m_sumZ.resize( number + 2 );
78  m_variance.resize( number );
79  }
80  else { // Y
81  int number_y = numberOfBins ( Axes::Y );
82  unsigned int i = 0; // For Visual Studio for scope bug.
83  for ( ; i < m_data.size(); i++ ) {
84  m_data[i].resize( number_y + 2 );
85  m_sumZ[i].resize( number_y + 2 );
86  }
87  for ( i = 0; i < m_variance.size(); i++ ) {
88  m_variance[i].resize( number_y );
89  }
90  }
91 
92  reset ();
93 
95 }
96 
98 {
99  unsigned int i = 0; // For Visual Studio for scope bug.
100  for ( ; i < m_data.size(); i++ ) {
101  fill ( m_data[i].begin(), m_data[i].end(), 0.0 );
102  fill ( m_sumZ[i].begin(), m_sumZ[i].end(), 0.0 );
103  }
104 
105  for ( i = 0; i < m_variance.size(); i++ ) {
106  fill ( m_variance[i].begin(), m_variance[i].end(), 0.0 );
107  }
108 
109  m_empty = true;
110 }
111 
112 void Bins2DProfile::accumulate( double x, double y, double z, double wt )
113 {
114  int i = binNumberX( x );
115  int j = binNumberY( y );
116 
117  if( i > 0 && i <= numberOfBins ( Axes::X ) &&
118  j > 0 && j <= numberOfBins ( Axes::Y ) ) {
119  m_variance[i-1][j-1] += wt * wt;
120  }
121  m_data[i][j] += wt;
122  m_sumZ[i][j] += z;
123 
124  m_empty = false;
125 }
126 
127 double Bins2DProfile::getZValue ( double x, double y ) const
128 {
129 
130  int i = binNumberX( x );
131  int j = binNumberY( y );
132 
133  /* Avoid nan, is this the fix? */
134  if (m_data[i][j] == 0) return 0.0;
135 
136  double v = m_sumZ[i][j] / m_data[i][j];
137  return v;
138 }
139 
140 NTuple *
142 createNTuple () const
143 {
144  unsigned int size = numberOfBins ( Axes::X ) * numberOfBins ( Axes::Y );
145  NTuple * ntuple = prepareNTuple ( size );
146 
147  fillDataSource ( ntuple );
148 
149  return ntuple;
150 }
151 
152 namespace dp = hippodraw::DataPoint3DTuple;
153 
157 void
159 fillDataSource ( DataSource * ntuple ) const
160 {
161  ntuple -> clear ();
162  vector < double > row ( dp::SIZE );
163 
164  int v_inc = 0;
165  vector<double>::const_iterator vity;
166  double next_x = getLow ( Axes::X );
167 
168  int num_x = numberOfBins ( Axes::X );
169  int num_y = numberOfBins ( Axes::Y );
170 
171  for ( int x_inc = 1; x_inc <= num_x; x_inc++ ) {
172  vity = m_variance[v_inc++].begin();
173  double widthX = binWidthX ( x_inc - 1 );
174  double half_widthX = 0.5 * widthX;
175  double x = next_x + half_widthX;
176  next_x += widthX;
177  double next_y = getLow ( Axes::Y );
178 
179  for ( int y_inc = 1; y_inc <= num_y; y_inc++ ) {
180  double widthY = binWidthY ( y_inc - 1 );
181  double half_widthY = 0.5 * widthY;
182  double y = next_y;
183  y += half_widthY;
184  next_y += widthY;
185 
186  double var2 = *vity++;
187  double v = 0.0;
188  double verr = 0.0;
189  if ( m_data[x_inc][y_inc] != 0 ) {
190  double num = m_data[x_inc][y_inc];
191  v = m_sumZ[x_inc][y_inc] / num;
192  verr = sqrt ( ( var2 / ( num - 1. ) - v * v ) );
193  }
194  row[dp::X] = x;
195  row[dp::Y] = y;
196  row[dp::Z] = v;
197  row [dp::XERR] = half_widthX;
198  row [dp::YERR] = half_widthY;
199  row [dp::ZERR] = verr;
200 
201  ntuple -> addRow ( row );
202  }
203  }
204  vector < unsigned int > shape ( 3 );
205  shape[0] = num_x;
206  shape[1] = num_y;
207  shape[2] = dp::SIZE;
208 
209  ntuple -> setShape ( shape );
210 }
211 
215 void
218 {
219 }

Generated for HippoDraw Class Library by doxygen