Belle II Software development
AxialTrackCreatorHitHough.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/AxialTrackCreatorHitHough.h>
9
10#include <tracking/trackFindingCDC/hough/perigee/AxialLegendreLeafProcessor.h>
11#include <tracking/trackFindingCDC/hough/perigee/AxialLegendreLeafProcessor.icc.h>
12#include <tracking/trackFindingCDC/hough/perigee/StandardBinSpec.h>
13
14#include <tracking/trackingUtilities/eventdata/tracks/CDCTrack.h>
15#include <tracking/trackingUtilities/eventdata/hits/CDCWireHit.h>
16
17#include <tracking/trackingUtilities/utilities/StringManipulation.h>
18
19#include <framework/core/ModuleParamList.templateDetails.h>
20
21using namespace Belle2;
22using namespace TrackFindingCDC;
23using namespace TrackingUtilities;
24
25#if 0
26namespace {
27 void saveBounds(std::vector<float> bounds, const std::string& fileName)
28 {
29 std::ofstream boundsFile;
30 boundsFile.open(fileName);
31 for (float bound : bounds) {
32 boundsFile << bound;
33 boundsFile << "\n";
34 }
35 boundsFile.close();
36 }
37
38 std::vector<float> loadBounds(const std::string& fileName)
39 {
40 std::vector<float> bounds;
41 std::ifstream boundsFile;
42 std::string boundLine;
43 boundsFile.open(fileName);
44 if (boundsFile.is_open()) {
45 while (std::getline(boundsFile, boundLine)) {
46 float bound = stof(boundLine);
47 bounds.push_back(bound);
48 }
49 boundsFile.close();
50 } else {
51 B2ERROR("Could not read bounds file");
52 }
53 return bounds;
54 }
55}
56#endif
57
59{
60 return "Generates axial tracks from hits using several increasingly relaxed hough space search over phi0 and curvature.";
61}
62
64 const std::string& prefix)
65{
66 // Parameters for the hough space
67 moduleParamList->addParameter(prefixed(prefix, "granularityLevel"),
69 "Level of divisions in the hough space.",
71
72 moduleParamList->addParameter(prefixed(prefix, "sectorLevelSkip"),
74 "Number of levels to be skipped in the hough "
75 "space on the first level to form sectors",
77
78 moduleParamList->addParameter(prefixed(prefix, "curvBounds"),
80 "Curvature bounds of the hough space. Either 2 or all discrete bounds",
82
83 moduleParamList->addParameter(prefixed(prefix, "discretePhi0Width"),
85 "Width of the phi0 bins at the lowest level of the hough space.",
87
88 moduleParamList->addParameter(prefixed(prefix, "discretePhi0Overlap"),
90 "Overlap of the phi0 bins at the lowest level of the hough space.",
92
93 moduleParamList->addParameter(prefixed(prefix, "discreteCurvWidth"),
95 "Width of the curvature bins at the lowest level of the hough space.",
97
98 moduleParamList->addParameter(prefixed(prefix, "discreteCurvOverlap"),
100 "Overlap of the curvature bins at the lowest level of the hough space.",
102
103 // Relaxation schedule
104 moduleParamList->addParameter(prefixed(prefix, "relaxationSchedule"),
106 "Relaxation schedule for the leaf processor in the hough tree. "
107 "For content of the individual parameter maps consider the parameters of the "
108 "AxialLegendreLeafProcessor",
110
111}
112
114{
116
117 // Construct the hough space
118 const long nPhi0Bins = std::pow(c_phi0Divisions, m_param_granularityLevel);
119 const Phi0BinsSpec phi0BinsSpec(nPhi0Bins,
122
123 if (m_param_curvBounds.size() == 2) {
124 // If parameters are unchanged use the legendre default binning
125 if (m_param_discreteCurvOverlap == -1) {
127 std::array<float, 2> curvSpan({m_param_curvBounds[0], m_param_curvBounds[1]});
129 } else {
130 std::array<double, 2> curvBounds{{m_param_curvBounds.front(), m_param_curvBounds.back()}};
131 const long nCurvBins = std::pow(c_curvDivisions, m_param_granularityLevel);
132 const CurvBinsSpec curvBinsSpec(curvBounds.front(),
133 curvBounds.back(),
134 nCurvBins,
137 m_param_curvBounds = curvBinsSpec.constructArray();
138 }
139 }
140
141 // Construct hough tree
143 m_houghTree = std::make_unique<SimpleRLTaggedWireHitPhi0CurvHough>(maxTreeLevel, m_curlCurv);
144 m_houghTree->setSectorLevelSkip(m_param_sectorLevelSkip);
145 m_houghTree->assignArray<DiscretePhi0>(phi0BinsSpec.constructArray(), phi0BinsSpec.getNOverlap());
147 m_houghTree->initialize();
148}
149
150void AxialTrackCreatorHitHough::apply(const std::vector<const CDCWireHit*>& axialWireHits,
151 std::vector<CDCTrack>& tracks)
152{
153 // Reset the mask flag and select only the untaken hits
154 std::vector<const CDCWireHit*> unusedAxialWireHits;
155 for (const CDCWireHit* wireHit : axialWireHits) {
156 (*wireHit)->setMaskedFlag(false);
157 if ((*wireHit)->hasTakenFlag()) continue;
158 unusedAxialWireHits.push_back(wireHit);
159 }
160
161 // Setup the level processor and obtain its parameter list to be set.
162 using Node = typename SimpleRLTaggedWireHitPhi0CurvHough::Node;
164 AxialLegendreLeafProcessor<Node> leafProcessor(maxTreeLevel);
165 leafProcessor.setAxialWireHits(axialWireHits);
166 ModuleParamList moduleParamList;
167 const std::string prefix = "";
168 leafProcessor.exposeParameters(&moduleParamList, prefix);
169
170 // Find tracks with increasingly relaxed conditions in the hough grid
171 m_houghTree->seed(std::move(unusedAxialWireHits));
172 for (const ParameterVariantMap& passParameters : m_param_relaxationSchedule) {
173 AssignParameterVisitor::update(&moduleParamList, passParameters);
174 leafProcessor.beginWalk();
175 m_houghTree->findUsing(leafProcessor);
176 }
177 m_houghTree->fell();
178
179 // Write out tracks as return value
180 const std::vector<CDCTrack>& foundTracks = leafProcessor.getTracks();
181 tracks.insert(tracks.end(), foundTracks.begin(), foundTracks.end());
182}
183
185{
186 m_houghTree->raze();
187 m_houghTree.reset();
189}
190
191std::vector<float> AxialTrackCreatorHitHough::getDefaultCurvBounds(std::array<float, 2> curvSpan, int granularityLevel)
192{
193 using BinSpan = std::array<float, 2>;
194 using BinSpans = std::vector<BinSpan>;
195 std::vector<BinSpans> binSpansByLevel(granularityLevel + 1);
196 binSpansByLevel[0].push_back(BinSpan({curvSpan[0], curvSpan[1]}));
197
198 for (int level = 1; level <= granularityLevel; ++level) {
199 for (const BinSpan& binSpan : binSpansByLevel[level - 1]) {
200 const float subBinWidth = std::fabs(binSpan[1] - binSpan[0]) / 2;
201 const float middle = binSpan[0] + (binSpan[1] - binSpan[0]) / 2.0;
202
203 // Expanding bins somewhat to have a overlap
204 // Assuming granularity level = 12
205 // For level 6 to 7 only expand 1 / 4, for higher levels expand 1 / 8.
206 // Never expand for curvatures lower than 0.005
207 // (copied from the Legendre method. Works well, but some experimentation
208 // needs to be made to know why)
209 const float extension = [&]() {
210 if ((level + 7 <= granularityLevel)
211 // or (std::fabs(middle) <= 0.007)
212 or (std::fabs(middle) <= 0.005)) {
213 return 0.0;
214 } else if (level + 5 < granularityLevel) {
215 return subBinWidth / 4.0;
216 } else {
217 return subBinWidth / 8.0;
218 }
219 }();
220
221 const float lower1 = binSpan[0] - extension;
222 const float upper1 = middle + extension;
223
224 const float lower2 = middle - extension;
225 const float upper2 = binSpan[1] + extension;
226
227 binSpansByLevel[level].push_back({lower1, upper1});
228 binSpansByLevel[level].push_back({lower2, upper2});
229 }
230 }
231
232 // Return highest level as prepared bin bounds.
233 std::vector<float> result;
234
235 for (BinSpan& binSpan : binSpansByLevel[granularityLevel]) {
236 result.push_back(binSpan[0]);
237 result.push_back(binSpan[1]);
238 }
239 return result;
240}
The Module parameter list class.
Predicate class that is feed the nodes in a WeightedHoughTree walk It decides if a node should be fur...
const std::vector< TrackingUtilities::CDCTrack > & getTracks() const
Getter for the tracks.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix)
Expose the parameters as a module parameter list.
void beginWalk()
Function to notify the leaf processor about changes in parameters before a new walk.
void setAxialWireHits(std::vector< const TrackingUtilities::CDCWireHit * > axialWireHits)
Set the pool of all axial wire hits to be used in the postprocessing.
void apply(const std::vector< const TrackingUtilities::CDCWireHit * > &axialWireHits, std::vector< TrackingUtilities::CDCTrack > &tracks) final
Generates the tracks from the given segments into the output argument.
const double m_curlCurv
Curvature below which particles from IP do not leave the CDC.
static const int c_curvDivisions
Fixed parameter: Number of divisions in the curv direction.
std::unique_ptr< SimpleRLTaggedWireHitPhi0CurvHough > m_houghTree
The hough space tree search.
void initialize() final
Initialize the findlet before event processing.
int m_param_sectorLevelSkip
Parameter: Number of levels to be skipped in the hough space on the first level to form sectors.
std::vector< TrackingUtilities::ParameterVariantMap > m_param_relaxationSchedule
Parameter: Relaxation schedule for the leaf processor in the hough tree.
std::string getDescription() final
Short description of the findlet.
int m_param_granularityLevel
Parameter: Level of divisions in the hough space.
int m_param_discretePhi0Width
Parameter: Width of the phi0 bins at the highest level of the hough space.
static std::vector< float > getDefaultCurvBounds(std::array< float, 2 > curvSpan, int granularityLevel)
Get a series of parameters to be set for each pass over the rough hough space.
std::vector< float > m_param_curvBounds
Parameter: hough bounds.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
int m_param_discreteCurvOverlap
Parameter: Overlap of the curvature bins at the highest level of the hough space.
static const int c_phi0Divisions
Fixed parameter: Number of divisions in the phi0 direction.
void terminate() final
Cleanup the findlet after event processing.
int m_param_discreteCurvWidth
Parameter: Width of the curvature bins at the highest level of the hough space.
int m_param_discretePhi0Overlap
Parameter: Overlap of the phi0 bins at the highest level of the hough space.
Strategy to construct discrete curve points from discrete overlap specifications.
Definition CurvRep.h:23
DiscreteCurv::Array constructArray() const
Construct the array of discrete curve positions.
Definition CurvRep.h:40
Strategy to construct discrete phi0 points from discrete overlap specifications.
Definition Phi0Rep.h:20
DiscretePhi0::Array constructArray() const
Construct the array of discrete phi0 positions.
Definition Phi0Rep.cc:24
int getNOverlap() const
Getter for the overlap in discrete number of positions.
Definition Phi0Rep.h:47
Class representing a hit wire in the central drift chamber.
Definition CDCWireHit.h:58
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Abstract base class for different kinds of events.
static void update(ModuleParamList *moduleParamList, const std::map< std::string, boost::variant< T... > > &valuesByName)
Transfer all the parameters from the map boost:variant values to the module parameter list.