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