CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/RecoTracker/SiTrackerMRHTools/src/GroupedDAFHitCollector.cc

Go to the documentation of this file.
00001 #include "RecoTracker/SiTrackerMRHTools/interface/GroupedDAFHitCollector.h"
00002 #include "RecoTracker/SiTrackerMRHTools/interface/SiTrackerMultiRecHitUpdator.h"
00003 #include "RecoTracker/SiTrackerMRHTools/interface/MeasurementByLayerGrouper.h"
00004 #include "RecoTracker/TransientTrackingRecHit/interface/TSiTrackerMultiRecHit.h"
00005 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
00006 #include "TrackingTools/PatternTools/interface/MeasurementEstimator.h"
00007 #include "TrackingTools/DetLayers/interface/DetLayer.h"
00008 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00009 #include "TrackingTools/MeasurementDet/interface/TrajectoryMeasurementGroup.h"
00010 #include "TrackingTools/TransientTrackingRecHit/interface/InvalidTransientRecHit.h"
00011 
00012 #include <vector>
00013 #include <map>
00014 
00015 #define _debug_GroupedDAFHitCollector_ 
00016 
00017  using namespace std;
00018 
00019 
00020 vector<TrajectoryMeasurement> GroupedDAFHitCollector::recHits(const Trajectory& traj) const{
00021 
00022         //it assumes that the measurements are sorted in the smoothing direction
00023         const vector<TrajectoryMeasurement>& meas = traj.measurements();
00024         const Propagator* forwardPropagator = getPropagator();
00025         const Propagator* backwardPropagator = getReversePropagator();
00026         if (traj.direction() == alongMomentum){
00027                 forwardPropagator = getReversePropagator();
00028                 backwardPropagator = getPropagator(); 
00029         }  
00030         if (meas.empty()) //return TransientTrackingRecHit::ConstRecHitContainer();     
00031                 return vector<TrajectoryMeasurement>();
00032 
00033         vector<pair<const DetLayer*, vector<TrajectoryMeasurement> > > mol = MeasurementByLayerGrouper(getMeasurementTracker()->geometricSearchTracker())(meas);
00034 
00035 
00036         //TransientTrackingRecHit::ConstRecHitContainer result;
00037         vector<TrajectoryMeasurement> result;
00038 
00039         //add a protection if all the measurement are on the same layer
00040         if(mol.size()<2)return vector<TrajectoryMeasurement>();
00041 
00042         //first layer
00043         //  cout<<"DAFHitCollectionFromRecTrack: first layer"<<endl;
00044 
00045         //it assumes that the measurements are sorted in the smoothing direction
00046         //TrajectoryStateOnSurface current = (*(mol.begin()+1)).second.front().updatedState();
00047         TrajectoryStateOnSurface current = (*(mol.rbegin()+1)).second.back().updatedState();
00048         //if (current.isValid()) current.rescaleError(10);
00049 
00050         
00051         vector<TrajectoryMeasurementGroup> groupedMeas;
00052         //protection for layers with invalid meas with no id associated
00053         //to be fixed
00054         //for the moment no hit are lookerd for in these layers
00055         //remind that:
00056         //groupedMeasurements will return at least a measurement with an invalid hit with no detid
00057         LogDebug("MultiRecHitCollector") << "Layer "  << mol.back().first  << " has " << mol.back().second.size() << " measurements";
00058         //debug
00059         LogTrace("MultiRecHitCollector") << "Original measurements are:";
00060         vector<TrajectoryMeasurement>::const_iterator ibeg = mol.back().second.begin();
00061         vector<TrajectoryMeasurement>::const_iterator iend = mol.back().second.end();
00062         for (vector<TrajectoryMeasurement>::const_iterator imeas = ibeg; imeas != iend; ++imeas){
00063                 if (imeas->recHit()->isValid()){
00064                   LogTrace("MultiRecHitCollector") << "Valid Hit with DetId " << imeas->recHit()->geographicalId().rawId()
00065                                                    << " local position " << imeas->recHit()->hit()->localPosition();
00066                     //                                             << " on " << giulioGetLayer(imeas->recHit()->geographicalId());
00067                 } else {
00068                   LogTrace("MultiRecHitCollector") << "Invalid Hit with DetId " << imeas->recHit()->geographicalId().rawId(); 
00069                     //" on " << giulioGetLayer(imeas->recHit()->geographicalId());
00070                 }
00071         }
00072         //debug          
00073         if (mol.back().first) groupedMeas = theLM.groupedMeasurements(*(mol.back().first), current, *backwardPropagator, *(getEstimator()));
00074         //in this case we have to sort the detGroups in the opposite way (according the forward propagator, not the backward one)
00075         vector<TrajectoryMeasurementGroup> sortedgroupedMeas; 
00076         for (vector<TrajectoryMeasurementGroup>::reverse_iterator iter = groupedMeas.rbegin();  iter != groupedMeas.rend(); iter++){
00077                 sortedgroupedMeas.push_back(*iter);
00078         }
00079         buildMultiRecHits(sortedgroupedMeas, result);
00080                 
00081 
00082         //other layers
00083         current = mol.back().second.front().updatedState();
00084         //if (current.isValid()) current.rescaleError(10);
00085         for(vector<pair<const DetLayer*, vector<TrajectoryMeasurement> > >::reverse_iterator imol = mol.rbegin() + 1; imol != mol.rend(); imol++) {
00086                 const DetLayer* lay = (*imol).first;
00087                 LogDebug("MultiRecHitCollector") << "Layer "  << lay << " has " << (*imol).second.size() << " measurements";
00088                 //debug
00089                 LogTrace("MultiRecHitCollector") << "Original measurements are:";
00090                 vector<TrajectoryMeasurement>::const_iterator ibeg = (*imol).second.begin();
00091                 vector<TrajectoryMeasurement>::const_iterator iend = (*imol).second.end();
00092                 for (vector<TrajectoryMeasurement>::const_iterator imeas = ibeg; imeas != iend; ++imeas){
00093                         if (imeas->recHit()->isValid()){
00094                           LogTrace("MultiRecHitCollector") << "Valid Hit with DetId " << imeas->recHit()->geographicalId().rawId()
00095                                                            << " local position " << imeas->recHit()->hit()->localPosition();
00096                             //<< " on " << giulioGetLayer(imeas->recHit()->geographicalId());
00097                         } else {
00098                           LogTrace("MultiRecHitCollector") << "Invalid Hit with DetId " << imeas->recHit()->geographicalId().rawId();
00099                           //<< " on " << giulioGetLayer(imeas->recHit()->geographicalId());
00100                         }
00101                 }
00102                 //debug 
00103                 vector<TrajectoryMeasurementGroup> currentLayerMeas;
00104                 if (lay) currentLayerMeas = theLM.groupedMeasurements(*lay, current, *forwardPropagator, *(getEstimator()));
00105                 buildMultiRecHits(currentLayerMeas, result);
00106                 current = (*imol).second.front().updatedState();
00107                 //if (current.isValid()) current.rescaleError(10);
00108         }
00109         LogTrace("MultiRecHitCollector") << "Original Measurement size "  << meas.size() << " GroupedDAFHitCollector returned " << result.size() << " measurements";
00110         //results are sorted in the fitting direction
00111 
00112  //     adding a protection against too few hits and invalid hits (due to failed propagation on the same surface of the original hits)
00113         if (result.size()>2)
00114           {
00115             int hitcounter=0;
00116             //check if the vector result has more than 3 valid hits
00117             for (vector<TrajectoryMeasurement>::const_iterator iimeas = result.begin(); iimeas != result.end(); ++iimeas)
00118               {
00119                 if(iimeas->recHit()->isValid()) hitcounter++;
00120               }
00121             
00122             if(hitcounter>2) 
00123               {return result;} 
00124             
00125             else return vector<TrajectoryMeasurement>();
00126           }
00127         
00128         else{return vector<TrajectoryMeasurement>();}
00129         
00130 }
00131 
00132 void GroupedDAFHitCollector::buildMultiRecHits(const vector<TrajectoryMeasurementGroup>& measgroup, vector<TrajectoryMeasurement>& result) const {
00133 
00134         unsigned int initial_size = result.size();
00135         //TransientTrackingRecHit::ConstRecHitContainer rhits;
00136         if (measgroup.empty()) {
00137                 LogTrace("MultiRecHitCollector") << "No TrajectoryMeasurementGroups found for this layer" ; 
00138                 //should we do something?
00139                 //result.push_back(InvalidTransientRecHit::build(0,TrackingRecHit::missing));
00140                 return;
00141         } 
00142 
00143         //we build a MultiRecHit out of each group
00144         //groups are sorted along momentum or opposite to momentum, 
00145         //measurements in groups are sorted with increating chi2
00146         LogTrace("MultiRecHitCollector") << "Found " << measgroup.size() << " groups for this layer";
00147         //trajectory state to store the last valid TrajectoryState (if present) to be used 
00148         //to add an invalid Measurement in case no valid state or no valid hits are found in any group
00149         for (vector<TrajectoryMeasurementGroup>::const_iterator igroup = measgroup.begin(); igroup != measgroup.end(); igroup++ ){
00150                 //the TrajectoryState is the first one
00151                 TrajectoryStateOnSurface state = igroup->measurements().front().predictedState();
00152                 if (!state.isValid()){
00153                         LogTrace("MultiRecHitCollector") << "Something wrong! no valid TSOS found in current group ";
00154                         continue;               
00155                 }
00156                 //debug
00157                 LogTrace("MultiRecHitCollector") << "This group has " << igroup->measurements().size() << " measurements";
00158                 LogTrace("MultiRecHitCollector") << "This group has the following " << igroup->detGroup().size() << " detector ids: " << endl;
00159                 for (DetGroup::const_iterator idet = igroup->detGroup().begin(); idet != igroup->detGroup().end(); ++idet){
00160                   LogTrace("MultiRecHitCollector") << idet->det()->geographicalId().rawId();
00161                           // " on " << giulioGetLayer(idet->det()->geographicalId());
00162                 }
00163                 //debug
00164                 vector<const TrackingRecHit*> hits;
00165                 for (vector<TrajectoryMeasurement>::const_iterator imeas = igroup->measurements().begin(); imeas != igroup->measurements().end(); imeas++){
00166                         //collect the non missing hits to build the MultiRecHits
00167                         //we ese the recHits method; anyway only simple hits, not MultiHits should be present 
00168                         if (imeas->recHit()->getType() != TrackingRecHit::missing) {
00169                                 LogTrace("MultiRecHitCollector") << "This hit is valid ";
00170                                 hits.push_back(imeas->recHit()->hit());
00171                         }
00172                 }
00173                 if (hits.empty()){
00174                         LogTrace("MultiRecHitCollector") << "No valid hits found in current group ";
00175                         continue;
00176                 }
00177                 LogTrace("MultiRecHitCollector") << "The best TSOS in this group is " << state << " it lays on surface located at " << state.surface().position();
00178 #ifdef _debug_GroupedDAFHitCollector_   
00179                 LogTrace("MultiRecHitCollector") << "For the MRH on this group the following hits will be used"; 
00180                 for (vector<const TrackingRecHit*>::iterator iter = hits.begin(); iter != hits.end(); iter++){  
00181                         string validity = "valid";
00182                         if ((*iter)->getType() == TrackingRecHit::missing ) validity = "missing !should not happen!";
00183                         else if ((*iter)->getType() == TrackingRecHit::inactive) validity = "inactive";
00184                         else if ((*iter)->getType() == TrackingRecHit::bad) validity = "bad";   
00185                         LogTrace("MultiRecHitCollector") << "DetId " << (*iter)->geographicalId().rawId()  << " validity: " << validity 
00186                                 << " surface position " << getMeasurementTracker()->geomTracker()->idToDet((*iter)->geographicalId())->position()  
00187                                 << " hit local position " << (*iter)->localPosition();
00188                 }
00189 #endif
00190                 
00191                 result.push_back(TrajectoryMeasurement(state,theUpdator->buildMultiRecHit(hits, state)));
00192         }
00193 
00194         //can this happen? it means that the measgroup was not empty but no valid measurement was found inside
00195         //in this case we add an invalid measuremnt for this layer 
00196         if (result.size() == initial_size){
00197                 LogTrace("MultiRecHitCollector") << "no valid measuremnt or no valid TSOS in none of the groups";
00198                 //measgroup has been already checked for size != 0
00199                 if (measgroup.back().measurements().size() != 0){       
00200                         result.push_back(measgroup.back().measurements().back());
00201                 } 
00202         }
00203 }
00204