ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/IPHCalignment2/TrackingTools/PatternTools/src/CollinearFitAtTM.cc
Revision: 1.1
Committed: Fri Nov 25 16:38:25 2011 UTC (13 years, 5 months ago) by econte
Content type: text/plain
Branch: MAIN
CVS Tags: TBD2011, TBD_2011, HEAD
Log Message:
new IPHC alignment

File Contents

# User Rev Content
1 econte 1.1 #include "TrackingTools/PatternTools/interface/CollinearFitAtTM.h"
2     #include "FWCore/MessageLogger/interface/MessageLogger.h"
3    
4     CollinearFitAtTM2::CollinearFitAtTM2 (const TrajectoryMeasurement& tm) :
5     valid_(false),chi2_(-1),ndof_(-1) {
6     //
7     // check input
8     //
9     if ( !tm.forwardPredictedState().isValid() ||
10     !tm.backwardPredictedState().isValid() ||
11     !tm.updatedState().isValid() ) {
12     edm::LogWarning("CollinearFitAtTM2") << "Invalid state in TrajectoryMeasurement";
13     return;
14     }
15     //
16     // prepare fit
17     //
18     initJacobian();
19     AlgebraicVector5 fwdPar = tm.forwardPredictedState().localParameters().vector();
20     AlgebraicSymMatrix55 fwdCov = tm.forwardPredictedState().localError().matrix();
21     AlgebraicVector5 bwdPar = tm.backwardPredictedState().localParameters().vector();
22     AlgebraicSymMatrix55 bwdCov = tm.backwardPredictedState().localError().matrix();
23    
24     LocalPoint hitPos(0.,0.,0.);
25     LocalError hitErr(-1.,-1.,-1.);
26     if ( tm.recHit()->isValid() ) {
27     hitPos = tm.recHit()->localPosition();
28     hitErr = tm.recHit()->localPositionError();
29     }
30    
31     fit(fwdPar,fwdCov,bwdPar,bwdCov,hitPos,hitErr);
32     }
33    
34     CollinearFitAtTM2::CollinearFitAtTM2 (const AlgebraicVector5& fwdParameters,
35     const AlgebraicSymMatrix55& fwdCovariance,
36     const AlgebraicVector5& bwdParameters,
37     const AlgebraicSymMatrix55& bwdCovariance,
38     const LocalPoint& hitPosition,
39     const LocalError& hitErrors) :
40     valid_(false),chi2_(-1),ndof_(-1) {
41     //
42     // prepare fit
43     //
44     initJacobian();
45    
46     fit(fwdParameters,fwdCovariance,bwdParameters,bwdCovariance,hitPosition,hitErrors);
47     }
48    
49     void
50     CollinearFitAtTM2::initJacobian ()
51     {
52     //
53     // Jacobian
54     //
55     for ( int i=0; i<12; ++i ) {
56     for ( int j=0; j<6; ++j ) jacobian_(i,j) = 0;
57     }
58     for ( int i=1; i<5; ++i ) {
59     jacobian_(i,ParQpOut+i) = jacobian_(i+5,ParQpOut+i) = 1;
60     }
61     jacobian_(0,ParQpIn) = 1.;
62     jacobian_(5,ParQpOut) = 1.;
63     }
64    
65     bool
66     CollinearFitAtTM2::fit (const AlgebraicVector5& fwdParameters,
67     const AlgebraicSymMatrix55& fwdCovariance,
68     const AlgebraicVector5& bwdParameters,
69     const AlgebraicSymMatrix55& bwdCovariance,
70     const LocalPoint& hitPos, const LocalError& hitErr)
71     {
72    
73     if ( hitErr.xx()>0 )
74     jacobian_(10,ParX) = jacobian_(11,ParY) = 1;
75     else
76     jacobian_(10,ParX) = jacobian_(11,ParY) = 0;
77    
78     for ( int i=0; i<12; ++i ) {
79     for ( int j=0; j<12; ++j ) weightMatrix_(i,j) = 0;
80     }
81    
82     for ( int i=0; i<5; ++i ) measurements_(i) = fwdParameters(i);
83     weightMatrix_.Place_at(fwdCovariance,0,0);
84     for ( int i=0; i<5; ++i ) measurements_(i+5) = bwdParameters(i);
85     weightMatrix_.Place_at(bwdCovariance,5,5);
86     if ( hitErr.xx()>0 ) {
87     measurements_(10) = hitPos.x();
88     measurements_(11) = hitPos.y();
89     weightMatrix_(10,10) = hitErr.xx();
90     weightMatrix_(10,11) = weightMatrix_(11,10) = hitErr.xy();
91     weightMatrix_(11,11) = hitErr.yy();
92     }
93     else {
94     measurements_(10) = measurements_(11) = 0.;
95     weightMatrix_(10,10) = weightMatrix_(11,11) = 1.;
96     weightMatrix_(10,11) = weightMatrix_(11,10) = 0.;
97     }
98     //
99     // invert covariance matrix
100     //
101     if ( !weightMatrix_.Invert() ) {
102     edm::LogWarning("CollinearFitAtTM2") << "Inversion of input covariance matrix failed";
103     return false;
104     }
105    
106     //
107     projectedMeasurements_ = ROOT::Math::Transpose(jacobian_)*(weightMatrix_*measurements_);
108     //
109     // Fitted parameters and covariance matrix
110     //
111     covariance_ = ROOT::Math::SimilarityT(jacobian_,weightMatrix_);
112     if ( !covariance_.Invert() ) {
113     edm::LogWarning("CollinearFitAtTM2") << "Inversion of resulting weight matrix failed";
114     return false;
115     }
116    
117     parameters_ = covariance_*projectedMeasurements_;
118    
119     //
120     // chi2
121     //
122     chi2_ = ROOT::Math::Similarity(measurements_,weightMatrix_) -
123     ROOT::Math::Similarity(projectedMeasurements_,covariance_);
124     ndof_ = hitErr.xx()>0 ? 6 : 4;
125    
126     valid_ = true;
127     return true;
128     }
129    
130     Measurement1D
131     CollinearFitAtTM2::deltaP () const {
132     //
133     // check validity
134     //
135     if ( !valid_ ) return Measurement1D();
136     //
137     // deltaP = 1/qpout - 1/qpin ; uncertainty from linear error propagation
138     //
139     double qpIn = parameters_(0);
140     double sig2In = covariance_(0,0);
141     double qpOut = parameters_(1);
142     double sig2Out = covariance_(1,1);
143     double corrInOut = covariance_(0,1);
144     double pIn = 1./fabs(qpIn);
145     double pOut = 1./fabs(qpOut);
146     double sig2DeltaP = pIn/qpIn*pIn/qpIn*sig2In - 2*pIn/qpIn*pOut/qpOut*corrInOut +
147     pOut/qpOut*pOut/qpOut*sig2Out;
148    
149     return Measurement1D(pOut-pIn,sig2DeltaP?sqrt(sig2DeltaP):0.);
150     }
151