GeneralBrokenLines  trunk_rev46
 All Classes Files Functions Variables Typedefs Pages
GblData.cpp
Go to the documentation of this file.
1 /*
2  * GblData.cpp
3  *
4  * Created on: Aug 18, 2011
5  * Author: kleinwrt
6  */
7 
8 #include "GblData.h"
9 
11 
16 GblData::GblData(unsigned int aLabel, double aValue, double aPrec) :
17  theLabel(aLabel), theValue(aValue), thePrecision(aPrec), theDownWeight(
18  1.), thePrediction(0.), theParameters(), theDerivatives(), globalLabels(), globalDerivatives() {
19 
20 }
21 
23 }
24 
26 
38 void GblData::addDerivatives(unsigned int iRow,
39  const std::vector<unsigned int> &labDer, const SMatrix55 &matDer,
40  unsigned int iOff, const TMatrixD &derLocal,
41  const std::vector<int> &labGlobal, const TMatrixD &derGlobal,
42  unsigned int extOff, const TMatrixD &extDer) {
43 
44  unsigned int nParMax = 5 + derLocal.GetNcols() + extDer.GetNcols();
45  theParameters.reserve(nParMax); // have to be sorted
46  theDerivatives.reserve(nParMax);
47 
48  for (int i = 0; i < derLocal.GetNcols(); ++i) // local derivatives
49  {
50  if (derLocal(iRow - iOff, i)) {
51  theParameters.push_back(i + 1);
52  theDerivatives.push_back(derLocal(iRow - iOff, i));
53  }
54  }
55 
56  for (int i = 0; i < extDer.GetNcols(); ++i) // external derivatives
57  {
58  if (extDer(iRow - iOff, i)) {
59  theParameters.push_back(extOff + i + 1);
60  theDerivatives.push_back(extDer(iRow - iOff, i));
61  }
62  }
63 
64  for (unsigned int i = 0; i < 5; ++i) // curvature, offset derivatives
65  {
66  if (labDer[i] and matDer(iRow, i)) {
67  theParameters.push_back(labDer[i]);
68  theDerivatives.push_back(matDer(iRow, i));
69  }
70  }
71 
72  globalLabels = labGlobal;
73  for (int i = 0; i < derGlobal.GetNcols(); ++i) // global derivatives
74  globalDerivatives.push_back(derGlobal(iRow - iOff, i));
75 }
76 
78 
86 void GblData::addDerivatives(unsigned int iRow,
87  const std::vector<unsigned int> &labDer, const SMatrix27 &matDer,
88  unsigned int extOff, const TMatrixD &extDer) {
89 
90  unsigned int nParMax = 7 + extDer.GetNcols();
91  theParameters.reserve(nParMax); // have to be sorted
92  theDerivatives.reserve(nParMax);
93 
94  for (int i = 0; i < extDer.GetNcols(); ++i) // external derivatives
95  {
96  if (extDer(iRow, i)) {
97  theParameters.push_back(extOff + i + 1);
98  theDerivatives.push_back(extDer(iRow, i));
99  }
100  }
101 
102  for (unsigned int i = 0; i < 7; ++i) // curvature, offset derivatives
103  {
104  if (labDer[i] and matDer(iRow, i)) {
105  theParameters.push_back(labDer[i]);
106  theDerivatives.push_back(matDer(iRow, i));
107  }
108  }
109 }
110 
112 
117 void GblData::addDerivatives(const std::vector<unsigned int> &index,
118  const std::vector<double> &derivatives) {
119  for (unsigned int i = 0; i < derivatives.size(); ++i) // any derivatives
120  {
121  if (derivatives[i]) {
122  theParameters.push_back(index[i]);
123  theDerivatives.push_back(derivatives[i]);
124  }
125  }
126 }
127 
129 void GblData::setPrediction(const VVector &aVector) {
130 
131  thePrediction = 0.;
132  for (unsigned int i = 0; i < theDerivatives.size(); ++i) {
133  thePrediction += theDerivatives[i] * aVector(theParameters[i] - 1);
134  }
135 }
136 
138 
141 double GblData::setDownWeighting(unsigned int aMethod) {
142 
143  double aWeight = 1.;
144  double scaledResidual = fabs(theValue - thePrediction) * sqrt(thePrecision);
145  if (aMethod == 1) // Tukey
146  {
147  if (scaledResidual < 4.6851) {
148  aWeight = (1.0 - 0.045558 * scaledResidual * scaledResidual);
149  aWeight *= aWeight;
150  } else {
151  aWeight = 0.;
152  }
153  } else if (aMethod == 2) //Huber
154  {
155  if (scaledResidual >= 1.345) {
156  aWeight = 1.345 / scaledResidual;
157  }
158  } else if (aMethod == 3) //Cauchy
159  {
160  aWeight = 1.0 / (1.0 + (scaledResidual * scaledResidual / 5.6877));
161  }
162  theDownWeight = aWeight;
163  return aWeight;
164 }
165 
167 
170 double GblData::getChi2() const {
171  double aDiff = theValue - thePrediction;
172  return aDiff * aDiff * thePrecision * theDownWeight;
173 }
174 
176 void GblData::printData() const {
177 
178  std::cout << " meas. " << theLabel << "," << theValue << "," << thePrecision
179  << std::endl;
180  std::cout << " param " << theParameters.size() << ":";
181  for (unsigned int i = 0; i < theParameters.size(); ++i) {
182  std::cout << " " << theParameters[i];
183  }
184  std::cout << std::endl;
185  std::cout << " deriv " << theDerivatives.size() << ":";
186  for (unsigned int i = 0; i < theDerivatives.size(); ++i) {
187  std::cout << " " << theDerivatives[i];
188  }
189  std::cout << std::endl;
190 }
191 
193 
199 void GblData::getLocalData(double &aValue, double &aWeight,
200  std::vector<unsigned int>* &indLocal, std::vector<double>* &derLocal) {
201 
202  aValue = theValue;
203  aWeight = thePrecision * theDownWeight;
204  indLocal = &theParameters;
205  derLocal = &theDerivatives;
206 }
207 
209 
217 void GblData::getAllData(float &fValue, float &fErr,
218  std::vector<unsigned int>* &indLocal, std::vector<double>* &derLocal,
219  std::vector<int>* &labGlobal, std::vector<double>* &derGlobal) {
220  fValue = theValue;
221  fErr = 1.0 / sqrt(thePrecision);
222  indLocal = &theParameters;
223  derLocal = &theDerivatives;
224  labGlobal = &globalLabels;
225  derGlobal = &globalDerivatives;
226 }
227 
229 
236 void GblData::getResidual(double &aResidual, double &aVariance,
237  double &aDownWeight, std::vector<unsigned int>* &indLocal,
238  std::vector<double>* &derLocal) {
239  aResidual = theValue - thePrediction;
240  aVariance = 1.0 / thePrecision;
241  aDownWeight = theDownWeight;
242  indLocal = &theParameters;
243  derLocal = &theDerivatives;
244 }
245