CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
VertexAssociatorByTracks.cc
Go to the documentation of this file.
2 
8 
11 
15 
17 
19 
20 /* Constructor */
22  double R2SMatchedSimRatio,
23  double R2SMatchedRecoRatio,
24  double S2RMatchedSimRatio,
25  double S2RMatchedRecoRatio,
26  const TrackingParticleSelector *selector,
28  const reco::RecoToSimCollection *trackRecoToSimAssociation,
29  const reco::SimToRecoCollection *trackSimToRecoAssociation)
30  : productGetter_(productGetter),
31  R2SMatchedSimRatio_(R2SMatchedSimRatio),
32  R2SMatchedRecoRatio_(R2SMatchedRecoRatio),
33  S2RMatchedSimRatio_(S2RMatchedSimRatio),
34  S2RMatchedRecoRatio_(S2RMatchedRecoRatio),
35  selector_(selector),
36  trackQuality_(trackQuality),
37  trackRecoToSimAssociation_(trackRecoToSimAssociation),
38  trackSimToRecoAssociation_(trackSimToRecoAssociation) {}
39 
40 /* Destructor */
42 
44  const edm::Handle<edm::View<reco::Vertex>> &recoVertexes,
45  const edm::Handle<TrackingVertexCollection> &trackingVertexes) const {
47 
48  std::map<TrackingVertexRef, std::pair<double, std::size_t>> matches;
49 
50  LogDebug("VertexAssociation") << "reco::VertexCollection size = " << recoVertexes->size()
51  << " ; TrackingVertexCollection size = " << trackingVertexes->size() << std::endl;
52 
53  // Loop over RecoVertex
54  for (std::size_t recoIndex = 0; recoIndex < recoVertexes->size(); ++recoIndex) {
55  reco::VertexBaseRef recoVertex(recoVertexes, recoIndex);
56 
57  matches.clear();
58 
59  double recoDaughterWeight = 0.;
60 
61  // Loop over daughter tracks of RecoVertex
62  for (reco::Vertex::trackRef_iterator recoDaughter = recoVertex->tracks_begin();
63  recoDaughter != recoVertex->tracks_end();
64  ++recoDaughter) {
65  // Check the quality of the RecoDoughters
66  if (!(*recoDaughter)->quality(trackQuality_))
67  continue;
68 
69  // Check for association for the given RecoDaughter
70  if (trackRecoToSimAssociation_->numberOfAssociations(*recoDaughter) > 0) {
71  std::vector<std::pair<TrackingParticleRef, double>> associations = (*trackRecoToSimAssociation_)[*recoDaughter];
72 
73  // Loop over TrackingParticles associated with RecoDaughter
74  for (std::vector<std::pair<TrackingParticleRef, double>>::const_iterator association = associations.begin();
75  association != associations.end();
76  ++association) {
77  // Get a reference to parent vertex of TrackingParticle associated to
78  // the RecoDaughter
79  TrackingVertexRef trackingVertex = association->first->parentVertex();
80  // Store matched RecoDaughter to the trackingVertex
81  matches[trackingVertex].first += recoVertex->trackWeight(*recoDaughter);
82  matches[trackingVertex].second++;
83  }
84  }
85 
86  recoDaughterWeight += recoVertex->trackWeight(*recoDaughter);
87  } // loop over daughter tracks of RecoVertex
88 
89  std::size_t assoIndex = 0;
90 
91  // Loop over map between TrackingVertexes and matched RecoDaugther
92  for (std::map<TrackingVertexRef, std::pair<double, std::size_t>>::const_iterator match = matches.begin();
93  match != matches.end();
94  ++match) {
95  // Getting the TrackingVertex information
96  TrackingVertexRef trackingVertex = match->first;
97  double matchedDaughterWeight = match->second.first;
98  std::size_t matchedDaughterCounter = match->second.second;
99 
100  // Count for only reconstructible SimDaughters
101  std::size_t simDaughterCounter = 0;
102 
103  for (TrackingVertex::tp_iterator simDaughter = trackingVertex->daughterTracks_begin();
104  simDaughter != trackingVertex->daughterTracks_end();
105  ++simDaughter)
106  if ((*selector_)(**simDaughter))
107  simDaughterCounter++;
108 
109  // Sanity condition in case that reconstructable condition is too tight
110  if (simDaughterCounter < matchedDaughterCounter)
111  simDaughterCounter = matchedDaughterCounter;
112 
113  // Condition over S2RMatchedSimRatio
114  if ((double)matchedDaughterCounter / simDaughterCounter < R2SMatchedSimRatio_)
115  continue;
116 
117  double quality = (double)matchedDaughterWeight / recoDaughterWeight;
118 
119  // Condition over R2SMatchedRecoRatio
120  if (quality < R2SMatchedRecoRatio_)
121  continue;
122 
123  outputCollection.insert(recoVertex, std::make_pair(trackingVertex, quality));
124 
125  LogTrace("VertexAssociation") << "R2S: INDEX " << assoIndex << " ; SimDaughterCounter = " << simDaughterCounter
126  << " ; RecoDaughterWeight = " << recoDaughterWeight
127  << " ; MatchedDaughterCounter = " << matchedDaughterCounter
128  << " ; MatchedDaughterWeight = " << matchedDaughterWeight
129  << " ; quality = " << quality;
130 
131  assoIndex++;
132  }
133  } // Loop on RecoVertex
134 
135  LogTrace("VertexAssociation") << "\nRecoToSim OUTPUT COLLECTION: outputCollection.size() = "
136  << outputCollection.size() << std::endl;
137 
138  return outputCollection;
139 }
140 
142  const edm::Handle<edm::View<reco::Vertex>> &recoVertexes,
143  const edm::Handle<TrackingVertexCollection> &trackingVertexes) const {
145 
146  // Loop over TrackingVertexes
147  std::map<std::size_t, std::pair<double, std::size_t>> matches;
148 
149  // Loop over TrackingVertexes
150  for (std::size_t simIndex = 0; simIndex < trackingVertexes->size(); ++simIndex) {
151  TrackingVertexRef trackingVertex(trackingVertexes, simIndex);
152 
153  matches.clear();
154 
155  std::size_t simDaughterCounter = 0;
156 
157  // Loop over daughter tracks of TrackingVertex
158  for (TrackingVertex::tp_iterator simDaughter = trackingVertex->daughterTracks_begin();
159  simDaughter != trackingVertex->daughterTracks_end();
160  ++simDaughter) {
161  // Select only reconstructible SimDaughters
162  if (!(*selector_)(**simDaughter))
163  continue;
164 
165  // Check for association for the given RecoDaughter
166  if (trackSimToRecoAssociation_->numberOfAssociations(*simDaughter) > 0) {
167  std::vector<std::pair<reco::TrackBaseRef, double>> associations = (*trackSimToRecoAssociation_)[*simDaughter];
168 
169  // Loop over RecoTracks associated with TrackingParticle
170  for (std::vector<std::pair<reco::TrackBaseRef, double>>::const_iterator association = associations.begin();
171  association != associations.end();
172  ++association) {
173  reco::TrackBaseRef recoTrack = association->first;
174 
175  for (std::size_t recoIndex = 0; recoIndex < recoVertexes->size(); ++recoIndex) {
176  reco::VertexBaseRef recoVertex(recoVertexes, recoIndex);
177 
178  for (reco::Vertex::trackRef_iterator recoDaughter = recoVertex->tracks_begin();
179  recoDaughter != recoVertex->tracks_end();
180  ++recoDaughter) {
181  // Store matched RecoDaughter to the RecoVertex
182  if (recoDaughter->id() == recoTrack.id() && recoDaughter->key() == recoTrack.key()) {
183  matches[recoIndex].first += recoVertex->trackWeight(*recoDaughter);
184  matches[recoIndex].second++;
185  }
186  }
187  }
188  } // loop a recotracks
189  }
190 
191  simDaughterCounter++;
192  } // loop a simDaughter
193 
194  std::size_t assoIndex = 0;
195 
196  // Loop over map, set score, add to outputCollection
197  for (std::map<std::size_t, std::pair<double, std::size_t>>::const_iterator match = matches.begin();
198  match != matches.end();
199  ++match) {
200  // Getting the TrackingVertex information
201  reco::VertexBaseRef recoVertex(recoVertexes, match->first);
202  double matchedDaughterWeight = match->second.first;
203  std::size_t matchedDaughterCounter = match->second.second;
204 
205  double recoDaughterWeight = 0.;
206 
207  // Weighted count of those tracks with a given quality of the
208  // RecoDoughters
209  for (reco::Vertex::trackRef_iterator recoDaughter = recoVertex->tracks_begin();
210  recoDaughter != recoVertex->tracks_end();
211  ++recoDaughter)
212  if ((*recoDaughter)->quality(trackQuality_))
213  recoDaughterWeight += recoVertex->trackWeight(*recoDaughter);
214 
215  // Sanity condition in case that track quality condition is too tight
216  if (recoDaughterWeight < matchedDaughterWeight)
217  recoDaughterWeight = matchedDaughterWeight;
218 
219  // Condition over S2RMatchedRecoRatio
220  if (matchedDaughterWeight / recoDaughterWeight < S2RMatchedRecoRatio_)
221  continue;
222 
223  double quality = (double)matchedDaughterCounter / simDaughterCounter;
224 
225  // Condition over S2RMatchedSimRatio
226  if (quality < S2RMatchedSimRatio_)
227  continue;
228 
229  outputCollection.insert(trackingVertex, std::make_pair(recoVertex, quality));
230 
231  LogTrace("VertexAssociation") << "R2S: INDEX " << assoIndex << " ; SimDaughterCounter = " << simDaughterCounter
232  << " ; RecoDaughterWeight = " << recoDaughterWeight
233  << " ; MatchedDaughterCounter = " << matchedDaughterCounter
234  << " ; MatchedDaughterWeight = " << matchedDaughterWeight
235  << " ; quality = " << quality;
236 
237  assoIndex++;
238  }
239  } // loop over TrackingVertexes
240 
241  LogTrace("VertexAssociation") << "SimToReco OUTPUT COLLECTION: outputCollection.size() = " << outputCollection.size()
242  << std::endl;
243 
244  return outputCollection;
245 }
const TrackingParticleSelector * selector_
TrackQuality
track quality
Definition: TrackBase.h:150
string quality
EDProductGetter const * productGetter(std::atomic< void const * > const &iCache)
ProductID id() const
Definition: RefToBase.h:214
size_type numberOfAssociations(const key_type &k) const
number of associations to a key
reco::VertexSimToRecoCollection associateSimToReco(const edm::Handle< edm::View< reco::Vertex >> &vCH, const edm::Handle< TrackingVertexCollection > &tVCH) const override
#define LogTrace(id)
std::tuple< layerClusterToCaloParticle, caloParticleToLayerCluster > association
size_t key() const
Definition: RefToBase.h:219
const reco::RecoToSimCollection * trackRecoToSimAssociation_
trackRef_iterator tracks_end() const
last iterator over tracks
Definition: Vertex.h:110
float trackWeight(const TREF &r) const
returns the weight with which a Track has contributed to the vertex-fit.
Definition: Vertex.h:96
trackRef_iterator tracks_begin() const
first iterator over tracks
Definition: Vertex.h:108
const reco::TrackBase::TrackQuality trackQuality_
size_type size() const
map size
void insert(const key_type &k, const data_type &v)
insert an association
const reco::SimToRecoCollection * trackSimToRecoAssociation_
reco::VertexRecoToSimCollection associateRecoToSim(const edm::Handle< edm::View< reco::Vertex >> &vCH, const edm::Handle< TrackingVertexCollection > &tVCH) const override
VertexAssociatorByTracks(const edm::EDProductGetter *productGetter, double R2SMatchedSimRatio, double R2SMatchedRecoRatio, double S2RMatchedSimRatio, double S2RMatchedRecoRatio, const TrackingParticleSelector *selector, reco::TrackBase::TrackQuality trackQuality, const reco::RecoToSimCollection *trackRecoToSimAssociation, const reco::SimToRecoCollection *trackSimToRecoAssociation)
const edm::EDProductGetter * productGetter_
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
std::vector< TrackBaseRef >::const_iterator trackRef_iterator
The iteratator for the vector&lt;TrackRef&gt;
Definition: Vertex.h:38
#define LogDebug(id)