CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoLocalMuon/DTSegment/src/DTSegmentCand.cc

Go to the documentation of this file.
00001 
00009 /* This Class Header */
00010 #include "RecoLocalMuon/DTSegment/src/DTSegmentCand.h"
00011 
00012 #include "Geometry/DTGeometry/interface/DTSuperLayer.h"
00013 #include "DataFormats/DTRecHit/interface/DTSLRecSegment2D.h"
00014 #include "DataFormats/DTRecHit/interface/DTChamberRecSegment2D.h"
00015 
00016 /* Collaborating Class Header */
00017 
00018 /* C++ Headers */
00019 
00020 /* ====================================================================== */
00021 double DTSegmentCand::chi2max=20.; // to be tuned!!
00022 unsigned int DTSegmentCand::nHitsMin=3; // to be tuned!!
00023 
00025 DTSegmentCand::DTSegmentCand(AssPointCont& hits,
00026                              const DTSuperLayer* sl) :
00027 theSL(sl), theChi2(-1.) , theHits(hits){
00028 }
00029 
00030 DTSegmentCand::DTSegmentCand(AssPointCont hits,
00031                              LocalPoint& position,
00032                              LocalVector& direction,
00033                              double chi2,
00034                              AlgebraicSymMatrix covMat,
00035                              const DTSuperLayer* sl) :
00036 theSL(sl), thePosition(position), theDirection(direction), theChi2(chi2),
00037   theCovMatrix( covMat), theHits(hits) {
00038 }
00039 
00041 DTSegmentCand::~DTSegmentCand() {
00042 }
00043 
00044 /* Operations */ 
00045 bool DTSegmentCand::operator==(const DTSegmentCand& seg){
00046   static const double epsilon=0.00001;
00047   if (nHits()!=seg.nHits()) return false;
00048   if (fabs(chi2()-seg.chi2())>epsilon) return false;
00049   if (fabs(position().x()-seg.position().x())>epsilon ||
00050       fabs(position().y()-seg.position().y())>epsilon ||
00051       fabs(position().z()-seg.position().z())>epsilon) return false;
00052   if (fabs(direction().x()-seg.direction().x())>epsilon ||
00053       fabs(direction().y()-seg.direction().y())>epsilon ||
00054       fabs(direction().z()-seg.direction().z())>epsilon) return false;
00055   return true;
00056 }
00057 
00058 bool DTSegmentCand::operator<(const DTSegmentCand& seg){
00059   if (nHits()==seg.nHits()) return (chi2()>seg.chi2());
00060   return (nHits()<seg.nHits());
00061 }
00062 
00063 void DTSegmentCand::add(DTHitPairForFit* hit, DTEnums::DTCellSide code) {
00064   theHits.insert(AssPoint(hit,code));
00065 }
00066 
00067 void DTSegmentCand::removeHit(AssPoint badHit) {
00068   theHits.erase(badHit);
00069 }
00070 
00071 int DTSegmentCand::nSharedHitPairs(const DTSegmentCand& seg) const{
00072   int result=0;
00073   AssPointCont hitsCont = seg.hits();
00074 
00075   for (AssPointCont::const_iterator hit=theHits.begin(); 
00076        hit!=theHits.end() ; ++hit) {
00077     for (AssPointCont::const_iterator hit2=hitsCont.begin();
00078          hit2!=hitsCont.end() ; ++hit2) {
00079       //  if(result) return result ; // TODO, uncomm this line or move it in another func
00080       if ((*(*hit).first)==(*(*hit2).first)) {
00081         ++result;
00082         continue;
00083       }
00084     }
00085   }
00086   return result;
00087 }
00088 
00089 DTSegmentCand::AssPointCont
00090 DTSegmentCand::conflictingHitPairs(const DTSegmentCand& seg) const{
00091   AssPointCont result;
00092   const AssPointCont & hits2 = seg.theHits;
00093   
00094 //  if (nSharedHitPairs(seg)==0) return result;
00095 
00096   AssPointCont::const_iterator hitBegin2 = hits2.begin(),   hitEnd2 = hits2.end();
00097   for (AssPointCont::const_iterator hit = theHits.begin(), hitEnd  = theHits.end(); 
00098        hit != hitEnd ; ++hit) {
00099     for (AssPointCont::const_iterator hit2 = hitBegin2; hit2 != hitEnd2; ++hit2) { 
00100         if ((*(*hit).first)==(*(*hit2).first) &&
00101                 (*hit).second != (*hit2).second) {
00102             result.insert(*hit);
00103             continue;
00104         }
00105     }
00106   }
00107   return result;
00108 }
00109 
00110 bool DTSegmentCand::good() const
00111 {
00112   if(NDOF() == 0) return false;
00113   if(chi2()/NDOF() > chi2max || nHits() < nHitsMin) return false;
00114 
00115   if(nHits() == nHitsMin && hitsShareLayer()) return false ;
00116 
00117   return true;
00118 }
00119 
00120 bool DTSegmentCand::hitsShareLayer() const
00121 {
00122   std::vector<int> layerN;
00123 
00124   for(DTSegmentCand::AssPointCont::iterator assHit=theHits.begin();
00125       assHit!=theHits.end(); ++assHit) {
00126     layerN.push_back((*assHit).first->id().layerId().layer());
00127 
00128     //std::cout << (*assHit).first->id().layerId().layer() << std::endl;
00129   }
00130 
00131   for(int i=0;i<(int)layerN.size();i++){
00132     for(int j=i+1;j<(int)layerN.size();j++){
00133       if(layerN[i] == layerN[j]) return true;
00134     }
00135   }
00136 
00137   return false;
00138 }
00139 
00140 int DTSegmentCand::nLayers() const {
00141   // TODO
00142   return 0;
00143 }
00144 
00145 DTSegmentCand::operator DTSLRecSegment2D*() const{
00146   
00147   LocalPoint seg2Dposition = position();
00148   LocalVector seg2DDirection = direction();
00149   double seg2DChi2 = chi2();
00150   AlgebraicSymMatrix seg2DCovMatrix = covMatrix();
00151   
00152   std::vector<DTRecHit1D> hits1D;
00153   for(DTSegmentCand::AssPointCont::iterator assHit=theHits.begin();
00154       assHit!=theHits.end(); ++assHit) {
00155 
00156     GlobalPoint hitGlobalPos =
00157       theSL->toGlobal( (*assHit).first->localPosition((*assHit).second) );
00158 
00159     LocalPoint hitPosInLayer = 
00160       theSL->layer( (*assHit).first->id().layerId() )->toLocal(hitGlobalPos);
00161 
00162     DTRecHit1D hit( ((*assHit).first)->id(),
00163                     (*assHit).second,
00164                     ((*assHit).first)->digiTime(),
00165                     hitPosInLayer,
00166                     ((*assHit).first)->localPositionError() );
00167     hits1D.push_back(hit);
00168   }
00169   
00170   return new DTSLRecSegment2D(theSL->id(),
00171                             seg2Dposition,seg2DDirection,seg2DCovMatrix,
00172                             seg2DChi2,hits1D);
00173 }
00174 
00175 DTSegmentCand::operator DTChamberRecSegment2D*() const{
00176   
00177   // input position and direction are in sl frame, while must be stored in
00178   // chamber one: so I have to extrapolate the position (along the direction) to
00179   // the chamber reference plane.
00180 
00181   LocalPoint posInCh = theSL->chamber()->toLocal(theSL->toGlobal( position() ));
00182   LocalVector dirInCh= theSL->chamber()->toLocal(theSL->toGlobal( direction() ));
00183   
00184   LocalPoint pos=posInCh + dirInCh * posInCh.z()/cos(dirInCh.theta());
00185   
00186   double seg2DChi2 = chi2();
00187   AlgebraicSymMatrix seg2DCovMatrix = covMatrix();
00188   
00189   std::vector<DTRecHit1D> hits1D;
00190   for(DTSegmentCand::AssPointCont::iterator assHit=theHits.begin();
00191       assHit!=theHits.end(); ++assHit) {
00192 
00193     GlobalPoint hitGlobalPos =
00194       theSL->toGlobal( (*assHit).first->localPosition((*assHit).second) );
00195     
00196     LocalPoint hitPosInLayer = 
00197       theSL->chamber()
00198       ->superLayer((*assHit).first->id().superlayerId())
00199       ->layer( (*assHit).first->id().layerId() )->toLocal(hitGlobalPos);
00200     
00201     DTRecHit1D hit( ((*assHit).first)->id(),
00202                     (*assHit).second,
00203                     ((*assHit).first)->digiTime(),
00204                     hitPosInLayer,
00205                     ((*assHit).first)->localPositionError() );
00206     hits1D.push_back(hit);
00207   }
00208   
00209   return new DTChamberRecSegment2D(theSL->chamber()->id(),
00210                                    pos,dirInCh,seg2DCovMatrix,
00211                                    seg2DChi2,hits1D);
00212   
00213   // chamber and Phi SLs' frame are oriented in the same way, only a transaltion,
00214   // so the covariance matrix is the same!
00215 }
00216 
00217 
00218 bool DTSegmentCand::AssPointLessZ::operator()(const AssPoint& pt1, 
00219                                               const AssPoint& pt2) const {
00220   return *(pt1.first) < *(pt2.first);
00221 }
00222 
00223 std::ostream& operator<<(std::ostream& out, const DTSegmentCand& seg) {
00224   out <<  " pos: " << seg.position() << " dir: " << seg.direction() 
00225       << " chi2/nHits: " << seg.chi2() << "/" << seg.DTSegmentCand::nHits() << "/" << seg.nHits();
00226   return out;
00227 }
00228 
00229 std::ostream& operator<<(std::ostream& out, const DTSegmentCand::AssPoint& hit) {
00230   // out << "Hits " << (hit.first)->localPosition(DTEnums::Left) <<
00231   //     " " << hit.second  << " Lay " << (hit.first)->layerNumber() << endl;
00232   return out;
00233 }