CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ConversionHitChecker.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <vector>
3 #include <memory>
5 // Framework
13 //
15 
16 //
17 #include "CLHEP/Units/GlobalPhysicalConstants.h"
18 #include <TMath.h>
19 
20 
21 std::pair<uint8_t,Measurement1DFloat> ConversionHitChecker::nHitsBeforeVtx(const Trajectory &traj, const reco::Vertex &vtx, double sigmaTolerance) const {
22 
23  const std::vector<TrajectoryMeasurement> &measurements = traj.measurements();
24 
25  //protect for empty trajectory
26  if (measurements.size()==0) {
27  return std::pair<unsigned int,Measurement1DFloat>(0,Measurement1DFloat());
28  }
29 
30  //check ordering of measurements in trajectory
31  bool inout = (traj.direction() == alongMomentum);
32 
33  GlobalPoint vtxPos(vtx.x(),vtx.y(),vtx.z());
34 
35  uint8_t nhits = 0;
36 
37  std::vector<TrajectoryMeasurement>::const_iterator closest = measurements.begin();
38  double distance = 1e6;
39  //iterate inside out, when distance to vertex starts increasing, we are at the closest hit
40  for (std::vector<TrajectoryMeasurement>::const_iterator fwdit = measurements.begin(), revit = measurements.end()-1;
41  fwdit != measurements.end(); ++fwdit,--revit) {
42 
43  std::vector<TrajectoryMeasurement>::const_iterator it;
44  if (inout) {
45  it = fwdit;
46  }
47  else {
48  it = revit;
49  }
50 
51  //increment hit counter, compute distance and set iterator if this hit is valid
52  if (it->recHit() && it->recHit()->isValid()) {
53  ++nhits;
54  distance = (vtxPos - it->updatedState().globalPosition()).mag();
55  closest = it;
56  }
57 
58  if ( (measurements.end()-fwdit)==1) {
59  break;
60  }
61 
62  //check if next valid hit is farther away from vertex than existing closest
63  std::vector<TrajectoryMeasurement>::const_iterator nextit;
64  if (inout) {
65  nextit = it+1;
66  }
67  else {
68  nextit = it-1;
69  }
70 
71 
72  if ( nextit->recHit() && nextit->recHit()->isValid() ) {
73  double nextDistance = (vtxPos - nextit->updatedState().globalPosition()).mag();
74  if (nextDistance > distance) {
75  break;
76  }
77  }
78 
79  }
80 
81  //compute signed decaylength significance for closest hit and check if it is before the vertex
82  //if not then we need to subtract it from the count of hits before the vertex, since it has been implicitly included
83 
84  GlobalVector momDir = closest->updatedState().globalMomentum().unit();
85  double decayLengthHitToVtx = (vtxPos - closest->updatedState().globalPosition()).dot(momDir);
86 
88  j[0] = momDir.x();
89  j[1] = momDir.y();
90  j[2] = momDir.z();
92  jj[0] = momDir.x();
93  jj[1] = momDir.y();
94  jj[2] = momDir.z();
95  jj[3] =0.;
96  jj[4] =0.;
97  jj[5] =0.;
98 
99  //TODO: In principle the hit measurement position is correlated with the vertex fit
100  //at worst though we inflate the uncertainty by a factor of two
101  double trackError2 = ROOT::Math::Similarity(jj,closest->updatedState().cartesianError().matrix());
102  double vertexError2 = ROOT::Math::Similarity(j,vtx.covariance());
103  double decayLenError = sqrt(trackError2+vertexError2);
104 
105  Measurement1DFloat decayLength(decayLengthHitToVtx,decayLenError);
106 
107  if (decayLength.significance() < sigmaTolerance) { //decay length is not (significantly) positive, so hit is consistent with the vertex position or late
108  //subtract it from wrong hits count
109  --nhits;
110  }
111 
112  return std::pair<unsigned int,Measurement1DFloat>(nhits,decayLength);
113 
114 }
115 
116 uint8_t ConversionHitChecker::nSharedHits(const reco::Track &trk1, const reco::Track &trk2) const {
117 
118  uint8_t nShared = 0;
119 
120  for (trackingRecHit_iterator iHit1 = trk1.recHitsBegin(); iHit1 != trk1.recHitsEnd(); ++iHit1) {
121  const TrackingRecHit *hit1 = iHit1->get();
122  if (hit1->isValid()) {
123  for (trackingRecHit_iterator iHit2 = trk2.recHitsBegin(); iHit2 != trk2.recHitsEnd(); ++iHit2) {
124  const TrackingRecHit *hit2 = iHit2->get();
125  if (hit2->isValid() && hit1->sharesInput(hit2,TrackingRecHit::some)) {
126  ++nShared;
127  }
128  }
129  }
130  }
131 
132  return nShared;
133 
134 }
135 
136 
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
std::pair< uint8_t, Measurement1DFloat > nHitsBeforeVtx(const Trajectory &traj, const reco::Vertex &vtx, double sigmaTolerance=3.0) const
double y() const
y coordinate
Definition: Vertex.h:97
T y() const
Definition: PV3DBase.h:57
virtual bool sharesInput(const TrackingRecHit *other, SharedInputType what) const
double covariance(int i, int j) const
(i, j)-th element of error matrix, i, j = 0, ... 2
Definition: Vertex.h:110
ROOT::Math::SVector< double, 6 > AlgebraicVector6
PropagationDirection const & direction() const
Definition: Trajectory.cc:195
DataContainer const & measurements() const
Definition: Trajectory.h:169
T sqrt(T t)
Definition: SSEVec.h:28
T z() const
Definition: PV3DBase.h:58
float significance() const
ROOT::Math::SVector< double, 3 > AlgebraicVector3
int j
Definition: DBlmapReader.cc:9
double z() const
y coordinate
Definition: Vertex.h:99
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:63
uint8_t nSharedHits(const reco::Track &trk1, const reco::Track &trk2) const
Vector3DBase unit() const
Definition: Vector3DBase.h:57
double x() const
x coordinate
Definition: Vertex.h:95
bool isValid() const
T dot(const Basic3DVector &v) const
Scalar product, or &quot;dot&quot; product, with a vector of same type.
T x() const
Definition: PV3DBase.h:56
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:65