CMS 3D CMS Logo

TrimmedVertexFinder.cc
Go to the documentation of this file.
6 
7 using namespace reco;
8 
10  const VertexUpdator<5>* vu,
12  : theFitter(vf->clone()), theUpdator(vu->clone()), theEstimator(ve->clone()), theMinProb(0.05) {}
13 
15  : theFitter(other.theFitter->clone()),
16  theUpdator(other.theUpdator->clone()),
17  theEstimator(other.theEstimator->clone()),
18  theMinProb(other.theMinProb) {}
19 
21  delete theFitter;
22  delete theUpdator;
23  delete theEstimator;
24 }
25 
26 std::vector<TransientVertex> TrimmedVertexFinder::vertices(std::vector<TransientTrack>& tks) const {
27  // FIXME write this!!!
28  return vertices(tks, reco::BeamSpot(), false);
29 }
30 
31 std::vector<TransientVertex> TrimmedVertexFinder::vertices(std::vector<TransientTrack>& tks,
32  const reco::BeamSpot& spot,
33  bool use_spot) const {
34  std::vector<TransientVertex> all;
35  if (tks.size() < 2)
36  return all;
37 
38  // prepare vertex tracks and initial vertex
40  if (use_spot) {
41  vtx = theFitter->vertex(tks, spot);
42  } else {
43  vtx = theFitter->vertex(tks);
44  }
45  if (!vtx.isValid()) {
46  edm::LogWarning("TrimmedVertexFinder") << "initial vertex invalid."
47  << " vertex finding stops here.";
48  return all;
49  }
50 
51  std::vector<RefCountedVertexTrack> selected = vtx.tracks();
52 
53  // reject incompatible tracks starting from the worst
54  std::vector<RefCountedVertexTrack> remain;
55  bool found = false;
56  while (!found && selected.size() >= 2) {
57  // find track with worst compatibility
58  std::vector<RefCountedVertexTrack>::iterator iWorst = theWorst(vtx, selected, theMinProb);
59 
60  if (iWorst != selected.end()) {
61  // reject track
62  remain.push_back(*iWorst);
63  selected.erase(iWorst);
64 
65  if (selected.size() == 1) {
66  // not enough tracks to build new vertices
67  remain.push_back(selected.front());
68  } else {
69  // removing track from vertex
70  // need to redo the vertex fit instead of removing the track;
71  // successive removals lead to numerical problems
72  // this is however quick since intermediate quantities are cached
73  // in the RefCountedVertexTracks
74  if (use_spot) // && all.size()==0 )
75  {
76  vtx = theFitter->vertex(selected, spot);
77  } else {
78  vtx = theFitter->vertex(selected);
79  }
80  if (!vtx.isValid()) {
81  edm::LogWarning("TrimmedVertexFinder") << "current vertex invalid"
82  << "vertex finding stops here.";
83  return all;
84  }
85 
86  // ref-counted tracks may have changed during the fit
87  // if the linearization point has moved too much
88  selected = vtx.tracks();
89  }
90 
91  } else {
92  // no incompatible track remains, make vertex
93  found = true;
94  int n_tracks_in_vertex = selected.size();
95 
96  // now return all tracks with weight < 0.5 to 'remain'.
97  for (std::vector<RefCountedVertexTrack>::const_iterator t = selected.begin(); t != selected.end(); ++t) {
98  if ((**t).weight() < 0.5) {
99  /*
100  cout << "[TrimmedVertexFinder] recycling track with weight "
101  << (**t).weight() << endl;*/
102  remain.push_back(*t);
103  n_tracks_in_vertex--; // one 'good' track less in the vertex
104  };
105  };
106 
107  if (n_tracks_in_vertex > 1) {
108  all.push_back(vtx);
109  } else {
110  edm::LogError("TrimmedVertexFinder") << "found vertex has less than 2 tracks";
111  }
112  }
113  }
114 
115  // modify list of incompatible tracks
116  tks.clear();
117  for (std::vector<RefCountedVertexTrack>::const_iterator i = remain.begin(); i != remain.end(); i++) {
118  const PerigeeLinearizedTrackState* plts =
119  dynamic_cast<const PerigeeLinearizedTrackState*>((**i).linearizedTrack().get());
120  if (plts == nullptr) {
121  throw cms::Exception("TrimmedVertexFinder: can't take track from non-perigee track state");
122  }
123 
124  tks.push_back(plts->track());
125  }
126 
127  // return 0 or 1 vertex
128  return all;
129 }
130 
131 std::vector<TrimmedVertexFinder::RefCountedVertexTrack>::iterator TrimmedVertexFinder::theWorst(
132  const CachingVertex<5>& vtx, std::vector<RefCountedVertexTrack>& vtxTracks, float cut) const {
133  // cout << "Cut is now " << cut << endl;
134 
135  // find track with worst compatibility
136  std::vector<RefCountedVertexTrack>::iterator iWorst = vtxTracks.end();
137  float worseChi2 = 0.;
138  for (std::vector<RefCountedVertexTrack>::iterator itr = vtxTracks.begin(); itr != vtxTracks.end(); itr++) {
140  if (!newV.isValid())
141  return itr;
142  std::pair<bool, double> result = theEstimator->estimate(newV, *itr);
143  if (!result.first)
144  return itr;
145  float chi2 = result.second;
146 
147  // compute number of degrees of freedom
148  if (chi2 > 0.) {
149  // we want to keep negative chi squares, and avoid calling
150  // ChiSquaredProbability with a negative chi2.
151  int ndf = 2;
152  if (vtx.tracks().size() == 2)
153  ndf = 1;
154 
155  float prob = ChiSquaredProbability(chi2, ndf);
156  if (prob < cut && chi2 >= worseChi2) {
157  // small inconsistency: sorting on chi2, not on chi2 probability
158  // because large chi2 are not rounded off while small probabilities
159  // are rounded off to 0....
160  // should not matter since at a given iteration
161  // all track chi2 increments have the same ndf
162  iWorst = itr;
163  worseChi2 = chi2;
164  }
165  }
166  }
167 
168  return iWorst;
169 }
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
Log< level::Error, false > LogError
virtual CachingVertex< N > remove(const CachingVertex< N > &v, const typename CachingVertex< N >::RefCountedVertexTrack t) const =0
virtual CachingVertex< N > vertex(const std::vector< reco::TransientTrack > &tracks) const =0
virtual BDpair estimate(const CachingVertex< N > &v, const RefCountedLinearizedTrackState track, unsigned int hint=UINT_MAX) const =0
VertexUpdator< 5 > * theUpdator
VertexTrackCompatibilityEstimator< 5 > * theEstimator
float ChiSquaredProbability(double chiSquared, double nrDOF)
std::vector< RefCountedVertexTrack >::iterator theWorst(const CachingVertex< 5 > &vtx, std::vector< RefCountedVertexTrack > &vtxTracks, float cut) const
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
fixed size matrix
reco::TransientTrack track() const override
VertexFitter< 5 > * theFitter
Log< level::Warning, false > LogWarning
std::vector< TransientVertex > vertices(std::vector< reco::TransientTrack > &remain) const
TrimmedVertexFinder(const VertexFitter< 5 > *vf, const VertexUpdator< 5 > *vu, const VertexTrackCompatibilityEstimator< 5 > *ve)