CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackGenAssociatorByChi2.cc
Go to the documentation of this file.
3 
5 
6 #include "getChi2.h"
7 
8 using namespace edm;
9 using namespace reco;
10 using namespace std;
11 
13  const TrackBase::CovarianceMatrix& recoTrackCovMatrix,
14  const Basic3DVector<double>& momAtVtx,
15  const Basic3DVector<double>& vert,
16  int charge,
17  const reco::BeamSpot& bs) const{
18  return track_associator::getChi2(rParameters, recoTrackCovMatrix,momAtVtx, vert, charge, *theMF, bs);
19 }
20 
23  const edm::Event * e,
24  const edm::EventSetup *setup ) const{
25  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
26  e->getByLabel(bsSrc,recoBeamSpotHandle);
27  reco::BeamSpot bs = *recoBeamSpotHandle;
28 
29  RecoToGenCollection outputCollection;
30 
31  //dereference the edm::Ref's only once
32  std::vector<const GenParticle*> tPC;
33  tPC.reserve(tPCH.size());
34  for(auto const& ref: tPCH) {
35  tPC.push_back(&(*ref));
36  }
37 
38  int tindex=0;
39  for (RefToBaseVector<reco::Track>::const_iterator rt=tC.begin(); rt!=tC.end(); rt++, tindex++){
40 
41  LogDebug("TrackAssociator") << "=========LOOKING FOR ASSOCIATION===========" << "\n"
42  << "rec::Track #"<<tindex<<" with pt=" << (*rt)->pt() << "\n"
43  << "===========================================" << "\n";
44 
45  TrackBase::ParameterVector rParameters = (*rt)->parameters();
46 
47  TrackBase::CovarianceMatrix recoTrackCovMatrix = (*rt)->covariance();
48  if (onlyDiagonal){
49  for (unsigned int i=0;i<5;i++){
50  for (unsigned int j=0;j<5;j++){
51  if (i!=j) recoTrackCovMatrix(i,j)=0;
52  }
53  }
54  }
55 
56  recoTrackCovMatrix.Invert();
57 
58  int tpindex =0;
59  for (auto tp=tPC.begin(); tp!=tPC.end(); tp++, ++tpindex){
60 
61  //skip tps with a very small pt
62  //if (sqrt((*tp)->momentum().perp2())<0.5) continue;
63  int charge = (*tp)->charge();
64  if (charge==0) continue;
65  Basic3DVector<double> momAtVtx((*tp)->momentum().x(),(*tp)->momentum().y(),(*tp)->momentum().z());
66  Basic3DVector<double> vert=(Basic3DVector<double>) (*tp)->vertex();
67 
68  double chi2 = getChi2(rParameters,recoTrackCovMatrix,momAtVtx,vert,charge,bs);
69 
70  if (chi2<chi2cut) {
71  //NOTE: tPCH and tPC have the same index for the same object
72  outputCollection.insert(tC[tindex],
73  std::make_pair(tPCH[tpindex],
74  -chi2));//-chi2 because the Association Map is ordered using std::greater
75  }
76  }
77  }
78  outputCollection.post_insert();
79  return outputCollection;
80 }
81 
82 
85  const edm::Event * e,
86  const edm::EventSetup *setup ) const {
87 
88  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
89  e->getByLabel(bsSrc,recoBeamSpotHandle);
90  reco::BeamSpot bs = *recoBeamSpotHandle;
91 
92  GenToRecoCollection outputCollection;
93 
94  //dereference the edm::Refs only once
95  std::vector<GenParticle const*> tPC;
96  tPC.reserve(tPCH.size());
97  for(auto const& ref: tPCH) {
98  tPC.push_back(&(*ref));
99  }
100 
101  int tpindex =0;
102  for (auto tp=tPC.begin(); tp!=tPC.end(); tp++, ++tpindex){
103 
104  //skip tps with a very small pt
105  //if (sqrt((*tp)->momentum().perp2())<0.5) continue;
106  int charge = (*tp)->charge();
107  if (charge==0) continue;
108 
109  LogDebug("TrackAssociator") << "=========LOOKING FOR ASSOCIATION===========" << "\n"
110  << "TrackingParticle #"<<tpindex<<" with pt=" << sqrt((*tp)->momentum().perp2()) << "\n"
111  << "===========================================" << "\n";
112 
113  Basic3DVector<double> momAtVtx((*tp)->momentum().x(),(*tp)->momentum().y(),(*tp)->momentum().z());
114  Basic3DVector<double> vert((*tp)->vertex().x(),(*tp)->vertex().y(),(*tp)->vertex().z());
115 
116  int tindex=0;
117  for (RefToBaseVector<reco::Track>::const_iterator rt=tC.begin(); rt!=tC.end(); rt++, tindex++){
118 
119  TrackBase::ParameterVector rParameters = (*rt)->parameters();
120  TrackBase::CovarianceMatrix recoTrackCovMatrix = (*rt)->covariance();
121  if (onlyDiagonal) {
122  for (unsigned int i=0;i<5;i++){
123  for (unsigned int j=0;j<5;j++){
124  if (i!=j) recoTrackCovMatrix(i,j)=0;
125  }
126  }
127  }
128  recoTrackCovMatrix.Invert();
129 
130  double chi2 = getChi2(rParameters,recoTrackCovMatrix,momAtVtx,vert,charge,bs);
131 
132  if (chi2<chi2cut) {
133  //NOTE: tPCH and tPC have the same index for the same object
134  outputCollection.insert(tPCH[tpindex],
135  std::make_pair(tC[tindex],
136  -chi2));//-chi2 because the Association Map is ordered using std::greater
137  }
138  }
139  }
140  outputCollection.post_insert();
141  return outputCollection;
142 }
#define LogDebug(id)
int i
Definition: DBlmapReader.cc:9
reco::GenToRecoCollection associateGenToReco(const edm::RefToBaseVector< reco::Track > &, const edm::RefVector< reco::GenParticleCollection > &, const edm::Event *event=0, const edm::EventSetup *setup=0) const
Association Sim To Reco with Collections (Gen Particle version)
double getChi2(const reco::TrackBase::ParameterVector &rParameters, const reco::TrackBase::CovarianceMatrix &recoTrackCovMatrix, const Basic3DVector< double > &momAtVtx, const Basic3DVector< double > &vert, int charge, const MagneticField &magfield, const reco::BeamSpot &bs)
basic method where chi2 is computed
Definition: getChi2.cc:7
const_iterator end() const
math::Vector< dimension >::type ParameterVector
parameter vector
Definition: TrackBase.h:74
void post_insert()
post insert action
T sqrt(T t)
Definition: SSEVec.h:48
int j
Definition: DBlmapReader.cc:9
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:405
void insert(const key_type &k, const data_type &v)
insert an association
reco::RecoToGenCollection associateRecoToGen(const edm::RefToBaseVector< reco::Track > &, const edm::RefVector< reco::GenParticleCollection > &, const edm::Event *event=0, const edm::EventSetup *setup=0) const
Association Sim To Reco with Collections (Gen Particle version)
const_iterator begin() const
size_type size() const
Size of the RefVector.
Definition: RefVector.h:89
double getChi2(const reco::TrackBase::ParameterVector &rParameters, const reco::TrackBase::CovarianceMatrix &recoTrackCovMatrix, const Basic3DVector< double > &momAtVtx, const Basic3DVector< double > &vert, int charge, const reco::BeamSpot &) const
basic method where chi2 is computed
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
math::Error< dimension >::type CovarianceMatrix
5 parameter covariance matrix
Definition: TrackBase.h:77