CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackCutClassifier.cc
Go to the documentation of this file.
2 
3 
5 
6 #include <cassert>
7 
8 #include "getBestVertex.h"
9 #include "powN.h"
10 
11 
12 namespace {
13 
14  void fillArrayF(float * x,const edm::ParameterSet & cfg, const char * name) {
15  auto v = cfg.getParameter< std::vector<double> >(name);
16  assert(v.size()==3);
18  }
19 
20  void fillArrayI(int * x,const edm::ParameterSet & cfg, const char * name) {
21  auto v = cfg.getParameter< std::vector<int> >(name);
22  assert(v.size()==3);
24  }
25 
26  // fake mva value to return for loose,tight,hp
27  constexpr float mvaVal[3] = {-.5,.5,1.};
28 
29  template<typename T,typename Comp>
30  inline float cut(T val, const T * cuts, Comp comp) {
31  for (int i=2; i>=0; --i) {
32  if ( comp(val,cuts[i]) ) return mvaVal[i];
33  }
34  return -1.f;
35  }
36 
37  inline float chi2n(reco::Track const & tk) { return tk.normalizedChi2();}
38 
39  inline int lostLayers(reco::Track const & tk) {
41  }
42 
43  inline int n3DLayers(reco::Track const & tk) {
46  }
47 
48  inline int nPixelHits(reco::Track const & tk) {
49  return tk.hitPattern().numberOfValidPixelHits();
50  }
51 
52  inline float dz(reco::Track const & trk, Point const & bestVertex) {
53  return std::abs(trk.dz(bestVertex));
54  }
55  inline float dr(reco::Track const & trk, Point const & bestVertex) {
56  return std::abs(trk.dxy(bestVertex));
57  }
58 
59  inline void dzCut_par1(reco::Track const & trk, int & nLayers, const float * par, const int * exp, float dzCut[]) {
60  float dzE = trk.dzError();
61  for (int i=2; i>=0; --i) {
62  dzCut[i] = powN(par[i]*nLayers,exp[i])*dzE;
63  }
64  }
65  inline void drCut_par1(reco::Track const & trk, int & nLayers, const float * par, const int * exp, float drCut[]) {
66  float drE = trk.d0Error();
67  for (int i=2; i>=0; --i) {
68  drCut[i] = powN(par[i]*nLayers,exp[i])*drE;
69  }
70  }
71 
72  inline void dzCut_par2(reco::Track const & trk, int & nLayers, const float * par, const int * exp, const float * d0err, const float * d0err_par, float dzCut[]) {
73  float pt = float(trk.pt());
74  float p = float(trk.p());
75 
76  for (int i=2; i>=0; --i) {
77  // parametrized d0 resolution for the track pt
78  float nomd0E = sqrt(d0err[i]*d0err[i]+(d0err_par[i]/pt)*(d0err_par[i]/pt));
79  // parametrized z0 resolution for the track pt and eta
80  float nomdzE = nomd0E*(p/pt); // cosh(eta):=abs(p)/pt
81 
82  dzCut[i] = powN(par[i]*nLayers,exp[i])*nomdzE;
83  }
84  }
85  inline void drCut_par2(reco::Track const & trk, int & nLayers, const float* par, const int * exp, const float * d0err, const float * d0err_par, float drCut[]) {
86  float pt = trk.pt();
87 
88  for (int i=2; i>=0; --i) {
89  // parametrized d0 resolution for the track pt
90  float nomd0E = sqrt(d0err[i]*d0err[i]+(d0err_par[i]/pt)*(d0err_par[i]/pt));
91 
92  drCut[i] = powN(par[i]*nLayers,exp[i])*nomd0E;
93  }
94  }
95 
96  struct Cuts {
97 
98  Cuts(const edm::ParameterSet & cfg) {
99  fillArrayF(minNdof, cfg,"minNdof");
100  fillArrayF(maxChi2, cfg,"maxChi2");
101  fillArrayF(maxChi2n, cfg,"maxChi2n");
102  fillArrayI(minPixelHits, cfg,"minPixelHits");
103  fillArrayI(min3DLayers, cfg,"min3DLayers");
104  fillArrayI(minLayers, cfg,"minLayers");
105  fillArrayI(maxLostLayers,cfg,"maxLostLayers");
106  minNVtxTrk = cfg.getParameter<int>("minNVtxTrk");
107  fillArrayF(maxDz, cfg,"maxDz");
108  fillArrayF(maxDzWrtBS, cfg,"maxDzWrtBS");
109  fillArrayF(maxDr, cfg,"maxDr");
110  edm::ParameterSet dz_par = cfg.getParameter<edm::ParameterSet>("dz_par");
111  fillArrayI(dz_exp, dz_par,"dz_exp");
112  fillArrayF(dz_par1, dz_par,"dz_par1");
113  fillArrayF(dz_par2, dz_par,"dz_par2");
114  edm::ParameterSet dr_par = cfg.getParameter<edm::ParameterSet>("dr_par");
115  fillArrayI(dr_exp, dr_par,"dr_exp");
116  fillArrayF(dr_par1, dr_par,"dr_par1");
117  fillArrayF(dr_par2, dr_par,"dr_par2");
118  fillArrayF(d0err, dr_par,"d0err");
119  fillArrayF(d0err_par, dr_par,"d0err_par");
120  }
121 
122 
123 
124  float operator()(reco::Track const & trk,
125  reco::BeamSpot const & beamSpot,
127  GBRForest const *) const {
128 
129  float ret = 1.f;
130  ret = std::min(ret,cut(float(trk.ndof()),minNdof,std::greater_equal<float>()) );
131  if (ret==-1.f) return ret;
132 
133  auto nLayers = trk.hitPattern().trackerLayersWithMeasurement();
134  ret = std::min(ret,cut(nLayers,minLayers,std::greater_equal<int>()));
135  if (ret==-1.f) return ret;
136 
137  ret = std::min(ret,cut(chi2n(trk)/float(nLayers),maxChi2n,std::less_equal<float>()));
138  if (ret==-1.f) return ret;
139 
140  ret = std::min(ret,cut(chi2n(trk),maxChi2,std::less_equal<float>()));
141  if (ret==-1.f) return ret;
142 
143  ret = std::min(ret,cut(n3DLayers(trk),min3DLayers,std::greater_equal<int>()));
144  if (ret==-1.f) return ret;
145 
146  ret = std::min(ret,cut(nPixelHits(trk),minPixelHits,std::greater_equal<int>()));
147  if (ret==-1.f) return ret;
148 
149  ret = std::min(ret,cut(lostLayers(trk),maxLostLayers,std::less_equal<int>()));
150  if (ret==-1.f) return ret;
151 
152  // original dz and dr cut
154 
155  // if not primaryVertices are reconstructed, check compatibility w.r.t. beam spot
156  Point bestVertex = getBestVertex(trk,vertices,minNVtxTrk); // min number of tracks [2 (=default) for offline, 3 for HLT]
157  float maxDzcut[3];
158  std::copy(std::begin(maxDz),std::end(maxDz),std::begin(maxDzcut));
159  if (bestVertex.z() < -99998.) {
160  bestVertex = beamSpot.position();
161  std::copy(std::begin(maxDzWrtBS),std::end(maxDzWrtBS),std::begin(maxDzcut));
162  }
163  ret = std::min(ret,cut(dr(trk,bestVertex), maxDr,std::less<float>()));
164  if (ret==-1.f) return ret;
165 
166  ret = std::min(ret,cut(dz(trk,bestVertex), maxDzcut,std::less<float>()));
167  if (ret==-1.f) return ret;
168  }
169 
170 
171  // parametrized dz and dr cut by using their error
173 
174  float maxDz_par1[3];
175  float maxDr_par1[3];
176  dzCut_par1(trk,nLayers,dz_par1,dz_exp, maxDz_par1);
177  drCut_par1(trk,nLayers,dr_par1,dr_exp, maxDr_par1);
178 
179  float maxDz_par[3];
180  float maxDr_par[3];
181  std::copy(std::begin(maxDz_par1),std::end(maxDz_par1),std::begin(maxDz_par));
182  std::copy(std::begin(maxDr_par1),std::end(maxDr_par1),std::begin(maxDr_par));
183 
184  // parametrized dz and dr cut by using d0 and z0 resolution
186 
187  float maxDz_par2[3];
188  float maxDr_par2[3];
189  dzCut_par2(trk,nLayers,dz_par2,dz_exp,d0err,d0err_par, maxDz_par2);
190  drCut_par2(trk,nLayers,dr_par2,dr_exp,d0err,d0err_par, maxDr_par2);
191 
192 
193  for (int i=2; i>=0; --i) {
194  if (maxDr_par2[i]<maxDr_par[i]) maxDr_par[i] = maxDr_par2[i];
195  if (maxDz_par2[i]<maxDz_par[i]) maxDz_par[i] = maxDz_par2[i];
196  }
197  }
198 
199  Point bestVertex = getBestVertex(trk,vertices,minNVtxTrk); // min number of tracks 3 @HLT
200  if (bestVertex.z() < -99998.) {
201  bestVertex = beamSpot.position();
202  }
203 
204  ret = std::min(ret,cut(dz(trk,bestVertex), maxDz_par,std::less<float>()));
205  if (ret==-1.f) return ret;
206  ret = std::min(ret,cut(dr(trk,bestVertex), maxDr_par,std::less<float>()));
207  if (ret==-1.f) return ret;
208 
209  }
210  if (ret==-1.f) return ret;
211 
212  return ret;
213 
214  }
215 
216 
217 
218  static const char * name() { return "TrackCutClassifier";}
219 
220  static void fillDescriptions(edm::ParameterSetDescription & desc) {
221  desc.add<std::vector<int>>("minPixelHits", { 0, 0, 1});
222  desc.add<std::vector<int>>("minLayers", { 3, 4, 5});
223  desc.add<std::vector<int>>("min3DLayers", { 1, 2, 3});
224  desc.add<std::vector<int>>("maxLostLayers",{99, 3, 3});
225  desc.add<std::vector<double>>("minNdof", {-1., -1., -1.});
226  desc.add<std::vector<double>>("maxChi2", {9999.,25., 16. });
227  desc.add<std::vector<double>>("maxChi2n", {9999., 1.0, 0.4});
228 
229  desc.add<int>("minNVtxTrk", 2);
230 
232  desc.add<std::vector<double>>("maxDzWrtBS",{std::numeric_limits<float>::max(),24.,15.});
234 
236  dz_par.add<std::vector<int>> ("dz_exp", {std::numeric_limits<int>::max(), std::numeric_limits<int>::max(), std::numeric_limits<int>::max()} ); // par = 4
237  dz_par.add<std::vector<double>>("dz_par1",{std::numeric_limits<float>::max(),std::numeric_limits<float>::max(),std::numeric_limits<float>::max()}); // par = 0.4
238  dz_par.add<std::vector<double>>("dz_par2",{std::numeric_limits<float>::max(),std::numeric_limits<float>::max(),std::numeric_limits<float>::max()}); // par = 0.35
239  desc.add<edm::ParameterSetDescription>("dz_par", dz_par);
240 
242  dr_par.add<std::vector<int>> ("dr_exp", {std::numeric_limits<int>::max(), std::numeric_limits<int>::max(), std::numeric_limits<int>::max()} ); // par = 4
243  dr_par.add<std::vector<double>>("dr_par1",{std::numeric_limits<float>::max(),std::numeric_limits<float>::max(),std::numeric_limits<float>::max()}); // par = 0.4
244  dr_par.add<std::vector<double>>("dr_par2",{std::numeric_limits<float>::max(),std::numeric_limits<float>::max(),std::numeric_limits<float>::max()}); // par = 0.3
245  dr_par.add<std::vector<double>>("d0err", {0.003, 0.003, 0.003});
246  dr_par.add<std::vector<double>>("d0err_par", {0.001, 0.001, 0.001});
247  desc.add<edm::ParameterSetDescription>("dr_par", dr_par);
248 
249  }
250 
251  float minNdof[3];
252  float maxChi2[3];
253  float maxChi2n[3];
254  int minLayers[3];
255  int min3DLayers[3];
256  int minPixelHits[3];
257  int maxLostLayers[3];
258  int minNVtxTrk;
259  float maxDz[3];
260  float maxDzWrtBS[3];
261  float maxDr[3];
262  int dz_exp[3];
263  float dz_par1[3];
264  float dz_par2[3];
265  int dr_exp[3];
266  float dr_par1[3];
267  float dr_par2[3];
268  float d0err[3];
269  float d0err_par[3];
270  };
271 
272 
273  using TrackCutClassifier = TrackMVAClassifier<Cuts>;
274 
275 }
276 
279 
280 DEFINE_FWK_MODULE(TrackCutClassifier);
281 
double p() const
momentum vector magnitude
Definition: TrackBase.h:610
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
tuple ret
prodAgent to be discontinued
double d0Error() const
error on d0
Definition: TrackBase.h:797
tuple cfg
Definition: looper.py:293
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:556
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
assert(m_qm.get())
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
int pixelLayersWithMeasurement() const
Definition: HitPattern.cc:508
int trackerLayersWithMeasurement() const
Definition: HitPattern.cc:527
#define constexpr
T x() const
Cartesian x coordinate.
int numberOfValidStripLayersWithMonoAndStereo(uint16_t stripdet, uint16_t layer) const
Definition: HitPattern.cc:350
double ndof() const
number of degrees of freedom of the fit
Definition: TrackBase.h:550
T sqrt(T t)
Definition: SSEVec.h:18
double pt() const
track transverse momentum
Definition: TrackBase.h:616
math::XYZPoint Point
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
#define end
Definition: vmac.h:37
T min(T a, T b)
Definition: MathUtil.h:58
ParameterDescriptionBase * add(U const &iLabel, T const &value)
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:604
double dzError() const
error on dz
Definition: TrackBase.h:809
tuple minPixelHits
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:445
int trackerLayersWithoutMeasurement(HitCategory category) const
Definition: HitPattern.cc:544
#define begin
Definition: vmac.h:30
int numberOfValidPixelHits() const
Definition: HitPattern.h:838
const Point & position() const
position
Definition: BeamSpot.h:62
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:586
long double T