CMS 3D CMS Logo

CosmicMuonSmoother.cc

Go to the documentation of this file.
00001 
00015 #include "RecoMuon/CosmicMuonProducer/interface/CosmicMuonSmoother.h"
00016 
00017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00018 #include "TrackingTools/KalmanUpdators/interface/KFUpdator.h"
00019 #include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
00020 #include "TrackingTools/TrackFitters/interface/TrajectoryStateWithArbitraryError.h"
00021 #include "TrackingTools/TrackFitters/interface/TrajectoryStateCombiner.h"
00022 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHit.h"
00023 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00024 #include "DataFormats/GeometrySurface/interface/PlaneBuilder.h"
00025 
00026 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00027 
00028 using namespace edm;
00029 using namespace std;
00030 
00031 //
00032 // constructor
00033 //
00034 CosmicMuonSmoother::CosmicMuonSmoother(const ParameterSet& par, const MuonServiceProxy *service) : theService(service) {
00035 
00036   theUpdator     = new KFUpdator;
00037   theUtilities   = new CosmicMuonUtilities; 
00038   theEstimator   = new Chi2MeasurementEstimator(200.0);
00039   thePropagatorAlongName = par.getParameter<string>("PropagatorAlong");
00040   thePropagatorOppositeName = par.getParameter<string>("PropagatorOpposite");
00041   theErrorRescaling = par.getParameter<double>("RescalingFactor");
00042 
00043   category_ = "Muon|RecoMuon|CosmicMuon|CosmicMuonSmoother";
00044 }
00045 
00046 //
00047 // destructor
00048 //
00049 CosmicMuonSmoother::~CosmicMuonSmoother() {
00050 
00051   if ( theUpdator ) delete theUpdator;
00052   if ( theUtilities ) delete theUtilities;
00053   if ( theEstimator ) delete theEstimator;
00054 
00055 }
00056 
00057 
00058 //
00059 // fit and smooth trajectory
00060 //
00061 vector<Trajectory> CosmicMuonSmoother::trajectories(const Trajectory& t) const {
00062    vector<Trajectory> fitted = fit(t);
00063    return smooth(fitted);
00064 
00065 }
00066 
00067 //
00068 // fit and smooth trajectory 
00069 //
00070 vector<Trajectory> CosmicMuonSmoother::trajectories(const TrajectorySeed& seed,
00071                                                    const ConstRecHitContainer& hits, 
00072                                                    const TrajectoryStateOnSurface& firstPredTsos) const {
00073 
00074   if ( hits.empty() ||!firstPredTsos.isValid() ) return vector<Trajectory>();
00075 
00076   LogTrace(category_)<< "trajectory begin (seed hits tsos)";
00077 
00078   TrajectoryStateOnSurface firstTsos = firstPredTsos;
00079   firstTsos.rescaleError(theErrorRescaling);
00080 
00081   LogTrace(category_)<< "first TSOS: "<<firstTsos;
00082 
00083   vector<Trajectory> fitted = fit(seed, hits, firstTsos);
00084   LogTrace(category_)<< "fitted: "<<fitted.size();
00085   vector<Trajectory> smoothed = smooth(fitted);
00086   LogTrace(category_)<< "smoothed: "<<smoothed.size();
00087 
00088   return smoothed;
00089 
00090 }
00091 
00092 //
00093 // fit trajectory
00094 //
00095 vector<Trajectory> CosmicMuonSmoother::fit(const Trajectory& t) const {
00096 
00097   if ( t.empty() ) return vector<Trajectory>();
00098 
00099   LogTrace(category_)<< "fit begin (trajectory) ";
00100 
00101   TrajectoryStateOnSurface firstTsos = initialState(t); 
00102   if ( !firstTsos.isValid() ) {
00103     LogTrace(category_)<< "Error: firstTsos invalid. ";
00104     return vector<Trajectory>();
00105   }
00106 
00107   LogTrace(category_)<< "firstTsos: "<<firstTsos;
00108 
00109   ConstRecHitContainer hits = t.recHits();
00110   LogTrace(category_)<< "hits: "<<hits.size();
00111   LogTrace(category_)<<"hit front" <<hits.front()->globalPosition()<< " hit back" 
00112     <<hits.back()->globalPosition();
00113 
00114   sortHitsAlongMom(hits, firstTsos);
00115 
00116   LogTrace(category_)<<"after sorting hit front" <<hits.front()->globalPosition()<< " hit back" 
00117     <<hits.back()->globalPosition();
00118 
00119   return fit(t.seed(), hits, firstTsos);
00120 
00121 }
00122 
00123 
00124 //
00125 // fit trajectory
00126 //
00127 vector<Trajectory> CosmicMuonSmoother::fit(const TrajectorySeed& seed,
00128                                           const ConstRecHitContainer& hits, 
00129                                           const TrajectoryStateOnSurface& firstPredTsos) const {
00130 
00131   LogTrace(category_)<< "fit begin (seed, hit, tsos).";
00132 
00133   if ( hits.empty() ) {
00134     LogTrace(category_)<< "Error: empty hits container.";
00135     return vector<Trajectory>();
00136   }
00137 
00138   Trajectory myTraj(seed, alongMomentum);
00139 
00140   TrajectoryStateOnSurface predTsos(firstPredTsos);
00141   LogTrace(category_)<< "first pred TSOS: "<<predTsos;
00142 
00143   if ( !predTsos.isValid() ) {
00144     LogTrace(category_)<< "Error: firstTsos invalid.";
00145     return vector<Trajectory>();
00146   }
00147   TrajectoryStateOnSurface currTsos;
00148 
00149   if ( hits.front()->isValid() ) {
00150 
00151     TransientTrackingRecHit::RecHitPointer preciseHit = hits.front()->clone(predTsos);
00152     LogTrace(category_)<<"first hit is at det "<< hits.front()->det()->surface().position();
00153 
00154     currTsos = theUpdator->update(predTsos, *preciseHit);
00155     myTraj.push(TrajectoryMeasurement(predTsos, currTsos, hits.front(),
00156                 theEstimator->estimate(predTsos, *hits.front()).second));
00157     if ( currTsos.isValid() )  LogTrace(category_)<< "first curr TSOS: "<<currTsos;
00158 
00159 
00160   } else {
00161 
00162     currTsos = predTsos;
00163     myTraj.push(TrajectoryMeasurement(predTsos, hits.front()));
00164   }
00165   //const TransientTrackingRecHit& firsthit = *hits.front();
00166 
00167   for ( ConstRecHitContainer::const_iterator ihit = hits.begin() + 1; 
00168         ihit != hits.end(); ++ihit ) {
00169 
00170     if ( !(**ihit).isValid() ) {
00171       LogTrace(category_)<< "Error: invalid hit.";
00172       continue;
00173     }
00174    if (currTsos.isValid())  {
00175      LogTrace(category_)<<"current pos "<<currTsos.globalPosition()
00176                        <<"mom "<<currTsos.globalMomentum();
00177     } else {
00178       LogTrace(category_)<<"current state invalid";
00179     }
00180 
00181     predTsos = propagatorAlong()->propagate(currTsos, (**ihit).det()->surface());
00182     LogTrace(category_)<<"predicted state propagate directly "<<predTsos.isValid();
00183 
00184     if ( !predTsos.isValid() ) {
00185       LogTrace(category_)<<"step-propagating from "<<currTsos.globalPosition() <<" to position: "<<(*ihit)->globalPosition();
00186       predTsos = theUtilities->stepPropagate(currTsos, (*ihit), *propagatorAlong());
00187     }
00188     if ( !predTsos.isValid() && (abs(theService->magneticField()->inTesla(GlobalPoint(0,0,0)).z()) < 0.01) && (theService->propagator("StraightLinePropagator").isValid() ) ) {
00189       LogTrace(category_)<<"straight-line propagating from "<<currTsos.globalPosition() <<" to position: "<<(*ihit)->globalPosition();
00190       predTsos = theService->propagator("StraightLinePropagator")->propagate(currTsos, (**ihit).det()->surface());
00191     }
00192     if (predTsos.isValid())  {
00193       LogTrace(category_)<<"predicted pos "<<predTsos.globalPosition()
00194                          <<"mom "<<predTsos.globalMomentum();
00195     } else {
00196       LogTrace(category_)<<"predicted state invalid";
00197     }
00198     if ( !predTsos.isValid() ) {
00199       LogTrace(category_)<< "Error: predTsos is still invalid forward fit.";
00200 //      return vector<Trajectory>();
00201       continue;
00202     } else if ( (**ihit).isValid() ) {
00203       // update
00204       TransientTrackingRecHit::RecHitPointer preciseHit = (**ihit).clone(predTsos);
00205 
00206       if ( !preciseHit->isValid() ) {
00207         currTsos = predTsos;
00208         myTraj.push(TrajectoryMeasurement(predTsos, *ihit));
00209       } else {
00210         currTsos = theUpdator->update(predTsos, *preciseHit);
00211         myTraj.push(TrajectoryMeasurement(predTsos, currTsos, preciseHit,
00212                        theEstimator->estimate(predTsos, *preciseHit).second));
00213       }
00214     } else {
00215       currTsos = predTsos;
00216       myTraj.push(TrajectoryMeasurement(predTsos, *ihit));
00217     }
00218 
00219   }
00220 
00221   std::vector<TrajectoryMeasurement> mytms = myTraj.measurements();
00222   LogTrace(category_)<<"fit result "<<mytms.size();
00223   for (std::vector<TrajectoryMeasurement>::const_iterator itm = mytms.begin();
00224        itm != mytms.end(); ++itm ) {
00225        LogTrace(category_)<<"updated pos "<<itm->updatedState().globalPosition()
00226                        <<"mom "<<itm->updatedState().globalMomentum();
00227        }
00228 
00229 
00230   return vector<Trajectory>(1, myTraj);
00231 
00232 }
00233 
00234 
00235 //
00236 // smooth trajectory
00237 //
00238 vector<Trajectory> CosmicMuonSmoother::smooth(const vector<Trajectory>& tc) const {
00239 
00240   vector<Trajectory> result; 
00241 
00242   for ( vector<Trajectory>::const_iterator it = tc.begin(); it != tc.end(); ++it ) {
00243     vector<Trajectory> smoothed = smooth(*it);
00244     result.insert(result.end(), smoothed.begin(), smoothed.end());
00245   }
00246 
00247   return result;
00248 
00249 }
00250 
00251 
00252 //
00253 // smooth trajectory
00254 //
00255 vector<Trajectory> CosmicMuonSmoother::smooth(const Trajectory& t) const {
00256 
00257   if ( t.empty() ) {
00258     LogTrace(category_)<< "Error: smooth: empty trajectory.";
00259     return vector<Trajectory>();
00260   }
00261 
00262   Trajectory myTraj(t.seed(), oppositeToMomentum);
00263 
00264   vector<TrajectoryMeasurement> avtm = t.measurements();
00265 
00266   if ( avtm.size() < 2 ) {
00267     LogTrace(category_)<< "Error: smooth: too little TM. ";
00268     return vector<Trajectory>();
00269   }
00270 
00271   TrajectoryStateOnSurface predTsos = avtm.back().forwardPredictedState();
00272   predTsos.rescaleError(theErrorRescaling);
00273 
00274   if ( !predTsos.isValid() ) {
00275     LogTrace(category_)<< "Error: smooth: first TSOS from back invalid. ";
00276     return vector<Trajectory>();
00277   }
00278 
00279   TrajectoryStateOnSurface currTsos;
00280 
00281   // first smoothed TrajectoryMeasurement is last fitted
00282   if ( avtm.back().recHit()->isValid() ) {
00283     currTsos = theUpdator->update(predTsos, (*avtm.back().recHit()));
00284     myTraj.push(TrajectoryMeasurement(avtm.back().forwardPredictedState(),
00285                    predTsos,
00286                    avtm.back().updatedState(),
00287                    avtm.back().recHit(),
00288                    avtm.back().estimate()//,
00289                    /*avtm.back().layer()*/), 
00290                 avtm.back().estimate());
00291 
00292   } else {
00293     currTsos = predTsos;
00294     myTraj.push(TrajectoryMeasurement(avtm.back().forwardPredictedState(),
00295                    avtm.back().recHit()//,
00296                    /*avtm.back().layer()*/));
00297 
00298   }
00299 
00300   TrajectoryStateCombiner combiner;
00301 
00302 
00303   for ( vector<TrajectoryMeasurement>::reverse_iterator itm = avtm.rbegin() + 1; 
00304         itm != avtm.rend() - 1; ++itm ) {
00305 
00306    if (currTsos.isValid())  {
00307      LogTrace(category_)<<"current pos "<<currTsos.globalPosition()
00308                        <<"mom "<<currTsos.globalMomentum();
00309     } else {
00310       LogTrace(category_)<<"current state invalid";
00311     }
00312 
00313     predTsos = propagatorOpposite()->propagate(currTsos,(*itm).recHit()->det()->surface());
00314 
00315     if ( !predTsos.isValid() ) {
00316       LogTrace(category_)<<"step-propagating from "<<currTsos.globalPosition() <<" to position: "<<(*itm).recHit()->globalPosition();
00317       predTsos = theUtilities->stepPropagate(currTsos, (*itm).recHit(), *propagatorOpposite());
00318     }
00319    if (predTsos.isValid())  {
00320       LogTrace(category_)<<"predicted pos "<<predTsos.globalPosition()
00321                        <<"mom "<<predTsos.globalMomentum();
00322     } else {
00323       LogTrace(category_)<<"predicted state invalid";
00324     }
00325 
00326     if ( !predTsos.isValid() ) {
00327       LogTrace(category_)<< "Error: predTsos is still invalid backward smooth.";
00328       return vector<Trajectory>();
00329     //  continue;
00330     } else if ( (*itm).recHit()->isValid() ) {
00331       //update
00332       currTsos = theUpdator->update(predTsos, (*(*itm).recHit()));
00333       TrajectoryStateOnSurface combTsos = combiner(predTsos, (*itm).forwardPredictedState());
00334       if ( !combTsos.isValid() ) {
00335          LogTrace(category_)<< "Error: smooth: combining pred TSOS failed. ";
00336          return vector<Trajectory>();
00337       }
00338 
00339       TrajectoryStateOnSurface smooTsos = combiner((*itm).updatedState(), predTsos);
00340 
00341       if ( !smooTsos.isValid() ) {
00342          LogTrace(category_)<< "Error: smooth: combining smooth TSOS failed. ";
00343          return vector<Trajectory>();
00344       }
00345 
00346       myTraj.push(TrajectoryMeasurement((*itm).forwardPredictedState(),
00347                      predTsos,
00348                      smooTsos,
00349                      (*itm).recHit(),
00350                      theEstimator->estimate(combTsos, (*(*itm).recHit())).second//,
00351                      /*(*itm).layer()*/),
00352                      (*itm).estimate());
00353     } else {
00354       currTsos = predTsos;
00355       TrajectoryStateOnSurface combTsos = combiner(predTsos, (*itm).forwardPredictedState());
00356       
00357       if ( !combTsos.isValid() ) {
00358          LogTrace(category_)<< "Error: smooth: combining TSOS failed. ";
00359          return vector<Trajectory>();
00360       }
00361 
00362       myTraj.push(TrajectoryMeasurement((*itm).forwardPredictedState(),
00363                      predTsos,
00364                      combTsos,
00365                      (*itm).recHit()//,
00366                      /*(*itm).layer()*/));
00367     }
00368   }
00369 
00370   // last smoothed TrajectoryMeasurement is last filtered
00371   predTsos = propagatorOpposite()->propagate(currTsos, avtm.front().recHit()->det()->surface());
00372   
00373   if ( !predTsos.isValid() ){
00374     LogTrace(category_)<< "Error: last predict TSOS failed, use original one. ";
00375  //    return vector<Trajectory>();
00376       myTraj.push(TrajectoryMeasurement(avtm.front().forwardPredictedState(),
00377                    avtm.front().recHit()));
00378   } else  {
00379     if ( avtm.front().recHit()->isValid() ) {
00380       //update
00381       currTsos = theUpdator->update(predTsos, (*avtm.front().recHit()));
00382       if (currTsos.isValid())
00383       myTraj.push(TrajectoryMeasurement(avtm.front().forwardPredictedState(),
00384                    predTsos,
00385                    currTsos,
00386                    avtm.front().recHit(),
00387                    theEstimator->estimate(predTsos, (*avtm.front().recHit())).second//,
00388                    /*avtm.front().layer()*/),
00389                 avtm.front().estimate());
00390     }
00391   }
00392   LogTrace(category_)<< "myTraj foundHits. "<<myTraj.foundHits();
00393 
00394   if (myTraj.foundHits() >= 3)
00395     return vector<Trajectory>(1, myTraj);
00396   else {
00397      LogTrace(category_)<< "Error: smooth: No enough hits in trajctory. ";
00398      return vector<Trajectory>();
00399   } 
00400 
00401 }
00402 
00403 TrajectoryStateOnSurface CosmicMuonSmoother::initialState(const Trajectory& t) const {
00404   if ( t.empty() ) return TrajectoryStateOnSurface();
00405 
00406   if ( !t.firstMeasurement().updatedState().isValid() || !t.lastMeasurement().updatedState().isValid() )  return TrajectoryStateOnSurface();
00407 
00408   TrajectoryStateOnSurface result;
00409 
00410   bool beamhaloFlag = ( t.firstMeasurement().updatedState().globalMomentum().eta() > 4.0 || t.lastMeasurement().updatedState().globalMomentum().eta() > 4.0 ); 
00411 
00412   if ( !beamhaloFlag ) { //initialState is the top one
00413      if (t.firstMeasurement().updatedState().globalPosition().y() > t.lastMeasurement().updatedState().globalPosition().y()) {
00414      result = t.firstMeasurement().updatedState();
00415      } else {
00416        result = t.lastMeasurement().updatedState();
00417      } 
00418      if (result.globalMomentum().y()> 1.0 ) //top tsos should pointing down
00419        theUtilities->reverseDirection(result,&*theService->magneticField());
00420   } else {
00421      if ( t.firstMeasurement().updatedState().globalPosition().z() * t.lastMeasurement().updatedState().globalPosition().z() >0 ) { //same side
00422        if ( fabs(t.firstMeasurement().updatedState().globalPosition().z()) > fabs(t.lastMeasurement().updatedState().globalPosition().z()) ) {
00423          result = t.firstMeasurement().updatedState();
00424        } else {
00425          result = t.lastMeasurement().updatedState();
00426        }
00427      } else { //different sides
00428 
00429        if ( fabs(t.firstMeasurement().updatedState().globalPosition().eta()) > fabs(t.lastMeasurement().updatedState().globalPosition().eta()) ) {
00430          result = t.firstMeasurement().updatedState();
00431        } else {
00432          result = t.lastMeasurement().updatedState();
00433        }
00434      }
00435 
00436   }
00437 
00438   return result;
00439 
00440 }
00441 
00442 void CosmicMuonSmoother::sortHitsAlongMom(ConstRecHitContainer& hits, const TrajectoryStateOnSurface& tsos) const {
00443 
00444     if (hits.size() < 2) return;
00445     float dis1 = (hits.front()->globalPosition() - tsos.globalPosition()).mag();
00446     float dis2 = (hits.back()->globalPosition() - tsos.globalPosition()).mag();
00447 
00448     if ( dis1 > dis2 )
00449       std::reverse(hits.begin(),hits.end());
00450 
00451     return;
00452 }
00453 

Generated on Tue Jun 9 17:44:09 2009 for CMSSW by  doxygen 1.5.4