CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/RecoTracker/FinalTrackSelectors/src/AnalyticalTrackSelector.cc

Go to the documentation of this file.
00001 #include "RecoTracker/FinalTrackSelectors/interface/AnalyticalTrackSelector.h"
00002 #include "RecoTracker/FinalTrackSelectors/src/MultiTrackSelector.h"
00003 
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include <Math/DistFunc.h>
00006 #include "TMath.h"
00007 
00008 using reco::modules::AnalyticalTrackSelector;
00009 
00010 AnalyticalTrackSelector::AnalyticalTrackSelector( const edm::ParameterSet & cfg ) : MultiTrackSelector( )
00011 {
00012     //Spoof the pset for each track selector!
00013     //Size is always 1!!!
00014     qualityToSet_.reserve(1);
00015     vtxNumber_.reserve(1);
00016     vertexCut_.reserve(1);
00017     res_par_.reserve(1);
00018     chi2n_par_.reserve(1);
00019     chi2n_no1Dmod_par_.reserve(1);
00020     d0_par1_.reserve(1);
00021     dz_par1_.reserve(1);
00022     d0_par2_.reserve(1);
00023     dz_par2_.reserve(1);
00024     applyAdaptedPVCuts_.reserve(1);
00025     max_d0_.reserve(1);
00026     max_z0_.reserve(1);
00027     nSigmaZ_.reserve(1);
00028     min_layers_.reserve(1);
00029     min_3Dlayers_.reserve(1);
00030     max_lostLayers_.reserve(1);
00031     applyAbsCutsIfNoPV_.reserve(1);
00032     max_d0NoPV_.reserve(1);
00033     max_z0NoPV_.reserve(1);
00034     preFilter_.reserve(1);
00035     max_relpterr_.reserve(1);
00036     min_nhits_.reserve(1);
00037 
00038     src_ = cfg.getParameter<edm::InputTag>( "src" );
00039     beamspot_ = cfg.getParameter<edm::InputTag>( "beamspot" );
00040     useVertices_ = cfg.getParameter<bool>( "useVertices" );
00041     useVtxError_ = cfg.getParameter<bool>( "useVtxError" );
00042     vertices_ = useVertices_ ? cfg.getParameter<edm::InputTag>( "vertices" ) : edm::InputTag("NONE");
00043     copyExtras_ = cfg.getUntrackedParameter<bool>("copyExtras", false);
00044     copyTrajectories_ = cfg.getUntrackedParameter<bool>("copyTrajectories", false);
00045     
00046     qualityToSet_.push_back( TrackBase::undefQuality );
00047     // parameters for vertex selection
00048     vtxNumber_.push_back( useVertices_ ? cfg.getParameter<int32_t>("vtxNumber") : 0 );
00049     vertexCut_.push_back( useVertices_ ? cfg.getParameter<std::string>("vertexCut") : "");
00050     //  parameters for adapted optimal cuts on chi2 and primary vertex compatibility
00051     res_par_.push_back(cfg.getParameter< std::vector<double> >("res_par") );
00052     chi2n_par_.push_back( cfg.getParameter<double>("chi2n_par") );
00053     chi2n_no1Dmod_par_.push_back( cfg.getParameter<double>("chi2n_no1Dmod_par") );
00054     d0_par1_.push_back(cfg.getParameter< std::vector<double> >("d0_par1"));
00055     dz_par1_.push_back(cfg.getParameter< std::vector<double> >("dz_par1"));
00056     d0_par2_.push_back(cfg.getParameter< std::vector<double> >("d0_par2"));
00057     dz_par2_.push_back(cfg.getParameter< std::vector<double> >("dz_par2"));
00058 
00059     // Boolean indicating if adapted primary vertex compatibility cuts are to be applied.
00060     applyAdaptedPVCuts_.push_back(cfg.getParameter<bool>("applyAdaptedPVCuts"));
00061     // Impact parameter absolute cuts.
00062     max_d0_.push_back(cfg.getParameter<double>("max_d0"));
00063     max_z0_.push_back(cfg.getParameter<double>("max_z0"));
00064     nSigmaZ_.push_back(cfg.getParameter<double>("nSigmaZ"));
00065     // Cuts on numbers of layers with hits/3D hits/lost hits.
00066     min_layers_.push_back(cfg.getParameter<uint32_t>("minNumberLayers") );
00067     min_3Dlayers_.push_back(cfg.getParameter<uint32_t>("minNumber3DLayers") );
00068     max_lostLayers_.push_back(cfg.getParameter<uint32_t>("maxNumberLostLayers"));
00069     max_relpterr_.push_back(cfg.getParameter<double>("max_relpterr"));
00070     min_nhits_.push_back(cfg.getParameter<uint32_t>("min_nhits"));
00071 
00072     // Flag to apply absolute cuts if no PV passes the selection
00073     applyAbsCutsIfNoPV_.push_back(cfg.getParameter<bool>("applyAbsCutsIfNoPV"));
00074     keepAllTracks_.push_back( cfg.exists("keepAllTracks") ?
00075                               cfg.getParameter<bool>("keepAllTracks") :
00076                               false ); 
00077  
00078     setQualityBit_.push_back( false );
00079     std::string qualityStr = cfg.getParameter<std::string>("qualityBit");
00080     
00081     if(d0_par1_[0].size()!=2 || dz_par1_[0].size()!=2 || d0_par2_[0].size()!=2 || dz_par2_[0].size()!=2)
00082     {
00083       edm::LogError("MisConfiguration")<<"vector of size less then 2";
00084       throw; 
00085     }
00086 
00087     if (cfg.exists("qualityBit")) {
00088       std::string qualityStr = cfg.getParameter<std::string>("qualityBit");
00089       if (qualityStr != "") {
00090         setQualityBit_[0] = true;
00091         qualityToSet_ [0] = TrackBase::qualityByName(cfg.getParameter<std::string>("qualityBit"));
00092       }
00093     }
00094   
00095     if (keepAllTracks_[0] && !setQualityBit_[0]) throw cms::Exception("Configuration") << 
00096       "If you set 'keepAllTracks' to true, you must specify which qualityBit to set.\n";
00097     if (setQualityBit_[0] && (qualityToSet_[0] == TrackBase::undefQuality)) throw cms::Exception("Configuration") <<
00098       "You can't set the quality bit " << cfg.getParameter<std::string>("qualityBit") << " as it is 'undefQuality' or unknown.\n";
00099     if (applyAbsCutsIfNoPV_[0]) {
00100       max_d0NoPV_.push_back(cfg.getParameter<double>("max_d0NoPV"));
00101       max_z0NoPV_.push_back(cfg.getParameter<double>("max_z0NoPV"));
00102     }
00103     else{//dummy values
00104       max_d0NoPV_.push_back(0.);
00105       max_z0NoPV_.push_back(0.);
00106     }
00107     
00108     std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
00109     produces<reco::TrackCollection>().setBranchAlias( alias + "Tracks");
00110     if (copyExtras_) {
00111       produces<reco::TrackExtraCollection>().setBranchAlias( alias + "TrackExtras");
00112       produces<TrackingRecHitCollection>().setBranchAlias( alias + "RecHits");
00113     }
00114     if (copyTrajectories_) {
00115       produces< std::vector<Trajectory> >().setBranchAlias( alias + "Trajectories");
00116       produces< TrajTrackAssociationCollection >().setBranchAlias( alias + "TrajectoryTrackAssociations");
00117     }
00118 }
00119 
00120 AnalyticalTrackSelector::~AnalyticalTrackSelector() {
00121 }
00122 
00123 void AnalyticalTrackSelector::produce( edm::Event& evt, const edm::EventSetup& es ) 
00124 {
00125   using namespace std; 
00126   using namespace edm;
00127   using namespace reco;
00128 
00129   Handle<TrackCollection> hSrcTrack;
00130   Handle< vector<Trajectory> > hTraj;
00131   Handle< vector<Trajectory> > hTrajP;
00132   Handle< TrajTrackAssociationCollection > hTTAss;
00133 
00134   // looking for the beam spot
00135   edm::Handle<reco::BeamSpot> hBsp;
00136   evt.getByLabel(beamspot_, hBsp);
00137   reco::BeamSpot vertexBeamSpot;
00138   vertexBeamSpot = *hBsp;
00139         
00140   // Select good primary vertices for use in subsequent track selection
00141   edm::Handle<reco::VertexCollection> hVtx;
00142   std::vector<Point> points;
00143   std::vector<double> vterr, vzerr;
00144   if (useVertices_) {
00145       evt.getByLabel(vertices_, hVtx);
00146       selectVertices(0,*hVtx, points, vterr, vzerr);
00147       // Debug 
00148       LogDebug("SelectVertex") << points.size() << " good pixel vertices";
00149   }
00150 
00151   // Get tracks 
00152   evt.getByLabel( src_, hSrcTrack );
00153 
00154   selTracks_ = auto_ptr<TrackCollection>(new TrackCollection());
00155   rTracks_ = evt.getRefBeforePut<TrackCollection>();      
00156   if (copyExtras_) {
00157     selTrackExtras_ = auto_ptr<TrackExtraCollection>(new TrackExtraCollection());
00158     selHits_ = auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
00159     rHits_ = evt.getRefBeforePut<TrackingRecHitCollection>();
00160     rTrackExtras_ = evt.getRefBeforePut<TrackExtraCollection>();
00161   }
00162 
00163   if (copyTrajectories_) trackRefs_.resize(hSrcTrack->size());
00164 
00165   // Loop over tracks
00166   size_t current = 0;
00167   for (TrackCollection::const_iterator it = hSrcTrack->begin(), ed = hSrcTrack->end(); it != ed; ++it, ++current) {
00168     const Track & trk = * it;
00169     // Check if this track passes cuts
00170 
00171     LogTrace("TrackSelection") << "ready to check track with pt="<< trk.pt() ;
00172 
00173     bool ok = select(0,vertexBeamSpot, trk, points, vterr, vzerr);
00174     if (!ok) {
00175 
00176       LogTrace("TrackSelection") << "track with pt="<< trk.pt() << " NOT selected";
00177 
00178       if (copyTrajectories_) trackRefs_[current] = reco::TrackRef();
00179       if (!keepAllTracks_[0]) continue;
00180     }
00181     LogTrace("TrackSelection") << "track with pt="<< trk.pt() << " selected";
00182     selTracks_->push_back( Track( trk ) ); // clone and store
00183     if (ok && setQualityBit_[0]) {
00184       selTracks_->back().setQuality(qualityToSet_[0]);
00185       if (!points.empty()) {
00186         if (qualityToSet_[0]==TrackBase::loose) selTracks_->back().setQuality(TrackBase::looseSetWithPV);
00187         else if (qualityToSet_[0]==TrackBase::highPurity) selTracks_->back().setQuality(TrackBase::highPuritySetWithPV);
00188       }
00189     }
00190     if (copyExtras_) {
00191       // TrackExtras
00192       selTrackExtras_->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
00193                                               trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
00194                                               trk.outerStateCovariance(), trk.outerDetId(),
00195                                               trk.innerStateCovariance(), trk.innerDetId(),
00196                                               trk.seedDirection(), trk.seedRef() ) );
00197       selTracks_->back().setExtra( TrackExtraRef( rTrackExtras_, selTrackExtras_->size() - 1) );
00198       TrackExtra & tx = selTrackExtras_->back();
00199       tx.setResiduals(trk.residuals());
00200       // TrackingRecHits
00201       for( trackingRecHit_iterator hit = trk.recHitsBegin(); hit != trk.recHitsEnd(); ++ hit ) {
00202         selHits_->push_back( (*hit)->clone() );
00203         tx.add( TrackingRecHitRef( rHits_, selHits_->size() - 1) );
00204       }
00205     }
00206     if (copyTrajectories_) {
00207       trackRefs_[current] = TrackRef(rTracks_, selTracks_->size() - 1);
00208     }
00209   }
00210   if ( copyTrajectories_ ) {
00211     Handle< vector<Trajectory> > hTraj;
00212     Handle< TrajTrackAssociationCollection > hTTAss;
00213     evt.getByLabel(src_, hTTAss);
00214     evt.getByLabel(src_, hTraj);
00215     selTrajs_ = auto_ptr< vector<Trajectory> >(new vector<Trajectory>()); 
00216     rTrajectories_ = evt.getRefBeforePut< vector<Trajectory> >();
00217     selTTAss_ = auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
00218     for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
00219       Ref< vector<Trajectory> > trajRef(hTraj, i);
00220       TrajTrackAssociationCollection::const_iterator match = hTTAss->find(trajRef);
00221       if (match != hTTAss->end()) {
00222         const Ref<TrackCollection> &trkRef = match->val; 
00223         short oldKey = static_cast<short>(trkRef.key());
00224         if (trackRefs_[oldKey].isNonnull()) {
00225           selTrajs_->push_back( Trajectory(*trajRef) );
00226           selTTAss_->insert ( Ref< vector<Trajectory> >(rTrajectories_, selTrajs_->size() - 1), trackRefs_[oldKey] );
00227         }
00228       }
00229     }
00230   }
00231 
00232   static const std::string emptyString;
00233   evt.put(selTracks_);
00234   if (copyExtras_ ) {
00235     evt.put(selTrackExtras_); 
00236     evt.put(selHits_);
00237   }
00238   if ( copyTrajectories_ ) {
00239     evt.put(selTrajs_);
00240     evt.put(selTTAss_);
00241   }
00242 }