CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AnalyticalTrackSelector.cc
Go to the documentation of this file.
1 
11 #include <utility>
12 #include <vector>
13 #include <memory>
14 #include <algorithm>
15 #include <map>
20 
31 
32 #include "MultiTrackSelector.h"
33 
34 using namespace reco;
35 
37  private:
38  public:
40  explicit AnalyticalTrackSelector( const edm::ParameterSet & cfg ) ;
42  virtual ~AnalyticalTrackSelector() ;
43 
44  private:
47  void run( edm::Event& evt, const edm::EventSetup& es ) const override;
48 
54  double minEta_;
55  double maxEta_;
56 
59 
60 
61  };
62 
63 
64 
65 
67 
69 #include <Math/DistFunc.h>
70 #include "TMath.h"
71 
72 
74 {
75  //Spoof the pset for each track selector!
76  //Size is always 1!!!
77  qualityToSet_.reserve(1);
78  vtxNumber_.reserve(1);
79  vertexCut_.reserve(1);
80  res_par_.reserve(1);
81  chi2n_par_.reserve(1);
82  chi2n_no1Dmod_par_.reserve(1);
83  d0_par1_.reserve(1);
84  dz_par1_.reserve(1);
85  d0_par2_.reserve(1);
86  dz_par2_.reserve(1);
87  applyAdaptedPVCuts_.reserve(1);
88  max_d0_.reserve(1);
89  max_z0_.reserve(1);
90  nSigmaZ_.reserve(1);
91  min_layers_.reserve(1);
92  min_3Dlayers_.reserve(1);
93  max_lostLayers_.reserve(1);
94  min_hits_bypass_.reserve(1);
95  applyAbsCutsIfNoPV_.reserve(1);
96  max_d0NoPV_.reserve(1);
97  max_z0NoPV_.reserve(1);
98  preFilter_.reserve(1);
99  max_relpterr_.reserve(1);
100  min_nhits_.reserve(1);
101  max_minMissHitOutOrIn_.reserve(1);
102  max_lostHitFraction_.reserve(1);
103  min_eta_.reserve(1);
104  max_eta_.reserve(1);
105  forest_.reserve(1);
106  mvaType_.reserve(1);
107  useMVA_.reserve(1);
108 
109  produces<edm::ValueMap<float> >("MVAVals");
110  useAnyMVA_ = false;
111  forest_[0] = nullptr;
112  if(cfg.exists("useAnyMVA")) useAnyMVA_ = cfg.getParameter<bool>("useAnyMVA");
113 
114  src_ = consumes<reco::TrackCollection>(cfg.getParameter<edm::InputTag>( "src" ));
115  hSrc_ = consumes<TrackingRecHitCollection>(cfg.getParameter<edm::InputTag>( "src" ));
116  beamspot_ = consumes<reco::BeamSpot>(cfg.getParameter<edm::InputTag>( "beamspot" ));
117  useVertices_ = cfg.getParameter<bool>( "useVertices" );
118  useVtxError_ = cfg.getParameter<bool>( "useVtxError" );
119  if (useVertices_) vertices_ = consumes<reco::VertexCollection>(cfg.getParameter<edm::InputTag>( "vertices" ));
120  copyExtras_ = cfg.getUntrackedParameter<bool>("copyExtras", false);
121  copyTrajectories_ = cfg.getUntrackedParameter<bool>("copyTrajectories", false);
122  if (copyTrajectories_) {
123  srcTraj_ = consumes<std::vector<Trajectory> >(cfg.getParameter<edm::InputTag>( "src" ));
124  srcTass_ = consumes<TrajTrackAssociationCollection>(cfg.getParameter<edm::InputTag>( "src" ));
125  }
126 
127  qualityToSet_.push_back( TrackBase::undefQuality );
128  // parameters for vertex selection
129  vtxNumber_.push_back( useVertices_ ? cfg.getParameter<int32_t>("vtxNumber") : 0 );
130  vertexCut_.push_back( useVertices_ ? cfg.getParameter<std::string>("vertexCut") : "");
131  // parameters for adapted optimal cuts on chi2 and primary vertex compatibility
132  res_par_.push_back(cfg.getParameter< std::vector<double> >("res_par") );
133  chi2n_par_.push_back( cfg.getParameter<double>("chi2n_par") );
134  chi2n_no1Dmod_par_.push_back( cfg.getParameter<double>("chi2n_no1Dmod_par") );
135  d0_par1_.push_back(cfg.getParameter< std::vector<double> >("d0_par1"));
136  dz_par1_.push_back(cfg.getParameter< std::vector<double> >("dz_par1"));
137  d0_par2_.push_back(cfg.getParameter< std::vector<double> >("d0_par2"));
138  dz_par2_.push_back(cfg.getParameter< std::vector<double> >("dz_par2"));
139 
140  // Boolean indicating if adapted primary vertex compatibility cuts are to be applied.
141  applyAdaptedPVCuts_.push_back(cfg.getParameter<bool>("applyAdaptedPVCuts"));
142  // Impact parameter absolute cuts.
143  max_d0_.push_back(cfg.getParameter<double>("max_d0"));
144  max_z0_.push_back(cfg.getParameter<double>("max_z0"));
145  nSigmaZ_.push_back(cfg.getParameter<double>("nSigmaZ"));
146  // Cuts on numbers of layers with hits/3D hits/lost hits.
147  min_layers_.push_back(cfg.getParameter<uint32_t>("minNumberLayers") );
148  min_3Dlayers_.push_back(cfg.getParameter<uint32_t>("minNumber3DLayers") );
149  max_lostLayers_.push_back(cfg.getParameter<uint32_t>("maxNumberLostLayers"));
150  min_hits_bypass_.push_back(cfg.getParameter<uint32_t>("minHitsToBypassChecks"));
151  max_relpterr_.push_back(cfg.getParameter<double>("max_relpterr"));
152  min_nhits_.push_back(cfg.getParameter<uint32_t>("min_nhits"));
153  max_minMissHitOutOrIn_.push_back(
154  cfg.existsAs<int32_t>("max_minMissHitOutOrIn") ?
155  cfg.getParameter<int32_t>("max_minMissHitOutOrIn") : 99);
156  max_lostHitFraction_.push_back(
157  cfg.existsAs<double>("max_lostHitFraction") ?
158  cfg.getParameter<double>("max_lostHitFraction") : 1.0);
159  min_eta_.push_back(cfg.getParameter<double>("min_eta"));
160  max_eta_.push_back(cfg.getParameter<double>("max_eta"));
161 
162  // Flag to apply absolute cuts if no PV passes the selection
163  applyAbsCutsIfNoPV_.push_back(cfg.getParameter<bool>("applyAbsCutsIfNoPV"));
164  keepAllTracks_.push_back( cfg.exists("keepAllTracks") ?
165  cfg.getParameter<bool>("keepAllTracks") :
166  false );
167 
168  setQualityBit_.push_back( false );
169  std::string qualityStr = cfg.getParameter<std::string>("qualityBit");
170 
171  if(d0_par1_[0].size()!=2 || dz_par1_[0].size()!=2 || d0_par2_[0].size()!=2 || dz_par2_[0].size()!=2)
172  {
173  edm::LogError("MisConfiguration")<<"vector of size less then 2";
174  throw;
175  }
176 
177  if (cfg.exists("qualityBit")) {
178  std::string qualityStr = cfg.getParameter<std::string>("qualityBit");
179  if (qualityStr != "") {
180  setQualityBit_[0] = true;
181  qualityToSet_ [0] = TrackBase::qualityByName(cfg.getParameter<std::string>("qualityBit"));
182  }
183  }
184 
185  if (keepAllTracks_[0] && !setQualityBit_[0]) throw cms::Exception("Configuration") <<
186  "If you set 'keepAllTracks' to true, you must specify which qualityBit to set.\n";
187  if (setQualityBit_[0] && (qualityToSet_[0] == TrackBase::undefQuality)) throw cms::Exception("Configuration") <<
188  "You can't set the quality bit " << cfg.getParameter<std::string>("qualityBit") << " as it is 'undefQuality' or unknown.\n";
189  if (applyAbsCutsIfNoPV_[0]) {
190  max_d0NoPV_.push_back(cfg.getParameter<double>("max_d0NoPV"));
191  max_z0NoPV_.push_back(cfg.getParameter<double>("max_z0NoPV"));
192  }
193  else{//dummy values
194  max_d0NoPV_.push_back(0.);
195  max_z0NoPV_.push_back(0.);
196  }
197 
198  if(useAnyMVA_){
199  bool thisMVA = false;
200  if(cfg.exists("useMVA"))thisMVA = cfg.getParameter<bool>("useMVA");
201  useMVA_.push_back(thisMVA);
202  if(thisMVA){
203  double minVal = -1;
204  if(cfg.exists("minMVA"))minVal = cfg.getParameter<double>("minMVA");
205  min_MVA_.push_back(minVal);
206  mvaType_.push_back(cfg.exists("mvaType") ? cfg.getParameter<std::string>("mvaType") : "Detached");
207  forestLabel_.push_back(cfg.exists("GBRForestLabel") ? cfg.getParameter<std::string>("GBRForestLabel") : "MVASelectorIter0");
208  useMVAonly_.push_back(cfg.exists("useMVAonly") ? cfg.getParameter<bool>("useMVAonly") : false);
209  }else{
210  min_MVA_.push_back(-9999.0);
211  useMVAonly_.push_back(false);
212  mvaType_.push_back("Detached");
213  forestLabel_.push_back("MVASelectorIter0");
214  }
215  }else{
216  useMVA_.push_back(false);
217  useMVAonly_.push_back(false);
218  min_MVA_.push_back(-9999.0);
219  mvaType_.push_back("Detached");
220  forestLabel_.push_back("MVASelectorIter0");
221  }
222 
223  std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
224  produces<reco::TrackCollection>().setBranchAlias( alias + "Tracks");
225  if (copyExtras_) {
226  produces<reco::TrackExtraCollection>().setBranchAlias( alias + "TrackExtras");
227  produces<TrackingRecHitCollection>().setBranchAlias( alias + "RecHits");
228  }
229  if (copyTrajectories_) {
230  produces< std::vector<Trajectory> >().setBranchAlias( alias + "Trajectories");
231  produces< TrajTrackAssociationCollection >().setBranchAlias( alias + "TrajectoryTrackAssociations");
232  }
233 }
234 
236 }
237 
238 
240 {
241 
242  // storage....
243  std::auto_ptr<reco::TrackCollection> selTracks_;
244  std::auto_ptr<reco::TrackExtraCollection> selTrackExtras_;
245  std::auto_ptr< TrackingRecHitCollection> selHits_;
246  std::auto_ptr< std::vector<Trajectory> > selTrajs_;
247  std::auto_ptr< std::vector<const Trajectory *> > selTrajPtrs_;
248  std::auto_ptr< TrajTrackAssociationCollection > selTTAss_;
249  reco::TrackRefProd rTracks_;
250  reco::TrackExtraRefProd rTrackExtras_;
251  TrackingRecHitRefProd rHits_;
252  edm::RefProd< std::vector<Trajectory> > rTrajectories_;
253  std::vector<reco::TrackRef> trackRefs_;
254 
255 
256  using namespace std;
257  using namespace edm;
258  using namespace reco;
259 
260  Handle<TrackCollection> hSrcTrack;
264 
265  // looking for the beam spot
267  evt.getByToken(beamspot_, hBsp);
268  reco::BeamSpot vertexBeamSpot;
269  vertexBeamSpot = *hBsp;
270 
271  // Select good primary vertices for use in subsequent track selection
273  std::vector<Point> points;
274  std::vector<float> vterr, vzerr;
275  if (useVertices_) {
276  evt.getByToken(vertices_, hVtx);
277  selectVertices(0,*hVtx, points, vterr, vzerr);
278  // Debug
279  LogDebug("SelectVertex") << points.size() << " good pixel vertices";
280  }
281 
282  // Get tracks
283  evt.getByToken( src_, hSrcTrack );
284  // get hits in track..
286  evt.getByToken( hSrc_, hSrcHits );
287  const TrackingRecHitCollection & srcHits(*hSrcHits);
288 
289 
290 
291  selTracks_ = auto_ptr<TrackCollection>(new TrackCollection());
292  rTracks_ = evt.getRefBeforePut<TrackCollection>();
293  if (copyExtras_) {
294  selTrackExtras_ = auto_ptr<TrackExtraCollection>(new TrackExtraCollection());
295  selHits_ = auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
297  rTrackExtras_ = evt.getRefBeforePut<TrackExtraCollection>();
298  }
299 
300  if (copyTrajectories_) trackRefs_.resize(hSrcTrack->size());
301 
302  std::vector<float> mvaVals_(hSrcTrack->size(),-99.f);
303  processMVA(evt,es,vertexBeamSpot,*(hVtx.product()),0,mvaVals_,true);
304 
305  // Loop over tracks
306  size_t current = 0;
307  for (TrackCollection::const_iterator it = hSrcTrack->begin(), ed = hSrcTrack->end(); it != ed; ++it, ++current) {
308  const Track & trk = * it;
309  // Check if this track passes cuts
310 
311  LogTrace("TrackSelection") << "ready to check track with pt="<< trk.pt() ;
312 
313  float mvaVal = 0;
314  if(useAnyMVA_)mvaVal = mvaVals_[current];
315  bool ok = select(0,vertexBeamSpot, srcHits, trk, points, vterr, vzerr,mvaVal);
316  if (!ok) {
317 
318  LogTrace("TrackSelection") << "track with pt="<< trk.pt() << " NOT selected";
319 
320  if (copyTrajectories_) trackRefs_[current] = reco::TrackRef();
321  if (!keepAllTracks_[0]) continue;
322  }
323  LogTrace("TrackSelection") << "track with pt="<< trk.pt() << " selected";
324  selTracks_->push_back( Track( trk ) ); // clone and store
325  if (ok && setQualityBit_[0]) {
326  selTracks_->back().setQuality(qualityToSet_[0]);
327  if (qualityToSet_[0]==TrackBase::tight) {
328  selTracks_->back().setQuality(TrackBase::loose);
329  }
330  else if (qualityToSet_[0]==TrackBase::highPurity) {
331  selTracks_->back().setQuality(TrackBase::loose);
332  selTracks_->back().setQuality(TrackBase::tight);
333  }
334  if (!points.empty()) {
335  if (qualityToSet_[0]==TrackBase::loose) {
336  selTracks_->back().setQuality(TrackBase::looseSetWithPV);
337  }
338  else if (qualityToSet_[0]==TrackBase::highPurity) {
339  selTracks_->back().setQuality(TrackBase::looseSetWithPV);
340  selTracks_->back().setQuality(TrackBase::highPuritySetWithPV);
341  }
342  }
343  }
344  if (copyExtras_) {
345  // TrackExtras
346  selTrackExtras_->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
347  trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
348  trk.outerStateCovariance(), trk.outerDetId(),
349  trk.innerStateCovariance(), trk.innerDetId(),
350  trk.seedDirection(), trk.seedRef() ) );
351  selTracks_->back().setExtra( TrackExtraRef( rTrackExtras_, selTrackExtras_->size() - 1) );
352  TrackExtra & tx = selTrackExtras_->back();
353  tx.setResiduals(trk.residuals());
354  // TrackingRecHits
355  auto const firstHitIndex = selHits_->size();
356  for( trackingRecHit_iterator hit = trk.recHitsBegin(); hit != trk.recHitsEnd(); ++ hit ) {
357  selHits_->push_back( (*hit)->clone() );
358  }
359  tx.setHits( rHits_, firstHitIndex, selHits_->size() - firstHitIndex);
360  }
361  if (copyTrajectories_) {
362  trackRefs_[current] = TrackRef(rTracks_, selTracks_->size() - 1);
363  }
364  }
365  if ( copyTrajectories_ ) {
368  evt.getByToken(srcTass_, hTTAss);
369  evt.getByToken(srcTraj_, hTraj);
370  selTrajs_ = auto_ptr< vector<Trajectory> >(new vector<Trajectory>());
371  rTrajectories_ = evt.getRefBeforePut< vector<Trajectory> >();
372  selTTAss_ = auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection(rTrajectories_, rTracks_));
373  for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
374  Ref< vector<Trajectory> > trajRef(hTraj, i);
376  if (match != hTTAss->end()) {
377  const Ref<TrackCollection> &trkRef = match->val;
378  short oldKey = static_cast<short>(trkRef.key());
379  if (trackRefs_[oldKey].isNonnull()) {
380  selTrajs_->push_back( Trajectory(*trajRef) );
381  selTTAss_->insert ( Ref< vector<Trajectory> >(rTrajectories_, selTrajs_->size() - 1), trackRefs_[oldKey] );
382  }
383  }
384  }
385  }
386 
387  evt.put(selTracks_);
388  if (copyExtras_ ) {
389  evt.put(selTrackExtras_);
390  evt.put(selHits_);
391  }
392  if ( copyTrajectories_ ) {
393  evt.put(selTrajs_);
394  evt.put(selTTAss_);
395  }
396 }
397 
398 
401 
402 
404 
#define LogDebug(id)
std::vector< double > chi2n_no1Dmod_par_
T getParameter(std::string const &) const
std::vector< uint32_t > min_nhits_
#define dso_hidden
T getUntrackedParameter(std::string const &, T const &) const
std::vector< double > chi2n_par_
int i
Definition: DBlmapReader.cc:9
std::vector< std::vector< double > > d0_par1_
std::vector< double > max_d0NoPV_
std::vector< double > max_z0_
tuple cfg
Definition: looper.py:259
std::vector< uint32_t > min_layers_
Cuts on numbers of layers with hits/3D hits/lost hits.
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
std::vector< double > max_z0NoPV_
std::vector< bool > useMVA_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
std::vector< bool > keepAllTracks_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
std::vector< uint32_t > min_hits_bypass_
bool select(unsigned tsNum, const reco::BeamSpot &vertexBeamSpot, const TrackingRecHitCollection &recHits, const reco::Track &tk, const std::vector< Point > &points, std::vector< float > &vterr, std::vector< float > &vzerr, double mvaVal) const
return class, or -1 if rejected
bool copyTrajectories_
copy also trajectories and trajectory-&gt;track associations
double minEta_
eta restrictions
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:13
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< bool > applyAdaptedPVCuts_
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:50
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:17
std::vector< double > nSigmaZ_
std::vector< std::string > mvaType_
std::vector< unsigned int > preFilter_
key_type key() const
Accessor for product key.
Definition: Ref.h:264
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:65
edm::EDGetTokenT< TrackingRecHitCollection > hSrc_
edm::EDGetTokenT< std::vector< Trajectory > > srcTraj_
edm::EDGetTokenT< reco::VertexCollection > vertices_
std::vector< uint32_t > min_3Dlayers_
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:55
std::vector< uint32_t > max_lostLayers_
std::vector< int32_t > max_lostHitFraction_
edm::EDGetTokenT< reco::BeamSpot > beamspot_
std::vector< double > min_eta_
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
std::vector< bool > setQualityBit_
do I have to set a quality bit?
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:75
double pt() const
track transverse momentum
Definition: TrackBase.h:584
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:94
std::vector< std::vector< double > > dz_par1_
std::vector< std::vector< double > > dz_par2_
std::vector< std::vector< double > > res_par_
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:104
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
#define LogTrace(id)
std::vector< double > max_relpterr_
RefProd< PROD > getRefBeforePut()
Definition: Event.h:135
AnalyticalTrackSelector(const edm::ParameterSet &cfg)
constructor
void run(edm::Event &evt, const edm::EventSetup &es) const override
process one event
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:11
std::vector< int32_t > vtxNumber_
vertex cuts
edm::OwnVector< TrackingRecHit > TrackingRecHitCollection
collection of TrackingRecHits
edm::RefToBase< TrajectorySeed > seedRef() const
Definition: Track.h:213
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:70
void selectVertices(unsigned int tsNum, const reco::VertexCollection &vtxs, std::vector< Point > &points, std::vector< float > &vterr, std::vector< float > &vzerr) const
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:45
virtual ~AnalyticalTrackSelector()
destructor
std::vector< reco::TrackBase::TrackQuality > qualityToSet_
edm::EDGetTokenT< TrajTrackAssociationCollection > srcTass_
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:19
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:80
std::vector< std::string > forestLabel_
bool copyExtras_
copy only the tracks, not extras and rechits (for AOD)
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:60
std::vector< StringCutObjectSelector< reco::Vertex > > vertexCut_
const TrackResiduals & residuals() const
Definition: Track.h:220
std::vector< bool > applyAbsCutsIfNoPV_
PropagationDirection seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:204
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
std::vector< int32_t > max_minMissHitOutOrIn_
std::vector< double > min_MVA_
edm::EDGetTokenT< reco::TrackCollection > src_
source collection label
std::vector< std::vector< double > > d0_par2_
std::vector< double > max_d0_
Impact parameter absolute cuts.
std::vector< bool > useMVAonly_
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:99
tuple size
Write out results.
void processMVA(edm::Event &evt, const edm::EventSetup &es, const reco::BeamSpot &beamspot, const reco::VertexCollection &vertices, int selIndex, std::vector< float > &mvaVals_, bool writeIt=false) const
std::vector< double > max_eta_
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
std::vector< GBRForest * > forest_
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:109
void setResiduals(const TrackResiduals &r)
set the residuals
Definition: TrackExtra.h:179