CMS 3D CMS Logo

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