Belle II Software development
SegmentTrackAdderWithNormalization.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/SegmentTrackAdderWithNormalization.h>
9
10#include <tracking/trackingUtilities/eventdata/tracks/CDCTrack.h>
11#include <tracking/trackingUtilities/eventdata/segments/CDCSegment2D.h>
12
13#include <framework/core/ModuleParamList.templateDetails.h>
14
15#include <tracking/trackingUtilities/utilities/StringManipulation.h>
16#include <tracking/trackingUtilities/utilities/Algorithms.h>
17#include <vector>
18
19using namespace Belle2;
20using namespace TrackFindingCDC;
21using namespace TrackingUtilities;
22
29
30void SegmentTrackAdderWithNormalization::exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
31{
32 m_singleHitSelector.exposeParameters(moduleParamList, prefixed(prefix, "hitSelector"));
33 moduleParamList->addParameter(prefixed(prefix, "removeUnmatchedSegments"),
35 "Switch to remove hits in segments that have no matching track from all tracks",
37
38}
39
41{
42 return "Add the matched segments to the tracks and normalize the tracks afterwards. Also deletes all "
43 "hits from tracks, that are now part in another track (or should not be part in any).";
44}
45
47 std::vector<CDCTrack>& tracks, const std::vector<CDCSegment2D>& segments)
48{
49 // Storage space for the hits
50 // Important to reserve enough space as otherwise references to the elements used by the
51 // `WeightedRelations` in the vector `trackHitRelations` are not valid anymore
52 // !!! We cannot use a deque or list as these do not guarantee valid pointer comparisons as
53 // used by `std::sort` below !!!
54 std::vector<CDCRecoHit3D> recoHits3D;
55 int hit_size = 0;
56 for (const auto& relation : relations) {
57 hit_size += relation.getTo()->size();
58 }
59 for (const CDCSegment2D& segment : segments) {
60 hit_size += segment.size();
61 }
62 for (CDCTrack& track : tracks) {
63 hit_size += track.size();
64 }
65 recoHits3D.reserve(hit_size);
66
67 // Relations for the matching tracks
68 std::vector<WeightedRelation<CDCTrack, const CDCRecoHit3D>> trackHitRelations;
69 trackHitRelations.reserve(2500);
70
71 // We construct track hit relations denoting to which track each hit should belong from 3 sources
72 // 1. From the given track segment matches with the weight of the match
73 // 2. From the unmatch, untaken segments schedule the hits for removal with lower weight
74 // 3. From the original track content with lowest weight
75 // Hence if a hit is mentioned in source 1. it takes precedence over 2. and 3. to so on.
76
77 // 1. Add the relations for the matched segments with the match weight
78 for (const auto& relation : relations) {
79 CDCTrack* track = relation.getFrom();
80 const CDCSegment2D& segment = *(relation.getTo());
81 const Weight weight = relation.getWeight();
82 const CDCTrajectory3D& trajectory3D = track->getStartTrajectory3D();
83
84 for (const CDCRecoHit2D& recoHit : segment) {
85
86 // In case the hit is already in the matched track - keep its reconstructed position
87 MayBePtr<const CDCRecoHit3D> ptrRecoHit3D = track->find(recoHit.getWireHit());
88 if (ptrRecoHit3D != nullptr) {
89 recoHits3D.push_back(*ptrRecoHit3D);
90 trackHitRelations.push_back({track, weight, &recoHits3D.back()});
91 continue;
92 }
93
94 // Otherwise reconstruct the position into the third dimension
95 CDCRecoHit3D recoHit3D = CDCRecoHit3D::reconstruct(recoHit, trajectory3D);
96 if (std::isnan(recoHit3D.getArcLength2D())) {
97 B2DEBUG(25, "Had to skip a NAN hit");
98 continue;
99 }
100 recoHits3D.push_back(recoHit3D);
101 trackHitRelations.push_back({track, weight, &recoHits3D.back()});
102 }
103 segment->setTakenFlag();
104 }
105
106 // 2. Add also those segments, that have no track-partner and schedule them for removal
108 for (const CDCSegment2D& segment : segments) {
109
110 // Skip segment already used in the steps before or marked outside as already taken.
111 if (segment->hasTakenFlag()) continue;
112
113 // Add hit with destination track nullptr
114 for (const CDCRecoHit2D& recoHit : segment) {
115 recoHits3D.push_back({recoHit.getRLWireHit(), Vector3D(recoHit.getRecoPos2D()), 0});
116 trackHitRelations.push_back({nullptr, 0, &recoHits3D.back()});
117 }
118 }
119 }
120
121 // 3. Add the original hit content of the track with lowest priority
122 for (CDCTrack& track : tracks) {
123 for (const CDCRecoHit3D& recoHit3D : track) {
124 recoHits3D.push_back(recoHit3D);
125 // cppcheck-suppress invalidContainer
126 trackHitRelations.push_back({&track, -INFINITY, &recoHits3D.back()});
127 }
128 }
129
130 // Thin out the weighted relations by selecting only the best matching track for each hit.
131 std::sort(trackHitRelations.begin(), trackHitRelations.end());
132 m_singleHitSelector.apply(trackHitRelations);
133
134 // Remove all hits from the tracks in order to rebuild them completely
135 for (CDCTrack& track : tracks) {
136 for (const CDCRecoHit3D& recoHit3D : track) {
137 recoHit3D.getWireHit()->unsetTakenFlag();
138 }
139 track.clear();
140 }
141
142 // Now add the hits to their destination tracks
143 for (const auto& trackHitRelation : trackHitRelations) {
144 CDCTrack* track = trackHitRelation.getFrom();
145 const CDCRecoHit3D* recoHit3D = trackHitRelation.getTo();
146
147 if (track == nullptr) continue;
148
149 track->push_back(*recoHit3D);
150 recoHit3D->getWireHit()->setTakenFlag();
151 }
152
153 // Drop tracks which have no hits
154 erase_remove_if(tracks, [](const CDCTrack & track) { return track.empty(); });
155
156 // Establish the ordering
157 for (CDCTrack& track : tracks) {
158 track.sortByArcLength2D();
159 CDCTrajectory3D startTrajectory = track.getStartTrajectory3D();
160 startTrajectory.setLocalOrigin(track.front().getRecoPos3D());
161 track.setStartTrajectory3D(startTrajectory);
162
163 CDCTrajectory3D endTrajectory = track.getEndTrajectory3D();
164 endTrajectory.setLocalOrigin(track.back().getRecoPos3D());
165 track.setEndTrajectory3D(endTrajectory);
166 }
167
168 // Normalize the trajectory and hit contents of the tracks
169 m_trackNormalizer.apply(tracks);
170}
The Module parameter list class.
std::string getDescription() override
Short description of the findlet.
void apply(std::vector< TrackingUtilities::WeightedRelation< TrackingUtilities::CDCTrack, const TrackingUtilities::CDCSegment2D > > &relations, std::vector< TrackingUtilities::CDCTrack > &tracks, const std::vector< TrackingUtilities::CDCSegment2D > &segment) override
Apply the findlet.
TrackNormalizer m_trackNormalizer
Findlet for performing the normalization of the tracks afterwards.
bool m_param_removeUnmatchedSegments
Parameter : Switch to remove hits in segments that have no matching track from all tracks.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) override
Expose the parameters of the sub-findlets.
TrackingUtilities::SingleMatchSelector< TrackingUtilities::CDCTrack, TrackingUtilities::CDCRecoHit3D, TrackingUtilities::HitComperator > m_singleHitSelector
The selector for finding the track each hit should belong to.
TrackingUtilities::Findlet< TrackingUtilities::WeightedRelation< TrackingUtilities::CDCTrack, const TrackingUtilities::CDCSegment2D > &, TrackingUtilities::CDCTrack &, const TrackingUtilities::CDCSegment2D > Super
Type of the base class.
SegmentTrackAdderWithNormalization()
Constructor for registering the sub-findlets.
void setTakenFlag(bool setTo=true)
Sets the taken flag to the given value. Default value true.
Class representing a two dimensional reconstructed hit in the central drift chamber.
Class representing a three dimensional reconstructed hit.
const CDCWireHit & getWireHit() const
Getter for the wire hit.
static CDCRecoHit3D reconstruct(const CDCRecoHit2D &recoHit2D, const CDCTrajectory2D &trajectory2D)
Reconstructs the three dimensional hit from the two dimensional and the two dimensional trajectory.
double getArcLength2D() const
Getter for the travel distance in the xy projection.
A reconstructed sequence of two dimensional hits in one super layer.
Class representing a sequence of three dimensional reconstructed hits.
Definition CDCTrack.h:39
Particle full three dimensional trajectory.
double setLocalOrigin(const Vector3D &localOrigin)
Setter for the origin of the local coordinate system.
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.
HepGeom::Vector3D< double > Vector3D
3D Vector
Definition Cell.h:34
Abstract base class for different kinds of events.