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 
15  std::pair<TrackingParticleRef, TrackPSimHitRef> clusterTPpairWithDummyTP(st,TrackPSimHitRef());//SimHit is dummy: for simHitTPAssociationListGreater
16  // sorting only the cluster is needed
17  auto range = std::equal_range(simHitsTPAssoc->begin(), simHitsTPAssoc->end(),
19 
20  // TrackingParticle* simtrack = const_cast<TrackingParticle*>(&st);
21  //loop over PSimHits
22  const PSimHit * psimhit=0;
23  const BoundPlane * plane=0;
24  double dLim=thePositionMinimumDistance;
25 
26  // look for the further most hit beyond a certain limit
27  auto start=range.first;
28  auto end=range.second;
29  LogDebug("TrackAssociatorByPosition")<<range.second-range.first<<" PSimHits.";
30 
31  unsigned int count=0;
32  for (auto ip=start;ip!=end;++ip){
33 
34  TrackPSimHitRef psit = ip->second;
35 
36  //get the detid
37  DetId dd(psit->detUnitId());
38 
39  if (!theConsiderAllSimHits && dd.det()!=DetId::Tracker) continue;
40 
41  LogDebug("TrackAssociatorByPosition")<<count++<<"] PSimHit on: "<<dd.rawId();
42  //get the surface from the global geometry
43  const GeomDet * gd=theGeometry->idToDet(dd);
44  if (!gd){edm::LogError("TrackAssociatorByPosition")<<"no geomdet for: "<<dd.rawId()<<". will skip.";
45  continue;}
46  double d=gd->surface().toGlobal(psit->localPosition()).mag();
47  if (d>dLim ){
48  dLim=d;
49  psimhit=&(*psit);
50  plane=&gd->surface();}
51  }
52 
53 
54  if (psimhit && plane){
55  //build a trajectorystate on this surface
57  GlobalPoint initialPoint=plane->toGlobal(psimhit->localPosition());
58  GlobalVector initialMomentum=plane->toGlobal(psimhit->momentumAtEntry());
59  int initialCharge = (psimhit->particleType()>0) ? -1:1;
60  CartesianTrajectoryError initialCartesianErrors; //no error at initial state
61  const GlobalTrajectoryParameters initialParameters(initialPoint,initialMomentum,initialCharge,thePropagator->magneticField());
62  return TrajectoryStateOnSurface(initialParameters,initialCartesianErrors,*plane,surfaceside);}
63  else{
64  // edm::LogError("TrackAssociatorByPosition")<<"no corresponding PSimHit for a tracking particle. will fail.";
65  return TrajectoryStateOnSurface();}
66 }
67 
69  //may be you want to do more than that if track does not go to IP
70  return trajectoryStateTransform::initialFreeState(track,thePropagator->magneticField());
71 }
72 
74  switch(theMethod){
75  case 0:
76  {
79  int ierr = ! m.Invert();
80  if (ierr!=0) edm::LogInfo("TrackAssociatorByPosition")<<"error inverting the error matrix:\n"<<m;
81  double est = ROOT::Math::Similarity(v,m);
82  return est;
83  break;
84  }
85  case 1:
86  {
87  return (tr.globalPosition() - sim.globalPosition()).mag();
88  break;
89  }
90  case 2:
91  {
92  return (deltaR<double>(tr.globalDirection().eta(),tr.globalDirection().phi(),
93  sim.globalDirection().eta(),sim.globalDirection().phi()));
94  break;
95  }
96  case 3:
97  {
98  return (deltaR<double>(tr.globalPosition().eta(),tr.globalPosition().phi(),
99  sim.globalPosition().eta(),sim.globalPosition().phi()));
100  break;
101  }
102  }
103  //should never be reached
104  edm::LogError("TrackAssociatorByPosition")<<"option: "<<theMethod<<" has not been recognized. association has no meaning.";
105  return -1;
106 }
107 
108 
111  const edm::Event * e,
112  const edm::EventSetup *setup ) const{
113  RecoToSimCollection outputCollection;
114  //for each reco track find a matching tracking particle
115  std::pair<unsigned int,unsigned int> minPair;
116  const double dQmin_default=1542543;
117  double dQmin=dQmin_default;
118 
119  //warning: make sure the TP collection used in the map is the same used in the associator!
120  e->getByLabel(_simHitTpMapTag,simHitsTPAssoc);
121 
122  for (unsigned int Ti=0; Ti!=tCH.size();++Ti){
123  //initial state (initial OR inner OR outter)
124  FreeTrajectoryState iState = getState(*(tCH)[Ti]);
125 
126  bool atLeastOne=false;
127  // for each tracking particle, find a state position and the plane to propagate the track to.
128  for (unsigned int TPi=0;TPi!=tPCH.size();++TPi) {
129  //get a state in the muon system
130  TrajectoryStateOnSurface simReferenceState = getState((tPCH)[TPi]);
131  if (!simReferenceState.isValid()) continue;
132 
133  //propagate the TRACK to the surface
134  TrajectoryStateOnSurface trackReferenceState = thePropagator->propagate(iState,simReferenceState.surface());
135  if (!trackReferenceState.isValid()) continue;
136 
137  //comparison
138  double dQ= quality(trackReferenceState,simReferenceState);
139  if (dQ < theQCut){
140  atLeastOne=true;
141  outputCollection.insert(tCH[Ti],
142  std::make_pair(edm::Ref<TrackingParticleCollection>(tPCH,TPi),-dQ));//association map with quality, is order greater-first
143  edm::LogVerbatim("TrackAssociatorByPosition")<<"track number: "<<Ti
144  <<" associated with dQ: "<<dQ
145  <<" to TrackingParticle number: " <<TPi;}
146  if (dQ < dQmin){
147  dQmin=dQ;
148  minPair = std::make_pair(Ti,TPi);}
149  }//loop over tracking particles
150  if (theMinIfNoMatch && !atLeastOne && dQmin!=dQmin_default){
151  outputCollection.insert(tCH[minPair.first],
152  std::make_pair(edm::Ref<TrackingParticleCollection>(tPCH,minPair.second),-dQmin));}
153  }//loop over tracks
154  outputCollection.post_insert();
155  return outputCollection;
156 }
157 
158 
159 
162  const edm::Event * e,
163  const edm::EventSetup *setup ) const {
164  SimToRecoCollection outputCollection;
165  //for each tracking particle, find matching tracks.
166 
167  std::pair<unsigned int,unsigned int> minPair;
168  const double dQmin_default=1542543;
169  double dQmin=dQmin_default;
170 
171  //warning: make sure the TP collection used in the map is the same used in the associator!
172  e->getByLabel(_simHitTpMapTag,simHitsTPAssoc);
173 
174  for (unsigned int TPi=0;TPi!=tPCH.size();++TPi){
175  //get a state in the muon system
176  TrajectoryStateOnSurface simReferenceState= getState((tPCH)[TPi]);
177 
178  if (!simReferenceState.isValid()) continue;
179  bool atLeastOne=false;
180  // propagate every track from any state (initial, inner, outter) to the surface
181  // and make the position test
182  for (unsigned int Ti=0; Ti!=tCH.size();++Ti){
183  //initial state
184  FreeTrajectoryState iState = getState(*(tCH)[Ti]);
185 
186  //propagation to surface
187  TrajectoryStateOnSurface trackReferenceState = thePropagator->propagate(iState,simReferenceState.surface());
188  if (!trackReferenceState.isValid()) continue;
189 
190  //comparison
191  double dQ= quality(trackReferenceState, simReferenceState);
192  if (dQ < theQCut){
193  atLeastOne=true;
194  outputCollection.insert(edm::Ref<TrackingParticleCollection>(tPCH,TPi),
195  std::make_pair(tCH[Ti],-dQ));//association map with quality, is order greater-first
196  edm::LogVerbatim("TrackAssociatorByPosition")<<"TrackingParticle number: "<<TPi
197  <<" associated with dQ: "<<dQ
198  <<" to track number: "<<Ti;}
199  if (dQ <dQmin){
200  dQmin=dQ;
201  minPair = std::make_pair(TPi,Ti);}
202  }//loop over tracks
203  if (theMinIfNoMatch && !atLeastOne && dQmin!=dQmin_default){
204  outputCollection.insert(edm::Ref<TrackingParticleCollection>(tPCH,minPair.first),
205  std::make_pair(tCH[minPair.second],-dQmin));}
206  }//loop over tracking particles
207 
208  outputCollection.post_insert();
209  return outputCollection;
210 }
#define LogDebug(id)
GlobalPoint toGlobal(const Point2DBase< Scalar, LocalTag > lp) const
Definition: Surface.h:114
double quality(const TrajectoryStateOnSurface &, const TrajectoryStateOnSurface &) const
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
const LocalTrajectoryParameters & localParameters() const
static bool simHitTPAssociationListGreater(SimHitTPPair i, SimHitTPPair j)
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
Definition: sim.h:19
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:35
AlgebraicVector5 vector() const
const SurfaceType & surface() const
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
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
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:76
edm::Ref< edm::PSimHitContainer > TrackPSimHitRef
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="")
GlobalVector globalDirection() const