CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/RecoTracker/SiTrackerMRHTools/src/SiTrackerMultiRecHitUpdatorMTF.cc

Go to the documentation of this file.
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 //#include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHitBuilder.h"
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 //theAnnealingStep(0),
00022 //theIsUpdating(true){}
00023 
00024 
00025 
00026 //modified respect to the DAF, to get an mtm, and from this one the vector of tsos to put the trajectories in competition for the hit
00027 TransientTrackingRecHit::RecHitPointer SiTrackerMultiRecHitUpdatorMTF::buildMultiRecHit(TrajectoryStateOnSurface& state, 
00028                                                                                         TransientTrackingRecHit::ConstRecHitContainer& rhv,
00029                                                                                         MultiTrajectoryMeasurement* mtm,
00030                                                                                         float annealing) const{
00031   
00032   //LogDebug("SiTrackerMultiRechitUpdatorMTF") << "Vector pushed back " << td::endl;
00033   
00034         
00035         //get the other variables needed to do the update
00036         std::vector<TransientTrackingRecHit::RecHitPointer> updatedcomponents = updatecomponents(rhv, state, annealing);
00037         
00038 
00039         //      LogDebug("SiTrackerMultiRechitUpdatorMTF") << "built a vector (updated) of size: "<<updatedcomponents.size() 
00040         //                                 << std::endl;
00041           
00042         double rowsum = updaterow(rhv, state, updatedcomponents, annealing);
00043         //if (rowsum==-1)return an invalid hit?
00044         //float columnsum = updatecolumn(hit, mtm, annealing);
00045         double c = calculatecut(rhv, state, updatedcomponents, annealing);
00046         //LogDebug("SiTrackerMultiRechitUpdatorMTF") <<" rowsum = " << rowsum <<std::endl
00047         //                                         <<" columnsum = "<<columnsum<<std::endl
00048         //                                         <<" c = "<<c<<std::endl;
00049         
00050         std::vector<std::pair<TransientTrackingRecHit::RecHitPointer, double> > themap = mapmaker(rhv, state, annealing);
00051         //LogDebug("SiTrackerMultiRechitUpdatorMTF") << " made a map of "<<themap.size()<<"components" 
00052         //                                         << std::endl;
00053 
00054         //LogDebug("SiTrackerMultiRechitUpdatorMTF") << " local position "<<themap.front().first->localPosition() 
00055         //                                         << std::endl;
00056         
00057         //returns a vector updated given 6 variables
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   //  LogTrace("SiTrackerMultiRecHitUpdator") << "Calling SiTrackerMultiRecHitUpdator::update with AnnealingFactor: "  << annealing;
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     //return original->clone();
00076     throw cms::Exception("SiTrackerMultiRecHitUpdator") << "!!! MultiRecHitUpdator::update(..): tsos NOT valid!!! ";
00077   }     
00078   
00079   //check if to clone is the right thing
00080   if (original->transientHits().empty()) return 0;
00081     //return original->clone(tsos);
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 //this method calculate the DAF weight. It is only a part of the MTF weight (we call it the updaterow method) when the tsos is fix and the weight of each
00094 //hit respect to the tsos is computed
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   //a loop to propagate all the hits on the same surface...
00113   //std::vector<TransientTrackingRecHit::RecHitPointer> updatedcomponents = updatecomponents(tcomponents,tsos,annealing);
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   //  LogDebug("SiTrackerMultiRecHitUpdatorMTF")<<  "TSOS position " << tsos.localPosition(); 
00123   
00124   for(std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin(); ihit != updatedcomponents.end(); ihit++) {
00125     
00126     //    LogDebug("SiTrackerMultiRecHitUpdatorMTF")<<  "hit position " << (*ihit)->localPosition();
00127     AlgebraicVector2 r(asSVector<2>((*ihit)->parameters()) - tsospos);
00128     AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
00129     V *= annealing;//assume that TSOS is smoothed one
00130     //V += me.measuredError(*ihit);// result = b*V + H*C*H.T()
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);// Chi2 = r.T()*W*r
00141       //LogDebug("SiTrackerMultiRecHitUpdatorMTF")<<"Chi2 (updaterow method)= "<<Chi2<<std::endl;
00142       //this is the probability of the hit respect to the track
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 //can make a new nethod on the base of the first one, which updates the sum over the columns(it gets a mtm and returns a double)
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     //return InvalidTransientRecHit::build(0);
00160   }
00161   
00162   int ierr;
00163   
00164   //const GeomDet* geomdet = 0;
00165   
00166   double a_sum=0;
00167   
00168   
00169   AlgebraicVector2 hitpos(asSVector<2>(trechit->parameters()));
00170   //hitpos[0]=trechit->localPosition().x();
00171   //hitpos[1]=trechit->localPosition().y();
00172   //  LogDebug("SiTrackerMultiRecHitUpdatorMTF")<<  "Hit position " << trechit->localPosition(); 
00173 
00174   //calculate parameters outside for cycle
00175   AlgebraicSymMatrix22 V( asSMatrix<2>(trechit->parametersError()));  
00176   V *= annealing;//assume that TSOS is smoothed one
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     //substitute the loop over the hits with a loop over the tsos
00189     for(std::map<int,TSOS>::const_iterator imtm=mtm->filteredStates().begin(); imtm!=mtm->filteredStates().end(); imtm++) {
00190     
00191 
00192     //LogDebug("SiTrackerMultiRecHitUpdatorMTF") << "entered in the for cicle";
00193     //std::cout << "debug message for cicle";
00194     //every step a different tsos
00195     //here I call a constructor
00196     //TSOS tsos( (*(imtm->second.freeState())), imtm->second.surface(), imtm->second.surfaceSide() );
00197     
00198     //then I fill the vector with the positions
00199   
00200     tsospos[0]= imtm->second.localPosition().x();
00201     tsospos[1]= imtm->second.localPosition().y();
00202     
00203     //LogDebug("SiTrackerMultiRecHitUpdatorMTF")<<  "TSOS position " << imtm->second.localPosition() ; 
00204     
00205     AlgebraicVector2 r( hitpos - tsospos);
00206 
00207     
00208 
00209     //V += me.measuredError(*ihit);// result = b*V + H*C*H.T()
00210     
00211     
00212     double Chi2 = ROOT::Math::Similarity(r,W);// Chi2 = r.T()*W*r
00213     //LogDebug("SiTrackerMultiRecHitUpdatorMTF")<<"Chi2 (updatecolumn method)= "<<Chi2<<std::endl;
00214     
00215     //this is the probability of the hit respect to the track
00216     //float a_i = exp(-0.5*Chi2)/(2.*M_PI*sqrt(V.determinant()));
00217     double a_i = exp(-0.5*Chi2)/denom;
00218     a_sum += a_i;
00219     
00220     //    LogDebug("SiTrackerMultiRecHitUpdatorMTF")<<  "Value of a " << a_i ; 
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   //a loop to propagate all the hits on the same surface...
00247   //std::vector<TransientTrackingRecHit::RecHitPointer> updatedcomponents = updatecomponents(tcomponents,tsos,annealing);
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   //LogTrace("SiTrackerMultiRecHitUpdator")<<  "Hit position " << tcomponents->localPosition(); 
00255   
00256   for(std::vector<TransientTrackingRecHit::RecHitPointer>::iterator ihit = updatedcomponents.begin(); ihit != updatedcomponents.end(); ihit++) {
00257     //    AlgebraicVector2 r(asSVector<2>((*ihit)->parameters()) - tsospos);
00258     AlgebraicSymMatrix22 V(asSMatrix<2>((*ihit)->parametersError()));
00259     V *= annealing;//assume that TSOS is smoothed one
00260     //V += me.measuredError(*ihit);// result = b*V + H*C*H.T()
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     //float Chi2 = W.similarity(r);// Chi2 = r.T()*W*r 
00271     //this is a normalization factor, to define a cut c, for dropping the probability to 0 
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     // return InvalidTransientRecHit::build(0); 
00291   }             
00292   
00293   if(!tsos.isValid()) {
00294     LogTrace("SiTrackerMultiRecHitUpdator")<<"SiTrackerMultiRecHitUpdator::update: tsos NOT valid!!!, returning an InvalidTransientRecHit";
00295     //return InvalidTransientRecHit::build(0);
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;//assume that TSOS is smoothed one
00312     //V += me.measuredError(*ihit);// result = b*V + H*C*H.T()
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);// Chi2 = r.T()*W*r
00324       //this is the probability of the hit respect to the track
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     //this is a normalization factor, to define a cut c, for dropping the probability to 0 
00329     //a_sum += a_i;     
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     //return InvalidTransientRecHit::build(0); 
00348   }             
00349   
00350   if(!tsos.isValid()) 
00351     {
00352       LogTrace("SiTrackerMultiRecHitUpdatorMTF")
00353         <<"SiTrackerMultiRecHitUpdatorMTF::updatecomponents: tsos NOT valid!!!, returning an InvalidTransientRecHit";
00354       //return InvalidTransientRecHit::build(0);
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       //to be fixed, to take into account hits coming from different modules
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       //      LogTrace("SiTrackerMultiRecHitUpdatorMTF") << "Current reference surface located at " << geomdet->surface().position();
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       //changed to limit the use of clone method
00384       //if ( (*iter)->isValid() ) updatedcomponents.push_back(*iter); 
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   //LogDebug("SiTrackerMultiRecHitUpdatorMTF") << "check if the map is good" << std::endl
00408   //                                 << "size: " << mymap.size()<< std::endl;
00409     
00410    
00411   unsigned int counter = 0;
00412   TransientTrackingRecHit::ConstRecHitContainer finalcomponents;
00413   //create a map normalized
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       // LogDebug("SiTrackerMultiRechitUpdatorMTF") <<" the value of phi is  "<< mymap[counter].second << std::endl
00420       //                                         <<" the value of totalsum is "<< total_sum << std::endl
00421       //                                         <<" the value of the cut is "<< c << std::endl
00422       //                                         <<" the size of the map is " << mymap.size() << std::endl
00423       //                                         <<" printing the first element of the map" << mymap[counter].first << std::endl;
00424       // <<" the local position of the hit is: "
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       //float p = ( (mymap[counter].second)/(total_sum - mymap[counter].second ) > 1.e-6 ? (mymap[counter].second)/(total_sum- mymap[counter].second) : 1.e-6);
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       //    LogDebug("SiTrackerMultiRechitUpdatorMTF") << " the probability of this hit is "
00434       //                                                 << p << std::endl;
00435       
00436       //float p = ((mymap[counter].second)/total_sum > 0.01 ? (mymap[counter].second)/total_sum : 1.e-6);
00437       normmap.push_back(std::pair<TransientTrackingRecHit::RecHitPointer, double>(mymap[counter].first, p) );
00438       //LogDebug("SiTrackerMultiRechitUpdatorMTF") <<"stored the pair <hit,p> in the map "
00439       //                                         <<std::endl;
00440       
00441       
00442       
00443       //let's store the weight in the component TransientTrackingRecHit too
00444       (*ihit)->setWeight(p);
00445       //      LogDebug("SiTrackerMultiRechitUpdatorMTF")<<"Weight set"<<std::endl;
00446       
00447       (*ihit)->setAnnealingFactor(annealing);
00448       // LogDebug("SiTrackerMultiRechitUpdatorMTF")<<"Annealing factor set"<<std::endl;
00449       
00450       finalcomponents.push_back(*ihit); 
00451       //LogDebug("SiTrackerMultiRechitUpdatorMTF")<<"Component pushed back"<<std::endl;
00452       
00453       //LogDebug("SiTrackerMultiRecHitUpdatorMTF")<<"FinalComponents size: "<<finalcomponents.size()<<std::endl;
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     //    LocalError er = calcParametersError(finalcomponents);
00471     
00472     //LocalPoint pos  = calcParameters(finalcomponents, er);
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     //return new SiTrackerMultiRecHit(normmap);         
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     //m_sum += ihit->weight()*(W*m);      
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   //AlgebraicSymMatrix V_sum(parametersError());
00566   AlgebraicVector2 parameters = V_sum*m_sum;
00567   return LocalPoint(parameters(0), parameters(1));
00568 }
00569