CMS 3D CMS Logo

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