CMS 3D CMS Logo

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