Belle II Software  release-05-01-25
SegmentTrackAdderWithNormalization.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <tracking/trackFindingCDC/findlets/minimal/SegmentTrackAdderWithNormalization.h>
11 
12 #include <tracking/trackFindingCDC/eventdata/tracks/CDCTrack.h>
13 #include <tracking/trackFindingCDC/eventdata/segments/CDCSegment2D.h>
14 
15 #include <framework/core/ModuleParamList.templateDetails.h>
16 
17 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
18 #include <tracking/trackFindingCDC/utilities/Algorithms.h>
19 #include <vector>
20 
21 using namespace Belle2;
22 using namespace TrackFindingCDC;
23 
25  : Super()
26 {
29 }
30 
31 void SegmentTrackAdderWithNormalization::exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
32 {
33  m_singleHitSelector.exposeParameters(moduleParamList, prefixed(prefix, "hitSelector"));
34  moduleParamList->addParameter(prefixed(prefix, "removeUnmatchedSegments"),
36  "Swtich to remove hits in segments that have no matching track from all tracks",
38 
39 }
40 
42 {
43  return "Add the matched segments to the tracks and normalize the tracks afterwards. Also deletes all "
44  "hits from tracks, that are now part in another track (or should not be part in any).";
45 }
46 
48  std::vector<CDCTrack>& tracks, const std::vector<CDCSegment2D>& segments)
49 {
50  // Storage space for the hits
51  // Important to reserve enough space as otherwise references to the elements used by the
52  // `WeightedRelations` in the vector `trackHitRelations` are not valid anymore
53  // !!! We cannot use a deque or list as these do not guarantee valid pointer comparisons as
54  // used by `std::sort` below !!!
55  std::vector<CDCRecoHit3D> recoHits3D;
56  int hit_size = 0;
57  for (const auto& relation : relations) {
58  hit_size += relation.getTo()->size();
59  }
60  for (const CDCSegment2D& segment : segments) {
61  hit_size += segment.size();
62  }
63  for (CDCTrack& track : tracks) {
64  hit_size += track.size();
65  }
66  recoHits3D.reserve(hit_size);
67 
68  // Relations for the matching tracks
69  std::vector<WeightedRelation<CDCTrack, const CDCRecoHit3D>> trackHitRelations;
70  trackHitRelations.reserve(2500);
71 
72  // We construct track hit relations denoting to which track each hit should belong from 3 sources
73  // 1. From the given track segment matches with the weight of the match
74  // 2. From the unmatch, untaken segments schedule the hits for removal with lower weight
75  // 3. From the original track content with lowest weight
76  // Hence if a hit is mentioned in source 1. it takes precedence over 2. and 3. to so on.
77 
78  // 1. Add the relations for the matched segments with the match weight
79  for (const auto& relation : relations) {
80  CDCTrack* track = relation.getFrom();
81  const CDCSegment2D& segment = *(relation.getTo());
82  const Weight weight = relation.getWeight();
83  const CDCTrajectory3D& trajectory3D = track->getStartTrajectory3D();
84 
85  for (const CDCRecoHit2D& recoHit : segment) {
86 
87  // In case the hit is already in the matched track - keep its reconstructed position
88  MayBePtr<const CDCRecoHit3D> ptrRecoHit3D = track->find(recoHit.getWireHit());
89  if (ptrRecoHit3D != nullptr) {
90  recoHits3D.push_back(*ptrRecoHit3D);
91  trackHitRelations.push_back({track, weight, &recoHits3D.back()});
92  continue;
93  }
94 
95  // Otherwise reconstruct the position into the third dimension
96  CDCRecoHit3D recoHit3D = CDCRecoHit3D::reconstruct(recoHit, trajectory3D);
97  if (std::isnan(recoHit3D.getArcLength2D())) {
98  B2DEBUG(100, "Had to skip a NAN hit");
99  continue;
100  }
101  recoHits3D.push_back(recoHit3D);
102  trackHitRelations.push_back({track, weight, &recoHits3D.back()});
103  }
104  segment->setTakenFlag();
105  }
106 
107  // 2. Add also those segments, that have no track-partner and schedule them for removal
109  for (const CDCSegment2D& segment : segments) {
110 
111  // Skip segment already used in the steps before or marked outside as already taken.
112  if (segment->hasTakenFlag()) continue;
113 
114  // Add hit with destination track nullptr
115  for (const CDCRecoHit2D& recoHit : segment) {
116  recoHits3D.push_back({recoHit.getRLWireHit(), Vector3D(recoHit.getRecoPos2D()), 0});
117  trackHitRelations.push_back({nullptr, 0, &recoHits3D.back()});
118  }
119  }
120  }
121 
122  // 3. Add the original hit content of the track with lowest priority
123  for (CDCTrack& track : tracks) {
124  for (const CDCRecoHit3D& recoHit3D : track) {
125  recoHits3D.push_back(recoHit3D);
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  TrackFindingCDC::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 }
Belle2::Vector3D
HepGeom::Vector3D< double > Vector3D
3D Vector
Definition: Cell.h:35
Belle2::TrackFindingCDC::CDCRecoHit3D
Class representing a three dimensional reconstructed hit.
Definition: CDCRecoHit3D.h:62
Belle2::TrackFindingCDC::CDCTrack
Class representing a sequence of three dimensional reconstructed hits.
Definition: CDCTrack.h:51
Belle2::TrackFindingCDC::SegmentTrackAdderWithNormalization::exposeParameters
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) override
Expose the parameters of the sub-findlets.
Definition: SegmentTrackAdderWithNormalization.cc:31
Belle2::TrackFindingCDC::Findlet< WeightedRelation< CDCTrack, const CDCSegment2D > &, CDCTrack &, const CDCSegment2D >
Belle2::TrackFindingCDC::CDCTrajectory3D::setLocalOrigin
double setLocalOrigin(const Vector3D &localOrigin)
Setter for the origin of the local coordinate system.
Definition: CDCTrajectory3D.cc:369
Belle2::TrackFindingCDC::SegmentTrackAdderWithNormalization::m_singleHitSelector
SingleMatchSelector< CDCTrack, CDCRecoHit3D, HitComperator > m_singleHitSelector
The selector for finding the track each hit should belong to.
Definition: SegmentTrackAdderWithNormalization.h:72
Belle2::TrackFindingCDC::AutomatonCell::setTakenFlag
void setTakenFlag(bool setTo=true)
Sets the taken flag to the given value. Default value true.
Definition: AutomatonCell.h:234
Belle2::TrackFindingCDC::CDCRecoHit3D::getWireHit
const CDCWireHit & getWireHit() const
Getter for the wire hit.
Definition: CDCRecoHit3D.h:248
Belle2::TrackFindingCDC::CDCRecoHit3D::getArcLength2D
double getArcLength2D() const
Getter for the travel distance in the xy projection.
Definition: CDCRecoHit3D.h:380
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::addProcessingSignalListener
void addProcessingSignalListener(ProcessingSignalListener *psl)
Register a processing signal listener to be notified.
Definition: CompositeProcessingSignalListener.cc:57
Belle2::TrackFindingCDC::SegmentTrackAdderWithNormalization::m_param_removeUnmatchedSegments
bool m_param_removeUnmatchedSegments
Parameter : Swtich to remove hits in segments that have no matching track from all tracks.
Definition: SegmentTrackAdderWithNormalization.h:68
Belle2::ModuleParamList::addParameter
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Definition: ModuleParamList.templateDetails.h:38
Belle2::TrackFindingCDC::CDCRecoHit2D
Class representing a two dimensional reconstructed hit in the central drift chamber.
Definition: CDCRecoHit2D.h:57
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::TrackNormalizer::apply
void apply(std::vector< CDCTrack > &tracks) final
Fit the tracks.
Definition: TrackNormalizer.cc:25
Belle2::TrackFindingCDC::SegmentTrackAdderWithNormalization::m_trackNormalizer
TrackNormalizer m_trackNormalizer
Findlet for performing the normalization of the tracks afterwards.
Definition: SegmentTrackAdderWithNormalization.h:75
Belle2::TrackFindingCDC::SegmentTrackAdderWithNormalization::getDescription
std::string getDescription() override
Short description of the findlet.
Definition: SegmentTrackAdderWithNormalization.cc:41
Belle2::TrackFindingCDC::CDCSegment2D
A reconstructed sequence of two dimensional hits in one super layer.
Definition: CDCSegment2D.h:40
Belle2::TrackFindingCDC::WeightedRelation
Type for two related objects with a weight.
Definition: CDCSegment2D.h:36
Belle2::TrackFindingCDC::SegmentTrackAdderWithNormalization::apply
void apply(std::vector< WeightedRelation< CDCTrack, const CDCSegment2D >> &relations, std::vector< CDCTrack > &tracks, const std::vector< CDCSegment2D > &segment) override
Apply the findlet.
Definition: SegmentTrackAdderWithNormalization.cc:47
Belle2::TrackFindingCDC::CDCRecoHit3D::reconstruct
static CDCRecoHit3D reconstruct(const CDCRecoHit2D &recoHit2D, const CDCTrajectory2D &trajectory2D)
Reconstructs the three dimensional hit from the two dimensional and the two dimensional trajectory.
Definition: CDCRecoHit3D.cc:58
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::TrackFindingCDC::CDCTrajectory3D
Particle full three dimensional trajectory.
Definition: CDCTrajectory3D.h:47
Belle2::TrackFindingCDC::SegmentTrackAdderWithNormalization::SegmentTrackAdderWithNormalization
SegmentTrackAdderWithNormalization()
Constructor for registering the sub-findlets.
Definition: SegmentTrackAdderWithNormalization.cc:24