00001 #include "RecoTracker/FinalTrackSelectors/interface/AnalyticalTrackSelector.h"
00002
00003 #include <Math/DistFunc.h>
00004 #include "TMath.h"
00005
00006 using reco::modules::AnalyticalTrackSelector;
00007
00008 AnalyticalTrackSelector::AnalyticalTrackSelector( const edm::ParameterSet & cfg ) :
00009 src_( cfg.getParameter<edm::InputTag>( "src" ) ),
00010 beamspot_( cfg.getParameter<edm::InputTag>( "beamspot" ) ),
00011 vertices_( cfg.getParameter<edm::InputTag>( "vertices" ) ),
00012 copyExtras_(cfg.getUntrackedParameter<bool>("copyExtras", false)),
00013 copyTrajectories_(cfg.getUntrackedParameter<bool>("copyTrajectories", false)),
00014 keepAllTracks_( cfg.exists("keepAllTracks") ?
00015 cfg.getParameter<bool>("keepAllTracks") :
00016 false ),
00017 setQualityBit_( false ),
00018 qualityToSet_( TrackBase::undefQuality ),
00019 vtxNumber_( cfg.getParameter<int32_t>("vtxNumber") ),
00020 vtxTracks_( cfg.getParameter<uint32_t>("vtxTracks") ),
00021 vtxChi2Prob_( cfg.getParameter<double>("vtxChi2Prob") ),
00022 res_par_(cfg.getParameter< std::vector<double> >("res_par") ),
00023 chi2n_par_( cfg.getParameter<double>("chi2n_par") ),
00024 d0_par1_(cfg.getParameter< std::vector<double> >("d0_par1")),
00025 dz_par1_(cfg.getParameter< std::vector<double> >("dz_par1")),
00026 d0_par2_(cfg.getParameter< std::vector<double> >("d0_par2")),
00027 dz_par2_(cfg.getParameter< std::vector<double> >("dz_par2")),
00028 min_layers_(cfg.getParameter<uint32_t>("minNumberLayers") )
00029
00030 {
00031 if (cfg.exists("qualityBit")) {
00032 std::string qualityStr = cfg.getParameter<std::string>("qualityBit");
00033 if (qualityStr != "") {
00034 setQualityBit_ = true;
00035 qualityToSet_ = TrackBase::qualityByName(cfg.getParameter<std::string>("qualityBit"));
00036 }
00037 }
00038 if (keepAllTracks_ && !setQualityBit_) throw cms::Exception("Configuration") <<
00039 "If you set 'keepAllTracks' to true, you must specify which qualityBit to set.\n";
00040 if (setQualityBit_ && (qualityToSet_ == TrackBase::undefQuality)) throw cms::Exception("Configuration") <<
00041 "You can't set the quality bit " << cfg.getParameter<std::string>("qualityBit") << " as it is 'undefQuality' or unknown.\n";
00042
00043 std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
00044 produces<reco::TrackCollection>().setBranchAlias( alias + "Tracks");
00045 if (copyExtras_) {
00046 produces<reco::TrackExtraCollection>().setBranchAlias( alias + "TrackExtras");
00047 produces<TrackingRecHitCollection>().setBranchAlias( alias + "RecHits");
00048 if (copyTrajectories_) {
00049 produces< std::vector<Trajectory> >().setBranchAlias( alias + "Trajectories");
00050 produces< TrajTrackAssociationCollection >().setBranchAlias( alias + "TrajectoryTrackAssociations");
00051 }
00052 }
00053 selTracks_ = std::auto_ptr<reco::TrackCollection>(new reco::TrackCollection());
00054 selTrackExtras_ = std::auto_ptr<reco::TrackExtraCollection>(new reco::TrackExtraCollection());
00055 selHits_ = std::auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
00056 selTrajs_ = std::auto_ptr< std::vector<Trajectory> >(new std::vector<Trajectory>());
00057 selTTAss_ = std::auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
00058
00059 }
00060
00061 AnalyticalTrackSelector::~AnalyticalTrackSelector() {
00062 }
00063
00064 void AnalyticalTrackSelector::produce( edm::Event& evt, const edm::EventSetup& es )
00065 {
00066 using namespace std;
00067 using namespace edm;
00068 using namespace reco;
00069
00070 Handle<TrackCollection> hSrcTrack;
00071 Handle< vector<Trajectory> > hTraj;
00072 Handle< TrajTrackAssociationCollection > hTTAss;
00073
00074
00075 edm::Handle<reco::BeamSpot> hBsp;
00076 evt.getByLabel(beamspot_, hBsp);
00077 reco::BeamSpot vertexBeamSpot;
00078 vertexBeamSpot = *hBsp;
00079
00080 edm::Handle<reco::VertexCollection> hVtx;
00081 evt.getByLabel(vertices_, hVtx);
00082 std::vector<Point> points;
00083 selectVertices(*hVtx, points);
00084
00085 evt.getByLabel( src_, hSrcTrack );
00086
00087 selTracks_ = auto_ptr<TrackCollection>(new TrackCollection());
00088 rTracks_ = evt.getRefBeforePut<TrackCollection>();
00089 if (copyExtras_) {
00090 selTrackExtras_ = auto_ptr<TrackExtraCollection>(new TrackExtraCollection());
00091 selHits_ = auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
00092 rHits_ = evt.getRefBeforePut<TrackingRecHitCollection>();
00093 rTrackExtras_ = evt.getRefBeforePut<TrackExtraCollection>();
00094 }
00095
00096 if (copyTrajectories_) trackRefs_.resize(hSrcTrack->size());
00097 size_t current = 0;
00098 for (TrackCollection::const_iterator it = hSrcTrack->begin(), ed = hSrcTrack->end(); it != ed; ++it, ++current) {
00099 const Track & trk = * it;
00100 bool ok = select(vertexBeamSpot, trk, points);
00101 if (!ok) {
00102 if (copyTrajectories_) trackRefs_[current] = reco::TrackRef();
00103 if (!keepAllTracks_) continue;
00104 }
00105 selTracks_->push_back( Track( trk ) );
00106 if (ok && setQualityBit_) selTracks_->back().setQuality(qualityToSet_);
00107 if (!copyExtras_) continue;
00108
00109
00110 selTrackExtras_->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
00111 trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
00112 trk.outerStateCovariance(), trk.outerDetId(),
00113 trk.innerStateCovariance(), trk.innerDetId(),
00114 trk.seedDirection(), trk.seedRef() ) );
00115 selTracks_->back().setExtra( TrackExtraRef( rTrackExtras_, selTrackExtras_->size() - 1) );
00116 TrackExtra & tx = selTrackExtras_->back();
00117
00118 for( trackingRecHit_iterator hit = trk.recHitsBegin(); hit != trk.recHitsEnd(); ++ hit ) {
00119 selHits_->push_back( (*hit)->clone() );
00120 tx.add( TrackingRecHitRef( rHits_, selHits_->size() - 1) );
00121 }
00122 if (copyTrajectories_) {
00123 trackRefs_[current] = TrackRef(rTracks_, selTracks_->size() - 1);
00124 }
00125 }
00126 if ( copyTrajectories_ ) {
00127 Handle< vector<Trajectory> > hTraj;
00128 Handle< TrajTrackAssociationCollection > hTTAss;
00129 evt.getByLabel(src_, hTTAss);
00130 evt.getByLabel(src_, hTraj);
00131 selTrajs_ = auto_ptr< vector<Trajectory> >(new vector<Trajectory>());
00132 rTrajectories_ = evt.getRefBeforePut< vector<Trajectory> >();
00133 selTTAss_ = auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
00134 for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
00135 Ref< vector<Trajectory> > trajRef(hTraj, i);
00136 TrajTrackAssociationCollection::const_iterator match = hTTAss->find(trajRef);
00137 if (match != hTTAss->end()) {
00138 const Ref<TrackCollection> &trkRef = match->val;
00139 short oldKey = static_cast<short>(trkRef.key());
00140 if (trackRefs_[oldKey].isNonnull()) {
00141 selTrajs_->push_back( Trajectory(*trajRef) );
00142 selTTAss_->insert ( Ref< vector<Trajectory> >(rTrajectories_, selTrajs_->size() - 1), trackRefs_[oldKey] );
00143 }
00144 }
00145 }
00146 }
00147
00148 static const std::string emptyString;
00149 evt.put(selTracks_);
00150 if (copyExtras_ ) {
00151 evt.put(selTrackExtras_);
00152 evt.put(selHits_);
00153 }
00154 if (copyExtras_ ) {
00155 if ( copyTrajectories_ ) {
00156 evt.put(selTrajs_);
00157 evt.put(selTTAss_);
00158 }
00159 }
00160 }
00161
00162
00163 bool AnalyticalTrackSelector::select(const reco::BeamSpot &vertexBeamSpot, const reco::Track &tk, const std::vector<Point> &points) {
00164 using namespace std;
00165 uint32_t nlayers = tk.hitPattern().trackerLayersWithMeasurement();
00166
00167 if (nlayers<min_layers_) return false;
00168
00169 double pt = tk.pt(),eta = tk.eta(), chi2n = tk.normalizedChi2();
00170 double d0 = -tk.dxy(vertexBeamSpot.position()), d0E = tk.d0Error(),dz = tk.dz(), dzE = tk.dzError();
00171
00172 double nomd0E = sqrt(res_par_[0]*res_par_[0]+(res_par_[1]/max(pt,1e-9))*(res_par_[1]/max(pt,1e-9)));
00173
00174 double nomdzE = nomd0E*(std::cosh(eta));
00175
00176 if (chi2n <= chi2n_par_*nlayers &&
00177 abs(d0) < pow(d0_par1_[0]*nlayers,d0_par1_[1])*nomd0E &&
00178 abs(d0) < pow(d0_par2_[0]*nlayers,d0_par2_[1])*d0E ) {
00179
00180 if (points.empty()) {
00181 if ( abs(dz) < (vertexBeamSpot.sigmaZ()*3) ) return true;
00182 }
00183
00184 for (std::vector<Point>::const_iterator point = points.begin(), end = points.end(); point != end; ++point) {
00185 if (
00186 abs(dz-(point->z())) < pow(dz_par1_[0]*nlayers,dz_par1_[1])*nomdzE &&
00187 abs(dz-(point->z())) < pow(dz_par2_[0]*nlayers,dz_par2_[1])*dzE ) return true;
00188 }
00189 }
00190 return false;
00191 }
00192 void AnalyticalTrackSelector::selectVertices(const reco::VertexCollection &vtxs, std::vector<Point> &points) {
00193 using namespace reco;
00194 int32_t toTake = vtxNumber_;
00195 for (VertexCollection::const_iterator it = vtxs.begin(), ed = vtxs.end(); it != ed; ++it) {
00196 if ((it->tracksSize() >= vtxTracks_) &&
00197 ( (it->chi2() == 0.0) || (TMath::Prob(it->chi2(), static_cast<int32_t>(it->ndof()) ) >= vtxChi2Prob_) ) ) {
00198 points.push_back(it->position());
00199 toTake--; if (toTake == 0) break;
00200 }
00201 }
00202 }