CMS 3D CMS Logo

SiTrackerMultiRecHitUpdator.cc
Go to the documentation of this file.
6 
10 
14 
16 
18  const TrackingRecHitPropagator* hitpropagator,
19  const float Chi2Cut1D,
20  const float Chi2Cut2D,
21  const std::vector<double>& anAnnealingProgram,
22  bool debug)
23  : theBuilder(builder),
24  theHitPropagator(hitpropagator),
25  theChi2Cut1D(Chi2Cut1D),
26  theChi2Cut2D(Chi2Cut2D),
27  theAnnealingProgram(anAnnealingProgram),
28  debug_(debug) {
29  theHitCloner = static_cast<TkTransientTrackingRecHitBuilder const*>(builder)->cloner();
30 }
31 
33  const std::vector<const TrackingRecHit*>& rhv,
34  const TrajectoryStateOnSurface& tsos,
35  MeasurementDetWithData& measDet,
36  float annealing) const {
37  LogTrace("SiTrackerMultiRecHitUpdator")
38  << "Calling SiTrackerMultiRecHitUpdator::buildMultiRecHit with AnnealingFactor: " << annealing;
39 
41  for (std::vector<const TrackingRecHit*>::const_iterator iter = rhv.begin(); iter != rhv.end(); iter++) {
43  if (transient->isValid())
44  tcomponents.push_back(transient);
45  }
46  return update(tcomponents, tsos, measDet, annealing);
47 }
48 
51  const TrajectoryStateOnSurface& tsos,
52  MeasurementDetWithData& measDet,
53  double annealing) const {
54  LogTrace("SiTrackerMultiRecHitUpdator")
55  << "Calling SiTrackerMultiRecHitUpdator::update with AnnealingFactor: " << annealing;
56 
57  if (!tsos.isValid()) {
58  //return original->clone();
59  throw cms::Exception("SiTrackerMultiRecHitUpdator") << "!!! MultiRecHitUpdator::update(..): tsos NOT valid!!! ";
60  }
61 
62  //check if to clone is the right thing
63  if (original->isValid()) {
64  if (original->transientHits().empty()) {
65  return theHitCloner.makeShared(original, tsos);
66  }
67  } else {
68  return theHitCloner.makeShared(original, tsos);
69  }
70 
71  TransientTrackingRecHit::ConstRecHitContainer tcomponents = original->transientHits();
72  return update(tcomponents, tsos, measDet, annealing);
73 }
74 
75 /*------------------------------------------------------------------------------------------------------------------------*/
78  const TrajectoryStateOnSurface& tsos,
79  MeasurementDetWithData& measDet,
80  double annealing) const {
81  if (tcomponents.empty()) {
82  LogTrace("SiTrackerMultiRecHitUpdator") << "Empty components vector passed to SiTrackerMultiRecHitUpdator::update, "
83  "returning an InvalidTransientRecHit ";
84  return std::make_shared<InvalidTrackingRecHit>(measDet.mdet().geomDet(), TrackingRecHit::missing);
85  }
86 
87  if (!tsos.isValid()) {
88  LogTrace("SiTrackerMultiRecHitUpdator")
89  << "SiTrackerMultiRecHitUpdator::update: tsos NOT valid!!!, returning an InvalidTransientRecHit";
90  return std::make_shared<InvalidTrackingRecHit>(measDet.mdet().geomDet(), TrackingRecHit::missing);
91  }
92 
93  std::vector<TransientTrackingRecHit::RecHitPointer> updatedcomponents;
94  const GeomDet* geomdet = nullptr;
95 
96  //running on all over the MRH components
97  for (TransientTrackingRecHit::ConstRecHitContainer::const_iterator iter = tcomponents.begin();
98  iter != tcomponents.end();
99  iter++) {
100  //the first rechit must belong to the same surface of TSOS
101  if (iter == tcomponents.begin()) {
102  if (&((*iter)->det()->surface()) != &(tsos.surface())) {
103  throw cms::Exception("SiTrackerMultiRecHitUpdator")
104  << "the Trajectory state and the first rechit "
105  "passed to the SiTrackerMultiRecHitUpdator lay on different surfaces!: state lays on surface "
106  << tsos.surface().position() << " hit with detid " << (*iter)->det()->geographicalId().rawId()
107  << " lays on surface " << (*iter)->det()->surface().position();
108  }
109 
110  geomdet = (*iter)->det();
111  }
112 
113  //if the rechit does not belong to the surface of the tsos
114  //GenericProjectedRecHit2D is used to prepagate
115  if (&((*iter)->det()->surface()) != &(tsos.surface())) {
118  //if it is used a sensor by sensor grouping this should not appear
119  if (cloned->isValid())
120  updatedcomponents.push_back(cloned);
121 
122  } else {
124  if (cloned->isValid()) {
125  updatedcomponents.push_back(cloned);
126  }
127  }
128  }
129 
130  std::vector<std::pair<const TrackingRecHit*, float> > mymap;
131  std::vector<std::pair<const TrackingRecHit*, float> > normmap;
132 
133  double a_sum = 0, c_sum = 0;
134 
135  for (std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin();
136  ihit != updatedcomponents.end();
137  ihit++) {
138  double a_i = ComputeWeight(tsos, *(*ihit), false, annealing); //exp(-0.5*Chi2)
139  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t a_i:" << a_i;
140  //double c_i = ComputeWeight(tsos, *(*ihit), true, annealing); //exp(-0.5*theChi2Cut/annealing)/(2.*M_PI*sqrt(det));
141  //LogTrace("SiTrackerMultiRecHitUpdator")<< "\t\t c_i:" << c_i ;
142  mymap.push_back(std::pair<const TrackingRecHit*, float>((*ihit)->hit(), a_i));
143 
144  a_sum += a_i;
145  //with the new definition, the cut weight is computed only once
146  if (ihit == updatedcomponents.begin())
147  c_sum = ComputeWeight(tsos, *(*ihit), true, annealing); //exp(-0.5*theChi2Cut/annealing)
148  }
149  double total_sum = a_sum + c_sum;
150  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t c_sum:" << c_sum;
151  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t total sum:" << total_sum;
152 
153  unsigned int counter = 0;
154  bool invalid = true;
155  for (std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin();
156  ihit != updatedcomponents.end();
157  ihit++) {
158  double p = ((mymap[counter].second) / total_sum > 1.e-12 ? (mymap[counter].second) / total_sum : 1.e-12);
159  //ORCA: float p = ((mymap[counter].second)/total_sum > 0.01 ? (mymap[counter].second)/total_sum : 1.e-6);
160 
161 #ifdef EDM_ML_DEBUG
162  auto const& tmp = *mymap[counter].first;
163  LogTrace("SiTrackerMultiRecHitUpdator")
164  << " Component hit type " << typeid(tmp).name() << " and dim:" << tmp.dimension() << " position (PRECISE!!!)"
165  << tmp.localPosition() << " error " << tmp.localPositionError() << " with weight " << p;
166 #endif
167 
168  if (p > 10e-6) {
169  invalid = false;
170  normmap.push_back(std::pair<const TrackingRecHit*, float>(mymap[counter].first, p));
171  }
172 
173  counter++;
174  }
175 
176  if (!invalid) {
178  SiTrackerMultiRecHit updated(param.first, param.second, *normmap.front().first->det(), normmap, annealing);
179  LogTrace("SiTrackerMultiRecHitUpdator") << " Updated Hit position " << updated.localPosition() << " updated error "
180  << updated.localPositionError() << std::endl;
181 
182  return std::make_shared<SiTrackerMultiRecHit>(
183  param.first, param.second, *normmap.front().first->det(), normmap, annealing);
184 
185  } else {
186  LogTrace("SiTrackerMultiRecHitUpdator")
187  << " No hits with weight (> 10e-6) have been found for this MRH." << std::endl;
188  return std::make_shared<InvalidTrackingRecHit>(measDet.mdet().geomDet(), TrackingRecHit::missing);
189  }
190 }
191 
192 //---------------------------------------------------------------------------------------------------------------
194  const TransientTrackingRecHit& aRecHit,
195  bool CutWeight,
196  double annealing) const {
197  switch (aRecHit.dimension()) {
198  case 1:
199  return ComputeWeight<1>(tsos, aRecHit, CutWeight, annealing);
200  case 2:
201  return ComputeWeight<2>(tsos, aRecHit, CutWeight, annealing);
202  case 3:
203  return ComputeWeight<3>(tsos, aRecHit, CutWeight, annealing);
204  case 4:
205  return ComputeWeight<4>(tsos, aRecHit, CutWeight, annealing);
206  case 5:
207  return ComputeWeight<5>(tsos, aRecHit, CutWeight, annealing);
208  }
209  throw cms::Exception("Rec hit of invalid dimension (not 1,2,3,4,5)")
210  << "The value was " << aRecHit.dimension() << ", type is " << typeid(aRecHit).name() << "\n";
211 }
212 
213 //---------------------------------------------------------------------------------------------------------------
214 template <unsigned int N>
216  const TransientTrackingRecHit& aRecHit,
217  bool CutWeight,
218  double annealing) const {
219  // typedef typename AlgebraicROOTObject<N,5>::Matrix MatN5;
220  // typedef typename AlgebraicROOTObject<5,N>::Matrix Mat5N;
221  // typedef typename AlgebraicROOTObject<N,N>::SymMatrix SMatNN;
222  // typedef typename AlgebraicROOTObject<N>::Vector VecN;
223 
224  // define variables that will be used to setup the KfComponentsHolder
226  typename AlgebraicROOTObject<N>::Vector r, rMeas;
227  typename AlgebraicROOTObject<N, N>::SymMatrix R, RMeas, W;
229  const AlgebraicSymMatrix55& C = (tsos.localError().matrix());
230 
231  // setup the holder with the correct dimensions and get the values
232  KfComponentsHolder holder;
233  holder.template setup<N>(&r, &R, &pf, &rMeas, &RMeas, x, C);
234  aRecHit.getKfComponents(holder);
235 
236  typename AlgebraicROOTObject<N>::Vector diff = r - rMeas;
237 
238  if (!CutWeight) {
239  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t r:" << r;
240  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t tsospos:" << rMeas;
241  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t diff:" << diff;
242  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t R:" << R;
243  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t RMeas:" << RMeas;
244  }
245 
246  R += RMeas; //assume that TSOS is predicted || comb one
247  if (!CutWeight)
248  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t R+RMeas:" << R;
249 
250  //computing chi2 with the smoothTsos
251  // SMatNN R_smooth = R - RMeas;
252  // if(!CutWeight) LogTrace("SiTrackerMultiRecHitUpdator")<< "\t\t R-=Rmeas:" << R_smooth ;
253  // bool ierr2_bis = invertPosDefMatrix(R_smooth);
254  // double Chi2_smooth = ROOT::Math::Similarity(diff, R_smooth);
255 
256  //computing chi2 with the smoothTsos
257  //SMatNN R_pred = R + RMeas;
258  //if(!CutWeight) LogTrace("SiTrackerMultiRecHitUpdator")<< "\t\t R+=Rmeas:" << R_pred ;
259  //bool ierr2_bis = invertPosDefMatrix(R_pred );
260  //double Chi2_pred = ROOT::Math::Similarity(diff, R_pred );
261 
262  //Det2 method will preserve the content of the Matrix
263  //and return true when the calculation is successfull
264  double det;
265  bool ierr = R.Det2(det);
266 
267  bool ierr2 = invertPosDefMatrix(R); //ierr will be set to true when inversion is successfull
268  double Chi2 = ROOT::Math::Similarity(diff, R);
269 
270  if (!ierr || !ierr2) {
271  LogTrace("SiTrackerMultiRecHitUpdator") << "SiTrackerMultiRecHitUpdator::ComputeWeight: W not valid!" << std::endl;
272  LogTrace("SiTrackerMultiRecHitUpdator") << "V: " << R << " AnnealingFactor: " << annealing << std::endl;
273  }
274 
275  if (!CutWeight) {
276  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t det:" << det;
277  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t Chi2:" << Chi2;
278  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t Chi2/ann:" << Chi2 / annealing;
279  }
280 
281  double temp_weight = 0.0;
282  if (CutWeight && N == 1) {
283  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t Chi2Cut1D:" << theChi2Cut1D;
284  temp_weight = exp(-0.5 * theChi2Cut1D / annealing);
285  } else if (CutWeight && N == 2) {
286  LogTrace("SiTrackerMultiRecHitUpdator") << "\t\t Chi2Cut2D:" << theChi2Cut2D;
287  temp_weight = exp(-0.5 * theChi2Cut2D / annealing);
288  }
289 
290  if (!CutWeight) {
291  temp_weight = exp(-0.5 * Chi2 / annealing);
292  }
293 
294  return temp_weight;
295 }
296 
297 //-----------------------------------------------------------------------------------------------------------
299  const TrajectoryStateOnSurface& tsos, std::vector<std::pair<const TrackingRecHit*, float> >& aHitMap) const {
300  //supposing all the hits inside of a MRH have the same dimension
301  LogTrace("SiTrackerMultiRecHitUpdator")
302  << "SiTrackerMultiRecHitUpdator::LocalParameters: dim first recHit: " << aHitMap[0].first->dimension()
303  << std::endl;
304  switch (aHitMap[0].first->dimension()) {
305  case 1:
306  return calcParameters<1>(tsos, aHitMap);
307  case 2:
308  return calcParameters<2>(tsos, aHitMap);
309  }
310  throw cms::Exception("Rec hit of invalid dimension for computing MRH (not 1,2)")
311  << "The value was " << aHitMap[0].first->dimension() << ", type is " << typeid(aHitMap[0].first).name() << "\n";
312 }
313 
314 //-----------------------------------------------------------------------------------------------------------
315 template <unsigned int N>
317  const TrajectoryStateOnSurface& tsos, std::vector<std::pair<const TrackingRecHit*, float> >& aHitMap) const {
318  typedef typename AlgebraicROOTObject<N, N>::SymMatrix SMatNN;
319  typedef typename AlgebraicROOTObject<N>::Vector VecN;
320 
321  VecN m_sum;
322  SMatNN W_sum;
325 
326  //for TID and TEC the correlation is really high -> need to be scorrelated and then correlated again
327  float s = 0.1;
328 
329  for (std::vector<std::pair<const TrackingRecHit*, float> >::const_iterator ihit = aHitMap.begin();
330  ihit != aHitMap.end();
331  ihit++) {
332  // define variables that will be used to setup the KfComponentsHolder
334  typename AlgebraicROOTObject<N>::Vector r, rMeas;
335  typename AlgebraicROOTObject<N, N>::SymMatrix V, VMeas, Wtemp;
337  const AlgebraicSymMatrix55& C = (tsos.localError().matrix());
338 
339  // setup the holder with the correct dimensions and get the values
340  KfComponentsHolder holder;
341  holder.template setup<N>(&r, &V, &pf, &rMeas, &VMeas, x, C);
342  (ihit->first)->getKfComponents(holder);
343 
344  LogTrace("SiTrackerMultiRecHitUpdator") << "\t position: " << r;
345  LogTrace("SiTrackerMultiRecHitUpdator") << "\t error: " << V;
346 
347  //scorrelation in TID and TEC
348  if (N == 2 && TIDorTEChit(ihit->first)) {
349  V(0, 1) = V(1, 0) = V(0, 1) * s;
350  // V(1,0) = V(1,0)*s;
351  LogTrace("SiTrackerMultiRecHitUpdator") << "\t error scorr: " << V;
352  }
353 
354  bool ierr = invertPosDefMatrix(V);
355  if (!ierr) {
356  edm::LogError("SiTrackerMultiRecHitUpdator") << "SiTrackerMultiRecHitUpdator::calcParameters: V not valid!";
357  }
358  LogTrace("SiTrackerMultiRecHitUpdator") << "\t inverse error: " << V;
359 
360  //compute m_sum and W_sum
361  m_sum += (ihit->second * V * r);
362  W_sum += (ihit->second * V);
363  }
364 
365  bool ierr_sum = invertPosDefMatrix(W_sum);
366  if (!ierr_sum) {
367  edm::LogError("SiTrackerMultiRecHitUpdator") << "SiTrackerMultiRecHitUpdator::calcParameters: W_sum not valid!";
368  }
369 
370  LogTrace("SiTrackerMultiRecHitUpdator") << "\t inverse total error: " << W_sum;
371  typename AlgebraicROOTObject<N>::Vector parameters = W_sum * m_sum;
372  if (N == 1) {
373  position = LocalPoint(parameters(0), 0.f);
374  error = LocalError(W_sum(0, 0), 0.f, std::numeric_limits<float>::max());
375  } else if (N == 2) {
377  //ri-correlation in TID and TEC
378  if (TIDorTEChit(aHitMap.at(0).first))
379  error = LocalError(W_sum(0, 0), W_sum(0, 1) / s, W_sum(1, 1));
380  else
381  error = LocalError(W_sum(0, 0), W_sum(0, 1), W_sum(1, 1));
382  }
383 
384  return std::make_pair(position, error);
385 }
386 
388  DetId hitId = hit->geographicalId();
389 
390  if (hitId.det() == DetId::Tracker &&
391  (hitId.subdetId() == StripSubdetector::TEC || hitId.subdetId() == StripSubdetector::TID)) {
392  return true;
393  }
394 
395  return false;
396 }
static constexpr auto TEC
virtual int dimension() const =0
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
const LocalTrajectoryError & localError() const
ROOT::Math::SMatrix< double, D1, D1, ROOT::Math::MatRepSym< double, D1 > > SymMatrix
virtual const GeomDet & geomDet() const
virtual TransientTrackingRecHit::RecHitPointer update(TransientTrackingRecHit::ConstRecHitPointer original, const TrajectoryStateOnSurface &tsos, MeasurementDetWithData &measDet, double annealing=1.) const
const LocalTrajectoryParameters & localParameters() const
Log< level::Error, false > LogError
TrackingRecHit::ConstRecHitPointer makeShared(SiPixelRecHit const &hit, TrajectoryStateOnSurface const &tsos) const override
Definition: TkClonerImpl.cc:64
const SurfaceType & surface() const
const TrackingRecHitPropagator * theHitPropagator
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46
#define LogTrace(id)
TrackingRecHit::RecHitPointer project(const TrackingRecHit::ConstRecHitPointer hit, const GeomDet &det, const TrajectoryStateOnSurface ts, const TransientTrackingRecHitBuilder *builder) const
U second(std::pair< T, U > const &p)
virtual TransientTrackingRecHit::RecHitPointer buildMultiRecHit(const std::vector< const TrackingRecHit *> &rhv, const TrajectoryStateOnSurface &tsos, MeasurementDetWithData &measDet, float annealing=1.) const
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t V
std::shared_ptr< TrackingRecHit const > ConstRecHitPointer
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
const TransientTrackingRecHitBuilder * theBuilder
AlgebraicVector5 vector() const
bool TIDorTEChit(const TrackingRecHit *const &hit) const
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
ROOT::Math::SVector< double, 5 > AlgebraicVector5
double f[11][100]
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
SiTrackerMultiRecHitUpdator(const TransientTrackingRecHitBuilder *builder, const TrackingRecHitPropagator *hitpropagator, const float Chi2Cut1D, const float Chi2Cut2D, const std::vector< double > &anAnnealingProgram, bool debug)
std::shared_ptr< TrackingRecHit const > RecHitPointer
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: DetId.h:17
std::pair< LocalPoint, LocalError > LocalParameters
#define debug
Definition: HDRShower.cc:19
#define N
Definition: blowfish.cc:9
const PositionType & position() const
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
double ComputeWeight(const TrajectoryStateOnSurface &tsos, const TransientTrackingRecHit &aRecHit, bool CutWeight, double annealing=1.) const
ROOT::Math::SVector< double, D1 > Vector
const MeasurementDet & mdet() const
static std::atomic< unsigned int > counter
static int position[264][3]
Definition: ReadPGInfo.cc:289
const AlgebraicSymMatrix55 & matrix() const
LocalParameters calcParameters(const TrajectoryStateOnSurface &tsos, std::vector< std::pair< const TrackingRecHit *, float > > &aHitMap) const
Definition: Chi2.h:15
tmp
align.sh
Definition: createJobs.py:716
static constexpr auto TID
virtual void getKfComponents(KfComponentsHolder &holder) const