00001 #include "RecoTracker/SiTrackerMRHTools/interface/SiTrackerMultiRecHitUpdatorMTF.h"
00002 #include "RecoTracker/TransientTrackingRecHit/interface/TSiTrackerMultiRecHit.h"
00003 #include "RecoTracker/SiTrackerMRHTools/interface/GenericProjectedRecHit2D.h"
00004 #include "RecoTracker/SiTrackerMRHTools/interface/MultiTrajectoryMeasurement.h"
00005
00006 #include "DataFormats/TrackerRecHit2D/interface/SiTrackerMultiRecHit.h"
00007 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00008 #include "TrackingTools/TransientTrackingRecHit/interface/TrackingRecHitProjector.h"
00009 #include "TrackingTools/TransientTrackingRecHit/interface/InvalidTransientRecHit.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011
00012
00013 SiTrackerMultiRecHitUpdatorMTF::SiTrackerMultiRecHitUpdatorMTF(const TransientTrackingRecHitBuilder* builder,
00014 const TrackingRecHitPropagator* hitpropagator,
00015 const float Chi2Cut,
00016 const std::vector<double>& anAnnealingProgram):
00017 theBuilder(builder),
00018 theHitPropagator(hitpropagator),
00019 theChi2Cut(Chi2Cut),
00020 theAnnealingProgram(anAnnealingProgram){}
00021
00022
00023
00024
00025
00026
00027 TransientTrackingRecHit::RecHitPointer SiTrackerMultiRecHitUpdatorMTF::buildMultiRecHit(TrajectoryStateOnSurface& state,
00028 TransientTrackingRecHit::ConstRecHitContainer& rhv,
00029 MultiTrajectoryMeasurement* mtm,
00030 float annealing) const{
00031
00032
00033
00034
00035
00036 std::vector<TransientTrackingRecHit::RecHitPointer> updatedcomponents = updatecomponents(rhv, state, annealing);
00037
00038
00039
00040
00041
00042 double rowsum = updaterow(rhv, state, updatedcomponents, annealing);
00043
00044
00045 double c = calculatecut(rhv, state, updatedcomponents, annealing);
00046
00047
00048
00049
00050 std::vector<std::pair<TransientTrackingRecHit::RecHitPointer, double> > themap = mapmaker(rhv, state, annealing);
00051
00052
00053
00054
00055
00056
00057
00058 return update(rowsum,mtm,c,themap,updatedcomponents,annealing);
00059
00060 }
00061
00062
00063
00064 double SiTrackerMultiRecHitUpdatorMTF::update(TransientTrackingRecHit::ConstRecHitPointer original,
00065 TrajectoryStateOnSurface tsos,
00066 float annealing) const{
00067
00068
00069 if (original->isValid())
00070 LogTrace("SiTrackerMultiRecHitUpdator") << "Original Hit position " << original->localPosition() << " original error "
00071 << original->parametersError();
00072 else LogTrace("SiTrackerMultiRecHitUpdator") << "Invalid hit";
00073
00074 if(!tsos.isValid()) {
00075
00076 throw cms::Exception("SiTrackerMultiRecHitUpdator") << "!!! MultiRecHitUpdator::update(..): tsos NOT valid!!! ";
00077 }
00078
00079
00080 if (original->transientHits().empty()) return 0;
00081
00082
00083 TransientTrackingRecHit::ConstRecHitContainer tcomponents = original->transientHits();
00084
00085 std::vector<TransientTrackingRecHit::RecHitPointer> updatedcomponents = updatecomponents(tcomponents, tsos, annealing);
00086
00087 return updaterow(tcomponents, tsos, updatedcomponents, annealing);
00088 }
00089
00090
00091
00092
00093
00094
00095 double SiTrackerMultiRecHitUpdatorMTF::updaterow(TransientTrackingRecHit::ConstRecHitContainer& tcomponents,
00096 TrajectoryStateOnSurface& tsos,
00097 std::vector<TransientTrackingRecHit::RecHitPointer>& updatedcomponents,
00098 float annealing) const{
00099
00100 if (tcomponents.empty()){
00101 LogTrace("SiTrackerMultiRecHitUpdator") << "Empty components vector passed to SiTrackerMultiRecHitUpdator::updateraw";
00102 return -1;
00103 }
00104
00105 if(!tsos.isValid()) {
00106 LogTrace("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::updateraw: tsos NOT valid!!!";
00107 return -2;
00108 }
00109
00110
00111
00112
00113
00114
00115 int ierr;
00116
00117 double a_sum=0;
00118
00119 AlgebraicVector2 tsospos;
00120 tsospos[0]=tsos.localPosition().x();
00121 tsospos[1]=tsos.localPosition().y();
00122
00123
00124 for(std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin(); ihit != updatedcomponents.end(); ihit++) {
00125
00126
00127 AlgebraicVector2 r(asSVector<2>((*ihit)->parameters()) - tsospos);
00128 AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
00129 V *= annealing;
00130
00131 AlgebraicSymMatrix22 W(V.Inverse(ierr));
00132 double det;
00133 bool ierr2=!(V.Det2(det));
00134
00135 if(ierr != 0|| ierr2) {
00136 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<<"MultiRecHitUpdator::update: W not valid!"<<std::endl;
00137 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<<"V: "<<V<<" AnnealingFactor: "<<annealing<<std::endl;
00138 }
00139 else{
00140 double Chi2 = ROOT::Math::Similarity(r,W);
00141
00142
00143 double a_i = exp(-0.5*Chi2)/(2.*M_PI*sqrt(det));
00144 a_sum += a_i;
00145 }
00146 }
00147
00148 return a_sum;
00149 }
00150
00151
00152 double SiTrackerMultiRecHitUpdatorMTF::updatecolumn(TransientTrackingRecHit::ConstRecHitPointer trechit,
00153 MultiTrajectoryMeasurement* mtm,
00154 float annealing) const{
00155
00156
00157 if(!trechit->isValid()) {
00158 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<<"SiTrackerMultiRecHitUpdatorMTF::updatecolumn: rechit NOT valid!!!, returning an InvalidTransientRecHit";
00159
00160 }
00161
00162 int ierr;
00163
00164
00165
00166 double a_sum=0;
00167
00168
00169 AlgebraicVector2 hitpos(asSVector<2>(trechit->parameters()));
00170
00171
00172
00173
00174
00175 AlgebraicSymMatrix22 V( asSMatrix<2>(trechit->parametersError()));
00176 V *= annealing;
00177 double det=0;
00178 bool ierr2=!(V.Det2(det));
00179 double denom= (2.*M_PI*sqrt(det));
00180 AlgebraicSymMatrix22 W(V.Inverse(ierr));
00181
00182 if(ierr != 0 ||ierr2) {
00183 edm::LogWarning("SiTrackerMultiRecHitUpdatorMTF")<<"MultiRecHitUpdator::update: W not valid!"<<std::endl;
00184 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<<"V: "<<V<<" AnnealingFactor: "<<annealing<<std::endl;
00185 }
00186 else{
00187 AlgebraicVector2 tsospos;
00188
00189 for(std::map<int,TSOS>::const_iterator imtm=mtm->filteredStates().begin(); imtm!=mtm->filteredStates().end(); imtm++) {
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 tsospos[0]= imtm->second.localPosition().x();
00201 tsospos[1]= imtm->second.localPosition().y();
00202
00203
00204
00205 AlgebraicVector2 r( hitpos - tsospos);
00206
00207
00208
00209
00210
00211
00212 double Chi2 = ROOT::Math::Similarity(r,W);
00213
00214
00215
00216
00217 double a_i = exp(-0.5*Chi2)/denom;
00218 a_sum += a_i;
00219
00220
00221 }
00222 }
00223
00224 return a_sum;
00225
00226 }
00227
00228
00229
00230 double SiTrackerMultiRecHitUpdatorMTF::calculatecut(TransientTrackingRecHit::ConstRecHitContainer& tcomponents,
00231 TrajectoryStateOnSurface& tsos,
00232 std::vector<TransientTrackingRecHit::RecHitPointer>& updatedcomponents,
00233 float annealing) const{
00234
00235 if (tcomponents.empty()){
00236 LogTrace("SiTrackerMultiRecHitUpdator")
00237 << "Empty components rechit vector passed to SiTrackerMultiRecHitUpdator::calculatecut";
00238 return -1;
00239 }
00240
00241 if(!tsos.isValid()) {
00242 LogTrace("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::calculatecut: tsos NOT valid!!!";
00243 return -2;
00244 }
00245
00246
00247
00248
00249 double c_sum=0;
00250 int ierr;
00251 AlgebraicVector2 tsospos;
00252 tsospos[0]=tsos.localPosition().x();
00253 tsospos[1]=tsos.localPosition().y();
00254
00255
00256 for(std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin(); ihit != updatedcomponents.end(); ihit++) {
00257
00258 AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
00259 V *= annealing;
00260
00261 AlgebraicSymMatrix22 W(V.Inverse(ierr));
00262 double det=0;
00263 bool ierr2=!(V.Det2(det));
00264
00265 if(ierr != 0||ierr2) {
00266 LogTrace("SiTrackerMultiRecHitUpdator")<<"MultiRecHitUpdator::calculatecut: W not valid!"<<std::endl;
00267 LogTrace("SiTrackerMultiRecHitUpdator")<<"V: "<<V<<" AnnealingFactor: "<<annealing<<std::endl;
00268 }
00269 else{
00270
00271
00272 LogTrace("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::calculatecut: det= "<<det<<std::endl;
00273 double c_i = exp(-0.5*theChi2Cut/annealing)/(2.*M_PI*sqrt(det));
00274 c_sum += c_i;
00275 }
00276 }
00277 LogTrace("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::calculatecut: csum= "<<c_sum<<std::endl;
00278 return c_sum;
00279
00280
00281 }
00282
00283 std::vector< std::pair<TransientTrackingRecHit::RecHitPointer, double> >
00284 SiTrackerMultiRecHitUpdatorMTF::mapmaker(TransientTrackingRecHit::ConstRecHitContainer& tcomponents,
00285 TrajectoryStateOnSurface& tsos,
00286 float annealing) const{
00287 if (tcomponents.empty()){
00288 LogTrace("SiTrackerMultiRecHitUpdator")
00289 << "Empty components vector passed to SiTrackerMultiRecHitUpdator::update, returning an InvalidTransientRecHit ";
00290
00291 }
00292
00293 if(!tsos.isValid()) {
00294 LogTrace("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::update: tsos NOT valid!!!, returning an InvalidTransientRecHit";
00295
00296 }
00297
00298 std::vector<TransientTrackingRecHit::RecHitPointer> updatedcomponents = updatecomponents(tcomponents, tsos,annealing);
00299
00300 int ierr;
00301 std::vector<std::pair<TransientTrackingRecHit::RecHitPointer, double> > mymap;
00302
00303
00304 AlgebraicVector2 tsospos;
00305 tsospos[0]=tsos.localPosition().x();
00306 tsospos[1]=tsos.localPosition().y();
00307 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<< "TSOS position " << tsos.localPosition();
00308 for(std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin(); ihit != updatedcomponents.end(); ihit++) {
00309 AlgebraicVector2 r(asSVector<2>((*ihit)->parameters()) - tsospos);
00310 AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
00311 V *= annealing;
00312
00313 AlgebraicSymMatrix22 W(V.Inverse(ierr));
00314 double det=0;
00315 bool ierr2=!(V.Det2(det));
00316 double a_i=0;
00317 if(ierr != 0||ierr2) {
00318 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<<"MultiRecHitUpdator::update: W not valid!"<<std::endl;
00319 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<<"V: "<<V<<" AnnealingFactor: "<<annealing<<std::endl;
00320
00321 }
00322 else{
00323 double Chi2 =ROOT::Math::Similarity(r,W);
00324
00325 a_i=exp(-0.5*Chi2)/(2.*M_PI*sqrt(det));
00326 }
00327 mymap.push_back(std::pair<TransientTrackingRecHit::RecHitPointer, float>((*ihit), a_i));
00328
00329
00330 }
00331
00332 LogDebug("SiTrackerMultiRecHitUpdatorMTF") << "Map mymap size: " << mymap.size() << std::endl
00333 << "Position 1st element: " << mymap.front().first->localPosition()<<std::endl;
00334
00335
00336 return mymap;
00337 }
00338
00339 std::vector<TransientTrackingRecHit::RecHitPointer>
00340 SiTrackerMultiRecHitUpdatorMTF::updatecomponents(TransientTrackingRecHit::ConstRecHitContainer& tcomponents,
00341 TrajectoryStateOnSurface& tsos,
00342 float annealing) const{
00343
00344 if (tcomponents.empty()){
00345 LogTrace("SiTrackerMultiRecHitUpdator")
00346 << "Empty components vector passed to SiTrackerMultiRecHitUpdatorMTF::updatecomponents, returning an InvalidTransientRecHit ";
00347
00348 }
00349
00350 if(!tsos.isValid())
00351 {
00352 LogTrace("SiTrackerMultiRecHitUpdatorMTF")
00353 <<"SiTrackerMultiRecHitUpdatorMTF::updatecomponents: tsos NOT valid!!!, returning an InvalidTransientRecHit";
00354
00355 }
00356
00357 std::vector<TransientTrackingRecHit::RecHitPointer> updatedcomponents;
00358 const GeomDet* geomdet = 0;
00359 for (TransientTrackingRecHit::ConstRecHitContainer::const_iterator iter = tcomponents.begin(); iter != tcomponents.end(); iter++){
00360 if (iter == tcomponents.begin()) {
00361
00362 if (&((*iter)->det()->surface())!=&(tsos.surface())){
00363 throw cms::Exception("SiTrackerMultiRecHitUpdatorMTF")
00364 << "the Trajectory state and the first rechit passed to the SiTrackerMultiRecHitUpdatorMTF lay on different surfaces!: state lays on surface "
00365 << tsos.surface().position()
00366 << "hit with detid "
00367 << (*iter)->det()->geographicalId().rawId()
00368 << "lays on surface "
00369 << (*iter)->det()->surface().position();
00370 }
00371 geomdet = (*iter)->det();
00372
00373 }
00374 if (&((*iter)->det()->surface())!=&(tsos.surface())){
00375 TransientTrackingRecHit::RecHitPointer cloned = theHitPropagator->project<GenericProjectedRecHit2D>(*iter, *geomdet, tsos);
00376 LogTrace("SiTrackerMultiRecHitUpdatorMTF") << "Projecting a hit from surface " << (*iter)->det()->surface().position()
00377 << " to surface " << tsos.surface().position() << " original global position "
00378 << (*iter)->globalPosition() << " projected position " << cloned->globalPosition();
00379 if (cloned->isValid()) updatedcomponents.push_back(cloned);
00380 } else {
00381
00382
00383
00384
00385 LogTrace("SiTrackerMultiRecHitUpdatorMTF") << "about to clone the tracking rechit with the method clone " << std::endl;
00386 TransientTrackingRecHit::RecHitPointer cloned = (*iter)->clone(tsos);
00387 LogTrace("SiTrackerMultiRecHitUpdatorMTF") << "cloned";
00388 LogTrace("SiTrackerMultiRecHitUpdatorMTF") <<"Original position: "<<(*iter)->localPosition()<<std::endl;
00389 LogTrace("SiTrackerMultiRecHitUpdatorMTF") <<"New position: "<<cloned->localPosition()<<std::endl;
00390
00391 if (cloned->isValid()) updatedcomponents.push_back(cloned);
00392 }
00393 }
00394
00395 return updatedcomponents;
00396 }
00397
00398
00399 TransientTrackingRecHit::RecHitPointer
00400 SiTrackerMultiRecHitUpdatorMTF::update(double rowsum,
00401 MultiTrajectoryMeasurement* mtm,
00402 double c,
00403 std::vector<std::pair<TransientTrackingRecHit::RecHitPointer,double> >& mymap,
00404 std::vector<TransientTrackingRecHit::RecHitPointer>& updatedcomponents,
00405 float annealing) const{
00406
00407
00408
00409
00410
00411 unsigned int counter = 0;
00412 TransientTrackingRecHit::ConstRecHitContainer finalcomponents;
00413
00414 std::vector<std::pair<TransientTrackingRecHit::RecHitPointer, double> > normmap;
00415 const GeomDet* geomdet = updatedcomponents.front()->det();
00416 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<<"Multirechit size"<<updatedcomponents.size()<<std::endl;
00417 for(std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin(); ihit != updatedcomponents.end(); ihit++) {
00418
00419
00420
00421
00422
00423
00424
00425
00426 double colsum = updatecolumn(*ihit, mtm, annealing);
00427 double total_sum = rowsum + colsum;
00428 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<<"rowsum= "<<rowsum<<", colsum= "<<colsum<<std::endl;
00429 LogTrace("SiTrackerMultiRecHitUpdatorMTF")<<"phi_i_j= "<<mymap[counter].second<<", c="<<c<<std::endl;
00430
00431 double p = ( (mymap[counter].second)/(total_sum - mymap[counter].second + c) > 1.e-6 ? (mymap[counter].second)/(total_sum- mymap[counter].second +c) : 1.e-6);
00432
00433
00434
00435
00436
00437 normmap.push_back(std::pair<TransientTrackingRecHit::RecHitPointer, double>(mymap[counter].first, p) );
00438
00439
00440
00441
00442
00443
00444 (*ihit)->setWeight(p);
00445
00446
00447 (*ihit)->setAnnealingFactor(annealing);
00448
00449
00450 finalcomponents.push_back(*ihit);
00451
00452
00453
00454
00455 LogDebug("SiTrackerMultiRecHitUpdatorMTF")<<"Component hit type "<< typeid(*(normmap[counter].first)).name() <<std::endl
00456
00457 << "position " << mymap[counter].first->localPosition()<<std::endl
00458 << "error " << mymap[counter].first->localPositionError()<<std::endl
00459 << "with weight " <<p<<std::endl;
00460
00461 counter++;
00462
00463 }
00464
00465
00466
00467 mymap = normmap;
00468 SiTrackerMultiRecHitUpdatorMTF::LocalParameters param=calcParameters(finalcomponents);
00469
00470
00471
00472
00473
00474 std::vector<std::pair<const TrackingRecHit*,float> > newmap;
00475
00476
00477 for(std::vector<std::pair<TransientTrackingRecHit::RecHitPointer,double> >::iterator imap=mymap.begin(); imap!=mymap.end(); imap++)
00478 {
00479
00480 newmap.push_back(std::pair<const TrackingRecHit*, float>(imap->first->hit(),imap->second));
00481
00482 }
00483
00484
00485
00486
00487
00488 SiTrackerMultiRecHit updatedmrh(param.first,param.second, newmap.front().first->geographicalId(), newmap);
00489
00490 LogDebug("SiTrackerMultiRecHitUpdatorMTF") << "SiTrackerMultiRecHit built " << std::endl;
00491
00492 LogDebug("SiTrackerMultiRecHitUpdatorMTF") << "Updated Hit position "
00493 << updatedmrh.localPosition() << " updated error "
00494 << updatedmrh.parametersError() << std::endl;
00495
00496
00497
00498 return TSiTrackerMultiRecHit::build(geomdet, &updatedmrh, finalcomponents, annealing);
00499
00500 }
00501
00502 SiTrackerMultiRecHitUpdatorMTF::LocalParameters SiTrackerMultiRecHitUpdatorMTF::calcParameters(TransientTrackingRecHit::ConstRecHitContainer& map)const{
00503 AlgebraicSymMatrix22 W_sum;
00504 AlgebraicVector2 m_sum;
00505 int ierr;
00506 for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator ihit = map.begin(); ihit != map.end(); ihit ++) {
00507 AlgebraicVector2 m(asSVector<2>((*ihit)->parameters()));
00508 AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
00509 AlgebraicSymMatrix22 W(V.Inverse(ierr));
00510
00511 if(ierr != 0) {
00512 edm::LogError("SiTrackerMultiRecHitUpdator")<<"MultiRecHit::checkParameters: W not valid!"<<std::endl;
00513 }
00514
00515 else {
00516 W_sum += ((*ihit)->weight()*W);
00517 m_sum += ((*ihit)->weight()*(W*m));
00518 }
00519 }
00520 AlgebraicSymMatrix22 V_sum= W_sum.Inverse(ierr);
00521 AlgebraicVector2 parameters = V_sum*m_sum;
00522 LocalError error=LocalError(V_sum(0,0), V_sum(0,1), V_sum(1,1));
00523 LocalPoint position=LocalPoint(parameters(0), parameters(1));
00524 return std::make_pair(position,error);
00525 }
00526
00527
00528 LocalError SiTrackerMultiRecHitUpdatorMTF::calcParametersError(TransientTrackingRecHit::ConstRecHitContainer& map) const {
00529 AlgebraicSymMatrix22 W_sum;
00530 int ierr;
00531 for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator ihit = map.begin(); ihit != map.end(); ihit ++) {
00532 AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
00533 AlgebraicSymMatrix22 W(V.Inverse(ierr));
00534
00535 if(ierr != 0) {
00536 std::cout<<"MultiRecHit::checkParametersError: W not valid!"<<std::endl;
00537 }
00538
00539 else W_sum += ((*ihit)->weight()*W);
00540 }
00541 AlgebraicSymMatrix22 parametersError = W_sum.Inverse(ierr);
00542 return LocalError(parametersError(0,0), parametersError(0,1), parametersError(1,1));
00543 }
00544
00545 LocalPoint SiTrackerMultiRecHitUpdatorMTF::calcParameters(TransientTrackingRecHit::ConstRecHitContainer& map, const LocalError& er) const {
00546 AlgebraicVector2 m_sum;
00547 int ierr;
00548 for( TransientTrackingRecHit::ConstRecHitContainer::const_iterator ihit = map.begin(); ihit != map.end(); ihit ++) {
00549 AlgebraicVector2 m(asSVector<2>((*ihit)->parameters()));
00550 AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
00551 AlgebraicSymMatrix22 W(V.Inverse(ierr));
00552
00553 if(ierr != 0) {
00554 std::cout<<"MultiRecHit::checkParameters: W not valid!"<<std::endl;
00555 }
00556
00557
00558 else m_sum += ((*ihit)->weight()*(W*m));
00559 }
00560 AlgebraicSymMatrix22 V_sum;
00561
00562 V_sum(0,0) = er.xx();
00563 V_sum(0,1) = er.xy();
00564 V_sum(1,1) = er.yy();
00565
00566 AlgebraicVector2 parameters = V_sum*m_sum;
00567 return LocalPoint(parameters(0), parameters(1));
00568 }
00569