CMS 3D CMS Logo

HIMultiTrackSelector.cc
Go to the documentation of this file.
1 #include "HIMultiTrackSelector.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  forest_ = nullptr;
62 }
63 
65 {
66  mvavars_indices.clear();
67  for (unsigned i=0;i<forestVars_.size();i++)
68  {
69  std::string v = forestVars_[i];
70  int ind = -1;
71  if (v=="chi2perdofperlayer") ind=chi2perdofperlayer;
72  if (v=="dxyperdxyerror") ind=dxyperdxyerror;
73  if (v=="dzperdzerror") ind=dzperdzerror;
74  if (v=="relpterr") ind=relpterr;
75  if (v=="lostmidfrac") ind=lostmidfrac;
76  if (v=="minlost") ind=minlost;
77  if (v=="nhits") ind=nhits;
78  if (v=="eta") ind=eta;
79  if (v=="chi2n_no1dmod") ind=chi2n_no1dmod;
80  if (v=="chi2n") ind=chi2n ;
81  if (v=="nlayerslost") ind=nlayerslost ;
82  if (v=="nlayers3d") ind=nlayers3d ;
83  if (v=="nlayers") ind=nlayers ;
84  if (v=="ndof") ind=ndof ;
85  if (v=="etaerror") ind=etaerror;
86 
87  if (ind==-1) edm::LogWarning("HIMultiTrackSelector") << "Unknown forest variable "<<v<<". Please make sure it's in the list of supported variables\n";
88 
89  mvavars_indices.push_back(ind);
90  }
91 
92 }
93 
95  src_( consumes<reco::TrackCollection>( cfg.getParameter<edm::InputTag>( "src" ) ) ),
96  hSrc_(consumes<TrackingRecHitCollection>( cfg.getParameter<edm::InputTag>( "src" ) ) ),
97  beamspot_( consumes<reco::BeamSpot>( cfg.getParameter<edm::InputTag>( "beamspot" ) ) ),
98  useVertices_( cfg.getParameter<bool>( "useVertices" ) ),
99  useVtxError_( cfg.getParameter<bool>( "useVtxError" ) )
100  // now get the pset for each selector
101 {
102  if (useVertices_) vertices_ = consumes<reco::VertexCollection>(cfg.getParameter<edm::InputTag>( "vertices" ));
103 
104  applyPixelMergingCuts_ = false;
105  if(cfg.exists("applyPixelMergingCuts"))
106  applyPixelMergingCuts_ = cfg.getParameter<bool>("applyPixelMergingCuts");
107 
108  useAnyMVA_ = false;
109  forestLabel_ = "MVASelectorIter0";
110  std::string type = "BDTG";
111  useForestFromDB_ = true;
112  dbFileName_ = "";
113 
114  forest_ = nullptr;
115 
116  if(cfg.exists("useAnyMVA")) useAnyMVA_ = cfg.getParameter<bool>("useAnyMVA");
117  if(useAnyMVA_){
118  if(cfg.exists("mvaType"))type = cfg.getParameter<std::string>("mvaType");
119  if(cfg.exists("GBRForestLabel"))forestLabel_ = cfg.getParameter<std::string>("GBRForestLabel");
120  if(cfg.exists("GBRForestVars")) {
121  forestVars_ = cfg.getParameter<std::vector<std::string> >("GBRForestVars");
122  ParseForestVars();
123  }
124  if(cfg.exists("GBRForestFileName")){
125  dbFileName_ = cfg.getParameter<std::string>("GBRForestFileName");
126  useForestFromDB_ = false;
127  }
128  mvaType_ = type;
129  }
130  std::vector<edm::ParameterSet> trkSelectors( cfg.getParameter<std::vector< edm::ParameterSet> >("trackSelectors") );
131  qualityToSet_.reserve(trkSelectors.size());
132  vtxNumber_.reserve(trkSelectors.size());
133  vertexCut_.reserve(trkSelectors.size());
134  res_par_.reserve(trkSelectors.size());
135  chi2n_par_.reserve(trkSelectors.size());
136  chi2n_no1Dmod_par_.reserve(trkSelectors.size());
137  d0_par1_.reserve(trkSelectors.size());
138  dz_par1_.reserve(trkSelectors.size());
139  d0_par2_.reserve(trkSelectors.size());
140  dz_par2_.reserve(trkSelectors.size());
141  applyAdaptedPVCuts_.reserve(trkSelectors.size());
142  max_d0_.reserve(trkSelectors.size());
143  max_z0_.reserve(trkSelectors.size());
144  nSigmaZ_.reserve(trkSelectors.size());
145  pixel_pTMinCut_.reserve(trkSelectors.size());
146  pixel_pTMaxCut_.reserve(trkSelectors.size());
147  min_layers_.reserve(trkSelectors.size());
148  min_3Dlayers_.reserve(trkSelectors.size());
149  max_lostLayers_.reserve(trkSelectors.size());
150  min_hits_bypass_.reserve(trkSelectors.size());
151  applyAbsCutsIfNoPV_.reserve(trkSelectors.size());
152  max_d0NoPV_.reserve(trkSelectors.size());
153  max_z0NoPV_.reserve(trkSelectors.size());
154  preFilter_.reserve(trkSelectors.size());
155  max_relpterr_.reserve(trkSelectors.size());
156  min_nhits_.reserve(trkSelectors.size());
157  max_minMissHitOutOrIn_.reserve(trkSelectors.size());
158  max_lostHitFraction_.reserve(trkSelectors.size());
159  min_eta_.reserve(trkSelectors.size());
160  max_eta_.reserve(trkSelectors.size());
161  useMVA_.reserve(trkSelectors.size());
162  //mvaReaders_.reserve(trkSelectors.size());
163  min_MVA_.reserve(trkSelectors.size());
164  //mvaType_.reserve(trkSelectors.size());
165 
166  produces<edm::ValueMap<float> >("MVAVals");
167 
168  for ( unsigned int i=0; i<trkSelectors.size(); i++) {
169 
170  qualityToSet_.push_back( TrackBase::undefQuality );
171  // parameters for vertex selection
172  vtxNumber_.push_back( useVertices_ ? trkSelectors[i].getParameter<int32_t>("vtxNumber") : 0 );
173  vertexCut_.push_back( useVertices_ ? trkSelectors[i].getParameter<std::string>("vertexCut") : nullptr);
174  // parameters for adapted optimal cuts on chi2 and primary vertex compatibility
175  res_par_.push_back(trkSelectors[i].getParameter< std::vector<double> >("res_par") );
176  chi2n_par_.push_back( trkSelectors[i].getParameter<double>("chi2n_par") );
177  chi2n_no1Dmod_par_.push_back( trkSelectors[i].getParameter<double>("chi2n_no1Dmod_par") );
178  d0_par1_.push_back(trkSelectors[i].getParameter< std::vector<double> >("d0_par1"));
179  dz_par1_.push_back(trkSelectors[i].getParameter< std::vector<double> >("dz_par1"));
180  d0_par2_.push_back(trkSelectors[i].getParameter< std::vector<double> >("d0_par2"));
181  dz_par2_.push_back(trkSelectors[i].getParameter< std::vector<double> >("dz_par2"));
182  // Boolean indicating if adapted primary vertex compatibility cuts are to be applied.
183  applyAdaptedPVCuts_.push_back(trkSelectors[i].getParameter<bool>("applyAdaptedPVCuts"));
184  // Impact parameter absolute cuts.
185  max_d0_.push_back(trkSelectors[i].getParameter<double>("max_d0"));
186  max_z0_.push_back(trkSelectors[i].getParameter<double>("max_z0"));
187  nSigmaZ_.push_back(trkSelectors[i].getParameter<double>("nSigmaZ"));
188  // Cuts on numbers of layers with hits/3D hits/lost hits.
189  min_layers_.push_back(trkSelectors[i].getParameter<uint32_t>("minNumberLayers") );
190  min_3Dlayers_.push_back(trkSelectors[i].getParameter<uint32_t>("minNumber3DLayers") );
191  max_lostLayers_.push_back(trkSelectors[i].getParameter<uint32_t>("maxNumberLostLayers"));
192  min_hits_bypass_.push_back(trkSelectors[i].getParameter<uint32_t>("minHitsToBypassChecks"));
193  // Flag to apply absolute cuts if no PV passes the selection
194  applyAbsCutsIfNoPV_.push_back(trkSelectors[i].getParameter<bool>("applyAbsCutsIfNoPV"));
195  keepAllTracks_.push_back( trkSelectors[i].getParameter<bool>("keepAllTracks"));
196  max_relpterr_.push_back(trkSelectors[i].getParameter<double>("max_relpterr"));
197  min_nhits_.push_back(trkSelectors[i].getParameter<uint32_t>("min_nhits"));
198  max_minMissHitOutOrIn_.push_back(
199  trkSelectors[i].existsAs<int32_t>("max_minMissHitOutOrIn") ?
200  trkSelectors[i].getParameter<int32_t>("max_minMissHitOutOrIn") : 99);
201  max_lostHitFraction_.push_back(
202  trkSelectors[i].existsAs<double>("max_lostHitFraction") ?
203  trkSelectors[i].getParameter<double>("max_lostHitFraction") : 1.0);
204  min_eta_.push_back(trkSelectors[i].existsAs<double>("min_eta") ?
205  trkSelectors[i].getParameter<double>("min_eta"):-9999);
206  max_eta_.push_back(trkSelectors[i].existsAs<double>("max_eta") ?
207  trkSelectors[i].getParameter<double>("max_eta"):9999);
208 
209  setQualityBit_.push_back( false );
210  std::string qualityStr = trkSelectors[i].getParameter<std::string>("qualityBit");
211  if (!qualityStr.empty()) {
212  setQualityBit_[i] = true;
213  qualityToSet_[i] = TrackBase::qualityByName(trkSelectors[i].getParameter<std::string>("qualityBit"));
214  }
215 
216  if (setQualityBit_[i] && (qualityToSet_[i] == TrackBase::undefQuality)) throw cms::Exception("Configuration") <<
217  "You can't set the quality bit " << trkSelectors[i].getParameter<std::string>("qualityBit") << " as it is 'undefQuality' or unknown.\n";
218 
219  if (applyAbsCutsIfNoPV_[i]) {
220  max_d0NoPV_.push_back(trkSelectors[i].getParameter<double>("max_d0NoPV"));
221  max_z0NoPV_.push_back(trkSelectors[i].getParameter<double>("max_z0NoPV"));
222  }
223  else{//dummy values
224  max_d0NoPV_.push_back(0.);
225  max_z0NoPV_.push_back(0.);
226  }
227 
228  name_.push_back( trkSelectors[i].getParameter<std::string>("name") );
229 
230  preFilter_[i]=trkSelectors.size(); // no prefilter
231 
232  std::string pfName=trkSelectors[i].getParameter<std::string>("preFilterName");
233  if (!pfName.empty()) {
234  bool foundPF=false;
235  for ( unsigned int j=0; j<i; j++)
236  if (name_[j]==pfName ) {
237  foundPF=true;
238  preFilter_[i]=j;
239  }
240  if ( !foundPF)
241  throw cms::Exception("Configuration") << "Invalid prefilter name in HIMultiTrackSelector "
242  << trkSelectors[i].getParameter<std::string>("preFilterName");
243 
244  }
245 
247  {
248  pixel_pTMinCut_.push_back(trkSelectors[i].getParameter< std::vector<double> >("pixel_pTMinCut"));
249  pixel_pTMaxCut_.push_back(trkSelectors[i].getParameter< std::vector<double> >("pixel_pTMaxCut"));
250  }
251 
252  // produces<std::vector<int> >(name_[i]).setBranchAlias( name_[i] + "TrackQuals");
253  produces<edm::ValueMap<int> >(name_[i]).setBranchAlias( name_[i] + "TrackQuals");
254  if(useAnyMVA_){
255  bool thisMVA = false;
256  if(trkSelectors[i].exists("useMVA"))thisMVA = trkSelectors[i].getParameter<bool>("useMVA");
257  useMVA_.push_back(thisMVA);
258  if(thisMVA){
259  double minVal = -1;
260  if(trkSelectors[i].exists("minMVA"))minVal = trkSelectors[i].getParameter<double>("minMVA");
261  min_MVA_.push_back(minVal);
262 
263  }else{
264  min_MVA_.push_back(-9999.0);
265  }
266  }else{
267  min_MVA_.push_back(-9999.0);
268  }
269 
270  }
271 }
272 
274  delete forest_;
275 }
276 
277 
279  if(!useForestFromDB_){
280  TFile gbrfile(dbFileName_.c_str());
281  forest_ = (GBRForest*)gbrfile.Get(forestLabel_.c_str());
282  }
283 
284 }
285 
286 
287 
289 {
290  using namespace std;
291  using namespace edm;
292  using namespace reco;
293 
294  // Get tracks
295  Handle<TrackCollection> hSrcTrack;
296  evt.getByToken(src_, hSrcTrack );
297  const TrackCollection& srcTracks(*hSrcTrack);
298 
299  // get hits in track..
301  evt.getByToken(hSrc_, hSrcHits );
302  const TrackingRecHitCollection & srcHits(*hSrcHits);
303 
304 
305  // looking for the beam spot
307  evt.getByToken(beamspot_, hBsp);
308  const reco::BeamSpot& vertexBeamSpot(*hBsp);
309 
310 
311  // Select good primary vertices for use in subsequent track selection
313  if (useVertices_) evt.getByToken(vertices_, hVtx);
314 
315  unsigned int trkSize=srcTracks.size();
316  std::vector<int> selTracksSave( qualityToSet_.size()*trkSize,0);
317 
318  std::vector<float> mvaVals_(srcTracks.size(),-99.f);
319  processMVA(evt,es, mvaVals_, *hVtx);
320 
321  for (unsigned int i=0; i<qualityToSet_.size(); i++) {
322  std::vector<int> selTracks(trkSize,0);
323  auto selTracksValueMap = std::make_unique<edm::ValueMap<int>>();
324  edm::ValueMap<int>::Filler filler(*selTracksValueMap);
325 
326  std::vector<Point> points;
327  std::vector<float> vterr, vzerr;
328  if (useVertices_) selectVertices(i,*hVtx, points, vterr, vzerr);
329 
330  // Loop over tracks
331  size_t current = 0;
332  for (TrackCollection::const_iterator it = srcTracks.begin(), ed = srcTracks.end(); it != ed; ++it, ++current) {
333  const Track & trk = * it;
334  // Check if this track passes cuts
335 
336  LogTrace("TrackSelection") << "ready to check track with pt="<< trk.pt() ;
337 
338  //already removed
339  bool ok=true;
340  float mvaVal = 0;
341  if (preFilter_[i]<i && selTracksSave[preFilter_[i]*trkSize+current] < 0) {
342  selTracks[current]=-1;
343  ok=false;
344  if ( !keepAllTracks_[i])
345  continue;
346  }
347  else {
348  if(useAnyMVA_) mvaVal = mvaVals_[current];
349  ok = select(i,vertexBeamSpot, srcHits, trk, points, vterr, vzerr,mvaVal);
350  if (!ok) {
351  LogTrace("TrackSelection") << "track with pt="<< trk.pt() << " NOT selected";
352  if (!keepAllTracks_[i]) {
353  selTracks[current]=-1;
354  continue;
355  }
356  }
357  else
358  LogTrace("TrackSelection") << "track with pt="<< trk.pt() << " selected";
359  }
360 
361  if (preFilter_[i]<i ) {
362  selTracks[current]=selTracksSave[preFilter_[i]*trkSize+current];
363  }
364  else {
365  selTracks[current]=trk.qualityMask();
366  }
367  if ( ok && setQualityBit_[i]) {
368  selTracks[current]= (selTracks[current] | (1<<qualityToSet_[i]));
369  if (qualityToSet_[i]==TrackBase::tight) {
370  selTracks[current]=(selTracks[current] | (1<<TrackBase::loose));
371  }
372  else if (qualityToSet_[i]==TrackBase::highPurity) {
373  selTracks[current]=(selTracks[current] | (1<<TrackBase::loose));
374  selTracks[current]=(selTracks[current] | (1<<TrackBase::tight));
375  }
376 
377  if (!points.empty()) {
378  if (qualityToSet_[i]==TrackBase::loose) {
379  selTracks[current]=(selTracks[current] | (1<<TrackBase::looseSetWithPV));
380  }
381  else if (qualityToSet_[i]==TrackBase::highPurity) {
382  selTracks[current]=(selTracks[current] | (1<<TrackBase::looseSetWithPV));
383  selTracks[current]=(selTracks[current] | (1<<TrackBase::highPuritySetWithPV));
384  }
385  }
386  }
387  }
388  for ( unsigned int j=0; j< trkSize; j++ ) selTracksSave[j+i*trkSize]=selTracks[j];
389  filler.insert(hSrcTrack, selTracks.begin(),selTracks.end());
390  filler.fill();
391 
392  // evt.put(std::move(selTracks),name_[i]);
393  evt.put(std::move(selTracksValueMap),name_[i]);
394  }
395 }
396 
397 
398  bool HIMultiTrackSelector::select(unsigned int tsNum,
399  const reco::BeamSpot &vertexBeamSpot,
400  const TrackingRecHitCollection & recHits,
401  const reco::Track &tk,
402  const std::vector<Point> &points,
403  std::vector<float> &vterr,
404  std::vector<float> &vzerr,
405  double mvaVal) const {
406  // Decide if the given track passes selection cuts.
407 
408  using namespace std;
409 
410  //cuts on number of valid hits
411  auto nhits = tk.numberOfValidHits();
412  if(nhits>=min_hits_bypass_[tsNum]) return true;
413  if(nhits < min_nhits_[tsNum]) return false;
414 
415  if ( tk.ndof() < 1E-5 ) return false;
416 
417 
419  //Adding the MVA selection before any other cut//
421  if(useAnyMVA_ && useMVA_[tsNum]){
422  if(mvaVal < min_MVA_[tsNum])return false; else return true;
423  }
425  //End of MVA selection section//
427 
428 
429  // Cuts on numbers of layers with hits/3D hits/lost hits.
431  uint32_t nlayers3D = tk.hitPattern().pixelLayersWithMeasurement() +
434  LogDebug("TrackSelection") << "cuts on nlayers: " << nlayers << " " << nlayers3D << " " << nlayersLost << " vs "
435  << min_layers_[tsNum] << " " << min_3Dlayers_[tsNum] << " " << max_lostLayers_[tsNum];
436  if (nlayers < min_layers_[tsNum]) return false;
437  if (nlayers3D < min_3Dlayers_[tsNum]) return false;
438  if (nlayersLost > max_lostLayers_[tsNum]) return false;
439  LogTrace("TrackSelection") << "cuts on nlayers passed";
440 
441  float chi2n = tk.normalizedChi2();
442  float chi2n_no1Dmod = chi2n;
443 
444  int count1dhits = 0;
445  auto ith = tk.extra()->firstRecHit();
446  auto edh = ith + tk.recHitsSize();
447  for (; ith<edh; ++ith) {
448  const TrackingRecHit & hit = recHits[ith];
449  if (hit.dimension()==1) ++count1dhits;
450  }
451  if (count1dhits > 0) {
452  float chi2 = tk.chi2();
453  float ndof = tk.ndof();
454  chi2n = (chi2+count1dhits)/float(ndof+count1dhits);
455  }
456  // For each 1D rechit, the chi^2 and ndof is increased by one. This is a way of retaining approximately
457  // the same normalized chi^2 distribution as with 2D rechits.
458  if (chi2n > chi2n_par_[tsNum]*nlayers) return false;
459 
460  if (chi2n_no1Dmod > chi2n_no1Dmod_par_[tsNum]*nlayers) return false;
461 
462  // Get track parameters
463  float pt = std::max(float(tk.pt()),0.000001f);
464  float eta = tk.eta();
465  if (eta<min_eta_[tsNum] || eta>max_eta_[tsNum]) return false;
466 
467  //cuts on relative error on pt
468  float relpterr = float(tk.ptError())/pt;
469  if(relpterr > max_relpterr_[tsNum]) return false;
470 
473  int minLost = std::min(lostIn,lostOut);
474  if (minLost > max_minMissHitOutOrIn_[tsNum]) return false;
475  float lostMidFrac = tk.numberOfLostHits()==0? 0. : tk.numberOfLostHits() / (tk.numberOfValidHits() + tk.numberOfLostHits());
476  if (lostMidFrac > max_lostHitFraction_[tsNum]) return false;
477 
478  // Pixel Track Merging pT dependent cuts
480  {
481  // hard cut at absolute min/max pt
482  if( pt < pixel_pTMinCut_[tsNum][0] ) return false;
483  if( pt > pixel_pTMaxCut_[tsNum][0] ) return false;
484  // tapering cuts with chi2n_no1Dmod
485  double pTMaxCutPos = ( pixel_pTMaxCut_[tsNum][0] - pt ) /
486  ( pixel_pTMaxCut_[tsNum][0] - pixel_pTMaxCut_[tsNum][1] );
487  double pTMinCutPos = ( pt - pixel_pTMinCut_[tsNum][0] ) /
488  ( pixel_pTMinCut_[tsNum][1] - pixel_pTMinCut_[tsNum][0] );
489  if( pt > pixel_pTMaxCut_[tsNum][1] &&
490  chi2n_no1Dmod > pixel_pTMaxCut_[tsNum][2]*nlayers*pow(pTMaxCutPos,pixel_pTMaxCut_[tsNum][3]) )
491  return false;
492  if( pt < pixel_pTMinCut_[tsNum][1] &&
493  chi2n_no1Dmod > pixel_pTMinCut_[tsNum][2]*nlayers*pow(pTMinCutPos,pixel_pTMinCut_[tsNum][3]) )
494  return false;
495  }
496 
497  //other track parameters
498  float d0 = -tk.dxy(vertexBeamSpot.position()), d0E = tk.d0Error(),
499  dz = tk.dz(vertexBeamSpot.position()), dzE = tk.dzError();
500 
501  // parametrized d0 resolution for the track pt
502  float nomd0E = sqrt(res_par_[tsNum][0]*res_par_[tsNum][0]+(res_par_[tsNum][1]/pt)*(res_par_[tsNum][1]/pt));
503  // parametrized z0 resolution for the track pt and eta
504  float nomdzE = nomd0E*(std::cosh(eta));
505 
506  float dzCut = std::min( powN(dz_par1_[tsNum][0]*nlayers,int(dz_par1_[tsNum][1]+0.5))*nomdzE,
507  powN(dz_par2_[tsNum][0]*nlayers,int(dz_par2_[tsNum][1]+0.5))*dzE );
508  float d0Cut = std::min( powN(d0_par1_[tsNum][0]*nlayers,int(d0_par1_[tsNum][1]+0.5))*nomd0E,
509  powN(d0_par2_[tsNum][0]*nlayers,int(d0_par2_[tsNum][1]+0.5))*d0E );
510 
511 
512  // ---- PrimaryVertex compatibility cut
513  bool primaryVertexZCompatibility(false);
514  bool primaryVertexD0Compatibility(false);
515 
516  if (points.empty()) { //If not primaryVertices are reconstructed, check just the compatibility with the BS
517  //z0 within (n sigma + dzCut) of the beam spot z, if no good vertex is found
518  if ( abs(dz) < hypot(vertexBeamSpot.sigmaZ()*nSigmaZ_[tsNum],dzCut) ) primaryVertexZCompatibility = true;
519  // d0 compatibility with beam line
520  if (abs(d0) < d0Cut) primaryVertexD0Compatibility = true;
521  }
522 
523  int iv=0;
524  for (std::vector<Point>::const_iterator point = points.begin(), end = points.end(); point != end; ++point) {
525  LogTrace("TrackSelection") << "Test track w.r.t. vertex with z position " << point->z();
526  if(primaryVertexZCompatibility && primaryVertexD0Compatibility) break;
527  float dzPV = tk.dz(*point); //re-evaluate the dz with respect to the vertex position
528  float d0PV = tk.dxy(*point); //re-evaluate the dxy with respect to the vertex position
529  if(useVtxError_){
530  float dzErrPV = std::sqrt(dzE*dzE+vzerr[iv]*vzerr[iv]); // include vertex error in z
531  float d0ErrPV = std::sqrt(d0E*d0E+vterr[iv]*vterr[iv]); // include vertex error in xy
532  iv++;
533  if (abs(dzPV) < dz_par1_[tsNum][0]*pow(nlayers,dz_par1_[tsNum][1])*nomdzE &&
534  abs(dzPV) < dz_par2_[tsNum][0]*pow(nlayers,dz_par2_[tsNum][1])*dzErrPV &&
535  abs(dzPV) < max_z0_[tsNum]) primaryVertexZCompatibility = true;
536  if (abs(d0PV) < d0_par1_[tsNum][0]*pow(nlayers,d0_par1_[tsNum][1])*nomd0E &&
537  abs(d0PV) < d0_par2_[tsNum][0]*pow(nlayers,d0_par2_[tsNum][1])*d0ErrPV &&
538  abs(d0PV) < max_d0_[tsNum]) primaryVertexD0Compatibility = true;
539  }else{
540  if (abs(dzPV) < dzCut) primaryVertexZCompatibility = true;
541  if (abs(d0PV) < d0Cut) primaryVertexD0Compatibility = true;
542  }
543  LogTrace("TrackSelection") << "distances " << dzPV << " " << d0PV << " vs " << dzCut << " " << d0Cut;
544  }
545 
546  if (points.empty() && applyAbsCutsIfNoPV_[tsNum]) {
547  if ( abs(dz) > max_z0NoPV_[tsNum] || abs(d0) > max_d0NoPV_[tsNum]) return false;
548  } else {
549  // Absolute cuts on all tracks impact parameters with respect to beam-spot.
550  // If BS is not compatible, verify if at least the reco-vertex is compatible (useful for incorrect BS settings)
551  if (abs(d0) > max_d0_[tsNum] && !primaryVertexD0Compatibility) return false;
552  LogTrace("TrackSelection") << "absolute cuts on d0 passed";
553  if (abs(dz) > max_z0_[tsNum] && !primaryVertexZCompatibility) return false;
554  LogTrace("TrackSelection") << "absolute cuts on dz passed";
555  }
556 
557  LogTrace("TrackSelection") << "cuts on PV: apply adapted PV cuts? " << applyAdaptedPVCuts_[tsNum]
558  << " d0 compatibility? " << primaryVertexD0Compatibility
559  << " z compatibility? " << primaryVertexZCompatibility ;
560 
561  if (applyAdaptedPVCuts_[tsNum]) {
562  return (primaryVertexD0Compatibility && primaryVertexZCompatibility);
563  } else {
564  return true;
565  }
566 
567 }
568 
569  void HIMultiTrackSelector::selectVertices(unsigned int tsNum,
570  const reco::VertexCollection &vtxs,
571  std::vector<Point> &points,
572  std::vector<float> &vterr,
573  std::vector<float> &vzerr) const {
574  // Select good primary vertices
575  using namespace reco;
576  int32_t toTake = vtxNumber_[tsNum];
577  for (VertexCollection::const_iterator it = vtxs.begin(), ed = vtxs.end(); it != ed; ++it) {
578 
579  LogDebug("SelectVertex") << " select vertex with z position " << it->z() << " "
580  << it->chi2() << " " << it->ndof() << " " << TMath::Prob(it->chi2(), static_cast<int32_t>(it->ndof()));
581  Vertex vtx = *it;
582  bool pass = vertexCut_[tsNum]( vtx );
583  if( pass ) {
584  points.push_back(it->position());
585  vterr.push_back(sqrt(it->yError()*it->xError()));
586  vzerr.push_back(it->zError());
587  LogTrace("SelectVertex") << " SELECTED vertex with z position " << it->z();
588  toTake--; if (toTake == 0) break;
589  }
590  }
591 }
592 
593 void HIMultiTrackSelector::processMVA(edm::Event& evt, const edm::EventSetup& es, std::vector<float> & mvaVals_,const reco::VertexCollection &vertices ) const
594 {
595 
596  using namespace std;
597  using namespace edm;
598  using namespace reco;
599 
600  // Get tracks
601  Handle<TrackCollection> hSrcTrack;
602  evt.getByToken( src_, hSrcTrack );
603  const TrackCollection& srcTracks(*hSrcTrack);
604  assert(mvaVals_.size()==srcTracks.size());
605 
606  // get hits in track..
608  evt.getByToken( hSrc_, hSrcHits );
609  const TrackingRecHitCollection & srcHits(*hSrcHits);
610 
611 
612  auto mvaValValueMap = std::make_unique<edm::ValueMap<float>>();
613  edm::ValueMap<float>::Filler mvaFiller(*mvaValValueMap);
614 
615 
616  if(!useAnyMVA_){
617  // mvaVals_ already initalized...
618  mvaFiller.insert(hSrcTrack,mvaVals_.begin(),mvaVals_.end());
619  mvaFiller.fill();
620  evt.put(std::move(mvaValValueMap),"MVAVals");
621  return;
622  }
623 
624  bool checkvertex = std::find(mvavars_indices.begin(), mvavars_indices.end(),dxyperdxyerror)!=mvavars_indices.end()
626 
627  size_t current = 0;
628  for (TrackCollection::const_iterator it = srcTracks.begin(), ed = srcTracks.end(); it != ed; ++it, ++current) {
629  const Track & trk = * it;
630 
631  float mvavalues[15];
632  mvavalues[ndof] = trk.ndof();
633  mvavalues[nlayers] = trk.hitPattern().trackerLayersWithMeasurement();
636  mvavalues[chi2n_no1dmod] = trk.normalizedChi2();
637  mvavalues[chi2perdofperlayer] = mvavalues[chi2n_no1dmod]/mvavalues[nlayers];
638 
639 
640  float chi2n1d = trk.normalizedChi2();
641  int count1dhits = 0;
642  auto ith = trk.extra()->firstRecHit();
643  auto edh = ith + trk.recHitsSize();
644  for (; ith<edh; ++ith) {
645  const TrackingRecHit & hit = srcHits[ith];
646  if (hit.dimension()==1) ++count1dhits;
647  }
648  if (count1dhits > 0) {
649  float chi2 = trk.chi2();
650  float ndof = trk.ndof();
651  chi2n1d = (chi2+count1dhits)/float(ndof+count1dhits);
652  }
653 
654  mvavalues[chi2n] = chi2n1d;//chi2 and 1d modes
655 
656  mvavalues[eta] = trk.eta();
657  mvavalues[relpterr] = float(trk.ptError())/std::max(float(trk.pt()),0.000001f);
658  mvavalues[nhits] = trk.numberOfValidHits();
659 
662  mvavalues[minlost] = std::min(lostIn,lostOut);
663  mvavalues[lostmidfrac] = trk.numberOfLostHits() / (trk.numberOfValidHits() + trk.numberOfLostHits());
664 
665  mvavalues[etaerror] = trk.etaError();
666 
667  float reldz = 0;
668  float reldxy = 0;
669  if (checkvertex) {
670  int vtxind = 0; // only first vertex is taken into account for the speed purposes
671  float dxy = trk.dxy(vertices[vtxind].position()), dxyE = sqrt(trk.dxyError()*trk.dxyError()+vertices[vtxind].xError()*vertices[vtxind].yError());
672  float dz = trk.dz(vertices[vtxind].position()), dzE = sqrt(trk.dzError()*trk.dzError()+vertices[vtxind].zError()*vertices[vtxind].zError());
673  reldz = dz/dzE;
674  reldxy = dxy/dxyE;
675 
676  }
677  mvavalues[dxyperdxyerror] = reldxy;
678  mvavalues[dzperdzerror] = reldz;
679 
680  std::vector<float> gbrValues;
681 
682  //fill in the gbrValues vector with the necessary variables
683  for (unsigned i=0;i<mvavars_indices.size();i++) {
684  gbrValues.push_back(mvavalues[mvavars_indices[i]]);
685  }
686 
687 
688  GBRForest const * forest = forest_;
689  if(useForestFromDB_){
690  edm::ESHandle<GBRForest> forestHandle;
691  es.get<GBRWrapperRcd>().get(forestLabel_,forestHandle);
692  forest = forestHandle.product();
693  }
694 
695  auto gbrVal = forest->GetClassifier(&gbrValues[0]);
696  mvaVals_[current] = gbrVal;
697  }
698  mvaFiller.insert(hSrcTrack,mvaVals_.begin(),mvaVals_.end());
699  mvaFiller.fill();
700  evt.put(std::move(mvaValValueMap),"MVAVals");
701 
702 }
703 
706 
708 
#define LogDebug(id)
std::vector< std::string > name_
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
double d0Error() const
error on d0
Definition: TrackBase.h:802
std::vector< uint32_t > max_lostLayers_
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
~HIMultiTrackSelector() override
destructor
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
std::vector< uint32_t > min_hits_bypass_
const TrackExtraRef & extra() const
reference to "extra" object
Definition: Track.h:189
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:561
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
std::vector< std::vector< double > > pixel_pTMaxCut_
double dxyError() const
error on dxy
Definition: TrackBase.h:796
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< uint32_t > min_nhits_
size_t recHitsSize() const
Get number of RecHits. (Warning, this includes invalid hits, which are not physical hits)...
Definition: Track.h:119
std::vector< bool > useMVA_
std::vector< std::vector< double > > dz_par1_
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
std::vector< double > chi2n_par_
double etaError() const
error on eta
Definition: TrackBase.h:784
bool exists(std::string const &parameterName) const
checks if a parameter exists
void selectVertices(unsigned int tsNum, const reco::VertexCollection &vtxs, std::vector< Point > &points, std::vector< float > &vterr, std::vector< float > &vzerr) const
unsigned short numberOfLostHits() const
number of cases where track crossed a layer without getting a hit.
Definition: TrackBase.h:826
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
std::vector< std::vector< double > > res_par_
std::vector< int32_t > max_minMissHitOutOrIn_
std::vector< double > chi2n_no1Dmod_par_
std::vector< uint32_t > min_3Dlayers_
int pixelLayersWithMeasurement() const
Definition: HitPattern.cc:501
void beginStream(edm::StreamID) final
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
virtual void run(edm::Event &evt, const edm::EventSetup &es) const
int trackerLayersWithMeasurement() const
Definition: HitPattern.cc:520
std::vector< double > min_eta_
int numberOfLostTrackerHits(HitCategory category) const
Definition: HitPattern.h:908
std::vector< double > max_eta_
std::vector< std::vector< double > > d0_par2_
std::vector< double > max_d0NoPV_
std::vector< std::vector< double > > d0_par1_
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:651
int numberOfValidStripLayersWithMonoAndStereo(uint16_t stripdet, uint16_t layer) const
Definition: HitPattern.cc:343
edm::EDGetTokenT< reco::VertexCollection > vertices_
double chi2() const
chi-squared of the fit
Definition: TrackBase.h:549
std::vector< StringCutObjectSelector< reco::Vertex > > vertexCut_
std::vector< bool > setQualityBit_
do I have to set a quality bit?
double ndof() const
number of degrees of freedom of the fit
Definition: TrackBase.h:555
T sqrt(T t)
Definition: SSEVec.h:18
virtual int dimension() const =0
double pt() const
track transverse momentum
Definition: TrackBase.h:621
double ptError() const
error on Pt (set to 1000 TeV if charge==0 for safety)
Definition: TrackBase.h:763
std::vector< int32_t > vtxNumber_
vertex cuts
int qualityMask() const
Definition: TrackBase.h:862
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< double > max_z0_
double f[11][100]
unsigned short numberOfValidHits() const
number of valid hits found
Definition: TrackBase.h:820
edm::EDGetTokenT< TrackingRecHitCollection > hSrc_
#define end
Definition: vmac.h:39
T min(T a, T b)
Definition: MathUtil.h:58
std::vector< std::vector< double > > dz_par2_
edm::EDGetTokenT< reco::TrackCollection > src_
source collection label
#define LogTrace(id)
std::vector< double > nSigmaZ_
std::vector< double > max_d0_
Impact parameter absolute cuts.
std::vector< bool > applyAdaptedPVCuts_
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:609
double dzError() const
error on dz
Definition: TrackBase.h:814
std::vector< double > max_relpterr_
std::vector< int > mvavars_indices
void processMVA(edm::Event &evt, const edm::EventSetup &es, std::vector< float > &mvaVals_, const reco::VertexCollection &hVtx) const
#define N
Definition: blowfish.cc:9
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:446
std::vector< std::string > forestVars_
double sigmaZ() const
sigma z
Definition: BeamSpot.h:80
std::vector< int32_t > max_lostHitFraction_
edm::EDGetTokenT< reco::BeamSpot > beamspot_
std::vector< bool > applyAbsCutsIfNoPV_
int trackerLayersWithoutMeasurement(HitCategory category) const
Definition: HitPattern.cc:537
std::vector< uint32_t > min_layers_
Cuts on numbers of layers with hits/3D hits/lost hits.
fixed size matrix
HLT enums.
std::vector< std::vector< double > > pixel_pTMinCut_
static int position[264][3]
Definition: ReadPGInfo.cc:509
T get() const
Definition: EventSetup.h:63
HIMultiTrackSelector()
constructor
std::vector< double > max_z0NoPV_
std::vector< bool > keepAllTracks_
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:591
std::vector< double > min_MVA_
double GetClassifier(const float *vector) const
Definition: GBRForest.h:43
long double T
std::vector< unsigned int > preFilter_
T const * product() const
Definition: ESHandle.h:86
std::vector< reco::TrackBase::TrackQuality > qualityToSet_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
def move(src, dest)
Definition: eostools.py:510
*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