CMS 3D CMS Logo

MatcherByPullsAlgorithm.cc
Go to the documentation of this file.
2 
3 // user include files
5 
6 /* ____ _ _
7  * / ___|___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __
8  * | | / _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__|
9  * | |__| (_) | | | \__ \ |_| | | |_| | (__| || (_) | |
10  * \____\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_|
11  *
12  */
14  : dr2_(std::pow(iConfig.getParameter<double>("maxDeltaR"), 2)),
15  cut_(iConfig.getParameter<double>("maxPull")),
16  diagOnly_(iConfig.getParameter<bool>("diagonalElementsOnly")),
17  useVertex_(iConfig.getParameter<bool>("useVertexVariables")) {
18  std::string track = iConfig.getParameter<std::string>("track");
19  if (track == "standAloneMuon")
20  track_ = StaTrack;
21  else if (track == "combinedMuon")
22  track_ = GlbTrack;
23  else if (track == "track")
24  track_ = TrkTrack;
25  else
26  throw cms::Exception("Configuration")
27  << "MatcherByPullsAlgorithm: track '" << track << "' is not known\n"
28  << "Allowed values are: 'track', 'combinedMuon', 'standAloneMuon' (as per reco::RecoCandidate object)\n";
29 }
30 
32 
33 /* __ __ _ _
34  * | \/ | __ _| |_ ___| |__ ___ _ __ ___
35  * | |\/| |/ _` | __/ __| '_ \ / _ \ '__/ __|
36  * | | | | (_| | || (__| | | | __/ | \__ \
37  * |_| |_|\__,_|\__\___|_| |_|\___|_| |___/
38  *
39  */
40 std::pair<bool, float> MatcherByPullsAlgorithm::match(const reco::Track &tk,
41  const reco::Candidate &c,
42  const AlgebraicSymMatrix55 &invCov) const {
43  if (::deltaR2(tk, c) <= dr2_) {
44  AlgebraicVector5 diff(tk.qoverp() - c.charge() / c.p(),
45  tk.theta() - c.theta(),
46  ::deltaPhi(tk.phi(), c.phi()),
47  tk.dxy(c.vertex()),
48  tk.dsz(c.vertex()));
49  double pull = ROOT::Math::Similarity(diff, invCov);
50 #if 0
51  std::cout << "Tk charge/pt/eta/phi/vx/vy/vz " << tk.charge() << "\t" << tk.pt() << "\t" << tk.eta() << "\t" << tk.phi() << "\t" << tk.vx() << "\t" << tk.vy() << "\t" << tk.vz() << std::endl;
52  std::cout << "MC charge/pt/eta/phi/vx/vy/vz " << c.charge() << "\t" << c.pt() << "\t" << c.eta() << "\t" << c.phi() << "\t" << c.vx() << "\t" << c.vy() << "\t" << c.vz() << std::endl;
53  std::cout << "Delta: " << diff << std::endl;
54  std::cout << "Sigmas: ";
55  for (size_t i = 0; i < 5; ++i) {
56  if (invCov(i,i) == 0) std::cout << "---\t";
57  else std::cout << std::sqrt(1.0/invCov(i,i)) << "\t";
58  }
59  std::cout << std::endl;
60  std::cout << "Items: ";
61  for (size_t i = 0; i < 5; ++i) {
62  if (invCov(i,i) == 0) std::cout << "---\t";
63  else std::cout << diff(i)*std::sqrt(invCov(i,i)) << "\t";
64  }
65  std::cout << std::endl;
66  std::cout << "Pull: " << pull << std::endl;
67 #endif
68  return std::pair<bool, float>(pull < cut_, pull);
69  }
70  return std::pair<bool, float>(false, 9e9);
71 }
72 
74  const std::vector<reco::GenParticle> &cands,
75  const std::vector<uint8_t> &good) const {
76  const reco::Track *tk = track(src);
77  return (tk == nullptr ? std::pair<int, float>(-1, 9e9) : match(*tk, cands, good));
78 }
79 
80 std::pair<int, float> MatcherByPullsAlgorithm::match(const reco::Track &tk,
81  const std::vector<reco::GenParticle> &cands,
82  const std::vector<uint8_t> &good) const {
83  std::pair<int, float> best(-1, 9e9);
84 
85  AlgebraicSymMatrix55 invCov;
86  fillInvCov(tk, invCov);
87  for (int i = 0, n = cands.size(); i < n; ++i) {
88  if (!good[i])
89  continue;
90  std::pair<bool, float> m = match(tk, cands[i], invCov);
91  if (m.first && (m.second < best.second)) {
92  best.first = i;
93  best.second = m.second;
94  }
95  }
96  return best;
97 }
98 
100  const std::vector<reco::GenParticle> &cands,
101  const std::vector<uint8_t> &good,
102  std::vector<std::pair<double, int> > &matchesToFill) const {
103  const reco::Track *tk = track(src);
104  if (tk != nullptr)
105  matchMany(*tk, cands, good, matchesToFill);
106 }
107 
109  const std::vector<reco::GenParticle> &cands,
110  const std::vector<uint8_t> &good,
111  std::vector<std::pair<double, int> > &matchesToFill) const {
112  AlgebraicSymMatrix55 invCov;
113  fillInvCov(tk, invCov);
114  for (int i = 0, n = cands.size(); i < n; ++i) {
115  if (!good[i])
116  continue;
117  std::pair<bool, double> m = match(tk, cands[i], invCov);
118  if (m.first)
119  matchesToFill.push_back(std::make_pair(m.second, i));
120  }
121  std::sort(matchesToFill.begin(), matchesToFill.end());
122 }
123 
124 /* _ _ _ _ _ _ _ _
125  * | | | | |_(_) (_) |_(_) ___ ___
126  * | | | | __| | | | __| |/ _ \/ __|
127  * | |_| | |_| | | | |_| | __/\__ \
128  * \___/ \__|_|_|_|\__|_|\___||___/
129  *
130  */
132  switch (track_) {
133  case StaTrack:
134  return muon.standAloneMuon().isNonnull() ? muon.standAloneMuon().get() : nullptr;
135  case GlbTrack:
136  return muon.combinedMuon().isNonnull() ? muon.combinedMuon().get() : nullptr;
137  case TrkTrack:
138  return muon.track().isNonnull() ? muon.track().get() : nullptr;
139  }
140  assert(false);
141 }
142 
144  if (useVertex_) {
145  invCov = tk.covariance();
146  if (diagOnly_) {
147  for (size_t i = 0; i < 5; ++i) {
148  for (size_t j = i + 1; j < 5; ++j) {
149  invCov(i, j) = 0;
150  }
151  }
152  }
153  invCov.Invert();
154  } else {
155  AlgebraicSymMatrix33 momCov = tk.covariance().Sub<AlgebraicSymMatrix33>(0, 0); // get 3x3 matrix
156  if (diagOnly_) {
157  momCov(0, 1) = 0;
158  momCov(0, 2) = 0;
159  momCov(1, 2) = 0;
160  }
161  momCov.Invert();
162  invCov.Place_at(momCov, 0, 0);
163  }
164 }
double qoverp() const
q / p
Definition: TrackBase.h:599
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void fillInvCov(const reco::Track &tk, AlgebraicSymMatrix55 &invCov) const
Fill the inverse covariance matrix for the match(track, candidate, invCov) method.
double vx() const
x coordinate of the reference point on track
Definition: TrackBase.h:655
bool diagOnly_
Use only the diagonal terms of the covariance matrix.
MatcherByPullsAlgorithm(const edm::ParameterSet &)
const reco::Track * track(const reco::RecoCandidate &src) const
Get track out of Candidate, NULL if missing.
assert(be >=bs)
double dsz() const
dsz parameter (THIS IS NOT the SZ impact parameter to (0,0,0) if refPoint is far from (0...
Definition: TrackBase.h:614
double dr2_
DeltaR of the matching cone.
double pt() const
track transverse momentum
Definition: TrackBase.h:637
bool useVertex_
Use also dxy / dsz in the matching.
int charge() const
track electric charge
Definition: TrackBase.h:596
CovarianceMatrix covariance() const
return track covariance matrix
Definition: TrackBase.h:716
double vz() const
z coordinate of the reference point on track
Definition: TrackBase.h:661
std::pair< bool, float > match(const reco::Track &tk, const reco::Candidate &mc, const AlgebraicSymMatrix55 &invertedCovariance) const
T sqrt(T t)
Definition: SSEVec.h:19
ROOT::Math::SVector< double, 5 > AlgebraicVector5
double phi() const
azimuthal angle of momentum vector
Definition: TrackBase.h:649
double cut_
Cut on the pull.
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:652
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
double theta() const
polar angle
Definition: TrackBase.h:602
TrackChoice track_
Track to be used in matching.
double vy() const
y coordinate of the reference point on track
Definition: TrackBase.h:658
void matchMany(const reco::RecoCandidate &src, const std::vector< reco::GenParticle > &cands, const std::vector< uint8_t > &good, std::vector< std::pair< double, int > > &matchesToFill) const
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepSym< double, 3 > > AlgebraicSymMatrix33
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
double dxy() const
dxy parameter. (This is the transverse impact parameter w.r.t. to (0,0,0) ONLY if refPoint is close t...
Definition: TrackBase.h:608