CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/RecoTracker/SiTrackerMRHTools/src/SimpleMTFHitCollector.cc

Go to the documentation of this file.
00001 #include "RecoTracker/SiTrackerMRHTools/interface/SimpleMTFHitCollector.h"
00002 #include "RecoTracker/SiTrackerMRHTools/interface/SiTrackerMultiRecHitUpdatorMTF.h"
00003 #include "RecoTracker/TransientTrackingRecHit/interface/TSiTrackerMultiRecHit.h"
00004 #include "TrackingTools/PatternTools/interface/MeasurementEstimator.h"
00005 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00006 #include "TrackingTools/TransientTrackingRecHit/interface/InvalidTransientRecHit.h"
00007 #include "TrackingTools/MeasurementDet/interface/MeasurementDet.h"
00008 #include "RecoTracker/SiTrackerMRHTools/interface/MultiTrajectoryMeasurement.h" //added
00009 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
00010 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHit.h"
00011 #include "TrackingTools/TrackFitters/interface/TrajectoryStateCombiner.h"
00012 #include <vector>
00013 #include <map>
00014 
00015 #define _debug_SimpleMTFHitCollector_ 
00016 
00017 using namespace std;
00018 
00019 vector<TrajectoryMeasurement> 
00020 SimpleMTFHitCollector::recHits(const std::map<int, vector<TrajectoryMeasurement> >& tmmap, 
00021                                int i,
00022                                double annealing) const{
00023   
00024   
00025   //it assumes that the measurements are sorted in the smoothing direction
00026   map< int, vector<TrajectoryMeasurement> >::const_iterator itmeas = tmmap.find(i);
00027   LogDebug("SimpleMTFHitCollector") << "found the element "<< i << "in the map" << std::endl;
00028   
00029   vector<TrajectoryMeasurement> meas = itmeas->second;
00030   LogDebug("SimpleMTFHitCollector") << "got the vector corresponding to " << i << " element of the map "<< std::endl;
00031   
00032   if (meas.empty())     
00033     return vector<TrajectoryMeasurement>();
00034   
00035   
00036   //TransientTrackingRecHit::ConstRecHitContainer result;
00037   vector<TrajectoryMeasurement> result;
00038   TrajectoryStateCombiner statecombiner;
00039 
00040   //loop on the vector<TM> from the bottom to the top (assumes the measurement are sorted in the smoothing direction)
00041   for(vector<TrajectoryMeasurement>::reverse_iterator imeas = meas.rbegin(); imeas != meas.rend(); imeas++) 
00042     
00043     {
00044       //check if the rechit is a valid one: if it is build a MultiRecHit
00045       if(imeas->recHit()->geographicalId().rawId())
00046         {
00047           //define the rechit
00048           TransientTrackingRecHit::ConstRecHitPointer rechit = imeas->recHit();
00049           //const DetLayer* layer = imeas->layer();
00050           
00051           uint32_t id = imeas->recHit()->geographicalId().rawId();
00052           
00053           LogDebug("SimpleMTFHitCollector") << "Detector Id: " << id << std::endl;
00054           
00055           
00056           std::vector<std::pair<int, TrajectoryMeasurement> > layermeas;
00057           getMeasurements(layermeas, tmmap,*imeas,i);
00058           
00059           
00060           //create a MTM
00061           MultiTrajectoryMeasurement mtm = getTSOS(layermeas, rechit, i);
00062           
00063           //then build a MRH from a vector<TM> and a MTM...the buildMRH method has to be a dedicated one                        
00064           TransientTrackingRecHit::ConstRecHitContainer hits;
00065           
00066           for (vector<std::pair<int, TrajectoryMeasurement> >::const_iterator ittmeas = layermeas.begin(); ittmeas != layermeas.end(); ittmeas++)
00067             {
00068               //TransientTrackingRecHit::ConstRecHitContainer sharedhits;
00069 
00070               if(ittmeas->second.recHit()->isValid())
00071                 {
00072                   
00073                   LogDebug("SimpleMTFHitCollector") << "this rechit in the vector layermeas is valid." << std::endl;
00074                   
00075                   int k=0;
00076                   //if the rechit is valid we push back the hit  
00077                   //hits.push_back(ittmeas->second.recHit());
00078                   
00079                   if (!hits.size())
00080                     {
00081                       hits.push_back(ittmeas->second.recHit());
00082                       
00083                       
00084                       //check if the rechits in hits are the same, and in case remove the hit from the vector
00085                     }
00086 
00087                   else
00088                     {
00089                       for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator ihits = hits.begin(); ihits != hits.end(); ihits++ )
00090                         {
00091                           
00092                           
00093                           //we check if the rechits are the same and if the vector hits is not made of 1 hit,
00094                           //if it is made of more than 1 hit we pop back the hit 
00095                           if((*ihits)->hit()->sharesInput(ittmeas->second.recHit()->hit(), TrackingRecHit::all))
00096                             {
00097                               LogDebug("SimpleMTFHitCollector") << "the rechit coming from layermeas is the same as the first rechit "
00098                                                               << "we skip this rechit in building the multirechit" << std::endl;
00099                               // sharedhits.push_back(ittmeas->second.recHit());
00100                               
00101                               k++;
00102                               break;
00103                             }
00105                         }
00106                   
00107                   //if(sharedhits.size()) continue;
00108               
00109                   //else
00110               // {
00111               //      hits.push_back(ittmeas->second.recHit()); 
00112               //     sharedhits.clear();
00113               //  }
00114               
00115                       if(k==0)
00116                         {
00117                           LogTrace("SimpleMTFHitCollector") << "This hit is valid ";
00118                           hits.push_back(ittmeas->second.recHit());
00119                         }
00120                       
00121                     }
00122                   
00123                 }
00124 
00125               else continue;
00126               
00127             }
00128           
00129               
00130           if(hits.size()==0)
00131             {
00132               result.push_back(*imeas);
00133               LogDebug("SimpleMTFHitCollector") << "we found no valid rechits, so we fill the vector with the initial measurement" << std::endl;
00134             }
00135               
00136           else
00137             {
00138               
00139               //              TrajectoryStateOnSurface state = layermeas.front().second.predictedState();
00140               TrajectoryStateOnSurface state= statecombiner.combine(layermeas.front().second.predictedState(), layermeas.front().second.backwardPredictedState());
00141 
00142               LogDebug("SimpleMTFHitCollector") << "we build a trajectory measurement from a vector of hits of size" << hits.size() <<std::endl;
00143               
00144               result.push_back(TrajectoryMeasurement(state,theUpdator->buildMultiRecHit(state, hits, &mtm, annealing)));
00145             }
00146           
00147         }
00148 
00149       
00150       
00151       //if the rechit is not valid, the measurement is pushed back as it is
00152       else 
00153         {
00154           result.push_back(*imeas);
00155         }
00156       
00157     }
00158   
00159       
00160   LogTrace("MultiRecHitCollector") << "Original Measurement size "  
00161                                    << meas.size() << " SimpleMTFHitCollector returned " 
00162                                    << result.size() << " rechits";
00163   //results are sorted in the fitting direction
00164   return result;        
00165   
00166 }
00167 
00168 
00169 void
00170 SimpleMTFHitCollector::getMeasurements(std::vector<std::pair<int, TrajectoryMeasurement> >& layermeas,
00171                                        const std::map<int, vector<TrajectoryMeasurement> >& tmmap, 
00172                                        TrajectoryMeasurement& pmeas,
00173                                        int i) const{
00174   
00175   uint32_t id = pmeas.recHit()->geographicalId().rawId();
00176   
00177   LogDebug("SimpleMTFHitCollector") << "Detector Id: " << id << std::endl;
00178   
00179   //vector<std::pair<int, TrajectoryMeasurement> > layermeas;
00180   
00181   if( pmeas.recHit()->geographicalId().rawId() )
00182     {
00183       //search for hits only on the same detector, comparing the detId. Fill also the vector layermeas with the original measurement. 
00184       
00185       
00186       //use the method fastmeas to search for compatible hits and put it into a vector
00187       // vector<TrajectoryMeasurement> veclayermeas = 
00188       //        getMeasurementTracker()->idToDet
00189       //        (pmeas.recHit()->geographicalId().rawId())->fastMeasurements(pmeas.updatedState(), 
00190       //    pmeas.updatedState(), 
00191       //        *thePropagator, 
00192       //        *(getEstimator()));
00193   
00194   
00195   //LogDebug("SimpleMTFHitCollector") << " method veclayermeas returned a vector of size: " 
00196   //                            << veclayermeas.size() 
00197   //                            << std::endl;
00198       
00199       //        for(vector<TrajectoryMeasurement>::iterator iveclayermeas=veclayermeas.begin();
00200       //      iveclayermeas!=veclayermeas.end();
00201       //      iveclayermeas++)
00202       
00203       // {
00204       
00205         //  layermeas.push_back(make_pair(i,pmeas));
00206           
00207       for (std::map< int, vector<TrajectoryMeasurement> >::const_iterator immap=tmmap.begin(); 
00208            immap!=tmmap.end(); 
00209            immap++)
00210         {
00211           int ntraj2 = immap->first;
00212           //LogDebug("SimpleMTFHitCollector") << " number of the trajectory examined: " << ntraj2 << std::endl;
00213               //map< int, vector<TrajectoryMeasurement> >::const_iterator k = immap->second;
00214           const vector<TrajectoryMeasurement> & vecmeas = immap->second;
00215           
00216           //    if(ntraj2 == i) continue;
00217           
00218           
00219           for(vector<TrajectoryMeasurement>::const_reverse_iterator itvmeas = vecmeas.rbegin(); itvmeas!=vecmeas.rend(); itvmeas++)
00220             {
00221               //    if ( ( itvmeas->recHit()->geographicalId().rawId() == id) && (itvmeas->recHit()->isValid()) && !(itvmeas->recHit()->hit()->sharesInput(pmeas.recHit()->hit(), TrackingRecHit::all)) ) //modoficare per inludere il layer nella ricerca
00222               
00223               //if(itvmeas->recHit()->hit()->sharesInput(iveclayermeas->recHit()->hit(), TrackingRecHit::some) )
00224               if ( (itvmeas->recHit()->geographicalId().rawId() == id) )
00225                 { 
00226                       LogDebug("SimpleMTFHitCollector") << "found a matching rechit in the collector";
00227                       //    if(iveclayermeas->updatedState() == itvmeas->updatedState())
00228                       
00229                       layermeas.push_back(make_pair(ntraj2,*itvmeas));
00230                       
00231                 }
00232               
00233               //if(itvmeas->recHit()->geographicalId().rawId() == id)
00234               
00235               //layermeas.push_back(make_pair(ntraj2,*itvmeas));
00236               
00237             }
00238           
00239         }
00240           
00241        
00242       //vector<TrajectoryMeasurement> 
00243       
00244       //currentLayerMeas=getMeasurementTracker()->idToDet(imeas->recHit()->geographicalId().rawId())->fastMeasurements(imeas->updatedState(), 
00245       //                                                                                                         imeas->updatedState(), 
00246       //                                                                                                         *thePropagator, 
00247       //                                                                                                         *(getEstimator()));
00248       LogDebug("SimpleMTFHitCollector") << " built a vector of Trajectory Measurements all on the same layer, of size: " 
00249                                         << layermeas.size() 
00250                                         << std::endl;
00251     }
00252   
00253   else layermeas.push_back(make_pair(i,pmeas));
00254   
00255   
00256 }
00257 
00258 MultiTrajectoryMeasurement SimpleMTFHitCollector::getTSOS(const vector<std::pair<int, TrajectoryMeasurement> >& layermeas, 
00259                                                           TransientTrackingRecHit::ConstRecHitPointer rechit, 
00260                                                           int i) const{
00261   
00262   //we should search even for compatible tsos on this layer and build the multirechit with this knowledge...we can build an MTM.
00263   uint32_t id = rechit->geographicalId().rawId();
00264   
00265   LogDebug("SimpleMTFHitCollector") << "Detector Id: " << id << std::endl;
00266 
00267   LogDebug("SimpleMTFHitCollector") << "LayerMeas size: " << layermeas.size() << std::endl;
00268   
00269   const DetLayer* layer = layermeas.front().second.layer();
00270   
00271   std::map<int,TSOS> predictions; 
00272   std::map<int,TSOS> updates;
00273   LogDebug("SimpleMTFHitCollector") << " about to build a map with predicted and updated states " << std::endl;
00274   
00275   //we insert initially at least the original predicted & updated states
00276   //if(imeas->predictedState().isValid())       
00277   //  predictions[i] = imeas->predictedState();
00278   //else if ( imeas->backwardPredictedState().isValid() )
00279   //  predictions[i] = imeas->backwardPredictedState();
00280   //else viva il dale
00281   //  LogDebug("SimpleMTFHitCollector") << "error:invalid predicted and backward predicted states" << std::endl;
00282   
00283   //first fill the map with the state relative to the measurement we are analizing
00284   //if(imeas->updatedState().isValid())
00285   //  updates[i] = imeas->updatedState();
00286   
00287   //LogDebug("SimpleMTFHitCollector") << "Local Position of predicted state" << imeas->predictedState().localPosition() << std::endl;
00288   //LogDebug("SimpleMTFHitCollector") << "Local Position of updated state" << imeas->updatedState().localPosition() << std::endl;           
00289   TrajectoryStateCombiner statecombiner;
00290   
00291   for (std::vector<std::pair<int,TrajectoryMeasurement> >::const_iterator itmeas=layermeas.begin(); itmeas!=layermeas.end(); itmeas++)
00292     {
00293       
00294       //we now have to build 2 maps, with the (predicted & updated) tsos for each track; then we can construct a MTM
00295       LogDebug("SimpleMTFHitCollector") << " number of the trajectory examined: " << itmeas->first << std::endl;
00296       
00297       //get the vector from the map
00298       //LogDebug("SimpleMTFHitCollector") << " size of the vector examining: " << vmeas.size() << std::endl;
00299       
00300       //if(ntraj == i)
00301       //{
00302       //          LogDebug("SimpleMTFHitCollector") << " skipping trajectory number: " << ntraj << std::endl;
00303       //  continue;
00304       //        }
00305       
00306       //begin a cicle to search for measurements compatible(same layer)
00307       //check if the _detid_ of the original measurement is the same of this one and if the rechits are not the same one
00308       if ( (itmeas->second.recHit()->geographicalId().rawId() == id) ) //controlla che siano sullo stesso layer (cambiare!!!)
00309         {
00310           
00311           //      LogDebug("SimpleMTFHitCollector") << "found a compatible hit " << std::endl;
00312 
00313           //add an element to the maps with predicted and updated states 
00314           if(itmeas->second.predictedState().isValid())
00315             {
00316               predictions[itmeas->first] = itmeas->second.predictedState();
00317               //              LogDebug("SimpleMTFHitCollector") << "predicted state inserted in the map " << std::endl;
00318               if ( itmeas->second.backwardPredictedState().isValid() ){
00319                 updates[itmeas->first]= statecombiner.combine(itmeas->second.predictedState(), itmeas->second.backwardPredictedState());
00320               }
00321             }
00322           
00323           //      else if ( itmeas->second.backwardPredictedState().isValid() )
00324           //        {
00325           //          predictions[itmeas->first] = itmeas->second.backwardPredictedState();
00326           //          LogDebug("SimpleMTFHitCollector") << "bacwardpredicted state inserted in the map " << std::endl;
00327           //        }
00328           
00329           else 
00330             LogDebug("SimpleMTFHitCollector") << "error:invalid predicted state" << std::endl; 
00331           
00332 //        if(itmeas->second.updatedState().isValid()){
00333             
00334 //          updates[itmeas->first] = itmeas->second.updatedState();
00335 //          LogDebug("SimpleMTFHitCollector") << "updated state inserted in the map " << std::endl;
00336 //        }
00337 //         else if ( itmeas->second.predictedState().isValid() )
00338 //           {
00339 //             LogDebug("SimpleMTFHitCollector") << "error: invalid updated state, taking the predicted one instead " << std::endl; 
00340 //             updates[itmeas->first] = itmeas->second.predictedState();
00341 //           }
00342           
00343 //        else 
00344 //          LogDebug("SimpleMTFHitCollector") << "error:invalid updated and backward predicted states" << std::endl;
00345           
00346           
00347           //get the iterator to the predicted & updated states of the maps, to print the TSOS predicted and updated 
00348           //      map<int,TSOS>::iterator ipred = predictions.find(itmeas->first); 
00349           //map<int,TSOS>::iterator iupd = updates.find(itmeas->first); 
00350           
00351           //controlla che siano giusti gli stati!!!
00352           //LogDebug("SimpleMTFHitCollector") << "Local Position of predicted state " << ipred->second.localPosition() << std::endl
00353           //                        << " of trajectory number " << ipred->first <<"\n" << std::endl;
00354           //LogDebug("SimpleMTFHitCollector") << "Local Position of updated state " << iupd->second.localPosition() << std::endl
00355           //<< " of trajectory number " << iupd->first <<"\n" << std::endl;
00356           
00357         }
00358     }
00359   
00360   //create a MTM
00361   MultiTrajectoryMeasurement mtm = MultiTrajectoryMeasurement(rechit,predictions,updates,layer);
00362   return mtm;
00363 }
00364 
00365 
00366 
00367 MultiTrajectoryMeasurement SimpleMTFHitCollector::TSOSfinder(const std::map<int, vector<TrajectoryMeasurement> >& tmmap, 
00368                                                              TrajectoryMeasurement& pmeas,
00369                                                              int i) const{
00370   
00371   
00372   //it assumes that the measurements are sorted in the smoothing direction
00373   //map< int, vector<TrajectoryMeasurement> >::const_iterator itmeas = tmmap.find(i);
00374   //LogDebug("SimpleMTFHitCollector") << "found the element "<< i << "in the map" << std::endl;
00375   
00376   //vector<TrajectoryMeasurement> meas = itmeas->second;
00377   //LogDebug("SimpleMTFHitCollector") << "got the vector corresponding to " << i << " element of the map "<< std::endl;
00378   
00379   //if (meas.empty())   
00380   // return vector<TrajectoryMeasurement>();
00381   
00382   
00383   //TransientTrackingRecHit::ConstRecHitContainer result;
00384   vector<TrajectoryMeasurement> result;
00385    
00386   
00387   //  if(pmeas.recHit()->isValid())
00388     
00389     
00390     
00391       //define the rechit
00392       TransientTrackingRecHit::ConstRecHitPointer rechit = pmeas.recHit();
00393       
00394       //      const DetLayer* layer = pmeas.layer();
00395       
00396       uint32_t id = pmeas.recHit()->geographicalId().rawId();
00397       
00398       LogDebug("SimpleMTFHitCollector") << "Detector Id: " << id << std::endl;
00399 
00400       //get all the measurement on the same layer, searching in the map
00401       vector<std::pair<int, TrajectoryMeasurement> > layermeas; 
00402       getMeasurements(layermeas, tmmap,pmeas,i);
00403       
00404       
00405       //create a MTM, by knowledge of the measurements on the same layer and the rechit...
00406       return getTSOS(layermeas, rechit, i);
00407     
00408       
00409     }
00410   
00411 
00412          
00413          //then build a MRH from a vector<TM> and a MTM...the buildMRH method has to be a dedicated one                         
00414          //TransientTrackingRecHit::ConstRecHitContainer hits;
00415          
00416          //for (vector<std::pair<int, TrajectoryMeasurement> >::const_iterator ittmeas = layermeas.begin(); ittmeas != layermeas.end(); ittmeas++){
00417          // if (ittmeas->second.recHit()->getType() != TrackingRecHit::missing) {
00418          //  LogTrace("SimpleMTFHitCollector") << "This hit is valid ";
00419          //  hits.push_back(ittmeas->second.recHit());
00420          //}
00421          //}
00422          //TrajectoryStateOnSurface state = layermeas.front().second.predictedState();
00423          
00424          
00425 
00426          
00427          //obsolete method,  not used in track reconstruction or MRH building
00428 void SimpleMTFHitCollector::buildMultiRecHits(const vector<std::pair<int, TrajectoryMeasurement> >& vmeas, 
00429                                               MultiTrajectoryMeasurement* mtm, 
00430                                               vector<TrajectoryMeasurement>& result,
00431                                               double annealing) const {
00432   
00433   if (vmeas.empty()) {
00434     LogTrace("SimpleMTFHitCollector") << "the search for the measurement on the same layer returned an empty vector...we have got a problem... " ; 
00435     //should we do something?
00436     //result.push_back(InvalidTransientRecHit::build(0,TrackingRecHit::missing));
00437     return;
00438     } 
00439   
00440   
00441   //the state is computed taking the vmeas (all on the same surface) and their predicted state
00442   TrajectoryStateOnSurface state = vmeas.front().second.predictedState();
00443   LogTrace("SimpleMTFHitCollector") << "Position (local) of the first measurement state: " << state.localPosition();
00444   LogTrace("SimpleMTFHitCollector") << "Position (global) of the first measurement state: " << state.globalPosition();
00445   
00446   if (state.isValid()==false){
00447     LogTrace("MultiRecHitCollector") << "first state is invalid; skipping ";
00448     return;
00449   }
00450   
00451   //vector<const TrackingRecHit*> hits;
00452   TransientTrackingRecHit::ConstRecHitContainer hits;
00453   
00454   for (vector<std::pair<int, TrajectoryMeasurement> >::const_iterator ittmeas = vmeas.begin(); ittmeas != vmeas.end(); ittmeas++){
00455     if (ittmeas->second.recHit()->getType() != TrackingRecHit::missing) {
00456       LogTrace("SimpleMTFHitCollector") << "This hit is valid ";
00457       hits.push_back(ittmeas->second.recHit());
00458     }
00459   }
00460   
00461   if (hits.empty()){
00462     LogTrace("MultiTrackFilterHitCollector") << "No valid hits found ";
00463     return;
00464   }
00465   
00466   LogTrace("SimpleMTFHitCollector") << "The hits vector has size: " << hits.size() << "\n"
00467                                     << "and has first component global position: " << hits.front()->globalPosition();
00468 
00469 
00470   //for(std::map<int,TSOS>::const_iterator imtm=mtm->filteredStates().begin(); imtm!=mtm->filteredStates().end(); imtm++)
00471   // {
00472   //  LogDebug("SimpleMTFHitCollector::BuildMultiRecHits") << "TSOS number " << imtm->first << "\n" 
00473   //                                               << "TSOS position " << imtm->second.localPosition() << std::endl;
00474   // }
00475   
00476   //LogDebug("SimpleMTFHitCollector::BuildMultiRecHits") << "Map Size:" << mtm->filteredStates().size() << "\n";
00477 
00478 
00479   //the work of building a concrete MRH is done by theUpdator->buildMultiRecHit method (modified, to include an mtm...)
00480   result.push_back(TrajectoryMeasurement(state,theUpdator->buildMultiRecHit(state, hits, mtm, annealing)));     
00481 
00482   
00483   //result.push_back(TrajectoryMeasurement(state,theUpdator->update(hits, state)));     
00484   
00485 }
00486 
00487