CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackAssociatorByPosition.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  // TrackingParticle* simtrack = const_cast<TrackingParticle*>(&st);
15  //loop over PSimHits
16  const PSimHit * psimhit=0;
17  const BoundPlane * plane=0;
18  double dLim=thePositionMinimumDistance;
19 
20  // look for the further most hit beyond a certain limit
21  std::vector<PSimHit> pSimHit = st.trackPSimHit();
22  if (!theConsiderAllSimHits) pSimHit=st.trackPSimHit(DetId::Tracker);
23  std::vector<PSimHit> ::const_iterator start=pSimHit.begin();
24  std::vector<PSimHit> ::const_iterator end=pSimHit.end();
25  LogDebug("TrackAssociatorByPosition")<<pSimHit.size()<<" PSimHits.";
26 
27  unsigned int count=0;
28  for (std::vector<PSimHit> ::const_iterator psit=start;psit!=end;++psit){
29  //get the detid
30  DetId dd(psit->detUnitId());
31  LogDebug("TrackAssociatorByPosition")<<count++<<"] PSimHit on: "<<dd.rawId();
32  //get the surface from the global geometry
33  const GeomDet * gd=theGeometry->idToDet(dd);
34  if (!gd){edm::LogError("TrackAssociatorByPosition")<<"no geomdet for: "<<dd.rawId()<<". will skip.";
35  continue;}
36  double d=gd->surface().toGlobal(psit->localPosition()).mag();
37  if (d>dLim ){
38  dLim=d;
39  psimhit=&(*psit);
40  plane=&gd->surface();}
41  }
42 
43 
44  if (psimhit && plane){
45  //build a trajectorystate on this surface
47  GlobalPoint initialPoint=plane->toGlobal(psimhit->localPosition());
48  GlobalVector initialMomentum=plane->toGlobal(psimhit->momentumAtEntry());
49  int initialCharge = (psimhit->particleType()>0) ? -1:1;
50  CartesianTrajectoryError initialCartesianErrors; //no error at initial state
51  const GlobalTrajectoryParameters initialParameters(initialPoint,initialMomentum,initialCharge,thePropagator->magneticField());
52  return TrajectoryStateOnSurface(initialParameters,initialCartesianErrors,*plane,surfaceside);}
53  else{
54  // edm::LogError("TrackAssociatorByPosition")<<"no corresponding PSimHit for a tracking particle. will fail.";
55  return TrajectoryStateOnSurface();}
56 }
57 
59  //may be you want to do more than that if track does not go to IP
60  return trajectoryStateTransform::initialFreeState(track,thePropagator->magneticField());
61 }
62 
64  switch(theMethod){
65  case 0:
66  {
69  int ierr = ! m.Invert();
70  if (ierr!=0) edm::LogInfo("TrackAssociatorByPosition")<<"error inverting the error matrix:\n"<<m;
71  double est = ROOT::Math::Similarity(v,m);
72  return est;
73  break;
74  }
75  case 1:
76  {
77  return (tr.globalPosition() - sim.globalPosition()).mag();
78  break;
79  }
80  case 2:
81  {
82  return (deltaR<double>(tr.globalDirection().eta(),tr.globalDirection().phi(),
83  sim.globalDirection().eta(),sim.globalDirection().phi()));
84  break;
85  }
86  case 3:
87  {
88  return (deltaR<double>(tr.globalPosition().eta(),tr.globalPosition().phi(),
89  sim.globalPosition().eta(),sim.globalPosition().phi()));
90  break;
91  }
92  }
93  //should never be reached
94  edm::LogError("TrackAssociatorByPosition")<<"option: "<<theMethod<<" has not been recognized. association has no meaning.";
95  return -1;
96 }
97 
98 
101  const edm::Event * e,
102  const edm::EventSetup *setup ) const{
103  RecoToSimCollection outputCollection;
104  //for each reco track find a matching tracking particle
105  std::pair<unsigned int,unsigned int> minPair;
106  const double dQmin_default=1542543;
107  double dQmin=dQmin_default;
108  for (unsigned int Ti=0; Ti!=tCH.size();++Ti){
109  //initial state (initial OR inner OR outter)
110  FreeTrajectoryState iState = getState(*(tCH)[Ti]);
111 
112  bool atLeastOne=false;
113  // for each tracking particle, find a state position and the plane to propagate the track to.
114  for (unsigned int TPi=0;TPi!=tPCH.size();++TPi) {
115  //get a state in the muon system
116  TrajectoryStateOnSurface simReferenceState = getState(*(tPCH)[TPi]);
117  if (!simReferenceState.isValid()) continue;
118 
119  //propagate the TRACK to the surface
120  TrajectoryStateOnSurface trackReferenceState = thePropagator->propagate(iState,simReferenceState.surface());
121  if (!trackReferenceState.isValid()) continue;
122 
123  //comparison
124  double dQ= quality(trackReferenceState,simReferenceState);
125  if (dQ < theQCut){
126  atLeastOne=true;
127  outputCollection.insert(tCH[Ti],
128  std::make_pair(edm::Ref<TrackingParticleCollection>(tPCH,TPi),-dQ));//association map with quality, is order greater-first
129  edm::LogVerbatim("TrackAssociatorByPosition")<<"track number: "<<Ti
130  <<" associated with dQ: "<<dQ
131  <<" to TrackingParticle number: " <<TPi;}
132  if (dQ < dQmin){
133  dQmin=dQ;
134  minPair = std::make_pair(Ti,TPi);}
135  }//loop over tracking particles
136  if (theMinIfNoMatch && !atLeastOne && dQmin!=dQmin_default){
137  outputCollection.insert(tCH[minPair.first],
138  std::make_pair(edm::Ref<TrackingParticleCollection>(tPCH,minPair.second),-dQmin));}
139  }//loop over tracks
140  outputCollection.post_insert();
141  return outputCollection;
142 }
143 
144 
145 
148  const edm::Event * e,
149  const edm::EventSetup *setup ) const {
150  SimToRecoCollection outputCollection;
151  //for each tracking particle, find matching tracks.
152 
153  std::pair<unsigned int,unsigned int> minPair;
154  const double dQmin_default=1542543;
155  double dQmin=dQmin_default;
156  for (unsigned int TPi=0;TPi!=tPCH.size();++TPi){
157  //get a state in the muon system
158  TrajectoryStateOnSurface simReferenceState= getState(*(tPCH)[TPi]);
159 
160  if (!simReferenceState.isValid()) continue;
161  bool atLeastOne=false;
162  // propagate every track from any state (initial, inner, outter) to the surface
163  // and make the position test
164  for (unsigned int Ti=0; Ti!=tCH.size();++Ti){
165  //initial state
166  FreeTrajectoryState iState = getState(*(tCH)[Ti]);
167 
168  //propagation to surface
169  TrajectoryStateOnSurface trackReferenceState = thePropagator->propagate(iState,simReferenceState.surface());
170  if (!trackReferenceState.isValid()) continue;
171 
172  //comparison
173  double dQ= quality(trackReferenceState, simReferenceState);
174  if (dQ < theQCut){
175  atLeastOne=true;
176  outputCollection.insert(edm::Ref<TrackingParticleCollection>(tPCH,TPi),
177  std::make_pair(tCH[Ti],-dQ));//association map with quality, is order greater-first
178  edm::LogVerbatim("TrackAssociatorByPosition")<<"TrackingParticle number: "<<TPi
179  <<" associated with dQ: "<<dQ
180  <<" to track number: "<<Ti;}
181  if (dQ <dQmin){
182  dQmin=dQ;
183  minPair = std::make_pair(TPi,Ti);}
184  }//loop over tracks
185  if (theMinIfNoMatch && !atLeastOne && dQmin!=dQmin_default){
186  outputCollection.insert(edm::Ref<TrackingParticleCollection>(tPCH,minPair.first),
187  std::make_pair(tCH[minPair.second],-dQmin));}
188  }//loop over tracking particles
189 
190  outputCollection.post_insert();
191  return outputCollection;
192 }
#define LogDebug(id)
GlobalPoint toGlobal(const Point2DBase< Scalar, LocalTag > lp) const
Definition: Surface.h:78
double quality(const TrajectoryStateOnSurface &, const TrajectoryStateOnSurface &) const
const LocalTrajectoryParameters & localParameters() const
LocalVector momentumAtEntry() const
The momentum of the track that produced the hit, at entry point.
Definition: PSimHit.h:47
const std::vector< PSimHit > & trackPSimHit() const
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Geom::Phi< T > phi() const
Definition: PV3DBase.h:68
GlobalPoint globalPosition() const
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
Definition: sim.h:19
AlgebraicVector5 vector() const
Local3DPoint localPosition() const
Definition: PSimHit.h:44
void post_insert()
post insert action
reco::SimToRecoCollection associateSimToReco(const edm::RefToBaseVector< reco::Track > &, const edm::RefVector< TrackingParticleCollection > &, const edm::Event *event=0, const edm::EventSetup *setup=0) const
compare reco to sim the handle of reco::Track and TrackingParticle collections
const AlgebraicSymMatrix55 & matrix() const
#define end
Definition: vmac.h:38
const LocalTrajectoryError & localError() const
size_type size() const
Definition: DetId.h:20
ROOT::Math::SVector< double, 5 > AlgebraicVector5
void insert(const key_type &k, const data_type &v)
insert an association
FreeTrajectoryState initialFreeState(const reco::Track &tk, const MagneticField *field)
T eta() const
Definition: PV3DBase.h:75
int particleType() const
Definition: PSimHit.h:85
const BoundPlane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:35
const Surface & surface() const
size_type size() const
Size of the RefVector.
Definition: RefVector.h:89
reco::RecoToSimCollection associateRecoToSim(const edm::RefToBaseVector< reco::Track > &, const edm::RefVector< TrackingParticleCollection > &, const edm::Event *event=0, const edm::EventSetup *setup=0) const
compare reco to sim the handle of reco::Track and TrackingParticle collections
FreeTrajectoryState getState(const reco::Track &) const
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
mathSSE::Vec4< T > v
GlobalVector globalDirection() const