CMS 3D CMS Logo

TrackAssociatorByPositionImpl.cc
Go to the documentation of this file.
3 
6 
8 //#include "PhysicsTools/Utilities/interface/DeltaR.h"
9 
10 using namespace edm;
11 using namespace reco;
12 
14  const SimHitTPAssociationList& simHitsTPAssoc) const {
15  using SimHitTPPair = std::pair<TrackingParticleRef, TrackPSimHitRef>;
16  SimHitTPPair clusterTPpairWithDummyTP(st, TrackPSimHitRef()); //SimHit is dummy:
17  // sorting only the cluster is needed
18  auto range = std::equal_range(
19  simHitsTPAssoc.begin(),
20  simHitsTPAssoc.end(),
21  clusterTPpairWithDummyTP,
22  [](const SimHitTPPair& iLHS, const SimHitTPPair& iRHS) -> bool { return iLHS.first.key() > iRHS.first.key(); });
23 
24  // TrackingParticle* simtrack = const_cast<TrackingParticle*>(&st);
25  //loop over PSimHits
26  const PSimHit* psimhit = nullptr;
27  const BoundPlane* plane = nullptr;
28  double dLim = thePositionMinimumDistance;
29 
30  // look for the further most hit beyond a certain limit
31  auto start = range.first;
32  auto end = range.second;
33  LogDebug("TrackAssociatorByPositionImpl") << range.second - range.first << " PSimHits.";
34 
35  unsigned int count = 0;
36  for (auto ip = start; ip != end; ++ip) {
37  TrackPSimHitRef psit = ip->second;
38 
39  //get the detid
40  DetId dd(psit->detUnitId());
41 
42  if (!theConsiderAllSimHits && dd.det() != DetId::Tracker)
43  continue;
44 
45  LogDebug("TrackAssociatorByPositionImpl") << count++ << "] PSimHit on: " << dd.rawId();
46  //get the surface from the global geometry
47  const GeomDet* gd = theGeometry->idToDet(dd);
48  if (!gd) {
49  edm::LogError("TrackAssociatorByPositionImpl") << "no geomdet for: " << dd.rawId() << ". will skip.";
50  continue;
51  }
52  double d = gd->surface().toGlobal(psit->localPosition()).mag();
53  if (d > dLim) {
54  dLim = d;
55  psimhit = &(*psit);
56  plane = &gd->surface();
57  }
58  }
59 
60  if (psimhit && plane) {
61  //build a trajectorystate on this surface
63  GlobalPoint initialPoint = plane->toGlobal(psimhit->localPosition());
64  GlobalVector initialMomentum = plane->toGlobal(psimhit->momentumAtEntry());
65  int initialCharge = (psimhit->particleType() > 0) ? -1 : 1;
66  CartesianTrajectoryError initialCartesianErrors; //no error at initial state
67  const GlobalTrajectoryParameters initialParameters(
68  initialPoint, initialMomentum, initialCharge, thePropagator->magneticField());
69  return TrajectoryStateOnSurface(initialParameters, initialCartesianErrors, *plane, surfaceside);
70  } else {
71  // edm::LogError("TrackAssociatorByPositionImpl")<<"no corresponding PSimHit for a tracking particle. will fail.";
72  return TrajectoryStateOnSurface();
73  }
74 }
75 
77  //may be you want to do more than that if track does not go to IP
78  return trajectoryStateTransform::initialFreeState(track, thePropagator->magneticField());
79 }
80 
82  const TrajectoryStateOnSurface& sim) const {
83  switch (theMethod) {
84  case Method::chi2: {
85  AlgebraicVector5 v(tr.localParameters().vector() - sim.localParameters().vector());
87  int ierr = !m.Invert();
88  if (ierr != 0)
89  edm::LogInfo("TrackAssociatorByPositionImpl") << "error inverting the error matrix:\n" << m;
90  double est = ROOT::Math::Similarity(v, m);
91  return est;
92  break;
93  }
94  case Method::dist: {
95  return (tr.globalPosition() - sim.globalPosition()).mag();
96  break;
97  }
98  case Method::momdr: {
99  return (deltaR<double>(tr.globalDirection().eta(),
100  tr.globalDirection().phi(),
101  sim.globalDirection().eta(),
102  sim.globalDirection().phi()));
103  break;
104  }
105  case Method::posdr: {
106  return (deltaR<double>(
107  tr.globalPosition().eta(), tr.globalPosition().phi(), sim.globalPosition().eta(), sim.globalPosition().phi()));
108  break;
109  }
110  }
111  //should never be reached
112  edm::LogError("TrackAssociatorByPositionImpl")
113  << "option: " << static_cast<int>(theMethod) << " has not been recognized. association has no meaning.";
114  return -1;
115 }
116 
119  RecoToSimCollection outputCollection(productGetter_);
120  //for each reco track find a matching tracking particle
121  std::pair<unsigned int, unsigned int> minPair;
122  const double dQmin_default = 1542543;
123  double dQmin = dQmin_default;
124 
125  //cdj edm::Handle<SimHitTPAssociationProducer::SimHitTPAssociationList> simHitsTPAssoc;
126 
127  //warning: make sure the TP collection used in the map is the same used in the associator!
128  //e->getByLabel(_simHitTpMapTag,simHitsTPAssoc);
129 
130  for (unsigned int Ti = 0; Ti != tCH.size(); ++Ti) {
131  //initial state (initial OR inner OR outter)
132  FreeTrajectoryState iState = getState(*(tCH)[Ti]);
133 
134  bool atLeastOne = false;
135  // for each tracking particle, find a state position and the plane to propagate the track to.
136  for (unsigned int TPi = 0; TPi != tPCH.size(); ++TPi) {
137  //get a state in the muon system
138  TrajectoryStateOnSurface simReferenceState = getState((tPCH)[TPi], *theSimHitsTPAssoc);
139  if (!simReferenceState.isValid())
140  continue;
141 
142  //propagate the TRACK to the surface
143  TrajectoryStateOnSurface trackReferenceState = thePropagator->propagate(iState, simReferenceState.surface());
144  if (!trackReferenceState.isValid())
145  continue;
146 
147  //comparison
148  double dQ = quality(trackReferenceState, simReferenceState);
149  if (dQ < theQCut) {
150  atLeastOne = true;
151  outputCollection.insert(tCH[Ti],
152  std::make_pair(tPCH[TPi], -dQ)); //association map with quality, is order greater-first
153  edm::LogVerbatim("TrackAssociatorByPositionImpl")
154  << "track number: " << Ti << " associated with dQ: " << dQ << " to TrackingParticle number: " << TPi;
155  }
156  if (dQ < dQmin) {
157  dQmin = dQ;
158  minPair = std::make_pair(Ti, TPi);
159  }
160  } //loop over tracking particles
161  if (theMinIfNoMatch && !atLeastOne && dQmin != dQmin_default) {
162  outputCollection.insert(tCH[minPair.first], std::make_pair(tPCH[minPair.second], -dQmin));
163  }
164  } //loop over tracks
165  outputCollection.post_insert();
166  return outputCollection;
167 }
168 
171  SimToRecoCollection outputCollection(productGetter_);
172  //for each tracking particle, find matching tracks.
173 
174  std::pair<unsigned int, unsigned int> minPair;
175  const double dQmin_default = 1542543;
176  double dQmin = dQmin_default;
177 
178  //edm::Handle<SimHitTPAssociationProducer::SimHitTPAssociationList> simHitsTPAssoc;
179 
180  //warning: make sure the TP collection used in the map is the same used in the associator!
181  //e->getByLabel(_simHitTpMapTag,simHitsTPAssoc);
182 
183  for (unsigned int TPi = 0; TPi != tPCH.size(); ++TPi) {
184  //get a state in the muon system
185  TrajectoryStateOnSurface simReferenceState = getState((tPCH)[TPi], *theSimHitsTPAssoc);
186 
187  if (!simReferenceState.isValid())
188  continue;
189  bool atLeastOne = false;
190  // propagate every track from any state (initial, inner, outter) to the surface
191  // and make the position test
192  for (unsigned int Ti = 0; Ti != tCH.size(); ++Ti) {
193  //initial state
194  FreeTrajectoryState iState = getState(*(tCH)[Ti]);
195 
196  //propagation to surface
197  TrajectoryStateOnSurface trackReferenceState = thePropagator->propagate(iState, simReferenceState.surface());
198  if (!trackReferenceState.isValid())
199  continue;
200 
201  //comparison
202  double dQ = quality(trackReferenceState, simReferenceState);
203  if (dQ < theQCut) {
204  atLeastOne = true;
205  outputCollection.insert(tPCH[TPi],
206  std::make_pair(tCH[Ti], -dQ)); //association map with quality, is order greater-first
207  edm::LogVerbatim("TrackAssociatorByPositionImpl")
208  << "TrackingParticle number: " << TPi << " associated with dQ: " << dQ << " to track number: " << Ti;
209  }
210  if (dQ < dQmin) {
211  dQmin = dQ;
212  minPair = std::make_pair(TPi, Ti);
213  }
214  } //loop over tracks
215  if (theMinIfNoMatch && !atLeastOne && dQmin != dQmin_default) {
216  outputCollection.insert(tPCH[minPair.first], std::make_pair(tCH[minPair.second], -dQmin));
217  }
218  } //loop over tracking particles
219 
220  outputCollection.post_insert();
221  return outputCollection;
222 }
Definition: start.py:1
Log< level::Info, true > LogVerbatim
const LocalTrajectoryError & localError() const
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
double quality(const TrajectoryStateOnSurface &, const TrajectoryStateOnSurface &) const
T eta() const
Definition: PV3DBase.h:73
const LocalTrajectoryParameters & localParameters() const
Log< level::Error, false > LogError
const SurfaceType & surface() const
size_type size() const
string dd
Definition: createTree.py:154
FreeTrajectoryState getState(const reco::Track &) const
GlobalPoint globalPosition() const
void post_insert()
post insert action
AlgebraicVector5 vector() const
ROOT::Math::SVector< double, 5 > AlgebraicVector5
std::pair< TrackingParticleRef, TrackPSimHitRef > SimHitTPPair
d
Definition: ztail.py:151
GlobalPoint toGlobal(const Point2DBase< Scalar, LocalTag > lp) const
Definition: Surface.h:79
Log< level::Info, false > LogInfo
Definition: DetId.h:17
reco::RecoToSimCollection associateRecoToSim(const edm::RefToBaseVector< reco::Track > &, const edm::RefVector< TrackingParticleCollection > &) const override
compare reco to sim the handle of reco::Track and TrackingParticle collections
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
size_type size() const
Size of the RefVector.
Definition: RefVector.h:102
void insert(const key_type &k, const data_type &v)
insert an association
GlobalVector globalDirection() const
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
fixed size matrix
HLT enums.
const AlgebraicSymMatrix55 & matrix() const
edm::Ref< edm::PSimHitContainer > TrackPSimHitRef
string quality
reco::SimToRecoCollection associateSimToReco(const edm::RefToBaseVector< reco::Track > &, const edm::RefVector< TrackingParticleCollection > &) const override
compare reco to sim the handle of reco::Track and TrackingParticle collections
FreeTrajectoryState initialFreeState(const reco::Track &tk, const MagneticField *field, bool withErr=true)
std::vector< SimHitTPPair > SimHitTPAssociationList
#define LogDebug(id)