Belle II Software development
TrackCombiner.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <tracking/trackFindingCDC/findlets/minimal/TrackCombiner.h>
9
10#include <tracking/trackingUtilities/eventdata/tracks/CDCTrack.h>
11#include <tracking/trackingUtilities/eventdata/segments/CDCSegment3D.h>
12#include <tracking/trackingUtilities/eventdata/hits/CDCRLWireHit.h>
13#include <tracking/trackingUtilities/eventdata/hits/CDCWireHit.h>
14
15#include <tracking/trackingUtilities/numerics/Index.h>
16
17#include <tracking/trackingUtilities/utilities/WeightedRelation.h>
18#include <tracking/trackingUtilities/utilities/Functional.h>
19#include <tracking/trackingUtilities/utilities/Range.h>
20#include <tracking/trackingUtilities/utilities/StringManipulation.h>
21
22#include <framework/core/ModuleParamList.templateDetails.h>
23
24#include <map>
25#include <deque>
26
27using namespace Belle2;
28using namespace CDC;
29using namespace TrackFindingCDC;
30using namespace TrackingUtilities;
31
32namespace {
33 std::array<int, ISuperLayerUtil::c_N> getNHitsByISuperLayer(const CDCTrack& track)
34 {
35 std::array<int, ISuperLayerUtil::c_N> nHitsBySLayer = {0};
36 for (const CDCRecoHit3D& hit : track) {
37 nHitsBySLayer[hit.getISuperLayer()]++;
38 }
39 return nHitsBySLayer;
40 }
41
42 CDCTrack condense(const Path<const CDCSegment3D>& segmentPath)
43 {
44 CDCTrack result;
45 for (const CDCSegment3D* segment : segmentPath) {
46 for (const CDCRecoHit3D& recoHit3D : *segment) {
47 result.push_back(recoHit3D);
48 }
49 }
50 if (segmentPath.size()) {
51 result.setStartTrajectory3D(segmentPath.front()->getTrajectory3D());
52 }
53 return result;
54 }
55
56 std::vector<CDCRLWireHit> getRLWireHitSegment(const CDCSegment3D& segment3D)
57 {
58 std::vector<CDCRLWireHit> result;
59 result.reserve(segment3D.size());
60 for (const CDCRecoHit3D& recoHit3D : segment3D) {
61 result.push_back(recoHit3D.getRLWireHit());
62 }
63 return result;
64 }
65
66 std::vector<std::pair<std::pair<Index, Index>, CDCRLWireHit>> getCommonRLWireHits(const std::vector<CDCRLWireHit>& rlWireHits1,
67 const std::vector<CDCRLWireHit>& rlWireHits2)
68 {
69 std::vector<std::pair<std::pair<Index, Index>, CDCRLWireHit>> result;
70 Index index1 = -1;
71 for (const CDCRLWireHit& rlWireHit : rlWireHits1) {
72 ++index1;
73 auto itRLWireHits2 = std::find(rlWireHits2.begin(), rlWireHits2.end(), rlWireHit);
74 if (itRLWireHits2 == rlWireHits2.end()) continue;
75 Index index2 = std::distance(rlWireHits2.begin(), itRLWireHits2);
76 assert(index2 >= 0);
77 result.push_back({{index1, index2}, rlWireHit});
78 }
79 return result;
80 }
81}
82
84
86{
87 return "Combines two sets of tracks to one final set by merging tracks that have large overlaps.";
88}
89
91 const std::string& prefix)
92{
93 moduleParamList->addParameter(prefixed(prefix, "identifyCommonSegments"),
95 "Activate the identification of common segments",
97}
98
99void TrackCombiner::apply(const std::vector<CDCTrack>& inputTracks,
100 const std::vector<CDCTrack>& secondInputTracks,
101 std::vector<CDCTrack>& outputTracks)
102{
103 using InTracks = std::array<const CDCTrack*, 2>;
104
105 // Constructing map of back links
106 std::map<const CDCWireHit*, InTracks> inTracksByWireHit;
107 for (const CDCTrack& track : inputTracks) {
108 for (const CDCRecoHit3D& recoHit3D : track) {
109 const CDCWireHit& wireHit = recoHit3D.getWireHit();
110 inTracksByWireHit[&wireHit][0] = &track;
111 // Prepare hits for the cellular automaton
112 wireHit->unsetTemporaryFlags();
113 wireHit->unsetMaskedFlag();
114 }
115 }
116
117 for (const CDCTrack& track : secondInputTracks) {
118 for (const CDCRecoHit3D& recoHit3D : track) {
119 const CDCWireHit& wireHit = recoHit3D.getWireHit();
120 inTracksByWireHit[&wireHit][1] = &track;
121 // Prepare hits for the cellular automaton
122 wireHit->unsetTemporaryFlags();
123 wireHit->unsetMaskedFlag();
124 }
125 }
126
127 // Rank tracks by number of superlayers touched first and number of hits second
128 std::vector<std::pair<std::pair<int, size_t>, const CDCTrack*> > rankedTracks;
129 for (const CDCTrack& track : inputTracks) {
130 std::array<int, ISuperLayerUtil::c_N> nHitsBySLayer = getNHitsByISuperLayer(track);
131 int nSL = std::count_if(nHitsBySLayer.begin(), nHitsBySLayer.end(), Id() > 0);
132 rankedTracks.push_back({{nSL, track.size()}, &track});
133 }
134 for (const CDCTrack& track : secondInputTracks) {
135 std::array<int, ISuperLayerUtil::c_N> nHitsBySLayer = getNHitsByISuperLayer(track);
136 int nSL = std::count_if(nHitsBySLayer.begin(), nHitsBySLayer.end(), Id() > 0);
137 rankedTracks.push_back({{nSL, track.size()}, &track});
138 }
139 std::sort(rankedTracks.begin(), rankedTracks.end(), GreaterOf<First>());
140
141 // Memory for the split segments
142 std::deque<CDCSegment3D> segments;
143
144 // Memory for the relations between tracks to be followed on linking
145 std::vector<WeightedRelation<const CDCSegment3D>> segmentRelations;
146
147 // Also keep a record to which of the tracks the segment
148 std::map<const CDCSegment3D*, InTracks> inTracksBySegment;
149
150 // Split tracks into segments - break segment such that there will be matching pieces with the other tracks
151 for (const std::pair<std::pair<int, size_t>, const CDCTrack*>& rankedTrack : rankedTracks) {
152 // const std::pair<ISuperLayer, size_t> rank = rankedTrack.first;
153 const CDCTrack* track = rankedTrack.second;
154 std::vector<std::pair<InTracks, CDCSegment3D> > segmentsInTrack;
155
156 // Split track into segments
157 CDCSegment3D* currentSegment = nullptr;
158 ISuperLayer lastISuperLayer = -1;
159 InTracks lastTracksForHit{{nullptr, nullptr}};
160 for (const CDCRecoHit3D& recoHit3D : *track) {
161 ISuperLayer iSuperLayer = recoHit3D.getISuperLayer();
162 std::array<const CDCTrack*, 2> tracksForWireHit = inTracksByWireHit[&recoHit3D.getWireHit()];
163 if (not currentSegment or iSuperLayer != lastISuperLayer or tracksForWireHit != lastTracksForHit) {
164 // Make new segments
165 segmentsInTrack.push_back({tracksForWireHit, CDCSegment3D()});
166 currentSegment = &segmentsInTrack.back().second;
167 currentSegment->setTrajectory3D(track->getStartTrajectory3D());
168 }
169 currentSegment->push_back(recoHit3D);
170 lastISuperLayer = iSuperLayer;
171 lastTracksForHit = tracksForWireHit;
172 }
173
174 // Merge small segments
175 auto absorbsSegment = [](std::pair<InTracks, CDCSegment3D>& segment1,
176 std::pair<InTracks, CDCSegment3D>& segment2) {
177
178 if (segment1.second.getISuperLayer() != segment2.second.getISuperLayer()) return false;
179 if (segment1.second.size() < 3) {
180 segment1.first = segment2.first;
181 }
182 // Absorb segment1 into segment2
183 if (segment1.second.size() < 3 or segment2.second.size() < 3 or
184 segment1.first == segment2.first) {
185 segment1.second.insert(segment1.second.end(),
186 segment2.second.begin(),
187 segment2.second.end());
188 return true;
189 }
190 return false;
191 };
192 auto itSegmentToDrop = std::unique(segmentsInTrack.begin(), segmentsInTrack.end(), absorbsSegment);
193 segmentsInTrack.erase(itSegmentToDrop, segmentsInTrack.end());
194
195 size_t nHits = 0;
196 for (std::pair<InTracks, CDCSegment3D>& segment : segmentsInTrack) {
197 nHits += segment.second.size();
198 }
199 B2ASSERT("Expected the number of hits to be the same", nHits == track->size());
200
201 // Push segments to the common pool
202 const CDCSegment3D* lastSegment = nullptr;
203 for (const std::pair<InTracks, CDCSegment3D>& segmentInTrack : segmentsInTrack) {
204 const InTracks& inTracks = segmentInTrack.first;
205 const CDCSegment3D& segment = segmentInTrack.second;
206 segment->setCellWeight(segment.size());
207 segments.push_back(std::move(segment));
208 inTracksBySegment[&segments.back()] = inTracks;
209 if (lastSegment != nullptr) {
210 segmentRelations.push_back({lastSegment, 0, &segments.back()});
211 }
212 lastSegment = &segments.back();
213 }
214 }
215
216 // Sort the relations for the lookup
217 std::sort(segmentRelations.begin(), segmentRelations.end());
218
219 // Identify common segments, also checking whether they have the same orientation
221 std::vector<std::pair<const CDCSegment3D*, const CDCSegment3D*>> segmentContainmentRelation;
222 for (auto itSegment1 = segments.begin(); itSegment1 != segments.end(); ++itSegment1) {
223 const CDCSegment3D& segment1 = *itSegment1;
224 std::vector<CDCRLWireHit> rlWireHits1 = getRLWireHitSegment(segment1);
225 ISuperLayer iSL1 = segment1.getISuperLayer();
226 InTracks inTracks1 = inTracksBySegment[&segment1];
227 for (auto itSegment2 = itSegment1 + 1; itSegment2 != segments.end(); ++itSegment2) {
228 const CDCSegment3D& segment2 = *itSegment2;
229
230 // Should not happen - here for safety reasons
231 if (&segment1 == &segment2) continue;
232
233 ISuperLayer iSL2 = segment2.getISuperLayer();
234 if (iSL1 != iSL2) continue;
235
236 InTracks inTracks2 = inTracksBySegment[&segment2];
237 if (inTracks1 != inTracks2) continue;
238
239 // Now answering the question if segment 1 is contained in segment 2
240 std::vector<CDCRLWireHit> rlWireHits2 = getRLWireHitSegment(segment2);
241 std::vector<std::pair<std::pair<int, int>, CDCRLWireHit>> commonRLWireHits =
242 getCommonRLWireHits(rlWireHits1, rlWireHits2);
243
244 // Check for containment, requiring that the majority of the wire hits of one is in two
245 // However also require that the two is not too much larger compared to the first one
246 bool contains = commonRLWireHits.size() > 2 and
247 commonRLWireHits.size() > rlWireHits1.size() * 0.8 and
248 commonRLWireHits.size() > rlWireHits2.size() * 0.8;
249 if (not contains) continue;
250
251 // Now finally check whether both segments point in the same direction
252 double n = commonRLWireHits.size();
253 double prod11 = 0;
254 double prod12 = 0;
255 double prod22 = 0;
256 double sum1 = 0;
257 double sum2 = 0;
258 for (const auto& commonRLWireHit : commonRLWireHits) {
259 const std::pair<int, int>& indices = commonRLWireHit.first;
260 prod11 += indices.first * indices.first;
261 prod12 += indices.first * indices.second;
262 prod22 += indices.second * indices.second;
263
264 sum1 += indices.first;
265 sum2 += indices.second;
266 }
267 double var1 = (prod11 - sum1 * sum1 / n);
268 double var2 = (prod22 - sum2 * sum2 / n);
269 double cov12 = (prod12 - sum1 * sum2 / n);
270 double cor = cov12 / std::sqrt(var1 * var2);
271 if (not(cor > 0.75)) continue;
272
273 segmentContainmentRelation.push_back({&segment1, &segment2});
274 }
275 }
276
277 // Create additional edges
278 std::vector<WeightedRelation<const CDCSegment3D>> additionalSegmentRelations;
279 for (std::pair<const CDCSegment3D*, const CDCSegment3D*>& rel : segmentContainmentRelation) {
281 asRange(std::equal_range(segmentRelations.begin(), segmentRelations.end(), rel.first))) {
282 additionalSegmentRelations.push_back({rel.second, 0, rel1.getTo()});
283 }
284
285 for (WeightedRelation<const CDCSegment3D> rel2 : asRange(
286 std::equal_range(segmentRelations.begin(), segmentRelations.end(), rel.second))) {
287 additionalSegmentRelations.push_back({rel.first, 0, rel2.getFrom()});
288 }
289 }
290 segmentRelations.insert(segmentRelations.end(),
291 additionalSegmentRelations.begin(),
292 additionalSegmentRelations.end());
293 std::sort(segmentRelations.begin(), segmentRelations.end());
294 }
295
296 // Extract paths
297 // Obtain the segments as pointers
298 std::vector<const CDCSegment3D*> segmentPtrs = as_pointers<const CDCSegment3D>(segments);
299
300 // Memory for the track paths generated from the graph.
301 std::vector<Path<const CDCSegment3D>> segmentPaths;
302 m_cellularPathFinder.apply(segmentPtrs, segmentRelations, segmentPaths);
303
304 // Put the linked segments together
305 outputTracks.clear();
306 for (const std::vector<const CDCSegment3D*>& segmentPath : segmentPaths) {
307 // Reject single left over segments
308 if (segmentPath.size() < 2) continue;
309 outputTracks.push_back(condense(segmentPath));
310 }
311
312 // Simple approach
313 // Incorporate the second input tracks in to the first input tracks by looking for large overlaps
314 // Very simple approach use the first tracks and add the ones from the second tracks with no overlap to the first
315 // outputTracks.insert(outputTracks.end(), inputTracks.begin(), inputTracks.end());
316 // for (const CDCTrack& secondTrack : secondInputTracks) {
317 // std::map<const CDCTrack*, int> overlappingTracks;
318 // for (const CDCRecoHit3D& recoHit3D : secondTrack) {
319 // const CDCWireHit& wireHit = recoHit3D.getWireHit();
320 // inTracksByWireHit.equal_range(&recoHit3D.getWireHit());
321 // auto tracksForWireHit = asRange(inTracksByWireHit.equal_range(&wireHit));
322 // for (const std::pair<const CDCWireHit*, InTracks>& trackForWireHit : inTracksByWireHit) {
323 // const CDCTrack* track = trackForWireHit.second[0];
324 // if (not track) continue;
325 // overlappingTracks[track]++;
326 // }
327 // }
328 // if (overlappingTracks.size() == 0) {
329 // outputTracks.push_back(secondTrack);
330 // }
331 // }
332}
The Module parameter list class.
Implements a path consisting of Module and/or Path objects.
Definition Path.h:38
void apply(const std::vector< TrackingUtilities::CDCTrack > &inputTracks, const std::vector< TrackingUtilities::CDCTrack > &secondInputTracks, std::vector< TrackingUtilities::CDCTrack > &tracks) final
Main algorithm.
std::string getDescription() final
Short description of the findlet.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
bool m_param_identifyCommonSegments
Parameter : Activate the identification of common segments.
TrackingUtilities::MultipassCellularPathFinder< const TrackingUtilities::CDCSegment3D > m_cellularPathFinder
Instance of the cellular automaton path finder.
void unsetMaskedFlag()
Resets the masked flag to false.
void unsetTemporaryFlags()
Resets the assigned, start and cycle marker flag.
Class representing an oriented hit wire including a hypotheses whether the causing track passes left ...
Class representing a three dimensional reconstructed hit.
A segment consisting of three dimensional reconstructed hits.
CDC::ISuperLayer getISuperLayer() const
Returns the common super layer id of all stored tracking hits.
Definition CDCSegment.h:57
Class representing a sequence of three dimensional reconstructed hits.
Definition CDCTrack.h:37
Class representing a hit wire in the central drift chamber.
Definition CDCWireHit.h:56
Type for two related objects with a weight.
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
signed short ISuperLayer
The type of the layer and superlayer ids.
Definition ISuperLayer.h:24
Abstract base class for different kinds of events.
Generic identity functor.
Definition Functional.h:25