CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CosmicTrackSelector.cc
Go to the documentation of this file.
2 
3 #include <Math/DistFunc.h>
4 #include "TMath.h"
5 
7 
8 CosmicTrackSelector::CosmicTrackSelector( const edm::ParameterSet & cfg ) :
9  src_( cfg.getParameter<edm::InputTag>( "src" ) ),
10  beamspot_( cfg.getParameter<edm::InputTag>( "beamspot" ) ),
11  copyExtras_(cfg.getUntrackedParameter<bool>("copyExtras", false)),
12  copyTrajectories_(cfg.getUntrackedParameter<bool>("copyTrajectories", false)),
13  keepAllTracks_( cfg.exists("keepAllTracks") ?
14  cfg.getParameter<bool>("keepAllTracks") :
15  false ), // as this is what you expect from a well behaved selector
16  setQualityBit_( false ),
17  qualityToSet_( TrackBase::undefQuality ),
18  chi2n_par_( cfg.getParameter<double>("chi2n_par") ),
19  // Impact parameter absolute cuts.
20  max_d0_(cfg.getParameter<double>("max_d0")),
21  max_z0_(cfg.getParameter<double>("max_z0")),
22  // Track parameter cuts.
23  min_pt_(cfg.getParameter<double>("min_pt")),
24  max_eta_(cfg.getParameter<double>("max_eta")),
25  // Cut on number of valid hits
26  min_nHit_(cfg.getParameter<uint32_t>("min_nHit")),
27  // Cut on number of valid hits
28  min_nPixelHit_(cfg.getParameter<uint32_t>("min_nPixelHit")),
29  // Cuts on numbers of layers with hits/3D hits/lost hits.
30  min_layers_(cfg.getParameter<uint32_t>("minNumberLayers") ),
31  min_3Dlayers_(cfg.getParameter<uint32_t>("minNumber3DLayers") ),
32  max_lostLayers_(cfg.getParameter<uint32_t>("maxNumberLostLayers") )
33 {
34  if (cfg.exists("qualityBit")) {
35  std::string qualityStr = cfg.getParameter<std::string>("qualityBit");
36  if (qualityStr != "") {
37  setQualityBit_ = true;
38  qualityToSet_ = TrackBase::qualityByName(cfg.getParameter<std::string>("qualityBit"));
39  }
40  }
41  if (keepAllTracks_ && !setQualityBit_) throw cms::Exception("Configuration") <<
42  "If you set 'keepAllTracks' to true, you must specify which qualityBit to set.\n";
43  if (setQualityBit_ && (qualityToSet_ == TrackBase::undefQuality)) throw cms::Exception("Configuration") <<
44  "You can't set the quality bit " << cfg.getParameter<std::string>("qualityBit") << " as it is 'undefQuality' or unknown.\n";
45 
46  std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
47  produces<reco::TrackCollection>().setBranchAlias( alias + "Tracks");
48  if (copyExtras_) {
49  produces<reco::TrackExtraCollection>().setBranchAlias( alias + "TrackExtras");
50  produces<TrackingRecHitCollection>().setBranchAlias( alias + "RecHits");
51  }
52  if (copyTrajectories_) {
53  produces< std::vector<Trajectory> >().setBranchAlias( alias + "Trajectories");
54  produces< TrajTrackAssociationCollection >().setBranchAlias( alias + "TrajectoryTrackAssociations");
55  }
56 
57 }
58 
60 }
61 
63 {
64  using namespace std;
65  using namespace edm;
66  using namespace reco;
67 
68  Handle<TrackCollection> hSrcTrack;
72 
73  // looking for the beam spot
75  evt.getByLabel(beamspot_, hBsp);
76  reco::BeamSpot vertexBeamSpot;
77  vertexBeamSpot = *hBsp;
78 
79  // Get tracks
80  evt.getByLabel( src_, hSrcTrack );
81 
82  selTracks_ = auto_ptr<TrackCollection>(new TrackCollection());
84  if (copyExtras_) {
85  selTrackExtras_ = auto_ptr<TrackExtraCollection>(new TrackExtraCollection());
86  selHits_ = auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
89  }
90 
91  if (copyTrajectories_) trackRefs_.resize(hSrcTrack->size());
92 
93  // Loop over tracks
94  size_t current = 0;
95  for (TrackCollection::const_iterator it = hSrcTrack->begin(), ed = hSrcTrack->end(); it != ed; ++it, ++current) {
96  const Track & trk = * it;
97  // Check if this track passes cuts
98  bool ok = select(vertexBeamSpot, trk);
99  if (!ok) {
101  if (!keepAllTracks_) continue;
102  }
103  selTracks_->push_back( Track( trk ) ); // clone and store
104  if (ok && setQualityBit_) selTracks_->back().setQuality(qualityToSet_);
105  if (copyExtras_) {
106  // TrackExtras
107  selTrackExtras_->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
108  trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
109  trk.outerStateCovariance(), trk.outerDetId(),
110  trk.innerStateCovariance(), trk.innerDetId(),
111  trk.seedDirection(), trk.seedRef() ) );
112  selTracks_->back().setExtra( TrackExtraRef( rTrackExtras_, selTrackExtras_->size() - 1) );
113  TrackExtra & tx = selTrackExtras_->back();
114  tx.setResiduals(trk.residuals());
115  // TrackingRecHits
116  for( trackingRecHit_iterator hit = trk.recHitsBegin(); hit != trk.recHitsEnd(); ++ hit ) {
117  selHits_->push_back( (*hit)->clone() );
118  tx.add( TrackingRecHitRef( rHits_, selHits_->size() - 1) );
119  }
120  }
121  if (copyTrajectories_) {
122  trackRefs_[current] = TrackRef(rTracks_, selTracks_->size() - 1);
123  }
124  }
125  if ( copyTrajectories_ ) {
128  evt.getByLabel(src_, hTTAss);
129  evt.getByLabel(src_, hTraj);
130  selTrajs_ = auto_ptr< vector<Trajectory> >(new vector<Trajectory>());
131  rTrajectories_ = evt.getRefBeforePut< vector<Trajectory> >();
132  selTTAss_ = auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
133  for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
134  Ref< vector<Trajectory> > trajRef(hTraj, i);
136  if (match != hTTAss->end()) {
137  const Ref<TrackCollection> &trkRef = match->val;
138  short oldKey = static_cast<short>(trkRef.key());
139  if (trackRefs_[oldKey].isNonnull()) {
140  selTrajs_->push_back( Trajectory(*trajRef) );
141  selTTAss_->insert ( Ref< vector<Trajectory> >(rTrajectories_, selTrajs_->size() - 1), trackRefs_[oldKey] );
142  }
143  }
144  }
145  }
146 
147  static const std::string emptyString;
148  evt.put(selTracks_);
149  if (copyExtras_ ) {
150  evt.put(selTrackExtras_);
151  evt.put(selHits_);
152  }
153  if ( copyTrajectories_ ) {
154  evt.put(selTrajs_);
155  evt.put(selTTAss_);
156  }
157 }
158 
159 
160 bool CosmicTrackSelector::select(const reco::BeamSpot &vertexBeamSpot, const reco::Track &tk) {
161  // Decide if the given track passes selection cuts.
162 
163  using namespace std;
164 
165  // Cuts on numbers of layers with hits/3D hits/lost hits.
166  uint32_t nlayers = tk.hitPattern().trackerLayersWithMeasurement();
167  uint32_t nlayers3D = tk.hitPattern().pixelLayersWithMeasurement() +
169  uint32_t nlayersLost = tk.hitPattern().trackerLayersWithoutMeasurement();
170 
171  // Get the number of valid hits and PixelHits
172  uint32_t nHit = 0;
173  uint32_t nPixelHit = 0;
174  for ( trackingRecHit_iterator recHit = tk.recHitsBegin(); recHit != tk.recHitsEnd(); ++recHit ) {
175  if ( !((*recHit)->isValid()) ) continue;
176  ++nHit;
177  DetId id((*recHit)->geographicalId());
178  if ( (unsigned int)id.subdetId() == PixelSubdetector::PixelBarrel
179  || (unsigned int)id.subdetId() == PixelSubdetector::PixelEndcap )
180  ++nPixelHit;
181  }
182 
183  // Cut on the number of valid hits
184  if (nHit < min_nHit_) return false;
185  // Cut on the number of valid Pixel hits
186  if (nPixelHit < min_nPixelHit_) return false;
187  if (nlayers < min_layers_) return false;
188  if (nlayers3D < min_3Dlayers_) return false;
189  if (nlayersLost > max_lostLayers_) return false;
190 
191  // Get track parameters
192  double pt = tk.pt(),eta = tk.eta(), chi2n = tk.normalizedChi2();
193  double d0 = -tk.dxy(vertexBeamSpot.position()), dz = tk.dz();
194 
195  // Absolute cuts on all tracks impact parameters with respect to beam-spot.
196  if (abs(d0) > max_d0_) return false;
197  if (abs(dz) > max_z0_) return false;
198 
199  // optimized cuts adapted to the track eta, pt and chiquare/ndof
200  if (abs(eta) > max_eta_) return false;
201  if (pt < min_pt_) return false;
202  if (chi2n > chi2n_par_*nlayers) return false;
203 
204 
205  else
206  return true;
207 
208 }
209 
210 
T getParameter(std::string const &) const
std::auto_ptr< reco::TrackExtraCollection > selTrackExtras_
int i
Definition: DBlmapReader.cc:9
double normalizedChi2() const
chi-squared divided by n.d.o.f. (or chi-squared * 1e6 if n.d.o.f. is zero)
Definition: TrackBase.h:111
edm::RefProd< std::vector< Trajectory > > rTrajectories_
std::auto_ptr< std::vector< Trajectory > > selTrajs_
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:10
bool exists(std::string const &parameterName) const
checks if a parameter exists
reco::TrackExtraRefProd rTrackExtras_
int pixelLayersWithMeasurement() const
Definition: HitPattern.h:710
std::vector< reco::TrackRef > trackRefs_
int trackerLayersWithoutMeasurement() const
Definition: HitPattern.h:723
#define abs(x)
Definition: mlp_lapack.h:159
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:40
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:13
T eta() const
int numberOfValidStripLayersWithMonoAndStereo() const
Definition: HitPattern.cc:224
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:47
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:42
TrackBase::TrackQuality qualityToSet_
std::auto_ptr< TrajTrackAssociationCollection > selTTAss_
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:141
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
int trackerLayersWithMeasurement() const
Definition: HitPattern.h:705
edm::Ref< TrackingRecHitCollection > TrackingRecHitRef
persistent reference to a TrackingRecHit
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:51
double pt() const
track transverse momentum
Definition: TrackBase.h:131
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:59
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:63
std::auto_ptr< TrackingRecHitCollection > selHits_
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:223
void produce(edm::Event &evt, const edm::EventSetup &es)
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
RefProd< PROD > getRefBeforePut()
Definition: Event.h:97
double dz() const
dz parameter (= dsz/cos(lambda)). This is the track z0 w.r.t (0,0,0) only if the refPoint is close to...
Definition: TrackBase.h:127
Definition: DetId.h:20
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:9
edm::OwnVector< TrackingRecHit > TrackingRecHitCollection
collection of TrackingRecHits
edm::RefToBase< TrajectorySeed > seedRef() const
Definition: Track.h:112
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:46
std::auto_ptr< reco::TrackCollection > selTracks_
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:49
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:38
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:14
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:53
key_type key() const
Accessor for product key.
Definition: Ref.h:266
void add(const TrackingRecHitRef &r)
add a reference to a RecHit
bool select(const reco::BeamSpot &vertexBeamSpot, const reco::Track &tk)
static std::string const emptyString("")
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:45
const TrackResiduals & residuals() const
Definition: Track.h:117
PropagationDirection seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:105
const Point & position() const
position
Definition: BeamSpot.h:63
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:6
double dxy() const
dxy parameter. (This is the transverse impact parameter w.r.t. to (0,0,0) ONLY if refPoint is close t...
Definition: TrackBase.h:121
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:61
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:65
void setResiduals(const TrackResiduals &r)
set the residuals
Definition: TrackExtra.h:131