CMS 3D CMS Logo

MultiTrackSelector.cc
Go to the documentation of this file.
1 #include "MultiTrackSelector.h"
2 
8 
9 #include <Math/DistFunc.h>
10 #include <TMath.h>
11 #include <TFile.h>
12 
13 #include "powN.h"
14 
15 using namespace reco;
16 
17 MultiTrackSelector::MultiTrackSelector() { useForestFromDB_ = true; }
18 
20  : src_(consumes<reco::TrackCollection>(cfg.getParameter<edm::InputTag>("src"))),
21  hSrc_(consumes<TrackingRecHitCollection>(cfg.getParameter<edm::InputTag>("src"))),
22  beamspot_(consumes<reco::BeamSpot>(cfg.getParameter<edm::InputTag>("beamspot"))),
23  useVertices_(cfg.getParameter<bool>("useVertices")),
24  useVtxError_(cfg.getParameter<bool>("useVtxError"))
25 // now get the pset for each selector
26 {
27  if (useVertices_)
28  vertices_ = consumes<reco::VertexCollection>(cfg.getParameter<edm::InputTag>("vertices"));
29  if (useVtxError_) {
30  edm::LogWarning("MultiTRackSelector") << "you are executing buggy code, if intentional please help to fix it";
31  }
32  useAnyMVA_ = false;
33  useForestFromDB_ = true;
34  dbFileName_ = "";
35 
36  if (cfg.exists("useAnyMVA"))
37  useAnyMVA_ = cfg.getParameter<bool>("useAnyMVA");
38 
39  if (useAnyMVA_) {
40  if (cfg.exists("GBRForestFileName")) {
41  dbFileName_ = cfg.getParameter<std::string>("GBRForestFileName");
42  useForestFromDB_ = false;
43  }
44  }
45  std::vector<edm::ParameterSet> trkSelectors(cfg.getParameter<std::vector<edm::ParameterSet>>("trackSelectors"));
46  qualityToSet_.reserve(trkSelectors.size());
47  vtxNumber_.reserve(trkSelectors.size());
48  vertexCut_.reserve(trkSelectors.size());
49  res_par_.reserve(trkSelectors.size());
50  chi2n_par_.reserve(trkSelectors.size());
51  chi2n_no1Dmod_par_.reserve(trkSelectors.size());
52  d0_par1_.reserve(trkSelectors.size());
53  dz_par1_.reserve(trkSelectors.size());
54  d0_par2_.reserve(trkSelectors.size());
55  dz_par2_.reserve(trkSelectors.size());
56  applyAdaptedPVCuts_.reserve(trkSelectors.size());
57  max_d0_.reserve(trkSelectors.size());
58  max_z0_.reserve(trkSelectors.size());
59  nSigmaZ_.reserve(trkSelectors.size());
60  min_layers_.reserve(trkSelectors.size());
61  min_3Dlayers_.reserve(trkSelectors.size());
62  max_lostLayers_.reserve(trkSelectors.size());
63  min_hits_bypass_.reserve(trkSelectors.size());
64  applyAbsCutsIfNoPV_.reserve(trkSelectors.size());
65  max_d0NoPV_.reserve(trkSelectors.size());
66  max_z0NoPV_.reserve(trkSelectors.size());
67  preFilter_.reserve(trkSelectors.size());
68  max_relpterr_.reserve(trkSelectors.size());
69  min_nhits_.reserve(trkSelectors.size());
70  max_minMissHitOutOrIn_.reserve(trkSelectors.size());
71  max_lostHitFraction_.reserve(trkSelectors.size());
72  min_eta_.reserve(trkSelectors.size());
73  max_eta_.reserve(trkSelectors.size());
74  useMVA_.reserve(trkSelectors.size());
75  useMVAonly_.reserve(trkSelectors.size());
76  //mvaReaders_.reserve(trkSelectors.size());
77  min_MVA_.reserve(trkSelectors.size());
78  mvaType_.reserve(trkSelectors.size());
79  forestLabel_.reserve(trkSelectors.size());
80  forest_.reserve(trkSelectors.size());
81 
82  produces<edm::ValueMap<float>>("MVAVals");
83 
84  //foward compatibility
85  produces<MVACollection>("MVAValues");
86 
87  for (unsigned int i = 0; i < trkSelectors.size(); i++) {
88  qualityToSet_.push_back(TrackBase::undefQuality);
89  // parameters for vertex selection
90  vtxNumber_.push_back(useVertices_ ? trkSelectors[i].getParameter<int32_t>("vtxNumber") : 0);
91  vertexCut_.push_back(useVertices_ ? trkSelectors[i].getParameter<std::string>("vertexCut") : nullptr);
92  // parameters for adapted optimal cuts on chi2 and primary vertex compatibility
93  res_par_.push_back(trkSelectors[i].getParameter<std::vector<double>>("res_par"));
94  chi2n_par_.push_back(trkSelectors[i].getParameter<double>("chi2n_par"));
95  chi2n_no1Dmod_par_.push_back(trkSelectors[i].getParameter<double>("chi2n_no1Dmod_par"));
96  d0_par1_.push_back(trkSelectors[i].getParameter<std::vector<double>>("d0_par1"));
97  dz_par1_.push_back(trkSelectors[i].getParameter<std::vector<double>>("dz_par1"));
98  d0_par2_.push_back(trkSelectors[i].getParameter<std::vector<double>>("d0_par2"));
99  dz_par2_.push_back(trkSelectors[i].getParameter<std::vector<double>>("dz_par2"));
100  // Boolean indicating if adapted primary vertex compatibility cuts are to be applied.
101  applyAdaptedPVCuts_.push_back(trkSelectors[i].getParameter<bool>("applyAdaptedPVCuts"));
102  // Impact parameter absolute cuts.
103  max_d0_.push_back(trkSelectors[i].getParameter<double>("max_d0"));
104  max_z0_.push_back(trkSelectors[i].getParameter<double>("max_z0"));
105  nSigmaZ_.push_back(trkSelectors[i].getParameter<double>("nSigmaZ"));
106  // Cuts on numbers of layers with hits/3D hits/lost hits.
107  min_layers_.push_back(trkSelectors[i].getParameter<uint32_t>("minNumberLayers"));
108  min_3Dlayers_.push_back(trkSelectors[i].getParameter<uint32_t>("minNumber3DLayers"));
109  max_lostLayers_.push_back(trkSelectors[i].getParameter<uint32_t>("maxNumberLostLayers"));
110  min_hits_bypass_.push_back(trkSelectors[i].getParameter<uint32_t>("minHitsToBypassChecks"));
111  // Flag to apply absolute cuts if no PV passes the selection
112  applyAbsCutsIfNoPV_.push_back(trkSelectors[i].getParameter<bool>("applyAbsCutsIfNoPV"));
113  keepAllTracks_.push_back(trkSelectors[i].getParameter<bool>("keepAllTracks"));
114  max_relpterr_.push_back(trkSelectors[i].getParameter<double>("max_relpterr"));
115  min_nhits_.push_back(trkSelectors[i].getParameter<uint32_t>("min_nhits"));
116  max_minMissHitOutOrIn_.push_back(trkSelectors[i].existsAs<int32_t>("max_minMissHitOutOrIn")
117  ? trkSelectors[i].getParameter<int32_t>("max_minMissHitOutOrIn")
118  : 99);
119  max_lostHitFraction_.push_back(trkSelectors[i].existsAs<double>("max_lostHitFraction")
120  ? trkSelectors[i].getParameter<double>("max_lostHitFraction")
121  : 1.0);
122  min_eta_.push_back(trkSelectors[i].existsAs<double>("min_eta") ? trkSelectors[i].getParameter<double>("min_eta")
123  : -9999);
124  max_eta_.push_back(trkSelectors[i].existsAs<double>("max_eta") ? trkSelectors[i].getParameter<double>("max_eta")
125  : 9999);
126 
127  setQualityBit_.push_back(false);
128  std::string qualityStr = trkSelectors[i].getParameter<std::string>("qualityBit");
129  if (!qualityStr.empty()) {
130  setQualityBit_[i] = true;
131  qualityToSet_[i] = TrackBase::qualityByName(trkSelectors[i].getParameter<std::string>("qualityBit"));
132  }
133 
134  if (setQualityBit_[i] && (qualityToSet_[i] == TrackBase::undefQuality))
135  throw cms::Exception("Configuration")
136  << "You can't set the quality bit " << trkSelectors[i].getParameter<std::string>("qualityBit")
137  << " as it is 'undefQuality' or unknown.\n";
138 
139  if (applyAbsCutsIfNoPV_[i]) {
140  max_d0NoPV_.push_back(trkSelectors[i].getParameter<double>("max_d0NoPV"));
141  max_z0NoPV_.push_back(trkSelectors[i].getParameter<double>("max_z0NoPV"));
142  } else { //dummy values
143  max_d0NoPV_.push_back(0.);
144  max_z0NoPV_.push_back(0.);
145  }
146 
147  name_.push_back(trkSelectors[i].getParameter<std::string>("name"));
148 
149  preFilter_[i] = trkSelectors.size(); // no prefilter
150 
151  std::string pfName = trkSelectors[i].getParameter<std::string>("preFilterName");
152  if (!pfName.empty()) {
153  bool foundPF = false;
154  for (unsigned int j = 0; j < i; j++)
155  if (name_[j] == pfName) {
156  foundPF = true;
157  preFilter_[i] = j;
158  }
159  if (!foundPF)
160  throw cms::Exception("Configuration") << "Invalid prefilter name in MultiTrackSelector "
161  << trkSelectors[i].getParameter<std::string>("preFilterName");
162  }
163 
164  // produces<std::vector<int> >(name_[i]).setBranchAlias( name_[i] + "TrackQuals");
165  produces<edm::ValueMap<int>>(name_[i]).setBranchAlias(name_[i] + "TrackQuals");
166  produces<QualityMaskCollection>(name_[i]).setBranchAlias(name_[i] + "QualityMasks");
167  if (useAnyMVA_) {
168  bool thisMVA = false;
169  if (trkSelectors[i].exists("useMVA"))
170  thisMVA = trkSelectors[i].getParameter<bool>("useMVA");
171  useMVA_.push_back(thisMVA);
172  if (thisMVA) {
173  double minVal = -1;
174  if (trkSelectors[i].exists("minMVA"))
175  minVal = trkSelectors[i].getParameter<double>("minMVA");
176  min_MVA_.push_back(minVal);
177  mvaType_.push_back(trkSelectors[i].exists("mvaType") ? trkSelectors[i].getParameter<std::string>("mvaType")
178  : "Detached");
179  forestLabel_.push_back(trkSelectors[i].exists("GBRForestLabel")
180  ? trkSelectors[i].getParameter<std::string>("GBRForestLabel")
181  : "MVASelectorIter0");
182  useMVAonly_.push_back(trkSelectors[i].exists("useMVAonly") ? trkSelectors[i].getParameter<bool>("useMVAonly")
183  : false);
184  } else {
185  min_MVA_.push_back(-9999.0);
186  useMVAonly_.push_back(false);
187  mvaType_.push_back("Detached");
188  forestLabel_.push_back("MVASelectorIter0");
189  }
190  } else {
191  useMVA_.push_back(false);
192  useMVAonly_.push_back(false);
193  min_MVA_.push_back(-9999.0);
194  mvaType_.push_back("Detached");
195  forestLabel_.push_back("MVASelectorIter0");
196  }
197  }
198 }
199 
201  for (auto forest : forest_)
202  delete forest;
203 }
204 
206  if (!useForestFromDB_) {
207  TFile gbrfile(dbFileName_.c_str());
208  for (int i = 0; i < (int)forestLabel_.size(); i++) {
209  forest_[i] = (GBRForest*)gbrfile.Get(forestLabel_[i].c_str());
210  }
211  }
212 }
213 
215  using namespace std;
216  using namespace edm;
217  using namespace reco;
218 
219  // Get tracks
220  Handle<TrackCollection> hSrcTrack;
221  evt.getByToken(src_, hSrcTrack);
222 
223  const TrackCollection& srcTracks(*hSrcTrack);
224  if (hSrcTrack.failedToGet())
225  edm::LogWarning("MultiTrackSelector") << "could not get Track collection";
226 
227  // get hits in track..
229  evt.getByToken(hSrc_, hSrcHits);
230  const TrackingRecHitCollection& srcHits(*hSrcHits);
231 
232  // looking for the beam spot
234  evt.getByToken(beamspot_, hBsp);
235  const reco::BeamSpot& vertexBeamSpot(*hBsp);
236 
237  // Select good primary vertices for use in subsequent track selection
239  if (useVertices_) {
240  evt.getByToken(vertices_, hVtx);
241  if (hVtx.failedToGet())
242  edm::LogWarning("MultiTrackSelector") << "could not get Vertex collection";
243  }
244 
245  unsigned int trkSize = srcTracks.size();
246  std::vector<int> selTracksSave(qualityToSet_.size() * trkSize, 0);
247 
248  std::vector<Point> points;
249  std::vector<float> vterr, vzerr;
250  if (useVertices_)
251  selectVertices(0, *hVtx, points, vterr, vzerr);
252  //auto vtxP = points.empty() ? vertexBeamSpot.position() : points[0]; // rare, very rare, still happens!
253  for (unsigned int i = 0; i < qualityToSet_.size(); i++) {
254  std::vector<float> mvaVals_(srcTracks.size(), -99.f);
255  processMVA(evt, es, vertexBeamSpot, *(hVtx.product()), i, mvaVals_, i == 0 ? true : false);
256  std::vector<int> selTracks(trkSize, 0);
257  auto selTracksValueMap = std::make_unique<edm::ValueMap<int>>();
258  edm::ValueMap<int>::Filler filler(*selTracksValueMap);
259 
260  if (useVertices_)
261  selectVertices(i, *hVtx, points, vterr, vzerr);
262 
263  // Loop over tracks
264  size_t current = 0;
265  for (TrackCollection::const_iterator it = srcTracks.begin(), ed = srcTracks.end(); it != ed; ++it, ++current) {
266  const Track& trk = *it;
267  // Check if this track passes cuts
268 
269  LogTrace("TrackSelection") << "ready to check track with pt=" << trk.pt();
270 
271  //already removed
272  bool ok = true;
273  if (preFilter_[i] < i && selTracksSave[preFilter_[i] * trkSize + current] < 0) {
274  selTracks[current] = -1;
275  ok = false;
276  if (!keepAllTracks_[i])
277  continue;
278  } else {
279  float mvaVal = 0;
280  if (useAnyMVA_)
281  mvaVal = mvaVals_[current];
282  ok = select(i, vertexBeamSpot, srcHits, trk, points, vterr, vzerr, mvaVal);
283  if (!ok) {
284  LogTrace("TrackSelection") << "track with pt=" << trk.pt() << " NOT selected";
285  if (!keepAllTracks_[i]) {
286  selTracks[current] = -1;
287  continue;
288  }
289  } else
290  LogTrace("TrackSelection") << "track with pt=" << trk.pt() << " selected";
291  }
292 
293  if (preFilter_[i] < i) {
294  selTracks[current] = selTracksSave[preFilter_[i] * trkSize + current];
295  } else {
296  selTracks[current] = trk.qualityMask();
297  }
298  if (ok && setQualityBit_[i]) {
299  selTracks[current] = (selTracks[current] | (1 << qualityToSet_[i]));
300  if (qualityToSet_[i] == TrackBase::tight) {
301  selTracks[current] = (selTracks[current] | (1 << TrackBase::loose));
302  } else if (qualityToSet_[i] == TrackBase::highPurity) {
303  selTracks[current] = (selTracks[current] | (1 << TrackBase::loose));
304  selTracks[current] = (selTracks[current] | (1 << TrackBase::tight));
305  }
306  if (!points.empty()) {
307  if (qualityToSet_[i] == TrackBase::loose) {
308  selTracks[current] = (selTracks[current] | (1 << TrackBase::looseSetWithPV));
309  } else if (qualityToSet_[i] == TrackBase::highPurity) {
310  selTracks[current] = (selTracks[current] | (1 << TrackBase::looseSetWithPV));
311  selTracks[current] = (selTracks[current] | (1 << TrackBase::highPuritySetWithPV));
312  }
313  }
314  }
315  }
316  for (unsigned int j = 0; j < trkSize; j++)
317  selTracksSave[j + i * trkSize] = selTracks[j];
318  filler.insert(hSrcTrack, selTracks.begin(), selTracks.end());
319  filler.fill();
320 
321  // evt.put(std::move(selTracks),name_[i]);
322  evt.put(std::move(selTracksValueMap), name_[i]);
323  for (auto& q : selTracks)
324  q = std::max(q, 0);
325  auto quals = std::make_unique<QualityMaskCollection>(selTracks.begin(), selTracks.end());
326  evt.put(std::move(quals), name_[i]);
327  }
328 }
329 
330 bool MultiTrackSelector::select(unsigned int tsNum,
331  const reco::BeamSpot& vertexBeamSpot,
333  const reco::Track& tk,
334  const std::vector<Point>& points,
335  std::vector<float>& vterr,
336  std::vector<float>& vzerr,
337  double mvaVal) const {
338  // Decide if the given track passes selection cuts.
339 
340  using namespace std;
341 
342  //cuts on number of valid hits
343  auto nhits = tk.numberOfValidHits();
344  if (nhits >= min_hits_bypass_[tsNum])
345  return true;
346  if (nhits < min_nhits_[tsNum])
347  return false;
348 
349  if (tk.ndof() < 1E-5)
350  return false;
351 
353  //Adding the MVA selection before any other cut//
355  if (useAnyMVA_ && useMVA_[tsNum]) {
356  if (useMVAonly_[tsNum])
357  return mvaVal > min_MVA_[tsNum];
358  if (mvaVal < min_MVA_[tsNum])
359  return false;
360  }
362  //End of MVA selection section//
364 
365  // Cuts on numbers of layers with hits/3D hits/lost hits.
367  uint32_t nlayers3D =
370  LogDebug("TrackSelection") << "cuts on nlayers: " << nlayers << " " << nlayers3D << " " << nlayersLost << " vs "
371  << min_layers_[tsNum] << " " << min_3Dlayers_[tsNum] << " " << max_lostLayers_[tsNum];
372  if (nlayers < min_layers_[tsNum])
373  return false;
374  if (nlayers3D < min_3Dlayers_[tsNum])
375  return false;
376  if (nlayersLost > max_lostLayers_[tsNum])
377  return false;
378  LogTrace("TrackSelection") << "cuts on nlayers passed";
379 
380  float chi2n = tk.normalizedChi2();
381  float chi2n_no1Dmod = chi2n;
382 
383  int count1dhits = 0;
384  auto ith = tk.extra()->firstRecHit();
385  auto edh = ith + tk.recHitsSize();
386  for (; ith < edh; ++ith) {
387  const TrackingRecHit& hit = recHits[ith];
388  if (hit.dimension() == 1)
389  ++count1dhits;
390  }
391  if (count1dhits > 0) {
392  float chi2 = tk.chi2();
393  float ndof = tk.ndof();
394  chi2n = (chi2 + count1dhits) / float(ndof + count1dhits);
395  }
396  // For each 1D rechit, the chi^2 and ndof is increased by one. This is a way of retaining approximately
397  // the same normalized chi^2 distribution as with 2D rechits.
398  if (chi2n > chi2n_par_[tsNum] * nlayers)
399  return false;
400 
401  if (chi2n_no1Dmod > chi2n_no1Dmod_par_[tsNum] * nlayers)
402  return false;
403 
404  // Get track parameters
405  float pt = std::max(float(tk.pt()), 0.000001f);
406  float eta = tk.eta();
407  if (eta < min_eta_[tsNum] || eta > max_eta_[tsNum])
408  return false;
409 
410  //cuts on relative error on pt
411  float relpterr = float(tk.ptError()) / pt;
412  if (relpterr > max_relpterr_[tsNum])
413  return false;
414 
417  int minLost = std::min(lostIn, lostOut);
418  if (minLost > max_minMissHitOutOrIn_[tsNum])
419  return false;
420  float lostMidFrac = tk.numberOfLostHits() / (tk.numberOfValidHits() + tk.numberOfLostHits());
421  if (lostMidFrac > max_lostHitFraction_[tsNum])
422  return false;
423 
424  //other track parameters
425  float d0 = -tk.dxy(vertexBeamSpot.position()), d0E = tk.d0Error(), dz = tk.dz(vertexBeamSpot.position()),
426  dzE = tk.dzError();
427 
428  // parametrized d0 resolution for the track pt
429  float nomd0E = sqrt(res_par_[tsNum][0] * res_par_[tsNum][0] + (res_par_[tsNum][1] / pt) * (res_par_[tsNum][1] / pt));
430  // parametrized z0 resolution for the track pt and eta
431  float nomdzE = nomd0E * (std::cosh(eta));
432 
433  float dzCut = std::min(powN(dz_par1_[tsNum][0] * nlayers, int(dz_par1_[tsNum][1] + 0.5)) * nomdzE,
434  powN(dz_par2_[tsNum][0] * nlayers, int(dz_par2_[tsNum][1] + 0.5)) * dzE);
435  float d0Cut = std::min(powN(d0_par1_[tsNum][0] * nlayers, int(d0_par1_[tsNum][1] + 0.5)) * nomd0E,
436  powN(d0_par2_[tsNum][0] * nlayers, int(d0_par2_[tsNum][1] + 0.5)) * d0E);
437 
438  // ---- PrimaryVertex compatibility cut
439  bool primaryVertexZCompatibility(false);
440  bool primaryVertexD0Compatibility(false);
441 
442  if (points.empty()) { //If not primaryVertices are reconstructed, check just the compatibility with the BS
443  //z0 within (n sigma + dzCut) of the beam spot z, if no good vertex is found
444  if (abs(dz) < hypot(vertexBeamSpot.sigmaZ() * nSigmaZ_[tsNum], dzCut))
445  primaryVertexZCompatibility = true;
446  // d0 compatibility with beam line
447  if (abs(d0) < d0Cut)
448  primaryVertexD0Compatibility = true;
449  }
450 
451  int iv = 0;
452  for (std::vector<Point>::const_iterator point = points.begin(), end = points.end(); point != end; ++point) {
453  LogTrace("TrackSelection") << "Test track w.r.t. vertex with z position " << point->z();
454  if (primaryVertexZCompatibility && primaryVertexD0Compatibility)
455  break;
456  float dzPV = tk.dz(*point); //re-evaluate the dz with respect to the vertex position
457  float d0PV = tk.dxy(*point); //re-evaluate the dxy with respect to the vertex position
458  if (useVtxError_) {
459  float dzErrPV = std::sqrt(dzE * dzE + vzerr[iv] * vzerr[iv]); // include vertex error in z
460  float d0ErrPV = std::sqrt(d0E * d0E + vterr[iv] * vterr[iv]); // include vertex error in xy
461  iv++;
462  if (abs(dzPV) < dz_par1_[tsNum][0] * pow(nlayers, dz_par1_[tsNum][1]) * nomdzE &&
463  abs(dzPV) < dz_par2_[tsNum][0] * pow(nlayers, dz_par2_[tsNum][1]) * dzErrPV && abs(dzPV) < max_z0_[tsNum])
464  primaryVertexZCompatibility = true;
465  if (abs(d0PV) < d0_par1_[tsNum][0] * pow(nlayers, d0_par1_[tsNum][1]) * nomd0E &&
466  abs(d0PV) < d0_par2_[tsNum][0] * pow(nlayers, d0_par2_[tsNum][1]) * d0ErrPV && abs(d0PV) < max_d0_[tsNum])
467  primaryVertexD0Compatibility = true;
468  } else {
469  if (abs(dzPV) < dzCut)
470  primaryVertexZCompatibility = true;
471  if (abs(d0PV) < d0Cut)
472  primaryVertexD0Compatibility = true;
473  }
474  LogTrace("TrackSelection") << "distances " << dzPV << " " << d0PV << " vs " << dzCut << " " << d0Cut;
475  }
476 
477  if (points.empty() && applyAbsCutsIfNoPV_[tsNum]) {
478  if (abs(dz) > max_z0NoPV_[tsNum] || abs(d0) > max_d0NoPV_[tsNum])
479  return false;
480  } else {
481  // Absolute cuts on all tracks impact parameters with respect to beam-spot.
482  // If BS is not compatible, verify if at least the reco-vertex is compatible (useful for incorrect BS settings)
483  if (abs(d0) > max_d0_[tsNum] && !primaryVertexD0Compatibility)
484  return false;
485  LogTrace("TrackSelection") << "absolute cuts on d0 passed";
486  if (abs(dz) > max_z0_[tsNum] && !primaryVertexZCompatibility)
487  return false;
488  LogTrace("TrackSelection") << "absolute cuts on dz passed";
489  }
490 
491  LogTrace("TrackSelection") << "cuts on PV: apply adapted PV cuts? " << applyAdaptedPVCuts_[tsNum]
492  << " d0 compatibility? " << primaryVertexD0Compatibility << " z compatibility? "
493  << primaryVertexZCompatibility;
494 
495  if (applyAdaptedPVCuts_[tsNum]) {
496  return (primaryVertexD0Compatibility && primaryVertexZCompatibility);
497  } else {
498  return true;
499  }
500 }
501 
502 void MultiTrackSelector::selectVertices(unsigned int tsNum,
503  const reco::VertexCollection& vtxs,
504  std::vector<Point>& points,
505  std::vector<float>& vterr,
506  std::vector<float>& vzerr) const {
507  // Select good primary vertices
508  using namespace reco;
509  int32_t toTake = vtxNumber_[tsNum];
510  for (VertexCollection::const_iterator it = vtxs.begin(), ed = vtxs.end(); it != ed; ++it) {
511  LogDebug("SelectVertex") << " select vertex with z position " << it->z() << " " << it->chi2() << " " << it->ndof()
512  << " " << TMath::Prob(it->chi2(), static_cast<int32_t>(it->ndof()));
513  Vertex vtx = *it;
514  bool pass = vertexCut_[tsNum](vtx);
515  if (pass) {
516  points.push_back(it->position());
517  vterr.push_back(sqrt(it->yError() * it->xError()));
518  vzerr.push_back(it->zError());
519  LogTrace("SelectVertex") << " SELECTED vertex with z position " << it->z();
520  toTake--;
521  if (toTake == 0)
522  break;
523  }
524  }
525 }
526 
528  const edm::EventSetup& es,
529  const reco::BeamSpot& beamspot,
531  int selIndex,
532  std::vector<float>& mvaVals_,
533  bool writeIt) const {
534  using namespace std;
535  using namespace edm;
536  using namespace reco;
537 
538  // Get tracks
539  Handle<TrackCollection> hSrcTrack;
540  evt.getByToken(src_, hSrcTrack);
541  const TrackCollection& srcTracks(*hSrcTrack);
542  RefToBaseProd<Track> rtbpTrackCollection(hSrcTrack);
543  assert(mvaVals_.size() == srcTracks.size());
544 
545  // get hits in track..
547  evt.getByToken(hSrc_, hSrcHits);
548  const TrackingRecHitCollection& srcHits(*hSrcHits);
549 
550  auto mvaValValueMap = std::make_unique<edm::ValueMap<float>>();
551  edm::ValueMap<float>::Filler mvaFiller(*mvaValValueMap);
552 
553  if (!useAnyMVA_ && writeIt) {
554  // mvaVals_ already initalized...
555  mvaFiller.insert(hSrcTrack, mvaVals_.begin(), mvaVals_.end());
556  mvaFiller.fill();
557  evt.put(std::move(mvaValValueMap), "MVAVals");
558  auto mvas = std::make_unique<MVACollection>(mvaVals_.begin(), mvaVals_.end());
559  evt.put(std::move(mvas), "MVAValues");
560  return;
561  }
562 
563  if (!useMVA_[selIndex] && !writeIt)
564  return;
565 
566  size_t current = 0;
567  for (TrackCollection::const_iterator it = srcTracks.begin(), ed = srcTracks.end(); it != ed; ++it, ++current) {
568  const Track& trk = *it;
569  RefToBase<Track> trackRef(rtbpTrackCollection, current);
570  auto tmva_ndof_ = trk.ndof();
571  auto tmva_nlayers_ = trk.hitPattern().trackerLayersWithMeasurement();
572  auto tmva_nlayers3D_ =
575  float chi2n = trk.normalizedChi2();
576  float chi2n_no1Dmod = chi2n;
577 
578  int count1dhits = 0;
579  auto ith = trk.extra()->firstRecHit();
580  auto edh = ith + trk.recHitsSize();
581  for (; ith < edh; ++ith) {
582  const TrackingRecHit& hit = srcHits[ith];
583  if (hit.dimension() == 1)
584  ++count1dhits;
585  }
586  if (count1dhits > 0) {
587  float chi2 = trk.chi2();
588  float ndof = trk.ndof();
589  chi2n = (chi2 + count1dhits) / float(ndof + count1dhits);
590  }
591  auto tmva_chi2n_ = chi2n;
592  auto tmva_chi2n_no1dmod_ = chi2n_no1Dmod;
593  auto tmva_eta_ = trk.eta();
594  auto tmva_relpterr_ = float(trk.ptError()) / std::max(float(trk.pt()), 0.000001f);
595  auto tmva_nhits_ = trk.numberOfValidHits();
598  auto tmva_minlost_ = std::min(lostIn, lostOut);
599  auto tmva_lostmidfrac_ = trk.numberOfLostHits() / (trk.numberOfValidHits() + trk.numberOfLostHits());
600  auto tmva_absd0_ = fabs(-trk.dxy(beamspot.position()));
601  auto tmva_absdz_ = fabs(trk.dz(beamspot.position()));
602  Point bestVertex = getBestVertex(trackRef, vertices);
603  auto tmva_absd0PV_ = fabs(trk.dxy(bestVertex));
604  auto tmva_absdzPV_ = fabs(trk.dz(bestVertex));
605  auto tmva_pt_ = trk.pt();
606 
607  GBRForest const* forest = forest_[selIndex];
608  if (useForestFromDB_) {
609  edm::ESHandle<GBRForest> forestHandle;
610  es.get<GBRWrapperRcd>().get(forestLabel_[selIndex], forestHandle);
611  forest = forestHandle.product();
612  }
613 
614  float gbrVals_[16];
615  gbrVals_[0] = tmva_pt_;
616  gbrVals_[1] = tmva_lostmidfrac_;
617  gbrVals_[2] = tmva_minlost_;
618  gbrVals_[3] = tmva_nhits_;
619  gbrVals_[4] = tmva_relpterr_;
620  gbrVals_[5] = tmva_eta_;
621  gbrVals_[6] = tmva_chi2n_no1dmod_;
622  gbrVals_[7] = tmva_chi2n_;
623  gbrVals_[8] = tmva_nlayerslost_;
624  gbrVals_[9] = tmva_nlayers3D_;
625  gbrVals_[10] = tmva_nlayers_;
626  gbrVals_[11] = tmva_ndof_;
627  gbrVals_[12] = tmva_absd0PV_;
628  gbrVals_[13] = tmva_absdzPV_;
629  gbrVals_[14] = tmva_absdz_;
630  gbrVals_[15] = tmva_absd0_;
631 
632  if (mvaType_[selIndex] == "Prompt") {
633  auto gbrVal = forest->GetClassifier(gbrVals_);
634  mvaVals_[current] = gbrVal;
635  } else {
636  float detachedGbrVals_[12];
637  for (int jjj = 0; jjj < 12; jjj++)
638  detachedGbrVals_[jjj] = gbrVals_[jjj];
639  auto gbrVal = forest->GetClassifier(detachedGbrVals_);
640  mvaVals_[current] = gbrVal;
641  }
642  }
643 
644  if (writeIt) {
645  mvaFiller.insert(hSrcTrack, mvaVals_.begin(), mvaVals_.end());
646  mvaFiller.fill();
647  evt.put(std::move(mvaValValueMap), "MVAVals");
648  auto mvas = std::make_unique<MVACollection>(mvaVals_.begin(), mvaVals_.end());
649  evt.put(std::move(mvas), "MVAValues");
650  }
651 }
652 
654  Point p(0, 0, -99999);
655  Point p_dz(0, 0, -99999);
656  float bestWeight = 0;
657  float dzmin = 10000;
658  bool weightMatch = false;
659  for (auto const& vertex : vertices) {
660  float w = vertex.trackWeight(track);
661  const Point& v_pos = vertex.position();
662  if (w > bestWeight) {
663  p = v_pos;
664  bestWeight = w;
665  weightMatch = true;
666  }
667  float dz = fabs(track.get()->dz(v_pos));
668  if (dz < dzmin) {
669  p_dz = v_pos;
670  dzmin = dz;
671  }
672  }
673  if (weightMatch)
674  return p;
675  else
676  return p_dz;
677 }
678 
681 
#define LogDebug(id)
value_type const * get() const
Definition: RefToBase.h:209
T getParameter(std::string const &) const
std::vector< uint32_t > min_nhits_
std::vector< double > max_d0NoPV_
double d0Error() const
error on d0
Definition: TrackBase.h:719
std::vector< double > max_z0_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
std::vector< uint32_t > min_layers_
Cuts on numbers of layers with hits/3D hits/lost hits.
~MultiTrackSelector() override
destructor
std::vector< std::string > name_
std::vector< double > max_z0NoPV_
MultiTrackSelector()
constructor
const double w
Definition: UKUtility.cc:23
const TrackExtraRef & extra() const
reference to "extra" object
Definition: Track.h:139
std::vector< bool > useMVA_
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:572
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
std::vector< bool > keepAllTracks_
size_t recHitsSize() const
Get number of RecHits. (Warning, this includes invalid hits, which are not physical hits)...
Definition: Track.h:97
Point getBestVertex(const reco::TrackBaseRef, const reco::VertexCollection) const
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
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< bool > applyAdaptedPVCuts_
unsigned short numberOfLostHits() const
number of cases where track crossed a layer without getting a hit.
Definition: TrackBase.h:743
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
std::vector< double > chi2n_no1Dmod_par_
virtual void run(edm::Event &evt, const edm::EventSetup &es) const
std::vector< double > nSigmaZ_
std::vector< std::string > mvaType_
std::vector< unsigned int > preFilter_
int pixelLayersWithMeasurement() const
Definition: HitPattern.cc:492
edm::EDGetTokenT< TrackingRecHitCollection > hSrc_
int trackerLayersWithMeasurement() const
Definition: HitPattern.cc:513
edm::EDGetTokenT< reco::VertexCollection > vertices_
std::vector< uint32_t > min_3Dlayers_
int numberOfLostTrackerHits(HitCategory category) const
Definition: HitPattern.h:863
std::vector< uint32_t > max_lostLayers_
std::vector< GBRForest * > forest_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:617
int numberOfValidStripLayersWithMonoAndStereo(uint16_t stripdet, uint16_t layer) const
Definition: HitPattern.cc:348
std::vector< int32_t > max_lostHitFraction_
edm::EDGetTokenT< reco::BeamSpot > beamspot_
std::vector< double > min_eta_
double chi2() const
chi-squared of the fit
Definition: TrackBase.h:566
double ndof() const
number of degrees of freedom of the fit
Definition: TrackBase.h:569
T sqrt(T t)
Definition: SSEVec.h:19
virtual int dimension() const =0
std::vector< bool > setQualityBit_
do I have to set a quality bit?
double pt() const
track transverse momentum
Definition: TrackBase.h:602
std::vector< std::vector< double > > dz_par1_
double ptError() const
error on Pt (set to 1000 TeV if charge==0 for safety)
Definition: TrackBase.h:696
int qualityMask() const
Definition: TrackBase.h:773
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
unsigned short numberOfValidHits() const
number of valid hits found
Definition: TrackBase.h:740
std::vector< std::vector< double > > d0_par1_
#define end
Definition: vmac.h:39
T min(T a, T b)
Definition: MathUtil.h:58
#define LogTrace(id)
std::vector< double > max_relpterr_
void beginStream(edm::StreamID) final
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:596
double dzError() const
error on dz
Definition: TrackBase.h:725
bool failedToGet() const
Definition: HandleBase.h:72
std::vector< int32_t > vtxNumber_
vertex cuts
void selectVertices(unsigned int tsNum, const reco::VertexCollection &vtxs, std::vector< Point > &points, std::vector< float > &vterr, std::vector< float > &vzerr) const
T const * product() const
Definition: Handle.h:69
std::vector< reco::TrackBase::TrackQuality > qualityToSet_
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:483
double sigmaZ() const
sigma z
Definition: BeamSpot.h:76
std::vector< std::string > forestLabel_
int trackerLayersWithoutMeasurement(HitCategory category) const
Definition: HitPattern.cc:532
fixed size matrix
HLT enums.
Structure Point Contains parameters of Gaussian fits to DMRs.
Definition: DMRtrends.cc:57
T get() const
Definition: EventSetup.h:73
std::vector< StringCutObjectSelector< reco::Vertex > > vertexCut_
std::vector< std::vector< double > > dz_par2_
std::vector< bool > applyAbsCutsIfNoPV_
const Point & position() const
position
Definition: BeamSpot.h:59
std::vector< int32_t > max_minMissHitOutOrIn_
std::vector< double > min_MVA_
math::XYZPoint Point
edm::EDGetTokenT< reco::TrackCollection > src_
source collection label
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:587
double GetClassifier(const float *vector) const
Definition: GBRForest.h:34
std::vector< double > max_d0_
Impact parameter absolute cuts.
std::vector< bool > useMVAonly_
T const * product() const
Definition: ESHandle.h:86
std::vector< std::vector< double > > res_par_
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
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30
def move(src, dest)
Definition: eostools.py:511
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
std::vector< double > max_eta_
std::vector< double > chi2n_par_
std::vector< std::vector< double > > d0_par2_