CMS 3D CMS Logo

TrackCutClassifier.cc
Go to the documentation of this file.
2 
4 
6 
8 
9 #include <cassert>
10 
11 #include "powN.h"
12 
13 namespace {
14 
15  void fillArrayF(float* x, const edm::ParameterSet& cfg, const char* name) {
16  auto v = cfg.getParameter<std::vector<double>>(name);
17  assert(v.size() == 3);
18  std::copy(std::begin(v), std::end(v), x);
19  }
20 
21  void fillArrayI(int* x, const edm::ParameterSet& cfg, const char* name) {
22  auto v = cfg.getParameter<std::vector<int>>(name);
23  assert(v.size() == 3);
24  std::copy(std::begin(v), std::end(v), x);
25  }
26 
27  // fake mva value to return for loose,tight,hp
28  constexpr float mvaVal[3] = {-.5, .5, 1.};
29 
30  template <typename T, typename Comp>
31  inline float cut(T val, const T* cuts, Comp comp) {
32  for (int i = 2; i >= 0; --i) {
33  if (comp(val, cuts[i]))
34  return mvaVal[i];
35  }
36  return -1.f;
37  }
38 
39  inline float chi2n(reco::Track const& tk) { return tk.normalizedChi2(); }
40 
41  inline float relPtErr(reco::Track const& tk) {
42  return (tk.pt() != 0. ? float(tk.ptError()) / float(tk.pt()) : 9999999.);
43  }
44 
45  inline int lostLayers(reco::Track const& tk) {
47  }
48 
49  inline int n3DLayers(reco::Track const& tk, bool isHLT) {
50  uint32_t nlayers3D = tk.hitPattern().pixelLayersWithMeasurement();
51  if (!isHLT)
53  else {
54  size_t count3D = 0;
55  for (auto it = tk.recHitsBegin(), et = tk.recHitsEnd(); it != et; ++it) {
56  const TrackingRecHit* hit = (*it);
58  continue;
59 
60  if (hit->dimension() == 2) {
61  auto const& thit = static_cast<BaseTrackerRecHit const&>(*hit);
62  if (thit.isMatched())
63  count3D++;
64  }
65  }
66  nlayers3D += count3D;
67  }
68  return nlayers3D;
69  }
70 
71  inline int nHits(reco::Track const& tk) { return tk.numberOfValidHits(); }
72 
73  inline int nPixelHits(reco::Track const& tk) { return tk.hitPattern().numberOfValidPixelHits(); }
74 
75  inline float dz(reco::Track const& trk, Point const& bestVertex) { return std::abs(trk.dz(bestVertex)); }
76  inline float dr(reco::Track const& trk, Point const& bestVertex) { return std::abs(trk.dxy(bestVertex)); }
77 
78  inline void dzCut_par1(reco::Track const& trk, int& nLayers, const float* par, const int* exp, float dzCut[]) {
79  float dzE = trk.dzError();
80  for (int i = 2; i >= 0; --i) {
81  dzCut[i] = powN(par[i] * nLayers, exp[i]) * dzE;
82  }
83  }
84  inline void drCut_par1(reco::Track const& trk, int& nLayers, const float* par, const int* exp, float drCut[]) {
85  float drE = trk.d0Error();
86  for (int i = 2; i >= 0; --i) {
87  drCut[i] = powN(par[i] * nLayers, exp[i]) * drE;
88  }
89  }
90 
91  inline void dzCut_par2(reco::Track const& trk,
92  int& nLayers,
93  const float* par,
94  const int* exp,
95  const float* d0err,
96  const float* d0err_par,
97  float dzCut[]) {
98  float pt = float(trk.pt());
99  float p = float(trk.p());
100 
101  for (int i = 2; i >= 0; --i) {
102  // parametrized d0 resolution for the track pt
103  float nomd0E = sqrt(d0err[i] * d0err[i] + (d0err_par[i] / pt) * (d0err_par[i] / pt));
104  // parametrized z0 resolution for the track pt and eta
105  float nomdzE = nomd0E * (p / pt); // cosh(eta):=abs(p)/pt
106 
107  dzCut[i] = powN(par[i] * nLayers, exp[i]) * nomdzE;
108  }
109  }
110  inline void drCut_par2(reco::Track const& trk,
111  int& nLayers,
112  const float* par,
113  const int* exp,
114  const float* d0err,
115  const float* d0err_par,
116  float drCut[]) {
117  float pt = trk.pt();
118 
119  for (int i = 2; i >= 0; --i) {
120  // parametrized d0 resolution for the track pt
121  float nomd0E = sqrt(d0err[i] * d0err[i] + (d0err_par[i] / pt) * (d0err_par[i] / pt));
122 
123  drCut[i] = powN(par[i] * nLayers, exp[i]) * nomd0E;
124  }
125  }
126 
127  inline void dzCut_wPVerror_par(reco::Track const& trk,
128  int& nLayers,
129  const float* par,
130  const int* exp,
131  Point const& bestVertexError,
132  float dzCut[]) {
133  float dzE = trk.dzError();
134  float zPVerr = bestVertexError.z();
135 
136  float dzErrPV = std::sqrt(dzE * dzE + zPVerr * zPVerr);
137  for (int i = 2; i >= 0; --i) {
138  dzCut[i] = par[i] * dzErrPV;
139  if (exp[i] != 0)
140  dzCut[i] *= pow(nLayers, exp[i]);
141  }
142  }
143 
144  inline void drCut_wPVerror_par(reco::Track const& trk,
145  int& nLayers,
146  const float* par,
147  const int* exp,
148  Point const& bestVertexError,
149  float drCut[]) {
150  float drE = trk.d0Error();
151  // shouldn't it be bestVertex.xError()*bestVertex.xError()+bestVertex.yError()*bestVertex.yError() ?!?!?
152  float rPVerr = sqrt(bestVertexError.x() * bestVertexError.y());
153 
154  float drErrPV = std::sqrt(drE * drE + rPVerr * rPVerr);
155  for (int i = 2; i >= 0; --i) {
156  drCut[i] = par[i] * drErrPV;
157  if (exp[i] != 0)
158  drCut[i] *= pow(nLayers, exp[i]);
159  }
160  }
161 
162  struct Cuts {
164  isHLT = cfg.getParameter<bool>("isHLT");
165  fillArrayF(minNdof, cfg, "minNdof");
166  fillArrayF(maxChi2, cfg, "maxChi2");
167  fillArrayF(maxChi2n, cfg, "maxChi2n");
168  fillArrayI(minHits4pass, cfg, "minHits4pass");
169  fillArrayI(minHits, cfg, "minHits");
170  fillArrayI(minPixelHits, cfg, "minPixelHits");
171  fillArrayI(min3DLayers, cfg, "min3DLayers");
172  fillArrayI(minLayers, cfg, "minLayers");
173  fillArrayI(maxLostLayers, cfg, "maxLostLayers");
174  fillArrayF(maxRelPtErr, cfg, "maxRelPtErr");
175  minNVtxTrk = cfg.getParameter<int>("minNVtxTrk");
176  fillArrayF(maxDz, cfg, "maxDz");
177  fillArrayF(maxDzWrtBS, cfg, "maxDzWrtBS");
178  fillArrayF(maxDr, cfg, "maxDr");
179  edm::ParameterSet dz_par = cfg.getParameter<edm::ParameterSet>("dz_par");
180  fillArrayI(dz_exp, dz_par, "dz_exp");
181  fillArrayF(dz_par1, dz_par, "dz_par1");
182  fillArrayF(dz_par2, dz_par, "dz_par2");
183  fillArrayF(dzWPVerr_par, dz_par, "dzWPVerr_par");
184  edm::ParameterSet dr_par = cfg.getParameter<edm::ParameterSet>("dr_par");
185  fillArrayI(dr_exp, dr_par, "dr_exp");
186  fillArrayF(dr_par1, dr_par, "dr_par1");
187  fillArrayF(dr_par2, dr_par, "dr_par2");
188  fillArrayF(d0err, dr_par, "d0err");
189  fillArrayF(d0err_par, dr_par, "d0err_par");
190  fillArrayF(drWPVerr_par, dr_par, "drWPVerr_par");
191  }
192 
193  void beginStream() {}
194  void initEvent(const edm::EventSetup&) {}
195 
196  float operator()(reco::Track const& trk,
197  reco::BeamSpot const& beamSpot,
198  reco::VertexCollection const& vertices) const {
199  float ret = 1.f;
200  // minimum number of hits for by-passing the other checks
201  if (minHits4pass[0] < std::numeric_limits<int>::max()) {
202  ret = std::min(ret, cut(nHits(trk), minHits4pass, std::greater_equal<int>()));
203  if (ret == 1.f)
204  return ret;
205  }
206 
208  ret = std::min(ret, cut(relPtErr(trk), maxRelPtErr, std::less_equal<float>()));
209  if (ret == -1.f)
210  return ret;
211  }
212 
213  ret = std::min(ret, cut(float(trk.ndof()), minNdof, std::greater_equal<float>()));
214  if (ret == -1.f)
215  return ret;
216 
218  ret = std::min(ret, cut(nLayers, minLayers, std::greater_equal<int>()));
219  if (ret == -1.f)
220  return ret;
221 
222  ret = std::min(ret, cut(chi2n(trk) / float(nLayers), maxChi2n, std::less_equal<float>()));
223  if (ret == -1.f)
224  return ret;
225 
226  ret = std::min(ret, cut(chi2n(trk), maxChi2, std::less_equal<float>()));
227  if (ret == -1.f)
228  return ret;
229 
230  ret = std::min(ret, cut(n3DLayers(trk, isHLT), min3DLayers, std::greater_equal<int>()));
231  if (ret == -1.f)
232  return ret;
233 
234  ret = std::min(ret, cut(nHits(trk), minHits, std::greater_equal<int>()));
235  if (ret == -1.f)
236  return ret;
237 
238  ret = std::min(ret, cut(nPixelHits(trk), minPixelHits, std::greater_equal<int>()));
239  if (ret == -1.f)
240  return ret;
241 
242  ret = std::min(ret, cut(lostLayers(trk), maxLostLayers, std::less_equal<int>()));
243  if (ret == -1.f)
244  return ret;
245 
246  // original dz and dr cut
248  // if not primaryVertices are reconstructed, check compatibility w.r.t. beam spot
249  // min number of tracks [2 (=default) for offline, 3 for HLT]
250  Point bestVertex = getBestVertex(trk, vertices, minNVtxTrk);
251  float maxDzcut[3];
252  std::copy(std::begin(maxDz), std::end(maxDz), std::begin(maxDzcut));
253  if (bestVertex.z() < -99998.) {
254  bestVertex = beamSpot.position();
255  std::copy(std::begin(maxDzWrtBS), std::end(maxDzWrtBS), std::begin(maxDzcut));
256  }
257  ret = std::min(ret, cut(dr(trk, bestVertex), maxDr, std::less<float>()));
258  if (ret == -1.f)
259  return ret;
260 
261  ret = std::min(ret, cut(dz(trk, bestVertex), maxDzcut, std::less<float>()));
262  if (ret == -1.f)
263  return ret;
264  }
265 
266  // parametrized dz and dr cut by using PV error
268  Point bestVertexError(-1., -1., -1.);
269  // min number of tracks [2 (=default) for offline, 3 for HLT]
270  Point bestVertex = getBestVertex_withError(trk, vertices, bestVertexError, minNVtxTrk);
271 
272  float maxDz_par[3];
273  float maxDr_par[3];
274  dzCut_wPVerror_par(trk, nLayers, dzWPVerr_par, dz_exp, bestVertexError, maxDz_par);
275  drCut_wPVerror_par(trk, nLayers, drWPVerr_par, dr_exp, bestVertexError, maxDr_par);
276 
277  ret = std::min(ret, cut(dr(trk, bestVertex), maxDr_par, std::less<float>()));
278  if (ret == -1.f)
279  return ret;
280 
281  ret = std::min(ret, cut(dz(trk, bestVertex), maxDr_par, std::less<float>()));
282  if (ret == -1.f)
283  return ret;
284  }
285 
286  // parametrized dz and dr cut by using their error
288  float maxDz_par1[3];
289  float maxDr_par1[3];
290  dzCut_par1(trk, nLayers, dz_par1, dz_exp, maxDz_par1);
291  drCut_par1(trk, nLayers, dr_par1, dr_exp, maxDr_par1);
292 
293  float maxDz_par[3];
294  float maxDr_par[3];
295  std::copy(std::begin(maxDz_par1), std::end(maxDz_par1), std::begin(maxDz_par));
296  std::copy(std::begin(maxDr_par1), std::end(maxDr_par1), std::begin(maxDr_par));
297 
298  // parametrized dz and dr cut by using d0 and z0 resolution
300  float maxDz_par2[3];
301  float maxDr_par2[3];
302  dzCut_par2(trk, nLayers, dz_par2, dz_exp, d0err, d0err_par, maxDz_par2);
303  drCut_par2(trk, nLayers, dr_par2, dr_exp, d0err, d0err_par, maxDr_par2);
304 
305  for (int i = 2; i >= 0; --i) {
306  if (maxDr_par2[i] < maxDr_par[i])
307  maxDr_par[i] = maxDr_par2[i];
308  if (maxDz_par2[i] < maxDz_par[i])
309  maxDz_par[i] = maxDz_par2[i];
310  }
311  }
312 
313  Point bestVertex = getBestVertex(trk, vertices, minNVtxTrk); // min number of tracks 3 @HLT
314  if (bestVertex.z() < -99998.) {
315  bestVertex = beamSpot.position();
316  }
317 
318  ret = std::min(ret, cut(dz(trk, bestVertex), maxDz_par, std::less<float>()));
319  if (ret == -1.f)
320  return ret;
321  ret = std::min(ret, cut(dr(trk, bestVertex), maxDr_par, std::less<float>()));
322  if (ret == -1.f)
323  return ret;
324  }
325  if (ret == -1.f)
326  return ret;
327 
328  return ret;
329  }
330 
331  static const char* name() { return "TrackCutClassifier"; }
332 
334  desc.add<bool>("isHLT", false);
335  desc.add<std::vector<int>>(
336  "minHits4pass",
338  desc.add<std::vector<int>>("minHits", {0, 0, 1});
339  desc.add<std::vector<int>>("minPixelHits", {0, 0, 1});
340  desc.add<std::vector<int>>("minLayers", {3, 4, 5});
341  desc.add<std::vector<int>>("min3DLayers", {1, 2, 3});
342  desc.add<std::vector<int>>("maxLostLayers", {99, 3, 3});
343  desc.add<std::vector<double>>(
344  "maxRelPtErr",
346  desc.add<std::vector<double>>("minNdof", {-1., -1., -1.});
347  desc.add<std::vector<double>>("maxChi2", {9999., 25., 16.});
348  desc.add<std::vector<double>>("maxChi2n", {9999., 1.0, 0.4});
349 
350  desc.add<int>("minNVtxTrk", 2);
351 
352  desc.add<std::vector<double>>(
353  "maxDz",
355  desc.add<std::vector<double>>("maxDzWrtBS", {std::numeric_limits<float>::max(), 24., 15.});
356  desc.add<std::vector<double>>(
357  "maxDr",
359 
361  dz_par.add<std::vector<int>>("dz_exp",
364  std::numeric_limits<int>::max()}); // par = 4
365  dz_par.add<std::vector<double>>("dz_par1",
368  std::numeric_limits<float>::max()}); // par = 0.4
369  dz_par.add<std::vector<double>>("dz_par2",
372  std::numeric_limits<float>::max()}); // par = 0.35
373  dz_par.add<std::vector<double>>("dzWPVerr_par",
376  std::numeric_limits<float>::max()}); // par = 3.
377  desc.add<edm::ParameterSetDescription>("dz_par", dz_par);
378 
380  dr_par.add<std::vector<int>>("dr_exp",
383  std::numeric_limits<int>::max()}); // par = 4
384  dr_par.add<std::vector<double>>("dr_par1",
387  std::numeric_limits<float>::max()}); // par = 0.4
388  dr_par.add<std::vector<double>>("dr_par2",
391  std::numeric_limits<float>::max()}); // par = 0.3
392  dr_par.add<std::vector<double>>("d0err", {0.003, 0.003, 0.003});
393  dr_par.add<std::vector<double>>("d0err_par", {0.001, 0.001, 0.001});
394  dr_par.add<std::vector<double>>("drWPVerr_par",
397  std::numeric_limits<float>::max()}); // par = 3.
398  desc.add<edm::ParameterSetDescription>("dr_par", dr_par);
399  }
400 
401  bool isHLT;
402  float maxRelPtErr[3];
403  float minNdof[3];
404  float maxChi2[3];
405  float maxChi2n[3];
406  int minLayers[3];
407  int min3DLayers[3];
408  int minHits4pass[3];
409  int minHits[3];
410  int minPixelHits[3];
411  int maxLostLayers[3];
412  int minNVtxTrk;
413  float maxDz[3];
414  float maxDzWrtBS[3];
415  float maxDr[3];
416  int dz_exp[3];
417  float dz_par1[3];
418  float dz_par2[3];
419  float dzWPVerr_par[3];
420  int dr_exp[3];
421  float dr_par1[3];
422  float dr_par2[3];
423  float d0err[3];
424  float d0err_par[3];
425  float drWPVerr_par[3];
426  };
427 
428  using TrackCutClassifier = TrackMVAClassifier<Cuts>;
429 
430 } // namespace
431 
434 
435 DEFINE_FWK_MODULE(TrackCutClassifier);
int numberOfValidPixelHits() const
Definition: HitPattern.h:831
double ptError() const
error on Pt (set to 1000 TeV if charge==0 for safety)
Definition: TrackBase.h:754
unsigned short numberOfValidHits() const
number of valid hits found
Definition: TrackBase.h:798
ret
prodAgent to be discontinued
double p() const
momentum vector magnitude
Definition: TrackBase.h:631
int trackerLayersWithMeasurement() const
Definition: HitPattern.cc:534
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
assert(be >=bs)
Point getBestVertex_withError(reco::Track const &trk, reco::VertexCollection const &vertices, Point &error, const size_t minNtracks=2)
Definition: getBestVertex.h:28
double pt() const
track transverse momentum
Definition: TrackBase.h:637
double ndof() const
number of degrees of freedom of the fit
Definition: TrackBase.h:590
int numberOfValidStripLayersWithMonoAndStereo(uint16_t stripdet, uint16_t layer) const
Definition: HitPattern.cc:369
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:622
double dzError() const
error on dz
Definition: TrackBase.h:778
T sqrt(T t)
Definition: SSEVec.h:19
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:91
double f[11][100]
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void fillArrayI(int *x, const edm::ParameterSet &cfg, const char *name)
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:88
int trackerLayersWithoutMeasurement(HitCategory category) const
Definition: HitPattern.cc:553
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:504
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:593
Structure Point Contains parameters of Gaussian fits to DMRs.
Point getBestVertex(reco::Track const &trk, reco::VertexCollection const &vertices, const size_t minNtracks=2)
Definition: getBestVertex.h:8
float x
bool isFromDetOrFast(TrackingRecHit const &hit)
int pixelLayersWithMeasurement() const
Definition: HitPattern.cc:513
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
double d0Error() const
error on d0
Definition: TrackBase.h:772
long double T
void fillArrayF(float *x, const edm::ParameterSet &cfg, const char *name)
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
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:608