CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackAssociatorByChi2.cc
Go to the documentation of this file.
3 
8 using namespace edm;
9 using namespace reco;
10 using namespace std;
11 
12 double TrackAssociatorByChi2::compareTracksParam ( TrackCollection::const_iterator rt,
13  SimTrackContainer::const_iterator st,
14  const math::XYZTLorentzVectorD vertexPosition,
15  GlobalVector magField,
16  TrackBase::CovarianceMatrix invertedCovariance,
17  const reco::BeamSpot& bs) const{
18 
19  Basic3DVector<double> momAtVtx(st->momentum().x(),st->momentum().y(),st->momentum().z());
20  Basic3DVector<double> vert = (Basic3DVector<double>) vertexPosition;
21 
22  std::pair<bool,reco::TrackBase::ParameterVector> params = parametersAtClosestApproach(vert, momAtVtx, st->charge(), bs);
23  if (params.first){
24  TrackBase::ParameterVector sParameters = params.second;
25  TrackBase::ParameterVector rParameters = rt->parameters();
26 
27  TrackBase::ParameterVector diffParameters = rParameters - sParameters;
28  double chi2 = ROOT::Math::Dot(diffParameters * invertedCovariance, diffParameters);
29 
30  return chi2;
31  } else {
32  return 10000000000.;
33  }
34 }
35 
36 
39  const SimTrackContainer& stColl,
40  const SimVertexContainer& svColl,
41  const reco::BeamSpot& bs) const{
42 
43  RecoToSimPairAssociation outputVec;
44 
45  for (TrackCollection::const_iterator track=rtColl.begin(); track!=rtColl.end(); track++){
46  Chi2SimMap outMap;
47 
48  TrackBase::ParameterVector rParameters = track->parameters();
49 
50  TrackBase::CovarianceMatrix recoTrackCovMatrix = track->covariance();
51  if (onlyDiagonal){
52  for (unsigned int i=0;i<5;i++){
53  for (unsigned int j=0;j<5;j++){
54  if (i!=j) recoTrackCovMatrix(i,j)=0;
55  }
56  }
57  }
58  recoTrackCovMatrix.Invert();
59 
60  for (SimTrackContainer::const_iterator st=stColl.begin(); st!=stColl.end(); st++){
61 
62  Basic3DVector<double> momAtVtx(st->momentum().x(),st->momentum().y(),st->momentum().z());
63  Basic3DVector<double> vert = (Basic3DVector<double>) svColl[st->vertIndex()].position();
64 
65  std::pair<bool,reco::TrackBase::ParameterVector> params = parametersAtClosestApproach(vert, momAtVtx, st->charge(), bs);
66  if (params.first){
67  TrackBase::ParameterVector sParameters = params.second;
68 
69  TrackBase::ParameterVector diffParameters = rParameters - sParameters;
70  double chi2 = ROOT::Math::Dot(diffParameters * recoTrackCovMatrix, diffParameters);
71  chi2/=5;
72  if (chi2<chi2cut) outMap[chi2]=*st;
73  }
74  }
75  outputVec.push_back(RecoToSimPair(*track,outMap));
76  }
77  return outputVec;
78 }
79 
80 
81 double TrackAssociatorByChi2::associateRecoToSim( TrackCollection::const_iterator rt,
82  TrackingParticleCollection::const_iterator tp,
83  const reco::BeamSpot& bs) const{
84 
85  double chi2;
86 
87  TrackBase::ParameterVector rParameters = rt->parameters();
88 
89  TrackBase::CovarianceMatrix recoTrackCovMatrix = rt->covariance();
90  if (onlyDiagonal) {
91  for (unsigned int i=0;i<5;i++){
92  for (unsigned int j=0;j<5;j++){
93  if (i!=j) recoTrackCovMatrix(i,j)=0;
94  }
95  }
96  }
97  recoTrackCovMatrix.Invert();
98 
99  Basic3DVector<double> momAtVtx(tp->momentum().x(),tp->momentum().y(),tp->momentum().z());
100  Basic3DVector<double> vert(tp->vertex().x(),tp->vertex().y(),tp->vertex().z());
101 
102  std::pair<bool,reco::TrackBase::ParameterVector> params = parametersAtClosestApproach(vert, momAtVtx, tp->charge(), bs);
103  if (params.first){
104  TrackBase::ParameterVector sParameters=params.second;
105 
106  TrackBase::ParameterVector diffParameters = rParameters - sParameters;
107  chi2 = ROOT::Math::Dot(diffParameters * recoTrackCovMatrix, diffParameters);
108  chi2 /= 5;
109 
110  LogDebug("TrackAssociator") << "====NEW RECO TRACK WITH PT=" << rt->pt() << "====\n"
111  << "qoverp sim: " << sParameters[0] << "\n"
112  << "lambda sim: " << sParameters[1] << "\n"
113  << "phi sim: " << sParameters[2] << "\n"
114  << "dxy sim: " << sParameters[3] << "\n"
115  << "dsz sim: " << sParameters[4] << "\n"
116  << ": " /*<< */ << "\n"
117  << "qoverp rec: " << rt->qoverp()/*rParameters[0]*/ << "\n"
118  << "lambda rec: " << rt->lambda()/*rParameters[1]*/ << "\n"
119  << "phi rec: " << rt->phi()/*rParameters[2]*/ << "\n"
120  << "dxy rec: " << rt->dxy(bs.position())/*rParameters[3]*/ << "\n"
121  << "dsz rec: " << rt->dsz(bs.position())/*rParameters[4]*/ << "\n"
122  << ": " /*<< */ << "\n"
123  << "chi2: " << chi2 << "\n";
124 
125  return chi2;
126  } else {
127  return 10000000000.;
128  }
129 }
130 
131 pair<bool,TrackBase::ParameterVector>
133  Basic3DVector<double> momAtVtx,
134  float charge,
135  const BeamSpot& bs) const{
136 
137  TrackBase::ParameterVector sParameters;
138  try {
139  FreeTrajectoryState ftsAtProduction(GlobalPoint(vertex.x(),vertex.y(),vertex.z()),
140  GlobalVector(momAtVtx.x(),momAtVtx.y(),momAtVtx.z()),
141  TrackCharge(charge),
142  theMF.product());
143  TSCBLBuilderNoMaterial tscblBuilder;
144  TrajectoryStateClosestToBeamLine tsAtClosestApproach = tscblBuilder(ftsAtProduction,bs);//as in TrackProducerAlgorithm
145 
146  GlobalPoint v = tsAtClosestApproach.trackStateAtPCA().position();
147  GlobalVector p = tsAtClosestApproach.trackStateAtPCA().momentum();
148  sParameters[0] = tsAtClosestApproach.trackStateAtPCA().charge()/p.mag();
149  sParameters[1] = Geom::halfPi() - p.theta();
150  sParameters[2] = p.phi();
151  sParameters[3] = (-v.x()*sin(p.phi())+v.y()*cos(p.phi()));
152  sParameters[4] = v.z()*p.perp()/p.mag() - (v.x()*p.x()+v.y()*p.y())/p.perp() * p.z()/p.mag();
153 
154  return pair<bool,TrackBase::ParameterVector>(true,sParameters);
155  } catch ( ... ) {
156  return pair<bool,TrackBase::ParameterVector>(false,sParameters);
157  }
158 }
159 
162  const edm::Event * e,
163  const edm::EventSetup *setup ) const{
164  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
165  e->getByLabel(bsSrc,recoBeamSpotHandle);
166  reco::BeamSpot bs = *recoBeamSpotHandle;
167 
168  RecoToSimCollection outputCollection;
169  double chi2;
170 
172  if (tPCH.size()!=0) tPC = *tPCH.product();
173 
174  int tindex=0;
175  for (RefToBaseVector<reco::Track>::const_iterator rt=tC.begin(); rt!=tC.end(); rt++, tindex++){
176 
177  LogDebug("TrackAssociator") << "=========LOOKING FOR ASSOCIATION===========" << "\n"
178  << "rec::Track #"<<tindex<<" with pt=" << (*rt)->pt() << "\n"
179  << "===========================================" << "\n";
180 
181  TrackBase::ParameterVector rParameters = (*rt)->parameters();
182 
183  TrackBase::CovarianceMatrix recoTrackCovMatrix = (*rt)->covariance();
184  if (onlyDiagonal){
185  for (unsigned int i=0;i<5;i++){
186  for (unsigned int j=0;j<5;j++){
187  if (i!=j) recoTrackCovMatrix(i,j)=0;
188  }
189  }
190  }
191 
192  recoTrackCovMatrix.Invert();
193 
194  int tpindex =0;
195  for (TrackingParticleCollection::const_iterator tp=tPC.begin(); tp!=tPC.end(); tp++, ++tpindex){
196 
197  //skip tps with a very small pt
198  //if (sqrt(tp->momentum().perp2())<0.5) continue;
199  Basic3DVector<double> momAtVtx(tp->momentum().x(),tp->momentum().y(),tp->momentum().z());
200  Basic3DVector<double> vert=(Basic3DVector<double>) tp->vertex();
201 
202  std::pair<bool,reco::TrackBase::ParameterVector> params = parametersAtClosestApproach(vert, momAtVtx, tp->charge(), bs);
203  if (params.first){
204  TrackBase::ParameterVector sParameters=params.second;
205 
206  TrackBase::ParameterVector diffParameters = rParameters - sParameters;
207 
208  chi2 = ROOT::Math::Similarity(diffParameters, recoTrackCovMatrix);
209  chi2 /= 5;
210 
211  LogTrace("TrackAssociator") << "====TP index=" << tpindex << " RECO TRACK index="<<tindex<<" WITH PT=" << (*rt)->pt() << "====\n"
212  << "qoverp sim: " << sParameters[0] << "\n"
213  << "lambda sim: " << sParameters[1] << "\n"
214  << "phi sim: " << sParameters[2] << "\n"
215  << "dxy sim: " << sParameters[3] << "\n"
216  << "dsz sim: " << sParameters[4] << "\n"
217  << ": " /*<< */ << "\n"
218  << "qoverp rec: " << (*rt)->qoverp() << " err: " << (*rt)->error(0) << "\n"
219  << "lambda rec: " << (*rt)->lambda() << " err: " << (*rt)->error(1) << "\n"
220  << "phi rec: " << (*rt)->phi() << " err: " << (*rt)->error(2) << "\n"
221  << "dxy rec: " << (*rt)->dxy(bs.position()) << " err: " << (*rt)->error(3) << "\n"
222  << "dsz rec: " << (*rt)->dsz(bs.position()) << " err: " << (*rt)->error(4) << "\n"
223  << ": " /*<< */ << "\n"
224  << "chi2: " << chi2 << "\n";
225 
226  if (chi2<chi2cut) {
227  outputCollection.insert(tC[tindex],
228  std::make_pair(edm::Ref<TrackingParticleCollection>(tPCH, tpindex),
229  -chi2));//-chi2 because the Association Map is ordered using std::greater
230  }
231  }
232  }
233  }
234  outputCollection.post_insert();
235  return outputCollection;
236 }
237 
238 
239 
242  const edm::Event * e,
243  const edm::EventSetup *setup ) const {
244  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
245  e->getByLabel(bsSrc,recoBeamSpotHandle);
246  reco::BeamSpot bs = *recoBeamSpotHandle;
247 
248  SimToRecoCollection outputCollection;
249  double chi2;
250 
252  if (tPCH.size()!=0) tPC = *tPCH.product();
253 
254  int tpindex =0;
255  for (TrackingParticleCollection::const_iterator tp=tPC.begin(); tp!=tPC.end(); tp++, ++tpindex){
256 
257  //skip tps with a very small pt
258  //if (sqrt(tp->momentum().perp2())<0.5) continue;
259 
260  LogDebug("TrackAssociator") << "=========LOOKING FOR ASSOCIATION===========" << "\n"
261  << "TrackingParticle #"<<tpindex<<" with pt=" << sqrt(tp->momentum().perp2()) << "\n"
262  << "===========================================" << "\n";
263 
264  Basic3DVector<double> momAtVtx(tp->momentum().x(),tp->momentum().y(),tp->momentum().z());
265  Basic3DVector<double> vert(tp->vertex().x(),tp->vertex().y(),tp->vertex().z());
266 
267  std::pair<bool,reco::TrackBase::ParameterVector> params = parametersAtClosestApproach(vert, momAtVtx, tp->charge(), bs);
268  if (params.first){
269  TrackBase::ParameterVector sParameters=params.second;
270 
271  int tindex=0;
272  for (RefToBaseVector<reco::Track>::const_iterator rt=tC.begin(); rt!=tC.end(); rt++, tindex++){
273 
274  TrackBase::ParameterVector rParameters = (*rt)->parameters();
275 
276  TrackBase::CovarianceMatrix recoTrackCovMatrix = (*rt)->covariance();
277  if (onlyDiagonal) {
278  for (unsigned int i=0;i<5;i++){
279  for (unsigned int j=0;j<5;j++){
280  if (i!=j) recoTrackCovMatrix(i,j)=0;
281  }
282  }
283  }
284 
285  recoTrackCovMatrix.Invert();
286 
287  TrackBase::ParameterVector diffParameters = rParameters - sParameters;
288 
289  chi2 = ROOT::Math::Similarity(recoTrackCovMatrix, diffParameters);
290  chi2 /= 5;
291 
292  LogTrace("TrackAssociator") << "====TP index=" << tpindex << " RECO TRACK index="<<tindex<<" WITH PT=" << (*rt)->pt() << "====\n"
293  << "qoverp sim: " << sParameters[0] << "\n"
294  << "lambda sim: " << sParameters[1] << "\n"
295  << "phi sim: " << sParameters[2] << "\n"
296  << "dxy sim: " << sParameters[3] << "\n"
297  << "dsz sim: " << sParameters[4] << "\n"
298  << ": " /*<< */ << "\n"
299  << "qoverp rec: " << (*rt)->qoverp() << " err: " << (*rt)->error(0) << "\n"
300  << "lambda rec: " << (*rt)->lambda() << " err: " << (*rt)->error(1) << "\n"
301  << "phi rec: " << (*rt)->phi() << " err: " << (*rt)->error(2) << "\n"
302  << "dxy rec: " << (*rt)->dxy(bs.position()) << " err: " << (*rt)->error(3) << "\n"
303  << "dsz rec: " << (*rt)->dsz(bs.position()) << " err: " << (*rt)->error(4) << "\n"
304  << ": " /*<< */ << "\n"
305  << "chi2: " << chi2 << "\n";
306 
307  if (chi2<chi2cut) {
308  outputCollection.insert(edm::Ref<TrackingParticleCollection>(tPCH, tpindex),
309  std::make_pair(tC[tindex],
310  -chi2));//-chi2 because the Association Map is ordered using std::greater
311  }
312  }
313  }
314  }
315  outputCollection.post_insert();
316  return outputCollection;
317 }
#define LogDebug(id)
int i
Definition: DBlmapReader.cc:9
T y() const
Cartesian y coordinate.
T perp() const
Definition: PV3DBase.h:66
T x() const
Cartesian x coordinate.
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:15
C const * product() const
Accessor for product collection.
Definition: RefVector.h:268
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Geom::Phi< T > phi() const
Definition: PV3DBase.h:63
const_iterator end() const
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:10
T y() const
Definition: PV3DBase.h:57
std::pair< bool, reco::TrackBase::ParameterVector > parametersAtClosestApproach(Basic3DVector< double >, Basic3DVector< double >, float, const reco::BeamSpot &) const
propagate the track parameters of TrackinParticle from production vertex to the point of closest appr...
std::pair< reco::Track, Chi2SimMap > RecoToSimPair
math::Vector< dimension >::type ParameterVector
parameter vector
Definition: TrackBase.h:69
TrackCharge charge() const
double charge(const std::vector< uint8_t > &Ampls)
double compareTracksParam(reco::TrackCollection::const_iterator, edm::SimTrackContainer::const_iterator, const math::XYZTLorentzVectorD, GlobalVector, reco::TrackBase::CovarianceMatrix, const reco::BeamSpot &) const
compare reco::TrackCollection and edm::SimTrackContainer iterators: returns the chi2 ...
Geom::Theta< T > theta() const
Definition: PV3DBase.h:69
int TrackCharge
Definition: TrackCharge.h:4
T mag() const
Definition: PV3DBase.h:61
T z() const
Cartesian z coordinate.
void post_insert()
post insert action
T sqrt(T t)
Definition: SSEVec.h:28
T z() const
Definition: PV3DBase.h:58
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
int j
Definition: DBlmapReader.cc:9
reco::SimToRecoCollection associateSimToReco(const edm::RefToBaseVector< reco::Track > &, const edm::RefVector< TrackingParticleCollection > &, const edm::Event *event=0, const edm::EventSetup *setup=0) const
Association Sim To Reco with Collections.
double halfPi()
Definition: Pi.h:33
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:359
#define LogTrace(id)
GlobalVector momentum() const
double associateRecoToSim(reco::TrackCollection::const_iterator, TrackingParticleCollection::const_iterator, const reco::BeamSpot &) const
compare reco::TrackCollection and TrackingParticleCollection iterators: returns the chi2 ...
GlobalPoint position() const
void insert(const key_type &k, const data_type &v)
insert an association
std::vector< SimVertex > SimVertexContainer
const_iterator begin() const
std::map< double, SimTrack > Chi2SimMap
std::vector< RecoToSimPair > RecoToSimPairAssociation
size_type size() const
Size of the RefVector.
Definition: RefVector.h:85
const Point & position() const
position
Definition: BeamSpot.h:63
std::vector< TrackingParticle > TrackingParticleCollection
T x() const
Definition: PV3DBase.h:56
std::vector< SimTrack > SimTrackContainer
mathSSE::Vec4< T > v
math::Error< dimension >::type CovarianceMatrix
5 parameter covariance matrix
Definition: TrackBase.h:71
Global3DVector GlobalVector
Definition: GlobalVector.h:10