CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/RecoEgamma/EgammaElectronAlgos/src/PixelHitMatcher.cc

Go to the documentation of this file.
00001 
00002 #include "DataFormats/Math/interface/Point3D.h"
00003 #include "RecoEgamma/EgammaElectronAlgos/interface/PixelHitMatcher.h"
00004 #include "RecoEgamma/EgammaElectronAlgos/interface/PixelMatchNextLayers.h"
00005 #include "RecoEgamma/EgammaElectronAlgos/interface/ElectronUtilities.h"
00006 #include "TrackingTools/PatternTools/interface/TrajectoryMeasurement.h"
00007 #include "TrackingTools/DetLayers/interface/DetLayer.h"
00008 #include "TrackingTools/DetLayers/interface/NavigationSetter.h"
00009 #include "TrackingTools/MeasurementDet/interface/LayerMeasurements.h"
00010 #include "RecoTracker/MeasurementDet/interface/MeasurementTracker.h"
00011 #include "RecoTracker/TkNavigation/interface/SimpleNavigationSchool.h"
00012 #include "MagneticField/Engine/interface/MagneticField.h"
00013 #include "DataFormats/DetId/interface/DetId.h"
00014 #include "DataFormats/GeometryCommonDetAlgo/interface/PerpendicularBoundPlaneBuilder.h"
00015 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017 
00018 #include <typeinfo>
00019 
00020 using namespace reco ;
00021 using namespace std ;
00022 
00023 PixelHitMatcher::PixelHitMatcher
00024  ( float phi1min, float phi1max,
00025    float phi2minB, float phi2maxB, float phi2minF, float phi2maxF,
00026    float z2minB, float z2maxB, float r2minF, float r2maxF,
00027    float rMinI, float rMaxI, bool searchInTIDTEC)
00028  : //zmin1 and zmax1 are dummy at this moment, set from beamspot later
00029    meas1stBLayer(phi1min,phi1max,0.,0.), meas2ndBLayer(phi2minB,phi2maxB,z2minB,z2maxB),
00030    meas1stFLayer(phi1min,phi1max,0.,0.), meas2ndFLayer(phi2minF,phi2maxF,r2minF,r2maxF),
00031    startLayers(),
00032    prop1stLayer(0), prop2ndLayer(0),theGeometricSearchTracker(0),theLayerMeasurements(0),vertex_(0.),
00033    searchInTIDTEC_(searchInTIDTEC), useRecoVertex_(false)
00034  {
00035   meas1stFLayer.setRRangeI(rMinI,rMaxI) ;
00036   meas2ndFLayer.setRRangeI(rMinI,rMaxI) ;
00037  }
00038 
00039 PixelHitMatcher::~PixelHitMatcher()
00040  {
00041   delete prop1stLayer ;
00042   delete prop2ndLayer ;
00043   delete theLayerMeasurements ;
00044  }
00045 
00046 void PixelHitMatcher::set1stLayer( float dummyphi1min, float dummyphi1max )
00047  {
00048   meas1stBLayer.setPhiRange(dummyphi1min,dummyphi1max) ;
00049   meas1stFLayer.setPhiRange(dummyphi1min,dummyphi1max) ;
00050  }
00051 
00052 void PixelHitMatcher::set1stLayerZRange( float zmin1, float zmax1 )
00053  {
00054   meas1stBLayer.setZRange(zmin1,zmax1) ;
00055   meas1stFLayer.setRRange(zmin1,zmax1) ;
00056  }
00057 
00058 void PixelHitMatcher::set2ndLayer( float dummyphi2minB, float dummyphi2maxB, float dummyphi2minF, float dummyphi2maxF )
00059  {
00060   meas2ndBLayer.setPhiRange(dummyphi2minB,dummyphi2maxB) ;
00061   meas2ndFLayer.setPhiRange(dummyphi2minF,dummyphi2maxF) ;
00062  }
00063 
00064 void PixelHitMatcher::setUseRecoVertex( bool val )
00065  { useRecoVertex_ = val ; }
00066 
00067 void PixelHitMatcher::setES
00068  ( const MagneticField * magField,
00069    const MeasurementTracker * theMeasurementTracker,
00070    const TrackerGeometry * trackerGeometry )
00071  {
00072   if (theMeasurementTracker)
00073    {
00074     theGeometricSearchTracker=theMeasurementTracker->geometricSearchTracker() ;
00075     startLayers.setup(theGeometricSearchTracker) ;
00076     if (theLayerMeasurements ) delete theLayerMeasurements ;
00077     theLayerMeasurements = new LayerMeasurements(theMeasurementTracker) ;
00078    }
00079 
00080   theMagField = magField ;
00081   theTrackerGeometry = trackerGeometry ;
00082   float mass=.000511 ; // electron propagation
00083   if (prop1stLayer) delete prop1stLayer ;
00084   prop1stLayer = new PropagatorWithMaterial(oppositeToMomentum,mass,theMagField) ;
00085   if (prop2ndLayer) delete prop2ndLayer ;
00086   prop2ndLayer = new PropagatorWithMaterial(alongMomentum,mass,theMagField) ;
00087  }
00088 
00089 vector<CLHEP::Hep3Vector> PixelHitMatcher::predicted1Hits()
00090  { return pred1Meas ; }
00091 
00092 vector<CLHEP::Hep3Vector> PixelHitMatcher::predicted2Hits()
00093  { return pred2Meas ; }
00094 
00095 float PixelHitMatcher::getVertex()
00096  { return vertex_ ; }
00097 
00098 //CLHEP::Hep3Vector point_to_vector( const GlobalPoint & p )
00099 // { return CLHEP::Hep3Vector(p.x(),p.y(),p.z()) ; }
00100 
00101 std::vector<SeedWithInfo>
00102 PixelHitMatcher::compatibleSeeds
00103  ( TrajectorySeedCollection * seeds, const GlobalPoint & xmeas,
00104    const GlobalPoint & vprim, float energy, float fcharge )
00105  {
00106   int charge = int(fcharge) ;
00107 
00108   FreeTrajectoryState fts = myFTS(theMagField,xmeas, vprim, energy, charge);
00109   PerpendicularBoundPlaneBuilder bpb;
00110   TrajectoryStateOnSurface tsos(fts, *bpb(fts.position(), fts.momentum()));
00111 
00112   std::vector<SeedWithInfo> result ;
00113   mapTsos_.clear() ;
00114   mapTsos2_.clear() ;
00115   mapTsos_.reserve(seeds->size()) ;
00116   mapTsos2_.reserve(seeds->size()) ;
00117 
00118   for (unsigned int i=0;i<seeds->size();++i)
00119    {
00120     assert((*seeds)[i].nHits()<=8) ;
00121     TrajectorySeed::range rhits=(*seeds)[i].recHits();
00122 
00123     // build all possible pairs
00124     unsigned char rank1, rank2, hitsMask ;
00125     TrajectorySeed::const_iterator it1, it2 ;
00126     for ( rank1=0, it1=rhits.first ; it1!=rhits.second ; rank1++, it1++ )
00127      {
00128       for ( rank2=rank1+1, it2=it1+1 ; it2!=rhits.second ; rank2++, it2++ )
00129        {
00130         //TrajectorySeed::range r(it1,it2) ;
00131 
00132         // first Hit
00133         TrajectorySeed::const_iterator it=it1 ;
00134         if (!(*it).isValid()) continue;
00135         DetId id=(*it).geographicalId();
00136         const GeomDet *geomdet=theTrackerGeometry->idToDet((*it).geographicalId());
00137         LocalPoint lp=(*it).localPosition() ;
00138         GlobalPoint hitPos=geomdet->surface().toGlobal(lp) ;
00139 
00140         TrajectoryStateOnSurface tsos1;
00141         bool found = false;
00142         std::vector<std::pair<const GeomDet *, TrajectoryStateOnSurface> >::iterator itTsos ;
00143         for (itTsos=mapTsos_.begin();itTsos!=mapTsos_.end();++itTsos)
00144          {
00145           if ((*itTsos).first==geomdet)
00146            { found=true ; break ; }
00147          }
00148         if (!found)
00149          {
00150           tsos1 = prop1stLayer->propagate(tsos,geomdet->surface()) ;
00151           mapTsos_.push_back(std::pair<const GeomDet *, TrajectoryStateOnSurface>(geomdet,tsos1));
00152          }
00153         else
00154          { tsos1=(*itTsos).second ; }
00155 
00156         if (tsos1.isValid())
00157          {
00158           std::pair<bool,double> est;
00159           if (id.subdetId()%2==1) est=meas1stBLayer.estimate(vprim, tsos1,hitPos);
00160           else est=meas1stFLayer.estimate(vprim, tsos1,hitPos);
00161           if (!est.first) continue ;
00162 
00163           if (std::abs(normalized_phi(hitPos.phi()-xmeas.phi()))>2.5) continue ;
00164           EleRelPointPair pp1(hitPos,tsos1.globalParameters().position(),vprim) ;
00165           int subDet1 = id.subdetId() ;
00166           float dRz1 = (subDet1%2==1)?pp1.dZ():pp1.dPerp() ;
00167           float dPhi1 = pp1.dPhi() ;
00168 
00169           // now second Hit
00170           //CC@@
00171           //it++;
00172           it=it2 ;
00173           if (!(*it).isValid()) continue ;
00174 
00175           DetId id2=(*it).geographicalId();
00176           const GeomDet *geomdet2=theTrackerGeometry->idToDet((*it).geographicalId());
00177           TrajectoryStateOnSurface tsos2;
00178 
00179           double zVertex;
00180           if (!useRecoVertex_) // we don't know the z vertex position, get it from linear extrapolation
00181            {
00182             // compute the z vertex from the cluster point and the found pixel hit
00183             double pxHit1z = hitPos.z();
00184             double pxHit1x = hitPos.x();
00185             double pxHit1y = hitPos.y();
00186             double r1diff = (pxHit1x-vprim.x())*(pxHit1x-vprim.x()) + (pxHit1y-vprim.y())*(pxHit1y-vprim.y()) ;
00187             r1diff=sqrt(r1diff) ;
00188             double r2diff = (xmeas.x()-pxHit1x)*(xmeas.x()-pxHit1x) + (xmeas.y()-pxHit1y)*(xmeas.y()-pxHit1y) ;
00189             r2diff=sqrt(r2diff);
00190             zVertex = pxHit1z - r1diff*(xmeas.z()-pxHit1z)/r2diff;
00191            }
00192           else // here use rather the reco vertex z position
00193            { zVertex = vprim.z() ; }
00194 
00195           GlobalPoint vertex(vprim.x(),vprim.y(),zVertex) ;
00196           FreeTrajectoryState fts2 = myFTS(theMagField,hitPos,vertex,energy, charge) ;
00197 
00198           found = false;
00199           std::vector<std::pair< std::pair<const GeomDet *,GlobalPoint>, TrajectoryStateOnSurface> >::iterator itTsos2 ;
00200           for (itTsos2=mapTsos2_.begin();itTsos2!=mapTsos2_.end();++itTsos2)
00201            {
00202             if (((*itTsos2).first).first==geomdet2 &&
00203                 (((*itTsos2).first).second).x()==hitPos.x() &&
00204                 (((*itTsos2).first).second).y()==hitPos.y() &&
00205                 (((*itTsos2).first).second).z()==hitPos.z()  )
00206              {
00207               found=true;
00208               break;
00209              }
00210            }
00211           if (!found)
00212            {
00213             tsos2 = prop2ndLayer->propagate(fts2,geomdet2->surface()) ;
00214             std::pair<const GeomDet *,GlobalPoint> pair(geomdet2,hitPos);
00215             mapTsos2_.push_back(std::pair<std::pair<const GeomDet *,GlobalPoint>, TrajectoryStateOnSurface> (pair,tsos2));
00216            }
00217           else
00218            { tsos2=(*itTsos2).second ; }
00219 
00220           if (tsos2.isValid())
00221            {
00222             LocalPoint lp2=(*it).localPosition() ;
00223             GlobalPoint hitPos2=geomdet2->surface().toGlobal(lp2) ;
00224             std::pair<bool,double> est2 ;
00225             if (id2.subdetId()%2==1) est2=meas2ndBLayer.estimate(vertex, tsos2,hitPos2) ;
00226             else est2=meas2ndFLayer.estimate(vertex, tsos2,hitPos2) ;
00227             if (est2.first)
00228              {
00229               EleRelPointPair pp2(hitPos2,tsos2.globalParameters().position(),vertex) ;
00230               int subDet2 = id2.subdetId() ;
00231               float dRz2 = (subDet2%2==1)?pp2.dZ():pp2.dPerp() ;
00232               float dPhi2 = pp2.dPhi() ;
00233               hitsMask = (1<<rank1)|(1<<rank2) ;
00234               result.push_back(SeedWithInfo((*seeds)[i],hitsMask,subDet2,dRz2,dPhi2,subDet1,dRz1,dPhi1)) ;
00235              }
00236            }
00237          } // end tsos1 is valid
00238        } // end loop on second seed hit
00239      } // end loop on first seed hit
00240    } // end loop on seeds
00241 
00242   mapTsos_.clear() ;
00243   mapTsos2_.clear() ;
00244 
00245   return result ;
00246  }
00247 
00248 //========================= OBSOLETE ? =========================
00249 
00250 vector< pair< RecHitWithDist, PixelHitMatcher::ConstRecHitPointer > >
00251 PixelHitMatcher::compatibleHits
00252  ( const GlobalPoint & xmeas,
00253    const GlobalPoint & vprim,
00254    float energy, float fcharge )
00255  {
00256   float SCl_phi = xmeas.phi();
00257 
00258   int charge = int(fcharge);
00259   // return all compatible RecHit pairs (vector< TSiPixelRecHit>)
00260   vector<pair<RecHitWithDist, ConstRecHitPointer> > result;
00261   LogDebug("") << "[PixelHitMatcher::compatibleHits] entering .. ";
00262 
00263   vector<TrajectoryMeasurement> validMeasurements;
00264   vector<TrajectoryMeasurement> invalidMeasurements;
00265 
00266   typedef vector<TrajectoryMeasurement>::const_iterator aMeas;
00267 
00268   pred1Meas.clear();
00269   pred2Meas.clear();
00270 
00271   typedef vector<BarrelDetLayer*>::const_iterator BarrelLayerIterator;
00272   BarrelLayerIterator firstLayer = startLayers.firstBLayer();
00273 
00274   FreeTrajectoryState fts =myFTS(theMagField,xmeas, vprim,
00275                                  energy, charge);
00276 
00277   PerpendicularBoundPlaneBuilder bpb;
00278   TrajectoryStateOnSurface tsos(fts, *bpb(fts.position(), fts.momentum()));
00279 
00280   if (tsos.isValid()) {
00281     vector<TrajectoryMeasurement> pixelMeasurements =
00282       theLayerMeasurements->measurements(**firstLayer,tsos,
00283                                          *prop1stLayer, meas1stBLayer);
00284 
00285     LogDebug("") <<"[PixelHitMatcher::compatibleHits] nbr of hits compatible with extrapolation to first layer: " << pixelMeasurements.size();
00286     for (aMeas m=pixelMeasurements.begin(); m!=pixelMeasurements.end(); m++){
00287      if (m->recHit()->isValid()) {
00288        float localDphi = normalized_phi(SCl_phi-m->forwardPredictedState().globalPosition().phi()) ;
00289        if(std::abs(localDphi)>2.5)continue;
00290         CLHEP::Hep3Vector prediction(m->forwardPredictedState().globalPosition().x(),
00291                               m->forwardPredictedState().globalPosition().y(),
00292                               m->forwardPredictedState().globalPosition().z());
00293         LogDebug("") << "[PixelHitMatcher::compatibleHits] compatible hit position " << m->recHit()->globalPosition();
00294         LogDebug("") << "[PixelHitMatcher::compatibleHits] predicted position " << m->forwardPredictedState().globalPosition();
00295         pred1Meas.push_back( prediction);
00296 
00297         validMeasurements.push_back(*m);
00298 
00299         LogDebug("") <<"[PixelHitMatcher::compatibleHits] Found a rechit in layer ";
00300         const BarrelDetLayer *bdetl = dynamic_cast<const BarrelDetLayer *>(*firstLayer);
00301         if (bdetl) {
00302           LogDebug("") <<" with radius "<<bdetl->specificSurface().radius();
00303         }
00304         else  LogDebug("") <<"Could not downcast!!";
00305      }
00306     }
00307 
00308 
00309     // check if there are compatible 1st hits in the second layer
00310     firstLayer++;
00311 
00312     vector<TrajectoryMeasurement> pixel2Measurements =
00313       theLayerMeasurements->measurements(**firstLayer,tsos,
00314                                          *prop1stLayer, meas1stBLayer);
00315 
00316     for (aMeas m=pixel2Measurements.begin(); m!=pixel2Measurements.end(); m++){
00317       if (m->recHit()->isValid()) {
00318         float localDphi = normalized_phi(SCl_phi-m->forwardPredictedState().globalPosition().phi()) ;
00319         if(std::abs(localDphi)>2.5)continue;
00320         CLHEP::Hep3Vector prediction(m->forwardPredictedState().globalPosition().x(),
00321                               m->forwardPredictedState().globalPosition().y(),
00322                               m->forwardPredictedState().globalPosition().z());
00323         pred1Meas.push_back( prediction);
00324         LogDebug("")  << "[PixelHitMatcher::compatibleHits] compatible hit position " << m->recHit()->globalPosition() << endl;
00325         LogDebug("") << "[PixelHitMatcher::compatibleHits] predicted position " << m->forwardPredictedState().globalPosition() << endl;
00326 
00327         validMeasurements.push_back(*m);
00328         LogDebug("") <<"[PixelHitMatcher::compatibleHits] Found a rechit in layer ";
00329         const BarrelDetLayer *bdetl = dynamic_cast<const BarrelDetLayer *>(*firstLayer);
00330         if (bdetl) {
00331           LogDebug("") <<" with radius "<<bdetl->specificSurface().radius();
00332         }
00333         else  LogDebug("") <<"Could not downcast!!";
00334       }
00335 
00336     }
00337   }
00338 
00339 
00340   // check if there are compatible 1st hits the forward disks
00341   typedef vector<ForwardDetLayer*>::const_iterator ForwardLayerIterator;
00342   ForwardLayerIterator flayer;
00343 
00344   TrajectoryStateOnSurface tsosfwd(fts, *bpb(fts.position(), fts.momentum()));
00345   if (tsosfwd.isValid()) {
00346 
00347     for (int i=0; i<2; i++) {
00348       i == 0 ? flayer = startLayers.pos1stFLayer() : flayer = startLayers.neg1stFLayer();
00349 
00350       if (i==0 && xmeas.z() < -100. ) continue;
00351       if (i==1 && xmeas.z() > 100. ) continue;
00352 
00353       vector<TrajectoryMeasurement> pixelMeasurements =
00354         theLayerMeasurements->measurements(**flayer, tsosfwd,
00355                                            *prop1stLayer, meas1stFLayer);
00356 
00357       for (aMeas m=pixelMeasurements.begin(); m!=pixelMeasurements.end(); m++){
00358         if (m->recHit()->isValid()) {
00359           float localDphi = normalized_phi(SCl_phi-m->forwardPredictedState().globalPosition().phi());
00360           if(std::abs(localDphi)>2.5)continue;
00361           CLHEP::Hep3Vector prediction(m->forwardPredictedState().globalPosition().x(),
00362                                 m->forwardPredictedState().globalPosition().y(),
00363                                 m->forwardPredictedState().globalPosition().z());
00364           pred1Meas.push_back( prediction);
00365 
00366           validMeasurements.push_back(*m);
00367         }
00368       }
00369 
00370       //check if there are compatible 1st hits the outer forward disks
00371       if (searchInTIDTEC_) {
00372         flayer++;
00373 
00374         vector<TrajectoryMeasurement> pixel2Measurements =
00375           theLayerMeasurements->measurements(**flayer, tsosfwd,
00376                                              *prop1stLayer, meas1stFLayer);
00377 
00378         for (aMeas m=pixel2Measurements.begin(); m!=pixel2Measurements.end(); m++){
00379           if (m->recHit()->isValid()) {
00380             float localDphi = normalized_phi(SCl_phi-m->forwardPredictedState().globalPosition().phi()) ;
00381             if(std::abs(localDphi)>2.5)continue;
00382             CLHEP::Hep3Vector prediction(m->forwardPredictedState().globalPosition().x(),
00383                                   m->forwardPredictedState().globalPosition().y(),
00384                                   m->forwardPredictedState().globalPosition().z());
00385             pred1Meas.push_back( prediction);
00386 
00387             validMeasurements.push_back(*m);
00388           }
00389           //    else{std::cout<<" hit non valid "<<std::endl; }
00390         }  //end 1st hit in outer f disk
00391       }
00392     }
00393   }
00394 
00395   // now we have the vector of all valid measurements of the first point
00396   for (unsigned i=0; i<validMeasurements.size(); i++){
00397 
00398     const DetLayer * newLayer = theGeometricSearchTracker->detLayer(validMeasurements[i].recHit()->det()->geographicalId());
00399 
00400     double zVertex ;
00401     if (!useRecoVertex_)
00402      {
00403       // we don't know the z vertex position, get it from linear extrapolation
00404       // compute the z vertex from the cluster point and the found pixel hit
00405       double pxHit1z = validMeasurements[i].recHit()->det()->surface().toGlobal(
00406         validMeasurements[i].recHit()->localPosition()).z();
00407       double pxHit1x = validMeasurements[i].recHit()->det()->surface().toGlobal(
00408         validMeasurements[i].recHit()->localPosition()).x();
00409       double pxHit1y = validMeasurements[i].recHit()->det()->surface().toGlobal(
00410         validMeasurements[i].recHit()->localPosition()).y();
00411       double r1diff = (pxHit1x-vprim.x())*(pxHit1x-vprim.x()) + (pxHit1y-vprim.y())*(pxHit1y-vprim.y());
00412       r1diff=sqrt(r1diff);
00413       double r2diff = (xmeas.x()-pxHit1x)*(xmeas.x()-pxHit1x) + (xmeas.y()-pxHit1y)*(xmeas.y()-pxHit1y);
00414       r2diff=sqrt(r2diff);
00415       zVertex = pxHit1z - r1diff*(xmeas.z()-pxHit1z)/r2diff;
00416      }
00417     else
00418      {
00419       // here we use the reco vertex z position
00420       zVertex = vprim.z();
00421      }
00422 
00423     if (i==0)
00424      { vertex_ = zVertex; }
00425 
00426     GlobalPoint vertexPred(vprim.x(),vprim.y(),zVertex) ;
00427     GlobalPoint hitPos( validMeasurements[i].recHit()->det()->surface().toGlobal( validMeasurements[i].recHit()->localPosition() ) ) ;
00428 
00429     FreeTrajectoryState secondFTS=myFTS(theMagField,hitPos,vertexPred,energy, charge);
00430 
00431     PixelMatchNextLayers secondHit(theLayerMeasurements, newLayer, secondFTS,
00432                                    prop2ndLayer, &meas2ndBLayer,&meas2ndFLayer,searchInTIDTEC_);
00433     vector<CLHEP::Hep3Vector> predictions = secondHit.predictionInNextLayers();
00434 
00435     for (unsigned it = 0; it < predictions.size(); it++) pred2Meas.push_back(predictions[it]);
00436 
00437     // we may get more than one valid second measurements here even for single electrons:
00438     // two hits from the same layer/disk (detector overlap) or from the loop over the
00439     // next layers in EPMatchLoopNextLayers. Take only the 1st hit.
00440 
00441     if(!secondHit.measurementsInNextLayers().empty()){
00442       for(unsigned int shit=0; shit<secondHit.measurementsInNextLayers().size(); shit++)
00443         {
00444           float dphi = normalized_phi(pred1Meas[i].phi()-validMeasurements[i].recHit()->globalPosition().phi()) ;
00445           if (std::abs(dphi)<2.5)
00446             {
00447               ConstRecHitPointer pxrh = validMeasurements[i].recHit();
00448               RecHitWithDist rh(pxrh,dphi);
00449 
00450               //  pxrh = secondHit.measurementsInNextLayers()[0].recHit();
00451               pxrh = secondHit.measurementsInNextLayers()[shit].recHit();
00452 
00453               pair<RecHitWithDist,ConstRecHitPointer> compatiblePair = pair<RecHitWithDist,ConstRecHitPointer>(rh,pxrh) ;
00454               result.push_back(compatiblePair);
00455               break;
00456             }
00457         }
00458     }
00459   }
00460   return result;
00461 }
00462 
00463