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 TrackingRecHit& 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 TrackingRecHit& 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  using ROOT::Math::SMatrixNoInit;
27 
28  std::vector<double> weights;
29  if ( predictedComponents.empty() ) {
30  edm::LogError("EmptyPredictedComponents")<<"a multi state is empty. cannot compute any weight.";
31  return weights;
32  }
33  weights.reserve(predictedComponents.size());
34 
35  std::vector<double> detRs;
36  detRs.reserve(predictedComponents.size());
37  std::vector<double> chi2s;
38  chi2s.reserve(predictedComponents.size());
39 
40  VecD r, rMeas;
41  SMatDD V(SMatrixNoInit{}), R(SMatrixNoInit{});
44  //
45  // calculate chi2 and determinant / component and find
46  // minimum / maximum of chi2
47  //
48  double chi2Min(DBL_MAX);
49  for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
50 
51  KfComponentsHolder holder;
52  x = predictedComponents[i].localParameters().vector();
53  holder.template setup<D>(&r, &V, &p, &rMeas, &R,
54  x, predictedComponents[i].localError().matrix());
55  recHit.getKfComponents(holder);
56 
57  r -= rMeas;
58  R += V;
59 
60  double detR;
61  if (! R.Det2(detR) ) {
62  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: determinant failed";
63  return std::vector<double>();
64  }
65  detRs.push_back(detR);
66 
67  int ierr = ! R.Invert(); // if (ierr != 0) throw exception;
68  if ( ierr!=0 ) {
69  edm::LogError("PosteriorWeightsCalculator")
70  << "PosteriorWeightsCalculator: inversion failed, ierr = " << ierr;
71  return std::vector<double>();
72  }
73  double chi2 = ROOT::Math::Similarity(r,R);
74  chi2s.push_back(chi2);
75  if ( chi2<chi2Min ) chi2Min = chi2;
76  }
77  if ( detRs.size()!=predictedComponents.size() ||
78  chi2s.size()!=predictedComponents.size() ) {
79  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes";
80  return std::vector<double>();
81  }
82  //
83  // calculate weights (extracting a common factor
84  // exp(-0.5*chi2Min) to avoid numerical problems
85  // during exponentation
86  //
87  double sumWeights(0.);
88  for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
89  double priorWeight = predictedComponents[i].weight();
90 
91  double chi2 = chi2s[i] - chi2Min;
92 
93  double tempWeight(0.);
94  if ( detRs[i]>FLT_MIN ) {
95  //
96  // Calculation of (non-normalised) weight. Common factors exp(-chi2Norm/2.) and
97  // 1./sqrt(2*pi*recHit.dimension()) have been omitted
98  //
99  tempWeight = priorWeight * sqrt(1./detRs[i]) * exp(-0.5 * chi2);
100  }
101  // else {
102  // edm::LogInfo("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: detR < FLT_MIN !!";
103  // }
104  weights.push_back(tempWeight);
105  sumWeights += tempWeight;
106  }
107  if ( sumWeights<DBL_MIN ) {
108  edm::LogInfo("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
109  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
110  return std::vector<double>();
111  }
112 
113  if ( weights.size()!=predictedComponents.size() ) {
114  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes (2)";
115  return std::vector<double>();
116  }
117  for (std::vector<double>::iterator iter = weights.begin();
118  iter != weights.end(); iter++) {
119  (*iter) /= sumWeights;
120  }
121 
122  return weights;
123 }
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
std::vector< double > weights(const TrackingRecHit &tsos) const
Create random state.
ROOT::Math::SMatrix< double, D1, D2, ROOT::Math::MatRepStd< double, D1, D2 > > Matrix
T sqrt(T t)
Definition: SSEVec.h:48
ROOT::Math::SVector< double, D1 > Vector
ROOT::Math::SVector< double, 5 > AlgebraicVector5
Definition: DDAxes.h:10