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.
11 
13  const TrackingRecHitPropagator* hitpropagator,
14  const float Chi2Cut,
15  const std::vector<double>& anAnnealingProgram,
16  bool debug):
17  theBuilder(builder),
18  theHitPropagator(hitpropagator),
19  theChi2Cut(Chi2Cut),
20  theAnnealingProgram(anAnnealingProgram),
21  debug_(debug){
22  theHitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(builder)->cloner();
23  }
24 
25 
27  const TrajectoryStateOnSurface& tsos,
28  float annealing) const{
29  LogTrace("SiTrackerMultiRecHitUpdator") << "Calling SiTrackerMultiRecHitUpdator::buildMultiRecHit with AnnealingFactor: " << annealing;
31  for (std::vector<const TrackingRecHit*>::const_iterator iter = rhv.begin(); iter != rhv.end(); iter++){
33  if (transient->isValid()) tcomponents.push_back(transient);
34  }
35  return update(tcomponents, tsos, annealing);
36 
37 }
38 
40  const TrajectoryStateOnSurface& tsos,
41  double annealing) const{
42  LogTrace("SiTrackerMultiRecHitUpdator") << "Calling SiTrackerMultiRecHitUpdator::update with AnnealingFactor: " << annealing;
43 
44  if(!tsos.isValid()) {
45  //return original->clone();
46  throw cms::Exception("SiTrackerMultiRecHitUpdator") << "!!! MultiRecHitUpdator::update(..): tsos NOT valid!!! ";
47  }
48 
49  //check if to clone is the right thing
50  if(original->isValid()){
51  if (original->transientHits().empty()){
52  return theHitCloner.makeShared(original,tsos);
53  }
54  } else {
55  return theHitCloner.makeShared(original,tsos);
56  }
57 
58  TransientTrackingRecHit::ConstRecHitContainer tcomponents = original->transientHits();
59  return update(tcomponents, tsos, annealing);
60 }
61 
62 /*------------------------------------------------------------------------------------------------------------------------*/
64  const TrajectoryStateOnSurface& tsos,
65  double annealing) const{
66 
67  if (tcomponents.empty()){
68  LogTrace("SiTrackerMultiRecHitUpdator") << "Empty components vector passed to SiTrackerMultiRecHitUpdator::update, returning an InvalidTransientRecHit ";
69  return std::make_shared<InvalidTrackingRecHitNoDet>();
70  }
71 
72  if(!tsos.isValid()) {
73  LogTrace("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::update: tsos NOT valid!!!, returning an InvalidTransientRecHit";
74  return std::make_shared<InvalidTrackingRecHitNoDet>();
75  }
76 
77  std::vector<TransientTrackingRecHit::RecHitPointer> updatedcomponents;
78  const GeomDet* geomdet = 0;
79 
80  //running on all over the MRH components
81  for (TransientTrackingRecHit::ConstRecHitContainer::const_iterator iter = tcomponents.begin(); iter != tcomponents.end(); iter++){
82 
83  //the first rechit must belong to the same surface of TSOS
84  if (iter == tcomponents.begin()) {
85 
86  if (&((*iter)->det()->surface())!=&(tsos.surface())){
87  throw cms::Exception("SiTrackerMultiRecHitUpdator") << "the Trajectory state and the first rechit passed to the SiTrackerMultiRecHitUpdator lay on different surfaces!: state lays on surface " << tsos.surface().position() << " hit with detid " << (*iter)->det()->geographicalId().rawId() << " lays on surface " << (*iter)->det()->surface().position();
88  }
89 
90  geomdet = (*iter)->det();
91 
92  }
93 
94  //if the rechit does not belong to the surface of the tsos
95  //GenericProjectedRecHit2D is used to prepagate
96  if (&((*iter)->det()->surface())!=&(tsos.surface())){
97 
99  //if it is used a sensor by sensor grouping this should not appear
100  if (cloned->isValid()) updatedcomponents.push_back(cloned);
101 
102  } else {
104  if (cloned->isValid()){
105  updatedcomponents.push_back(cloned);
106  }
107  }
108  }
109 
110  std::vector<std::pair<const TrackingRecHit*, float> > mymap;
111  std::vector<std::pair<const TrackingRecHit*, float> > normmap;
112 
113  double a_sum=0, c_sum=0;
114 
115 
116  for(std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin();
117  ihit != updatedcomponents.end(); ihit++) {
118 
119  double a_i = ComputeWeight(tsos, *(*ihit), false, annealing); //exp(-0.5*Chi2)/(2.*M_PI*sqrt(det));
120  double c_i = ComputeWeight(tsos, *(*ihit), true, annealing); //exp(-0.5*theChi2Cut/annealing)/(2.*M_PI*sqrt(det));
121  mymap.push_back(std::pair<const TrackingRecHit*, float>((*ihit)->hit(), a_i));
122 
123  a_sum += a_i;
124  c_sum += c_i;
125  }
126  double total_sum = a_sum + c_sum;
127 
128  unsigned int counter = 0;
129  for(std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin();
130  ihit != updatedcomponents.end(); ihit++) {
131 
132  double p = ((mymap[counter].second)/total_sum > 1.e-6 ? (mymap[counter].second)/total_sum : 1.e-6);
133  //ORCA: float p = ((mymap[counter].second)/total_sum > 0.01 ? (mymap[counter].second)/total_sum : 1.e-6);
134  normmap.push_back(std::pair<const TrackingRecHit*,float>(mymap[counter].first, p));
135 
136  LogTrace("SiTrackerMultiRecHitUpdator")<< " Component hit type " << typeid(*mymap[counter].first).name()
137  << " position " << mymap[counter].first->localPosition()
138  << " error " << mymap[counter].first->localPositionError()
139  << " with weight " << p;
140  counter++;
141  }
142 
144 
145  SiTrackerMultiRecHit updated(param.first, param.second, *normmap.front().first->det(), normmap, annealing);
146  LogTrace("SiTrackerMultiRecHitUpdator") << " Updated Hit position " << updated.localPosition()
147  << " updated error " << updated.localPositionError() << std::endl;
148 
149  return std::make_shared<SiTrackerMultiRecHit>(param.first, param.second, *normmap.front().first->det(), normmap, annealing);
150 }
151 
152 
153 //---------------------------------------------------------------------------------------------------------------
155  const TransientTrackingRecHit& aRecHit, bool CutWeight, double annealing) const{
156 
157  switch (aRecHit.dimension()) {
158  case 1: return ComputeWeight<1>(tsos,aRecHit,CutWeight,annealing);
159  case 2: return ComputeWeight<2>(tsos,aRecHit,CutWeight,annealing);
160  case 3: return ComputeWeight<3>(tsos,aRecHit,CutWeight,annealing);
161  case 4: return ComputeWeight<4>(tsos,aRecHit,CutWeight,annealing);
162  case 5: return ComputeWeight<5>(tsos,aRecHit,CutWeight,annealing);
163  }
164  throw cms::Exception("Rec hit of invalid dimension (not 1,2,3,4,5)") <<
165  "The value was " << aRecHit.dimension() <<
166  ", type is " << typeid(aRecHit).name() << "\n";
167 }
168 
169 //---------------------------------------------------------------------------------------------------------------
170 template <unsigned int N>
172  const TransientTrackingRecHit& aRecHit, bool CutWeight, double annealing) const {
173 
174  //define tsos position depending on the dimension of the hit
175  typename AlgebraicROOTObject<N>::Vector tsospos;
176  if( N==1 ){
177  tsospos[0]=tsos.localPosition().x();
178  if(!CutWeight)
179  LogTrace("SiTrackerMultiRecHitUpdator")<< " TSOS position " << tsos.localPosition();
180  }
181  else if(N==2){
182  tsospos[0]=tsos.localPosition().x();
183  tsospos[1]=tsos.localPosition().y();
184  if(!CutWeight)
185  LogTrace("SiTrackerMultiRecHitUpdator")<< " TSOS position " << tsos.localPosition();
186  }
187  else{
188  if(!CutWeight)
189  LogTrace("SiTrackerMultiRecHitUpdator")<< " TSOS position not correct. Rec hit of invalid dimension (not 1, 2) but "
190  << aRecHit.dimension();
191  }
192 
193  // define variables that will be used to setup the KfComponentsHolder
195  typename AlgebraicROOTObject<N>::Vector r, rMeas;
196  typename AlgebraicROOTObject<N,N>::SymMatrix V, VMeas, W;
198  const AlgebraicSymMatrix55 &C = (tsos.localError().matrix());
199 
200  // setup the holder with the correct dimensions and get the values
201  KfComponentsHolder holder;
202  holder.template setup<N>(&r, &V, &pf, &rMeas, &VMeas, x, C);
203  aRecHit.getKfComponents(holder);
204 
206  diff = r - tsospos;
207 
208  //assume that TSOS is smoothed one
209  V *= annealing;
210  W = V;
211  //V += me.measuredError(*ihit);// result = b*V + H*C*H.T()
212 
213  //ierr will be set to true when inversion is successfull
214  bool ierr = invertPosDefMatrix(W);
215 
216  //Det2 method will preserve the content of the Matrix
217  //and return true when the calculation is successfull
218  double det;
219  bool ierr2 = V.Det2(det);
220 
221  if( !ierr || !ierr2) {
222  LogTrace("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::ComputeWeight: W not valid!"<<std::endl;
223  LogTrace("SiTrackerMultiRecHitUpdator")<<"V: "<<V<<" AnnealingFactor: "<<annealing<<std::endl;
224  }
225 
226  double Chi2 = ROOT::Math::Similarity(diff,W);// Chi2 = diff.T()*W*diff
227 
228  double temp_weight;
229  if( CutWeight ) temp_weight = exp(-0.5*theChi2Cut/annealing)/(2.*M_PI*sqrt(det));
230  else temp_weight = exp(-0.5*Chi2)/(2.*M_PI*sqrt(det));
231 
232  return temp_weight;
233 
234 }
235 
236 //-----------------------------------------------------------------------------------------------------------
237 SiTrackerMultiRecHitUpdator::LocalParameters SiTrackerMultiRecHitUpdator::calcParameters(const TrajectoryStateOnSurface& tsos, std::vector<std::pair<const TrackingRecHit*, float> >& aHitMap) const{
238 
239  AlgebraicSymMatrix22 W_sum;
240  AlgebraicVector2 m_sum;
241 
242  for(std::vector<std::pair<const TrackingRecHit*, float> >::const_iterator ihit = aHitMap.begin(); ihit != aHitMap.end(); ihit++){
243 
244  std::pair<AlgebraicVector2,AlgebraicSymMatrix22> PositionAndError22;
245  PositionAndError22 = ComputeParameters2dim(tsos, *(ihit->first));
246 
247  W_sum += (ihit->second*PositionAndError22.second);
248  m_sum += (ihit->second*(PositionAndError22.second*PositionAndError22.first));
249 
250  }
251 
252  AlgebraicSymMatrix22 V_sum = W_sum;
253  bool ierr = invertPosDefMatrix(V_sum);
254  if( !ierr ) {
255  edm::LogError("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::calcParameters: V_sum not valid!"<<std::endl;
256  }
257 
258  AlgebraicVector2 parameters = V_sum*m_sum;
259  LocalError error = LocalError(V_sum(0,0), V_sum(0,1), V_sum(1,1));
261 
262  return std::make_pair(position,error);
263 }
264 
265 //-----------------------------------------------------------------------------------------------------------
266 std::pair<AlgebraicVector2,AlgebraicSymMatrix22> SiTrackerMultiRecHitUpdator::ComputeParameters2dim(const TrajectoryStateOnSurface& tsos,
267  const TransientTrackingRecHit& aRecHit) const{
268 
269  switch (aRecHit.dimension()) {
270  case 2: return ComputeParameters2dim<2>(tsos,aRecHit);
271  //avoiding the not-2D hit due to the matrix final sum
272  case ( 1 || 3 || 4 || 5 ):{
273  AlgebraicVector2 dummyVector2D (0.0,0.0);
274  AlgebraicSymMatrix22 dummyMatrix2D;
275  dummyMatrix2D(0,0) = dummyMatrix2D(1,0) = dummyMatrix2D(1,1) = 0.0;
276  return std::make_pair(dummyVector2D,dummyMatrix2D);
277  }
278  }
279  throw cms::Exception("Rec hit of invalid dimension (not 1,2,3,4,5)") <<
280  "The value was " << aRecHit.dimension() <<
281  ", type is " << typeid(aRecHit).name() << "\n";
282 }
283 
284 //---------------------------------------------------------------------------------------------------------------
285 template <unsigned int N>
286 std::pair<AlgebraicVector2,AlgebraicSymMatrix22> SiTrackerMultiRecHitUpdator::ComputeParameters2dim(const TrajectoryStateOnSurface& tsos,
287  const TransientTrackingRecHit& aRecHit) const {
288 
289  // define variables that will be used to setup the KfComponentsHolder
291  typename AlgebraicROOTObject<N>::Vector r, rMeas;
292  typename AlgebraicROOTObject<N,N>::SymMatrix V, VMeas, Wtemp;
294  const AlgebraicSymMatrix55 &C = (tsos.localError().matrix());
295 
296  // setup the holder with the correct dimensions and get the values
297  KfComponentsHolder holder;
298  holder.template setup<N>(&r, &V, &pf, &rMeas, &VMeas, x, C);
299  aRecHit.getKfComponents(holder);
300 
301  Wtemp = V;
302  bool ierr = invertPosDefMatrix(Wtemp);
303  if( !ierr ) {
304  edm::LogError("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::ComputeParameters2dim: W not valid!"<<std::endl;
305  }
306 
307  // moving to N dimensions to 2 dimensions
308  AlgebraicVector2 m2dim;
309  AlgebraicSymMatrix22 W2dim;
310 
311  m2dim(0) = r(0);
312  m2dim(1) = r(1);
313 
314  W2dim(0,0) = Wtemp(0,0);
315  W2dim(1,0) = Wtemp(1,0);
316  W2dim(1,1) = Wtemp(1,1);
317 
318  return std::make_pair(m2dim,W2dim);
319 
320 }
321 
322 //---------------------------------------------------------------------------------------------------------------
323 /* Obsolete methods */
324 /*LocalError SiTrackerMultiRecHitUpdator::calcParametersError(TransientTrackingRecHit::ConstRecHitContainer& map) const {
325  AlgebraicSymMatrix22 W_sum;
326  int ierr;
327  for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator ihit = map.begin(); ihit != map.end(); ihit ++) {
328  AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
329  AlgebraicSymMatrix22 W(V.Inverse(ierr));
330 
331  if(ierr != 0) {
332  edm::LogError("SiTrackerMultiRecHitUpdator")<<"MultiRecHit::checkParametersError: W not valid!"<<std::endl;
333  }
334 
335  else W_sum += ((*ihit)->weight()*W);
336  }
337  AlgebraicSymMatrix22 parametersError = W_sum.Inverse(ierr);
338  return LocalError(parametersError(0,0), parametersError(0,1), parametersError(1,1));
339 }
340 
341 LocalPoint SiTrackerMultiRecHitUpdator::calcParameters(TransientTrackingRecHit::ConstRecHitContainer& map, const LocalError& er) const {
342  AlgebraicVector2 m_sum;
343  int ierr;
344  for( TransientTrackingRecHit::ConstRecHitContainer::const_iterator ihit = map.begin(); ihit != map.end(); ihit ++) {
345  AlgebraicVector2 m(asSVector<2>((*ihit)->parameters()));
346  AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
347  AlgebraicSymMatrix22 W(V.Inverse(ierr));
348 
349  if(ierr != 0) {
350  edm::LogError("SiTrackerMultiRecHitUpdator")<<"MultiRecHit::checkParameters: W not valid!"<<std::endl;
351  }
352 
353  //m_sum += ihit->weight()*(W*m);
354  else m_sum += ((*ihit)->weight()*(W*m));
355  }
356  AlgebraicSymMatrix22 V_sum;
357 
358  V_sum(0,0) = er.xx();
359  V_sum(0,1) = er.xy();
360  V_sum(1,1) = er.yy();
361  //AlgebraicSymMatrix V_sum(parametersError());
362  AlgebraicVector2 parameters = V_sum*m_sum;
363  return LocalPoint(parameters(0), parameters(1));
364 }
365 */
virtual int dimension() const =0
dictionary parameters
Definition: Parameters.py:2
virtual void getKfComponents(KfComponentsHolder &holder) const
std::pair< AlgebraicVector2, AlgebraicSymMatrix22 > ComputeParameters2dim(const TrajectoryStateOnSurface &tsos, const TransientTrackingRecHit &aRecHit) const
ROOT::Math::SMatrix< double, D1, D1, ROOT::Math::MatRepSym< double, D1 > > SymMatrix
ROOT::Math::SMatrix< double, 2, 2, ROOT::Math::MatRepSym< double, 2 > > AlgebraicSymMatrix22
const LocalTrajectoryParameters & localParameters() const
virtual TransientTrackingRecHit::RecHitPointer update(TransientTrackingRecHit::ConstRecHitPointer original, const TrajectoryStateOnSurface &tsos, double annealing=1.) const
list original
Definition: definitions.py:57
T y() const
Definition: PV3DBase.h:63
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
U second(std::pair< T, U > const &p)
AlgebraicVector5 vector() const
virtual TransientTrackingRecHit::RecHitPointer buildMultiRecHit(const std::vector< const TrackingRecHit * > &rhv, const TrajectoryStateOnSurface &tsos, float annealing=1.) const
const SurfaceType & surface() const
tuple normmap
Definition: lumiCalc2.py:361
std::shared_ptr< TrackingRecHit const > ConstRecHitPointer
T sqrt(T t)
Definition: SSEVec.h:48
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
const TransientTrackingRecHitBuilder * theBuilder
SiTrackerMultiRecHitUpdator(const TransientTrackingRecHitBuilder *builder, const TrackingRecHitPropagator *hitpropagator, const float Chi2Cut, const std::vector< double > &anAnnealingProgram, bool debug)
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
const AlgebraicSymMatrix55 & matrix() const
const LocalTrajectoryError & localError() const
#define LogTrace(id)
#define M_PI
std::shared_ptr< TrackingRecHit const > RecHitPointer
std::vector< ConstRecHitPointer > ConstRecHitContainer
ROOT::Math::SVector< double, D1 > Vector
#define debug
Definition: HDRShower.cc:19
#define N
Definition: blowfish.cc:9
ROOT::Math::SVector< double, 5 > AlgebraicVector5
static std::atomic< unsigned int > counter
static int position[264][3]
Definition: ReadPGInfo.cc:509
Local3DPoint LocalPoint
Definition: LocalPoint.h:11
TrackingRecHit::RecHitPointer project(const TrackingRecHit::ConstRecHitPointer hit, const GeomDet &det, const TrajectoryStateOnSurface ts, const TransientTrackingRecHitBuilder *builder) const
Definition: DDAxes.h:10
Definition: Chi2.h:17
T x() const
Definition: PV3DBase.h:62
const PositionType & position() const
LocalParameters calcParameters(const TrajectoryStateOnSurface &tsos, std::vector< std::pair< const TrackingRecHit *, float > > &aHitMap) const
ROOT::Math::SVector< double, 2 > AlgebraicVector2