CMS 3D CMS Logo

AdaptiveVertexReconstructor.cc
Go to the documentation of this file.
7 #include <algorithm>
8 
9 using namespace std;
10 
12  vector<reco::TransientTrack> trks = old.originalTracks();
13  vector<reco::TransientTrack> newtrks;
15  static const float minweight = 1.e-8; // discard all tracks with lower weight
16  for (vector<reco::TransientTrack>::const_iterator i = trks.begin(); i != trks.end(); ++i) {
17  if (old.trackWeight(*i) > minweight) {
18  newtrks.push_back(*i);
19  mp[*i] = old.trackWeight(*i);
20  }
21  }
22 
24 
25  if (old.hasPrior()) {
26  VertexState priorstate(old.priorPosition(), old.priorError());
27  ret = TransientVertex(priorstate, old.vertexState(), newtrks, old.totalChiSquared(), old.degreesOfFreedom());
28  } else {
29  ret = TransientVertex(old.vertexState(), newtrks, old.totalChiSquared(), old.degreesOfFreedom());
30  }
31  ret.weightMap(mp); // set weight map
32 
33  if (old.hasRefittedTracks()) {
34  // we have refitted tracks -- copy them!
35  vector<reco::TransientTrack> newrfs;
36  vector<reco::TransientTrack> oldrfs = old.refittedTracks();
37  vector<reco::TransientTrack>::const_iterator origtrkiter = trks.begin();
38  for (vector<reco::TransientTrack>::const_iterator i = oldrfs.begin(); i != oldrfs.end(); ++i) {
39  if (old.trackWeight(*origtrkiter) > minweight) {
40  newrfs.push_back(*i);
41  }
42  origtrkiter++;
43  }
44  if (!newrfs.empty())
45  ret.refittedTracks(newrfs); // copy refitted tracks
46  }
47 
48  if (ret.refittedTracks().size() > ret.originalTracks().size()) {
49  edm::LogError("AdaptiveVertexReconstructor") << "More refitted tracks than original tracks!";
50  }
51 
52  return ret;
53 }
54 
56  set<reco::TransientTrack>& remainingtrks,
57  float w) const {
58  /*
59  * Erase tracks that are in newvtx from remainingtrks
60  * But erase only if trackweight > w
61  */
62  const vector<reco::TransientTrack>& origtrks = newvtx.originalTracks();
63  for (vector<reco::TransientTrack>::const_iterator i = origtrks.begin(); i != origtrks.end(); ++i) {
64  double weight = newvtx.trackWeight(*i);
65  if (weight > w) {
66  remainingtrks.erase(*i);
67  };
68  };
69 }
70 
72  : thePrimaryFitter(nullptr), theSecondaryFitter(nullptr), theMinWeight(min_weight), theWeightThreshold(0.001) {
73  setupFitters(primcut, 256., 0.25, seccut, 256., 0.25, smoothing);
74 }
75 
77  if (thePrimaryFitter)
78  delete thePrimaryFitter;
80  delete theSecondaryFitter;
81 }
82 
84  float primcut, float primT, float primr, float seccut, float secT, float secr, bool smoothing) {
85  VertexSmoother<5>* smoother;
86  if (smoothing) {
87  smoother = new KalmanVertexSmoother();
88  } else {
89  smoother = new DummyVertexSmoother<5>();
90  }
91 
92  if (thePrimaryFitter)
93  delete thePrimaryFitter;
95  delete theSecondaryFitter;
96 
97  /*
98  edm::LogError ("AdaptiveVertexReconstructor" )
99  << "Tini and r are hardcoded now!";
100  */
105  *smoother);
107  // if the primary fails, sth is wrong, so here we set a threshold on the weight.
112  *smoother);
114  // need to set it or else we have
115  // unwanted exceptions to deal with.
116  // cleanup can come later!
117  delete smoother;
118 }
119 
121  : thePrimaryFitter(nullptr), theSecondaryFitter(nullptr), theMinWeight(0.5), theWeightThreshold(0.001) {
122  float primcut = 2.0;
123  float seccut = 6.0;
124  bool smoothing = false;
125  // float primT = 4096.;
126  // float primr = 0.125;
127  float primT = 256.;
128  float primr = 0.25;
129  float secT = 256.;
130  float secr = 0.25;
131 
132  try {
133  primcut = m.getParameter<double>("primcut");
134  primT = m.getParameter<double>("primT");
135  primr = m.getParameter<double>("primr");
136  seccut = m.getParameter<double>("seccut");
137  secT = m.getParameter<double>("secT");
138  secr = m.getParameter<double>("secr");
139  theMinWeight = m.getParameter<double>("minweight");
140  theWeightThreshold = m.getParameter<double>("weightthreshold");
141  smoothing = m.getParameter<bool>("smoothing");
142  } catch (edm::Exception& e) {
143  edm::LogError("AdaptiveVertexReconstructor") << e.what();
144  }
145 
146  setupFitters(primcut, primT, primr, seccut, secT, secr, smoothing);
147 }
148 
149 vector<TransientVertex> AdaptiveVertexReconstructor::vertices(const vector<reco::TransientTrack>& t,
150  const reco::BeamSpot& s) const {
151  return vertices(vector<reco::TransientTrack>(), t, s, false, true);
152 }
153 
154 vector<TransientVertex> AdaptiveVertexReconstructor::vertices(const vector<reco::TransientTrack>& primaries,
155  const vector<reco::TransientTrack>& tracks,
156  const reco::BeamSpot& s) const {
157  return vertices(primaries, tracks, s, true, true);
158 }
159 
160 vector<TransientVertex> AdaptiveVertexReconstructor::vertices(const vector<reco::TransientTrack>& tracks) const {
161  return vertices(vector<reco::TransientTrack>(), tracks, reco::BeamSpot(), false, false);
162 }
163 
164 vector<TransientVertex> AdaptiveVertexReconstructor::vertices(const vector<reco::TransientTrack>& primaries,
165  const vector<reco::TransientTrack>& tracks,
166  const reco::BeamSpot& s,
167  bool has_primaries,
168  bool usespot) const {
169  vector<TransientVertex> ret;
170  set<reco::TransientTrack> remainingtrks;
171 
172  copy(tracks.begin(), tracks.end(), inserter(remainingtrks, remainingtrks.begin()));
173 
174  int ctr = 0;
175  unsigned int n_tracks = remainingtrks.size();
176 
177  // cout << "[AdaptiveVertexReconstructor] DEBUG ::vertices!!" << endl;
178  try {
179  while (remainingtrks.size() > 1) {
180  /*
181  cout << "[AdaptiveVertexReconstructor] next round: "
182  << remainingtrks.size() << endl;
183  */
184  ctr++;
186  if (ret.empty()) {
187  fitter = thePrimaryFitter;
188  };
189  vector<reco::TransientTrack> fittrks;
190  fittrks.reserve(remainingtrks.size());
191 
192  copy(remainingtrks.begin(), remainingtrks.end(), back_inserter(fittrks));
193 
194  TransientVertex tmpvtx;
195  if ((ret.empty()) && has_primaries) {
196  // add the primaries to the fitted tracks.
197  copy(primaries.begin(), primaries.end(), back_inserter(fittrks));
198  }
199  if ((ret.empty()) && usespot) {
200  tmpvtx = fitter->vertex(fittrks, s);
201  } else {
202  tmpvtx = fitter->vertex(fittrks);
203  }
204  TransientVertex newvtx = cleanUp(tmpvtx);
205  ret.push_back(newvtx);
206  erase(newvtx, remainingtrks, theMinWeight);
207  if (n_tracks == remainingtrks.size()) {
208  if (usespot) {
209  // try once more without beamspot constraint!
210  usespot = false;
211  LogDebug("AdaptiveVertexReconstructor") << "no tracks in vertex. trying again without beamspot constraint!";
212  continue;
213  }
214  LogDebug("AdaptiveVertexReconstructor")
215  << "all tracks (" << n_tracks << ") would be recycled for next fit. Trying with low threshold!";
216  erase(newvtx, remainingtrks, 1.e-5);
217  if (n_tracks == remainingtrks.size()) {
218  LogDebug("AdaptiveVertexReconstructor") << "low threshold didnt help! "
219  << "Discontinue procedure!";
220  break;
221  }
222  };
223 
224  // cout << "[AdaptiveVertexReconstructor] erased" << endl;
225  n_tracks = remainingtrks.size();
226  };
227  } catch (VertexException& v) {
228  // Will catch all (not enough significant tracks exceptions.
229  // in this case, the iteration can safely terminate.
230  };
231 
232  return cleanUpVertices(ret);
233 }
234 
235 vector<TransientVertex> AdaptiveVertexReconstructor::cleanUpVertices(const vector<TransientVertex>& old) const {
236  vector<TransientVertex> ret;
237  for (vector<TransientVertex>::const_iterator i = old.begin(); i != old.end(); ++i) {
238  if (!(i->hasTrackWeight())) { // if we dont have track weights, we take the vtx
239  ret.push_back(*i);
240  continue;
241  }
242 
243  // maybe this should be replaced with asking for the ndf ...
244  // e.g. if ( ndf > - 1. )
245  int nsig = 0; // number of significant tracks.
247  for (TransientVertex::TransientTrackToFloatMap::const_iterator w = wm.begin(); w != wm.end(); ++w) {
248  if (w->second > theWeightThreshold)
249  nsig++;
250  }
251  if (nsig > 1)
252  ret.push_back(*i);
253  }
254 
255  return ret;
256 }
AdaptiveVertexReconstructor(float primcut=2.0, float seccut=6.0, float minweight=0.5, bool smoothing=false)
float totalChiSquared() const
std::vector< TransientVertex > cleanUpVertices(const std::vector< TransientVertex > &) const
T w() const
Common base class.
ret
prodAgent to be discontinued
std::map< reco::TransientTrack, float > TransientTrackToFloatMap
Definition: weight.py:1
Log< level::Error, false > LogError
CachingVertex< 5 > vertex(const std::vector< reco::TransientTrack > &) const override
float degreesOfFreedom() const
bool hasPrior() const
void setupFitters(float primcut, float primT, float primr, float seccut, float secT, float secr, bool smoothing)
GlobalPoint priorPosition() const
VertexState const & vertexState() const
std::vector< reco::TransientTrack > const & originalTracks() const
AdaptiveVertexFitter * theSecondaryFitter
void erase(const TransientVertex &newvtx, std::set< reco::TransientTrack > &remainingtrks, float w) const
GlobalError priorError() const
bool hasRefittedTracks() const
TransientVertex cleanUp(const TransientVertex &old) const
std::vector< TransientVertex > vertices(const std::vector< reco::TransientTrack > &v) const override
float trackWeight(const reco::TransientTrack &track) const
std::vector< reco::TransientTrack > const & refittedTracks() const
#define LogDebug(id)