CMS 3D CMS Logo

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