GeneralBrokenLines  Rev: 2.2.0
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 
30 #include "GblData.h"
31 using namespace Eigen;
32 
34 namespace gbl {
35 
37 
45 GblData::GblData(unsigned int aLabel, dataBlockType aType, double aValue,
46  double aPrec, unsigned int aTraj, unsigned int aPoint) :
47  theLabel(aLabel), theRow(0), theType(aType), theValue(aValue), thePrecision(
48  aPrec), theTrajectory(aTraj), thePoint(aPoint), theDWMethod(0), theDownWeight(
49  1.), thePrediction(0.), theNumLocal(0), moreParameters(), moreDerivatives() {
50 
51 }
52 
54 }
55 
57 
62 void GblData::addDerivatives(const std::vector<unsigned int> &index,
63  const std::vector<double> &derivatives) {
64  for (unsigned int i = 0; i < derivatives.size(); ++i) // any derivatives
65  {
66  if (derivatives[i]) {
67  moreParameters.push_back(index[i]);
68  moreDerivatives.push_back(derivatives[i]);
69  }
70  }
71 }
72 
74 void GblData::setPrediction(const VVector &aVector) {
75 
76  thePrediction = 0.;
77  if (theNumLocal > 0) {
78  for (unsigned int i = 0; i < theNumLocal; ++i) {
79  thePrediction += theDerivatives[i] * aVector(theParameters[i] - 1);
80  }
81  } else {
82  for (unsigned int i = 0; i < moreDerivatives.size(); ++i) {
84  * aVector(moreParameters[i] - 1);
85  }
86  }
87 }
88 
90 
93 double GblData::setDownWeighting(unsigned int aMethod) {
94 
95  theDWMethod = aMethod;
96  double aWeight = 1.;
97  double scaledResidual = fabs(theValue - thePrediction) * sqrt(thePrecision);
98  if (aMethod == 1) // Tukey
99  {
100  if (scaledResidual < 4.6851) {
101  aWeight = (1.0 - 0.045558 * scaledResidual * scaledResidual);
102  aWeight *= aWeight;
103  } else {
104  aWeight = 0.;
105  }
106  } else if (aMethod == 2) //Huber
107  {
108  if (scaledResidual >= 1.345) {
109  aWeight = 1.345 / scaledResidual;
110  }
111  } else if (aMethod == 3) //Cauchy
112  {
113  aWeight = 1.0 / (1.0 + (scaledResidual * scaledResidual / 5.6877));
114  }
115  theDownWeight = aWeight;
116  return aWeight;
117 }
118 
120 
125 double GblData::getChi2() const {
126  double scaledResidual = fabs(theValue - thePrediction) * sqrt(thePrecision);
127  double chi2 = scaledResidual * scaledResidual;
128  if (theDWMethod == 1) // Tukey
129  {
130  if (scaledResidual < 4.6851) {
131  chi2 = (1.0
132  - pow(1.0 - 0.045558 * scaledResidual * scaledResidual, 3))
133  / (3. * 0.045558);
134  } else {
135  chi2 = 1.0 / (3. * 0.045558);
136  }
137  } else if (theDWMethod == 2) //Huber
138  {
139  if (scaledResidual >= 1.345) {
140  chi2 = 1.345 * (2. * scaledResidual - 1.345);
141  }
142  } else if (theDWMethod == 3) //Cauchy
143  {
144  chi2 = log(1.0 + (scaledResidual * scaledResidual / 5.6877)) * 5.6877;
145  }
146  return chi2;
147 }
148 
150 void GblData::printData() const {
151 
152  std::cout << " measurement at label " << theLabel << " of type " << theType
153  << " from row " << theRow << ": " << theValue << ", "
154  << thePrecision << std::endl;
155  std::cout << " param " << moreParameters.size() + theNumLocal << ":";
156  for (unsigned int i = 0; i < moreParameters.size(); ++i) {
157  std::cout << " " << moreParameters[i];
158  }
159  for (unsigned int i = 0; i < theNumLocal; ++i) {
160  std::cout << " " << theParameters[i];
161  }
162  std::cout << std::endl;
163  std::cout << " deriv " << moreDerivatives.size() + theNumLocal << ":";
164  for (unsigned int i = 0; i < moreDerivatives.size(); ++i) {
165  std::cout << " " << moreDerivatives[i];
166  }
167  for (unsigned int i = 0; i < theNumLocal; ++i) {
168  std::cout << " " << theDerivatives[i];
169  }
170  std::cout << std::endl;
171 }
172 
174 
177 unsigned int GblData::getLabel() const {
178  return theLabel;
179 }
180 
182 
186  return theType;
187 }
188 
190 
197 void GblData::getLocalData(double &aValue, double &aWeight,
198  unsigned int &numLocal, unsigned int* &indLocal, double* &derLocal) {
199 
200  aValue = theValue;
201  aWeight = thePrecision * theDownWeight;
202  if (theNumLocal > 0) {
203  numLocal = theNumLocal;
204  indLocal = theParameters;
205  derLocal = theDerivatives;
206  } else {
207  numLocal = moreParameters.size();
208  indLocal = &moreParameters[0];
209  derLocal = &moreDerivatives[0];
210  }
211 }
212 
214 
224 void GblData::getAllData(double &aValue, double &aErr, unsigned int &numLocal,
225  unsigned int* &indLocal, double* &derLocal, unsigned int &aTraj,
226  unsigned int &aPoint, unsigned int &aRow) {
227  aValue = theValue;
228  aErr = 1.0 / sqrt(thePrecision);
229  if (theNumLocal > 0) {
230  numLocal = theNumLocal;
231  indLocal = theParameters;
232  derLocal = theDerivatives;
233  } else {
234  numLocal = moreParameters.size();
235  indLocal = &moreParameters[0];
236  derLocal = &moreDerivatives[0];
237  }
238  aTraj = theTrajectory;
239  aPoint = thePoint;
240  aRow = theRow;
241 }
242 
244 
252 void GblData::getResidual(double &aResidual, double &aVariance,
253  double &aDownWeight, unsigned int &numLocal, unsigned int* &indLocal,
254  double* &derLocal) {
255  aResidual = theValue - thePrediction;
256  aVariance = 1.0 / thePrecision;
257  aDownWeight = theDownWeight;
258  if (theNumLocal > 0) {
259  numLocal = theNumLocal;
260  indLocal = theParameters;
261  derLocal = theDerivatives;
262  } else {
263  numLocal = moreParameters.size();
264  indLocal = &moreParameters[0];
265  derLocal = &moreDerivatives[0];
266  }
267 }
268 }
double thePrecision
Precision (1/sigma**2)
Definition: GblData.h:128
std::vector< unsigned int > moreParameters
List of fit parameters (with non zero derivatives)
Definition: GblData.h:139
double theDownWeight
Down-weighting factor (0-1)
Definition: GblData.h:132
double theDerivatives[7]
List of derivatives for fit.
Definition: GblData.h:137
double thePrediction
Prediction from fit.
Definition: GblData.h:133
unsigned int theTrajectory
Trajectory number.
Definition: GblData.h:129
unsigned int theNumLocal
Number of (non zero) local derivatives (max 7 for kinks)
Definition: GblData.h:135
unsigned int theLabel
Label (of corresponding point)
Definition: GblData.h:124
dataBlockType theType
Type (None, InternalMeasurement, InternalKink, ExternalSeed, ExternalMeasurement)
Definition: GblData.h:126
unsigned int theDWMethod
Down-weighting method (0: None, 1: Tukey, 2: Huber, 3: Cauchy)
Definition: GblData.h:131
virtual ~GblData()
Definition: GblData.cpp:53
void addDerivatives(unsigned int iRow, const std::array< unsigned int, 5 > &labDer, const Matrix5d &matDer, unsigned int iOff, const Eigen::MatrixBase< LocalDerivative > &derLocal, unsigned int extOff, const Eigen::MatrixBase< ExtDerivative > &extDer)
Add derivatives from measurement.
Definition: GblData.h:144
dataBlockType getType() const
Get type.
Definition: GblData.cpp:185
unsigned int theParameters[7]
List of parameters (with non zero derivatives)
Definition: GblData.h:136
unsigned int getLabel() const
Get label.
Definition: GblData.cpp:177
unsigned int theRow
Row number (of measurement)
Definition: GblData.h:125
double getChi2() const
Calculate Chi2 contribution.
Definition: GblData.cpp:125
void printData() const
Print data block.
Definition: GblData.cpp:150
void getResidual(double &aResidual, double &aVariance, double &aDownWeight, unsigned int &numLocal, unsigned int *&indLocal, double *&derLocal)
Get data for residual (and errors).
Definition: GblData.cpp:252
unsigned int thePoint
Point number (on trajectory)
Definition: GblData.h:130
void getLocalData(double &aValue, double &aWeight, unsigned int &numLocal, unsigned int *&indLocal, double *&derLocal)
Get Data for local fit.
Definition: GblData.cpp:197
void getAllData(double &aValue, double &aErr, unsigned int &numLocal, unsigned int *&indLocal, double *&derLocal, unsigned int &aTraj, unsigned int &aPoint, unsigned int &aRow)
Get all Data for MP-II binary record.
Definition: GblData.cpp:224
double theValue
Value (residual)
Definition: GblData.h:127
double setDownWeighting(unsigned int aMethod)
Outlier down weighting with M-estimators (by GblTrajectory::fit).
Definition: GblData.cpp:93
std::vector< double > moreDerivatives
List of derivatives for fit.
Definition: GblData.h:140
void setPrediction(const VVector &aVector)
Calculate prediction for data from fit (by GblTrajectory::fit).
Definition: GblData.cpp:74
Simple Vector based on std::vector<double>
Definition: VMatrix.h:43
Namespace for the general broken lines package.
dataBlockType
Definition: GblData.h:49