CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/RecoLocalTracker/SiStripRecHitConverter/src/SiStripRecHitMatcher.cc

Go to the documentation of this file.
00001 // File: SiStripRecHitMatcher.cc
00002 // Description:  Matches into rechits
00003 // Author:  C.Genta
00004 #include "RecoLocalTracker/SiStripRecHitConverter/interface/SiStripRecHitMatcher.h"
00005 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00006 #include "Geometry/TrackerGeometryBuilder/interface/GluedGeomDet.h"
00007 #include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
00008 #include "DataFormats/CLHEP/interface/AlgebraicObjects.h"
00009 
00010 #include "TrackingTools/TransientTrackingRecHit/interface/HelpertRecHit2DLocalPos.h"
00011 #include<boost/bind.hpp>
00012 
00013 #include <DataFormats/TrackingRecHit/interface/AlignmentPositionError.h>
00014 
00015 
00016 
00017 
00018 SiStripRecHitMatcher::SiStripRecHitMatcher(const edm::ParameterSet& conf){   
00019   scale_=conf.getParameter<double>("NSigmaInside");  
00020 }
00021 
00022 SiStripRecHitMatcher::SiStripRecHitMatcher(const double theScale){   
00023   scale_=theScale;  
00024 }
00025 
00026 
00027 //match a single hit
00028 SiStripMatchedRecHit2D * SiStripRecHitMatcher::match(const SiStripRecHit2D *monoRH, 
00029                                                      const SiStripRecHit2D *stereoRH,
00030                                                      const GluedGeomDet* gluedDet,
00031                                                      LocalVector trackdirection) const{
00032   SimpleHitCollection stereoHits(1,stereoRH);
00033   std::vector<SiStripMatchedRecHit2D*>  collection;
00034   match( monoRH,
00035          stereoHits.begin(), stereoHits.end(),
00036          collection,
00037          gluedDet,trackdirection);
00038   
00039   return collection.empty() ? (SiStripMatchedRecHit2D*)(0) : collection.front();
00040 }
00041 
00042 //repeat matching for an already  a single hit
00043 SiStripMatchedRecHit2D* SiStripRecHitMatcher::match(const SiStripMatchedRecHit2D *origRH, 
00044                                                     const GluedGeomDet* gluedDet,
00045                                                     LocalVector trackdirection) const{
00046   
00047   const SiStripRecHit2D* theMonoRH   = origRH->monoHit();
00048   // const SiStripRecHit2D* theStereoRH = origRH->stereoHit();
00049   SimpleHitCollection theStereoHits(1, origRH->stereoHit());
00050   // theStereoHits.push_back(theStereoRH);
00051   
00052   std::vector<SiStripMatchedRecHit2D*>  collection;
00053   match( theMonoRH,
00054          theStereoHits.begin(), theStereoHits.end(),
00055          collection,
00056          gluedDet,trackdirection);
00057   
00058   return collection.empty() ? (SiStripMatchedRecHit2D*)(0) : collection.front();
00059   
00060 }
00061 
00062 
00063 edm::OwnVector<SiStripMatchedRecHit2D> 
00064 SiStripRecHitMatcher::match( const  SiStripRecHit2D *monoRH,
00065                              RecHitIterator begin, RecHitIterator end, 
00066                              const GluedGeomDet* gluedDet,
00067                              LocalVector trackdirection) const
00068 {
00069   SimpleHitCollection stereoHits;
00070   stereoHits.reserve(end-begin);
00071 
00072   for (RecHitIterator i=begin; i != end; ++i) {
00073     stereoHits.push_back( &(*i)); // convert to simple pointer
00074   }
00075   return match( monoRH,
00076                 stereoHits.begin(), stereoHits.end(), 
00077                 gluedDet,trackdirection);
00078 }
00079 
00080 edm::OwnVector<SiStripMatchedRecHit2D> 
00081 SiStripRecHitMatcher::match( const  SiStripRecHit2D *monoRH,
00082                              SimpleHitIterator begin, SimpleHitIterator end,
00083                              const GluedGeomDet* gluedDet,
00084                              LocalVector trackdirection) const {
00085   edm::OwnVector<SiStripMatchedRecHit2D> collector;
00086   collector.reserve(end-begin); // a resonable estimate of its size... 
00087   match(monoRH,begin,end,collector,gluedDet,trackdirection);
00088   return collector;
00089 }
00090 
00091 
00092 void
00093 SiStripRecHitMatcher::match( const SiStripRecHit2D *monoRH,
00094                              SimpleHitIterator begin, SimpleHitIterator end,
00095                              edm::OwnVector<SiStripMatchedRecHit2D> & collector, 
00096                              const GluedGeomDet* gluedDet,
00097                              LocalVector trackdirection) const {
00098 
00099   std::vector<SiStripMatchedRecHit2D*> result;
00100   result.reserve(end-begin);
00101   match(monoRH,begin,end,result,gluedDet,trackdirection);
00102   for (std::vector<SiStripMatchedRecHit2D*>::iterator p=result.begin(); p!=result.end();
00103        p++) collector.push_back(*p);
00104 }
00105 
00106 namespace {
00107   // FIXME for c++0X
00108   inline void pb(std::vector<SiStripMatchedRecHit2D*> & v, SiStripMatchedRecHit2D* h) {
00109     v.push_back(h);
00110   }
00111 }
00112 
00113 void
00114 SiStripRecHitMatcher::match( const SiStripRecHit2D *monoRH,
00115                              SimpleHitIterator begin, SimpleHitIterator end,
00116                              std::vector<SiStripMatchedRecHit2D*> & collector, 
00117                              const GluedGeomDet* gluedDet,
00118                              LocalVector trackdirection) const {
00119   Collector result(boost::bind(&pb,boost::ref(collector),
00120                              boost::bind(&SiStripMatchedRecHit2D::clone,_1)));
00121   match(monoRH,begin,end,result,gluedDet,trackdirection);
00122 }
00123 
00124 
00125 // this is the one used by the RecHitConverter
00126 void
00127 SiStripRecHitMatcher::match( const SiStripRecHit2D *monoRH,
00128                              RecHitIterator begin, RecHitIterator end,
00129                              CollectorMatched & collector,
00130                              const GluedGeomDet* gluedDet,
00131                              LocalVector trackdirection) const {
00132   
00133   // is this really needed now????
00134   SimpleHitCollection stereoHits;
00135   stereoHits.reserve(end-begin);
00136   for (RecHitIterator i=begin; i != end; ++i) 
00137     stereoHits.push_back( &(*i)); // convert to simple pointer
00138   
00139   return match( monoRH,
00140                 stereoHits.begin(), stereoHits.end(),
00141                 collector,
00142                 gluedDet,trackdirection);
00143 }
00144 
00145 void
00146 SiStripRecHitMatcher::match( const SiStripRecHit2D *monoRH,
00147                              SimpleHitIterator begin, SimpleHitIterator end,
00148                              CollectorMatched & collector,
00149                              const GluedGeomDet* gluedDet,
00150                              LocalVector trackdirection) const {
00151 
00152   Collector result(boost::bind(&CollectorMatched::push_back,boost::ref(collector),_1));
00153   match(monoRH,begin,end,result,gluedDet,trackdirection);
00154   
00155 }
00156 
00157 
00158 // the "real implementation"
00159 void
00160 SiStripRecHitMatcher::match( const SiStripRecHit2D *monoRH,
00161                              SimpleHitIterator begin, SimpleHitIterator end,
00162                              Collector & collector, 
00163                              const GluedGeomDet* gluedDet,
00164                              LocalVector trackdirection) const {
00165   // stripdet = mono
00166   // partnerstripdet = stereo
00167   const GeomDetUnit* stripdet = gluedDet->monoDet();
00168   const GeomDetUnit* partnerstripdet = gluedDet->stereoDet();
00169   const StripTopology& topol=(const StripTopology&)stripdet->topology();
00170 
00171   // position of the initial and final point of the strip (RPHI cluster) in local strip coordinates
00172   double RPHIpointX = topol.measurementPosition(monoRH->localPositionFast()).x();
00173   MeasurementPoint RPHIpointini(RPHIpointX,-0.5);
00174   MeasurementPoint RPHIpointend(RPHIpointX,0.5);
00175 
00176   // position of the initial and final point of the strip in local coordinates (mono det)
00177   StripPosition stripmono=StripPosition(topol.localPosition(RPHIpointini),topol.localPosition(RPHIpointend));
00178 
00179   if(trackdirection.mag2()<FLT_MIN){// in case of no track hypothesis assume a track from the origin through the center of the strip
00180     LocalPoint lcenterofstrip=monoRH->localPositionFast();
00181     GlobalPoint gcenterofstrip=(stripdet->surface()).toGlobal(lcenterofstrip);
00182     GlobalVector gtrackdirection=gcenterofstrip-GlobalPoint(0,0,0);
00183     trackdirection=(gluedDet->surface()).toLocal(gtrackdirection);
00184   }
00185 
00186   //project mono hit on glued det
00187   StripPosition projectedstripmono=project(stripdet,gluedDet,stripmono,trackdirection);
00188   const StripTopology& partnertopol=(const StripTopology&)partnerstripdet->topology();
00189 
00190   double m00 = -(projectedstripmono.second.y()-projectedstripmono.first.y()); 
00191   double m01 =  (projectedstripmono.second.x()-projectedstripmono.first.x());
00192   double c0  =  m01*projectedstripmono.first.y()   + m00*projectedstripmono.first.x();
00193  
00194   //error calculation (the part that depends on mono RH only)
00195   //  LocalVector  RPHIpositiononGluedendvector=projectedstripmono.second-projectedstripmono.first;
00196   /*
00197   double l1 = 1./RPHIpositiononGluedendvector.perp2();
00198   double c1 = RPHIpositiononGluedendvector.y();
00199   double s1 =-RPHIpositiononGluedendvector.x();
00200   */
00201   double c1 = -m00;
00202   double s1 = -m01;
00203   double l1 = 1./(c1*c1+s1*s1);
00204 
00205  
00206   // FIXME: here for test...
00207   double sigmap12 = monoRH->sigmaPitch();
00208   if (sigmap12<0) {
00209     //AlgebraicSymMatrix tmpMatrix = monoRH->parametersError();
00210     /*
00211     std::cout << "DEBUG START" << std::endl;
00212     std::cout << "APE mono,stereo,glued : " 
00213               << stripdet->alignmentPositionError()->globalError().cxx()  << " , "
00214               << partnerstripdet->alignmentPositionError()->globalError().cxx()  << " , "
00215               << gluedDet->alignmentPositionError()->globalError().cxx()  << std::endl;
00216     */
00217     LocalError tmpError(monoRH->localPositionErrorFast());
00218     HelpertRecHit2DLocalPos::updateWithAPE(tmpError,*stripdet);
00219     MeasurementError errormonoRH=topol.measurementError(monoRH->localPositionFast(),tmpError);
00220     /*
00221     std::cout << "localPosError.xx(), helper.xx(), param.xx(): "
00222          << monoRH->localPositionError().xx() << " , "
00223          << monoRH->parametersError()[0][0] << " , "
00224          << tmpMatrix[0][0] << std::endl;
00225     */
00226     //MeasurementError errormonoRH=topol.measurementError(monoRH->localPosition(),monoRH->localPositionError());
00227     double pitch=topol.localPitch(monoRH->localPositionFast());
00228     monoRH->setSigmaPitch(sigmap12=errormonoRH.uu()*pitch*pitch);
00229   }
00230 
00231   SimpleHitIterator seconditer;  
00232 
00233   for(seconditer=begin;seconditer!=end;++seconditer){//iterate on stereo rechits
00234 
00235     // position of the initial and final point of the strip (STEREO cluster)
00236     double STEREOpointX=partnertopol.measurementPosition((*seconditer)->localPositionFast()).x();
00237     MeasurementPoint STEREOpointini(STEREOpointX,-0.5);
00238     MeasurementPoint STEREOpointend(STEREOpointX,0.5);
00239 
00240     // position of the initial and final point of the strip in local coordinates (stereo det)
00241     StripPosition stripstereo(partnertopol.localPosition(STEREOpointini),partnertopol.localPosition(STEREOpointend));
00242  
00243     //project stereo hit on glued det
00244     StripPosition projectedstripstereo=project(partnerstripdet,gluedDet,stripstereo,trackdirection);
00245 
00246  
00247     double m10=-(projectedstripstereo.second.y()-projectedstripstereo.first.y()); 
00248     double m11=(projectedstripstereo.second.x()-projectedstripstereo.first.x());
00249 
00250    //perform the matching
00251    //(x2-x1)(y-y1)=(y2-y1)(x-x1)
00252     AlgebraicMatrix22 m; AlgebraicVector2 c; // FIXME understand why moving this initializer out of the loop changes the output!
00253     m(0,0)=m00; 
00254     m(0,1)=m01;
00255     m(1,0)=m10;
00256     m(1,1)=m11;
00257     c(0)=c0;
00258     c(1)=m11*projectedstripstereo.first.y()+m10*projectedstripstereo.first.x();
00259     m.Invert(); 
00260     AlgebraicVector2 solution = m * c;
00261     LocalPoint position(solution(0),solution(1));
00262 
00263     /*
00264     {
00265       double m00 = -(projectedstripmono.second.y()-projectedstripmono.first.y()); 
00266       double m01 =  (projectedstripmono.second.x()-projectedstripmono.first.x());
00267       double m10 = -(projectedstripstereo.second.y()-projectedstripstereo.first.y()); 
00268       double m11 =  (projectedstripstereo.second.x()-projectedstripstereo.first.x());
00269       double c0  =  m01*projectedstripmono.first.y()   + m00*projectedstripmono.first.x();
00270       double c1  =  m11*projectedstripstereo.first.y() + m10*projectedstripstereo.first.x();
00271       
00272       double invDet = 1./(m00*m11-m10*m01);
00273     }
00274     */
00275 
00276     //
00277     // temporary fix by tommaso
00278     //
00279 
00280 
00281     LocalError tempError (100,0,100);
00282     if (!((gluedDet->surface()).bounds().inside(position,tempError,scale_))) continue;                                                       
00283 
00284     // then calculate the error
00285     /*
00286     LocalVector  stereopositiononGluedendvector=projectedstripstereo.second-projectedstripstereo.first;
00287     double l2 = 1./stereopositiononGluedendvector.perp2();
00288     double c2 = stereopositiononGluedendvector.y(); 
00289     double s2 =-stereopositiononGluedendvector.x();
00290     */
00291 
00292     double c2 = -m10;
00293     double s2 = -m11;
00294     double l2 = 1./(c2*c2+s2*s2);
00295 
00296 
00297     // FIXME: here for test...
00298     double sigmap22 = (*seconditer)->sigmaPitch();
00299     if (sigmap22<0) {
00300       //AlgebraicSymMatrix tmpMatrix = (*seconditer)->parametersError();
00301       LocalError tmpError((*seconditer)->localPositionErrorFast());
00302       HelpertRecHit2DLocalPos::updateWithAPE(tmpError, *partnerstripdet);
00303       MeasurementError errorstereoRH=partnertopol.measurementError((*seconditer)->localPositionFast(),tmpError);
00304       //MeasurementError errorstereoRH=partnertopol.measurementError((*seconditer)->localPosition(),(*seconditer)->localPositionError());
00305       double pitch=partnertopol.localPitch((*seconditer)->localPositionFast());
00306       (*seconditer)->setSigmaPitch(sigmap22=errorstereoRH.uu()*pitch*pitch);
00307     }
00308 
00309     double diff=(c1*s2-c2*s1);
00310     double invdet2=1/(diff*diff*l1*l2);
00311     float xx= invdet2*(sigmap12*s2*s2*l2+sigmap22*s1*s1*l1);
00312     float xy=-invdet2*(sigmap12*c2*s2*l2+sigmap22*c1*s1*l1);
00313     float yy= invdet2*(sigmap12*c2*c2*l2+sigmap22*c1*c1*l1);
00314     LocalError error(xx,xy,yy);
00315 
00316     if((gluedDet->surface()).bounds().inside(position,error,scale_)){ //if it is inside the gluedet bonds
00317       //Change NSigmaInside in the configuration file to accept more hits
00318       //...and add it to the Rechit collection 
00319 
00320       const SiStripRecHit2D* secondHit = *seconditer;
00321       collector(SiStripMatchedRecHit2D(position, error,gluedDet->geographicalId() ,
00322                                        monoRH,secondHit));
00323     }
00324   }
00325 }
00326 
00327 
00328 SiStripRecHitMatcher::StripPosition SiStripRecHitMatcher::project(const GeomDetUnit *det,const GluedGeomDet* glueddet,StripPosition strip,LocalVector trackdirection)const
00329 {
00330 
00331   GlobalPoint globalpointini=(det->surface()).toGlobal(strip.first);
00332   GlobalPoint globalpointend=(det->surface()).toGlobal(strip.second);
00333 
00334   // position of the initial and final point of the strip in glued local coordinates
00335   LocalPoint positiononGluedini=(glueddet->surface()).toLocal(globalpointini);
00336   LocalPoint positiononGluedend=(glueddet->surface()).toLocal(globalpointend);
00337 
00338   //correct the position with the track direction
00339 
00340   float scale=-positiononGluedini.z()/trackdirection.z();
00341 
00342   LocalPoint projpositiononGluedini= positiononGluedini + scale*trackdirection;
00343   LocalPoint projpositiononGluedend= positiononGluedend + scale*trackdirection;
00344 
00345   return StripPosition(projpositiononGluedini,projpositiononGluedend);
00346 }