test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CSCCondSegFit.cc
Go to the documentation of this file.
1 // CSCCondSegFit.cc
2 // Last mod: 23.01.2015
3 
5 
9 
10 #include <algorithm>
11 
12 void CSCCondSegFit::fit( bool condpass1, bool condpass2 ) {
13 
14  // See base class CSCSegFit::fit for detailed explanation of algorithm.
15  // This is exactly the same, except it adds two conditioning passes
16  // which can improve the robustness of the calculations.
17 
18  lex_.clear(); // Vector of the error matrix (only xx)
19 
20  if(condpass1 && !condpass2){
22  if(lex_.size()!=hits_.size()){
23  LogTrace("CSCSegment|CSC") << "[CSCSegFit::fit] lex_.size()!=hits_.size() ALARM! Please inform CSC DPG " << std::endl;
24  }
25  }
26 
27  SMatrix4 M; // 4x4, init to 0
28  SVector4 B; // 4x1, init to 0;
29 
30  CSCSetOfHits::const_iterator ih = hits_.begin();
31 
32  for (ih = hits_.begin(); ih != hits_.end(); ++ih) {
33  const CSCRecHit2D& hit = (**ih);
34  const CSCLayer* layer = chamber()->layer(hit.cscDetId().layer());
35  GlobalPoint gp = layer->toGlobal(hit.localPosition());
36  LocalPoint lp = chamber()->toLocal(gp);
37 
38  // Local position of hit w.r.t. chamber
39  double u = lp.x();
40  double v = lp.y();
41  double z = lp.z();
42 
43  // Covariance matrix of local errors
44  SMatrixSym2 IC; // 2x2, init to 0
45 
46  // Adjust covariance matrix elements to improve numerical stability?
47  if(condpass1 && !condpass2){
48  IC(0,0) = lex_.at(ih-hits_.begin());
49  }
50  else {
51  IC(0,0) = hit.localPositionError().xx();
52  }
53  IC(1,1) = hit.localPositionError().yy();
54  //@@ NOT SURE WHICH OFF-DIAGONAL ELEMENT MUST BE DEFINED BUT (1,0) WORKS
55  //@@ (and SMatrix enforces symmetry)
56  IC(1,0) = hit.localPositionError().xy();
57  // IC(0,1) = IC(1,0);
58 
59  // Correct the covariance matrix to improve numerical stability?
60  if(condpass2){
62  }
63 
64  // Invert covariance matrix (and trap if it fails!)
65  bool ok = IC.Invert();
66  if ( !ok ) {
67  LogTrace("CSCSegment|CSC") << "[CSCSegFit::fit] Failed to invert covariance matrix: \n" << IC;
68  // return ok; //@@ SHOULD PASS THIS BACK TO CALLER?
69  }
70 
71  M(0,0) += IC(0,0);
72  M(0,1) += IC(0,1);
73  M(0,2) += IC(0,0) * z;
74  M(0,3) += IC(0,1) * z;
75  B(0) += u * IC(0,0) + v * IC(0,1);
76 
77  M(1,0) += IC(1,0);
78  M(1,1) += IC(1,1);
79  M(1,2) += IC(1,0) * z;
80  M(1,3) += IC(1,1) * z;
81  B(1) += u * IC(1,0) + v * IC(1,1);
82 
83  M(2,0) += IC(0,0) * z;
84  M(2,1) += IC(0,1) * z;
85  M(2,2) += IC(0,0) * z * z;
86  M(2,3) += IC(0,1) * z * z;
87  B(2) += ( u * IC(0,0) + v * IC(0,1) ) * z;
88 
89  M(3,0) += IC(1,0) * z;
90  M(3,1) += IC(1,1) * z;
91  M(3,2) += IC(1,0) * z * z;
92  M(3,3) += IC(1,1) * z * z;
93  B(3) += ( u * IC(1,0) + v * IC(1,1) ) * z;
94 
95  }
96 
97  SVector4 p;
98  bool ok = M.Invert();
99  if (!ok ){
100  LogTrace("CSCSegment|CSC") << "[CSCSegFit::fit] Failed to invert matrix: \n" << M;
101  // return ok; //@@ SHOULD PASS THIS BACK TO CALLER?
102  }
103  else {
104  p = M * B;
105  }
106 
107  // LogTrace("CSCSegFit") << "[CSCSegFit::fit] p = "
108  // << p(0) << ", " << p(1) << ", " << p(2) << ", " << p(3);
109 
110  // fill member variables (note origin has local z = 0)
111  intercept_ = LocalPoint(p(0), p(1), 0.);
112 
113  // localdir_
114  uslope_ = p(2);
115  vslope_ = p(3);
116  setOutFromIP();
117 
118  // calculate chi2 of fit
119  setChi2( condpass1, condpass2 );
120 
121 }
122 
123 
124 
125 void CSCCondSegFit::setChi2( bool condpass1, bool condpass2 ) {
126 
127  double chsq = 0.;
128 
129  CSCSetOfHits::const_iterator ih;
130  for (ih = hits_.begin(); ih != hits_.end(); ++ih) {
131 
132  const CSCRecHit2D& hit = (**ih);
133  const CSCLayer* layer = chamber()->layer(hit.cscDetId().layer());
134  GlobalPoint gp = layer->toGlobal(hit.localPosition());
135  LocalPoint lp = chamber()->toLocal(gp);
136 
137  double u = lp.x();
138  double v = lp.y();
139  double z = lp.z();
140 
141  double du = intercept_.x() + uslope_ * z - u;
142  double dv = intercept_.y() + vslope_ * z - v;
143 
144  // LogTrace("CSCSegFit") << "[CSCSegFit::setChi2] u, v, z = " << u << ", " << v << ", " << z;
145 
146  SMatrixSym2 IC; // 2x2, init to 0
147 
148  if(condpass1 && !condpass2){
149  IC(0,0) = lex_.at(ih-hits_.begin());
150  }
151  else{
152  IC(0,0) = hit.localPositionError().xx();
153  }
154  // IC(0,1) = hit.localPositionError().xy();
155  IC(1,0) = hit.localPositionError().xy();
156  IC(1,1) = hit.localPositionError().yy();
157  // IC(1,0) = IC(0,1);
158 
159  // LogTrace("CSCSegFit") << "[CSCSegFit::setChi2] IC before = \n" << IC;
160 
162  if(condpass2){
164  }
165 
166  // Invert covariance matrix
167  bool ok = IC.Invert();
168  if (!ok ){
169  LogTrace("CSCSegment|CSC") << "[CSCSegFit::setChi2] Failed to invert covariance matrix: \n" << IC;
170  // return ok;
171  }
172  // LogTrace("CSCSegFit") << "[CSCSegFit::setChi2] IC after = \n" << IC;
173  chsq += du*du*IC(0,0) + 2.*du*dv*IC(0,1) + dv*dv*IC(1,1);
174  }
175 
176  // fill member variables
177  chi2_ = chsq;
178  ndof_ = 2.*hits_.size() - 4;
179 
180  // LogTrace("CSCSegFit") << "[CSCSegFit::setChi2] chi2 = " << chi2_ << "/" << ndof_ << " dof";
181 
182 }
183 
184 
185 
187  std::vector<double> uu, vv, zz; // Vectors of coordinates
188 
189  lex_.clear(); // x component of local error for each hit
190 
191  double sum_U_err=0.0;
192  double sum_Z_U_err=0.0;
193  double sum_Z2_U_err=0.0;
194  double sum_U_U_err=0.0;
195  double sum_UZ_U_err=0.0;
196  std::vector<double> chiUZind;
197  std::vector<double>::iterator chiContribution;
198  double chiUZ=0.0;
199  CSCSetOfHits::const_iterator ih = hits_.begin();
200  for (ih = hits_.begin(); ih != hits_.end(); ++ih) {
201  const CSCRecHit2D& hit = (**ih);
202  lex_.push_back(hit.localPositionError().xx());
203 
204  const CSCLayer* layer = chamber()->layer(hit.cscDetId().layer());
205  GlobalPoint gp = layer->toGlobal(hit.localPosition());
206  LocalPoint lp = chamber()->toLocal(gp);
207  // Local position of hit w.r.t. chamber
208  double u = lp.x();
209  double v = lp.y();
210  double z = lp.z();
211  uu.push_back(u);
212  vv.push_back(v);
213  zz.push_back(z);
214  // Prepare the sums for the standard linear fit
215  sum_U_err += 1./lex_.back();
216  sum_Z_U_err += z/lex_.back();
217  sum_Z2_U_err += (z*z)/lex_.back();
218  sum_U_U_err += u/lex_.back();
219  sum_UZ_U_err += (u*z)/lex_.back();
220  }
221 
224 
225  double denom=sum_U_err*sum_Z2_U_err-pow(sum_Z_U_err,2);
226  double U0=(sum_Z2_U_err*sum_U_U_err-sum_Z_U_err*sum_UZ_U_err)/denom;
227  double UZ=(sum_U_err*sum_UZ_U_err-sum_Z_U_err*sum_U_U_err)/denom;
228 
231 
232  for(unsigned i=0; i<uu.size(); ++i){
233  double uMean = U0+UZ*zz[i];
234  chiUZind.push_back((pow((uMean-uu[i]),2))/lex_[i]);
235  chiUZ += (pow((uMean-uu[i]),2))/lex_[i];
236  }
237  chiUZ = chiUZ/(uu.size()-2);
238 
239  if(chiUZ>=chi2Norm_){
240  double chi2uCorrection = chiUZ/chi2Norm_;
241  for(unsigned i=0; i<uu.size(); ++i)
242  lex_[i]=lex_[i]*chi2uCorrection;
243  setScaleXError(chi2uCorrection);
244  }
245 
246  // Find most deviant hit
247  chiContribution=max_element(chiUZind.begin(),chiUZind.end());
248  worstHit_ = chiContribution - chiUZind.begin();
249 
250 }
251 
253  //double condNumberCorr1=0.0;
254  double condNumberCorr2=0.0;
255  double detCov=0.0;
256  double diag1=0.0;
257  double diag2=0.0;
258  double IC_01_corr=0.0;
259  double IC_00_corr=0.0;
260  if(!covToAnyNumberAll_){
261  //condNumberCorr1=condSeed1_*IC(1,1);
262  condNumberCorr2=condSeed2_*IC(1,1);
263  diag1=IC(0,0)*IC(1,1);
264  diag2=IC(0,1)*IC(0,1);
265  detCov=fabs(diag1-diag2);
266  if((diag1<condNumberCorr2)&&(diag2<condNumberCorr2)){
267  if(covToAnyNumber_)
268  IC(0,1)=covAnyNumber_;
269  else {
270  IC_00_corr=condSeed1_+fabs(IC(0,1))/IC(1,1);
271  IC(0,0)=IC_00_corr;
272  }
273  }
274 
275  if(((detCov<condNumberCorr2)&&(diag1>condNumberCorr2))||
276  ((diag2>condNumberCorr2)&&(detCov<condNumberCorr2) )){
277  if(covToAnyNumber_)
278  IC(0,1)=covAnyNumber_;
279  else {
280  IC_01_corr=sqrt(fabs(diag1-condNumberCorr2));
281  if(IC(0,1)<0)
282  IC(0,1)=(-1)*IC_01_corr;
283  else
284  IC(0,1)=IC_01_corr;
285  }
286  }
287  }
288  else{
289  IC(0,1)=covAnyNumber_;
290  }
291  // With SMatrix formulation. the following might be unnecessary since it might
292  // enforce the symmetry. But can't hurt to leave it (it WAS a real bug fix for
293  // the original CLHEP formulation.)
294  IC(1,0) = IC(0,1); //@@ ADDED TO MAINTAIN SYMMETRY OF IC
295 }
LocalPoint intercept_
Definition: CSCSegFit.h:118
int i
Definition: DBlmapReader.cc:9
void fit(void)
Definition: CSCSegFit.cc:14
void correctTheCovX(void)
float xx() const
Definition: LocalError.h:24
CSCDetId cscDetId() const
Definition: CSCRecHit2D.h:52
void correctTheCovMatrix(CSCSegFit::SMatrixSym2 &IC)
double_binary B
Definition: DDStreamer.cc:234
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:52
double chi2_
Definition: CSCSegFit.h:120
T y() const
Definition: PV3DBase.h:63
float vslope_
Definition: CSCSegFit.h:117
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:67
std::vector< double > lex_
Definition: CSCCondSegFit.h:58
int layer() const
Definition: CSCDetId.h:74
bool covToAnyNumber_
The correction parameters.
Definition: CSCCondSegFit.h:65
float xy() const
Definition: LocalError.h:25
LocalError localPositionError() const
Definition: CSCRecHit2D.h:51
double covAnyNumber_
Allow to use any number for covariance for all RecHits.
Definition: CSCCondSegFit.h:67
float yy() const
Definition: LocalError.h:26
bool covToAnyNumberAll_
Allow to use any number for covariance (by hand)
Definition: CSCCondSegFit.h:66
T sqrt(T t)
Definition: SSEVec.h:48
double chi2Norm_
Definition: CSCCondSegFit.h:59
T z() const
Definition: PV3DBase.h:64
ROOT::Math::SVector< double, 4 > SVector4
Definition: CSCSegFit.h:52
const CSCLayer * layer(CSCDetId id) const
Return the layer corresponding to the given id.
Definition: CSCChamber.cc:39
double condSeed2_
Definition: CSCCondSegFit.h:64
#define LogTrace(id)
CSCSetOfHits hits_
Definition: CSCSegFit.h:115
ROOT::Math::SMatrix< double, 4 > SMatrix4
Definition: CSCSegFit.h:45
void setOutFromIP(void)
Definition: CSCSegFit.cc:355
void setScaleXError(double factor)
Definition: CSCSegFit.h:70
const CSCChamber * chamber() const
Definition: CSCSegFit.h:89
double condSeed1_
Definition: CSCCondSegFit.h:64
Local3DPoint LocalPoint
Definition: LocalPoint.h:11
ROOT::Math::SMatrix< double, 2, 2, ROOT::Math::MatRepSym< double, 2 > > SMatrixSym2
Definition: CSCSegFit.h:49
LocalPoint localPosition() const
Definition: CSCRecHit2D.h:50
T x() const
Definition: PV3DBase.h:62
float uslope_
Definition: CSCSegFit.h:116
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
int ndof_
Definition: CSCSegFit.h:121
void setChi2(void)
Definition: CSCSegFit.cc:246