CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/RecoTracker/TkNavigation/src/SimpleNavigableLayer.cc

Go to the documentation of this file.
00001 #include "RecoTracker/TkNavigation/interface/SimpleNavigableLayer.h"
00002 
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 
00005 #include "DataFormats/GeometrySurface/interface/BoundCylinder.h"
00006 #include "DataFormats/GeometrySurface/interface/BoundDisk.h"
00007 #include "TrackingTools/PatternTools/interface/TransverseImpactPointExtrapolator.h"
00008 #include <set>
00009 
00010 using namespace std;
00011 
00012 TrajectoryStateOnSurface SimpleNavigableLayer::crossingState(const FreeTrajectoryState& fts,
00013                                                              PropagationDirection dir) const{
00014   //self propagating. step one: go close to the center
00015   GlobalPoint initialPoint = fts.position();
00016   TransverseImpactPointExtrapolator middle;
00017   GlobalPoint center(0,0,0);
00018   TSOS propState = middle.extrapolate(fts, center, propagator(dir));
00019   if ( !propState.isValid()) return TrajectoryStateOnSurface();
00020   
00021   FreeTrajectoryState const & dest = *propState.freeState();
00022   GlobalPoint middlePoint = dest.position();
00023   const float toCloseToEachOther2 = 1e-4*1e-4;
00024   if unlikely( (middlePoint-initialPoint).mag2() < toCloseToEachOther2){
00025     LogDebug("SimpleNavigableLayer")<<"initial state and PCA are identical. Things are bound to fail. Do not add the link.";
00026     return TrajectoryStateOnSurface();
00027   }
00028   
00029  
00030   /*
00031   std::string dirS;
00032   if (dir==alongMomentum) dirS = "alongMomentum";
00033   else if (dir==oppositeToMomentum) dirS = "oppositeToMomentum";
00034   else dirS = "anyDirection";
00035   */
00036 
00037   LogDebug("SimpleNavigableLayer")<<"self propagating("<< dir <<") from:\n"
00038                                   <<fts<<"\n"
00039                                   <<dest<<"\n"
00040                                   <<" and the direction is: "<<dir;
00041   
00042   //second propagation to go on the other side of the barrel
00043   //propState = propagator(dir).propagate( dest, detLayer()->specificSurface());
00044   propState = propagator(dir).propagate( dest, detLayer()->surface());
00045   if ( !propState.isValid()) return TrajectoryStateOnSurface();
00046   
00047   FreeTrajectoryState & dest2 = *propState.freeState();
00048   GlobalPoint finalPoint = dest2.position();
00049   LogDebug("SimpleNavigableLayer")<<"second propagation("<< dir <<") to: \n"
00050                                   <<dest2;
00051   double finalDot = (middlePoint - initialPoint).basicVector().dot((finalPoint-middlePoint).basicVector());
00052   if unlikely(finalDot<0){ // check that before and after are in different side.
00053     LogDebug("SimpleNavigableLayer")<<"switch side back: ABORT.";
00054     return TrajectoryStateOnSurface();
00055   }
00056   return propState;
00057 }
00058 
00059 bool SimpleNavigableLayer::wellInside( const FreeTrajectoryState& fts,
00060                                        PropagationDirection dir,
00061                                        const BarrelDetLayer* bl,
00062                                        DLC& result) const
00063 {
00064 
00065   TSOS propState = (bl==detLayer()) ?
00066     crossingState(fts,dir)
00067     :
00068     propagator(dir).propagate( fts, bl->specificSurface());
00069 
00070   if ( !propState.isValid()) return false;
00071  
00072   //if requested check that the layer is crossed on the right side
00073   if (theCheckCrossingSide){
00074     bool backTobackTransverse = (fts.position().x()*propState.globalPosition().x() + fts.position().y()*propState.globalPosition().y())<0;
00075     bool backToback = propState.globalPosition().basicVector().dot(fts.position().basicVector())<0;
00076 
00077     if (backTobackTransverse || backToback ){
00078       LogTrace("TkNavigation") << "Crossing over prevented!\nStaring from (x,y,z,r) (" 
00079                                << fts.position().x()<<","<< fts.position().y()<<","<< fts.position().z()<<","<<fts.position().perp()
00080                                << ") going to TSOS (x,y,z,r)" 
00081                                << propState.globalPosition().x()<<","<< propState.globalPosition().y()
00082                                <<","<< propState.globalPosition().z()<<","<<propState.globalPosition().perp()<<")";
00083       return false;
00084     
00085     /*
00086     //we have to check the crossing side only if we are going to something smaller
00087     if (fts.position().perp()>bl->specificSurface().radius() || 
00088     fabs(fts.position().z())>bl->surface().bounds().length()/2. ){
00089     if (propState.globalPosition().basicVector().dot(fts.position().basicVector())<0){
00090     LogTrace("TkNavigation") << "Crossing over prevented!\nStaring from (x,y,z,r) (" 
00091     << fts.position().x()<<","<< fts.position().y()<<","<< fts.position().z()<<","<<fts.position().perp()
00092     << ") going to TSOS (x,y,z,r)" 
00093     << propState.globalPosition().x()<<","<< propState.globalPosition().y()<<","<< propState.globalPosition().z()<<","<<propState.globalPosition().perp()<<")";; 
00094     return false;
00095     }
00096     }   
00097     */
00098     }}
00099 
00100   const Bounds& bounds( bl->specificSurface().bounds());
00101   float length = bounds.length()*0.5f;
00102 
00103   // take into account the thickness of the layer
00104   float deltaZ = 0.5f*bounds.thickness() *
00105     std::abs(propState.globalDirection().z())/propState.globalDirection().perp();
00106 
00107 
00108   // take into account the error on the predicted state
00109   const float nSigma = theEpsilon;  // temporary reuse of epsilon
00110   if (propState.hasError()) {
00111     deltaZ += nSigma * sqrt( fts.cartesianError().position().czz());
00112   }
00113 
00114   // cout << "SimpleNavigableLayer BarrelDetLayer deltaZ = " << deltaZ << endl;
00115 
00116   float zpos = propState.globalPosition().z();
00117   if ( std::abs( zpos) < length + deltaZ) result.push_back( bl);
00118 
00119   return std::abs( zpos) < length - deltaZ;
00120 }
00121 
00122 bool SimpleNavigableLayer::wellInside( const FreeTrajectoryState& fts,
00123                                        PropagationDirection dir,
00124                                        const ForwardDetLayer* fl,
00125                                        DLC& result) const
00126 {
00127   TSOS propState = propagator(dir).propagate( fts, fl->specificSurface());
00128   if ( !propState.isValid()) return false;
00129 
00130   if (fl==detLayer()){
00131     LogDebug("SimpleNavigableLayer")<<"self propagating from:\n"
00132                                     <<fts<<"\n to \n"
00133                                     <<*propState.freeState();
00134   }
00135 
00136   //if requested avoids crossing over the tracker 
00137   if (theCheckCrossingSide){
00138     bool backTobackTransverse = (fts.position().x()*propState.globalPosition().x() + fts.position().y()*propState.globalPosition().y())<0;
00139     bool backToback = propState.globalPosition().basicVector().dot(fts.position().basicVector())<0;
00140 
00141     if (backTobackTransverse || backToback ){
00142       LogTrace("TkNavigation") << "Crossing over prevented!\nStaring from (x,y,z,r) (" 
00143                                << fts.position().x()<<","<< fts.position().y()<<","<< fts.position().z()<<","<<fts.position().perp()
00144                                << ") going to TSOS (x,y,z,r)" 
00145                                << propState.globalPosition().x()<<","<< propState.globalPosition().y()
00146                                <<","<< propState.globalPosition().z()<<","<<propState.globalPosition().perp()<<")";; 
00147       return false;
00148     
00149   //    if (fts.position().z()*propState.globalPosition().z() < 0) return false;
00150     }}
00151 
00152 
00153   float rpos = propState.globalPosition().perp();
00154   float innerR = fl->specificSurface().innerRadius();
00155   float outerR = fl->specificSurface().outerRadius();
00156  
00157   // take into account the thickness of the layer
00158   float deltaR = 0.5f*fl->surface().bounds().thickness() *
00159     propState.localDirection().perp()/std::abs(propState.localDirection().z());
00160 
00161   // take into account the error on the predicted state
00162   const float nSigma = theEpsilon;
00163   if (propState.hasError()) {
00164     LocalError err = propState.localError().positionError();
00165     // ignore correlation for the moment...
00166     deltaR += nSigma * sqrt(err.xx() + err.yy());
00167   }
00168 
00169   // cout << "SimpleNavigableLayer BarrelDetLayer deltaR = " << deltaR << endl;
00170 
00171   if ( innerR-deltaR < rpos && rpos < outerR+deltaR) result.push_back( fl);
00172   return ( innerR+deltaR < rpos && rpos < outerR-deltaR);
00173 }
00174 
00175 bool SimpleNavigableLayer::wellInside( const FreeTrajectoryState& fts,
00176                                        PropagationDirection dir,
00177                                        const DLC& layers,
00178                                        DLC& result) const
00179 {
00180   for (auto l : layers) {
00181     if (l->isBarrel()) {
00182         const BarrelDetLayer * bl = reinterpret_cast<const BarrelDetLayer *>(l);
00183         if (wellInside( fts, dir, bl, result)) return true;
00184       }
00185     else {
00186       const ForwardDetLayer* fl = reinterpret_cast<const ForwardDetLayer*>(l);
00187       if (wellInside( fts, dir, fl, result)) return true;
00188     }
00189   }
00190   return false;
00191 }
00192 
00193 bool SimpleNavigableLayer::wellInside( const FreeTrajectoryState& fts,
00194                                        PropagationDirection dir,
00195                                        ConstBDLI begin, ConstBDLI end,
00196                                        DLC& result) const
00197 {
00198   for ( ConstBDLI i = begin; i < end; i++) {
00199     if (wellInside( fts, dir, *i, result)) return true;
00200   }
00201   return false;
00202 }
00203 
00204 bool SimpleNavigableLayer::wellInside( const FreeTrajectoryState& fts,
00205                                        PropagationDirection dir,
00206                                        ConstFDLI begin, ConstFDLI end,
00207                                        DLC& result) const
00208 {
00209   for ( ConstFDLI i = begin; i < end; i++) {
00210     if (wellInside( fts, dir, *i, result)) return true;
00211   }
00212   return false;
00213 }
00214 
00215 
00216 std::vector< const DetLayer * > SimpleNavigableLayer::compatibleLayers (const FreeTrajectoryState &fts, 
00217                                                                         PropagationDirection timeDirection,
00218                                                                         int& counter) const {
00219   typedef std::vector<const DetLayer*> Lvect;
00220   typedef std::set<const DetLayer *> Lset;  
00221 
00222   //initiate the first iteration
00223   Lvect && someLayers = nextLayers(fts,timeDirection);
00224   if (someLayers.empty()) {
00225     LogDebug("SimpleNavigableLayer")  <<"Number of compatible layers: "<< 0;
00226     return someLayers;
00227   }
00228 
00229 
00230   Lset collect; //a container of unique instances. to avoid duplicates
00231   Lset layerToTry, nextLayerToTry;//set used for iterations
00232   layerToTry.insert(someLayers.begin(),someLayers.end());
00233   
00234   while (!layerToTry.empty() && (counter++)<=150){
00235     LogDebug("SimpleNavigableLayer")
00236       <<counter<<"] going to check on : "<<layerToTry.size()<<" next layers.";
00237     //clear this set first, it will be swaped with layerToTry
00238     nextLayerToTry.clear();
00239     for (auto toTry : layerToTry){
00240       //add the layer you tried.
00241       LogDebug("SimpleNavigableLayer")
00242         <<counter<<"] adding layer with pointer: "<<(toTry)
00243         <<" first detid: "<< (toTry)->basicComponents().front()->geographicalId();
00244       if (!collect.insert(toTry).second) continue;
00245       
00246       //find the next layers from it
00247       Lvect && nextLayers = (toTry)->nextLayers(fts,timeDirection);
00248       LogDebug("SimpleNavigableLayer")
00249         <<counter<<"] this layer has : "<<nextLayers.size()<<" next layers.";
00250       nextLayerToTry.insert(nextLayers.begin(),nextLayers.end());
00251     } // layerToTry
00252     //swap now that you where to go next.
00253     layerToTry.swap(nextLayerToTry);
00254   }
00255   if(counter>=150) {
00256     edm::LogWarning("SimpleNavigableLayer") << "WARNING: compatibleLayers() more than 150 iterations!!! Bailing out..";
00257     counter = -1;
00258     return Lvect();
00259   }
00260 
00261   LogDebug("SimpleNavigableLayer")
00262    <<"Number of compatible layers: "<< collect.size();
00263   
00264   return Lvect(collect.begin(),collect.end());
00265 
00266 }