CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SequentialVertexFitter.cc
Go to the documentation of this file.
4 
5 #include <algorithm>
6 using namespace std;
7 using namespace reco;
8 
9 namespace {
10  // FIXME
11  // hard-coded tracker bounds
12  // workaround while waiting for Geometry service
13  const float TrackerBoundsRadius = 112;
14  const float TrackerBoundsHalfLength = 273.5;
15  bool insideTrackerBounds(const GlobalPoint& point) {
16  return ((point.transverse() < TrackerBoundsRadius) && (abs(point.z()) < TrackerBoundsHalfLength));
17  }
18 } // namespace
19 
20 template <unsigned int N>
23  const VertexSmoother<N>& smoother,
24  const AbstractLTSFactory<N>& ltsf)
25  : theLinP(linP.clone()),
26  theUpdator(updator.clone()),
27  theSmoother(smoother.clone()),
28  theLTrackFactory(ltsf.clone()) {
30 }
31 
32 template <unsigned int N>
34  const LinearizationPointFinder& linP,
36  const VertexSmoother<N>& smoother,
37  const AbstractLTSFactory<N>& ltsf)
38  : thePSet(pSet),
39  theLinP(linP.clone()),
40  theUpdator(updator.clone()),
41  theSmoother(smoother.clone()),
42  theLTrackFactory(ltsf.clone()) {
44 }
45 
46 template <unsigned int N>
48  thePSet = original.parameterSet();
49  theLinP = original.linearizationPointFinder()->clone();
50  theUpdator = original.vertexUpdator()->clone();
51  theSmoother = original.vertexSmoother()->clone();
52  theMaxShift = original.maxShift();
53  theMaxStep = original.maxStep();
54  theLTrackFactory = original.linearizedTrackStateFactory()->clone();
55 }
56 
57 template <unsigned int N>
59  delete theLinP;
60  delete theUpdator;
61  delete theSmoother;
62  delete theLTrackFactory;
63 }
64 
65 template <unsigned int N>
67  theMaxShift = thePSet.getParameter<double>("maxDistance"); //0.01
68  theMaxStep = thePSet.getParameter<int>("maxNbrOfIterations"); //10
69 }
70 
71 template <unsigned int N>
73  thePSet.addParameter<double>("maxDistance", 0.01);
74  thePSet.addParameter<int>("maxNbrOfIterations", 10); //10
75  readParameters();
76 }
77 
78 template <unsigned int N>
79 CachingVertex<N> SequentialVertexFitter<N>::vertex(const std::vector<reco::TransientTrack>& tracks) const {
80  // Linearization Point
81  GlobalPoint linP = theLinP->getLinearizationPoint(tracks);
82  if (!insideTrackerBounds(linP))
83  linP = GlobalPoint(0, 0, 0);
84 
85  // Initial vertex state, with a very large error matrix
86  ROOT::Math::SMatrixIdentity id;
87  AlgebraicSymMatrix33 we(id);
88  GlobalError error(we * 10000);
89  VertexState state(linP, error);
90  std::vector<RefCountedVertexTrack> vtContainer = linearizeTracks(tracks, state);
91  return fit(vtContainer, state, false);
92 }
93 
94 template <unsigned int N>
95 CachingVertex<N> SequentialVertexFitter<N>::vertex(const std::vector<RefCountedVertexTrack>& tracks,
96  const reco::BeamSpot& spot) const {
97  VertexState state(spot);
98  return fit(tracks, state, true);
99 }
100 
101 template <unsigned int N>
102 CachingVertex<N> SequentialVertexFitter<N>::vertex(const std::vector<RefCountedVertexTrack>& tracks) const {
103  // Initial vertex state, with a very small weight matrix
104  GlobalPoint linP = tracks[0]->linearizedTrack()->linearizationPoint();
105  ROOT::Math::SMatrixIdentity id;
106  AlgebraicSymMatrix33 we(id);
107  GlobalError error(we * 10000);
108  VertexState state(linP, error);
109  return fit(tracks, state, false);
110 }
111 
112 // Fit vertex out of a set of RecTracks.
113 // Uses the specified linearization point.
114 //
115 template <unsigned int N>
116 CachingVertex<N> SequentialVertexFitter<N>::vertex(const std::vector<reco::TransientTrack>& tracks,
117  const GlobalPoint& linPoint) const {
118  // Initial vertex state, with a very large error matrix
119  ROOT::Math::SMatrixIdentity id;
120  AlgebraicSymMatrix33 we(id);
121  GlobalError error(we * 10000);
122  VertexState state(linPoint, error);
123  std::vector<RefCountedVertexTrack> vtContainer = linearizeTracks(tracks, state);
124  return fit(vtContainer, state, false);
125 }
126 
131 template <unsigned int N>
132 CachingVertex<N> SequentialVertexFitter<N>::vertex(const std::vector<reco::TransientTrack>& tracks,
133  const BeamSpot& beamSpot) const {
134  VertexState beamSpotState(beamSpot);
135  std::vector<RefCountedVertexTrack> vtContainer;
136 
137  if (tracks.size() > 1) {
138  // Linearization Point search if there are more than 1 track
139  GlobalPoint linP = theLinP->getLinearizationPoint(tracks);
140  if (!insideTrackerBounds(linP))
141  linP = GlobalPoint(0, 0, 0);
142  ROOT::Math::SMatrixIdentity id;
143  AlgebraicSymMatrix33 we(id);
144  GlobalError error(we * 10000);
145  VertexState lpState(linP, error);
146  vtContainer = linearizeTracks(tracks, lpState);
147  } else {
148  // otherwise take the beamspot position.
149  vtContainer = linearizeTracks(tracks, beamSpotState);
150  }
151 
152  return fit(vtContainer, beamSpotState, true);
153 }
154 
155 // Fit vertex out of a set of RecTracks.
156 // Uses the position as both the linearization point AND as prior
157 // estimate of the vertex position. The error is used for the
158 // weight of the prior estimate.
159 //
160 template <unsigned int N>
161 CachingVertex<N> SequentialVertexFitter<N>::vertex(const std::vector<reco::TransientTrack>& tracks,
162  const GlobalPoint& priorPos,
163  const GlobalError& priorError) const {
164  VertexState state(priorPos, priorError);
165  std::vector<RefCountedVertexTrack> vtContainer = linearizeTracks(tracks, state);
166  return fit(vtContainer, state, true);
167 }
168 
169 // Fit vertex out of a set of VertexTracks
170 // Uses the position and error for the prior estimate of the vertex.
171 // This position is not used to relinearize the tracks.
172 //
173 template <unsigned int N>
174 CachingVertex<N> SequentialVertexFitter<N>::vertex(const std::vector<RefCountedVertexTrack>& tracks,
175  const GlobalPoint& priorPos,
176  const GlobalError& priorError) const {
177  VertexState state(priorPos, priorError);
178  return fit(tracks, state, true);
179 }
180 
181 // Construct a container of VertexTrack from a set of RecTracks.
182 //
183 template <unsigned int N>
184 vector<typename SequentialVertexFitter<N>::RefCountedVertexTrack> SequentialVertexFitter<N>::linearizeTracks(
185  const std::vector<reco::TransientTrack>& tracks, const VertexState state) const {
186  GlobalPoint linP = state.position();
187  std::vector<RefCountedVertexTrack> finalTracks;
188  finalTracks.reserve(tracks.size());
189  for (vector<reco::TransientTrack>::const_iterator i = tracks.begin(); i != tracks.end(); i++) {
190  RefCountedLinearizedTrackState lTrData = theLTrackFactory->linearizedTrackState(linP, *i);
191  RefCountedVertexTrack vTrData = theVTrackFactory.vertexTrack(lTrData, state);
192  finalTracks.push_back(vTrData);
193  }
194  return finalTracks;
195 }
196 
197 // Construct new a container of VertexTrack with a new linearization point
198 // and vertex state, from an existing set of VertexTrack, from which only the
199 // recTracks will be used.
200 //
201 template <unsigned int N>
202 vector<typename SequentialVertexFitter<N>::RefCountedVertexTrack> SequentialVertexFitter<N>::reLinearizeTracks(
203  const std::vector<RefCountedVertexTrack>& tracks, const VertexState state) const {
204  GlobalPoint linP = state.position();
205  std::vector<RefCountedVertexTrack> finalTracks;
206  finalTracks.reserve(tracks.size());
207  for (typename std::vector<RefCountedVertexTrack>::const_iterator i = tracks.begin(); i != tracks.end(); i++) {
208  RefCountedLinearizedTrackState lTrData = (**i).linearizedTrack()->stateWithNewLinearizationPoint(linP);
209  // RefCountedLinearizedTrackState lTrData =
210  // theLTrackFactory->linearizedTrackState(linP,
211  // (**i).linearizedTrack()->track());
212  RefCountedVertexTrack vTrData = theVTrackFactory.vertexTrack(lTrData, state, (**i).weight());
213  finalTracks.push_back(vTrData);
214  }
215  return finalTracks;
216 }
217 
218 // The method where the vertex fit is actually done!
219 //
220 template <unsigned int N>
221 CachingVertex<N> SequentialVertexFitter<N>::fit(const std::vector<RefCountedVertexTrack>& tracks,
222  const VertexState priorVertex,
223  bool withPrior) const {
224  std::vector<RefCountedVertexTrack> initialTracks;
225  GlobalPoint priorVertexPosition = priorVertex.position();
226  GlobalError priorVertexError = priorVertex.error();
227 
228  CachingVertex<N> returnVertex(priorVertexPosition, priorVertexError, initialTracks, 0);
229  if (withPrior) {
230  returnVertex = CachingVertex<N>(
231  priorVertexPosition, priorVertexError, priorVertexPosition, priorVertexError, initialTracks, 0);
232  }
233  CachingVertex<N> initialVertex = returnVertex;
234  std::vector<RefCountedVertexTrack> globalVTracks = tracks;
235 
236  // main loop through all the VTracks
237  bool validVertex = true;
238  int step = 0;
239  GlobalPoint newPosition = priorVertexPosition;
240  GlobalPoint previousPosition;
241  do {
242  CachingVertex<N> fVertex = initialVertex;
243  // make new linearized and vertex tracks for the next iteration
244  if (step != 0)
245  globalVTracks = reLinearizeTracks(tracks, returnVertex.vertexState());
246 
247  // update sequentially the vertex estimate
248  for (typename std::vector<RefCountedVertexTrack>::const_iterator i = globalVTracks.begin();
249  i != globalVTracks.end();
250  i++) {
251  fVertex = theUpdator->add(fVertex, *i);
252  if (!fVertex.isValid())
253  break;
254  }
255 
256  validVertex = fVertex.isValid();
257  // check tracker bounds and NaN in position
258  if (validVertex && hasNan(fVertex.position())) {
259  LogDebug("RecoVertex/SequentialVertexFitter") << "Fitted position is NaN.\n";
260  validVertex = false;
261  }
262 
263  if (validVertex && !insideTrackerBounds(fVertex.position())) {
264  LogDebug("RecoVertex/SequentialVertexFitter") << "Fitted position is out of tracker bounds.\n";
265  validVertex = false;
266  }
267 
268  if (!validVertex) {
269  // reset initial vertex position to (0,0,0) and force new iteration
270  // if number of steps not exceeded
271  ROOT::Math::SMatrixIdentity id;
272  AlgebraicSymMatrix33 we(id);
273  GlobalError error(we * 10000);
274  fVertex = CachingVertex<N>(GlobalPoint(0, 0, 0), error, initialTracks, 0);
275  }
276 
277  previousPosition = newPosition;
278  newPosition = fVertex.position();
279 
280  returnVertex = fVertex;
281  globalVTracks.clear();
282  step++;
283  } while ((step != theMaxStep) && (((previousPosition - newPosition).transverse() > theMaxShift) || (!validVertex)));
284 
285  if (!validVertex) {
286  LogDebug("RecoVertex/SequentialVertexFitter")
287  << "Fitted position is invalid (out of tracker bounds or has NaN). Returned vertex is invalid\n";
288  return CachingVertex<N>(); // return invalid vertex
289  }
290 
291  if (step >= theMaxStep) {
292  LogDebug("RecoVertex/SequentialVertexFitter")
293  << "The maximum number of steps has been exceeded. Returned vertex is invalid\n";
294  return CachingVertex<N>(); // return invalid vertex
295  }
296 
297  // smoothing
298  returnVertex = theSmoother->smooth(returnVertex);
299 
300  return returnVertex;
301 }
302 
303 template class SequentialVertexFitter<5>;
304 template class SequentialVertexFitter<6>;
std::vector< RefCountedVertexTrack > reLinearizeTracks(const std::vector< RefCountedVertexTrack > &tracks, const VertexState state) const
uint16_t *__restrict__ id
const VertexUpdator< N > * vertexUpdator() const
CachingVertex< N > fit(const std::vector< RefCountedVertexTrack > &tracks, const VertexState priorVertex, bool withPrior) const
VertexState const & vertexState() const
list original
Definition: definitions.py:57
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
auto const & tracks
cannot be loose
const AbstractLTSFactory< N > * linearizedTrackStateFactory() const
GlobalPoint position() const
Definition: VertexState.h:62
CachingVertex< N > vertex(const std::vector< reco::TransientTrack > &tracks) const override
T transverse() const
Definition: PV3DBase.h:70
const VertexSmoother< N > * vertexSmoother() const
auto const & fit
T z() const
Definition: PV3DBase.h:61
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
GlobalWeight weight() const
Definition: VertexState.h:69
const edm::ParameterSet parameterSet() const
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
GlobalPoint position() const
virtual LinearizationPointFinder * clone() const =0
T transverse() const
Another name for perp()
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepSym< double, 3 > > AlgebraicSymMatrix33
bool isValid() const
GlobalError error() const
Definition: VertexState.h:64
step
Definition: StallMonitor.cc:94
std::vector< RefCountedVertexTrack > linearizeTracks(const std::vector< reco::TransientTrack > &tracks, const VertexState state) const
const float maxShift() const
*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
const LinearizationPointFinder * linearizationPointFinder() const
#define LogDebug(id)