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