CMS 3D CMS Logo

PosteriorWeightsCalculator.cc
Go to the documentation of this file.
2 
8 
9 #include <cfloat>
10 
11 std::vector<double> PosteriorWeightsCalculator::weights(const TrackingRecHit& recHit) const {
12  switch (recHit.dimension()) {
13  case 1:
14  return weights<1>(recHit);
15  case 2:
16  return weights<2>(recHit);
17  case 3:
18  return weights<3>(recHit);
19  case 4:
20  return weights<4>(recHit);
21  case 5:
22  return weights<5>(recHit);
23  }
24  throw cms::Exception("Error: rechit of size not 1,2,3,4,5");
25 }
26 
27 template <unsigned int D>
28 std::vector<double> PosteriorWeightsCalculator::weights(const TrackingRecHit& recHit) const {
29  typedef typename AlgebraicROOTObject<D, 5>::Matrix MatD5;
30  typedef typename AlgebraicROOTObject<5, D>::Matrix Mat5D;
31  typedef typename AlgebraicROOTObject<D, D>::SymMatrix SMatDD;
32  typedef typename AlgebraicROOTObject<D>::Vector VecD;
33  using ROOT::Math::SMatrixNoInit;
34 
35  std::vector<double> weights;
36  if (predictedComponents.empty()) {
37  edm::LogError("EmptyPredictedComponents") << "a multi state is empty. cannot compute any weight.";
38  return weights;
39  }
40  weights.reserve(predictedComponents.size());
41 
42  std::vector<double> detRs;
43  detRs.reserve(predictedComponents.size());
44  std::vector<double> chi2s;
45  chi2s.reserve(predictedComponents.size());
46 
47  VecD r, rMeas;
48  SMatDD V(SMatrixNoInit{}), R(SMatrixNoInit{});
50  //
51  // calculate chi2 and determinant / component and find
52  // minimum / maximum of chi2
53  //
54  double chi2Min(DBL_MAX);
55  for (unsigned int i = 0; i < predictedComponents.size(); i++) {
56  KfComponentsHolder holder;
57  auto const& x = predictedComponents[i].localParameters().vector();
58  holder.template setup<D>(&r, &V, &p, &rMeas, &R, x, predictedComponents[i].localError().matrix());
59  recHit.getKfComponents(holder);
60 
61  r -= rMeas;
62  R += V;
63 
64  double detR;
65  if (!R.Det2(detR)) {
66  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: determinant failed";
67  return std::vector<double>();
68  }
69  detRs.push_back(detR);
70 
71  bool ok = invertPosDefMatrix(R);
72  if (!ok) {
73  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: inversion failed";
74  return std::vector<double>();
75  }
76  double chi2 = ROOT::Math::Similarity(r, R);
77  chi2s.push_back(chi2);
78  if (chi2 < chi2Min)
79  chi2Min = chi2;
80  }
81 
82  if (detRs.size() != predictedComponents.size() || chi2s.size() != predictedComponents.size()) {
83  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes";
84  return std::vector<double>();
85  }
86 
87  //
88  // calculate weights (extracting a common factor
89  // exp(-0.5*chi2Min) to avoid numerical problems
90  // during exponentation
91  //
92  double sumWeights(0.);
93  for (unsigned int i = 0; i < predictedComponents.size(); i++) {
94  double priorWeight = predictedComponents[i].weight();
95 
96  double chi2 = chi2s[i] - chi2Min;
97 
98  double tempWeight(0.);
99  if (detRs[i] > FLT_MIN) {
100  //
101  // Calculation of (non-normalised) weight. Common factors exp(-chi2Norm/2.) and
102  // 1./sqrt(2*pi*recHit.dimension()) have been omitted
103  //
104  tempWeight = priorWeight * std::sqrt(1. / detRs[i]) * std::exp(-0.5 * chi2);
105  } else {
106  LogDebug("GsfTrackFitters") << "PosteriorWeightsCalculator: detR < FLT_MIN !!";
107  }
108  weights.push_back(tempWeight);
109  sumWeights += tempWeight;
110  }
111 
112  if (sumWeights < DBL_MIN) {
113  LogDebug("GsfTrackFitters") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
114  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
115  return std::vector<double>();
116  }
117 
118  if (weights.size() != predictedComponents.size()) {
119  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes (2)";
120  return std::vector<double>();
121  }
122  sumWeights = 1. / sumWeights;
123  for (auto& w : weights)
124  w *= sumWeights;
125  return weights;
126 }
ROOT::Math::SMatrix< double, D1, D1, ROOT::Math::MatRepSym< double, D1 > > SymMatrix
T w() const
Log< level::Error, false > LogError
ROOT::Math::SMatrix< double, D1, D2, ROOT::Math::MatRepStd< double, D1, D2 > > Matrix
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t V
T sqrt(T t)
Definition: SSEVec.h:19
std::vector< double > weights(const TrackingRecHit &tsos) const
Create random state.
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
ROOT::Math::SVector< double, D1 > Vector
#define LogDebug(id)