CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoTracker/DebugTools/plugins/CkfDebugger.cc

Go to the documentation of this file.
00001 #include "RecoTracker/DebugTools/interface/CkfDebugger.h"
00002 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00003 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00004 #include "TrackingTools/PatternTools/interface/TrajectoryMeasurement.h"
00005 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00006 #include "Geometry/CommonTopologies/interface/Topology.h"
00007 #include "DataFormats/GeometryCommonDetAlgo/interface/MeasurementVector.h"
00008 #include "RecoTracker/MeasurementDet/interface/MeasurementTracker.h"
00009 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00010 #include "RecoTracker/DebugTools/interface/TSOSFromSimHitFactory.h"
00011 #include "TrackingTools/MeasurementDet/interface/MeasurementDet.h"
00012 #include "TrackingTools/MaterialEffects/interface/PropagatorWithMaterial.h"
00013 #include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
00014 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00015 
00016 #include "RecoTracker/DebugTools/interface/GluedDetFromDetUnit.h"
00017 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2D.h"
00018 
00019 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00020 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00021 #include "RecoTracker/TkNavigation/interface/TkLayerLess.h"
00022 #include "RecoTracker/Record/interface/TrackerRecoGeometryRecord.h"
00023 #include "TrackingTools/DetLayers/interface/BarrelDetLayer.h"
00024 // #include "RecoTracker/TkDetLayers/interface/PixelBarrelLayer.h"
00025 
00026 #include "TrackingTools/TransientTrackingRecHit/interface/TrackingRecHitProjector.h"
00027 #include "RecoTracker/TransientTrackingRecHit/interface/ProjectedRecHit2D.h"
00028 
00029 #include <iostream>
00030 #include <iomanip>
00031 #include <sstream>
00032 
00033 using namespace std;
00034 
00035 CkfDebugger::CkfDebugger( edm::EventSetup const & es ):totSeeds(0)
00036 {
00037   file = new TFile("out.root","recreate");
00038   hchi2seedAll = new TH1F("hchi2seedAll","hchi2seedAll",2000,0,200);
00039   hchi2seedProb = new TH1F("hchi2seedProb","hchi2seedProb",2000,0,200);
00040 
00041   edm::ESHandle<TrackerGeometry> tracker;
00042   es.get<TrackerDigiGeometryRecord>().get(tracker);
00043   theTrackerGeom = &(*tracker);
00044 
00045   edm::ESHandle<MagneticField>                theField;
00046   es.get<IdealMagneticFieldRecord>().get(theField);
00047   theMagField = &(*theField);
00048   
00049   for (int i=0; i!=17; i++){
00050     dump.push_back(0);
00051   }
00052 
00053   std::stringstream title;
00054   for (int i=0; i!=6; i++)
00055     for (int j=0; j!=9; j++){
00056       if (i==0 && j>2) break;
00057       if (i==1 && j>1) break;
00058       if (i==2 && j>3) break;
00059       if (i==3 && j>2) break;
00060       if (i==4 && j>5) break;
00061       if (i==5 && j>8) break;
00062       dump2[pair<int,int>(i,j)]=0;
00063       dump3[pair<int,int>(i,j)]=0;
00064       dump4[pair<int,int>(i,j)]=0;
00065       dump5[pair<int,int>(i,j)]=0;
00066       dump6[pair<int,int>(i,j)]=0;
00067       title.str("");
00068       title << "pullX_" << i+1 << "-" << j+1 << "_sh-rh";
00069       hPullX_shrh[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00070       title.str("");
00071       title << "pullY_" << i+1 << "-" << j+1 << "_sh-rh";
00072       hPullY_shrh[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00073       title.str("");
00074       title << "pullX_" << i+1 << "-" << j+1 << "_sh-st";
00075       hPullX_shst[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00076       title.str("");
00077       title << "pullY_" << i+1 << "-" << j+1 << "_sh-st";
00078       hPullY_shst[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00079       title.str("");
00080       title << "pullX_" << i+1 << "-" << j+1 << "_st-rh";
00081       hPullX_strh[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00082       title.str("");
00083       title << "pullY_" << i+1 << "-" << j+1 << "_st-rh";
00084       hPullY_strh[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00085       title.str("");
00086       title << "PullGP_X_" << i+1 << "-" << j+1 << "_sh-st";
00087       hPullGP_X_shst[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00088       title.str("");
00089       title << "PullGP_Y_" << i+1 << "-" << j+1 << "_sh-st";
00090       hPullGP_Y_shst[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00091       title.str("");
00092       title << "PullGP_Z_" << i+1 << "-" << j+1 << "_sh-st";
00093       hPullGP_Z_shst[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00094       if ( ((i==2||i==4)&&(j==0||j==1)) || (i==3||i==5) ){
00095         title.str("");
00096         title << "pullM_" << i+1 << "-" << j+1 << "_sh-rh";
00097         hPullM_shrh[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00098         title.str("");
00099         title << "pullS_" << i+1 << "-" << j+1 << "_sh-rh";
00100         hPullS_shrh[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00101         title.str("");
00102         title << "pullM_" << i+1 << "-" << j+1 << "_sh-st";
00103         hPullM_shst[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00104         title.str("");
00105         title << "pullS_" << i+1 << "-" << j+1 << "_sh-st";
00106         hPullS_shst[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00107         title.str("");
00108         title << "pullM_" << i+1 << "-" << j+1 << "_st-rh";
00109         hPullM_strh[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00110         title.str("");
00111         title << "pullS_" << i+1 << "-" << j+1 << "_st-rh";
00112         hPullS_strh[title.str()] = new TH1F(title.str().c_str(),title.str().c_str(),1000,-50,50);
00113       }
00114     }
00115 
00116   hPullGPXvsGPX_shst   = new TH2F("PullGPXvsGPX_shst","PullGPXvsGPX_shst",1000,-50,50,100,-50,50);
00117   hPullGPXvsGPY_shst   = new TH2F("PullGPXvsGPY_shst","PullGPXvsGPY_shst",1000,-50,50,100,-50,50);
00118   hPullGPXvsGPZ_shst   = new TH2F("PullGPXvsGPZ_shst","PullGPXvsGPZ_shst",1000,-50,50,200,-100,100);
00119   hPullGPXvsGPr_shst   = new TH2F("PullGPXvsGPr_shst","PullGPXvsGPr_shst",1000,-50,50,300,-150,150);
00120   hPullGPXvsGPeta_shst = new TH2F("PullGPXvsGPeta_shst","PullGPXvsGPeta_shst",1000,-50,50,50,-2.5,2.5);
00121   hPullGPXvsGPphi_shst = new TH2F("PullGPXvsGPphi_shst","PullGPXvsGPphi_shst",1000,-50,50,63,0,6.3);
00122  
00123   seedWithDelta=0;
00124   problems=0;
00125   no_sim_hit=0;
00126   no_layer=0;
00127   layer_not_found=0;
00128   det_not_found=0;
00129   chi2gt30=0;
00130   chi2gt30delta=0;
00131   chi2gt30deltaSeed=0;
00132   chi2ls30=0;
00133   simple_hit_not_found=0;
00134   no_component=0;
00135   only_one_component=0;
00136   matched_not_found=0;
00137   matched_not_associated=0;
00138   partner_det_not_fuond=0;
00139   glued_det_not_fuond=0;
00140   propagation=0;
00141   other=0;
00142   totchi2gt30=0;
00143 }
00144 
00145 void CkfDebugger::printSimHits( const edm::Event& iEvent)
00146 {
00147   edm::LogVerbatim("CkfDebugger") << "\nEVENT #" << iEvent.id();
00148 
00149   hitAssociator = new TrackerHitAssociator(iEvent);//delete deleteHitAssociator() in TrackCandMaker.cc
00150 
00151   std::map<unsigned int, std::vector<PSimHit> >& theHitsMap = hitAssociator->SimHitMap;
00152   idHitsMap.clear();
00153 
00154   for (std::map<unsigned int, std::vector<PSimHit> >::iterator it=theHitsMap.begin();
00155        it!=theHitsMap.end();it++){
00156     for (std::vector<PSimHit>::iterator isim = it->second.begin();
00157          isim != it->second.end(); ++isim){
00158       idHitsMap[isim->trackId()].push_back(&*isim);
00159     }
00160   }
00161   
00162   for (std::map<unsigned int,std::vector<PSimHit*> >::iterator it=idHitsMap.begin();
00163        it!=idHitsMap.end();it++){
00164     sort(it->second.begin(),it->second.end(),less_mag());
00165     for (std::vector<PSimHit*>::iterator isim = it->second.begin();
00166          isim != it->second.end(); ++isim){
00167       const GeomDetUnit* detUnit = theTrackerGeom->idToDetUnit( DetId((*isim)->detUnitId()));
00168       dumpSimHit(SimHit( (*isim), detUnit));
00169     }
00170   }
00171   
00172 }
00173 
00174 void CkfDebugger::dumpSimHit(const SimHit& hit) const
00175 {
00176   GlobalPoint pos = hit.globalPosition();
00177   edm::LogVerbatim("CkfDebugger") << "SimHit pos" << pos
00178                                   << " r=" << pos.perp() << " phi=" << pos.phi() 
00179                                   << " trackId=" << hit.trackId() 
00180                                   << " particleType=" << hit.particleType() 
00181                                   << " pabs=" << hit.pabs() 
00182                                   << " processType=" << hit. processType();
00183 }
00184 
00185 
00186 bool CkfDebugger::analyseCompatibleMeasurements(const Trajectory& traj,
00187                                                 const std::vector<TrajectoryMeasurement>& meas,
00188                                                 const MeasurementTracker* aMeasurementTracker, 
00189                                                 const Propagator*                     propagator,
00190                                                 const Chi2MeasurementEstimatorBase*   estimator,
00191                                                 const TransientTrackingRecHitBuilder* aTTRHBuilder)
00192 {
00193   LogTrace("CkfDebugger") << "\nnow in analyseCompatibleMeasurements" ;
00194   LogTrace("CkfDebugger") << "number of input hits:" << meas.size() ;
00195   for(std::vector<TrajectoryMeasurement>::const_iterator tmpIt=meas.begin();tmpIt!=meas.end();tmpIt++){
00196     if (tmpIt->recHit()->isValid()) LogTrace("CkfDebugger") << "valid hit at position:" << tmpIt->recHit()->globalPosition() ;
00197   }
00198   theForwardPropagator = propagator;
00199   theChi2 = estimator;
00200   theMeasurementTracker = aMeasurementTracker;
00201   theGeomSearchTracker = theMeasurementTracker->geometricSearchTracker();
00202   theTTRHBuilder = aTTRHBuilder;
00203   unsigned int trajId = 0;
00204   if ( !correctTrajectory(traj, trajId)) {
00205     LogTrace("CkfDebugger") << "trajectory not correct" ;
00206     return true;
00207   } // only correct trajectories analysed
00208   LogTrace("CkfDebugger") << "correct trajectory" ;
00209 
00210   if (traj.measurements().size() == 2){
00211     if ( testSeed(traj.firstMeasurement().recHit(),traj.lastMeasurement().recHit(),traj.lastMeasurement().updatedState()) == -1 ) {
00212       LogTrace("CkfDebugger") << "Seed has delta" ;
00213       seedWithDelta++;
00214       return false;//true;//false?
00215     }
00216   }
00217 
00218   //const PSimHit* correctHit = nextCorrectHit(traj, trajId);
00219   //if ( correctHit == 0) return true; // no more simhits on this track
00220   std::vector<const PSimHit*> correctHits = nextCorrectHits(traj, trajId);
00221   if ( correctHits.size() == 0) return true; // no more simhits on this track
00222 
00223   for (std::vector<const PSimHit*>::iterator corHit=correctHits.begin();corHit!=correctHits.end();corHit++){
00224     for (std::vector<TM>::const_iterator i=meas.begin(); i!=meas.end(); i++) {
00225       if (correctMeas( *i, *corHit)) {
00226         LogTrace("CkfDebugger") << "Correct hit found at position " << i-meas.begin() ;
00227         return true;
00228       }
00229     }
00230   }
00231 
00232   //debug why the first hit in correctHits is not found 
00233   //FIXME should loop over all hits
00234   const PSimHit* correctHit = *(correctHits.begin());
00235 
00236   // correct hit not found
00237   edm::LogVerbatim("CkfDebugger") << std::endl << "CkfDebugger: problem found: correct hit not found by findCompatibleMeasurements" ;
00238   edm::LogVerbatim("CkfDebugger") << "The correct hit position is " << position(correctHit)  << " lp " << correctHit->localPosition() ;
00239   edm::LogVerbatim("CkfDebugger") << "The size of the meas vector is " << meas.size() ;
00240   dump[0]++;problems++;
00241 
00242   for (std::vector<TM>::const_iterator i=meas.begin(); i!=meas.end(); i++) {
00243     edm::LogVerbatim("CkfDebugger") << "Is the hit valid? " << i->recHit()->isValid() ;
00244     if (i->recHit()->isValid()) {
00245       edm::LogVerbatim("CkfDebugger") << "RecHit at " << i->recHit()->globalPosition()
00246                                       << " layer " <<   ((i->recHit()->det()->geographicalId().rawId() >>16) & 0xF)
00247                                       << " subdet " << i->recHit()->det()->geographicalId().subdetId() 
00248                                       << " Chi2 " << i->estimate() ;
00249     }
00250     else if (i->recHit()->det() == 0) {
00251       edm::LogVerbatim("CkfDebugger") << "Invalid RecHit returned with zero Det pointer" ;
00252     }
00253     else if (i->recHit()->det() == det(correctHit)) {
00254       edm::LogVerbatim("CkfDebugger") << "Invalid hit returned in correct Det" ;
00255     }
00256     else {
00257       edm::LogVerbatim("CkfDebugger") << "Invalid hit returned in Det at gpos " << i->recHit()->det()->position()
00258                                       << " correct Det is at " << det(correctHit)->position() ;
00259     }
00260   }
00261 
00262   //Look if the correct RecHit exists
00263   std::pair<CTTRHp, double> correctRecHit = 
00264     analyseRecHitExistance( *correctHit, traj.lastMeasurement().updatedState());
00265   if (correctRecHit.first==0 ) {
00266     //the hit does not exist or is uncorrectly matched
00267     if ( fabs(correctRecHit.second-0)<0.01 ) {dump[1]++;}//other
00268     if ( fabs(correctRecHit.second+1)<0.01 ) {dump[8]++;}//propagation
00269     if ( fabs(correctRecHit.second+2)<0.01 ) {dump[9]++;}//No component is found
00270     if ( fabs(correctRecHit.second+3)<0.01 ) {dump[10]++;}//Partner measurementDet not found
00271     if ( fabs(correctRecHit.second+4)<0.01 ) {dump[11]++;}//glued MeasurementDet not found
00272     if ( fabs(correctRecHit.second+5)<0.01 ) {dump[12]++;}//matched not found
00273     if ( fabs(correctRecHit.second+6)<0.01 ) {dump[13]++;}//Matched not associated
00274     if ( fabs(correctRecHit.second+7)<0.01 ) {dump[14]++;}//Only one component is found
00275     if ( fabs(correctRecHit.second+8)<0.01 ) {dump[15]++;}//not found (is not a glued det)
00276   }
00277   else {
00278     //the hit exists: why wasn't it found?
00279     int result = analyseRecHitNotFound(traj,correctRecHit.first);
00280     if (result == 5){
00281       if (correctRecHit.second>30) {
00282         edm::LogVerbatim("CkfDebugger") << "Outling RecHit at pos=" << correctRecHit.first->globalPosition()
00283                                         << " from SimHit at pos="<< position(correctHit) 
00284                                         << " det=" << correctHit->detUnitId() << " process=" << correctHit->processType() ;
00285         if (hasDelta(correctHit)){
00286           edm::LogVerbatim("CkfDebugger") << "there are deltas on this det" ;
00287           chi2gt30delta++;
00288           dump5[pair<int,int>((correctRecHit.first->det()->geographicalId().subdetId()-1),(layer(correctRecHit.first->det()))-1)]++;  
00289         }else{
00290           edm::LogVerbatim("CkfDebugger") << "no deltas on this det" ;
00291           dump[5]++;
00292           chi2gt30++;
00293           dump3[pair<int,int>((correctRecHit.first->det()->geographicalId().subdetId()-1),(layer(correctRecHit.first->det()))-1)]++;
00294           CTTRHp h1 = traj.measurements()[0].recHit();
00295           CTTRHp h2 = traj.measurements()[1].recHit();
00296           TSOS t = traj.measurements()[1].updatedState();
00297           double chi2 = testSeed(h1,h2,t);
00298           if (chi2==-1) {
00299             edm::LogVerbatim("CkfDebugger") << "there were deltas in the seed" ;
00300             chi2gt30deltaSeed++;
00301           }
00302           else {
00303             hchi2seedProb->Fill(chi2);
00304             edm::LogVerbatim("CkfDebugger") << "no deltas in the seed. What is wrong?" ;
00305 
00306             TSOS detState = theForwardPropagator->propagate( traj.lastMeasurement().updatedState(), correctRecHit.first->det()->surface());
00307             TSOS simDetState = theForwardPropagator->propagate( traj.lastMeasurement().updatedState(), det(correctHit)->surface());
00308 
00309             if (true/*detState.globalMomentum().y()>0*/){
00310               int subdetId = correctRecHit.first->det()->geographicalId().subdetId();
00311               int layerId  = layer(correctRecHit.first->det());
00312 
00313 
00314               LogTrace("CkfDebugger") << "position(correctHit)=" << position(correctHit) ;
00315               LogTrace("CkfDebugger") << "correctRecHit.first->globalPosition()=" << correctRecHit.first->globalPosition() ;
00316               LogTrace("CkfDebugger") << "detState.globalPosition()=" << detState.globalPosition() ;
00317               LogTrace("CkfDebugger") << "simDetState.globalPosition()=" << simDetState.globalPosition() ;
00318 
00319               LogTrace("CkfDebugger") << "correctHit->localPosition()=" << correctHit->localPosition() ;
00320               LogTrace("CkfDebugger") << "correctRecHit.first->localPosition()=" << correctRecHit.first->localPosition() ;
00321               LogTrace("CkfDebugger") << "correctRecHit.first->localPositionError()=" << correctRecHit.first->localPositionError() ;
00322               LogTrace("CkfDebugger") << "detState.localPosition()=" << detState.localPosition() ;
00323               LogTrace("CkfDebugger") << "detState.localError().positionError()=" << detState.localError().positionError() ;
00324               LogTrace("CkfDebugger") << "simDetState.localPosition()=" << simDetState.localPosition() ;
00325               LogTrace("CkfDebugger") << "simDetState.localError().positionError()=" << simDetState.localError().positionError() ;
00326               double pullx_shrh = (correctHit->localPosition().x()-correctRecHit.first->localPosition().x())/
00327                 sqrt(correctRecHit.first->localPositionError().xx());
00328               double pully_shrh = 0;
00329               if (correctRecHit.first->localPositionError().yy()!=0) 
00330                 pully_shrh = (correctHit->localPosition().y()-correctRecHit.first->localPosition().y())/
00331                   sqrt(correctRecHit.first->localPositionError().yy());
00332               double pullx_shst = (correctHit->localPosition().x()-simDetState.localPosition().x())/
00333                 sqrt(simDetState.localError().positionError().xx());
00334               double pully_shst = (correctHit->localPosition().y()-simDetState.localPosition().y())/
00335                 sqrt(simDetState.localError().positionError().yy());
00336 
00337               LogTrace("CkfDebugger") << "pullx(sh-rh)=" << pullx_shrh ;
00338               LogTrace("CkfDebugger") << "pully(sh-rh)=" << pully_shrh ;
00339               LogTrace("CkfDebugger") << "pullx(sh-st)=" << pullx_shst ;
00340               LogTrace("CkfDebugger") << "pully(sh-st)=" << pully_shst ;
00341 
00342               LogTrace("CkfDebugger") << "pullx(st-rh)=" << (detState.localPosition().x()-correctRecHit.first->localPosition().x())/
00343                 sqrt(correctRecHit.first->localPositionError().xx()+detState.localError().positionError().xx()) ;
00344 
00345               std::pair<double,double> pulls = computePulls(correctRecHit.first, detState);
00346               if (subdetId>0 &&subdetId<7 && layerId>0 && layerId<10) {
00347                 stringstream title;
00348                 title.str("");
00349                 title << "pullX_" << subdetId << "-" << layerId << "_sh-rh";
00350                 hPullX_shrh[title.str()]->Fill( pullx_shrh );
00351                 title.str("");
00352                 title << "pullY_" << subdetId << "-" << layerId << "_sh-rh";
00353                 hPullY_shrh[title.str()]->Fill( pully_shrh );
00354                 title.str("");
00355                 title << "pullX_" << subdetId << "-" << layerId <<"_sh-st";
00356                 hPullX_shst[title.str()]->Fill( pullx_shst );
00357                 title.str("");
00358                 title << "pullY_" << subdetId << "-" << layerId <<"_sh-st";
00359                 hPullY_shst[title.str()]->Fill( pully_shst );
00360                 title.str("");
00361                 title << "pullX_" << subdetId << "-" << layerId <<"_st-rh";
00362                 hPullX_strh[title.str()]->Fill(pulls.first);
00363                 title.str("");
00364                 title << "pullY_" << subdetId << "-" << layerId <<"_st-rh";
00365                 hPullY_strh[title.str()]->Fill(pulls.second);
00366 
00367                 GlobalPoint shGPos = position(correctHit);
00368                 GlobalPoint stGPos = simDetState.globalPosition();
00369                 GlobalError stGPosErr = simDetState.cartesianError().position();
00370                 double pullGPx = (shGPos.x()-stGPos.x())/sqrt(stGPosErr.cxx());
00371                 title.str("");
00372                 title << "PullGP_X_" << subdetId << "-" << layerId << "_sh-st";
00373                 hPullGP_X_shst[title.str()]->Fill(pullGPx);
00374                 title.str("");
00375                 title << "PullGP_Y_" << subdetId << "-" << layerId << "_sh-st";
00376                 hPullGP_Y_shst[title.str()]->Fill((shGPos.y()-stGPos.y())/sqrt(stGPosErr.cyy()));
00377                 title.str("");
00378                 title << "PullGP_Z_" << subdetId << "-" << layerId << "_sh-st";
00379                 hPullGP_Z_shst[title.str()]->Fill((shGPos.z()-stGPos.z())/sqrt(stGPosErr.czz()));
00380 
00381                 if (subdetId==3&&layerId==1){
00382                   hPullGPXvsGPX_shst->Fill(pullGPx,shGPos.x());
00383                   hPullGPXvsGPY_shst->Fill(pullGPx,shGPos.y());
00384                   hPullGPXvsGPZ_shst->Fill(pullGPx,shGPos.z());
00385                   hPullGPXvsGPr_shst->Fill(pullGPx,shGPos.mag());
00386                   hPullGPXvsGPeta_shst->Fill(pullGPx,shGPos.eta());
00387                   hPullGPXvsGPphi_shst->Fill(pullGPx,shGPos.phi());
00388                 }
00389                 if (dynamic_cast<const SiStripMatchedRecHit2D*>(correctRecHit.first->hit())) {
00390                   LogTrace("CkfDebugger") << "MONO HIT";
00391                   auto m = dynamic_cast<const SiStripMatchedRecHit2D*>(correctRecHit.first->hit())->monoHit();
00392                   CTTRHp tMonoHit = theTTRHBuilder->build(&m);
00393                   const PSimHit sMonoHit = *(hitAssociator->associateHit(*tMonoHit->hit()).begin());
00394                   TSOS monoState = theForwardPropagator->propagate( traj.lastMeasurement().updatedState(), tMonoHit->det()->surface());
00395                   double pullM_shrh = (sMonoHit.localPosition().x()-tMonoHit->localPosition().x())/
00396                     sqrt(tMonoHit->localPositionError().xx());
00397                   double pullM_shst = (sMonoHit.localPosition().x()-monoState.localPosition().x())/
00398                     sqrt(monoState.localError().positionError().xx());
00399                   std::pair<double,double> pullsMono = computePulls(tMonoHit, monoState);
00400                   title.str("");
00401                   title << "pullM_" << subdetId << "-" << layerId << "_sh-rh";
00402                   hPullM_shrh[title.str()]->Fill(pullM_shrh);
00403                   title.str("");
00404                   title << "pullM_" << subdetId << "-" << layerId << "_sh-st";
00405                   hPullM_shst[title.str()]->Fill(pullM_shst);
00406                   title.str("");
00407                   title << "pullM_" << subdetId << "-" << layerId << "_st-rh";
00408                   hPullM_strh[title.str()]->Fill(pullsMono.first);
00409 
00410                   LogTrace("CkfDebugger") << "STEREO HIT";
00411                   auto s= dynamic_cast<const SiStripMatchedRecHit2D*>(correctRecHit.first->hit())->stereoHit();
00412                   CTTRHp tStereoHit = theTTRHBuilder->build(&s);
00413                   const PSimHit sStereoHit = *(hitAssociator->associateHit(*tStereoHit->hit()).begin());
00414                   TSOS stereoState = theForwardPropagator->propagate( traj.lastMeasurement().updatedState(), tStereoHit->det()->surface());
00415                   double pullS_shrh = (sStereoHit.localPosition().x()-tStereoHit->localPosition().x())/
00416                     sqrt(tStereoHit->localPositionError().xx());
00417                   double pullS_shst = (sStereoHit.localPosition().x()-stereoState.localPosition().x())/
00418                     sqrt(stereoState.localError().positionError().xx());
00419                   std::pair<double,double> pullsStereo = computePulls(tStereoHit, stereoState);
00420                   title.str("");
00421                   title << "pullS_" << subdetId  << "-" << layerId << "_sh-rh";
00422                   hPullS_shrh[title.str()]->Fill(pullS_shrh);
00423                   title.str("");
00424                   title << "pullS_" << subdetId << "-" << layerId << "_sh-st";
00425                   hPullS_shst[title.str()]->Fill(pullS_shst);
00426                   title.str("");
00427                   title << "pullS_" << subdetId << "-" << layerId << "_st-rh";
00428                   hPullS_strh[title.str()]->Fill(pullsStereo.first);
00429                 }
00430               } else 
00431                 edm::LogVerbatim("CkfDebugger") << "unexpected result: wrong det or layer id " 
00432                                                 << subdetId << " " << layerId << " " 
00433                                                 << correctRecHit.first->det()->geographicalId().rawId();
00434             }
00435           }
00436         }
00437       }
00438       else {
00439         edm::LogVerbatim("CkfDebugger") << "unexpected result " << correctRecHit.second ;
00440         dump[6]++;chi2ls30++;
00441       }
00442     }
00443     else dump[result]++;
00444     if (result == 3){
00445       dump2[pair<int,int>((correctRecHit.first->det()->geographicalId().subdetId()-1),(layer(correctRecHit.first->det()))-1)]++; 
00446     }
00447     if (result == 4){
00448       dump4[pair<int,int>((correctRecHit.first->det()->geographicalId().subdetId()-1),(layer(correctRecHit.first->det()))-1)]++; 
00449     }
00450     if (correctRecHit.second>30) {
00451       dump[7]++;totchi2gt30++; 
00452     }
00453   }
00454   return false;
00455 }
00456 
00457 
00458 bool CkfDebugger::correctTrajectory( const Trajectory& traj,unsigned int& trajId) const
00459 {
00460   LogTrace("CkfDebugger") << "now in correctTrajectory" ;
00461   Trajectory::RecHitContainer hits = traj.recHits();
00462 
00463   std::vector<SimHitIdpr> currentTrackId = hitAssociator->associateHitId(*hits.front()->hit());
00464   if (currentTrackId.size() == 0) return false;
00465 
00466   for (Trajectory::RecHitContainer::const_iterator rh=hits.begin(); rh!=hits.end(); ++rh) {
00467 
00468     //if invalid hit exit
00469     if (!(*rh)->hit()->isValid()) {
00470       //LogTrace("CkfDebugger") << "invalid hit" ;
00471       return false;
00472     }
00473 
00474     //if hits from deltas exit
00475     bool nogoodhit = true;
00476     std::vector<PSimHit> assSimHits = hitAssociator->associateHit(*(*rh)->hit());
00477     for (std::vector<PSimHit>::iterator shit=assSimHits.begin();shit!=assSimHits.end();shit++){
00478       if (goodSimHit(*shit)) nogoodhit=false;
00479     }
00480     if (nogoodhit) return false;
00481     
00482     //all hits must be associated to the same sim track
00483     bool test = true;
00484     std::vector<SimHitIdpr> nextTrackId = hitAssociator->associateHitId(*(*rh)->hit());
00485     for (std::vector<SimHitIdpr>::iterator i=currentTrackId.begin();i!=currentTrackId.end();i++){
00486       for (std::vector<SimHitIdpr>::iterator j=nextTrackId.begin();j!=nextTrackId.end();j++){
00487         if (i->first == j->first) test = false;
00488         //LogTrace("CkfDebugger") << "valid " << *i << " " << *j ;
00489         trajId = j->first;
00490       }
00491     }
00492     if (test) {/*LogTrace("CkfDebugger") << "returning false" ;*/return false;}
00493     //     std::vector<PSimHit*> simTrackHits = idHitsMap[trajId];
00494     //     if (!goodSimHit(simTrackHits.))
00495   }
00496   //LogTrace("CkfDebugger") << "returning true" ;
00497   return true;
00498 }
00499 
00500 int CkfDebugger::assocTrackId(CTTRHp rechit) const
00501 {
00502   LogTrace("CkfDebugger") << "now in assocTrackId" ;
00503 
00504   if (!rechit->hit()->isValid()) {
00505     return -1;
00506   }
00507 
00508   std::vector<SimHitIdpr> ids = hitAssociator->associateHitId(*rechit->hit());
00509   if (ids.size()!=0) {
00510     return ids[0].first;//FIXME if size>1!!
00511   }
00512   else {
00513     return -1;
00514   }
00515 }
00516 
00517 
00518 vector<const PSimHit*> CkfDebugger::nextCorrectHits( const Trajectory& traj, unsigned int& trajId)
00519 {
00520   std::vector<const PSimHit*> result;
00521   // find the component of the RecHit at largest distance from origin (FIXME: should depend on propagation direction)
00522   LogTrace("CkfDebugger") << "now in nextCorrectHits" ;
00523   TransientTrackingRecHit::ConstRecHitPointer lastRecHit = traj.lastMeasurement().recHit();
00524   TransientTrackingRecHit::RecHitContainer comp = lastRecHit->transientHits();
00525   if (!comp.empty()) {
00526     float maxR = 0;
00527     for (TransientTrackingRecHit::RecHitContainer::const_iterator ch=comp.begin(); 
00528          ch!=comp.end(); ++ch) {
00529       if ((*ch)->globalPosition().mag() > maxR) lastRecHit = *ch; 
00530       maxR = (*ch)->globalPosition().mag();
00531     }
00532   }
00533   edm::LogVerbatim("CkfDebugger") << "CkfDebugger: lastRecHit is at gpos " << lastRecHit->globalPosition() 
00534                                   << " layer " << layer((lastRecHit->det())) 
00535                                   << " subdet " << lastRecHit->det()->geographicalId().subdetId() ;
00536 
00537   //find the simHits associated to the recHit
00538   const std::vector<PSimHit>& pSimHitVec = hitAssociator->associateHit(*lastRecHit->hit());
00539   for (std::vector<PSimHit>::const_iterator shit=pSimHitVec.begin();shit!=pSimHitVec.end();shit++){
00540     const GeomDetUnit* detUnit = theTrackerGeom->idToDetUnit( DetId(shit->detUnitId()));
00541     LogTrace("CkfDebugger") << "from hitAssociator SimHits are at GP=" << detUnit->toGlobal( shit->localPosition())
00542                                     << " traId=" << shit->trackId() << " particleType " << shit->particleType() 
00543                                     << " pabs=" << shit->pabs() << " detUnitId=" << shit->detUnitId() << " layer " << layer((det(&*shit))) 
00544                                     << " subdet " << det(&*shit)->geographicalId().subdetId() ;
00545   }
00546 
00547   //choose the simHit from the same track that has the highest tof
00548   const PSimHit * lastPSH = 0;
00549   if (!pSimHitVec.empty()) {
00550     float maxTOF = 0;
00551     for (std::vector<PSimHit>::const_iterator ch=pSimHitVec.begin(); ch!=pSimHitVec.end(); ++ch) {
00552       if ( ( ch->trackId()== trajId) && (ch->timeOfFlight() > maxTOF)  && ( goodSimHit(*ch) )) {
00553         lastPSH = &*ch; 
00554         maxTOF = lastPSH->timeOfFlight();
00555       }
00556     }
00557   }
00558   else return result;//return empty vector: no more hits on the sim track
00559   if (lastPSH == 0) return result; //return empty vector: no more good hits on the sim track
00560   edm::LogVerbatim("CkfDebugger") << "CkfDebugger: corresponding SimHit is at gpos " << position(&*lastPSH) ;
00561 
00562   //take the simHits on the simTrack that are in the nextLayer (could be > 1 if overlap or matched)
00563   std::vector<PSimHit*> trackHits = idHitsMap[trajId];
00564   if (fabs((double)(trackHits.back()->detUnitId()-lastPSH->detUnitId()))<1 ) return result;//end of sim track
00565   std::vector<PSimHit*>::iterator currentIt = trackHits.end();
00566   for (std::vector<PSimHit*>::iterator it=trackHits.begin();
00567        it!=trackHits.end();it++){
00568     if (goodSimHit(**it) && //good hit
00569         ( lastPSH->timeOfFlight()<(*it)->timeOfFlight() ) && //greater tof
00570         //( fabs((double)((*it)->detUnitId()-(lastPSH->detUnitId()) ))>1) && //not components of the same matched hit
00571         ( (det(lastPSH)->geographicalId().subdetId()!=det(*it)->geographicalId().subdetId()) || 
00572           (layer(det(lastPSH))!=layer(det(*it)) ) ) //change layer or detector(tib,tob,...)
00573         ){
00574       edm::LogVerbatim("CkfDebugger") << "Next good PSimHit is at gpos " << position(*it) ;
00575       result.push_back(*it);
00576       currentIt = it;
00577       break;
00578     }
00579   }
00580   bool samelayer = true;
00581   if (currentIt!=(trackHits.end()-1) && currentIt!=trackHits.end()) {
00582     for (std::vector<PSimHit*>::iterator nextIt = currentIt; (samelayer && nextIt!=trackHits.end()) ;nextIt++){
00583       if (goodSimHit(**nextIt)){
00584         if ( (det(*nextIt)->geographicalId().subdetId()==det(*currentIt)->geographicalId().subdetId()) && 
00585              (layer(det(*nextIt))==layer(det(*currentIt)) ) ) {
00586           result.push_back(*nextIt);
00587         }
00588         else samelayer = false;
00589       }
00590     }
00591   }
00592   
00593   return result;
00594 }
00595 
00596 bool CkfDebugger::goodSimHit(const PSimHit& sh) const
00597 {
00598   if (sh.pabs() > 0.9) return true; // GeV, reject delta rays from association
00599   else return false;
00600 }
00601 
00602 
00603 bool CkfDebugger::associated(CTTRHp rechit, const PSimHit& pSimHit) const
00604 {
00605   LogTrace("CkfDebugger") << "now in associated" ;
00606 
00607   if (!rechit->isValid()) return false;
00608   //   LogTrace("CkfDebugger") << "rec hit valid" ;
00609   const std::vector<PSimHit>& pSimHitVec = hitAssociator->associateHit(*rechit->hit());
00610   //   LogTrace("CkfDebugger") << "size=" << pSimHitVec.size() ;
00611   for (std::vector<PSimHit>::const_iterator shit=pSimHitVec.begin();shit!=pSimHitVec.end();shit++){
00612     //const GeomDetUnit* detUnit = theTrackerGeom->idToDetUnit( DetId(shit->detUnitId()));
00613     //         LogTrace("CkfDebugger") << "pSimHit.timeOfFlight()=" << pSimHit.timeOfFlight() 
00614     //           << " pSimHit.pabs()=" << pSimHit.pabs() << " GP=" << position(&pSimHit);
00615     //         LogTrace("CkfDebugger") << "(*shit).timeOfFlight()=" << (*shit).timeOfFlight() 
00616     //           << " (*shit).pabs()=" << (*shit).pabs() << " GP=" << detUnit->toGlobal( shit->localPosition());
00617     if ( ( fabs((*shit).timeOfFlight()-pSimHit.timeOfFlight())<1e-9  ) && 
00618          ( fabs((*shit).pabs()-pSimHit.pabs())<1e-9 ) ) return true;
00619   }
00620   return false;
00621 }
00622 
00623 bool CkfDebugger::correctMeas( const TM& tm, const PSimHit* correctHit) const
00624 {
00625   LogTrace("CkfDebugger") << "now in correctMeas" ;
00626   CTTRHp recHit = tm.recHit();
00627   if (recHit->isValid()) LogTrace("CkfDebugger") << "hit at position:" << recHit->globalPosition() ;
00628   TransientTrackingRecHit::RecHitContainer comp = recHit->transientHits();
00629   if (comp.empty()) {
00630     //     LogTrace("CkfDebugger") << "comp.empty()==true" ;
00631     return associated( recHit, *correctHit);
00632   }
00633   else {
00634     for (TransientTrackingRecHit::RecHitContainer::const_iterator ch=comp.begin(); 
00635          ch!=comp.end(); ++ch) {
00636       if ( associated( recHit, *correctHit)) {
00637         // check if the other components are associated to the same trackId
00638         for (TransientTrackingRecHit::RecHitContainer::const_iterator ch2=comp.begin(); 
00639              ch2!=comp.end(); ++ch2) {
00640           if (ch2 == ch) continue;
00642           //      LogTrace("CkfDebugger") << "correctHit->trackId()=" << correctHit->trackId() ;
00643           bool test=true;
00644           std::vector<SimHitIdpr> ids = hitAssociator->associateHitId(*(*ch2)->hit());
00645           for (std::vector<SimHitIdpr>::iterator j=ids.begin();j!=ids.end();j++){
00646             //      LogTrace("CkfDebugger") << "j=" <<j->first;
00647             if (correctHit->trackId()==j->first) {
00648               test=false;
00649               //              LogTrace("CkfDebugger") << correctHit->trackId()<< " " <<j->first;
00650             }
00651           }
00652           if (assocTrackId( *ch2) != ((int)( correctHit->trackId())) ) {LogTrace("CkfDebugger") << "returning false 1" ;/*return false;*/}//fixme
00653           if (test) {
00654             //      LogTrace("CkfDebugger") << "returning false 2" ;
00655             return false; // not all components from same simtrack
00656           }
00657           //      if (assocTrackId( **ch2) != ((int)( correctHit->trackId())) ) {
00658           //        return false; // not all components from same simtrack
00659           //      }
00660         }
00661         return true; // if all components from same simtrack
00662       }
00663     }
00664     return false;
00665   }
00666 }
00667 
00668 //this checks only if there is the rechit on the det where the sim hit is 
00669 pair<CTTRHp, double> CkfDebugger::analyseRecHitExistance( const PSimHit& sh, const TSOS& startingState)
00670 {
00671   LogTrace("CkfDebugger") << "now in analyseRecHitExistance" ;
00672 
00673   std::pair<CTTRHp, double> result;
00674   
00675   const MeasurementDet* simHitDet = theMeasurementTracker->idToDet( DetId( sh.detUnitId()));
00676   TSOS simHitState = TSOSFromSimHitFactory()(sh, *det(&sh), *theMagField);
00677   MeasurementDet::RecHitContainer recHits = simHitDet->recHits( simHitState);//take all hits from det
00678 
00679   //check if the hit is not present or is a problem of association
00680   TSOS firstDetState = theForwardPropagator->propagate( startingState, det(&sh)->surface());
00681   if (!firstDetState.isValid()) {
00682     edm::LogVerbatim("CkfDebugger") << "CkfDebugger: propagation failed from state " << startingState << " to first det surface " 
00683                                     << position(&sh) ;
00684     propagation++;
00685     return std::pair<CTTRHp, double>((CTTRHp)(0),-1);
00686   }
00687 
00688   bool found = false;
00689   for ( MeasurementDet::RecHitContainer::const_iterator rh = recHits.begin(); rh != recHits.end(); rh++) {
00690     if ( associated( *rh, sh)) {
00691       found = true;
00692       result = std::pair<CTTRHp, double>(*rh,theChi2->estimate( firstDetState, **rh).second);
00693       edm::LogVerbatim("CkfDebugger") << "CkfDebugger: A RecHit associated to the correct Simhit exists at lpos " 
00694                                       << (**rh).localPosition()
00695                                       << " gpos " << (**rh).globalPosition()
00696                                       << " layer " <<   layer((**rh).det())
00697                                       << " subdet " << (**rh).det()->geographicalId().subdetId() 
00698                                       << " Chi2 " << theChi2->estimate( firstDetState, **rh).second;
00699     }
00700   }
00701   if (!found) {
00702     edm::LogVerbatim("CkfDebugger") << "CkfDebugger: there is no RecHit associated to the correct SimHit." ;
00703     edm::LogVerbatim("CkfDebugger") << " There are " <<  recHits.size() << " RecHits in the simHit DetUnit" ;
00704     edm::LogVerbatim("CkfDebugger") << "SH GP=" << position(&sh) << " subdet=" << det(&sh)->geographicalId().subdetId() 
00705                                     << " layer=" << layer(det(&sh)) ;
00706     int y=0;
00707     for (MeasurementDet::RecHitContainer::const_iterator rh = recHits.begin(); rh != recHits.end(); rh++)
00708       edm::LogVerbatim("CkfDebugger") << "RH#" << y++ << " GP=" << (**rh).globalPosition() << " subdet=" << (**rh).det()->geographicalId().subdetId() 
00709                                       << " layer=" << layer((**rh).det()) ;
00710     for ( MeasurementDet::RecHitContainer::const_iterator rh = recHits.begin(); rh != recHits.end(); rh++) {
00711       edm::LogVerbatim("CkfDebugger") << "Non-associated RecHit at pos " << (**rh).localPosition() ;
00712     }
00713   }
00714 
00715   bool found2 = false;
00716   const PSimHit* sh2;
00717   StripSubdetector subdet( det(&sh)->geographicalId());
00718   if (!subdet.glued()) {
00719     edm::LogVerbatim("CkfDebugger") << "The DetUnit is not part of a GluedDet" ;
00720     if (found) {
00721       if (result.second>30){
00722         LogTrace("CkfDebugger") << "rh->parameters()=" << result.first->parameters() ;
00723         LogTrace("CkfDebugger") << "rh->parametersError()=" << result.first->parametersError() ;
00724         MeasurementExtractor me(firstDetState);
00725         AlgebraicVector r(result.first->parameters() - me.measuredParameters(*result.first));
00726         LogTrace("CkfDebugger") << "me.measuredParameters(**rh)=" << me.measuredParameters(*result.first) ;
00727         LogTrace("CkfDebugger") << "me.measuredError(**rh)=" << me.measuredError(*result.first) ;
00728         AlgebraicSymMatrix R(result.first->parametersError() + me.measuredError(*result.first));
00729         LogTrace("CkfDebugger") << "r=" << r ;
00730         LogTrace("CkfDebugger") << "R=" << R ;
00731         int ierr; 
00732         R.invert(ierr);
00733         LogTrace("CkfDebugger") << "R(-1)=" << R ;
00734         LogTrace("CkfDebugger") << "chi2=" << R.similarity(r) ;
00735       }
00736       return result;
00737     }
00738     else {
00739       simple_hit_not_found++;
00740       return std::pair<CTTRHp, double>((CTTRHp)(0),-8);//not found (is not a glued det)
00741     }
00742   } else {
00743     edm::LogVerbatim("CkfDebugger") << "The DetUnit is part of a GluedDet" ;
00744     DetId partnerDetId = DetId( subdet.partnerDetId());
00745 
00746     sh2 = pSimHit( sh.trackId(), partnerDetId);
00747     if (sh2 == 0) {
00748       edm::LogVerbatim("CkfDebugger") << "Partner DetUnit does not have a SimHit from the same track" ;
00749       if (found) {
00750         //projected rec hit
00751         TrackingRecHitProjector<ProjectedRecHit2D> proj;
00752         DetId gid = gluedId( subdet);
00753         const MeasurementDet* gluedDet = theMeasurementTracker->idToDet( gid);
00754         TSOS gluedTSOS = theForwardPropagator->propagate(startingState, gluedDet->geomDet().surface());
00755         CTTRHp projHit = proj.project( *result.first,gluedDet->geomDet(),gluedTSOS).get();
00756         //LogTrace("CkfDebugger") << proj.project( *result.first,gluedDet->geomDet(),gluedTSOS)->parameters() ;
00757         //LogTrace("CkfDebugger") << projHit->parametersError() ;
00758         double chi2 = theChi2->estimate(gluedTSOS, *proj.project( *result.first,gluedDet->geomDet(),gluedTSOS)).second;
00759         return std::pair<CTTRHp, double>(projHit,chi2);
00760       }
00761     }
00762     else {
00763       edm::LogVerbatim("CkfDebugger") << "Partner DetUnit has a good SimHit at gpos " << position(sh2) 
00764                                       << " lpos " << sh2->localPosition() ;
00765       //}
00766     
00767       const MeasurementDet* partnerDet = theMeasurementTracker->idToDet( partnerDetId);
00768       if (partnerDet == 0) {
00769         edm::LogVerbatim("CkfDebugger") << "Partner measurementDet not found!!!" ;
00770         partner_det_not_fuond++;
00771         return std::pair<CTTRHp, double>((CTTRHp)(0),-3);
00772       }
00773       TSOS simHitState2 = TSOSFromSimHitFactory()(*sh2, *det(sh2), *theMagField);
00774       MeasurementDet::RecHitContainer recHits2 = partnerDet->recHits( simHitState2);
00775 
00776       TSOS secondDetState = theForwardPropagator->propagate( startingState, det(sh2)->surface());
00777       if (!secondDetState.isValid()) {
00778         edm::LogVerbatim("CkfDebugger") << "CkfDebugger: propagation failed from state " << startingState << " to second det surface " 
00779                                         << position(sh2) ;
00780         propagation++;
00781         return std::pair<CTTRHp, double>((CTTRHp)(0),-1);
00782       }
00783 
00784       for ( MeasurementDet::RecHitContainer::const_iterator rh = recHits2.begin(); rh != recHits2.end(); rh++) {
00785         if ( associated( *rh, *sh2)) {
00786           found2 = true;
00787           edm::LogVerbatim("CkfDebugger") << "CkfDebugger: A RecHit associated to the correct Simhit exists at lpos " 
00788                                           << (**rh).localPosition()
00789                                           << " gpos " << (**rh).globalPosition()
00790                                           << " Chi2 " << theChi2->estimate( secondDetState, **rh).second
00791             ;
00792         }
00793       }
00794       if (!found2) {
00795         edm::LogVerbatim("CkfDebugger") << "CkfDebugger: there is no RecHit associated to the correct SimHit." ;
00796         LogTrace("CkfDebugger") << " There are " <<  recHits.size() << " RecHits in the simHit DetUnit" ;
00797         for ( MeasurementDet::RecHitContainer::const_iterator rh = recHits.begin(); rh != recHits.end(); rh++) {
00798           LogTrace("CkfDebugger") << "Non-associated RecHit at pos " << (**rh).localPosition() ;
00799         }
00800       }
00801     }
00802   }
00803 
00804   MeasurementDet::RecHitContainer gluedHits;
00805   if (found && found2) {
00806     // look in the glued det
00807     DetId gid = gluedId( subdet);
00808     const MeasurementDet* gluedDet = theMeasurementTracker->idToDet( gid);
00809     if ( gluedDet == 0) {
00810       edm::LogVerbatim("CkfDebugger") << "CkfDebugger ERROR: glued MeasurementDet not found!" ;
00811       glued_det_not_fuond++;
00812       return std::pair<CTTRHp, double>((CTTRHp)(0),-4);
00813     }
00814 
00815     TSOS gluedDetState = theForwardPropagator->propagate( startingState, gluedDet->surface());
00816     if (!gluedDetState.isValid()) {
00817       edm::LogVerbatim("CkfDebugger") << "CkfDebugger: propagation failed from state " << startingState << " to det surface " 
00818                                       << gluedDet->position() ;
00819       propagation++;
00820       return std::pair<CTTRHp, double>((CTTRHp)(0),-1);
00821     }
00822 
00823     gluedHits = gluedDet->recHits( gluedDetState);
00824     edm::LogVerbatim("CkfDebugger") << "CkfDebugger: the GluedDet returned " << gluedHits.size() << " hits" ;
00825     if (gluedHits.size()==0){
00826       edm::LogVerbatim("CkfDebugger") << "Found and associated mono and stereo recHits but not matched!!!" ;
00827       matched_not_found++;
00828       return std::pair<CTTRHp, double>((CTTRHp)(0),-5);
00829     } 
00830     bool found3 = false;
00831     for ( MeasurementDet::RecHitContainer::const_iterator rh = gluedHits.begin(); rh != gluedHits.end(); rh++) {
00832       if ( associated( *rh, sh) && associated( *rh, *sh2)) {
00833         double chi2 = theChi2->estimate(gluedDetState, **rh).second;
00834         edm::LogVerbatim("CkfDebugger") << "Matched hit at lpos " << (**rh).localPosition()
00835                                         << " gpos " << (**rh).globalPosition()
00836                                         << " has Chi2 " << chi2
00837           ;
00838         result = std::pair<CTTRHp, double>(&**rh,chi2);
00839         found3 = true;
00840         if (chi2>30){
00841           LogTrace("CkfDebugger") << "rh->parameters()=" << (*rh)->parameters() ;
00842           LogTrace("CkfDebugger") << "rh->parametersError()=" << (*rh)->parametersError() ;
00843           MeasurementExtractor me(gluedDetState);
00844           AlgebraicVector r((*rh)->parameters() - me.measuredParameters(**rh));
00845           LogTrace("CkfDebugger") << "me.measuredParameters(**rh)=" << me.measuredParameters(**rh) ;
00846           LogTrace("CkfDebugger") << "me.measuredError(**rh)=" << me.measuredError(**rh) ;
00847           AlgebraicSymMatrix R((*rh)->parametersError() + me.measuredError(**rh));
00848           LogTrace("CkfDebugger") << "r=" << r ;
00849           LogTrace("CkfDebugger") << "R=" << R ;
00850           int ierr; 
00851           R.invert(ierr);
00852           LogTrace("CkfDebugger") << "R(-1)=" << R ;
00853           LogTrace("CkfDebugger") << "chi2=" << R.similarity(r) ;
00854         }
00855         break;
00856       }
00857     }
00858     if (found3) return result;
00859     else {
00860       edm::LogVerbatim("CkfDebugger") << "Found and associated mono and stereo recHits. Matched found but not associated!!!" ;
00861       matched_not_associated++;
00862       return std::pair<CTTRHp, double>((CTTRHp)(0),-6);
00863     }
00864   }
00865   else if ( (found && !found2) || (!found && found2) ) {
00866     edm::LogVerbatim("CkfDebugger") << "Only one component is found" ;
00867     only_one_component++;
00868     return std::pair<CTTRHp, double>((CTTRHp)(0),-7);
00869   }
00870   else {
00871     edm::LogVerbatim("CkfDebugger") << "No component is found" ;
00872     no_component++;
00873     return std::pair<CTTRHp, double>((CTTRHp)(0),-2);
00874   }
00875   other++;
00876   return std::pair<CTTRHp, double>((CTTRHp)(0),0);//other
00877 }
00878 
00879 const PSimHit* CkfDebugger::pSimHit(unsigned int tkId, DetId detId)
00880 {
00881   for (std::vector<PSimHit*>::iterator shi=idHitsMap[tkId].begin(); shi!=idHitsMap[tkId].end(); ++shi) {    
00882     if ( (*shi)->detUnitId() == detId.rawId() && 
00883          //(shi)->trackId() == tkId &&
00884          goodSimHit(**shi) ) {
00885       return (*shi);
00886     }
00887   }
00888   return 0;
00889 }
00890 
00891 int CkfDebugger::analyseRecHitNotFound(const Trajectory& traj, CTTRHp correctRecHit)
00892 {
00893   unsigned int correctDetId = correctRecHit->det()->geographicalId().rawId();
00894   int correctLayId = layer(correctRecHit->det());
00895   LogTrace("CkfDebugger") << "correct layer id=" << correctLayId ;
00896 
00897   TSOS currentState( traj.lastMeasurement().updatedState() );
00898   std::vector<const DetLayer*> nl = traj.lastLayer()->nextLayers( *currentState.freeState(),traj.direction() );
00899   if (nl.empty()) {
00900     edm::LogVerbatim("CkfDebugger") << "no compatible layers" ;
00901     no_layer++;return 2;
00902   }
00903 
00904   TkLayerLess lless;//FIXME - was lless(traj.direction())
00905   const DetLayer* detLayer = 0;
00906   bool navLayerAfter = false;
00907   bool test = false;
00908   for (std::vector<const DetLayer*>::iterator il = nl.begin(); il != nl.end(); il++) {
00909     if ( dynamic_cast<const BarrelDetLayer*>(*il) ){
00910       const BarrelDetLayer* pbl = dynamic_cast<const BarrelDetLayer*>(*il);
00911       LogTrace("CkfDebugger") << "pbl->specificSurface().bounds().length()=" << pbl->specificSurface().bounds().length() ;
00912       LogTrace("CkfDebugger") << "pbl->specificSurface().bounds().width()=" << pbl->specificSurface().bounds().width() ;
00913     }
00914     int layId = layer(((*(*il)->basicComponents().begin())));
00915     LogTrace("CkfDebugger") << " subdet=" << (*(*il)->basicComponents().begin())->geographicalId().subdetId() << "layer id=" << layId ;
00916     if (layId==correctLayId) {
00917       test = true;
00918       detLayer = &**il;
00919       break;
00920     }
00921     if ( lless( *il, theGeomSearchTracker->detLayer(correctRecHit->det()->geographicalId()) )) 
00922       navLayerAfter = true; //it is enough that only one layer is after the correct one?
00923   }
00924 
00925   if (test) {
00926     edm::LogVerbatim("CkfDebugger") << "correct layer taken into account. layer id: " << correctLayId ;
00927   } else if (navLayerAfter){
00928     edm::LogVerbatim("CkfDebugger")<< "SimHit layer after the layers returned by Navigation.";
00929     edm::LogVerbatim("CkfDebugger")<< "Probably a missing SimHit." ;
00930     edm::LogVerbatim("CkfDebugger")<< "check: " << (correctRecHit->det()->geographicalId().subdetId()) << " " << (layer(correctRecHit->det()));
00931     dump6[pair<int,int>((correctRecHit->det()->geographicalId().subdetId()-1),(layer(correctRecHit->det()))-1)]++; 
00932     no_sim_hit++;return 16;
00933   }
00934   else {
00935     edm::LogVerbatim("CkfDebugger") << "correct layer NOT taken into account. correct layer id: " << correctLayId ;
00936     layer_not_found++;
00937     return 3;
00938   }
00939 
00940   typedef DetLayer::DetWithState  DetWithState;
00941   std::vector<DetWithState> compatDets = detLayer->compatibleDets(currentState,*theForwardPropagator,*theChi2);
00942   //   LogTrace("CkfDebugger") << "DEBUGGER" ;
00943   //   LogTrace("CkfDebugger") << "runned compatDets." ;
00944   //   LogTrace("CkfDebugger") << "started from the following TSOS:" ;
00945   //   LogTrace("CkfDebugger") << currentState ;
00946   //   LogTrace("CkfDebugger") << "number of dets found=" << compatDets.size() ;
00947   //   for (std::vector<DetWithState>::iterator det=compatDets.begin();det!=compatDets.end();det++){
00948   //     unsigned int detId = det->first->geographicalId().rawId();
00949   //     LogTrace("CkfDebugger") << "detId=" << detId ; 
00950   //   }
00951   bool test2 = false;
00952   for (std::vector<DetWithState>::iterator det=compatDets.begin();det!=compatDets.end();det++){
00953     unsigned int detId = det->first->geographicalId().rawId();
00954     //     LogTrace("CkfDebugger") << "detId=" << detId 
00955     //   << "\ncorrectRecHit->det()->geographicalId().rawId()=" << correctRecHit->det()->geographicalId().rawId() 
00956     //   << "\ngluedId(correctRecHit->det()->geographicalId()).rawId()=" << gluedId(correctRecHit->det()->geographicalId()).rawId()
00957     //   ; 
00958     if (detId==gluedId(correctRecHit->det()->geographicalId()).rawId()) {
00959       test2=true;
00960       break;
00961     }
00962   }
00963   
00964   if (test2){
00965     edm::LogVerbatim("CkfDebugger") << "correct det taken into account. correctDetId is: " << correctDetId 
00966                                     << ". please check chi2." ;
00967     return 5;
00968   }
00969   else {
00970     edm::LogVerbatim("CkfDebugger") << "correct det NOT taken into account. correctDetId: " << correctDetId ;
00971     det_not_found++;return 4;
00972   }
00973 
00974 }
00975 
00976 double CkfDebugger::testSeed(CTTRHp recHit1, CTTRHp recHit2, TSOS state){
00977   //edm::LogVerbatim("CkfDebugger") << "CkfDebugger::testSeed";
00978   //test Deltas
00979   const std::vector<PSimHit>& pSimHitVec1 = hitAssociator->associateHit(*recHit1->hit());
00980   const std::vector<PSimHit>& pSimHitVec2 = hitAssociator->associateHit(*recHit2->hit());
00981   
00982   if ( pSimHitVec1.size()==0 || pSimHitVec2.size()==0 || hasDelta(&(*pSimHitVec1.begin())) || hasDelta(&(*pSimHitVec2.begin())) ) {
00983     edm::LogVerbatim("CkfDebugger") << "Seed has delta or problems" ;
00984     return -1;
00985   }
00986 
00987   //   LogTrace("CkfDebugger") << "state=\n" << state ;
00988   //   double stlp1 = state.localParameters().vector()[0];
00989   //   double stlp2 = state.localParameters().vector()[1];
00990   //   double stlp3 = state.localParameters().vector()[2];
00991   //   double stlp4 = state.localParameters().vector()[3];
00992   //   double stlp5 = state.localParameters().vector()[4];
00993 
00994   if (pSimHitVec2.size()!=0) {
00995     const PSimHit& simHit = *pSimHitVec2.begin();
00996     
00997     double shlp1 = -1/simHit.momentumAtEntry().mag();
00998     double shlp2 = simHit.momentumAtEntry().x()/simHit.momentumAtEntry().z();
00999     double shlp3 = simHit.momentumAtEntry().y()/simHit.momentumAtEntry().z();
01000     double shlp4 = simHit.localPosition().x();
01001     double shlp5 = simHit.localPosition().y();
01002     AlgebraicVector5 v;
01003     v[0] = shlp1;
01004     v[1] = shlp2;
01005     v[2] = shlp3;
01006     v[3] = shlp4;
01007     v[4] = shlp5;
01008   
01009     //     LogTrace("CkfDebugger") << "simHit.localPosition()=" << simHit.localPosition() ;
01010     //     LogTrace("CkfDebugger") << "simHit.momentumAtEntry()=" << simHit.momentumAtEntry() ;
01011     //     LogTrace("CkfDebugger") << "recHit2->localPosition()=" << recHit2->localPosition() ;
01012     //     LogTrace("CkfDebugger") << "recHit2->localPositionError()=" << recHit2->localPositionError() ;
01013     //     LogTrace("CkfDebugger") << "state.localPosition()=" << state.localPosition() ;
01014     //     LogTrace("CkfDebugger") << "state.localError().positionError()=" << state.localError().positionError() ;
01015 
01016     //     LogTrace("CkfDebugger") << "pullx(sh-rh)=" << (simHit.localPosition().x()-recHit2->localPosition().x())/sqrt(recHit2->localPositionError().xx()) ;
01017     //     LogTrace("CkfDebugger") << "pullx(sh-st)=" << (simHit.localPosition().x()-state.localPosition().x())/sqrt(state.localError().positionError().xx()) ;
01018     //     LogTrace("CkfDebugger") << "pullx(st-rh)=" << (state.localPosition().x()-recHit2->localPosition().x())/
01019     //       sqrt(recHit2->localPositionError().xx()+state.localError().positionError().xx()) ;
01020 
01021     //     LogTrace("CkfDebugger") << "local parameters" ;
01022     //     LogTrace("CkfDebugger") << left;
01023     //     LogTrace("CkfDebugger") << setw(15) << stlp1 << setw(15) << shlp1 << setw(15) << sqrt(state.localError().matrix()[0][0]) 
01024     //   << setw(15) << (stlp1-shlp1)/stlp1 << setw(15) << (stlp1-shlp1)/sqrt(state.localError().matrix()[0][0]) ;
01025     //     LogTrace("CkfDebugger") << setw(15) << stlp2 << setw(15) << shlp2 << setw(15) << sqrt(state.localError().matrix()[1][1]) 
01026     //   << setw(15) << (stlp2-shlp2)/stlp2 << setw(15) << (stlp2-shlp2)/sqrt(state.localError().matrix()[1][1]) ;
01027     //     LogTrace("CkfDebugger") << setw(15) << stlp3 << setw(15) << shlp3 << setw(15) << sqrt(state.localError().matrix()[2][2]) 
01028     //       << setw(15) << (stlp3-shlp3)/stlp3 << setw(15) << (stlp3-shlp3)/sqrt(state.localError().matrix()[2][2]) ;
01029     //     LogTrace("CkfDebugger") << setw(15) << stlp4 << setw(15) << shlp4 << setw(15) << sqrt(state.localError().matrix()[3][3]) 
01030     //       << setw(15) << (stlp4-shlp4)/stlp4 << setw(15) << (stlp4-shlp4)/sqrt(state.localError().matrix()[3][3]) ;
01031     //     LogTrace("CkfDebugger") << setw(15) << stlp5 << setw(15) << shlp5 << setw(15) << sqrt(state.localError().matrix()[4][4]) << 
01032     //       setw(15) << (stlp5-shlp5)/stlp5 << setw(15) << (stlp5-shlp5)/sqrt(state.localError().matrix()[4][4]) ;
01033 
01034     AlgebraicSymMatrix55 R = state.localError().matrix();
01035     R.Invert();
01036     double chi2 = ROOT::Math::Similarity(v-state.localParameters().vector(), R);
01037     LogTrace("CkfDebugger") << "chi2=" << chi2 ;
01038     return chi2;
01039   }
01040 
01041   return 0;//fixme
01042 
01043 }
01044 
01045 
01046 CkfDebugger::~CkfDebugger(){
01047   for (int it=0; it!=((int)(dump.size())); it++)
01048     edm::LogVerbatim("CkfDebugger") << "dump " << it << " " << dump[it] ;
01049   
01050   edm::LogVerbatim("CkfDebugger") ;
01051   edm::LogVerbatim("CkfDebugger") << "seedWithDelta=" <<  ((double)seedWithDelta/totSeeds) ;
01052   edm::LogVerbatim("CkfDebugger") << "problems=" << ((double)problems/totSeeds) ;
01053   edm::LogVerbatim("CkfDebugger") << "no_sim_hit=" << ((double)no_sim_hit/totSeeds) ;
01054   edm::LogVerbatim("CkfDebugger") << "no_layer=" << ((double)no_layer/totSeeds) ;
01055   edm::LogVerbatim("CkfDebugger") << "layer_not_found=" << ((double)layer_not_found/totSeeds) ;
01056   edm::LogVerbatim("CkfDebugger") << "det_not_found=" << ((double)det_not_found/totSeeds) ;
01057   edm::LogVerbatim("CkfDebugger") << "chi2gt30=" << ((double)chi2gt30/totSeeds) ;
01058   edm::LogVerbatim("CkfDebugger") << "chi2gt30deltaSeed=" << ((double)chi2gt30deltaSeed/totSeeds) ;
01059   edm::LogVerbatim("CkfDebugger") << "chi2gt30delta=" << ((double)chi2gt30delta/totSeeds) ;
01060   edm::LogVerbatim("CkfDebugger") << "chi2ls30=" << ((double)chi2ls30/totSeeds) ;
01061   edm::LogVerbatim("CkfDebugger") << "simple_hit_not_found=" << ((double)simple_hit_not_found/totSeeds) ;
01062   edm::LogVerbatim("CkfDebugger") << "no_component=" << ((double)no_component/totSeeds) ;
01063   edm::LogVerbatim("CkfDebugger") << "only_one_component=" << ((double)only_one_component/totSeeds) ;
01064   edm::LogVerbatim("CkfDebugger") << "matched_not_found=" << ((double)matched_not_found/totSeeds) ;
01065   edm::LogVerbatim("CkfDebugger") << "matched_not_associated=" << ((double)matched_not_associated/totSeeds) ;
01066   edm::LogVerbatim("CkfDebugger") << "partner_det_not_fuond=" << ((double)partner_det_not_fuond/totSeeds) ;
01067   edm::LogVerbatim("CkfDebugger") << "glued_det_not_fuond=" << ((double)glued_det_not_fuond/totSeeds) ;
01068   edm::LogVerbatim("CkfDebugger") << "propagation=" << ((double)propagation/totSeeds) ;
01069   edm::LogVerbatim("CkfDebugger") << "other=" << ((double)other/totSeeds) ;
01070   edm::LogVerbatim("CkfDebugger") << "totchi2gt30=" << ((double)totchi2gt30/totSeeds) ;
01071   edm::LogVerbatim("CkfDebugger") << "totSeeds=" << totSeeds ;
01072   edm::LogVerbatim("CkfDebugger") ;
01073   
01074   edm::LogVerbatim("CkfDebugger") << "layer navigation problems:" ;
01075   for (int i=0; i!=6; i++)
01076     for (int j=0; j!=9; j++){
01077       if (i==0 && j>2) break;
01078       if (i==1 && j>1) break;
01079       if (i==2 && j>3) break;
01080       if (i==3 && j>2) break;
01081       if (i==4 && j>5) break;
01082       if (i==5 && j>8) break;
01083       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump2[pair<int,int>(i,j)] ;
01084     }
01085   edm::LogVerbatim("CkfDebugger") << "\nlayer with hit having chi2>30:" ;
01086   for (int i=0; i!=6; i++)
01087     for (int j=0; j!=9; j++){
01088       if (i==0 && j>2) break;
01089       if (i==1 && j>1) break;
01090       if (i==2 && j>3) break;
01091       if (i==3 && j>2) break;
01092       if (i==4 && j>5) break;
01093       if (i==5 && j>8) break;
01094       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump3[pair<int,int>(i,j)] ;
01095     }
01096   edm::LogVerbatim("CkfDebugger") << "\nlayer with hit having chi2>30 for delta rays:" ;
01097   for (int i=0; i!=6; i++)
01098     for (int j=0; j!=9; j++){
01099       if (i==0 && j>2) break;
01100       if (i==1 && j>1) break;
01101       if (i==2 && j>3) break;
01102       if (i==3 && j>2) break;
01103       if (i==4 && j>5) break;
01104       if (i==5 && j>8) break;
01105       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump5[pair<int,int>(i,j)] ;
01106     }
01107   edm::LogVerbatim("CkfDebugger") << "\nlayer with det not found:" ;
01108   for (int i=0; i!=6; i++)
01109     for (int j=0; j!=9; j++){
01110       if (i==0 && j>2) break;
01111       if (i==1 && j>1) break;
01112       if (i==2 && j>3) break;
01113       if (i==3 && j>2) break;
01114       if (i==4 && j>5) break;
01115       if (i==5 && j>8) break;
01116       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump4[pair<int,int>(i,j)] ;
01117     }
01118   edm::LogVerbatim("CkfDebugger") << "\nlayer with correct RecHit after missing Sim Hit:" ;
01119   for (int i=0; i!=6; i++)
01120     for (int j=0; j!=9; j++){
01121       if (i==0 && j>2) break;
01122       if (i==1 && j>1) break;
01123       if (i==2 && j>3) break;
01124       if (i==3 && j>2) break;
01125       if (i==4 && j>5) break;
01126       if (i==5 && j>8) break;
01127       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump6[pair<int,int>(i,j)] ;
01128     }
01129   hchi2seedAll->Write();
01130   hchi2seedProb->Write();
01131   std::stringstream title;
01132   for (int i=0; i!=6; i++)
01133     for (int j=0; j!=9; j++){
01134       if (i==0 && j>2) break;
01135       if (i==1 && j>1) break;
01136       if (i==2 && j>3) break;
01137       if (i==3 && j>2) break;
01138       if (i==4 && j>5) break;
01139       if (i==5 && j>8) break;
01140       title.str("");
01141       title << "pullX_" << i+1 << "-" << j+1 << "_sh-rh";
01142       hPullX_shrh[title.str()]->Write();
01143       title.str("");
01144       title << "pullY_" << i+1 << "-" << j+1 << "_sh-rh";
01145       hPullY_shrh[title.str()]->Write();
01146       title.str("");
01147       title << "pullX_" << i+1 << "-" << j+1 << "_sh-st";
01148       hPullX_shst[title.str()]->Write();
01149       title.str("");
01150       title << "pullY_" << i+1 << "-" << j+1 << "_sh-st";
01151       hPullY_shst[title.str()]->Write();
01152       title.str("");
01153       title << "pullX_" << i+1 << "-" << j+1 << "_st-rh";
01154       hPullX_strh[title.str()]->Write();
01155       title.str("");
01156       title << "pullY_" << i+1 << "-" << j+1 << "_st-rh";
01157       hPullY_strh[title.str()]->Write();
01158       title.str("");
01159       title << "PullGP_X_" << i+1 << "-" << j+1 << "_sh-st";
01160       hPullGP_X_shst[title.str()]->Write();
01161       title.str("");
01162       title << "PullGP_Y_" << i+1 << "-" << j+1 << "_sh-st";
01163       hPullGP_Y_shst[title.str()]->Write();
01164       title.str("");
01165       title << "PullGP_Z_" << i+1 << "-" << j+1 << "_sh-st";
01166       hPullGP_Z_shst[title.str()]->Write();
01167       if ( ((i==2||i==4)&&(j==0||j==1)) || (i==3||i==5) ){
01168         title.str("");
01169         title << "pullM_" << i+1 << "-" << j+1 << "_sh-rh";
01170         hPullM_shrh[title.str()]->Write();
01171         title.str("");
01172         title << "pullS_" << i+1 << "-" << j+1 << "_sh-rh";
01173         hPullS_shrh[title.str()]->Write();
01174         title.str("");
01175         title << "pullM_" << i+1 << "-" << j+1 << "_sh-st";
01176         hPullM_shst[title.str()]->Write();
01177         title.str("");
01178         title << "pullS_" << i+1 << "-" << j+1 << "_sh-st";
01179         hPullS_shst[title.str()]->Write();
01180         title.str("");
01181         title << "pullM_" << i+1 << "-" << j+1 << "_st-rh";
01182         hPullM_strh[title.str()]->Write();
01183         title.str("");
01184         title << "pullS_" << i+1 << "-" << j+1 << "_st-rh";
01185         hPullS_strh[title.str()]->Write();
01186       }
01187     }
01188   hPullGPXvsGPX_shst->Write();
01189   hPullGPXvsGPY_shst->Write();
01190   hPullGPXvsGPZ_shst->Write();
01191   hPullGPXvsGPr_shst->Write();
01192   hPullGPXvsGPeta_shst->Write();
01193   hPullGPXvsGPphi_shst->Write();
01194   
01195   //file->Write();
01196   file->Close();
01197 }