CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
KinematicConstrainedVertexUpdator.cc
Go to the documentation of this file.
3 
5 {
8 }
9 
11 {
12  delete vFactory;
13  delete vConstraint;
14 }
15 
16 std::pair<std::pair<std::vector<KinematicState>, AlgebraicMatrix >, RefCountedKinematicVertex >
18  const AlgebraicMatrix& inCov, std::vector<KinematicState> lStates,
19  const GlobalPoint& lPoint, MultiTrackKinematicConstraint * cs)const
20 {
21  const MagneticField* field=lStates.front().magneticField();
22  AlgebraicMatrix d_matrix = vConstraint->parametersDerivative(lStates, lPoint);
23  AlgebraicMatrix e_matrix = vConstraint->positionDerivative(lStates, lPoint);
24  AlgebraicVector val_s = vConstraint->value(lStates, lPoint);
25  int vSize = lStates.size();
26 
27 //delta alpha
28  AlgebraicVector d_a(7*vSize + 3);
29  d_a(1) = lPoint.x();
30  d_a(2) = lPoint.y();
31  d_a(3) = lPoint.z();
32 
33  int cst = 0;
34  for(std::vector<KinematicState>::const_iterator i = lStates.begin(); i != lStates.end(); i++)
35  {
36  AlgebraicVector lst_par = asHepVector<7>(i->kinematicParameters().vector());
37  for(int j = 1; j<lst_par.num_row()+1; j++)
38  {d_a(3+7*cst+j) = lst_par(j);}
39  cst++;
40  }
41 
42 
43  AlgebraicVector delta_alpha = inPar - d_a;
44 
45 // cout<<"delta_alpha"<<delta_alpha<<endl;
46 //resulting matrix of derivatives and vector of values.
47 //their size depends of number of tracks to analyze and number of
48 //additional constraints to apply
50  AlgebraicVector val;
51  if(cs == 0)
52  {
53 
54 //unconstrained vertex fitter case
55  g = AlgebraicMatrix(2*vSize,7*vSize+3,0);
56  val = AlgebraicVector(2*vSize);
57 
58 //filling the derivative matrix
59  g.sub(1,1,e_matrix);
60  g.sub(1,4,d_matrix);
61 
62 //filling the vector of values
63  val = val_s;
64  }else{
65 
66 //constrained vertex fitter case
67  int n_eq =cs->numberOfEquations();
68  g = AlgebraicMatrix(n_eq + 2*vSize,7*vSize + 3,0);
69  val = AlgebraicVector(n_eq + 2*vSize);
70  AlgebraicMatrix n_x = cs->positionDerivative(lStates, lPoint);
71  AlgebraicMatrix n_alpha = cs->parametersDerivative(lStates, lPoint);
72  AlgebraicVector c_val = cs->value(lStates, lPoint);
73 
74 //filling the derivative matrix
75 // cout<<"n_x:"<<n_x<<endl;
76 // cout<<"n_alpha"<<n_alpha<<endl;
77 
78  g.sub(1,1,n_x);
79  g.sub(1,4,n_alpha);
80  g.sub(n_eq+1, 1, e_matrix);
81  g.sub(n_eq+1, 4, d_matrix);
82 
83 //filling the vector of values
84  for(int i = 1;i< n_eq+1; i++)
85  {val(i) = c_val(i);}
86  for(int i = 1; i<(2*vSize+1); i++)
87  {val(i+n_eq) = val_s(i);}
88  }
89 
90  //check for NaN
91  for(int i = 1; i<=val.num_row();++i) {
92  if (std::isnan(val(i))) {
93  LogDebug("KinematicConstrainedVertexUpdator")
94  << "catched NaN.\n";
95  return std::pair<std::pair<std::vector<KinematicState>, AlgebraicMatrix>, RefCountedKinematicVertex >(
96  std::pair<std::vector<KinematicState>, AlgebraicMatrix>(std::vector<KinematicState>(), AlgebraicMatrix(1,0)),
98  }
99  }
100 
101 //debug feature
102  AlgebraicSymMatrix in_cov_sym(7*vSize + 3,0);
103 
104  for(int i = 1; i<7*vSize+4; ++i)
105  {
106  for(int j = 1; j<7*vSize+4; ++j)
107  {if(i<=j) in_cov_sym(i,j) = inCov(i,j);}
108  }
109 
110 //debug code
111  AlgebraicSymMatrix v_g_sym = in_cov_sym.similarity(g);
112 
113  int ifl1 = 0;
114  v_g_sym.invert(ifl1);
115  if(ifl1 !=0) {
116  LogDebug("KinematicConstrainedVertexFitter")
117  << "Fit failed: unable to invert SYM gain matrix\n";
118  return std::pair<std::pair<std::vector<KinematicState>, AlgebraicMatrix>, RefCountedKinematicVertex >(
119  std::pair<std::vector<KinematicState>, AlgebraicMatrix>(std::vector<KinematicState>(), AlgebraicMatrix(1,0)),
121  }
122 
123 // delta alpha is now valid!
124 //full math case now!
125  AlgebraicVector lambda = v_g_sym *(g*delta_alpha + val);
126 
127 //final parameters
128  AlgebraicVector finPar = inPar - in_cov_sym * g.T() * lambda;
129 
130 //covariance matrix business:
131  AlgebraicMatrix mFactor = in_cov_sym *(v_g_sym.similarityT(g))* in_cov_sym;
132 
133 //refitted covariance
134  AlgebraicMatrix rCov = in_cov_sym - mFactor;
135 
136 //symmetric covariance:
137  AlgebraicSymMatrix r_cov_sym(7*vSize+3,0);
138  for(int i = 1; i<7*vSize+4; ++i)
139  {
140  for(int j = 1; j<7*vSize+4; ++j)
141  {if(i<=j)r_cov_sym(i,j) = rCov(i,j);}
142  }
143 
144  AlgebraicSymMatrix pCov = r_cov_sym.sub(1,3);
145 
146 // chi2
147  AlgebraicVector chi = lambda.T()*(g*delta_alpha + val);
148 
149 //this is ndf without significant prior
150 //vertex so -3 factor exists here
151  float ndf = 2*vSize - 3;
152  if(cs != 0){ndf += cs->numberOfEquations();}
153 
154 
155 //making resulting vertex
156  GlobalPoint vPos (finPar(1),finPar(2),finPar(3));
157  VertexState st(vPos,GlobalError( asSMatrix<3>(pCov)));
158  RefCountedKinematicVertex rVtx = vFactory->vertex(st,chi(1),ndf);
159 
160 //making refitted states of Kinematic Particles
161  int i_int = 0;
162  std::vector<KinematicState> ns;
163  for(std::vector<KinematicState>::iterator i_st=lStates.begin(); i_st != lStates.end(); i_st++)
164  {
165  AlgebraicVector7 newPar;
166  for(int i =0; i<7; i++)
167  {newPar(i) = finPar(4 + i_int*7 + i);}
168 
169  AlgebraicSymMatrix nCovariance = r_cov_sym.sub(4 + i_int*7, 10 + i_int*7);
170  TrackCharge chl = i_st->particleCharge();
171  KinematicParameters nrPar(newPar);
172  KinematicParametersError nrEr(asSMatrix<7>(nCovariance));
173  KinematicState newState(nrPar,nrEr,chl, field);
174  ns.push_back(newState);
175  i_int++;
176  }
177  std::pair<std::vector<KinematicState>, AlgebraicMatrix> ns_m(ns,rCov);
178  return std::pair<std::pair<std::vector<KinematicState>, AlgebraicMatrix>, RefCountedKinematicVertex >(ns_m,rVtx);
179 }
#define LogDebug(id)
virtual AlgebraicMatrix positionDerivative(const std::vector< KinematicState >, const GlobalPoint &) const =0
int i
Definition: DBlmapReader.cc:9
auto_ptr< ClusterSequence > cs
T y() const
Definition: PV3DBase.h:62
virtual AlgebraicMatrix parametersDerivative(const std::vector< KinematicState >, const GlobalPoint &) const =0
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
GlobalErrorBase< double, ErrorMatrixTag > GlobalError
Definition: GlobalError.h:11
int TrackCharge
Definition: TrackCharge.h:4
ROOT::Math::SVector< double, 7 > AlgebraicVector7
Definition: Matrices.h:7
CLHEP::HepMatrix AlgebraicMatrix
bool isnan(float x)
Definition: math.h:13
T z() const
Definition: PV3DBase.h:63
virtual int numberOfEquations() const =0
int j
Definition: DBlmapReader.cc:9
CLHEP::HepVector AlgebraicVector
ReferenceCountingPointer< KinematicVertex > RefCountedKinematicVertex
virtual AlgebraicMatrix parametersDerivative(const std::vector< KinematicState > states, const GlobalPoint &point) const
virtual AlgebraicMatrix positionDerivative(const std::vector< KinematicState > states, const GlobalPoint &point) const
CLHEP::HepSymMatrix AlgebraicSymMatrix
virtual AlgebraicVector value(const std::vector< KinematicState > states, const GlobalPoint &point) const
static RefCountedKinematicVertex vertex(const VertexState &state, float totalChiSq, float degreesOfFr)
virtual AlgebraicVector value(const std::vector< KinematicState >, const GlobalPoint &) const =0
std::pair< std::pair< std::vector< KinematicState >, AlgebraicMatrix >, RefCountedKinematicVertex > update(const AlgebraicVector &inState, const AlgebraicMatrix &inCov, std::vector< KinematicState > lStates, const GlobalPoint &lPoint, MultiTrackKinematicConstraint *cs) const
T x() const
Definition: PV3DBase.h:61