CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PosteriorWeightsCalculator.cc
Go to the documentation of this file.
2 
6 
7 #include <cfloat>
8 
9 std::vector<double> PosteriorWeightsCalculator::weights(const TransientTrackingRecHit& recHit) const {
10  switch (recHit.dimension()) {
11  case 1: return weights<1>(recHit);
12  case 2: return weights<2>(recHit);
13  case 3: return weights<3>(recHit);
14  case 4: return weights<4>(recHit);
15  case 5: return weights<5>(recHit);
16  }
17  throw cms::Exception("Error: rechit of size not 1,2,3,4,5");
18 }
19 
20 template<unsigned int D>
21 std::vector<double> PosteriorWeightsCalculator::weights(const TransientTrackingRecHit& recHit) const {
22  typedef typename AlgebraicROOTObject<D,5>::Matrix MatD5;
23  typedef typename AlgebraicROOTObject<5,D>::Matrix Mat5D;
24  typedef typename AlgebraicROOTObject<D,D>::SymMatrix SMatDD;
25  typedef typename AlgebraicROOTObject<D>::Vector VecD;
26 
27  std::vector<double> weights;
28  if ( predictedComponents.empty() ) {
29  edm::LogError("EmptyPredictedComponents")<<"a multi state is empty. cannot compute any weight.";
30  return weights;
31  }
32  weights.reserve(predictedComponents.size());
33 
34  std::vector<double> detRs;
35  detRs.reserve(predictedComponents.size());
36  std::vector<double> chi2s;
37  chi2s.reserve(predictedComponents.size());
38 
39  MatD5 H;
40  VecD r, rMeas;
41  SMatDD V, R;
43  //
44  // calculate chi2 and determinant / component and find
45  // minimum / maximum of chi2
46  //
47  double chi2Min(DBL_MAX);
48  for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
49 // MeasurementExtractor me(predictedComponents[i]);
50 // // Residuals of aPredictedState w.r.t. aRecHit,
51 // //!!! AlgebraicVector r(recHit.parameters(predictedComponents[i]) - me.measuredParameters(recHit));
52 // VecD r = asSVector<D>(recHit.parameters()) - me.measuredParameters<D>(recHit);
53 // // and covariance matrix of residuals
54 // //!!! AlgebraicSymMatrix V(recHit.parametersError(predictedComponents[i]));
55 // SMatDD V = asSMatrix<D>(recHit.parametersError());
56 // SMatDD R = V + me.measuredError<D>(recHit);
57 
58  KfComponentsHolder holder;
59  x = predictedComponents[i].localParameters().vector();
60  holder.template setup<D>(&r, &V, &H, &rMeas, &R,
61  x, predictedComponents[i].localError().matrix());
62  recHit.getKfComponents(holder);
63 
64  r -= rMeas;
65  R += V;
66 
67  double detR;
68  if (! R.Det2(detR) ) {
69  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: determinant failed";
70  return std::vector<double>();
71  }
72  detRs.push_back(detR);
73 
74  int ierr = ! R.Invert(); // if (ierr != 0) throw exception;
75  if ( ierr!=0 ) {
76  edm::LogError("PosteriorWeightsCalculator")
77  << "PosteriorWeightsCalculator: inversion failed, ierr = " << ierr;
78  return std::vector<double>();
79  }
80  double chi2 = ROOT::Math::Similarity(r,R);
81  chi2s.push_back(chi2);
82  if ( chi2<chi2Min ) chi2Min = chi2;
83  }
84  if ( detRs.size()!=predictedComponents.size() ||
85  chi2s.size()!=predictedComponents.size() ) {
86  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes";
87  return std::vector<double>();
88  }
89  //
90  // calculate weights (extracting a common factor
91  // exp(-0.5*chi2Min) to avoid numerical problems
92  // during exponentation
93  //
94  double sumWeights(0.);
95  for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
96  double priorWeight = predictedComponents[i].weight();
97 
98  double chi2 = chi2s[i] - chi2Min;
99 
100  double tempWeight(0.);
101  if ( detRs[i]>FLT_MIN ) {
102  //
103  // Calculation of (non-normalised) weight. Common factors exp(-chi2Norm/2.) and
104  // 1./sqrt(2*pi*recHit.dimension()) have been omitted
105  //
106  tempWeight = priorWeight * sqrt(1./detRs[i]) * exp(-0.5 * chi2);
107  }
108  // else {
109  // edm::LogInfo("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: detR < FLT_MIN !!";
110  // }
111  weights.push_back(tempWeight);
112  sumWeights += tempWeight;
113  }
114  if ( sumWeights<DBL_MIN ) {
115  edm::LogInfo("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
116  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
117  return std::vector<double>();
118  }
119 
120  if ( weights.size()!=predictedComponents.size() ) {
121  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes (2)";
122  return std::vector<double>();
123  }
124  for (std::vector<double>::iterator iter = weights.begin();
125  iter != weights.end(); iter++) {
126  (*iter) /= sumWeights;
127  }
128 
129  return weights;
130 }
int i
Definition: DBlmapReader.cc:9
virtual int dimension() const =0
virtual void getKfComponents(KfComponentsHolder &holder) const
ROOT::Math::SMatrix< double, D1, D1, ROOT::Math::MatRepSym< double, D1 > > SymMatrix
ROOT::Math::SMatrix< double, D1, D2, ROOT::Math::MatRepStd< double, D1, D2 > > Matrix
T sqrt(T t)
Definition: SSEVec.h:46
std::vector< double > weights(const TransientTrackingRecHit &tsos) const
Create random state.
ROOT::Math::SVector< double, D1 > Vector
ROOT::Math::SVector< double, 5 > AlgebraicVector5
x
Definition: VDTMath.h:216