CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/TrackingTools/PatternTools/src/TwoTrackMinimumDistance.cc

Go to the documentation of this file.
00001 #include "TrackingTools/PatternTools/interface/TwoTrackMinimumDistance.h"
00002 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00003 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h" 
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include "FWCore/Utilities/interface/Exception.h"
00006 #include "MagneticField/Engine/interface/MagneticField.h"
00007 
00008 using namespace std;
00009 
00010 namespace {
00011   inline GlobalPoint mean ( pair<GlobalPoint, GlobalPoint> pr ) {
00012     return GlobalPoint ( 0.5*(pr.first.basicVector() + pr.second.basicVector()) );
00013   }
00014 
00015   inline double dist ( pair<GlobalPoint, GlobalPoint> pr ) {
00016     return ( pr.first - pr.second ).mag();
00017   }
00018 }
00019 
00020 double TwoTrackMinimumDistance::firstAngle() const
00021 {
00022   if (!status_)
00023     throw cms::Exception("TrackingTools/PatternTools","TwoTrackMinimumDistance::could not compute track crossing. Check status before calling this method!");
00024   switch ( theCharge ) {
00025    case (hh): return theTTMDhh.firstAngle(); break;
00026    case (hl): return theTTMDhl.firstAngle(); break;
00027    case (ll): return theTTMDll.firstAngle(); break;
00028   }
00029   return 0;
00030 }
00031 
00032 double TwoTrackMinimumDistance::secondAngle() const
00033 {
00034   if (!status_)
00035     throw cms::Exception("TrackingTools/PatternTools","TwoTrackMinimumDistance::could not compute track crossing. Check status before calling this method!");
00036   switch ( theCharge ) {
00037    case (hh): return theTTMDhh.secondAngle(); break;
00038    case (hl): return theTTMDhl.secondAngle(); break;
00039    case (ll): return theTTMDll.secondAngle(); break;
00040   }
00041   return 0;
00042 }
00043 
00044 
00045 pair <double, double> TwoTrackMinimumDistance::pathLength() const
00046 {
00047   if (!status_)
00048     throw cms::Exception("TrackingTools/PatternTools","TwoTrackMinimumDistance::could not compute track crossing. Check status before calling this method!");
00049   switch ( theCharge ) {
00050    case (hh): return theTTMDhh.pathLength(); break;
00051    case (hl): return theTTMDhl.pathLength(); break;
00052    case (ll): return theTTMDll.pathLength(); break;
00053   }
00054   return std::pair<double,double>(0,0);
00055 }
00056 
00057 pair<GlobalPoint, GlobalPoint> TwoTrackMinimumDistance::points() const
00058 {
00059   if (!status_)
00060     throw cms::Exception("TrackingTools/PatternTools","TwoTrackMinimumDistance::could not compute track crossing. Check status before calling this method!");
00061   return points_;
00062 }
00063 
00064 bool
00065 TwoTrackMinimumDistance::calculate(const TrajectoryStateOnSurface & sta, 
00066                                 const TrajectoryStateOnSurface & stb) 
00067 {
00068   return calculate ( sta.globalParameters(), stb.globalParameters() );
00069 }
00070 
00071 
00072 bool
00073 TwoTrackMinimumDistance::calculate(const FreeTrajectoryState & sta, 
00074                                 const FreeTrajectoryState & stb) 
00075 {
00076 //  pair<GlobalPoint, GlobalPoint> ret  = theIniAlgo.points ( sta, stb );
00077   return calculate ( sta.parameters(), stb.parameters() );
00078 }
00079 
00080 bool
00081 TwoTrackMinimumDistance::calculate(const GlobalTrajectoryParameters & sta,
00082                                 const GlobalTrajectoryParameters & stb)
00083 {
00084   if ((sta.magneticField().inTesla(sta.position()).z() == 0.)|| 
00085         (stb.magneticField().inTesla(stb.position()).z() == 0.)) {
00086     status_ = pointsLineLine(sta, stb);
00087   } else if ( sta.charge() != 0. && stb.charge() != 0. ) {
00088     status_ = pointsHelixHelix(sta, stb);
00089   } else if ( sta.charge() == 0. && stb.charge() == 0. ) {
00090     status_ = pointsLineLine(sta, stb);
00091   } else {
00092     status_ = pointsHelixLine(sta, stb);
00093   }
00094   return status_;
00095 }
00096 
00097 bool
00098 TwoTrackMinimumDistance::pointsLineLine(const GlobalTrajectoryParameters & sta,
00099                                 const GlobalTrajectoryParameters & stb)
00100 {
00101   theCharge = ll;
00102   if (theTTMDll.calculate(sta, stb)) return false;
00103   points_ = theTTMDll.points();
00104   return true;
00105 }
00106 
00107 bool
00108 TwoTrackMinimumDistance::pointsHelixLine(const GlobalTrajectoryParameters & sta,
00109                                 const GlobalTrajectoryParameters & stb)
00110 {
00111   theCharge = hl;
00112   if (theTTMDhl.calculate(sta, stb, 0.000001)) return false;
00113   points_ = theTTMDhl.points();
00114   return true;
00115 }
00116 
00117 bool
00118 TwoTrackMinimumDistance::pointsHelixHelix(const GlobalTrajectoryParameters & sta,
00119                                 const GlobalTrajectoryParameters & stb)
00120 {
00121   if ( ( sta.position() - stb.position() ).mag2() < 1e-7f &&
00122        ( sta.momentum() - stb.momentum() ).mag2() < 1e-7f &&
00123        sta.charge()==stb.charge()
00124        )
00125   {
00126     edm::LogWarning ( "TwoTrackMinimumDistance") << "comparing track with itself!";
00127   };
00128 
00129   theCharge = hh;
00130   if ( theModus == FastMode )
00131   {
00132     // first we try directly - in FastMode only ...
00133     if ( !(theTTMDhh.calculate ( sta, stb, .0001 )) )
00134     {
00135       points_ = theTTMDhh.points();
00136       return true;
00137     };
00138   };
00139 
00140   // okay. did not work. so we use CAIR, and then TTMD again.
00141   bool cairStat = theIniAlgo.calculate ( sta, stb );
00142   
00143   if (!cairStat) { // yes. CAIR may fail.
00144     edm::LogWarning ( "TwoTrackMinimumDistance" ) << "Computation HelixHelix::CAIR failed.";
00145     if ( theModus == SlowMode ) { // we can still try ttmd here.
00146       if ( !(theTTMDhh.calculate ( sta, stb, .0001 )) ) {
00147         points_ = theTTMDhh.points();
00148         return true;
00149       }
00150     };
00151     // we can try with more sloppy settings
00152     if ( !(theTTMDhh.calculate ( sta, stb, .1 )) ) {
00153         points_ = theTTMDhh.points();
00154         return true;
00155       }
00156     return false;
00157     edm::LogWarning ( "TwoTrackMinimumDistance" ) << "TwoTrackMinimumDistanceHelixHelix failed";
00158   };
00159 
00160   pair<GlobalTrajectoryParameters, GlobalTrajectoryParameters >
00161         ini = theIniAlgo.trajectoryParameters();
00162 
00163   pair<GlobalPoint, GlobalPoint> inip ( ini.first.position(), 
00164       ini.second.position() );
00165   if ( theTTMDhh.calculate ( ini.first, ini.second, .0001 ) ) {
00166     points_ = inip;
00167   } else {
00168     points_ = theTTMDhh.points();
00169     // if we are still worse than CAIR, we use CAIR results.
00170     if ( dist ( points_ ) > dist ( inip ) ) points_ = inip;
00171   };
00172   return true;
00173 }
00174 
00175 
00176 GlobalPoint TwoTrackMinimumDistance::crossingPoint() const
00177 {
00178   return mean ( points_ );
00179 }
00180 
00181 
00182 float TwoTrackMinimumDistance::distance() const
00183 {
00184   return dist ( points_ );
00185 }