CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/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                   CTTRHp tMonoHit = theTTRHBuilder->build(dynamic_cast<const SiStripMatchedRecHit2D*>(correctRecHit.first->hit())->monoHit());
00392                   const PSimHit sMonoHit = *(hitAssociator->associateHit(*tMonoHit->hit()).begin());
00393                   TSOS monoState = theForwardPropagator->propagate( traj.lastMeasurement().updatedState(), tMonoHit->det()->surface());
00394                   double pullM_shrh = (sMonoHit.localPosition().x()-tMonoHit->localPosition().x())/
00395                     sqrt(tMonoHit->localPositionError().xx());
00396                   double pullM_shst = (sMonoHit.localPosition().x()-monoState.localPosition().x())/
00397                     sqrt(monoState.localError().positionError().xx());
00398                   std::pair<double,double> pullsMono = computePulls(tMonoHit, monoState);
00399                   title.str("");
00400                   title << "pullM_" << subdetId << "-" << layerId << "_sh-rh";
00401                   hPullM_shrh[title.str()]->Fill(pullM_shrh);
00402                   title.str("");
00403                   title << "pullM_" << subdetId << "-" << layerId << "_sh-st";
00404                   hPullM_shst[title.str()]->Fill(pullM_shst);
00405                   title.str("");
00406                   title << "pullM_" << subdetId << "-" << layerId << "_st-rh";
00407                   hPullM_strh[title.str()]->Fill(pullsMono.first);
00408 
00409                   LogTrace("CkfDebugger") << "STEREO HIT";
00410                   CTTRHp tStereoHit = theTTRHBuilder->build(dynamic_cast<const SiStripMatchedRecHit2D*>(correctRecHit.first->hit())->stereoHit());
00411                   const PSimHit sStereoHit = *(hitAssociator->associateHit(*tStereoHit->hit()).begin());
00412                   TSOS stereoState = theForwardPropagator->propagate( traj.lastMeasurement().updatedState(), tStereoHit->det()->surface());
00413                   double pullS_shrh = (sStereoHit.localPosition().x()-tStereoHit->localPosition().x())/
00414                     sqrt(tStereoHit->localPositionError().xx());
00415                   double pullS_shst = (sStereoHit.localPosition().x()-stereoState.localPosition().x())/
00416                     sqrt(stereoState.localError().positionError().xx());
00417                   std::pair<double,double> pullsStereo = computePulls(tStereoHit, stereoState);
00418                   title.str("");
00419                   title << "pullS_" << subdetId  << "-" << layerId << "_sh-rh";
00420                   hPullS_shrh[title.str()]->Fill(pullS_shrh);
00421                   title.str("");
00422                   title << "pullS_" << subdetId << "-" << layerId << "_sh-st";
00423                   hPullS_shst[title.str()]->Fill(pullS_shst);
00424                   title.str("");
00425                   title << "pullS_" << subdetId << "-" << layerId << "_st-rh";
00426                   hPullS_strh[title.str()]->Fill(pullsStereo.first);
00427                 }
00428               } else 
00429                 edm::LogVerbatim("CkfDebugger") << "unexpected result: wrong det or layer id " 
00430                                                 << subdetId << " " << layerId << " " 
00431                                                 << correctRecHit.first->det()->geographicalId().rawId();
00432             }
00433           }
00434         }
00435       }
00436       else {
00437         edm::LogVerbatim("CkfDebugger") << "unexpected result " << correctRecHit.second ;
00438         dump[6]++;chi2ls30++;
00439       }
00440     }
00441     else dump[result]++;
00442     if (result == 3){
00443       dump2[pair<int,int>((correctRecHit.first->det()->geographicalId().subdetId()-1),(layer(correctRecHit.first->det()))-1)]++; 
00444     }
00445     if (result == 4){
00446       dump4[pair<int,int>((correctRecHit.first->det()->geographicalId().subdetId()-1),(layer(correctRecHit.first->det()))-1)]++; 
00447     }
00448     if (correctRecHit.second>30) {
00449       dump[7]++;totchi2gt30++; 
00450     }
00451   }
00452   return false;
00453 }
00454 
00455 
00456 bool CkfDebugger::correctTrajectory( const Trajectory& traj,unsigned int& trajId) const
00457 {
00458   LogTrace("CkfDebugger") << "now in correctTrajectory" ;
00459   Trajectory::RecHitContainer hits = traj.recHits();
00460 
00461   std::vector<SimHitIdpr> currentTrackId = hitAssociator->associateHitId(*hits.front()->hit());
00462   if (currentTrackId.size() == 0) return false;
00463 
00464   for (Trajectory::RecHitContainer::const_iterator rh=hits.begin(); rh!=hits.end(); ++rh) {
00465 
00466     //if invalid hit exit
00467     if (!(*rh)->hit()->isValid()) {
00468       //LogTrace("CkfDebugger") << "invalid hit" ;
00469       return false;
00470     }
00471 
00472     //if hits from deltas exit
00473     bool nogoodhit = true;
00474     std::vector<PSimHit> assSimHits = hitAssociator->associateHit(*(*rh)->hit());
00475     for (std::vector<PSimHit>::iterator shit=assSimHits.begin();shit!=assSimHits.end();shit++){
00476       if (goodSimHit(*shit)) nogoodhit=false;
00477     }
00478     if (nogoodhit) return false;
00479     
00480     //all hits must be associated to the same sim track
00481     bool test = true;
00482     std::vector<SimHitIdpr> nextTrackId = hitAssociator->associateHitId(*(*rh)->hit());
00483     for (std::vector<SimHitIdpr>::iterator i=currentTrackId.begin();i!=currentTrackId.end();i++){
00484       for (std::vector<SimHitIdpr>::iterator j=nextTrackId.begin();j!=nextTrackId.end();j++){
00485         if (i->first == j->first) test = false;
00486         //LogTrace("CkfDebugger") << "valid " << *i << " " << *j ;
00487         trajId = j->first;
00488       }
00489     }
00490     if (test) {/*LogTrace("CkfDebugger") << "returning false" ;*/return false;}
00491     //     std::vector<PSimHit*> simTrackHits = idHitsMap[trajId];
00492     //     if (!goodSimHit(simTrackHits.))
00493   }
00494   //LogTrace("CkfDebugger") << "returning true" ;
00495   return true;
00496 }
00497 
00498 int CkfDebugger::assocTrackId(CTTRHp rechit) const
00499 {
00500   LogTrace("CkfDebugger") << "now in assocTrackId" ;
00501 
00502   if (!rechit->hit()->isValid()) {
00503     return -1;
00504   }
00505 
00506   std::vector<SimHitIdpr> ids = hitAssociator->associateHitId(*rechit->hit());
00507   if (ids.size()!=0) {
00508     return ids[0].first;//FIXME if size>1!!
00509   }
00510   else {
00511     return -1;
00512   }
00513 }
00514 
00515 
00516 vector<const PSimHit*> CkfDebugger::nextCorrectHits( const Trajectory& traj, unsigned int& trajId)
00517 {
00518   std::vector<const PSimHit*> result;
00519   // find the component of the RecHit at largest distance from origin (FIXME: should depend on propagation direction)
00520   LogTrace("CkfDebugger") << "now in nextCorrectHits" ;
00521   TransientTrackingRecHit::ConstRecHitPointer lastRecHit = traj.lastMeasurement().recHit();
00522   TransientTrackingRecHit::RecHitContainer comp = lastRecHit->transientHits();
00523   if (!comp.empty()) {
00524     float maxR = 0;
00525     for (TransientTrackingRecHit::RecHitContainer::const_iterator ch=comp.begin(); 
00526          ch!=comp.end(); ++ch) {
00527       if ((*ch)->globalPosition().mag() > maxR) lastRecHit = *ch; 
00528       maxR = (*ch)->globalPosition().mag();
00529     }
00530   }
00531   edm::LogVerbatim("CkfDebugger") << "CkfDebugger: lastRecHit is at gpos " << lastRecHit->globalPosition() 
00532                                   << " layer " << layer((lastRecHit->det())) 
00533                                   << " subdet " << lastRecHit->det()->geographicalId().subdetId() ;
00534 
00535   //find the simHits associated to the recHit
00536   const std::vector<PSimHit>& pSimHitVec = hitAssociator->associateHit(*lastRecHit->hit());
00537   for (std::vector<PSimHit>::const_iterator shit=pSimHitVec.begin();shit!=pSimHitVec.end();shit++){
00538     const GeomDetUnit* detUnit = theTrackerGeom->idToDetUnit( DetId(shit->detUnitId()));
00539     LogTrace("CkfDebugger") << "from hitAssociator SimHits are at GP=" << detUnit->toGlobal( shit->localPosition())
00540                                     << " traId=" << shit->trackId() << " particleType " << shit->particleType() 
00541                                     << " pabs=" << shit->pabs() << " detUnitId=" << shit->detUnitId() << " layer " << layer((det(&*shit))) 
00542                                     << " subdet " << det(&*shit)->geographicalId().subdetId() ;
00543   }
00544 
00545   //choose the simHit from the same track that has the highest tof
00546   const PSimHit * lastPSH = 0;
00547   if (!pSimHitVec.empty()) {
00548     float maxTOF = 0;
00549     for (std::vector<PSimHit>::const_iterator ch=pSimHitVec.begin(); ch!=pSimHitVec.end(); ++ch) {
00550       if ( ( ch->trackId()== trajId) && (ch->timeOfFlight() > maxTOF)  && ( goodSimHit(*ch) )) {
00551         lastPSH = &*ch; 
00552         maxTOF = lastPSH->timeOfFlight();
00553       }
00554     }
00555   }
00556   else return result;//return empty vector: no more hits on the sim track
00557   if (lastPSH == 0) return result; //return empty vector: no more good hits on the sim track
00558   edm::LogVerbatim("CkfDebugger") << "CkfDebugger: corresponding SimHit is at gpos " << position(&*lastPSH) ;
00559 
00560   //take the simHits on the simTrack that are in the nextLayer (could be > 1 if overlap or matched)
00561   std::vector<PSimHit*> trackHits = idHitsMap[trajId];
00562   if (fabs((double)(trackHits.back()->detUnitId()-lastPSH->detUnitId()))<1 ) return result;//end of sim track
00563   std::vector<PSimHit*>::iterator currentIt = trackHits.end();
00564   for (std::vector<PSimHit*>::iterator it=trackHits.begin();
00565        it!=trackHits.end();it++){
00566     if (goodSimHit(**it) && //good hit
00567         ( lastPSH->timeOfFlight()<(*it)->timeOfFlight() ) && //greater tof
00568         //( fabs((double)((*it)->detUnitId()-(lastPSH->detUnitId()) ))>1) && //not components of the same matched hit
00569         ( (det(lastPSH)->geographicalId().subdetId()!=det(*it)->geographicalId().subdetId()) || 
00570           (layer(det(lastPSH))!=layer(det(*it)) ) ) //change layer or detector(tib,tob,...)
00571         ){
00572       edm::LogVerbatim("CkfDebugger") << "Next good PSimHit is at gpos " << position(*it) ;
00573       result.push_back(*it);
00574       currentIt = it;
00575       break;
00576     }
00577   }
00578   bool samelayer = true;
00579   if (currentIt!=(trackHits.end()-1) && currentIt!=trackHits.end()) {
00580     for (std::vector<PSimHit*>::iterator nextIt = currentIt; (samelayer && nextIt!=trackHits.end()) ;nextIt++){
00581       if (goodSimHit(**nextIt)){
00582         if ( (det(*nextIt)->geographicalId().subdetId()==det(*currentIt)->geographicalId().subdetId()) && 
00583              (layer(det(*nextIt))==layer(det(*currentIt)) ) ) {
00584           result.push_back(*nextIt);
00585         }
00586         else samelayer = false;
00587       }
00588     }
00589   }
00590   
00591   return result;
00592 }
00593 
00594 bool CkfDebugger::goodSimHit(const PSimHit& sh) const
00595 {
00596   if (sh.pabs() > 0.9) return true; // GeV, reject delta rays from association
00597   else return false;
00598 }
00599 
00600 
00601 bool CkfDebugger::associated(CTTRHp rechit, const PSimHit& pSimHit) const
00602 {
00603   LogTrace("CkfDebugger") << "now in associated" ;
00604 
00605   if (!rechit->isValid()) return false;
00606   //   LogTrace("CkfDebugger") << "rec hit valid" ;
00607   const std::vector<PSimHit>& pSimHitVec = hitAssociator->associateHit(*rechit->hit());
00608   //   LogTrace("CkfDebugger") << "size=" << pSimHitVec.size() ;
00609   for (std::vector<PSimHit>::const_iterator shit=pSimHitVec.begin();shit!=pSimHitVec.end();shit++){
00610     //const GeomDetUnit* detUnit = theTrackerGeom->idToDetUnit( DetId(shit->detUnitId()));
00611     //         LogTrace("CkfDebugger") << "pSimHit.timeOfFlight()=" << pSimHit.timeOfFlight() 
00612     //           << " pSimHit.pabs()=" << pSimHit.pabs() << " GP=" << position(&pSimHit);
00613     //         LogTrace("CkfDebugger") << "(*shit).timeOfFlight()=" << (*shit).timeOfFlight() 
00614     //           << " (*shit).pabs()=" << (*shit).pabs() << " GP=" << detUnit->toGlobal( shit->localPosition());
00615     if ( ( fabs((*shit).timeOfFlight()-pSimHit.timeOfFlight())<1e-9  ) && 
00616          ( fabs((*shit).pabs()-pSimHit.pabs())<1e-9 ) ) return true;
00617   }
00618   return false;
00619 }
00620 
00621 bool CkfDebugger::correctMeas( const TM& tm, const PSimHit* correctHit) const
00622 {
00623   LogTrace("CkfDebugger") << "now in correctMeas" ;
00624   CTTRHp recHit = tm.recHit();
00625   if (recHit->isValid()) LogTrace("CkfDebugger") << "hit at position:" << recHit->globalPosition() ;
00626   TransientTrackingRecHit::RecHitContainer comp = recHit->transientHits();
00627   if (comp.empty()) {
00628     //     LogTrace("CkfDebugger") << "comp.empty()==true" ;
00629     return associated( recHit, *correctHit);
00630   }
00631   else {
00632     for (TransientTrackingRecHit::RecHitContainer::const_iterator ch=comp.begin(); 
00633          ch!=comp.end(); ++ch) {
00634       if ( associated( recHit, *correctHit)) {
00635         // check if the other components are associated to the same trackId
00636         for (TransientTrackingRecHit::RecHitContainer::const_iterator ch2=comp.begin(); 
00637              ch2!=comp.end(); ++ch2) {
00638           if (ch2 == ch) continue;
00640           //      LogTrace("CkfDebugger") << "correctHit->trackId()=" << correctHit->trackId() ;
00641           bool test=true;
00642           std::vector<SimHitIdpr> ids = hitAssociator->associateHitId(*(*ch2)->hit());
00643           for (std::vector<SimHitIdpr>::iterator j=ids.begin();j!=ids.end();j++){
00644             //      LogTrace("CkfDebugger") << "j=" <<j->first;
00645             if (correctHit->trackId()==j->first) {
00646               test=false;
00647               //              LogTrace("CkfDebugger") << correctHit->trackId()<< " " <<j->first;
00648             }
00649           }
00650           if (assocTrackId( *ch2) != ((int)( correctHit->trackId())) ) {LogTrace("CkfDebugger") << "returning false 1" ;/*return false;*/}//fixme
00651           if (test) {
00652             //      LogTrace("CkfDebugger") << "returning false 2" ;
00653             return false; // not all components from same simtrack
00654           }
00655           //      if (assocTrackId( **ch2) != ((int)( correctHit->trackId())) ) {
00656           //        return false; // not all components from same simtrack
00657           //      }
00658         }
00659         return true; // if all components from same simtrack
00660       }
00661     }
00662     return false;
00663   }
00664 }
00665 
00666 //this checks only if there is the rechit on the det where the sim hit is 
00667 pair<CTTRHp, double> CkfDebugger::analyseRecHitExistance( const PSimHit& sh, const TSOS& startingState)
00668 {
00669   LogTrace("CkfDebugger") << "now in analyseRecHitExistance" ;
00670 
00671   std::pair<CTTRHp, double> result;
00672   
00673   const MeasurementDet* simHitDet = theMeasurementTracker->idToDet( DetId( sh.detUnitId()));
00674   TSOS simHitState = TSOSFromSimHitFactory()(sh, *det(&sh), *theMagField);
00675   MeasurementDet::RecHitContainer recHits = simHitDet->recHits( simHitState);//take all hits from det
00676 
00677   //check if the hit is not present or is a problem of association
00678   TSOS firstDetState = theForwardPropagator->propagate( startingState, det(&sh)->surface());
00679   if (!firstDetState.isValid()) {
00680     edm::LogVerbatim("CkfDebugger") << "CkfDebugger: propagation failed from state " << startingState << " to first det surface " 
00681                                     << position(&sh) ;
00682     propagation++;
00683     return std::pair<CTTRHp, double>((CTTRHp)(0),-1);
00684   }
00685 
00686   bool found = false;
00687   for ( MeasurementDet::RecHitContainer::const_iterator rh = recHits.begin(); rh != recHits.end(); rh++) {
00688     if ( associated( *rh, sh)) {
00689       found = true;
00690       result = std::pair<CTTRHp, double>(*rh,theChi2->estimate( firstDetState, **rh).second);
00691       edm::LogVerbatim("CkfDebugger") << "CkfDebugger: A RecHit associated to the correct Simhit exists at lpos " 
00692                                       << (**rh).localPosition()
00693                                       << " gpos " << (**rh).globalPosition()
00694                                       << " layer " <<   layer((**rh).det())
00695                                       << " subdet " << (**rh).det()->geographicalId().subdetId() 
00696                                       << " Chi2 " << theChi2->estimate( firstDetState, **rh).second;
00697     }
00698   }
00699   if (!found) {
00700     edm::LogVerbatim("CkfDebugger") << "CkfDebugger: there is no RecHit associated to the correct SimHit." ;
00701     edm::LogVerbatim("CkfDebugger") << " There are " <<  recHits.size() << " RecHits in the simHit DetUnit" ;
00702     edm::LogVerbatim("CkfDebugger") << "SH GP=" << position(&sh) << " subdet=" << det(&sh)->geographicalId().subdetId() 
00703                                     << " layer=" << layer(det(&sh)) ;
00704     int y=0;
00705     for (MeasurementDet::RecHitContainer::const_iterator rh = recHits.begin(); rh != recHits.end(); rh++)
00706       edm::LogVerbatim("CkfDebugger") << "RH#" << y++ << " GP=" << (**rh).globalPosition() << " subdet=" << (**rh).det()->geographicalId().subdetId() 
00707                                       << " layer=" << layer((**rh).det()) ;
00708     for ( MeasurementDet::RecHitContainer::const_iterator rh = recHits.begin(); rh != recHits.end(); rh++) {
00709       edm::LogVerbatim("CkfDebugger") << "Non-associated RecHit at pos " << (**rh).localPosition() ;
00710     }
00711   }
00712 
00713   bool found2 = false;
00714   const PSimHit* sh2;
00715   StripSubdetector subdet( det(&sh)->geographicalId());
00716   if (!subdet.glued()) {
00717     edm::LogVerbatim("CkfDebugger") << "The DetUnit is not part of a GluedDet" ;
00718     if (found) {
00719       if (result.second>30){
00720         LogTrace("CkfDebugger") << "rh->parameters()=" << result.first->parameters() ;
00721         LogTrace("CkfDebugger") << "rh->parametersError()=" << result.first->parametersError() ;
00722         MeasurementExtractor me(firstDetState);
00723         AlgebraicVector r(result.first->parameters() - me.measuredParameters(*result.first));
00724         LogTrace("CkfDebugger") << "me.measuredParameters(**rh)=" << me.measuredParameters(*result.first) ;
00725         LogTrace("CkfDebugger") << "me.measuredError(**rh)=" << me.measuredError(*result.first) ;
00726         AlgebraicSymMatrix R(result.first->parametersError() + me.measuredError(*result.first));
00727         LogTrace("CkfDebugger") << "r=" << r ;
00728         LogTrace("CkfDebugger") << "R=" << R ;
00729         int ierr; 
00730         R.invert(ierr);
00731         LogTrace("CkfDebugger") << "R(-1)=" << R ;
00732         LogTrace("CkfDebugger") << "chi2=" << R.similarity(r) ;
00733       }
00734       return result;
00735     }
00736     else {
00737       simple_hit_not_found++;
00738       return std::pair<CTTRHp, double>((CTTRHp)(0),-8);//not found (is not a glued det)
00739     }
00740   } else {
00741     edm::LogVerbatim("CkfDebugger") << "The DetUnit is part of a GluedDet" ;
00742     DetId partnerDetId = DetId( subdet.partnerDetId());
00743 
00744     sh2 = pSimHit( sh.trackId(), partnerDetId);
00745     if (sh2 == 0) {
00746       edm::LogVerbatim("CkfDebugger") << "Partner DetUnit does not have a SimHit from the same track" ;
00747       if (found) {
00748         //projected rec hit
00749         TrackingRecHitProjector<ProjectedRecHit2D> proj;
00750         DetId gid = gluedId( subdet);
00751         const MeasurementDet* gluedDet = theMeasurementTracker->idToDet( gid);
00752         TSOS gluedTSOS = theForwardPropagator->propagate(startingState, gluedDet->geomDet().surface());
00753         CTTRHp projHit = proj.project( *result.first,gluedDet->geomDet(),gluedTSOS).get();
00754         //LogTrace("CkfDebugger") << proj.project( *result.first,gluedDet->geomDet(),gluedTSOS)->parameters() ;
00755         //LogTrace("CkfDebugger") << projHit->parametersError() ;
00756         double chi2 = theChi2->estimate(gluedTSOS, *proj.project( *result.first,gluedDet->geomDet(),gluedTSOS)).second;
00757         return std::pair<CTTRHp, double>(projHit,chi2);
00758       }
00759     }
00760     else {
00761       edm::LogVerbatim("CkfDebugger") << "Partner DetUnit has a good SimHit at gpos " << position(sh2) 
00762                                       << " lpos " << sh2->localPosition() ;
00763       //}
00764     
00765       const MeasurementDet* partnerDet = theMeasurementTracker->idToDet( partnerDetId);
00766       if (partnerDet == 0) {
00767         edm::LogVerbatim("CkfDebugger") << "Partner measurementDet not found!!!" ;
00768         partner_det_not_fuond++;
00769         return std::pair<CTTRHp, double>((CTTRHp)(0),-3);
00770       }
00771       TSOS simHitState2 = TSOSFromSimHitFactory()(*sh2, *det(sh2), *theMagField);
00772       MeasurementDet::RecHitContainer recHits2 = partnerDet->recHits( simHitState2);
00773 
00774       TSOS secondDetState = theForwardPropagator->propagate( startingState, det(sh2)->surface());
00775       if (!secondDetState.isValid()) {
00776         edm::LogVerbatim("CkfDebugger") << "CkfDebugger: propagation failed from state " << startingState << " to second det surface " 
00777                                         << position(sh2) ;
00778         propagation++;
00779         return std::pair<CTTRHp, double>((CTTRHp)(0),-1);
00780       }
00781 
00782       for ( MeasurementDet::RecHitContainer::const_iterator rh = recHits2.begin(); rh != recHits2.end(); rh++) {
00783         if ( associated( *rh, *sh2)) {
00784           found2 = true;
00785           edm::LogVerbatim("CkfDebugger") << "CkfDebugger: A RecHit associated to the correct Simhit exists at lpos " 
00786                                           << (**rh).localPosition()
00787                                           << " gpos " << (**rh).globalPosition()
00788                                           << " Chi2 " << theChi2->estimate( secondDetState, **rh).second
00789             ;
00790         }
00791       }
00792       if (!found2) {
00793         edm::LogVerbatim("CkfDebugger") << "CkfDebugger: there is no RecHit associated to the correct SimHit." ;
00794         LogTrace("CkfDebugger") << " There are " <<  recHits.size() << " RecHits in the simHit DetUnit" ;
00795         for ( MeasurementDet::RecHitContainer::const_iterator rh = recHits.begin(); rh != recHits.end(); rh++) {
00796           LogTrace("CkfDebugger") << "Non-associated RecHit at pos " << (**rh).localPosition() ;
00797         }
00798       }
00799     }
00800   }
00801 
00802   MeasurementDet::RecHitContainer gluedHits;
00803   if (found && found2) {
00804     // look in the glued det
00805     DetId gid = gluedId( subdet);
00806     const MeasurementDet* gluedDet = theMeasurementTracker->idToDet( gid);
00807     if ( gluedDet == 0) {
00808       edm::LogVerbatim("CkfDebugger") << "CkfDebugger ERROR: glued MeasurementDet not found!" ;
00809       glued_det_not_fuond++;
00810       return std::pair<CTTRHp, double>((CTTRHp)(0),-4);
00811     }
00812 
00813     TSOS gluedDetState = theForwardPropagator->propagate( startingState, gluedDet->surface());
00814     if (!gluedDetState.isValid()) {
00815       edm::LogVerbatim("CkfDebugger") << "CkfDebugger: propagation failed from state " << startingState << " to det surface " 
00816                                       << gluedDet->position() ;
00817       propagation++;
00818       return std::pair<CTTRHp, double>((CTTRHp)(0),-1);
00819     }
00820 
00821     gluedHits = gluedDet->recHits( gluedDetState);
00822     edm::LogVerbatim("CkfDebugger") << "CkfDebugger: the GluedDet returned " << gluedHits.size() << " hits" ;
00823     if (gluedHits.size()==0){
00824       edm::LogVerbatim("CkfDebugger") << "Found and associated mono and stereo recHits but not matched!!!" ;
00825       matched_not_found++;
00826       return std::pair<CTTRHp, double>((CTTRHp)(0),-5);
00827     } 
00828     bool found3 = false;
00829     for ( MeasurementDet::RecHitContainer::const_iterator rh = gluedHits.begin(); rh != gluedHits.end(); rh++) {
00830       if ( associated( *rh, sh) && associated( *rh, *sh2)) {
00831         double chi2 = theChi2->estimate(gluedDetState, **rh).second;
00832         edm::LogVerbatim("CkfDebugger") << "Matched hit at lpos " << (**rh).localPosition()
00833                                         << " gpos " << (**rh).globalPosition()
00834                                         << " has Chi2 " << chi2
00835           ;
00836         result = std::pair<CTTRHp, double>(&**rh,chi2);
00837         found3 = true;
00838         if (chi2>30){
00839           LogTrace("CkfDebugger") << "rh->parameters()=" << (*rh)->parameters() ;
00840           LogTrace("CkfDebugger") << "rh->parametersError()=" << (*rh)->parametersError() ;
00841           MeasurementExtractor me(gluedDetState);
00842           AlgebraicVector r((*rh)->parameters() - me.measuredParameters(**rh));
00843           LogTrace("CkfDebugger") << "me.measuredParameters(**rh)=" << me.measuredParameters(**rh) ;
00844           LogTrace("CkfDebugger") << "me.measuredError(**rh)=" << me.measuredError(**rh) ;
00845           AlgebraicSymMatrix R((*rh)->parametersError() + me.measuredError(**rh));
00846           LogTrace("CkfDebugger") << "r=" << r ;
00847           LogTrace("CkfDebugger") << "R=" << R ;
00848           int ierr; 
00849           R.invert(ierr);
00850           LogTrace("CkfDebugger") << "R(-1)=" << R ;
00851           LogTrace("CkfDebugger") << "chi2=" << R.similarity(r) ;
00852         }
00853         break;
00854       }
00855     }
00856     if (found3) return result;
00857     else {
00858       edm::LogVerbatim("CkfDebugger") << "Found and associated mono and stereo recHits. Matched found but not associated!!!" ;
00859       matched_not_associated++;
00860       return std::pair<CTTRHp, double>((CTTRHp)(0),-6);
00861     }
00862   }
00863   else if ( (found && !found2) || (!found && found2) ) {
00864     edm::LogVerbatim("CkfDebugger") << "Only one component is found" ;
00865     only_one_component++;
00866     return std::pair<CTTRHp, double>((CTTRHp)(0),-7);
00867   }
00868   else {
00869     edm::LogVerbatim("CkfDebugger") << "No component is found" ;
00870     no_component++;
00871     return std::pair<CTTRHp, double>((CTTRHp)(0),-2);
00872   }
00873   other++;
00874   return std::pair<CTTRHp, double>((CTTRHp)(0),0);//other
00875 }
00876 
00877 const PSimHit* CkfDebugger::pSimHit(unsigned int tkId, DetId detId)
00878 {
00879   for (std::vector<PSimHit*>::iterator shi=idHitsMap[tkId].begin(); shi!=idHitsMap[tkId].end(); ++shi) {    
00880     if ( (*shi)->detUnitId() == detId.rawId() && 
00881          //(shi)->trackId() == tkId &&
00882          goodSimHit(**shi) ) {
00883       return (*shi);
00884     }
00885   }
00886   return 0;
00887 }
00888 
00889 int CkfDebugger::analyseRecHitNotFound(const Trajectory& traj, CTTRHp correctRecHit)
00890 {
00891   unsigned int correctDetId = correctRecHit->det()->geographicalId().rawId();
00892   int correctLayId = layer(correctRecHit->det());
00893   LogTrace("CkfDebugger") << "correct layer id=" << correctLayId ;
00894 
00895   TSOS currentState( traj.lastMeasurement().updatedState() );
00896   std::vector<const DetLayer*> nl = traj.lastLayer()->nextLayers( *currentState.freeState(),traj.direction() );
00897   if (nl.empty()) {
00898     edm::LogVerbatim("CkfDebugger") << "no compatible layers" ;
00899     no_layer++;return 2;
00900   }
00901 
00902   TkLayerLess lless;//FIXME - was lless(traj.direction())
00903   const DetLayer* detLayer = 0;
00904   bool navLayerAfter = false;
00905   bool test = false;
00906   for (std::vector<const DetLayer*>::iterator il = nl.begin(); il != nl.end(); il++) {
00907     if ( dynamic_cast<const BarrelDetLayer*>(*il) ){
00908       const BarrelDetLayer* pbl = dynamic_cast<const BarrelDetLayer*>(*il);
00909       LogTrace("CkfDebugger") << "pbl->specificSurface().bounds().length()=" << pbl->specificSurface().bounds().length() ;
00910       LogTrace("CkfDebugger") << "pbl->specificSurface().bounds().width()=" << pbl->specificSurface().bounds().width() ;
00911     }
00912     int layId = layer(((*(*il)->basicComponents().begin())));
00913     LogTrace("CkfDebugger") << " subdet=" << (*(*il)->basicComponents().begin())->geographicalId().subdetId() << "layer id=" << layId ;
00914     if (layId==correctLayId) {
00915       test = true;
00916       detLayer = &**il;
00917       break;
00918     }
00919     if ( lless( *il, theGeomSearchTracker->detLayer(correctRecHit->det()->geographicalId()) )) 
00920       navLayerAfter = true; //it is enough that only one layer is after the correct one?
00921   }
00922 
00923   if (test) {
00924     edm::LogVerbatim("CkfDebugger") << "correct layer taken into account. layer id: " << correctLayId ;
00925   } else if (navLayerAfter){
00926     edm::LogVerbatim("CkfDebugger")<< "SimHit layer after the layers returned by Navigation.";
00927     edm::LogVerbatim("CkfDebugger")<< "Probably a missing SimHit." ;
00928     edm::LogVerbatim("CkfDebugger")<< "check: " << (correctRecHit->det()->geographicalId().subdetId()) << " " << (layer(correctRecHit->det()));
00929     dump6[pair<int,int>((correctRecHit->det()->geographicalId().subdetId()-1),(layer(correctRecHit->det()))-1)]++; 
00930     no_sim_hit++;return 16;
00931   }
00932   else {
00933     edm::LogVerbatim("CkfDebugger") << "correct layer NOT taken into account. correct layer id: " << correctLayId ;
00934     layer_not_found++;
00935     return 3;
00936   }
00937 
00938   typedef DetLayer::DetWithState  DetWithState;
00939   std::vector<DetWithState> compatDets = detLayer->compatibleDets(currentState,*theForwardPropagator,*theChi2);
00940   //   LogTrace("CkfDebugger") << "DEBUGGER" ;
00941   //   LogTrace("CkfDebugger") << "runned compatDets." ;
00942   //   LogTrace("CkfDebugger") << "started from the following TSOS:" ;
00943   //   LogTrace("CkfDebugger") << currentState ;
00944   //   LogTrace("CkfDebugger") << "number of dets found=" << compatDets.size() ;
00945   //   for (std::vector<DetWithState>::iterator det=compatDets.begin();det!=compatDets.end();det++){
00946   //     unsigned int detId = det->first->geographicalId().rawId();
00947   //     LogTrace("CkfDebugger") << "detId=" << detId ; 
00948   //   }
00949   bool test2 = false;
00950   for (std::vector<DetWithState>::iterator det=compatDets.begin();det!=compatDets.end();det++){
00951     unsigned int detId = det->first->geographicalId().rawId();
00952     //     LogTrace("CkfDebugger") << "detId=" << detId 
00953     //   << "\ncorrectRecHit->det()->geographicalId().rawId()=" << correctRecHit->det()->geographicalId().rawId() 
00954     //   << "\ngluedId(correctRecHit->det()->geographicalId()).rawId()=" << gluedId(correctRecHit->det()->geographicalId()).rawId()
00955     //   ; 
00956     if (detId==gluedId(correctRecHit->det()->geographicalId()).rawId()) {
00957       test2=true;
00958       break;
00959     }
00960   }
00961   
00962   if (test2){
00963     edm::LogVerbatim("CkfDebugger") << "correct det taken into account. correctDetId is: " << correctDetId 
00964                                     << ". please check chi2." ;
00965     return 5;
00966   }
00967   else {
00968     edm::LogVerbatim("CkfDebugger") << "correct det NOT taken into account. correctDetId: " << correctDetId ;
00969     det_not_found++;return 4;
00970   }
00971 
00972 }
00973 
00974 double CkfDebugger::testSeed(CTTRHp recHit1, CTTRHp recHit2, TSOS state){
00975   //edm::LogVerbatim("CkfDebugger") << "CkfDebugger::testSeed";
00976   //test Deltas
00977   const std::vector<PSimHit>& pSimHitVec1 = hitAssociator->associateHit(*recHit1->hit());
00978   const std::vector<PSimHit>& pSimHitVec2 = hitAssociator->associateHit(*recHit2->hit());
00979   
00980   if ( pSimHitVec1.size()==0 || pSimHitVec2.size()==0 || hasDelta(&(*pSimHitVec1.begin())) || hasDelta(&(*pSimHitVec2.begin())) ) {
00981     edm::LogVerbatim("CkfDebugger") << "Seed has delta or problems" ;
00982     return -1;
00983   }
00984 
00985   //   LogTrace("CkfDebugger") << "state=\n" << state ;
00986   //   double stlp1 = state.localParameters().vector()[0];
00987   //   double stlp2 = state.localParameters().vector()[1];
00988   //   double stlp3 = state.localParameters().vector()[2];
00989   //   double stlp4 = state.localParameters().vector()[3];
00990   //   double stlp5 = state.localParameters().vector()[4];
00991 
00992   if (pSimHitVec2.size()!=0) {
00993     const PSimHit& simHit = *pSimHitVec2.begin();
00994     
00995     double shlp1 = -1/simHit.momentumAtEntry().mag();
00996     double shlp2 = simHit.momentumAtEntry().x()/simHit.momentumAtEntry().z();
00997     double shlp3 = simHit.momentumAtEntry().y()/simHit.momentumAtEntry().z();
00998     double shlp4 = simHit.localPosition().x();
00999     double shlp5 = simHit.localPosition().y();
01000     AlgebraicVector5 v;
01001     v[0] = shlp1;
01002     v[1] = shlp2;
01003     v[2] = shlp3;
01004     v[3] = shlp4;
01005     v[4] = shlp5;
01006   
01007     //     LogTrace("CkfDebugger") << "simHit.localPosition()=" << simHit.localPosition() ;
01008     //     LogTrace("CkfDebugger") << "simHit.momentumAtEntry()=" << simHit.momentumAtEntry() ;
01009     //     LogTrace("CkfDebugger") << "recHit2->localPosition()=" << recHit2->localPosition() ;
01010     //     LogTrace("CkfDebugger") << "recHit2->localPositionError()=" << recHit2->localPositionError() ;
01011     //     LogTrace("CkfDebugger") << "state.localPosition()=" << state.localPosition() ;
01012     //     LogTrace("CkfDebugger") << "state.localError().positionError()=" << state.localError().positionError() ;
01013 
01014     //     LogTrace("CkfDebugger") << "pullx(sh-rh)=" << (simHit.localPosition().x()-recHit2->localPosition().x())/sqrt(recHit2->localPositionError().xx()) ;
01015     //     LogTrace("CkfDebugger") << "pullx(sh-st)=" << (simHit.localPosition().x()-state.localPosition().x())/sqrt(state.localError().positionError().xx()) ;
01016     //     LogTrace("CkfDebugger") << "pullx(st-rh)=" << (state.localPosition().x()-recHit2->localPosition().x())/
01017     //       sqrt(recHit2->localPositionError().xx()+state.localError().positionError().xx()) ;
01018 
01019     //     LogTrace("CkfDebugger") << "local parameters" ;
01020     //     LogTrace("CkfDebugger") << left;
01021     //     LogTrace("CkfDebugger") << setw(15) << stlp1 << setw(15) << shlp1 << setw(15) << sqrt(state.localError().matrix()[0][0]) 
01022     //   << setw(15) << (stlp1-shlp1)/stlp1 << setw(15) << (stlp1-shlp1)/sqrt(state.localError().matrix()[0][0]) ;
01023     //     LogTrace("CkfDebugger") << setw(15) << stlp2 << setw(15) << shlp2 << setw(15) << sqrt(state.localError().matrix()[1][1]) 
01024     //   << setw(15) << (stlp2-shlp2)/stlp2 << setw(15) << (stlp2-shlp2)/sqrt(state.localError().matrix()[1][1]) ;
01025     //     LogTrace("CkfDebugger") << setw(15) << stlp3 << setw(15) << shlp3 << setw(15) << sqrt(state.localError().matrix()[2][2]) 
01026     //       << setw(15) << (stlp3-shlp3)/stlp3 << setw(15) << (stlp3-shlp3)/sqrt(state.localError().matrix()[2][2]) ;
01027     //     LogTrace("CkfDebugger") << setw(15) << stlp4 << setw(15) << shlp4 << setw(15) << sqrt(state.localError().matrix()[3][3]) 
01028     //       << setw(15) << (stlp4-shlp4)/stlp4 << setw(15) << (stlp4-shlp4)/sqrt(state.localError().matrix()[3][3]) ;
01029     //     LogTrace("CkfDebugger") << setw(15) << stlp5 << setw(15) << shlp5 << setw(15) << sqrt(state.localError().matrix()[4][4]) << 
01030     //       setw(15) << (stlp5-shlp5)/stlp5 << setw(15) << (stlp5-shlp5)/sqrt(state.localError().matrix()[4][4]) ;
01031 
01032     AlgebraicSymMatrix55 R = state.localError().matrix();
01033     R.Invert();
01034     double chi2 = ROOT::Math::Similarity(v-state.localParameters().vector(), R);
01035     LogTrace("CkfDebugger") << "chi2=" << chi2 ;
01036     return chi2;
01037   }
01038 
01039   return 0;//fixme
01040 
01041 }
01042 
01043 
01044 CkfDebugger::~CkfDebugger(){
01045   for (int it=0; it!=((int)(dump.size())); it++)
01046     edm::LogVerbatim("CkfDebugger") << "dump " << it << " " << dump[it] ;
01047   
01048   edm::LogVerbatim("CkfDebugger") ;
01049   edm::LogVerbatim("CkfDebugger") << "seedWithDelta=" <<  ((double)seedWithDelta/totSeeds) ;
01050   edm::LogVerbatim("CkfDebugger") << "problems=" << ((double)problems/totSeeds) ;
01051   edm::LogVerbatim("CkfDebugger") << "no_sim_hit=" << ((double)no_sim_hit/totSeeds) ;
01052   edm::LogVerbatim("CkfDebugger") << "no_layer=" << ((double)no_layer/totSeeds) ;
01053   edm::LogVerbatim("CkfDebugger") << "layer_not_found=" << ((double)layer_not_found/totSeeds) ;
01054   edm::LogVerbatim("CkfDebugger") << "det_not_found=" << ((double)det_not_found/totSeeds) ;
01055   edm::LogVerbatim("CkfDebugger") << "chi2gt30=" << ((double)chi2gt30/totSeeds) ;
01056   edm::LogVerbatim("CkfDebugger") << "chi2gt30deltaSeed=" << ((double)chi2gt30deltaSeed/totSeeds) ;
01057   edm::LogVerbatim("CkfDebugger") << "chi2gt30delta=" << ((double)chi2gt30delta/totSeeds) ;
01058   edm::LogVerbatim("CkfDebugger") << "chi2ls30=" << ((double)chi2ls30/totSeeds) ;
01059   edm::LogVerbatim("CkfDebugger") << "simple_hit_not_found=" << ((double)simple_hit_not_found/totSeeds) ;
01060   edm::LogVerbatim("CkfDebugger") << "no_component=" << ((double)no_component/totSeeds) ;
01061   edm::LogVerbatim("CkfDebugger") << "only_one_component=" << ((double)only_one_component/totSeeds) ;
01062   edm::LogVerbatim("CkfDebugger") << "matched_not_found=" << ((double)matched_not_found/totSeeds) ;
01063   edm::LogVerbatim("CkfDebugger") << "matched_not_associated=" << ((double)matched_not_associated/totSeeds) ;
01064   edm::LogVerbatim("CkfDebugger") << "partner_det_not_fuond=" << ((double)partner_det_not_fuond/totSeeds) ;
01065   edm::LogVerbatim("CkfDebugger") << "glued_det_not_fuond=" << ((double)glued_det_not_fuond/totSeeds) ;
01066   edm::LogVerbatim("CkfDebugger") << "propagation=" << ((double)propagation/totSeeds) ;
01067   edm::LogVerbatim("CkfDebugger") << "other=" << ((double)other/totSeeds) ;
01068   edm::LogVerbatim("CkfDebugger") << "totchi2gt30=" << ((double)totchi2gt30/totSeeds) ;
01069   edm::LogVerbatim("CkfDebugger") << "totSeeds=" << totSeeds ;
01070   edm::LogVerbatim("CkfDebugger") ;
01071   
01072   edm::LogVerbatim("CkfDebugger") << "layer navigation problems:" ;
01073   for (int i=0; i!=6; i++)
01074     for (int j=0; j!=9; j++){
01075       if (i==0 && j>2) break;
01076       if (i==1 && j>1) break;
01077       if (i==2 && j>3) break;
01078       if (i==3 && j>2) break;
01079       if (i==4 && j>5) break;
01080       if (i==5 && j>8) break;
01081       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump2[pair<int,int>(i,j)] ;
01082     }
01083   edm::LogVerbatim("CkfDebugger") << "\nlayer with hit having chi2>30:" ;
01084   for (int i=0; i!=6; i++)
01085     for (int j=0; j!=9; j++){
01086       if (i==0 && j>2) break;
01087       if (i==1 && j>1) break;
01088       if (i==2 && j>3) break;
01089       if (i==3 && j>2) break;
01090       if (i==4 && j>5) break;
01091       if (i==5 && j>8) break;
01092       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump3[pair<int,int>(i,j)] ;
01093     }
01094   edm::LogVerbatim("CkfDebugger") << "\nlayer with hit having chi2>30 for delta rays:" ;
01095   for (int i=0; i!=6; i++)
01096     for (int j=0; j!=9; j++){
01097       if (i==0 && j>2) break;
01098       if (i==1 && j>1) break;
01099       if (i==2 && j>3) break;
01100       if (i==3 && j>2) break;
01101       if (i==4 && j>5) break;
01102       if (i==5 && j>8) break;
01103       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump5[pair<int,int>(i,j)] ;
01104     }
01105   edm::LogVerbatim("CkfDebugger") << "\nlayer with det not found:" ;
01106   for (int i=0; i!=6; i++)
01107     for (int j=0; j!=9; j++){
01108       if (i==0 && j>2) break;
01109       if (i==1 && j>1) break;
01110       if (i==2 && j>3) break;
01111       if (i==3 && j>2) break;
01112       if (i==4 && j>5) break;
01113       if (i==5 && j>8) break;
01114       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump4[pair<int,int>(i,j)] ;
01115     }
01116   edm::LogVerbatim("CkfDebugger") << "\nlayer with correct RecHit after missing Sim Hit:" ;
01117   for (int i=0; i!=6; i++)
01118     for (int j=0; j!=9; j++){
01119       if (i==0 && j>2) break;
01120       if (i==1 && j>1) break;
01121       if (i==2 && j>3) break;
01122       if (i==3 && j>2) break;
01123       if (i==4 && j>5) break;
01124       if (i==5 && j>8) break;
01125       edm::LogVerbatim("CkfDebugger") << "det=" << i+1 << " lay=" << j+1 << " " << dump6[pair<int,int>(i,j)] ;
01126     }
01127   hchi2seedAll->Write();
01128   hchi2seedProb->Write();
01129   std::stringstream title;
01130   for (int i=0; i!=6; i++)
01131     for (int j=0; j!=9; j++){
01132       if (i==0 && j>2) break;
01133       if (i==1 && j>1) break;
01134       if (i==2 && j>3) break;
01135       if (i==3 && j>2) break;
01136       if (i==4 && j>5) break;
01137       if (i==5 && j>8) break;
01138       title.str("");
01139       title << "pullX_" << i+1 << "-" << j+1 << "_sh-rh";
01140       hPullX_shrh[title.str()]->Write();
01141       title.str("");
01142       title << "pullY_" << i+1 << "-" << j+1 << "_sh-rh";
01143       hPullY_shrh[title.str()]->Write();
01144       title.str("");
01145       title << "pullX_" << i+1 << "-" << j+1 << "_sh-st";
01146       hPullX_shst[title.str()]->Write();
01147       title.str("");
01148       title << "pullY_" << i+1 << "-" << j+1 << "_sh-st";
01149       hPullY_shst[title.str()]->Write();
01150       title.str("");
01151       title << "pullX_" << i+1 << "-" << j+1 << "_st-rh";
01152       hPullX_strh[title.str()]->Write();
01153       title.str("");
01154       title << "pullY_" << i+1 << "-" << j+1 << "_st-rh";
01155       hPullY_strh[title.str()]->Write();
01156       title.str("");
01157       title << "PullGP_X_" << i+1 << "-" << j+1 << "_sh-st";
01158       hPullGP_X_shst[title.str()]->Write();
01159       title.str("");
01160       title << "PullGP_Y_" << i+1 << "-" << j+1 << "_sh-st";
01161       hPullGP_Y_shst[title.str()]->Write();
01162       title.str("");
01163       title << "PullGP_Z_" << i+1 << "-" << j+1 << "_sh-st";
01164       hPullGP_Z_shst[title.str()]->Write();
01165       if ( ((i==2||i==4)&&(j==0||j==1)) || (i==3||i==5) ){
01166         title.str("");
01167         title << "pullM_" << i+1 << "-" << j+1 << "_sh-rh";
01168         hPullM_shrh[title.str()]->Write();
01169         title.str("");
01170         title << "pullS_" << i+1 << "-" << j+1 << "_sh-rh";
01171         hPullS_shrh[title.str()]->Write();
01172         title.str("");
01173         title << "pullM_" << i+1 << "-" << j+1 << "_sh-st";
01174         hPullM_shst[title.str()]->Write();
01175         title.str("");
01176         title << "pullS_" << i+1 << "-" << j+1 << "_sh-st";
01177         hPullS_shst[title.str()]->Write();
01178         title.str("");
01179         title << "pullM_" << i+1 << "-" << j+1 << "_st-rh";
01180         hPullM_strh[title.str()]->Write();
01181         title.str("");
01182         title << "pullS_" << i+1 << "-" << j+1 << "_st-rh";
01183         hPullS_strh[title.str()]->Write();
01184       }
01185     }
01186   hPullGPXvsGPX_shst->Write();
01187   hPullGPXvsGPY_shst->Write();
01188   hPullGPXvsGPZ_shst->Write();
01189   hPullGPXvsGPr_shst->Write();
01190   hPullGPXvsGPeta_shst->Write();
01191   hPullGPXvsGPphi_shst->Write();
01192   
01193   //file->Write();
01194   file->Close();
01195 }