CMS 3D CMS Logo

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