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  forest_ = nullptr;
62 }
63 
65  src_( consumes<reco::TrackCollection>( cfg.getParameter<edm::InputTag>( "src" ) ) ),
66  hSrc_(consumes<TrackingRecHitCollection>( cfg.getParameter<edm::InputTag>( "src" ) ) ),
67  beamspot_( consumes<reco::BeamSpot>( cfg.getParameter<edm::InputTag>( "beamspot" ) ) ),
68  useVertices_( cfg.getParameter<bool>( "useVertices" ) ),
69  useVtxError_( cfg.getParameter<bool>( "useVtxError" ) )
70  // now get the pset for each selector
71 {
72  if (useVertices_) vertices_ = consumes<reco::VertexCollection>(cfg.getParameter<edm::InputTag>( "vertices" ));
73  if(useVtxError_){
74  edm::LogWarning("MultiTRackSelector") << "you are executing buggy code, if intentional please help to fix it";
75  }
76  useAnyMVA_ = false;
77  forestLabel_ = "MVASelectorIter0";
78  std::string type = "BDTG";
79  useForestFromDB_ = true;
80  dbFileName_ = "";
81 
82  forest_ = nullptr;
83 
84  if(cfg.exists("useAnyMVA")) useAnyMVA_ = cfg.getParameter<bool>("useAnyMVA");
85 
86  if(useAnyMVA_){
87  if(cfg.exists("mvaType"))type = cfg.getParameter<std::string>("mvaType");
88  if(cfg.exists("GBRForestLabel"))forestLabel_ = cfg.getParameter<std::string>("GBRForestLabel");
89  if(cfg.exists("GBRForestFileName")){
90  dbFileName_ = cfg.getParameter<std::string>("GBRForestFileName");
91  useForestFromDB_ = false;
92  }
93 
94  mvaType_ = type;
95  }
96  std::vector<edm::ParameterSet> trkSelectors( cfg.getParameter<std::vector< edm::ParameterSet> >("trackSelectors") );
97  qualityToSet_.reserve(trkSelectors.size());
98  vtxNumber_.reserve(trkSelectors.size());
99  vertexCut_.reserve(trkSelectors.size());
100  res_par_.reserve(trkSelectors.size());
101  chi2n_par_.reserve(trkSelectors.size());
102  chi2n_no1Dmod_par_.reserve(trkSelectors.size());
103  d0_par1_.reserve(trkSelectors.size());
104  dz_par1_.reserve(trkSelectors.size());
105  d0_par2_.reserve(trkSelectors.size());
106  dz_par2_.reserve(trkSelectors.size());
107  applyAdaptedPVCuts_.reserve(trkSelectors.size());
108  max_d0_.reserve(trkSelectors.size());
109  max_z0_.reserve(trkSelectors.size());
110  nSigmaZ_.reserve(trkSelectors.size());
111  min_layers_.reserve(trkSelectors.size());
112  min_3Dlayers_.reserve(trkSelectors.size());
113  max_lostLayers_.reserve(trkSelectors.size());
114  min_hits_bypass_.reserve(trkSelectors.size());
115  applyAbsCutsIfNoPV_.reserve(trkSelectors.size());
116  max_d0NoPV_.reserve(trkSelectors.size());
117  max_z0NoPV_.reserve(trkSelectors.size());
118  preFilter_.reserve(trkSelectors.size());
119  max_relpterr_.reserve(trkSelectors.size());
120  min_nhits_.reserve(trkSelectors.size());
121  max_minMissHitOutOrIn_.reserve(trkSelectors.size());
122  max_lostHitFraction_.reserve(trkSelectors.size());
123  min_eta_.reserve(trkSelectors.size());
124  max_eta_.reserve(trkSelectors.size());
125  useMVA_.reserve(trkSelectors.size());
126  //mvaReaders_.reserve(trkSelectors.size());
127  min_MVA_.reserve(trkSelectors.size());
128  //mvaType_.reserve(trkSelectors.size());
129 
130  produces<edm::ValueMap<float> >("MVAVals");
131 
132  for ( unsigned int i=0; i<trkSelectors.size(); i++) {
133 
134  qualityToSet_.push_back( TrackBase::undefQuality );
135  // parameters for vertex selection
136  vtxNumber_.push_back( useVertices_ ? trkSelectors[i].getParameter<int32_t>("vtxNumber") : 0 );
137  vertexCut_.push_back( useVertices_ ? trkSelectors[i].getParameter<std::string>("vertexCut") : 0);
138  // parameters for adapted optimal cuts on chi2 and primary vertex compatibility
139  res_par_.push_back(trkSelectors[i].getParameter< std::vector<double> >("res_par") );
140  chi2n_par_.push_back( trkSelectors[i].getParameter<double>("chi2n_par") );
141  chi2n_no1Dmod_par_.push_back( trkSelectors[i].getParameter<double>("chi2n_no1Dmod_par") );
142  d0_par1_.push_back(trkSelectors[i].getParameter< std::vector<double> >("d0_par1"));
143  dz_par1_.push_back(trkSelectors[i].getParameter< std::vector<double> >("dz_par1"));
144  d0_par2_.push_back(trkSelectors[i].getParameter< std::vector<double> >("d0_par2"));
145  dz_par2_.push_back(trkSelectors[i].getParameter< std::vector<double> >("dz_par2"));
146  // Boolean indicating if adapted primary vertex compatibility cuts are to be applied.
147  applyAdaptedPVCuts_.push_back(trkSelectors[i].getParameter<bool>("applyAdaptedPVCuts"));
148  // Impact parameter absolute cuts.
149  max_d0_.push_back(trkSelectors[i].getParameter<double>("max_d0"));
150  max_z0_.push_back(trkSelectors[i].getParameter<double>("max_z0"));
151  nSigmaZ_.push_back(trkSelectors[i].getParameter<double>("nSigmaZ"));
152  // Cuts on numbers of layers with hits/3D hits/lost hits.
153  min_layers_.push_back(trkSelectors[i].getParameter<uint32_t>("minNumberLayers") );
154  min_3Dlayers_.push_back(trkSelectors[i].getParameter<uint32_t>("minNumber3DLayers") );
155  max_lostLayers_.push_back(trkSelectors[i].getParameter<uint32_t>("maxNumberLostLayers"));
156  min_hits_bypass_.push_back(trkSelectors[i].getParameter<uint32_t>("minHitsToBypassChecks"));
157  // Flag to apply absolute cuts if no PV passes the selection
158  applyAbsCutsIfNoPV_.push_back(trkSelectors[i].getParameter<bool>("applyAbsCutsIfNoPV"));
159  keepAllTracks_.push_back( trkSelectors[i].getParameter<bool>("keepAllTracks"));
160  max_relpterr_.push_back(trkSelectors[i].getParameter<double>("max_relpterr"));
161  min_nhits_.push_back(trkSelectors[i].getParameter<uint32_t>("min_nhits"));
162  max_minMissHitOutOrIn_.push_back(
163  trkSelectors[i].existsAs<int32_t>("max_minMissHitOutOrIn") ?
164  trkSelectors[i].getParameter<int32_t>("max_minMissHitOutOrIn") : 99);
165  max_lostHitFraction_.push_back(
166  trkSelectors[i].existsAs<double>("max_lostHitFraction") ?
167  trkSelectors[i].getParameter<double>("max_lostHitFraction") : 1.0);
168  min_eta_.push_back(trkSelectors[i].existsAs<double>("min_eta") ?
169  trkSelectors[i].getParameter<double>("min_eta"):-9999);
170  max_eta_.push_back(trkSelectors[i].existsAs<double>("max_eta") ?
171  trkSelectors[i].getParameter<double>("max_eta"):9999);
172 
173  setQualityBit_.push_back( false );
174  std::string qualityStr = trkSelectors[i].getParameter<std::string>("qualityBit");
175  if (qualityStr != "") {
176  setQualityBit_[i] = true;
177  qualityToSet_[i] = TrackBase::qualityByName(trkSelectors[i].getParameter<std::string>("qualityBit"));
178  }
179 
180  if (setQualityBit_[i] && (qualityToSet_[i] == TrackBase::undefQuality)) throw cms::Exception("Configuration") <<
181  "You can't set the quality bit " << trkSelectors[i].getParameter<std::string>("qualityBit") << " as it is 'undefQuality' or unknown.\n";
182 
183  if (applyAbsCutsIfNoPV_[i]) {
184  max_d0NoPV_.push_back(trkSelectors[i].getParameter<double>("max_d0NoPV"));
185  max_z0NoPV_.push_back(trkSelectors[i].getParameter<double>("max_z0NoPV"));
186  }
187  else{//dummy values
188  max_d0NoPV_.push_back(0.);
189  max_z0NoPV_.push_back(0.);
190  }
191 
192  name_.push_back( trkSelectors[i].getParameter<std::string>("name") );
193 
194  preFilter_[i]=trkSelectors.size(); // no prefilter
195 
196  std::string pfName=trkSelectors[i].getParameter<std::string>("preFilterName");
197  if (pfName!="") {
198  bool foundPF=false;
199  for ( unsigned int j=0; j<i; j++)
200  if (name_[j]==pfName ) {
201  foundPF=true;
202  preFilter_[i]=j;
203  }
204  if ( !foundPF)
205  throw cms::Exception("Configuration") << "Invalid prefilter name in MultiTrackSelector "
206  << trkSelectors[i].getParameter<std::string>("preFilterName");
207 
208  }
209 
210  // produces<std::vector<int> >(name_[i]).setBranchAlias( name_[i] + "TrackQuals");
211  produces<edm::ValueMap<int> >(name_[i]).setBranchAlias( name_[i] + "TrackQuals");
212  if(useAnyMVA_){
213  bool thisMVA = false;
214  if(trkSelectors[i].exists("useMVA"))thisMVA = trkSelectors[i].getParameter<bool>("useMVA");
215  useMVA_.push_back(thisMVA);
216  if(thisMVA){
217  double minVal = -1;
218  if(trkSelectors[i].exists("minMVA"))minVal = trkSelectors[i].getParameter<double>("minMVA");
219  min_MVA_.push_back(minVal);
220 
221  }else{
222  min_MVA_.push_back(-9999.0);
223  }
224  }else{
225  min_MVA_.push_back(-9999.0);
226  }
227 
228  }
229 }
230 
232  delete forest_;
233 }
234 
235 
237  if(!useForestFromDB_){
238  TFile gbrfile(dbFileName_.c_str());
239  forest_ = (GBRForest*)gbrfile.Get(forestLabel_.c_str());
240  }
241 
242 }
243 
244 
245 
247 {
248  using namespace std;
249  using namespace edm;
250  using namespace reco;
251 
252  // Get tracks
253  Handle<TrackCollection> hSrcTrack;
254  evt.getByToken(src_, hSrcTrack );
255  const TrackCollection& srcTracks(*hSrcTrack);
256 
257  // get hits in track..
259  evt.getByToken(hSrc_, hSrcHits );
260  const TrackingRecHitCollection & srcHits(*hSrcHits);
261 
262 
263  // looking for the beam spot
265  evt.getByToken(beamspot_, hBsp);
266  const reco::BeamSpot& vertexBeamSpot(*hBsp);
267 
268 
269  // Select good primary vertices for use in subsequent track selection
271  if (useVertices_) evt.getByToken(vertices_, hVtx);
272 
273  unsigned int trkSize=srcTracks.size();
274  std::vector<int> selTracksSave( qualityToSet_.size()*trkSize,0);
275 
276  std::vector<float> mvaVals_(srcTracks.size(),-99.f);
277  processMVA(evt,es, mvaVals_);
278 
279  for (unsigned int i=0; i<qualityToSet_.size(); i++) {
280  std::vector<int> selTracks(trkSize,0);
281  auto_ptr<edm::ValueMap<int> > selTracksValueMap = auto_ptr<edm::ValueMap<int> >(new edm::ValueMap<int>);
282  edm::ValueMap<int>::Filler filler(*selTracksValueMap);
283 
284  std::vector<Point> points;
285  std::vector<float> vterr, vzerr;
286  if (useVertices_) selectVertices(i,*hVtx, points, vterr, vzerr);
287 
288  // Loop over tracks
289  size_t current = 0;
290  for (TrackCollection::const_iterator it = srcTracks.begin(), ed = srcTracks.end(); it != ed; ++it, ++current) {
291  const Track & trk = * it;
292  // Check if this track passes cuts
293 
294  LogTrace("TrackSelection") << "ready to check track with pt="<< trk.pt() ;
295 
296  //already removed
297  bool ok=true;
298  if (preFilter_[i]<i && selTracksSave[preFilter_[i]*trkSize+current] < 0) {
299  selTracks[current]=-1;
300  ok=false;
301  if ( !keepAllTracks_[i])
302  continue;
303  }
304  else {
305  float mvaVal = 0;
306  if(useAnyMVA_) mvaVal = mvaVals_[current];
307  ok = select(i,vertexBeamSpot, srcHits, trk, points, vterr, vzerr,mvaVal);
308  if (!ok) {
309  LogTrace("TrackSelection") << "track with pt="<< trk.pt() << " NOT selected";
310  if (!keepAllTracks_[i]) {
311  selTracks[current]=-1;
312  continue;
313  }
314  }
315  else
316  LogTrace("TrackSelection") << "track with pt="<< trk.pt() << " selected";
317  }
318 
319  if (preFilter_[i]<i ) {
320  selTracks[current]=selTracksSave[preFilter_[i]*trkSize+current];
321  }
322  else {
323  selTracks[current]=trk.qualityMask();
324  }
325  if ( ok && setQualityBit_[i]) {
326  selTracks[current]= (selTracks[current] | (1<<qualityToSet_[i]));
327  if (qualityToSet_[i]==TrackBase::tight) {
328  selTracks[current]=(selTracks[current] | (1<<TrackBase::loose));
329  }
330  else if (qualityToSet_[i]==TrackBase::highPurity) {
331  selTracks[current]=(selTracks[current] | (1<<TrackBase::loose));
332  selTracks[current]=(selTracks[current] | (1<<TrackBase::tight));
333  }
334  if (!points.empty()) {
335  if (qualityToSet_[i]==TrackBase::loose) {
336  selTracks[current]=(selTracks[current] | (1<<TrackBase::looseSetWithPV));
337  }
338  else if (qualityToSet_[i]==TrackBase::highPurity) {
339  selTracks[current]=(selTracks[current] | (1<<TrackBase::looseSetWithPV));
340  selTracks[current]=(selTracks[current] | (1<<TrackBase::highPuritySetWithPV));
341  }
342  }
343  }
344  }
345  for ( unsigned int j=0; j< trkSize; j++ ) selTracksSave[j+i*trkSize]=selTracks[j];
346  filler.insert(hSrcTrack, selTracks.begin(),selTracks.end());
347  filler.fill();
348 
349  // evt.put(selTracks,name_[i]);
350  evt.put(selTracksValueMap,name_[i]);
351  }
352 }
353 
354 
355  bool MultiTrackSelector::select(unsigned int tsNum,
356  const reco::BeamSpot &vertexBeamSpot,
358  const reco::Track &tk,
359  const std::vector<Point> &points,
360  std::vector<float> &vterr,
361  std::vector<float> &vzerr,
362  double mvaVal) const {
363  // Decide if the given track passes selection cuts.
364 
365  using namespace std;
366 
367  //cuts on number of valid hits
368  auto nhits = tk.numberOfValidHits();
369  if(nhits>=min_hits_bypass_[tsNum]) return true;
370  if(nhits < min_nhits_[tsNum]) return false;
371 
372  if ( tk.ndof() < 1E-5 ) return false;
373 
374 
376  //Adding the MVA selection before any other cut//
378  if(useAnyMVA_ && useMVA_[tsNum]){
379  if(mvaVal < min_MVA_[tsNum])return false;
380  }
382  //End of MVA selection section//
384 
385 
386  // Cuts on numbers of layers with hits/3D hits/lost hits.
387  uint32_t nlayers = tk.hitPattern().trackerLayersWithMeasurement();
388  uint32_t nlayers3D = tk.hitPattern().pixelLayersWithMeasurement() +
391  LogDebug("TrackSelection") << "cuts on nlayers: " << nlayers << " " << nlayers3D << " " << nlayersLost << " vs "
392  << min_layers_[tsNum] << " " << min_3Dlayers_[tsNum] << " " << max_lostLayers_[tsNum];
393  if (nlayers < min_layers_[tsNum]) return false;
394  if (nlayers3D < min_3Dlayers_[tsNum]) return false;
395  if (nlayersLost > max_lostLayers_[tsNum]) return false;
396  LogTrace("TrackSelection") << "cuts on nlayers passed";
397 
398  float chi2n = tk.normalizedChi2();
399  float chi2n_no1Dmod = chi2n;
400 
401  int count1dhits = 0;
402  auto ith = tk.extra()->firstRecHit();
403  auto edh = ith + tk.recHitsSize();
404  for (; ith<edh; ++ith) {
405  const TrackingRecHit & hit = recHits[ith];
406  if (hit.dimension()==1) ++count1dhits;
407  }
408  if (count1dhits > 0) {
409  float chi2 = tk.chi2();
410  float ndof = tk.ndof();
411  chi2n = (chi2+count1dhits)/float(ndof+count1dhits);
412  }
413  // For each 1D rechit, the chi^2 and ndof is increased by one. This is a way of retaining approximately
414  // the same normalized chi^2 distribution as with 2D rechits.
415  if (chi2n > chi2n_par_[tsNum]*nlayers) return false;
416 
417  if (chi2n_no1Dmod > chi2n_no1Dmod_par_[tsNum]*nlayers) return false;
418 
419  // Get track parameters
420  float pt = std::max(float(tk.pt()),0.000001f);
421  float eta = tk.eta();
422  if (eta<min_eta_[tsNum] || eta>max_eta_[tsNum]) return false;
423 
424  //cuts on relative error on pt
425  float relpterr = float(tk.ptError())/pt;
426  if(relpterr > max_relpterr_[tsNum]) return false;
427 
430  int minLost = std::min(lostIn,lostOut);
431  if (minLost > max_minMissHitOutOrIn_[tsNum]) return false;
432  float lostMidFrac = tk.numberOfLostHits() / (tk.numberOfValidHits() + tk.numberOfLostHits());
433  if (lostMidFrac > max_lostHitFraction_[tsNum]) return false;
434 
435 
436 
437  //other track parameters
438  float d0 = -tk.dxy(vertexBeamSpot.position()), d0E = tk.d0Error(),
439  dz = tk.dz(vertexBeamSpot.position()), dzE = tk.dzError();
440 
441  // parametrized d0 resolution for the track pt
442  float nomd0E = sqrt(res_par_[tsNum][0]*res_par_[tsNum][0]+(res_par_[tsNum][1]/pt)*(res_par_[tsNum][1]/pt));
443  // parametrized z0 resolution for the track pt and eta
444  float nomdzE = nomd0E*(std::cosh(eta));
445 
446  float dzCut = std::min( powN(dz_par1_[tsNum][0]*nlayers,int(dz_par1_[tsNum][1]+0.5))*nomdzE,
447  powN(dz_par2_[tsNum][0]*nlayers,int(dz_par2_[tsNum][1]+0.5))*dzE );
448  float d0Cut = std::min( powN(d0_par1_[tsNum][0]*nlayers,int(d0_par1_[tsNum][1]+0.5))*nomd0E,
449  powN(d0_par2_[tsNum][0]*nlayers,int(d0_par2_[tsNum][1]+0.5))*d0E );
450 
451 
452  // ---- PrimaryVertex compatibility cut
453  bool primaryVertexZCompatibility(false);
454  bool primaryVertexD0Compatibility(false);
455 
456  if (points.empty()) { //If not primaryVertices are reconstructed, check just the compatibility with the BS
457  //z0 within (n sigma + dzCut) of the beam spot z, if no good vertex is found
458  if ( abs(dz) < hypot(vertexBeamSpot.sigmaZ()*nSigmaZ_[tsNum],dzCut) ) primaryVertexZCompatibility = true;
459  // d0 compatibility with beam line
460  if (abs(d0) < d0Cut) primaryVertexD0Compatibility = true;
461  }
462 
463  int iv=0;
464  for (std::vector<Point>::const_iterator point = points.begin(), end = points.end(); point != end; ++point) {
465  LogTrace("TrackSelection") << "Test track w.r.t. vertex with z position " << point->z();
466  if(primaryVertexZCompatibility && primaryVertexD0Compatibility) break;
467  float dzPV = tk.dz(*point); //re-evaluate the dz with respect to the vertex position
468  float d0PV = tk.dxy(*point); //re-evaluate the dxy with respect to the vertex position
469  if(useVtxError_){
470  float dzErrPV = std::sqrt(dzE*dzE+vzerr[iv]*vzerr[iv]); // include vertex error in z
471  float d0ErrPV = std::sqrt(d0E*d0E+vterr[iv]*vterr[iv]); // include vertex error in xy
472  iv++;
473  if (abs(dzPV) < dz_par1_[tsNum][0]*pow(nlayers,dz_par1_[tsNum][1])*nomdzE &&
474  abs(dzPV) < dz_par2_[tsNum][0]*pow(nlayers,dz_par2_[tsNum][1])*dzErrPV &&
475  abs(dzPV) < max_z0_[tsNum]) primaryVertexZCompatibility = true;
476  if (abs(d0PV) < d0_par1_[tsNum][0]*pow(nlayers,d0_par1_[tsNum][1])*nomd0E &&
477  abs(d0PV) < d0_par2_[tsNum][0]*pow(nlayers,d0_par2_[tsNum][1])*d0ErrPV &&
478  abs(d0PV) < max_d0_[tsNum]) primaryVertexD0Compatibility = true;
479  }else{
480  if (abs(dzPV) < dzCut) primaryVertexZCompatibility = true;
481  if (abs(d0PV) < d0Cut) primaryVertexD0Compatibility = true;
482  }
483  LogTrace("TrackSelection") << "distances " << dzPV << " " << d0PV << " vs " << dzCut << " " << d0Cut;
484  }
485 
486  if (points.empty() && applyAbsCutsIfNoPV_[tsNum]) {
487  if ( abs(dz) > max_z0NoPV_[tsNum] || abs(d0) > max_d0NoPV_[tsNum]) return false;
488  } else {
489  // Absolute cuts on all tracks impact parameters with respect to beam-spot.
490  // If BS is not compatible, verify if at least the reco-vertex is compatible (useful for incorrect BS settings)
491  if (abs(d0) > max_d0_[tsNum] && !primaryVertexD0Compatibility) return false;
492  LogTrace("TrackSelection") << "absolute cuts on d0 passed";
493  if (abs(dz) > max_z0_[tsNum] && !primaryVertexZCompatibility) return false;
494  LogTrace("TrackSelection") << "absolute cuts on dz passed";
495  }
496 
497  LogTrace("TrackSelection") << "cuts on PV: apply adapted PV cuts? " << applyAdaptedPVCuts_[tsNum]
498  << " d0 compatibility? " << primaryVertexD0Compatibility
499  << " z compatibility? " << primaryVertexZCompatibility ;
500 
501  if (applyAdaptedPVCuts_[tsNum]) {
502  return (primaryVertexD0Compatibility && primaryVertexZCompatibility);
503  } else {
504  return true;
505  }
506 
507 }
508 
509  void MultiTrackSelector::selectVertices(unsigned int tsNum,
510  const reco::VertexCollection &vtxs,
511  std::vector<Point> &points,
512  std::vector<float> &vterr,
513  std::vector<float> &vzerr) const {
514  // Select good primary vertices
515  using namespace reco;
516  int32_t toTake = vtxNumber_[tsNum];
517  for (VertexCollection::const_iterator it = vtxs.begin(), ed = vtxs.end(); it != ed; ++it) {
518 
519  LogDebug("SelectVertex") << " select vertex with z position " << it->z() << " "
520  << it->chi2() << " " << it->ndof() << " " << TMath::Prob(it->chi2(), static_cast<int32_t>(it->ndof()));
521  Vertex vtx = *it;
522  bool pass = vertexCut_[tsNum]( vtx );
523  if( pass ) {
524  points.push_back(it->position());
525  vterr.push_back(sqrt(it->yError()*it->xError()));
526  vzerr.push_back(it->zError());
527  LogTrace("SelectVertex") << " SELECTED vertex with z position " << it->z();
528  toTake--; if (toTake == 0) break;
529  }
530  }
531 }
532 
533 void MultiTrackSelector::processMVA(edm::Event& evt, const edm::EventSetup& es, std::vector<float> & mvaVals_) const
534 {
535 
536  using namespace std;
537  using namespace edm;
538  using namespace reco;
539 
540  // Get tracks
541  Handle<TrackCollection> hSrcTrack;
542  evt.getByToken( src_, hSrcTrack );
543  const TrackCollection& srcTracks(*hSrcTrack);
544  assert(mvaVals_.size()==srcTracks.size());
545 
546  // get hits in track..
548  evt.getByToken( hSrc_, hSrcHits );
549  const TrackingRecHitCollection & srcHits(*hSrcHits);
550 
551 
552  auto_ptr<edm::ValueMap<float> >mvaValValueMap = auto_ptr<edm::ValueMap<float> >(new edm::ValueMap<float>);
553  edm::ValueMap<float>::Filler mvaFiller(*mvaValValueMap);
554 
555 
556  if(!useAnyMVA_){
557  // mvaVals_ already initalized...
558  mvaFiller.insert(hSrcTrack,mvaVals_.begin(),mvaVals_.end());
559  mvaFiller.fill();
560  evt.put(mvaValValueMap,"MVAVals");
561  return;
562  }
563 
564 
565  size_t current = 0;
566  for (TrackCollection::const_iterator it = srcTracks.begin(), ed = srcTracks.end(); it != ed; ++it, ++current) {
567  const Track & trk = * it;
568  auto tmva_ndof_ = trk.ndof();
569  auto tmva_nlayers_ = trk.hitPattern().trackerLayersWithMeasurement();
570  auto tmva_nlayers3D_ = trk.hitPattern().pixelLayersWithMeasurement()
573  float chi2n = trk.normalizedChi2();
574  float chi2n_no1Dmod = chi2n;
575 
576  int count1dhits = 0;
577  auto ith = trk.extra()->firstRecHit();
578  auto edh = ith + trk.recHitsSize();
579  for (; ith<edh; ++ith) {
580  const TrackingRecHit & hit = srcHits[ith];
581  if (hit.dimension()==1) ++count1dhits;
582  }
583  if (count1dhits > 0) {
584  float chi2 = trk.chi2();
585  float ndof = trk.ndof();
586  chi2n = (chi2+count1dhits)/float(ndof+count1dhits);
587  }
588  auto tmva_chi2n_ = chi2n;
589  auto tmva_chi2n_no1dmod_ = chi2n_no1Dmod;
590  auto tmva_eta_ = trk.eta();
591  auto tmva_relpterr_ = float(trk.ptError())/std::max(float(trk.pt()),0.000001f);
592  auto tmva_nhits_ = trk.numberOfValidHits();
595  auto tmva_minlost_ = std::min(lostIn,lostOut);
596  auto tmva_lostmidfrac_ = trk.numberOfLostHits() / (trk.numberOfValidHits() + trk.numberOfLostHits());
597 
598  float gbrVals_[11];
599  gbrVals_[0] = tmva_lostmidfrac_;
600  gbrVals_[1] = tmva_minlost_;
601  gbrVals_[2] = tmva_nhits_;
602  gbrVals_[3] = tmva_relpterr_;
603  gbrVals_[4] = tmva_eta_;
604  gbrVals_[5] = tmva_chi2n_no1dmod_;
605  gbrVals_[6] = tmva_chi2n_;
606  gbrVals_[7] = tmva_nlayerslost_;
607  gbrVals_[8] = tmva_nlayers3D_;
608  gbrVals_[9] = tmva_nlayers_;
609  gbrVals_[10] = tmva_ndof_;
610 
611 
612  GBRForest const * forest = forest_;
613  if(useForestFromDB_){
614  edm::ESHandle<GBRForest> forestHandle;
615  es.get<GBRWrapperRcd>().get(forestLabel_,forestHandle);
616  forest = forestHandle.product();
617  }
618 
619  auto gbrVal = forest->GetClassifier(gbrVals_);
620  mvaVals_[current] = gbrVal;
621  }
622  mvaFiller.insert(hSrcTrack,mvaVals_.begin(),mvaVals_.end());
623  mvaFiller.fill();
624  evt.put(mvaValValueMap,"MVAVals");
625 
626 }
627 
630 
632 
#define LogDebug(id)
std::vector< double > chi2n_no1Dmod_par_
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
std::vector< uint32_t > min_nhits_
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:850
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 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:609
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
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
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:874
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< unsigned int > preFilter_
int pixelLayersWithMeasurement() const
Definition: HitPattern.cc:458
T eta() const
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:699
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:597
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
double ndof() const
number of degrees of freedom of the fit
Definition: TrackBase.h:603
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:669
double ptError() const
error on Pt (set to 1000 TeV if charge==0 for safety)
Definition: TrackBase.h:811
int qualityMask() const
Definition: TrackBase.h:904
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:868
#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:657
double dzError() const
error on dz
Definition: TrackBase.h:862
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:384
const T & get() const
Definition: EventSetup.h:55
double sigmaZ() const
sigma z
Definition: BeamSpot.h:80
void processMVA(edm::Event &evt, const edm::EventSetup &es, std::vector< float > &mvaVals_) const
T const * product() const
Definition: ESHandle.h:86
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_
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:639
std::vector< std::vector< double > > d0_par2_
double GetClassifier(const float *vector) const
Definition: GBRForest.h:64
long double T
std::vector< double > max_d0_
Impact parameter absolute cuts.
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_