CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SurveyPxbImageLocalFit.cc
Go to the documentation of this file.
3 
4 #include <stdexcept>
5 #include <utility>
6 #include <sstream>
7 #include <vector>
8 #include <cmath>
10 #include "Math/SMatrix.h"
11 #include "Math/SVector.h"
12 
13 #include <iostream>
14 
15 void SurveyPxbImageLocalFit::doFit(const fidpoint_t &fidpointvec, const pede_label_t &label1, const pede_label_t &label2)
16 {
17  labelVec1_.clear();
18  labelVec1_.push_back(label1+0);
19  labelVec1_.push_back(label1+1);
20  labelVec1_.push_back(label1+5);
21  labelVec2_.clear();
22  labelVec2_.push_back(label2+0);
23  labelVec2_.push_back(label2+1);
24  labelVec2_.push_back(label2+5);
25  doFit(fidpointvec);
26 }
27 
28 void SurveyPxbImageLocalFit::doFit(const fidpoint_t &fidpointvec)
29 {
30  fitValidFlag_ = false;
31 
32  // Calculate gamma of right module w.r.t left modules' fram
33  const value_t dxr = fidpointvec[3].x()-fidpointvec[2].x();
34  const value_t dyr = fidpointvec[3].y()-fidpointvec[2].y();
35  const value_t gammar = atan(dyr/dxr);
36  const value_t dxl = fidpointvec[1].x()-fidpointvec[0].x();
37  const value_t dyl = fidpointvec[1].y()-fidpointvec[0].y();
38  const value_t gammal = atan(dyl/dxl);
39  const value_t gamma = gammar-gammal;
40  //const value_t gamma = 0.; // Testhalber
41  const value_t sing = sin(gamma);
42  const value_t cosg = cos(gamma);
43 #ifdef DEBUG
44  std::cout << "gamma: " << gamma << " gamma left: " << gammal << " gamma right: " << gammar << std::endl;
45 #endif
46 
47 
48 #ifdef DEBUG
49  std::cout << "&fidpointvec: " << std::endl;
50  for (count_t i=0; i!=fidpointvec.size(); i++)
51  std::cout << i << ": " << fidpointvec[i] << std::endl;
52  std::cout << "&fidpoints_: " << std::endl;
53  for (count_t i=0; i!=fidpoints_.size(); i++)
54  std::cout << i << ": " << fidpoints_[i] << std::endl;
55 #endif
56 
57  // Matrix of the local derivatives
58  ROOT::Math::SMatrix<value_t,nMsrmts,nLcD> A; // 8x4
59  A(0,0)=1.; A(0,1)=0; A(0,2)=+fidpointvec[0].x(); A(0,3)=+fidpointvec[0].y();
60  A(1,0)=0.; A(1,1)=1; A(1,2)=+fidpointvec[0].y(); A(1,3)=-fidpointvec[0].x();
61  A(2,0)=1.; A(2,1)=0; A(2,2)=+fidpointvec[1].x(); A(2,3)=+fidpointvec[1].y();
62  A(3,0)=0.; A(3,1)=1; A(3,2)=+fidpointvec[1].y(); A(3,3)=-fidpointvec[1].x();
63  A(4,0)=1.; A(4,1)=0; A(4,2)=+fidpointvec[2].x(); A(4,3)=+fidpointvec[2].y();
64  A(5,0)=0.; A(5,1)=1; A(5,2)=+fidpointvec[2].y(); A(5,3)=-fidpointvec[2].x();
65  A(6,0)=1.; A(6,1)=0; A(6,2)=+fidpointvec[3].x(); A(6,3)=+fidpointvec[3].y();
66  A(7,0)=0.; A(7,1)=1; A(7,2)=+fidpointvec[3].y(); A(7,3)=-fidpointvec[3].x();
67 #ifdef DEBUG
68  std::cout << "A: \n" << A << std::endl;
69 #endif
70 
71  // Covariance matrix
72  ROOT::Math::SMatrix<value_t,nMsrmts,nMsrmts> W; // 8x8
73  //ROOT::Math::MatRepSym<value_t,8> W;
74  const value_t sigma_x2inv = 1./(sigma_x_*sigma_x_);
75  const value_t sigma_y2inv = 1./(sigma_y_*sigma_y_);
76  W(0,0) = sigma_x2inv;
77  W(1,1) = sigma_y2inv;
78  W(2,2) = sigma_x2inv;
79  W(3,3) = sigma_y2inv;
80  W(4,4) = sigma_x2inv;
81  W(5,5) = sigma_y2inv;
82  W(6,6) = sigma_x2inv;
83  W(7,7) = sigma_y2inv;
84 #ifdef DEBUG
85  std::cout << "W: \n" << W << std::endl;
86 #endif
87 
88  // Prepare for the fit
89  ROOT::Math::SMatrix<value_t,nLcD,nLcD> ATWA; // 4x4
90  ATWA = ROOT::Math::Transpose(A) * W * A;
91  //ATWA = ROOT::Math::SimilarityT(A,W); // W muss symmterisch sein -> aendern.
92  //std::cout << "ATWA: \n" << ATWA << std::endl;
93  ROOT::Math::SMatrix<value_t,nLcD,nLcD> ATWAi; // 4x4
94  int ifail = 0;
95  ATWAi = ATWA.Inverse(ifail);
96  if (ifail != 0)
97  { // TODO: ifail Pruefung auf message logger ausgeben statt cout
98  std::cout << "Problem singular - fit impossible." << std::endl;
99  fitValidFlag_ = false;
100  return;
101  }
102 #ifdef DEBUG
103  std::cout << "ATWA-1: \n" << ATWAi << ifail << std::endl;
104 #endif
105 
106  // Measurements
107  ROOT::Math::SVector<value_t,nMsrmts> y; // 8
108  y(0) = measurementVec_[0].x();
109  y(1) = measurementVec_[0].y();
110  y(2) = measurementVec_[1].x();
111  y(3) = measurementVec_[1].y();
112  y(4) = measurementVec_[2].x();
113  y(5) = measurementVec_[2].y();
114  y(6) = measurementVec_[3].x();
115  y(7) = measurementVec_[3].y();
116 #ifdef DEBUG
117  std::cout << "y: " << y << std::endl;
118 #endif
119 
120  // do the fit
121  ROOT::Math::SVector<value_t,nLcD> a; // 4
122  a = ATWAi * ROOT::Math::Transpose(A) * W * y;
123  chi2_ = ROOT::Math::Dot(y,W*y)-ROOT::Math::Dot(a,ROOT::Math::Transpose(A)*W*y);
124 #ifdef DEBUG
125  std::cout << "a: " << a
126  << " S= " << sqrt(a[2]*a[2]+a[3]*a[3])
127  << " phi= " << atan(a[3]/a[2])
128  << " chi2= " << chi2_ << std::endl;
129  std::cout << "A*a: " << A*a << std::endl;
130 #endif
131  a_.assign(a.begin(),a.end());
132 
133  // Calculate vector of residuals
134  r = y - A*a;
135 #ifdef DEBUG
136  std::cout << "r: " << r << std::endl;
137 #endif
138 
139  // Fill matrix for global fit with local derivatives
140  localDerivsMatrix_(0,0)=1.; localDerivsMatrix_(0,1)=0;
141  localDerivsMatrix_(1,0)=0.; localDerivsMatrix_(1,1)=1;
142  localDerivsMatrix_(2,0)=1.; localDerivsMatrix_(2,1)=0;
143  localDerivsMatrix_(3,0)=0.; localDerivsMatrix_(3,1)=1;
144  localDerivsMatrix_(4,0)=1.; localDerivsMatrix_(4,1)=0;
145  localDerivsMatrix_(5,0)=0.; localDerivsMatrix_(5,1)=1;
146  localDerivsMatrix_(6,0)=1.; localDerivsMatrix_(6,1)=0;
147  localDerivsMatrix_(7,0)=0.; localDerivsMatrix_(7,1)=1;
148  localDerivsMatrix_(0,2)=+fidpointvec[0].x()+cosg*fidpoints_[0].x()-sing*fidpoints_[0].y();
149  localDerivsMatrix_(0,3)=+fidpointvec[0].y()+cosg*fidpoints_[0].y()+sing*fidpoints_[0].x();
150  localDerivsMatrix_(1,2)=+fidpointvec[0].y()+cosg*fidpoints_[0].y()+sing*fidpoints_[0].x();
151  localDerivsMatrix_(1,3)=-fidpointvec[0].x()-cosg*fidpoints_[0].x()+sing*fidpoints_[0].y();
152  localDerivsMatrix_(2,2)=+fidpointvec[1].x()+cosg*fidpoints_[1].x()-sing*fidpoints_[1].y();
153  localDerivsMatrix_(2,3)=+fidpointvec[1].y()+cosg*fidpoints_[1].y()+sing*fidpoints_[1].x();
154  localDerivsMatrix_(3,2)=+fidpointvec[1].y()+cosg*fidpoints_[1].y()+sing*fidpoints_[1].x();
155  localDerivsMatrix_(3,3)=-fidpointvec[1].x()-cosg*fidpoints_[1].x()+sing*fidpoints_[1].y();
156  localDerivsMatrix_(4,2)=+fidpointvec[2].x()+cosg*fidpoints_[2].x()-sing*fidpoints_[2].y();
157  localDerivsMatrix_(4,3)=+fidpointvec[2].y()+cosg*fidpoints_[2].y()+sing*fidpoints_[2].x();
158  localDerivsMatrix_(5,2)=+fidpointvec[2].y()+cosg*fidpoints_[2].y()+sing*fidpoints_[2].x();
159  localDerivsMatrix_(5,3)=-fidpointvec[2].x()-cosg*fidpoints_[2].x()+sing*fidpoints_[2].y();
160  localDerivsMatrix_(6,2)=+fidpointvec[3].x()+cosg*fidpoints_[3].x()-sing*fidpoints_[3].y();
161  localDerivsMatrix_(6,3)=+fidpointvec[3].y()+cosg*fidpoints_[3].y()+sing*fidpoints_[3].x();
162  localDerivsMatrix_(7,2)=+fidpointvec[3].y()+cosg*fidpoints_[3].y()+sing*fidpoints_[3].x();
163  localDerivsMatrix_(7,3)=-fidpointvec[3].x()-cosg*fidpoints_[3].x()+sing*fidpoints_[3].y();
164 
165  // Fill vector with global derivatives and labels (8x3)
166  globalDerivsMatrix_(0,0) = +a(2);
167  globalDerivsMatrix_(0,1) = +a(3);
168  globalDerivsMatrix_(0,2) = +cosg*(a(3)*fidpoints_[0].x()-a(2)*fidpoints_[0].y())
169  -sing*(a(2)*fidpoints_[0].x()+a(3)*fidpoints_[0].y());
170  globalDerivsMatrix_(1,0) = -a(3);
171  globalDerivsMatrix_(1,1) = +a(2);
172  globalDerivsMatrix_(1,2) = +cosg*(a(2)*fidpoints_[0].x()+a(3)*fidpoints_[0].y())
173  -sing*(a(2)*fidpoints_[0].y()-a(3)*fidpoints_[0].x());
174  globalDerivsMatrix_(2,0) = +a(2);
175  globalDerivsMatrix_(2,1) = +a(3);
176  globalDerivsMatrix_(2,2) = +cosg*(a(3)*fidpoints_[1].x()-a(2)*fidpoints_[1].y())
177  -sing*(a(2)*fidpoints_[1].x()+a(3)*fidpoints_[1].y());
178  globalDerivsMatrix_(3,0) = -a(3);
179  globalDerivsMatrix_(3,1) = +a(2);
180  globalDerivsMatrix_(3,2) = +cosg*(a(2)*fidpoints_[1].x()+a(3)*fidpoints_[1].y())
181  -sing*(a(2)*fidpoints_[1].y()-a(3)*fidpoints_[1].x());
182 
183  globalDerivsMatrix_(4,0) = +a(2);
184  globalDerivsMatrix_(4,1) = +a(3);
185  globalDerivsMatrix_(4,2) = +cosg*(a(3)*fidpoints_[2].x()-a(2)*fidpoints_[2].y())
186  -sing*(a(2)*fidpoints_[2].x()+a(3)*fidpoints_[2].y());
187  globalDerivsMatrix_(5,0) = -a(3);
188  globalDerivsMatrix_(5,1) = +a(2);
189  globalDerivsMatrix_(5,2) = +cosg*(a(2)*fidpoints_[2].x()+a(3)*fidpoints_[2].y())
190  -sing*(a(2)*fidpoints_[2].y()-a(3)*fidpoints_[2].x());
191  globalDerivsMatrix_(6,0) = +a(2);
192  globalDerivsMatrix_(6,1) = +a(3);
193  globalDerivsMatrix_(6,2) = +cosg*(a(3)*fidpoints_[3].x()-a(2)*fidpoints_[3].y())
194  -sing*(a(2)*fidpoints_[3].x()+a(3)*fidpoints_[3].y());
195  globalDerivsMatrix_(7,0) = -a(3);
196  globalDerivsMatrix_(7,1) = +a(2);
197  globalDerivsMatrix_(7,2) = +cosg*(a(2)*fidpoints_[3].x()+a(3)*fidpoints_[3].y())
198  -sing*(a(2)*fidpoints_[3].y()-a(3)*fidpoints_[3].x());
199 
200  fitValidFlag_ = true;
201 }
202 
203 
205 {
206  // Creating vectors with the global parameters of the two modules
207  ROOT::Math::SVector<value_t,4> mod1, mod2;
208  mod1(0)=u1;
209  mod1(1)=v1;
210  mod1(2)=cos(g1);
211  mod1(3)=sin(g1);
212  mod2(0)=u2;
213  mod2(1)=v2;
214  mod2(2)=cos(g2);
215  mod2(3)=sin(g2);
216  //std::cout << "mod1: " << mod1 << std::endl;
217  //std::cout << "mod2: " << mod2 << std::endl;
218 
219  // Create a matrix for the transformed position of the fidpoints
220  ROOT::Math::SMatrix<value_t,4,4> M1, M2;
221  M1(0,0)=1.; M1(0,1)=0.; M1(0,2)=+fidpoints_[0].x(); M1(0,3)=-fidpoints_[0].y();
222  M1(1,0)=0.; M1(1,1)=1.; M1(1,2)=+fidpoints_[0].y(); M1(1,3)=+fidpoints_[0].x();
223  M1(2,0)=1.; M1(2,1)=0.; M1(2,2)=+fidpoints_[1].x(); M1(2,3)=-fidpoints_[1].y();
224  M1(3,0)=0.; M1(3,1)=1.; M1(3,2)=+fidpoints_[1].y(); M1(3,3)=+fidpoints_[1].x();
225  M2(0,0)=1.; M2(0,1)=0.; M2(0,2)=+fidpoints_[2].x(); M2(0,3)=-fidpoints_[2].y();
226  M2(1,0)=0.; M2(1,1)=1.; M2(1,2)=+fidpoints_[2].y(); M2(1,3)=+fidpoints_[2].x();
227  M2(2,0)=1.; M2(2,1)=0.; M2(2,2)=+fidpoints_[3].x(); M2(2,3)=-fidpoints_[3].y();
228  M2(3,0)=0.; M2(3,1)=1.; M2(3,2)=+fidpoints_[3].y(); M2(3,3)=+fidpoints_[3].x();
229 
230  //std::cout << "M1:\n" << M1 << std::endl;
231  //std::cout << "M2:\n" << M2 << std::endl;
232 
233  ROOT::Math::SVector<value_t,4> mod_tr1, mod_tr2;
234  mod_tr1 = M1*mod2;
235  mod_tr2 = M2*mod1;
236  //std::cout << "mod_tr1: " << mod_tr1 << std::endl;
237  //std::cout << "mod_tr2: " << mod_tr2 << std::endl;
238 
239  fidpoint_t fidpointvec;
240  fidpointvec.push_back(coord_t(mod_tr1(0),mod_tr1(1)));
241  fidpointvec.push_back(coord_t(mod_tr1(2),mod_tr1(3)));
242  fidpointvec.push_back(coord_t(mod_tr2(0),mod_tr2(1)));
243  fidpointvec.push_back(coord_t(mod_tr2(2),mod_tr2(3)));
244 
245  doFit(fidpointvec);
246 }
247 
249 {
250  if (!fitValidFlag_) throw std::logic_error("SurveyPxbImageLocalFit::getLocalParameters(): Fit is not valid. Call doFit(...) before calling this function.");
251  return a_;
252 }
253 
255 {
256  if (!fitValidFlag_) throw std::logic_error("SurveyPxbImageLocalFit::getChi2(): Fit is not valid. Call doFit(...) before calling this function.");
257  return chi2_;
258 }
259 
261 {
262  if (!(j < nLcD)) throw std::range_error("SurveyPxbImageLocalFit::setLocalDerivsToZero(j): j out of range.");
263  for(count_t i=0; i!=nMsrmts; i++)
264  localDerivsMatrix_(i,j)=0;
265 }
266 
268 {
269  if (!(j < nGlD)) throw std::range_error("SurveyPxbImageLocalFit::setLocalDerivsToZero(j): j out of range.");
270  for(count_t i=0; i!=nMsrmts; i++)
271  globalDerivsMatrix_(i,j)=0;
272 }
273 
274 
int i
Definition: DBlmapReader.cc:9
std::vector< coord_t > fidpoint_t
localpars_t getLocalParameters()
returns local parameters after fit
bool fitValidFlag_
Validity Flag.
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
std::vector< pede_label_t > labelVec2_
value_t sigma_x_
Gaussian errors.
localpars_t a_
Local parameters.
value_t getChi2()
returns the chi^2 of the fit
ROOT::Math::SVector< value_t, nMsrmts > r
Vector of residuals.
Point3DBase< value_t, LocalTag > coord_t
T sqrt(T t)
Definition: SSEVec.h:18
static const count_t nGlD
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
int j
Definition: DBlmapReader.cc:9
ROOT::Math::SMatrix< pede_deriv_t, nMsrmts, nLcD > localDerivsMatrix_
Matrix with local derivs.
static const count_t nLcD
ROOT::Math::SMatrix< pede_deriv_t, nMsrmts, nGlD > globalDerivsMatrix_
Matrix with global derivs.
std::vector< pede_label_t > labelVec1_
Vector with labels to global derivs.
void doFit(const fidpoint_t &fidpointvec)
Invoke the fit.
value_t chi2_
chi2 of the local fit
std::vector< value_t > localpars_t
std::vector< coord_t > fidpoints_
True position of the fiducial points on a sensor wrt. local frame (u,v)
std::vector< coord_t > measurementVec_
Vector to hold four measurements.
double a
Definition: hdecay.h:121
tuple cout
Definition: gather_cfg.py:145
static const count_t nMsrmts