CMS 3D CMS Logo

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