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  unsigned int n_tracks = remainingtrks.size();
175 
176  // cout << "[AdaptiveVertexReconstructor] DEBUG ::vertices!!" << endl;
177  try {
178  while (remainingtrks.size() > 1) {
179  /*
180  cout << "[AdaptiveVertexReconstructor] next round: "
181  << remainingtrks.size() << endl;
182  */
184  if (ret.empty()) {
185  fitter = thePrimaryFitter;
186  };
187  vector<reco::TransientTrack> fittrks;
188  fittrks.reserve(remainingtrks.size());
189 
190  copy(remainingtrks.begin(), remainingtrks.end(), back_inserter(fittrks));
191 
192  TransientVertex tmpvtx;
193  if ((ret.empty()) && has_primaries) {
194  // add the primaries to the fitted tracks.
195  copy(primaries.begin(), primaries.end(), back_inserter(fittrks));
196  }
197  if ((ret.empty()) && usespot) {
198  tmpvtx = fitter->vertex(fittrks, s);
199  } else {
200  tmpvtx = fitter->vertex(fittrks);
201  }
202  TransientVertex newvtx = cleanUp(tmpvtx);
203  ret.push_back(newvtx);
204  erase(newvtx, remainingtrks, theMinWeight);
205  if (n_tracks == remainingtrks.size()) {
206  if (usespot) {
207  // try once more without beamspot constraint!
208  usespot = false;
209  LogDebug("AdaptiveVertexReconstructor") << "no tracks in vertex. trying again without beamspot constraint!";
210  continue;
211  }
212  LogDebug("AdaptiveVertexReconstructor")
213  << "all tracks (" << n_tracks << ") would be recycled for next fit. Trying with low threshold!";
214  erase(newvtx, remainingtrks, 1.e-5);
215  if (n_tracks == remainingtrks.size()) {
216  LogDebug("AdaptiveVertexReconstructor") << "low threshold didnt help! "
217  << "Discontinue procedure!";
218  break;
219  }
220  };
221 
222  // cout << "[AdaptiveVertexReconstructor] erased" << endl;
223  n_tracks = remainingtrks.size();
224  };
225  } catch (VertexException& v) {
226  // Will catch all (not enough significant tracks exceptions.
227  // in this case, the iteration can safely terminate.
228  };
229 
230  return cleanUpVertices(ret);
231 }
232 
233 vector<TransientVertex> AdaptiveVertexReconstructor::cleanUpVertices(const vector<TransientVertex>& old) const {
234  vector<TransientVertex> ret;
235  for (vector<TransientVertex>::const_iterator i = old.begin(); i != old.end(); ++i) {
236  if (!(i->hasTrackWeight())) { // if we dont have track weights, we take the vtx
237  ret.push_back(*i);
238  continue;
239  }
240 
241  // maybe this should be replaced with asking for the ndf ...
242  // e.g. if ( ndf > - 1. )
243  int nsig = 0; // number of significant tracks.
245  for (TransientVertex::TransientTrackToFloatMap::const_iterator w = wm.begin(); w != wm.end(); ++w) {
246  if (w->second > theWeightThreshold)
247  nsig++;
248  }
249  if (nsig > 1)
250  ret.push_back(*i);
251  }
252 
253  return ret;
254 }
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)