Belle II Software  release-08-01-10
GblData.cc
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 
33 namespace gbl {
34 
36 
41 GblData::GblData(unsigned int aLabel, double aValue, double aPrec) :
42  theLabel(aLabel), theValue(aValue), thePrecision(aPrec), theDownWeight(
43  1.), thePrediction(0.), theParameters(), theDerivatives(), globalLabels(), globalDerivatives() {
44 
45 }
46 
47 GblData::~GblData() {
48 }
49 
51 
63 void GblData::addDerivatives(unsigned int iRow,
64  const std::vector<unsigned int> &labDer, const SMatrix55 &matDer,
65  unsigned int iOff, const TMatrixD &derLocal,
66  const std::vector<int> &labGlobal, const TMatrixD &derGlobal,
67  unsigned int extOff, const TMatrixD &extDer) {
68 
69  unsigned int nParMax = 5 + derLocal.GetNcols() + extDer.GetNcols();
70  theParameters.reserve(nParMax); // have to be sorted
71  theDerivatives.reserve(nParMax);
72 
73  for (int i = 0; i < derLocal.GetNcols(); ++i) // local derivatives
74  {
75  if (derLocal(iRow - iOff, i)) {
76  theParameters.push_back(i + 1);
77  theDerivatives.push_back(derLocal(iRow - iOff, i));
78  }
79  }
80 
81  for (int i = 0; i < extDer.GetNcols(); ++i) // external derivatives
82  {
83  if (extDer(iRow - iOff, i)) {
84  theParameters.push_back(extOff + i + 1);
85  theDerivatives.push_back(extDer(iRow - iOff, i));
86  }
87  }
88 
89  for (unsigned int i = 0; i < 5; ++i) // curvature, offset derivatives
90  {
91  if (labDer[i] and matDer(iRow, i)) {
92  theParameters.push_back(labDer[i]);
93  theDerivatives.push_back(matDer(iRow, i));
94  }
95  }
96 
97  globalLabels = labGlobal;
98  for (int i = 0; i < derGlobal.GetNcols(); ++i) // global derivatives
99  globalDerivatives.push_back(derGlobal(iRow - iOff, i));
100 }
101 
103 
111 void GblData::addDerivatives(unsigned int iRow,
112  const std::vector<unsigned int> &labDer, const SMatrix27 &matDer,
113  unsigned int extOff, const TMatrixD &extDer) {
114 
115  unsigned int nParMax = 7 + extDer.GetNcols();
116  theParameters.reserve(nParMax); // have to be sorted
117  theDerivatives.reserve(nParMax);
118 
119  for (int i = 0; i < extDer.GetNcols(); ++i) // external derivatives
120  {
121  if (extDer(iRow, i)) {
122  theParameters.push_back(extOff + i + 1);
123  theDerivatives.push_back(extDer(iRow, i));
124  }
125  }
126 
127  for (unsigned int i = 0; i < 7; ++i) // curvature, offset derivatives
128  {
129  if (labDer[i] and matDer(iRow, i)) {
130  theParameters.push_back(labDer[i]);
131  theDerivatives.push_back(matDer(iRow, i));
132  }
133  }
134 }
135 
137 
142 void GblData::addDerivatives(const std::vector<unsigned int> &index,
143  const std::vector<double> &derivatives) {
144  for (unsigned int i = 0; i < derivatives.size(); ++i) // any derivatives
145  {
146  if (derivatives[i]) {
147  theParameters.push_back(index[i]);
148  theDerivatives.push_back(derivatives[i]);
149  }
150  }
151 }
152 
154 void GblData::setPrediction(const VVector &aVector) {
155 
156  thePrediction = 0.;
157  for (unsigned int i = 0; i < theDerivatives.size(); ++i) {
158  thePrediction += theDerivatives[i] * aVector(theParameters[i] - 1);
159  }
160 }
161 
163 
166 double GblData::setDownWeighting(unsigned int aMethod) {
167 
168  double aWeight = 1.;
169  double scaledResidual = fabs(theValue - thePrediction) * sqrt(thePrecision);
170  if (aMethod == 1) // Tukey
171  {
172  if (scaledResidual < 4.6851) {
173  aWeight = (1.0 - 0.045558 * scaledResidual * scaledResidual);
174  aWeight *= aWeight;
175  } else {
176  aWeight = 0.;
177  }
178  } else if (aMethod == 2) //Huber
179  {
180  if (scaledResidual >= 1.345) {
181  aWeight = 1.345 / scaledResidual;
182  }
183  } else if (aMethod == 3) //Cauchy
184  {
185  aWeight = 1.0 / (1.0 + (scaledResidual * scaledResidual / 5.6877));
186  }
187  theDownWeight = aWeight;
188  return aWeight;
189 }
190 
192 
195 double GblData::getChi2() const {
196  double aDiff = theValue - thePrediction;
197  return aDiff * aDiff * thePrecision * theDownWeight;
198 }
199 
201 void GblData::printData() const {
202 
203  std::cout << " measurement at label " << theLabel << ": " << theValue
204  << ", " << thePrecision << std::endl;
205  std::cout << " param " << theParameters.size() << ":";
206  for (unsigned int i = 0; i < theParameters.size(); ++i) {
207  std::cout << " " << theParameters[i];
208  }
209  std::cout << std::endl;
210  std::cout << " deriv " << theDerivatives.size() << ":";
211  for (unsigned int i = 0; i < theDerivatives.size(); ++i) {
212  std::cout << " " << theDerivatives[i];
213  }
214  std::cout << std::endl;
215 }
216 
218 
224 void GblData::getLocalData(double &aValue, double &aWeight,
225  std::vector<unsigned int>* &indLocal, std::vector<double>* &derLocal) {
226 
227  aValue = theValue;
228  aWeight = thePrecision * theDownWeight;
229  indLocal = &theParameters;
230  derLocal = &theDerivatives;
231 }
232 
234 
242 void GblData::getAllData(double &aValue, double &aErr,
243  std::vector<unsigned int>* &indLocal, std::vector<double>* &derLocal,
244  std::vector<int>* &labGlobal, std::vector<double>* &derGlobal) {
245  aValue = theValue;
246  aErr = 1.0 / sqrt(thePrecision);
247  indLocal = &theParameters;
248  derLocal = &theDerivatives;
249  labGlobal = &globalLabels;
250  derGlobal = &globalDerivatives;
251 }
252 
254 
261 void GblData::getResidual(double &aResidual, double &aVariance,
262  double &aDownWeight, std::vector<unsigned int>* &indLocal,
263  std::vector<double>* &derLocal) {
264  aResidual = theValue - thePrediction;
265  aVariance = 1.0 / thePrecision;
266  aDownWeight = theDownWeight;
267  indLocal = &theParameters;
268  derLocal = &theDerivatives;
269 }
270 }
GblData definition.
double theDownWeight
Down-weighting factor (0-1)
Definition: GblData.h:90
double setDownWeighting(unsigned int aMethod)
Outlier down weighting with M-estimators (by GblTrajectory::fit).
Definition: GblData.cc:166
double getChi2() const
Calculate Chi2 contribution.
Definition: GblData.cc:195
unsigned int theLabel
Label (of measurements point)
Definition: GblData.h:87
void getAllData(double &aValue, double &aErr, std::vector< unsigned int > *&indLocal, std::vector< double > *&derLocal, std::vector< int > *&labGlobal, std::vector< double > *&derGlobal)
Get all Data for MP-II binary record.
Definition: GblData.cc:242
std::vector< int > globalLabels
Labels for global derivatives.
Definition: GblData.h:94
double theValue
Value (residual)
Definition: GblData.h:88
std::vector< double > globalDerivatives
Global derivatives.
Definition: GblData.h:95
std::vector< unsigned int > theParameters
List of fit parameters (with non zero derivatives)
Definition: GblData.h:92
void addDerivatives(unsigned int iRow, const std::vector< unsigned int > &labDer, const SMatrix55 &matDer, unsigned int iOff, const TMatrixD &derLocal, const std::vector< int > &labGlobal, const TMatrixD &derGlobal, unsigned int nLocal, const TMatrixD &derTrans)
Add derivatives from measurement.
Definition: GblData.cc:63
void setPrediction(const VVector &aVector)
Calculate prediction for data from fit (by GblTrajectory::fit).
Definition: GblData.cc:154
double thePrecision
Precision (1/sigma**2)
Definition: GblData.h:89
void getResidual(double &aResidual, double &aVariance, double &aDownWeight, std::vector< unsigned int > *&indLocal, std::vector< double > *&derLocal)
Get data for residual (and errors).
Definition: GblData.cc:261
std::vector< double > theDerivatives
List of derivatives for fit.
Definition: GblData.h:93
void printData() const
Print data block.
Definition: GblData.cc:201
void getLocalData(double &aValue, double &aWeight, std::vector< unsigned int > *&indLocal, std::vector< double > *&derLocal)
Get Data for local fit.
Definition: GblData.cc:224
double thePrediction
Prediction from fit.
Definition: GblData.h:91
Simple Vector based on std::vector<double>
Definition: VMatrix.h:43
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
Namespace for the general broken lines package.