CMS 3D CMS Logo

KinematicConstrainedVertexUpdatorT.h
Go to the documentation of this file.
1 #ifndef KinematicConstrainedVertexUpdatorT_H
2 #define KinematicConstrainedVertexUpdatorT_H
3 
4 #include <atomic>
5 #include <cassert>
6 #include <iostream>
7 
13 
14 // the usual stupid counter
15 namespace KineDebug3 {
16  struct Count {
17  std::atomic<int> n;
18  Count() : n(0){}
19  ~Count() {
20  }
21 
22  };
23  inline void count() {
24  CMS_THREAD_SAFE static Count c;
25  ++c.n;
26  }
27 
28 }
29 
35 template < int nTrk, int nConstraint> class KinematicConstrainedVertexUpdatorT
36 {
37 public:
38 
43 
45 
55  update(const ROOT::Math::SVector<double, 3+7*nTrk> & inState,
56  ROOT::Math::SMatrix<double, 3+7*nTrk,3+7*nTrk ,ROOT::Math::MatRepSym<double,3+7*nTrk> >& inCov, std::vector<KinematicState> & lStates,
58 
59 private:
60 
63  ROOT::Math::SVector<double,3+7*nTrk> delta_alpha;
64  ROOT::Math::SMatrix<double,nConstraint+4,3+7*nTrk> g;
65  ROOT::Math::SVector<double,nConstraint+4> val;
66  ROOT::Math::SVector<double, 3+7*nTrk> finPar;
67  ROOT::Math::SVector<double, nConstraint+4> lambda;
68  ROOT::Math::SMatrix<double,3,3,ROOT::Math::MatRepSym<double,3> > pCov = ROOT::Math::SMatrixNoInit();
69  ROOT::Math::SMatrix<double,7,7,ROOT::Math::MatRepSym<double,7> > nCovariance = ROOT::Math::SMatrixNoInit();
70  ROOT::Math::SMatrix<double,nConstraint+4,nConstraint+4,ROOT::Math::MatRepSym<double,nConstraint+4> > v_g_sym = ROOT::Math::SMatrixNoInit();
71 
72 };
73 
75 
76 template < int nTrk, int nConstraint>
78 {}
79 
80 template < int nTrk, int nConstraint>
82 {}
83 
84 template < int nTrk, int nConstraint>
86 KinematicConstrainedVertexUpdatorT< nTrk, nConstraint >::update(const ROOT::Math::SVector<double,3+7*nTrk>& inPar,
87  ROOT::Math::SMatrix<double, 3+7*nTrk,3+7*nTrk ,ROOT::Math::MatRepSym<double,3+7*nTrk> >& inCov,
88  std::vector<KinematicState> & lStates,
89  const GlobalPoint& lPoint,
90  GlobalVector const & fieldValue,
92 {
94 
95  int vSize = lStates.size();
96 
97  assert( nConstraint==0 || cs!=nullptr);
98  assert(vSize == nConstraint);
99 
100  const MagneticField* field=lStates.front().magneticField();
101 
102  delta_alpha=inPar;
103  delta_alpha(0)-=lPoint.x();
104  delta_alpha(1)-=lPoint.y();
105  delta_alpha(2)-=lPoint.z();
106  int cst=3;
107  for(std::vector<KinematicState>::const_iterator i = lStates.begin(); i != lStates.end(); i++)
108  for ( int j=0; j<7; j++) {
109  delta_alpha(cst)-=i->kinematicParameters()(j);
110  cst++;
111  }
112 
113  // cout<<"delta_alpha"<<delta_alpha<<endl;
114  //resulting matrix of derivatives and vector of values.
115  //their size depends of number of tracks to analyze and number of
116  //additional constraints to apply
117 
118  if( nConstraint !=0) {
119  cs->init(lStates, lPoint, fieldValue);
120  val.Place_at(cs->value(),0);
121  g.Place_at(cs->positionDerivative(),0,0);
122  g.Place_at(cs->parametersDerivative(),0,3);
123  }
124 
125  vConstraint.init(lStates, lPoint, fieldValue);
126  val.Place_at(vConstraint.value(),nConstraint);
127  g.Place_at(vConstraint.positionDerivative(),nConstraint, 0);
128  g.Place_at(vConstraint.parametersDerivative(),nConstraint, 3);
129 
130 
131 
132 
133  //debug code
134  v_g_sym = ROOT::Math::Similarity(g,inCov);
135 
136  // bool ifl1 = v_g_sym.Invert();
137  bool ifl1 = invertPosDefMatrix(v_g_sym);
138  if(!ifl1) {
139  edm::LogWarning("KinematicConstrainedVertexUpdatorFailed")<< "invert failed\n"
140  << v_g_sym;
141  LogDebug("KinematicConstrainedVertexFitter3")
142  << "Fit failed: unable to invert SYM gain matrix\n";
143  return RefCountedKinematicVertex();
144  }
145 
146  // delta alpha is now valid!
147  //full math case now!
148  val += g*delta_alpha;
149  lambda = v_g_sym *val;
150 
151  //final parameters
152  finPar = inPar - inCov * (ROOT::Math::Transpose(g) * lambda);
153 
154  //refitted covariance
155  ROOT::Math::SMatrix<double,3+7*nTrk,3+7*nTrk,ROOT::Math::MatRepSym<double,3+7*nTrk> > prod = ROOT::Math::SimilarityT(g,v_g_sym);
156  ROOT::Math::SMatrix<double,3+7*nTrk,3+7*nTrk,ROOT::Math::MatRepSym<double,3+7*nTrk> > prod1;
157  ROOT::Math::AssignSym::Evaluate(prod1, inCov * prod * inCov);
158  // ROOT::Math::AssignSym::Evaluate(prod, prod1 * inCov);
159  inCov -= prod1;
160 
161  pCov = inCov.template Sub< ROOT::Math::SMatrix<double,3,3,ROOT::Math::MatRepSym<double,3> > >(0,0);
162 
163  // chi2
164  double chi = ROOT::Math::Dot(lambda,val); //??
165 
166  //this is ndf without significant prior
167  //vertex so -3 factor exists here
168  float ndf = 2*vSize - 3;
169  ndf += nConstraint;
170 
171 
172  //making resulting vertex
173  GlobalPoint vPos (finPar(0),finPar(1),finPar(2));
174  VertexState st(vPos,GlobalError(pCov));
175  RefCountedKinematicVertex rVtx = vFactory.vertex(st,chi,ndf);
176 
177  //making refitted states of Kinematic Particles
178  AlgebraicVector7 newPar;
179  int i_int = 0;
180  for(std::vector<KinematicState>::iterator i_st=lStates.begin(); i_st != lStates.end(); i_st++)
181  {
182  for(int i =0; i<7; i++)
183  {newPar(i) = finPar(3 + i_int*7 + i);}
184 
185  nCovariance = inCov.template Sub<ROOT::Math::SMatrix<double, 7,7,ROOT::Math::MatRepSym<double,7> > >(3 + i_int*7, 3 + i_int*7);
186  TrackCharge chl = i_st->particleCharge();
187  KinematicParameters nrPar(newPar);
188  KinematicParametersError nrEr(nCovariance);
189  KinematicState newState(nrPar,nrEr,chl, field);
190  (*i_st) = newState;
191  i_int++;
192  }
193  return rVtx;
194 }
195 
196 #endif
#define LogDebug(id)
ROOT::Math::SVector< double, 3+7 *nTrk > finPar
RefCountedKinematicVertex update(const ROOT::Math::SVector< double, 3+7 *nTrk > &inState, ROOT::Math::SMatrix< double, 3+7 *nTrk, 3+7 *nTrk, ROOT::Math::MatRepSym< double, 3+7 *nTrk > > &inCov, std::vector< KinematicState > &lStates, const GlobalPoint &lPoint, GlobalVector const &fieldValue, MultiTrackKinematicConstraintT< nTrk, nConstraint > *cs)
unique_ptr< ClusterSequence > cs
T y() const
Definition: PV3DBase.h:63
ROOT::Math::SMatrix< double, nConstraint+4, 3+7 *nTrk > g
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:13
int TrackCharge
Definition: TrackCharge.h:4
ROOT::Math::SVector< double, 7 > AlgebraicVector7
Definition: Matrices.h:8
T z() const
Definition: PV3DBase.h:64
ROOT::Math::SVector< double, nConstraint+4 > val
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
#define CMS_THREAD_SAFE
parametersDerivativeType const & parametersDerivative() const
ReferenceCountingPointer< KinematicVertex > RefCountedKinematicVertex
positionDerivativeType const & positionDerivative() const
ROOT::Math::SVector< double, 3+7 *nTrk > delta_alpha
ROOT::Math::SVector< double, nConstraint+4 > lambda
#define update(a, b)
virtual void init(const std::vector< KinematicState > &states, const GlobalPoint &point, const GlobalVector &mf)=0
T x() const
Definition: PV3DBase.h:62